Debian Codegraph @

bits_test.go

raw
1 package nebula
2
3 import (
4 "testing"
5
6 "github.com/slackhq/nebula/test"
7 "github.com/stretchr/testify/assert"
8 )
9
10 // snapshot returns the bitmap as a []bool of length b.length, for readable
11 // test assertions against the now-packed []uint64 storage.
12 func (b *Bits) snapshot() []bool {
13 out := make([]bool, b.length)
14 for i := uint64(0); i < b.length; i++ {
15 out[i] = b.get(i)
16 }
17 return out
18 }
19
20 func TestBitsRequiresPowerOfTwo(t *testing.T) {
21 assert.Panics(t, func() { NewBits(10) })
22 assert.Panics(t, func() { NewBits(0) })
23 assert.NotPanics(t, func() { NewBits(1) })
24 assert.NotPanics(t, func() { NewBits(16) })
25 assert.NotPanics(t, func() { NewBits(1024) })
26 assert.NotPanics(t, func() { NewBits(16384) })
27 }
28
29 func TestBits(t *testing.T) {
30 l := test.NewLogger()
31 b := NewBits(16)
32 assert.EqualValues(t, 16, b.length)
33
34 // This is initialized to zero - receive one. This should work.
35 assert.True(t, b.Check(l, 1))
36 assert.True(t, b.Update(l, 1))
37 assert.EqualValues(t, 1, b.current)
38 g := []bool{true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false}
39 assert.Equal(t, g, b.snapshot())
40
41 // Receive two
42 assert.True(t, b.Check(l, 2))
43 assert.True(t, b.Update(l, 2))
44 assert.EqualValues(t, 2, b.current)
45 g = []bool{true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false}
46 assert.Equal(t, g, b.snapshot())
47
48 // Receive two again - it will fail
49 assert.False(t, b.Check(l, 2))
50 assert.False(t, b.Update(l, 2))
51 assert.EqualValues(t, 2, b.current)
52
53 // Jump ahead to 25, which clears the window and sets slot 25%16 = 9.
54 assert.True(t, b.Check(l, 25))
55 assert.True(t, b.Update(l, 25))
56 assert.EqualValues(t, 25, b.current)
57 g = []bool{false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false}
58 assert.Equal(t, g, b.snapshot())
59
60 // Mark 24, which is in window (current 25, length 16, window covers [10,25]).
61 assert.True(t, b.Check(l, 24))
62 assert.True(t, b.Update(l, 24))
63 assert.EqualValues(t, 25, b.current)
64 g = []bool{false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false}
65 assert.Equal(t, g, b.snapshot())
66
67 // Mark 5, not allowed because 5 <= current-length (25-16=9).
68 assert.False(t, b.Check(l, 5))
69 assert.False(t, b.Update(l, 5))
70 assert.EqualValues(t, 25, b.current)
71 g = []bool{false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false}
72 assert.Equal(t, g, b.snapshot())
73
74 // Make sure we handle wrapping around once to the same slot. With
75 // length=16, packets 1 and 17 share slot 1.
76 b = NewBits(16)
77 assert.True(t, b.Update(l, 1))
78 assert.True(t, b.Update(l, 17))
79 assert.Equal(t, []bool{false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false}, b.snapshot())
80
81 // Walk through a few windows in order
82 b = NewBits(16)
83 for i := uint64(1); i <= 100; i++ {
84 assert.True(t, b.Check(l, i), "Error while checking %v", i)
85 assert.True(t, b.Update(l, i), "Error while updating %v", i)
86 }
87
88 assert.False(t, b.Check(l, 1), "Out of window check")
89 }
90
91 func TestBitsLargeJumps(t *testing.T) {
92 l := test.NewLogger()
93
94 // length=16. Update(55) from current=0:
95 // warmup, per-bit loop sees no n>16 with unset bits (slot 0 was set by
96 // NewBits and gets re-evaluated when n=16; n=16 is not strictly > 16),
97 // so the loop contributes 0. The jump exceeds the window so we record
98 // 55 - 0 - 16 = 39 packets fell out the back.
99 b := NewBits(16)
100 b.lostCounter.Clear()
101 assert.True(t, b.Update(l, 55))
102 assert.Equal(t, int64(39), b.lostCounter.Count())
103
104 // Update(100): clears 16 slots starting at slot 56%16=8. Only slot 7 (for
105 // packet 55) was set, so 16 - 1 = 15 evicted slots had unset bits.
106 // Plus 100 - 55 - 16 = 29 packets fell past the window. Total 44.
107 assert.True(t, b.Update(l, 100))
108 assert.Equal(t, int64(39+44), b.lostCounter.Count())
109
110 // Update(200): same shape: 16 - 1 = 15 evicted unset, plus 200 - 100 - 16 = 84 past window. Total 99.
111 assert.True(t, b.Update(l, 200))
112 assert.Equal(t, int64(39+44+99), b.lostCounter.Count())
113 }
114
115 func TestBitsDupeCounter(t *testing.T) {
116 l := test.NewLogger()
117 b := NewBits(16)
118 b.lostCounter.Clear()
119 b.dupeCounter.Clear()
120 b.outOfWindowCounter.Clear()
121
122 assert.True(t, b.Update(l, 1))
123 assert.Equal(t, int64(0), b.dupeCounter.Count())
124
125 assert.False(t, b.Update(l, 1))
126 assert.Equal(t, int64(1), b.dupeCounter.Count())
127
128 assert.True(t, b.Update(l, 2))
129 assert.Equal(t, int64(1), b.dupeCounter.Count())
130
131 assert.True(t, b.Update(l, 3))
132 assert.Equal(t, int64(1), b.dupeCounter.Count())
133
134 assert.False(t, b.Update(l, 1))
135 assert.Equal(t, int64(0), b.lostCounter.Count())
136 assert.Equal(t, int64(2), b.dupeCounter.Count())
137 assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
138 }
139
140 func TestBitsOutOfWindowCounter(t *testing.T) {
141 l := test.NewLogger()
142 b := NewBits(16)
143 b.lostCounter.Clear()
144 b.dupeCounter.Clear()
145 b.outOfWindowCounter.Clear()
146
147 // Jump to 20 (warmup branch + 4 past-window packets).
148 assert.True(t, b.Update(l, 20))
149 assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
150
151 // 9 single-step advances, each evicts a slot whose bit was cleared during
152 // the jump above and whose value was never seen, so each contributes 1
153 // to lostCounter.
154 for n := uint64(21); n <= 29; n++ {
155 assert.True(t, b.Update(l, n))
156 }
157 assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
158
159 // 0 is below current-length (29-16=13) so it falls outside the window.
160 assert.False(t, b.Update(l, 0))
161 assert.Equal(t, int64(1), b.outOfWindowCounter.Count())
162
163 // 4 from the Update(20) jump + 9 from 21..29.
164 assert.Equal(t, int64(13), b.lostCounter.Count())
165 assert.Equal(t, int64(0), b.dupeCounter.Count())
166 assert.Equal(t, int64(1), b.outOfWindowCounter.Count())
167 }
168
169 func TestBitsLostCounter(t *testing.T) {
170 l := test.NewLogger()
171 b := NewBits(16)
172 b.lostCounter.Clear()
173 b.dupeCounter.Clear()
174 b.outOfWindowCounter.Clear()
175
176 // Walk 20..29 like the original, just with a bigger window. Same
177 // reasoning as TestBitsOutOfWindowCounter: 4 past-window from Update(20),
178 // then 9 more from the unit advances.
179 for n := uint64(20); n <= 29; n++ {
180 assert.True(t, b.Update(l, n))
181 }
182 assert.Equal(t, int64(13), b.lostCounter.Count())
183 assert.Equal(t, int64(0), b.dupeCounter.Count())
184 assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
185
186 b = NewBits(16)
187 b.lostCounter.Clear()
188 b.dupeCounter.Clear()
189 b.outOfWindowCounter.Clear()
190
191 // Update(15) clears the warmup window (no lost), sets slot 15.
192 assert.True(t, b.Update(l, 15))
193 assert.Equal(t, int64(0), b.lostCounter.Count())
194
195 // Update(16): slot 0 was already set (NewBits seeded it), and 16 is not
196 // strictly > length, so nothing is recorded as lost.
197 assert.True(t, b.Update(l, 16))
198 assert.Equal(t, int64(0), b.lostCounter.Count())
199
200 // Update(17): we jumped straight from 0 to 15, so slot 1 was cleared
201 // (and never re-set). 17 > 16 is past warmup, so packet 1 is recorded lost.
202 assert.True(t, b.Update(l, 17))
203 assert.Equal(t, int64(1), b.lostCounter.Count())
204
205 // Fill in 18..30 in single steps. Each i evicts slot i%16. Slots 2..14
206 // were all cleared during Update(15), and we never re-set any of them,
207 // so each i in 18..30 is a fresh lost packet — 13 more.
208 for n := uint64(18); n <= 30; n++ {
209 assert.True(t, b.Update(l, n))
210 }
211 assert.Equal(t, int64(14), b.lostCounter.Count())
212
213 // Jump ahead by exactly one window size.
214 assert.True(t, b.Update(l, 46))
215 // end = min(46, 30+16) = 46, count = 16, all slots cleared. Before the
216 // jump every slot 0..15 had been set (Update(15), (16), (17), 18..30),
217 // so wasSet=16 and 46 == current+length means no past-window slack:
218 // lost contribution = 0.
219 assert.Equal(t, int64(14), b.lostCounter.Count())
220
221 // Walk 47..55. The Update(46) jump cleared every slot, so only slot 14
222 // (for packet 46) is set when we start. Each subsequent unit step lands
223 // on a slot that was cleared and is past warmup, so it counts as lost.
224 // 9 more = 23.
225 for n := uint64(47); n <= 55; n++ {
226 assert.True(t, b.Update(l, n))
227 }
228 assert.Equal(t, int64(23), b.lostCounter.Count())
229
230 // Jump ahead by two windows: clears the window plus past-window loss.
231 assert.True(t, b.Update(l, 87))
232 // current=55, length=16. end = min(87, 71) = 71. count=16, all slots
233 // cleared. Slots set before the clear are slots 14,15,0..7 (10 total).
234 // Lost from clear = 16 - 10 = 6. Past window: 87 - 55 - 16 = 16. +22.
235 assert.Equal(t, int64(45), b.lostCounter.Count())
236 assert.Equal(t, int64(0), b.dupeCounter.Count())
237 assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
238 }
239
240 func TestBitsLostCounterIssue1(t *testing.T) {
241 l := test.NewLogger()
242 b := NewBits(16)
243 b.lostCounter.Clear()
244 b.dupeCounter.Clear()
245 b.outOfWindowCounter.Clear()
246
247 // Receive 4, backfill 1, then 9, 2, 3, 5, 6, 7 (skip 8), 10, 11, 14.
248 // Then jump to 25 — slot 25%16=9 is being evicted, but it had been set
249 // (we received packet 9), so no spurious lost increment. The original
250 // regression was about double-counting a missing packet when its slot
251 // got cleared on a jump. With the jump path now using clearRange's
252 // word-level wasSet count, the same semantics hold.
253 assert.True(t, b.Update(l, 4))
254 assert.Equal(t, int64(0), b.lostCounter.Count())
255 assert.True(t, b.Update(l, 1))
256 assert.Equal(t, int64(0), b.lostCounter.Count())
257 assert.True(t, b.Update(l, 9))
258 assert.Equal(t, int64(0), b.lostCounter.Count())
259 assert.True(t, b.Update(l, 2))
260 assert.Equal(t, int64(0), b.lostCounter.Count())
261 assert.True(t, b.Update(l, 3))
262 assert.Equal(t, int64(0), b.lostCounter.Count())
263 assert.True(t, b.Update(l, 5))
264 assert.Equal(t, int64(0), b.lostCounter.Count())
265 assert.True(t, b.Update(l, 6))
266 assert.Equal(t, int64(0), b.lostCounter.Count())
267 assert.True(t, b.Update(l, 7))
268 assert.Equal(t, int64(0), b.lostCounter.Count())
269 // Skip packet 8.
270 assert.True(t, b.Update(l, 10))
271 assert.Equal(t, int64(0), b.lostCounter.Count())
272 assert.True(t, b.Update(l, 11))
273 assert.Equal(t, int64(0), b.lostCounter.Count())
274
275 assert.True(t, b.Update(l, 14))
276 assert.Equal(t, int64(0), b.lostCounter.Count())
277
278 // Jump to 25. With length=16, slot 25%16=9 corresponds to packet 9
279 // (which we DID receive), so its bit is set and no lost++ from that
280 // eviction. The trace below shows the only loss is packet 8.
281 assert.True(t, b.Update(l, 25))
282 // current was 14, i=25. end=min(25,30)=25. count=11. startPos=15.
283 // steady? current=14<16, so warmup branch: per-bit n=15..25, count those
284 // with !get(n) AND n>16. n=17..25 are >16. Among slots 17%16=1..25%16=9
285 // did we set slots 1..9 (packets 1..9)? Yes for all but slot 8 (packet 8
286 // was skipped). n=24 maps to slot 8 which is FALSE → lost++. All other
287 // n in 17..25 map to slots that are set. n=16 is not strictly > 16. So
288 // lost = 1.
289 assert.Equal(t, int64(1), b.lostCounter.Count())
290
291 // Fill in 12, 13, 15, 16. Each is below current=25 (in-window). 16 must
292 // recheck slot 0 — it was set by NewBits and then cleared by the
293 // Update(25) jump, so 16 backfills cleanly.
294 assert.True(t, b.Update(l, 12))
295 assert.Equal(t, int64(1), b.lostCounter.Count())
296 assert.True(t, b.Update(l, 13))
297 assert.Equal(t, int64(1), b.lostCounter.Count())
298 assert.True(t, b.Update(l, 15))
299 assert.Equal(t, int64(1), b.lostCounter.Count())
300 assert.True(t, b.Update(l, 16))
301 assert.Equal(t, int64(1), b.lostCounter.Count())
302
303 // We missed packet 8 above and that loss is still recorded once, never
304 // double-counted, never zeroed.
305 assert.Equal(t, int64(1), b.lostCounter.Count())
306 assert.Equal(t, int64(0), b.dupeCounter.Count())
307 assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
308 }
309
310 // TestBitsWarmupOvershoot exercises the jump path's warmup arm with an
311 // overshoot past one full window. NewBits leaves current=0 with only slot 0
312 // "set" by the marker. Jumping straight to length+k must (a) clear every
313 // slot the jump straddles, (b) count only past-window slack (not the
314 // in-window slots, which never had a "lost" tenant during warmup), and
315 // (c) leave the cursor at the new counter so subsequent unit advances
316 // count from steady state. The marker bit at slot 0 is irrelevant once
317 // current >= length.
318 func TestBitsWarmupOvershoot(t *testing.T) {
319 l := test.NewLogger()
320 b := NewBits(16)
321 b.lostCounter.Clear()
322
323 // Jump from current=0 to i=20 (length=16, overshoot=4).
324 // Warmup arm: counts slots in [1..16] where bit unset and n>length.
325 // Only n=16 was unset and >length: but slot 16%16=0 is the marker,
326 // so b.get(16) reads bits[0]=1 and skips. Result: 0 lost from the loop.
327 // Past-window: i - current - length = 20 - 0 - 16 = 4 lost.
328 assert.True(t, b.Update(l, 20))
329 assert.Equal(t, int64(4), b.lostCounter.Count())
330 assert.Equal(t, uint64(20), b.current)
331
332 // Steady state now (current=20 >= length=16). Unit advance to 21
333 // stomps slot 21%16=5, which was cleared by the jump and not reset,
334 // so this is +1 lost.
335 assert.True(t, b.Update(l, 21))
336 assert.Equal(t, int64(5), b.lostCounter.Count())
337 }
338
339 // TestBitsCheckAcrossWarmupBoundary pins the underflow trick in Check's
340 // in-window clause. While in warmup, b.current-b.length underflows uint64
341 // to a huge value so the first OR-clause is always false; the second
342 // clause (i < length && current < length) carries the in-window check.
343 // Once current >= length the regimes flip cleanly.
344 func TestBitsCheckAcrossWarmupBoundary(t *testing.T) {
345 l := test.NewLogger()
346 b := NewBits(16)
347
348 // Warmup: current=0. Check(0) must read the marker (set) and return false.
349 assert.False(t, b.Check(l, 0), "marker slot should look already-received")
350 // Warmup: any 0 < i < length is in-window and unset → accepted.
351 for i := uint64(1); i < 16; i++ {
352 assert.True(t, b.Check(l, i), "warmup in-window i=%d should be accepted", i)
353 }
354 // Warmup: i >= length but > current is "next number" so accepted.
355 assert.True(t, b.Check(l, 16))
356 assert.True(t, b.Check(l, 1_000_000))
357
358 // Cross into steady state.
359 assert.True(t, b.Update(l, 100))
360 // Now current=100, length=16. In-window range is [85..100].
361 // 84 is just outside: the underflow clause activates; 84 > 100-16=84 is false.
362 // And the warmup clause is false (current >= length). So out of window.
363 assert.False(t, b.Check(l, 84))
364 // 85 sits at the boundary. 85 > 84 is true → in window, unset → accept.
365 assert.True(t, b.Check(l, 85))
366 // 100 is current itself; not strictly greater, in-window, but already set.
367 assert.False(t, b.Check(l, 100))
368 // Way out: clearly out of window.
369 assert.False(t, b.Check(l, 50))
370 }
371
372 // TestBitsMarkerInvariant verifies the seeded bits[0]=1 marker behaves
373 // correctly across warmup and beyond. Update should never clear the marker
374 // during warmup (clearRange skips position 0 when startPos=1), and once
375 // current >= length the marker is no longer consulted by Check/Update on
376 // the live path — but it must still report counter 0 as a duplicate while
377 // we are in warmup.
378 func TestBitsMarkerInvariant(t *testing.T) {
379 l := test.NewLogger()
380 b := NewBits(8)
381
382 // Counter 0 is the seeded marker; Check sees it as already received.
383 assert.False(t, b.Check(l, 0))
384 // Update(0) at current=0 hits the duplicate branch.
385 b.dupeCounter.Clear()
386 assert.False(t, b.Update(l, 0))
387 assert.Equal(t, int64(1), b.dupeCounter.Count())
388
389 // Walk forward through warmup; the marker must remain set.
390 for n := uint64(1); n <= 7; n++ {
391 assert.True(t, b.Update(l, n))
392 }
393 // Position 0 (the marker) should still read as set because we never
394 // cleared it; Update(0) still looks like a duplicate.
395 assert.False(t, b.Check(l, 0))
396
397 // Cross into steady state with a unit advance to 8: pos=0, evicts the
398 // marker bit. The lost-counter guard (i > b.length) is false (8 == 8),
399 // so this advance does NOT charge a lost packet — exactly what the
400 // marker is there to prevent.
401 b.lostCounter.Clear()
402 assert.True(t, b.Update(l, 8))
403 assert.Equal(t, int64(0), b.lostCounter.Count())
404 // The slot at pos 0 is now occupied by counter 8.
405 assert.False(t, b.Check(l, 8))
406 }
407
408 // BenchmarkBitsUpdateInOrder is the steady-state hot path: each call is
409 // i == current+1.
410 func BenchmarkBitsUpdateInOrder(b *testing.B) {
411 l := test.NewLogger()
412 z := NewBits(16384)
413 for n := 0; n < b.N; n++ {
414 z.Update(l, uint64(n)+1)
415 }
416 }
417
418 // BenchmarkBitsUpdateReorder simulates light reorder within the window:
419 // every other packet arrives one slot behind its predecessor (forces the
420 // in-window backfill branch).
421 func BenchmarkBitsUpdateReorder(b *testing.B) {
422 l := test.NewLogger()
423 z := NewBits(16384)
424 for n := 0; n < b.N; n++ {
425 base := uint64(n) * 2
426 z.Update(l, base+2)
427 z.Update(l, base+1)
428 }
429 }
430
431 // BenchmarkBitsUpdateLargeJumps stresses the clearRange word-level path.
432 func BenchmarkBitsUpdateLargeJumps(b *testing.B) {
433 l := test.NewLogger()
434 z := NewBits(16384)
435 for n := 0; n < b.N; n++ {
436 z.Update(l, uint64(n+1)*1000)
437 }
438 }