From c0de4d3ac8e665dea0e0cff23f8dbf3cbf66ba4e Mon Sep 17 00:00:00 2001 From: Kei Kamikawa Date: Sun, 14 Apr 2024 22:24:04 +0900 Subject: [PATCH] fixed issue #51 --- cache_internal_test.go | 20 ++++++++++++++++++++ expiration.go | 1 + 2 files changed, 21 insertions(+) diff --git a/cache_internal_test.go b/cache_internal_test.go index 688a667..580b726 100644 --- a/cache_internal_test.go +++ b/cache_internal_test.go @@ -123,6 +123,26 @@ func TestDeleteExpired(t *testing.T) { t.Errorf("want2 %d items but got2 %d", want2, got2) } }) + + t.Run("issue #51", func(t *testing.T) { + defer restore() + c := New[string, int]() + + c.Set("1", 10, WithExpiration(10*time.Millisecond)) + c.Set("2", 20, WithExpiration(20*time.Millisecond)) + c.Set("1", 30, WithExpiration(100*time.Millisecond)) // expected do not expired key "1" + + nowFunc = func() time.Time { + return now.Add(30 * time.Millisecond).Add(time.Millisecond) + } + + c.DeleteExpired() + + got := c.Len() + if want := 1; want != got { + t.Errorf("want %d items but got %d", want, got) + } + }) } func max(x, y int) int { diff --git a/expiration.go b/expiration.go index 1ac5cc0..44ee4ea 100644 --- a/expiration.go +++ b/expiration.go @@ -21,6 +21,7 @@ func newExpirationManager[K comparable]() *expirationManager[K] { func (m *expirationManager[K]) update(key K, expiration time.Time) { if e, ok := m.mapping[key]; ok { + e.expiration = expiration heap.Fix(&m.queue, e.index) } else { v := &expirationKey[K]{