-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_test.go
98 lines (87 loc) · 1.66 KB
/
buffer_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
86
87
88
89
90
91
92
93
94
95
96
97
98
package workpool
import (
"runtime"
"sync"
"testing"
)
var procs = runtime.GOMAXPROCS(8)
var rb = func() *ringBuffer {
r, _ := newRingBuffer(4096)
return r
}()
var wg sync.WaitGroup
func BenchmarkRingBufferParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
w := newWorker(1)
rb.push(w)
}
})
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rb.pop()
}
})
}
func TestRingBuffer(t *testing.T) {
w1 := newWorker(1)
w2 := newWorker(2)
w3 := newWorker(3)
for _, unit := range []struct {
x *worker
expected error
}{
{w1, nil},
{w2, nil},
{w3, nil},
} {
if ac := rb.push(unit.x); ac != unit.expected {
t.Errorf("TestRingBuffer [%v], actually: [%v]", unit.expected, ac)
}
}
for _, unit := range []struct {
expected *worker
}{
{w1},
{w2},
{w3},
{nil},
{nil},
} {
if ac := rb.pop(); ac != unit.expected {
t.Errorf("TestRingBuffer [%v], actually: [%v]", unit.expected, ac)
}
}
for _, unit := range []struct {
x *worker
y *worker
err error
}{
{w1, w1, nil},
{w2, w2, nil},
{w3, w3, nil},
} {
if ac1 := rb.push(unit.x); ac1 != unit.err {
t.Errorf("TestRingBuffer [%v], actually: [%v]", unit.err, ac1)
}
if ac2 := rb.pop(); ac2 != unit.y {
t.Errorf("TestRingBuffer [%v], actually: [%v]", unit.y, ac2)
}
}
}
func TestRingBufferParallel(t *testing.T) {
t.Parallel()
wg.Add(1)
for i := 0; i <= 10000; i++ {
if i&1 == 0 {
if err := rb.push(newWorker(uint64(i))); err != nil {
t.Errorf("TestRingBufferParallel push error [%v]", err)
}
} else {
if w := rb.pop(); w == nil {
t.Errorf("TestRingBufferParallel pop nil")
}
}
}
wg.Done()
}