Repository: bacalhau-project/bacalhau (language: python)

Issue:
Bad timer parameter to cache causes flaky tests
    base.go:115: Command to execute: [--api-host 127.0.0.1 --api-port 43619 cancel efc0908a-010a-4bc6-b7d2-eb648e6c9bbb]
panic: non-positive interval for NewTicker

goroutine 489 [running]:
time.NewTicker(0x4000aee738?)
	/usr/local/go/src/time/tick.go:24 +0x110
github.com/bacalhau-project/bacalhau/pkg/cache/basic.(*BasicCache[...]).cleanup(0x3a38b00, 0x4000f4ad80)
	/home/circleci/repo/pkg/cache/basic/cache.go:98 +0x68
created by github.com/bacalhau-project/bacalhau/pkg/cache/basic.NewCache[...]
	/home/circleci/repo/pkg/cache/basic/cache.go:53 +0x24c


Discussion / hints:

Fix patch:
diff --git a/pkg/cache/basic/cache.go b/pkg/cache/basic/cache.go
index 981db9305c..35efd1f998 100644
--- a/pkg/cache/basic/cache.go
+++ b/pkg/cache/basic/cache.go
@@ -11,6 +11,7 @@ import (
 const (
 	DefaultTTLSeconds = int64(3600)
 	DefaultMaxCost    = uint64(1000)
+	DefaultFrequency  = time.Hour
 )
 
 type BasicCache[T any] struct {
@@ -31,7 +32,7 @@ func NewCache[T any](options ...Option) (*BasicCache[T], error) {
 	// initialize config with default values (these could be constants).
 	config := &Config{
 		maxCost:          DefaultMaxCost,
-		cleanupFrequency: time.Hour,
+		cleanupFrequency: DefaultFrequency,
 		evictionFunction: func(key string, cost uint64, expiresAt int64, now int64) bool {
 			return expiresAt != 0 && expiresAt <= now
 		},
@@ -50,6 +51,10 @@ func NewCache[T any](options ...Option) (*BasicCache[T], error) {
 		defaultTTL:       config.defaultTTL,
 	}
 
+	if config.cleanupFrequency == 0 {
+		config.cleanupFrequency = DefaultFrequency
+	}
+
 	go c.cleanup(config.cleanupFrequency)
 	return c, nil
 }


Test patch:
diff --git a/pkg/cache/basic/cache_test.go b/pkg/cache/basic/cache_test.go
index 626c8ab643..fe932248a6 100644
--- a/pkg/cache/basic/cache_test.go
+++ b/pkg/cache/basic/cache_test.go
@@ -110,7 +110,7 @@ func (s *BasicCacheSuite) TestExpiry() {
 		return willEvict
 	}
 
-	c, err := s.createTestCache("TestExpiry", 1, 1, f)
+	c, err := s.createTestCache("TestExpiry", 1, 100*time.Millisecond, f)
 	require.NoError(s.T(), err)
 	defer c.Close()
 
@@ -138,7 +138,7 @@ func (s *BasicCacheSuite) TestExpiryDefaultTTL() {
 		return false
 	}
 
-	c, err := s.createTestCacheWithDefaultTTL("TestExpiry", 1, 1, 1*time.Second, f)
+	c, err := s.createTestCacheWithDefaultTTL("TestExpiry", 1, 100*time.Millisecond, 1*time.Second, f)
 	require.NoError(s.T(), err)
 	defer c.Close()