-
Notifications
You must be signed in to change notification settings - Fork 38
/
mpmcqueueof_test.go
326 lines (305 loc) · 6.97 KB
/
mpmcqueueof_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//go:build go1.19
// +build go1.19
// Copyright notice. The following tests are partially based on
// the following file from the Go Programming Language core repo:
// https://github.com/golang/go/blob/831f9376d8d730b16fb33dfd775618dffe13ce7a/src/runtime/chan_test.go
package xsync_test
import (
"runtime"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
. "github.com/puzpuzpuz/xsync/v3"
)
func TestQueueOf_InvalidSize(t *testing.T) {
defer func() { recover() }()
NewMPMCQueueOf[int](0)
t.Fatal("no panic detected")
}
func TestQueueOfEnqueueDequeueInt(t *testing.T) {
q := NewMPMCQueueOf[int](10)
for i := 0; i < 10; i++ {
q.Enqueue(i)
}
for i := 0; i < 10; i++ {
if got := q.Dequeue(); got != i {
t.Fatalf("got %v, want %d", got, i)
}
}
}
func TestQueueOfEnqueueDequeueString(t *testing.T) {
q := NewMPMCQueueOf[string](10)
for i := 0; i < 10; i++ {
q.Enqueue(strconv.Itoa(i))
}
for i := 0; i < 10; i++ {
if got := q.Dequeue(); got != strconv.Itoa(i) {
t.Fatalf("got %v, want %d", got, i)
}
}
}
func TestQueueOfEnqueueDequeueStruct(t *testing.T) {
type foo struct {
bar int
baz int
}
q := NewMPMCQueueOf[foo](10)
for i := 0; i < 10; i++ {
q.Enqueue(foo{i, i})
}
for i := 0; i < 10; i++ {
if got := q.Dequeue(); got.bar != i || got.baz != i {
t.Fatalf("got %v, want %d", got, i)
}
}
}
func TestQueueOfEnqueueDequeueStructRef(t *testing.T) {
type foo struct {
bar int
baz int
}
q := NewMPMCQueueOf[*foo](11)
for i := 0; i < 10; i++ {
q.Enqueue(&foo{i, i})
}
q.Enqueue(nil)
for i := 0; i < 10; i++ {
if got := q.Dequeue(); got.bar != i || got.baz != i {
t.Fatalf("got %v, want %d", got, i)
}
}
if last := q.Dequeue(); last != nil {
t.Fatalf("got %v, want nil", last)
}
}
func TestQueueOfEnqueueBlocksOnFull(t *testing.T) {
q := NewMPMCQueueOf[string](1)
q.Enqueue("foo")
cdone := make(chan bool)
flag := int32(0)
go func() {
q.Enqueue("bar")
if atomic.LoadInt32(&flag) == 0 {
t.Error("enqueue on full queue didn't wait for dequeue")
}
cdone <- true
}()
time.Sleep(50 * time.Millisecond)
atomic.StoreInt32(&flag, 1)
if got := q.Dequeue(); got != "foo" {
t.Fatalf("got %v, want foo", got)
}
<-cdone
}
func TestQueueOfDequeueBlocksOnEmpty(t *testing.T) {
q := NewMPMCQueueOf[string](2)
cdone := make(chan bool)
flag := int32(0)
go func() {
q.Dequeue()
if atomic.LoadInt32(&flag) == 0 {
t.Error("dequeue on empty queue didn't wait for enqueue")
}
cdone <- true
}()
time.Sleep(50 * time.Millisecond)
atomic.StoreInt32(&flag, 1)
q.Enqueue("foobar")
<-cdone
}
func TestQueueOfTryEnqueueDequeue(t *testing.T) {
q := NewMPMCQueueOf[int](10)
for i := 0; i < 10; i++ {
if !q.TryEnqueue(i) {
t.Fatalf("failed to enqueue for %d", i)
}
}
for i := 0; i < 10; i++ {
if got, ok := q.TryDequeue(); !ok || got != i {
t.Fatalf("got %v, want %d, for status %v", got, i, ok)
}
}
}
func TestQueueOfTryEnqueueOnFull(t *testing.T) {
q := NewMPMCQueueOf[string](1)
if !q.TryEnqueue("foo") {
t.Error("failed to enqueue initial item")
}
if q.TryEnqueue("bar") {
t.Error("got success for enqueue on full queue")
}
}
func TestQueueOfTryDequeueBlocksOnEmpty(t *testing.T) {
q := NewMPMCQueueOf[int](2)
if _, ok := q.TryDequeue(); ok {
t.Error("got success for enqueue on empty queue")
}
}
func hammerQueueOfBlockingCalls(t *testing.T, gomaxprocs, numOps, numThreads int) {
runtime.GOMAXPROCS(gomaxprocs)
q := NewMPMCQueueOf[int](numThreads)
startwg := sync.WaitGroup{}
startwg.Add(1)
csum := make(chan int, numThreads)
// Start producers.
for i := 0; i < numThreads; i++ {
go func(n int) {
startwg.Wait()
for j := n; j < numOps; j += numThreads {
q.Enqueue(j)
}
}(i)
}
// Start consumers.
for i := 0; i < numThreads; i++ {
go func(n int) {
startwg.Wait()
sum := 0
for j := n; j < numOps; j += numThreads {
item := q.Dequeue()
sum += item
}
csum <- sum
}(i)
}
startwg.Done()
// Wait for all the sums from producers.
sum := 0
for i := 0; i < numThreads; i++ {
s := <-csum
sum += s
}
// Assert the total sum.
expectedSum := numOps * (numOps - 1) / 2
if sum != expectedSum {
t.Fatalf("sums don't match for %d num ops, %d num threads: got %d, want %d",
numOps, numThreads, sum, expectedSum)
}
}
func TestQueueOfBlockingCalls(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1))
n := 100
if testing.Short() {
n = 10
}
hammerQueueOfBlockingCalls(t, 1, 100*n, n)
hammerQueueOfBlockingCalls(t, 1, 1000*n, 10*n)
hammerQueueOfBlockingCalls(t, 4, 100*n, n)
hammerQueueOfBlockingCalls(t, 4, 1000*n, 10*n)
hammerQueueOfBlockingCalls(t, 8, 100*n, n)
hammerQueueOfBlockingCalls(t, 8, 1000*n, 10*n)
}
func hammerQueueOfNonBlockingCalls(t *testing.T, gomaxprocs, numOps, numThreads int) {
runtime.GOMAXPROCS(gomaxprocs)
q := NewMPMCQueueOf[int](numThreads)
startwg := sync.WaitGroup{}
startwg.Add(1)
csum := make(chan int, numThreads)
// Start producers.
for i := 0; i < numThreads; i++ {
go func(n int) {
startwg.Wait()
for j := n; j < numOps; j += numThreads {
for !q.TryEnqueue(j) {
// busy spin until success
}
}
}(i)
}
// Start consumers.
for i := 0; i < numThreads; i++ {
go func(n int) {
startwg.Wait()
sum := 0
for j := n; j < numOps; j += numThreads {
var (
item int
ok bool
)
for {
// busy spin until success
if item, ok = q.TryDequeue(); ok {
sum += item
break
}
}
}
csum <- sum
}(i)
}
startwg.Done()
// Wait for all the sums from producers.
sum := 0
for i := 0; i < numThreads; i++ {
s := <-csum
sum += s
}
// Assert the total sum.
expectedSum := numOps * (numOps - 1) / 2
if sum != expectedSum {
t.Fatalf("sums don't match for %d num ops, %d num threads: got %d, want %d",
numOps, numThreads, sum, expectedSum)
}
}
func TestQueueOfNonBlockingCalls(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1))
n := 10
if testing.Short() {
n = 1
}
hammerQueueOfNonBlockingCalls(t, 1, n, n)
hammerQueueOfNonBlockingCalls(t, 2, 10*n, 2*n)
hammerQueueOfNonBlockingCalls(t, 4, 100*n, 4*n)
}
func benchmarkQueueOfProdCons(b *testing.B, queueSize, localWork int) {
callsPerSched := queueSize
procs := runtime.GOMAXPROCS(-1) / 2
if procs == 0 {
procs = 1
}
N := int32(b.N / callsPerSched)
c := make(chan bool, 2*procs)
q := NewMPMCQueueOf[int](queueSize)
for p := 0; p < procs; p++ {
go func() {
foo := 0
for atomic.AddInt32(&N, -1) >= 0 {
for g := 0; g < callsPerSched; g++ {
for i := 0; i < localWork; i++ {
foo *= 2
foo /= 2
}
q.Enqueue(1)
}
}
q.Enqueue(0)
c <- foo == 42
}()
go func() {
foo := 0
for {
v := q.Dequeue()
if v == 0 {
break
}
for i := 0; i < localWork; i++ {
foo *= 2
foo /= 2
}
}
c <- foo == 42
}()
}
for p := 0; p < procs; p++ {
<-c
<-c
}
}
func BenchmarkQueueOfProdCons(b *testing.B) {
benchmarkQueueOfProdCons(b, 1000, 0)
}
func BenchmarkOfQueueProdConsWork100(b *testing.B) {
benchmarkQueueOfProdCons(b, 1000, 100)
}