Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Su <[email protected]>
  • Loading branch information
pingsutw committed Oct 3, 2023
1 parent a130ff6 commit 185cf47
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion flytestdlib/cache/auto_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
30 changes: 29 additions & 1 deletion flytestdlib/cache/auto_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 185cf47

Please sign in to comment.