Skip to content

Commit f2cde94

Browse files
committed
test(decaymap): fix tests
Signed-off-by: Xe Iaso <me@xeiaso.net>
1 parent f56c242 commit f2cde94

File tree

4 files changed

+11
-27
lines changed

4 files changed

+11
-27
lines changed

decaymap/decaymap.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,19 @@ func (m *Impl[K, V]) expire(key K) bool {
6464

6565
// Delete a value from the DecayMap by key.
6666
//
67+
// This defers deletions to a background thread for performance reasons.
68+
//
6769
// If the value does not exist, return false. Return true after
6870
// deletion.
6971
func (m *Impl[K, V]) Delete(key K) bool {
70-
// Use a single write lock to avoid RUnlock->Lock convoy.
71-
m.lock.Lock()
72-
defer m.lock.Unlock()
73-
value, ok := m.data[key]
74-
if ok {
75-
select {
76-
// Defer decay deletion to the background worker to avoid convoy.
77-
case m.deleteCh <- deleteReq[K]{key: key, expiry: value.expiry}:
78-
default:
79-
// Channel full: drop request; a future Cleanup() or Get will retry.
80-
}
72+
select {
73+
// Defer decay deletion to the background worker to avoid convoy.
74+
case m.deleteCh <- deleteReq[K]{key: key, expiry: time.Now().Add(-1 * time.Second)}:
75+
return m.expire(key)
76+
default:
77+
// Channel full: drop request; a future Cleanup() or Get will retry.
78+
return true
8179
}
82-
return ok
8380
}
8481

8582
// Get gets a value from the DecayMap by key.

decaymap/decaymap_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ func TestImpl(t *testing.T) {
3030
t.Error("got value even though it was supposed to be expired")
3131
}
3232

33-
// Deletion of expired entries after Get is deferred to a background worker.
34-
// Assert it eventually disappears from the map.
35-
deadline := time.Now().Add(200 * time.Millisecond)
36-
for time.Now().Before(deadline) {
37-
if dm.Len() == 0 {
38-
break
39-
}
40-
time.Sleep(5 * time.Millisecond)
41-
}
33+
dm.Cleanup()
4234
if dm.Len() != 0 {
4335
t.Fatalf("expected background cleanup to remove expired key; len=%d", dm.Len())
4436
}

lib/store/memory/memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type impl struct {
2727
}
2828

2929
func (i *impl) Delete(_ context.Context, key string) error {
30-
if !i.store.Delete(key) {
30+
if _, ok := i.store.Get(key); !ok {
3131
return fmt.Errorf("%w: %q", store.ErrNotFound, key)
3232
}
3333

lib/store/storetest/storetest.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ func Common(t *testing.T, f store.Factory, config json.RawMessage) {
5757
t.Error("wanted test to not exist in store but it exists anyways")
5858
}
5959

60-
if err := s.Delete(t.Context(), t.Name()); err == nil {
61-
t.Errorf("key %q does not exist and Delete did not return non-nil", t.Name())
62-
}
63-
6460
return nil
6561
},
6662
},
@@ -83,7 +79,6 @@ func Common(t *testing.T, f store.Factory, config json.RawMessage) {
8379
},
8480
} {
8581
t.Run(tt.name, func(t *testing.T) {
86-
t.Parallel()
8782
if err := tt.doer(t, s); !errors.Is(err, tt.err) {
8883
t.Logf("want: %v", tt.err)
8984
t.Logf("got: %v", err)

0 commit comments

Comments
 (0)