Debian Codegraph @

batcher/batcher_test.go

raw
1 package batcher
2
3 import (
4 "errors"
5 "sync"
6 "sync/atomic"
7 "testing"
8 "time"
9 )
10
11 var errSomeError = errors.New("errSomeError")
12
13 func returnsError(params []interface{}) error {
14 return errSomeError
15 }
16
17 func returnsSuccess(params []interface{}) error {
18 return nil
19 }
20
21 func TestBatcherSuccess(t *testing.T) {
22 b := New(10*time.Millisecond, returnsSuccess)
23
24 wg := &sync.WaitGroup{}
25 for i := 0; i < 10; i++ {
26 wg.Add(1)
27 go func() {
28 if err := b.Run(nil); err != nil {
29 t.Error(err)
30 }
31 wg.Done()
32 }()
33 }
34 wg.Wait()
35
36 b = New(0, returnsSuccess)
37 for i := 0; i < 10; i++ {
38 if err := b.Run(nil); err != nil {
39 t.Error(err)
40 }
41 }
42 }
43
44 func TestShutdownSuccess(t *testing.T) {
45 sleepDuration := 5 * time.Millisecond
46 durationLimit := 2 * sleepDuration
47 timeout := 2 * durationLimit
48 total := 0
49 doSum := func(params []interface{}) error {
50 for _, param := range params {
51 intValue, ok := param.(int)
52 if !ok {
53 t.Error("expected type int")
54 }
55 total += intValue
56 }
57 return nil
58 }
59
60 b := New(timeout, doSum)
61 go func() {
62 time.Sleep(sleepDuration)
63 b.Shutdown(true)
64 }()
65
66 wg := &sync.WaitGroup{}
67 expectedTotal := 0
68 start := time.Now()
69 for i := 0; i < 10; i++ {
70 expectedTotal += i
71 wg.Add(1)
72 go func(i int) {
73 if err := b.Run(i); err != nil {
74 t.Error(err)
75 }
76 wg.Done()
77 }(i)
78 }
79 wg.Wait()
80
81 duration := time.Since(start)
82 if duration >= durationLimit {
83 t.Errorf("expected duration[%v] < durationLimit[%v]", duration, durationLimit)
84 }
85
86 if total != expectedTotal {
87 t.Errorf("expected processed count[%v] < actual[%v]", expectedTotal, total)
88 }
89 }
90
91 func TestShutdownEmpty(t *testing.T) {
92 b := New(10*time.Millisecond, returnsSuccess)
93 b.Shutdown(true)
94 }
95
96 func TestBatcherError(t *testing.T) {
97 b := New(10*time.Millisecond, returnsError)
98
99 wg := &sync.WaitGroup{}
100 for i := 0; i < 10; i++ {
101 wg.Add(1)
102 go func() {
103 if err := b.Run(nil); err != errSomeError {
104 t.Error(err)
105 }
106 wg.Done()
107 }()
108 }
109 wg.Wait()
110 }
111
112 func TestBatcherPrefilter(t *testing.T) {
113 b := New(1*time.Millisecond, returnsSuccess)
114
115 b.Prefilter(func(param interface{}) error {
116 if param == nil {
117 return errSomeError
118 }
119 return nil
120 })
121
122 if err := b.Run(nil); err != errSomeError {
123 t.Error(err)
124 }
125
126 if err := b.Run(1); err != nil {
127 t.Error(err)
128 }
129 }
130
131 func TestBatcherMultipleBatches(t *testing.T) {
132 var iters uint32
133
134 b := New(10*time.Millisecond, func(params []interface{}) error {
135 atomic.AddUint32(&iters, 1)
136 return nil
137 })
138
139 wg := &sync.WaitGroup{}
140
141 for group := 0; group < 5; group++ {
142 for i := 0; i < 10; i++ {
143 wg.Add(1)
144 go func() {
145 if err := b.Run(nil); err != nil {
146 t.Error(err)
147 }
148 wg.Done()
149 }()
150 }
151 time.Sleep(15 * time.Millisecond)
152 }
153
154 wg.Wait()
155
156 if iters != 5 {
157 t.Error("Wrong number of iters:", iters)
158 }
159 }
160
161 func ExampleBatcher() {
162 b := New(10*time.Millisecond, func(params []interface{}) error {
163 // do something with the batch of parameters
164 return nil
165 })
166
167 b.Prefilter(func(param interface{}) error {
168 // do some sort of sanity check on the parameter, and return an error if it fails
169 return nil
170 })
171
172 for i := 0; i < 10; i++ {
173 go b.Run(i)
174 }
175 }