-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathlist_queue_test.go
More file actions
79 lines (69 loc) · 1.13 KB
/
list_queue_test.go
File metadata and controls
79 lines (69 loc) · 1.13 KB
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
package queue
import (
"fmt"
"sync/atomic"
"testing"
)
type testJob struct {
payload int
callback func(int)
}
func (t *testJob) Job() {
t.payload++
t.callback(t.payload)
}
func TestListQueue(t *testing.T) {
q := NewListQueue(2)
q.Run()
var data int64
q.Push(&testJob{
payload: 0,
callback: func(result int) {
atomic.AddInt64(&data, int64(result))
},
})
q.Push(&testJob{
payload: 0,
callback: func(result int) {
atomic.AddInt64(&data, int64(result))
},
})
q.Push(&testJob{
payload: 0,
callback: func(result int) {
atomic.AddInt64(&data, int64(result))
},
})
q.Terminate()
if data != 3 {
t.Error(data)
}
}
func ExampleListQueue() {
q := NewListQueue(10)
q.Run()
var count int64
for i := 0; i < 10; i++ {
job := NewJob("foo", func(v interface{}) {
atomic.AddInt64(&count, 1)
})
q.Push(job)
}
q.Terminate()
fmt.Println(count)
// output: 10
}
func BenchmarkListQueue(b *testing.B) {
q := NewListQueue(100)
q.Run()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
job := NewJob("", func(v interface{}) {
_ = v
})
q.Push(job)
}
})
q.Terminate()
}