fix stress test: it's not doing anything

This commit is contained in:
Mike Danese 2019-11-15 18:59:51 -08:00
parent 16e0976bd1
commit 7af4249edc
2 changed files with 15 additions and 7 deletions

View File

@ -102,8 +102,8 @@ func (c *Expiring) Set(key interface{}, val interface{}, ttl time.Duration) {
heap.Push(&c.heap, &expiringHeapEntry{
key: key,
generation: c.generation,
expiry: expiry,
generation: c.generation,
})
}
@ -158,13 +158,13 @@ func (c *Expiring) gc(now time.Time) {
type expiringHeapEntry struct {
key interface{}
generation uint64
expiry time.Time
generation uint64
}
// expiringHeap is a min-heap ordered by expiration time of it's entries. The
// expiring cache uses this as a priority queue efficiently organize entries to
// be garbage collected once they expire.
// expiringHeap is a min-heap ordered by expiration time of its entries. The
// expiring cache uses this as a priority queue to efficiently organize entries
// which will be garbage collected once they expire.
type expiringHeap []*expiringHeapEntry
var _ heap.Interface = &expiringHeap{}

View File

@ -264,6 +264,7 @@ func TestStressExpiringCache(t *testing.T) {
for i := 0; i < 256; i++ {
wg.Add(1)
go func() {
defer wg.Done()
rand := rand.New(rand.NewSource(rand.Int63()))
for {
select {
@ -273,11 +274,18 @@ func TestStressExpiringCache(t *testing.T) {
}
key := keys[rand.Intn(numKeys)]
if _, ok := cache.Get(key); !ok {
cache.Set(key, struct{}{}, time.Second)
cache.Set(key, struct{}{}, 50*time.Millisecond)
}
}
}()
}
wg.Done()
wg.Wait()
// trigger a GC with a set and check the cache size.
time.Sleep(60 * time.Millisecond)
cache.Set("trigger", "gc", time.Second)
if cache.Len() != 1 {
t.Errorf("unexpected cache size: got=%d, want=1", cache.Len())
}
}