Skip to content

Commit 3aa6a05

Browse files
committed
Make test more robust
Attempt to fix: #90
1 parent 6d135b0 commit 3aa6a05

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

cache_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func Test_ConcurrentStop(t *testing.T) {
408408
}
409409

410410
func Test_ConcurrentClearAndSet(t *testing.T) {
411-
for i := 0; i < 100; i++ {
411+
for i := 0; i < 1000000; i++ {
412412
var stop atomic.Bool
413413
var wg sync.WaitGroup
414414

@@ -424,20 +424,27 @@ func Test_ConcurrentClearAndSet(t *testing.T) {
424424
cache.Clear()
425425
stop.Store(true)
426426
wg.Wait()
427-
time.Sleep(time.Millisecond)
428427
cache.SyncUpdates()
429428

430-
known := make(map[string]struct{})
431-
for node := cache.list.Head; node != nil; node = node.Next {
432-
known[node.Value.key] = struct{}{}
433-
}
434-
435-
for _, bucket := range cache.buckets {
436-
for key := range bucket.lookup {
437-
_, exists := known[key]
438-
assert.True(t, exists)
429+
// The point of this test is to make sure that the cache's lookup and its
430+
// recency list are in sync. But the two aren't written to atomically:
431+
// the lookup is written to directly from the call to Set, whereas the
432+
// list is maintained by the background worker. This can create a period
433+
// where the two are out of sync. Even SyncUpdate is helpless here, since
434+
// it can only sync what's been written to the buffers.
435+
for i := 0; i < 10; i++ {
436+
expectedCount := 0
437+
if cache.list.Head != nil {
438+
expectedCount = 1
439+
}
440+
actualCount := cache.ItemCount()
441+
if expectedCount == actualCount {
442+
return
439443
}
444+
time.Sleep(time.Millisecond)
440445
}
446+
t.Errorf("cache list and lookup are not consistent")
447+
t.FailNow()
441448
}
442449
}
443450

0 commit comments

Comments
 (0)