]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
deps: Upgrade github.com/bep/lazycache v0.6.0 => v0.7.0
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 27 Oct 2024 09:31:59 +0000 (10:31 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 27 Oct 2024 11:43:36 +0000 (12:43 +0100)
cache/dynacache/dynacache.go
cache/dynacache/dynacache_test.go
go.mod
go.sum

index 5007e27baacbf37fb0f16b822f84249d0455c570..a906a0dd31564dac378e14435c4d2e6fe0248c6e 100644 (file)
@@ -430,12 +430,25 @@ func (p *Partition[K, V]) doGetOrCreateWitTimeout(key K, duration time.Duration,
        errch := make(chan error, 1)
 
        go func() {
-               v, _, err := p.c.GetOrCreate(key, create)
-               if err != nil {
-                       errch <- err
-                       return
-               }
-               resultch <- v
+               var (
+                       v   V
+                       err error
+               )
+               defer func() {
+                       if r := recover(); r != nil {
+                               if rerr, ok := r.(error); ok {
+                                       err = rerr
+                               } else {
+                                       err = fmt.Errorf("panic: %v", r)
+                               }
+                       }
+                       if err != nil {
+                               errch <- err
+                       } else {
+                               resultch <- v
+                       }
+               }()
+               v, _, err = p.c.GetOrCreate(key, create)
        }()
 
        select {
index 9a932729b23cd1c46352743c60f35278436b8afd..87239479b86b0ef043d392691be70af7a3a4a4a3 100644 (file)
@@ -14,6 +14,7 @@
 package dynacache
 
 import (
+       "errors"
        "fmt"
        "path/filepath"
        "testing"
@@ -174,18 +175,47 @@ func TestPanicInCreate(t *testing.T) {
 
        p1 := GetOrCreatePartition[string, testItem](cache, "/aaaa/bbbb", OptionsPartition{Weight: 30, ClearWhen: ClearOnRebuild})
 
+       willPanic := func(i int) func() {
+               return func() {
+                       p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
+                               panic(errors.New(key))
+                       })
+               }
+       }
+
+       // GetOrCreateWitTimeout needs to recover from panics in the create func.
+       willErr := func(i int) error {
+               _, err := p1.GetOrCreateWitTimeout(fmt.Sprintf("error-%d", i), 10*time.Second, func(key string) (testItem, error) {
+                       return testItem{}, errors.New(key)
+               })
+               return err
+       }
+
        for i := 0; i < 3; i++ {
                for j := 0; j < 3; j++ {
-                       _, err := p1.GetOrCreate(fmt.Sprintf("panic1-%d", i), func(string) (testItem, error) {
-                               panic("failed")
-                       })
+                       c.Assert(willPanic(i), qt.PanicMatches, fmt.Sprintf("panic-%d", i))
+                       c.Assert(willErr(i), qt.ErrorMatches, fmt.Sprintf("error-%d", i))
+               }
+       }
 
-                       c.Assert(err, qt.Not(qt.IsNil))
+       // Test the same keys again without the panic.
+       for i := 0; i < 3; i++ {
+               for j := 0; j < 3; j++ {
+                       v, err := p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
+                               return testItem{
+                                       name: key,
+                               }, nil
+                       })
+                       c.Assert(err, qt.IsNil)
+                       c.Assert(v.name, qt.Equals, fmt.Sprintf("panic-%d", i))
 
-                       _, err = p1.GetOrCreateWitTimeout(fmt.Sprintf("panic2-%d", i), 10*time.Second, func(string) (testItem, error) {
-                               panic("failed")
+                       v, err = p1.GetOrCreateWitTimeout(fmt.Sprintf("error-%d", i), 10*time.Second, func(key string) (testItem, error) {
+                               return testItem{
+                                       name: key,
+                               }, nil
                        })
-                       c.Assert(err, qt.Not(qt.IsNil))
+                       c.Assert(err, qt.IsNil)
+                       c.Assert(v.name, qt.Equals, fmt.Sprintf("error-%d", i))
                }
        }
 }
diff --git a/go.mod b/go.mod
index 3f5d4a707f80b38864bc20d6073a54e0b2b44a10..108241dc5464706832e3a88bdb1de996d4fbdd52 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -16,7 +16,7 @@ require (
        github.com/bep/gowebp v0.3.0
        github.com/bep/helpers v0.5.0
        github.com/bep/imagemeta v0.8.1
-       github.com/bep/lazycache v0.6.0
+       github.com/bep/lazycache v0.7.0
        github.com/bep/logg v0.4.0
        github.com/bep/mclib v1.20400.20402
        github.com/bep/overlayfs v0.9.2
diff --git a/go.sum b/go.sum
index 6346fb099510fe1e1eb61dc6dcbf71539becd333..cfc34880b2be08025e4381668847532931763e84 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -145,6 +145,8 @@ github.com/bep/lazycache v0.4.0 h1:X8yVyWNVupPd4e1jV7efi3zb7ZV/qcjKQgIQ5aPbkYI=
 github.com/bep/lazycache v0.4.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
 github.com/bep/lazycache v0.6.0 h1:0vCgFo7TBtMQpSx64jnH1sagmw0ZougIFRpsqPHTa5U=
 github.com/bep/lazycache v0.6.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
+github.com/bep/lazycache v0.7.0 h1:VM257SkkjcR9z55eslXTkUIX8QMNKoqQRNKV/4xIkCY=
+github.com/bep/lazycache v0.7.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
 github.com/bep/logg v0.4.0 h1:luAo5mO4ZkhA5M1iDVDqDqnBBnlHjmtZF6VAyTp+nCQ=
 github.com/bep/logg v0.4.0/go.mod h1:Ccp9yP3wbR1mm++Kpxet91hAZBEQgmWgFgnXX3GkIV0=
 github.com/bep/mclib v1.20400.20402 h1:olpCE2WSPpOAbFE1R4hnftSEmQ34+xzy2HRzd0m69rA=