edge: burst clock-under-lock test asserts the mutex, no timeouts
The reordered-calls test released its stalled call after 200 ms, so a slow second call could let the old code pass. The clock now checks with TryLock that the limiter's mutex is held on every read (note, apply, cooling).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+26
−47
internal/edge/burst_test.go+26 −47
diff --git a/internal/edge/burst_test.go b/internal/edge/burst_test.go| index 23b8c2aa0..18152ccc6 100644 |
| --- a/internal/edge/burst_test.go |
| +++ b/internal/edge/burst_test.go |
| @@ -323,58 +323,37 @@ func TestBurstExpiredEntriesAreDroppedBeforeEvicting(t *testing.T) { |
| } |
| } |
| -// Time is read under the lock, so calls are recorded in the order their times |
| -// were read. Before that, a call could read 59.99 s, stall, and record after |
| -// another had recorded 60.01 s; the ring then held 60 notes that no window of |
| -// 60 s contained, and tripped. The clock below stalls the first call after |
| -// the arm point until the second call has finished, or 200 ms pass: with the |
| -// clock read outside the lock the second call finishes at once and the first |
| -// trips falsely; under the lock the second call waits its turn. |
| -func TestBurstReorderedCallsNeverTripFalsely(t *testing.T) { |
| +// Time must be read under the limiter's mutex. A ring assumes its timestamps |
| +// are in time order, and a call that read the clock and then waited for the |
| +// lock could record an earlier time after a later one, tripping on 60 notes |
| +// that no 60 s window held. This asserts the cause directly instead of racing |
| +// goroutines: the clock checks, on every call, that the mutex is held. |
| +func TestBurstReadsTheClockUnderTheLock(t *testing.T) { |
| base := newBurstClock() |
| - var ( |
| - armed atomic.Bool |
| - calls atomic.Int32 |
| - sampled = make(chan struct{}) |
| - secondEnd = make(chan struct{}) |
| - ) |
| - b := newBurstLimiter(func() time.Time { |
| - if !armed.Load() { |
| - return base.now() |
| - } |
| - switch calls.Add(1) { |
| - case 1: |
| - close(sampled) |
| - select { |
| - case <-secondEnd: |
| - case <-time.After(200 * time.Millisecond): |
| - } |
| - return base.now().Add(59*time.Second + 990*time.Millisecond) |
| - default: |
| - return base.now().Add(60*time.Second + 10*time.Millisecond) |
| + var b *burstLimiter |
| + var reads, unlocked atomic.Int32 |
| + b = newBurstLimiter(func() time.Time { |
| + reads.Add(1) |
| + // TryLock succeeds only if nobody holds the mutex: that is the fault. |
| + if b.mu.TryLock() { |
| + b.mu.Unlock() |
| + unlocked.Add(1) |
| } |
| + return base.now() |
| }) |
| - for i := 0; i < 58; i++ { |
| - b.note("bab/shop", burstClientA) |
| + b.note("bab/shop", burstClientA) // counts |
| + b.note("bab/shop", burstClientA) // counts again, same counter |
| + b.apply(burstClientB, base.now().Add(time.Minute)) |
| + b.cooling(burstClientB) |
| + for i := 0; i < 60; i++ { // trip, then notes during the cool-down |
| + b.note("bdk/other", burstClientA) |
| } |
| - armed.Store(true) |
| - |
| - var first, second bool |
| - var wg sync.WaitGroup |
| - wg.Add(1) |
| - go func() { |
| - defer wg.Done() |
| - first, _ = b.note("bab/shop", burstClientA) |
| - }() |
| - <-sampled |
| - second, _ = b.note("bab/shop", burstClientA) |
| - close(secondEnd) |
| - wg.Wait() |
| - if first || second { |
| - t.Fatalf("tripped (first %v, second %v) though the 58 earlier notes are more than 60 s from the last", first, second) |
| + b.note("bab/shop", burstClientB) // no-op while cooling |
| + if reads.Load() == 0 { |
| + t.Fatal("the clock was never read") |
| } |
| - if _, _, ok := b.cooling(burstClientA); ok { |
| - t.Fatal("client is cooling though no 60 s window held 60 notes") |
| + if n := unlocked.Load(); n != 0 { |
| + t.Fatalf("the clock was read %d times (of %d) without the limiter's mutex held", n, reads.Load()) |
| } |
| } |