| index a710f1717..23b8c2aa0 100644 |
| --- a/internal/edge/burst_test.go |
| +++ b/internal/edge/burst_test.go |
| @@ -3,6 +3,7 @@ package edge |
| import ( |
| "net/netip" |
| "sync" |
| + "sync/atomic" |
| "testing" |
| "time" |
| ) |
| @@ -284,20 +285,99 @@ func TestBurstBoundsEvictOldest(t *testing.T) { |
| func TestBurstExpiredEntriesAreDroppedBeforeEvicting(t *testing.T) { |
| lowerBurstCaps(t, 2, 2) |
| b, c := burstTestLimiter() |
| - b.note("bab/shop", burstClientA) |
| - b.apply(burstClientA, c.now().Add(time.Minute)) |
| + // Fill both caps with entries that then expire. The counters and the |
| + // cool-downs use different clients: apply drops a client's counters. |
| + counterClients := []netip.Prefix{ |
| + netip.MustParsePrefix("198.51.100.1/32"), |
| + netip.MustParsePrefix("198.51.100.2/32"), |
| + } |
| + coolClients := []netip.Prefix{ |
| + netip.MustParsePrefix("198.51.100.3/32"), |
| + netip.MustParsePrefix("198.51.100.4/32"), |
| + } |
| + for _, cl := range counterClients { |
| + b.note("bab/shop", cl) |
| + } |
| + for _, cl := range coolClients { |
| + if !b.apply(cl, c.now().Add(time.Minute)) { |
| + t.Fatal("apply refused") |
| + } |
| + } |
| + if b.counterCount() != 2 || b.cooldownCount() != 2 { |
| + t.Fatalf("setup: %d counters, %d cool-downs, want 2 and 2", b.counterCount(), b.cooldownCount()) |
| + } |
| c.advance(burstCooldown) |
| - // Both held entries are long expired: a new one needs no eviction. |
| - other := netip.MustParsePrefix("198.51.100.9/32") |
| - b.note("bab/shop", other) |
| - b.note("bab/shop", burstClientB) |
| - b.apply(other, c.now().Add(time.Minute)) |
| - b.apply(burstClientB, c.now().Add(time.Minute)) |
| + // A new counter and a new cool-down fit only because the expired ones go. |
| + b.note("bab/shop", burstClientA) |
| + if !b.apply(burstClientB, c.now().Add(time.Minute)) { |
| + t.Fatal("apply refused") |
| + } |
| + if n := b.counterCount(); n != 1 { |
| + t.Fatalf("counters = %d, want the 2 expired gone and 1 new", n) |
| + } |
| + if n := b.cooldownCount(); n != 1 { |
| + t.Fatalf("cool-downs = %d, want the 2 ended gone and 1 new", n) |
| + } |
| if s := b.stats(); s.CountersEvicted != 0 || s.CooldownsEvicted != 0 { |
| t.Fatalf("expired entries were counted as evictions: %+v", s) |
| } |
| } |
| |
| +// 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) { |
| + 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) |
| + } |
| + }) |
| + for i := 0; i < 58; i++ { |
| + b.note("bab/shop", 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) |
| + } |
| + if _, _, ok := b.cooling(burstClientA); ok { |
| + t.Fatal("client is cooling though no 60 s window held 60 notes") |
| + } |
| +} |
| + |
| func TestBurstSingleKeyFloodBounded(t *testing.T) { |
| b, c := burstTestLimiter() |
| // A flood that never lets 60 land inside a window: one note per 2 s. |