Skip to content

Commit 7f14c6a

Browse files
Ubuntuclaude
authored andcommitted
test(ttlcache): de-flake TestCache_Expiry timing under parallel CI load
The t.Parallel test used TTL=10ms with a 20ms post-TTL sleep, a margin too tight when the scheduler pauses the goroutine under CI load. Raise the TTL to 50ms and sleep 5x past it (named const), keeping the test fast (~250ms) while removing the nondeterminism Copilot flagged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4d75c2e commit 7f14c6a

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

internal/common/ttlcache/cache_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,23 @@ func TestCache(t *testing.T) {
8484
}
8585

8686
// TestCache_Expiry verifies that a cached entry is considered a miss after
87-
// its TTL has elapsed. We use a very small TTL and sleep past it.
87+
// its TTL has elapsed. The TTL is kept small for speed but the post-TTL sleep
88+
// uses a generous multiple of it so the test stays deterministic under CI load
89+
// (where this t.Parallel test can be paused between statements).
8890
func TestCache_Expiry(t *testing.T) {
8991
t.Parallel()
92+
const ttl = 50 * time.Millisecond
9093
var c Cache[int]
91-
c.SetTTL(10 * time.Millisecond)
94+
c.SetTTL(ttl)
9295
c.Set("x", 42)
9396

9497
// Confirm hit immediately.
9598
if _, ok := c.Get("x"); !ok {
9699
t.Fatal("Get returned miss immediately after Set; want hit")
97100
}
98101

99-
// Sleep past TTL.
100-
time.Sleep(20 * time.Millisecond)
102+
// Sleep well past the TTL (5x) to avoid flakiness under scheduler pressure.
103+
time.Sleep(5 * ttl)
101104

102105
if _, ok := c.Get("x"); ok {
103106
t.Error("Get returned hit after TTL expired; want miss")

0 commit comments

Comments
 (0)