Make test more robust

Attempt to fix: https://github.com/karlseguin/ccache/issues/90
This commit is contained in:
Karl Seguin
2023-11-23 09:48:42 +08:00
parent 6d135b03a9
commit 3aa6a053b7

View File

@@ -408,7 +408,7 @@ func Test_ConcurrentStop(t *testing.T) {
} }
func Test_ConcurrentClearAndSet(t *testing.T) { func Test_ConcurrentClearAndSet(t *testing.T) {
for i := 0; i < 100; i++ { for i := 0; i < 1000000; i++ {
var stop atomic.Bool var stop atomic.Bool
var wg sync.WaitGroup var wg sync.WaitGroup
@@ -424,20 +424,27 @@ func Test_ConcurrentClearAndSet(t *testing.T) {
cache.Clear() cache.Clear()
stop.Store(true) stop.Store(true)
wg.Wait() wg.Wait()
time.Sleep(time.Millisecond)
cache.SyncUpdates() cache.SyncUpdates()
known := make(map[string]struct{}) // The point of this test is to make sure that the cache's lookup and its
for node := cache.list.Head; node != nil; node = node.Next { // recency list are in sync. But the two aren't written to atomically:
known[node.Value.key] = struct{}{} // the lookup is written to directly from the call to Set, whereas the
} // list is maintained by the background worker. This can create a period
// where the two are out of sync. Even SyncUpdate is helpless here, since
for _, bucket := range cache.buckets { // it can only sync what's been written to the buffers.
for key := range bucket.lookup { for i := 0; i < 10; i++ {
_, exists := known[key] expectedCount := 0
assert.True(t, exists) if cache.list.Head != nil {
expectedCount = 1
} }
actualCount := cache.ItemCount()
if expectedCount == actualCount {
return
}
time.Sleep(time.Millisecond)
} }
t.Errorf("cache list and lookup are not consistent")
t.FailNow()
} }
} }