-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitring_test.go
85 lines (76 loc) · 1.85 KB
/
bitring_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package bitring
import (
"math/rand"
"testing"
)
func TestRing(t *testing.T) {
const (
messages = 1500
permutations = 10000
)
bitmap := make([]uint64, 2)
for i := uint(0); i < 128; i++ {
setBit(bitmap, i)
if !getBit(bitmap, i) {
t.Fatalf("failed to set/get bit at %v", i)
}
}
switch {
case pow2BitCapacity(15) != 64, pow2BitCapacity(64) != 64,
pow2BitCapacity(127) != 128, pow2BitCapacity(128) != 128,
pow2BitCapacity(511) != 512, pow2BitCapacity(512) != 512:
t.Fatalf("wrong calculated capacity")
}
// forward completion order
ring := New(64)
for i := 0; i < messages; i++ {
ring.MarkPending(i)
}
for i := 0; i < messages; i++ {
ring.MarkComplete(i)
}
if ring.CommittableOffset() != messages-1 {
t.Fatalf("committable offset: %v", ring.CommittableOffset())
}
// reverse completion order
ring.Reset(64)
for i := 0; i < messages; i++ {
ring.MarkPending(i)
}
for i := messages - 1; i >= 0; i-- {
ring.MarkComplete(i)
}
if ring.CommittableOffset() != messages-1 {
t.Fatalf("committable offset: %v", ring.CommittableOffset())
}
// random completion order
shuffled := make([]int, messages)
for perm := 0; perm < permutations; perm++ {
rand.Shuffle(messages, func(i, j int) { shuffled[i], shuffled[j] = shuffled[j], shuffled[i] })
ring.Reset(64)
for i := 0; i < messages; i++ {
ring.MarkPending(i)
}
for i := range shuffled {
ring.MarkComplete(i)
}
if ring.CommittableOffset() != messages-1 {
t.Fatalf("committable offset: %v", ring.CommittableOffset())
}
}
}
func BenchmarkCoalescedCommittableOffsets(b *testing.B) {
const messages = 1500
ring, n := New(minCapacity), b.N
b.ResetTimer()
for i := 0; i < n; i++ {
// reverse completion order
ring.Reset(minCapacity)
for i := 0; i < messages; i++ {
ring.MarkPending(i)
}
for i := messages - 1; i >= 0; i-- {
ring.MarkComplete(i)
}
}
}