-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathchan_queue_test.go
82 lines (66 loc) · 1.17 KB
/
chan_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
package queue
import (
"fmt"
"reflect"
"sync/atomic"
"testing"
)
func TestQueue(t *testing.T) {
q := NewQueue(2, 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()
if count != 10 {
t.Error(count)
}
}
func TestSyncQueue(t *testing.T) {
q := NewQueue(1, 2)
q.Run()
defer q.Terminate()
sjob := NewSyncJob("foo", func(v interface{}) (interface{}, error) {
return fmt.Sprintf("%s_bar", v), nil
})
q.Push(sjob)
result := <-sjob.Wait()
if err := sjob.Error(); err != nil {
t.Error(err.Error())
}
if !reflect.DeepEqual(result, "foo_bar") {
t.Error(result)
}
}
func ExampleQueue() {
q := NewQueue(1, 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 BenchmarkQueue(b *testing.B) {
q := NewQueue(10, 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()
}