Skip to content

Commit

Permalink
Add fix to verify Keys are expired during Keys() call.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Oct 30, 2023
1 parent 0e92f33 commit 22f4401
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
13 changes: 11 additions & 2 deletions memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,18 @@ func (s *Storage) Keys() ([][]byte, error) {
return nil, nil
}

ts := atomic.LoadUint32(&internal.Timestamp)
keys := make([][]byte, 0, len(s.db))
for key := range s.db {
keys = append(keys, []byte(key))
for key, v := range s.db {
// Filter out the expired keys
if v.expiry == 0 || v.expiry > ts {
keys = append(keys, []byte(key))
}
}

// Double check if no valid keys were found
if len(keys) == 0 {
return nil, nil
}

return keys, nil
Expand Down
31 changes: 31 additions & 0 deletions memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ func Test_Storage_Memory_Set_Expiration(t *testing.T) {
require.Nil(t, keys)
}

func Test_Storage_Memory_Set_Long_Expiration_with_Keys(t *testing.T) {
var (
testStore = New()
key = "john"
val = []byte("doe")
exp = 5 * time.Second
)

keys, err := testStore.Keys()
require.NoError(t, err)
require.Nil(t, keys)

err = testStore.Set(key, val, exp)
require.NoError(t, err)

time.Sleep(1100 * time.Millisecond)

keys, err = testStore.Keys()
require.NoError(t, err)
require.Len(t, keys, 1)

time.Sleep(4000 * time.Millisecond)
result, err := testStore.Get(key)
require.NoError(t, err)
require.Zero(t, len(result))

keys, err = testStore.Keys()
require.NoError(t, err)
require.Nil(t, keys)
}

func Test_Storage_Memory_Get_NotExist(t *testing.T) {
testStore := New()
result, err := testStore.Get("notexist")
Expand Down

0 comments on commit 22f4401

Please sign in to comment.