test(decaymap): fix tests

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2025-12-28 18:46:32 -05:00
parent f56c242167
commit f2cde94b71
4 changed files with 11 additions and 27 deletions
+9 -12
View File
@@ -64,22 +64,19 @@ func (m *Impl[K, V]) expire(key K) bool {
// Delete a value from the DecayMap by key.
//
// This defers deletions to a background thread for performance reasons.
//
// If the value does not exist, return false. Return true after
// deletion.
func (m *Impl[K, V]) Delete(key K) bool {
// Use a single write lock to avoid RUnlock->Lock convoy.
m.lock.Lock()
defer m.lock.Unlock()
value, ok := m.data[key]
if ok {
select {
// Defer decay deletion to the background worker to avoid convoy.
case m.deleteCh <- deleteReq[K]{key: key, expiry: value.expiry}:
default:
// Channel full: drop request; a future Cleanup() or Get will retry.
}
select {
// Defer decay deletion to the background worker to avoid convoy.
case m.deleteCh <- deleteReq[K]{key: key, expiry: time.Now().Add(-1 * time.Second)}:
return m.expire(key)
default:
// Channel full: drop request; a future Cleanup() or Get will retry.
return true
}
return ok
}
// Get gets a value from the DecayMap by key.
+1 -9
View File
@@ -30,15 +30,7 @@ func TestImpl(t *testing.T) {
t.Error("got value even though it was supposed to be expired")
}
// Deletion of expired entries after Get is deferred to a background worker.
// Assert it eventually disappears from the map.
deadline := time.Now().Add(200 * time.Millisecond)
for time.Now().Before(deadline) {
if dm.Len() == 0 {
break
}
time.Sleep(5 * time.Millisecond)
}
dm.Cleanup()
if dm.Len() != 0 {
t.Fatalf("expected background cleanup to remove expired key; len=%d", dm.Len())
}