diff --git a/flytestdlib/cache/auto_refresh.go b/flytestdlib/cache/auto_refresh.go index 15765d7d5c..d8a71dc2eb 100644 --- a/flytestdlib/cache/auto_refresh.go +++ b/flytestdlib/cache/auto_refresh.go @@ -175,7 +175,7 @@ func (w *autoRefresh) Start(ctx context.Context) error { return nil } -// Update updates the item in the cache only if it exists, return true if we updated the item. +// Update updates the item only if it exists in the cache, return true if we updated the item. func (w *autoRefresh) Update(id ItemID, item Item) (ok bool) { w.lock.Lock() ok = w.lruMap.Contains(id) @@ -186,6 +186,7 @@ func (w *autoRefresh) Update(id ItemID, item Item) (ok bool) { return ok } +// Delete deletes the item from the cache if it exists. func (w *autoRefresh) Delete(key interface{}) { w.lock.Lock() w.toDelete.Remove(key) diff --git a/flytestdlib/cache/auto_refresh_test.go b/flytestdlib/cache/auto_refresh_test.go index e1250afd68..ccd2da6a19 100644 --- a/flytestdlib/cache/auto_refresh_test.go +++ b/flytestdlib/cache/auto_refresh_test.go @@ -62,7 +62,7 @@ func syncTerminalItem(_ context.Context, batch Batch) ([]ItemSyncResponse, error panic("This should never be called") } -func TestCacheThree(t *testing.T) { +func TestCacheFour(t *testing.T) { testResyncPeriod := time.Millisecond rateLimiter := workqueue.DefaultControllerRateLimiter() @@ -142,6 +142,34 @@ func TestCacheThree(t *testing.T) { cancel() }) + + t.Run("Test update and delete cache", func(t *testing.T) { + cache, err := NewAutoRefreshCache("fake3", syncTerminalItem, rateLimiter, testResyncPeriod, 10, 2, promutils.NewTestScope()) + assert.NoError(t, err) + + ctx, cancel := context.WithCancel(context.Background()) + assert.NoError(t, cache.Start(ctx)) + + itemID := "dummy_id" + _, err = cache.GetOrCreate(itemID, terminalCacheItem{ + val: 0, + }) + assert.NoError(t, err) + + // Wait half a second for all resync periods to complete + // If the cache tries to enqueue the item, a panic will be thrown. + time.Sleep(500 * time.Millisecond) + + err = cache.DeleteDelayed(itemID) + assert.NoError(t, err) + + time.Sleep(500 * time.Millisecond) + item, err := cache.Get(itemID) + assert.Nil(t, item) + assert.Error(t, err) + + cancel() + }) } func TestQueueBuildUp(t *testing.T) {