* Add periodic cleanup job for DecayMap see https://github.com/TecharoHQ/anubis/issues/8 * Refactor: Improve DecayMap cleanup tests and add Len method - Refactored DecayMap cleanup tests to use the new Len method for more precise assertions. - Added a Len method to DecayMap to retrieve the number of entries. - Simplified conditional checks in Get method. * chore(changelog): add entry * fix(tests): Use Impl.expire for decaymap cleanup Signed-off-by: Jason Cameron <git@jasoncameron.dev> --------- Signed-off-by: Jason Cameron <git@jasoncameron.dev>
This commit is contained in:
parent
052316ba25
commit
0f41388bd7
5 changed files with 70 additions and 1 deletions
|
|
@ -29,3 +29,32 @@ func TestImpl(t *testing.T) {
|
|||
t.Error("got value even though it was supposed to be expired")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanup(t *testing.T) {
|
||||
dm := New[string, string]()
|
||||
|
||||
dm.Set("test1", "hi1", 1*time.Second)
|
||||
dm.Set("test2", "hi2", 2*time.Second)
|
||||
dm.Set("test3", "hi3", 3*time.Second)
|
||||
|
||||
dm.expire("test1") // Force expire test1
|
||||
dm.expire("test2") // Force expire test2
|
||||
|
||||
dm.Cleanup()
|
||||
|
||||
finalLen := dm.Len() // Get the length after cleanup
|
||||
|
||||
if finalLen != 1 { // "test3" should be the only one left
|
||||
t.Errorf("Cleanup failed to remove expired entries. Expected length 1, got %d", finalLen)
|
||||
}
|
||||
|
||||
if _, ok := dm.Get("test1"); ok { // Verify Get still behaves correctly after Cleanup
|
||||
t.Error("test1 should not be found after cleanup")
|
||||
}
|
||||
if _, ok := dm.Get("test2"); ok {
|
||||
t.Error("test2 should not be found after cleanup")
|
||||
}
|
||||
if val, ok := dm.Get("test3"); !ok || val != "hi3" {
|
||||
t.Error("test3 should still be found after cleanup")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue