Skip to content

Commit

Permalink
fix(compute): flakey cache test (#15520)
Browse files Browse the repository at this point in the history
  • Loading branch information
MStreet3 authored Dec 10, 2024
1 parent c1341a5 commit 01c05a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 6 additions & 5 deletions core/capabilities/compute/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type moduleCache struct {
timeout time.Duration
evictAfterSize int

clock clockwork.Clock
onReaper chan struct{}
clock clockwork.Clock
reapTicker <-chan time.Time
onReaper chan struct{}
}

func newModuleCache(clock clockwork.Clock, tick, timeout time.Duration, evictAfterSize int) *moduleCache {
Expand All @@ -49,6 +50,7 @@ func newModuleCache(clock clockwork.Clock, tick, timeout time.Duration, evictAft
timeout: timeout,
evictAfterSize: evictAfterSize,
clock: clock,
reapTicker: clock.NewTicker(tick).Chan(),
stopChan: make(chan struct{}),
}
}
Expand All @@ -67,10 +69,9 @@ func (mc *moduleCache) close() {
}

func (mc *moduleCache) reapLoop() {
ticker := mc.clock.NewTicker(mc.tickInterval)
for {
select {
case <-ticker.Chan():
case <-mc.reapTicker:
mc.evictOlderThan(mc.timeout)
if mc.onReaper != nil {
mc.onReaper <- struct{}{}
Expand All @@ -84,7 +85,7 @@ func (mc *moduleCache) reapLoop() {
func (mc *moduleCache) add(id string, mod *module) {
mc.mu.Lock()
defer mc.mu.Unlock()
mod.lastFetchedAt = time.Now()
mod.lastFetchedAt = mc.clock.Now()
mc.m[id] = mod
moduleCacheAddition.Inc()
}
Expand Down
9 changes: 9 additions & 0 deletions core/capabilities/compute/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ const (
binaryCmd = "core/capabilities/compute/test/simple/cmd"
)

// Verify that cache evicts an expired module.
func TestCache(t *testing.T) {
t.Parallel()
clock := clockwork.NewFakeClock()
tick := 1 * time.Second
timeout := 1 * time.Second
reapTicker := make(chan time.Time)

cache := newModuleCache(clock, tick, timeout, 0)
cache.onReaper = make(chan struct{}, 1)
cache.reapTicker = reapTicker
cache.start()
defer cache.close()

Expand All @@ -50,20 +53,24 @@ func TestCache(t *testing.T) {
assert.Equal(t, got, mod)

clock.Advance(15 * time.Second)
reapTicker <- time.Now()
<-cache.onReaper
_, ok = cache.get(id)
assert.False(t, ok)
}

// Verify that an expired module is not evicted because evictAfterSize is 1
func TestCache_EvictAfterSize(t *testing.T) {
t.Parallel()
ctx := tests.Context(t)
clock := clockwork.NewFakeClock()
tick := 1 * time.Second
timeout := 1 * time.Second
reapTicker := make(chan time.Time)

cache := newModuleCache(clock, tick, timeout, 1)
cache.onReaper = make(chan struct{}, 1)
cache.reapTicker = reapTicker
cache.start()
defer cache.close()

Expand All @@ -79,13 +86,15 @@ func TestCache_EvictAfterSize(t *testing.T) {
module: hmod,
}
cache.add(id, mod)
assert.Len(t, cache.m, 1)

got, ok := cache.get(id)
assert.True(t, ok)

assert.Equal(t, got, mod)

clock.Advance(15 * time.Second)
reapTicker <- time.Now()
select {
case <-ctx.Done():
return
Expand Down

0 comments on commit 01c05a9

Please sign in to comment.