-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_test.go
371 lines (325 loc) · 6.44 KB
/
queue_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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package ringbuf
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestRbQueueQuantity(t *testing.T) {
q := NewQueue(8)
// Test empty queue
if q.Quantity() != 0 {
t.Errorf("Expected quantity to be 0, but got %d", q.Quantity())
}
// Test single element
q.Put(1)
if q.Quantity() != 1 {
t.Errorf("Expected quantity to be 1, but got %d", q.Quantity())
}
// Test multiple elements
q.Put(2)
q.Put(3)
if q.Quantity() != 3 {
t.Errorf("Expected quantity to be 3, but got %d", q.Quantity())
}
// Test concurrent access
var wg sync.WaitGroup
var expected uint32 = 1000
for i := uint32(0); i < expected; i++ {
wg.Add(1)
go func() {
q.Put(i)
wg.Done()
}()
}
wg.Wait()
if q.Quantity() != 8 {
t.Errorf("Expected quantity to be %d, but got %d", 8, q.Quantity())
}
// Test concurrent access with removal
var removed uint32
for i := uint32(0); i < expected; i++ {
wg.Add(1)
go func() {
_, ok := q.Get()
if ok {
atomic.AddUint32(&removed, 1)
}
wg.Done()
}()
}
wg.Wait()
if q.Quantity() != 0 {
t.Errorf("Expected quantity to be 0, but got %d", q.Quantity())
}
}
func TestRbQueueIsFull(t *testing.T) {
// Test empty queue
q := NewQueue(8)
if q.IsFull() {
t.Errorf("Expected IsFull to return false, but it returned true")
}
// Test partially full queue
q.Put(1)
q.Put(2)
if q.IsFull() {
t.Errorf("Expected IsFull to return false, but it returned true")
}
// Test full queue
for i := 3; i <= 8; i++ {
q.Put(i)
}
if !q.IsFull() {
t.Errorf("Expected IsFull to return true, but it returned false")
}
// Test concurrent access
var wg sync.WaitGroup
var expected uint32 = 1000
for i := uint32(0); i < expected; i++ {
wg.Add(1)
go func() {
q.Put(0)
wg.Done()
}()
}
wg.Wait()
if !q.IsFull() {
t.Errorf("Expected IsFull to return true, but it returned false")
}
// Test concurrent access with removal
for i := uint32(0); i < expected; i++ {
q.Get()
}
if q.IsFull() {
t.Errorf("Expected IsFull to return false, but it returned true")
}
// Test concurrent access with removal and addition
for i := uint32(0); i < expected; i++ {
wg.Add(1)
go func() {
q.Put(0)
q.Get()
wg.Done()
}()
}
wg.Wait()
if q.IsFull() {
t.Errorf("Expected IsFull to return false, but it returned true")
}
}
func TestRbQueueIsEmpty(t *testing.T) {
q := NewQueue(8)
// Test empty queue
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it is not")
}
// Test single element
q.Put(1)
if q.IsEmpty() {
t.Errorf("Expected queue to not be empty, but it is")
}
// Test multiple elements
q.Put(2)
q.Put(3)
if q.IsEmpty() {
t.Errorf("Expected queue to not be empty, but it is")
}
// Test concurrent access
var wg sync.WaitGroup
var expected uint32 = 1000
for i := uint32(0); i < expected; i++ {
wg.Add(1)
go func(i uint32) {
q.Put(i)
wg.Done()
}(i)
}
wg.Wait()
if q.IsEmpty() {
t.Errorf("Expected queue to not be empty, but it is")
}
// Test concurrent access with removal
var removed uint32
for i := uint32(0); i < expected; i++ {
wg.Add(1)
go func() {
_, ok := q.Get()
if ok {
atomic.AddUint32(&removed, 1)
}
wg.Done()
}()
}
wg.Wait()
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it is not")
}
}
func TestRbQueuePut(t *testing.T) {
q := NewQueue(8)
// Test single element
if !q.Put(1) {
t.Errorf("Expected Put to return true, but it returned false")
}
if q.Quantity() != 1 {
t.Errorf("Expected quantity to be 1, but got %d", q.Quantity())
}
// Test multiple elements
if !q.Put(2) {
t.Errorf("Expected Put to return true, but it returned false")
}
if !q.Put(3) {
t.Errorf("Expected Put to return true, but it returned false")
}
if q.Quantity() != 3 {
t.Errorf("Expected quantity to be 3, but got %d", q.Quantity())
}
// Test concurrent access
var wg sync.WaitGroup
var expected uint32 = 1000
for i := uint32(0); i < expected; i++ {
wg.Add(1)
go func(i uint32) {
q.Put(i)
wg.Done()
}(i)
}
wg.Wait()
if q.Quantity() != 8 {
t.Errorf("Expected quantity to be %d, but got %d", 8, q.Quantity())
}
// Test concurrent access with contention
q = NewQueue(1)
q.Put(1)
wg.Add(2)
go func() {
if !q.Put(2) {
t.Errorf("Expected Put to return true, but it returned false")
}
wg.Done()
}()
go func() {
if !q.Put(3) {
t.Errorf("Expected Put to return true, but it returned false")
}
wg.Done()
}()
wg.Wait()
if q.Quantity() != 3 {
t.Errorf("Expected quantity to be %d, but got %d", 3, q.Quantity())
}
}
func TestRbQueueGet(t *testing.T) {
q := NewQueue(8)
var (
w sync.WaitGroup
get uint32
put uint32
num = 10
jump = 100000
)
w.Add(num)
go func() {
for i := 0; i < num; i++ {
go func() {
defer w.Done()
var val int
for {
ok := q.Put(val)
for !ok {
runtime.Gosched()
ok = q.Put(val)
}
atomic.AddUint32(&put, 1)
val++
if val == jump {
break
}
}
}()
}
}()
w.Add(num)
go func() {
for i := 0; i < num; i++ {
go func() {
defer w.Done()
var val int
for {
_, ok := q.Get()
for !ok {
runtime.Gosched()
_, ok = q.Get()
}
atomic.AddUint32(&get, 1)
val++
if val == jump {
break
}
}
}()
}
}()
go func() {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
<-ticker.C
head := atomic.LoadUint32(&q.head)
tail := atomic.LoadUint32(&q.tail)
var full, empty bool
var quantity uint32
if head == tail {
empty = true
} else if tail-head == q.cap {
full = true
} else {
quantity = tail - head
}
nput := atomic.LoadUint32(&put)
nget := atomic.LoadUint32(&get)
fmt.Printf("put: %d; get: %d; full: %t; empty: %t; size: %d; head: %d; tail: %d \n",
nput, nget, full, empty, quantity, head, tail)
}
}()
w.Wait()
fmt.Printf("put: %d; get: %d \n", put, get)
}
func BenchmarkQueue(b *testing.B) {
b.Run("Queue", func(b *testing.B) {
q := NewQueue(8)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
for {
if q.Put(1) {
break
} else {
runtime.Gosched()
}
}
for {
if _, ok := q.Get(); ok {
break
} else {
runtime.Gosched()
}
}
}
})
})
b.Run("Channel", func(b *testing.B) {
q := make(chan int, 8)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
q <- 1
<-q
}
})
})
}