Skip to content

Commit

Permalink
add test for update of expired items
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Yarmak <vladislav-ex-src@vm-0.com>
  • Loading branch information
Snawoot authored and swithek committed Jun 11, 2024
1 parent d62194a commit 9ca4fc0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,36 @@ func Test_Cache_set(t *testing.T) {
}
})
}

// finally, test proper expiration queue handling on expired item update.
// recreate situation when expired item gets updated
// and not auto-cleaned up yet.
c := New[string, struct{}](
WithDisableTouchOnHit[string,struct{}](),
)

// insert an item and let it expire
c.Set("test", struct{}{}, 1)
time.Sleep(50*time.Millisecond)

// update the expired item
updatedItem := c.Set("test", struct{}{}, 100*time.Millisecond)

// eviction should not happen as we prolonged element
cl := c.OnEviction(func(_ context.Context, _ EvictionReason, item *Item[string, struct{}]){
t.Errorf("eviction happened even though item has not expired yet: key=%s, evicted item expiresAt=%s, updated item expiresAt=%s, now=%s",
item.Key(),
item.ExpiresAt().String(),
updatedItem.ExpiresAt().String(),
time.Now().String())
})
// start of automatic cleanup process is delayed to allow update win the race
// and update expired before its removal
go c.Start()

time.Sleep(90*time.Millisecond)
cl()
c.Stop()
}

func Test_Cache_get(t *testing.T) {
Expand Down

0 comments on commit 9ca4fc0

Please sign in to comment.