forked from valyala/batcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatcher_test.go
241 lines (223 loc) · 5.24 KB
/
batcher_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
package batcher
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func TestBatcherStartStop(t *testing.T) {
b := &Batcher{
Func: func(batch []interface{}) {},
}
for i := 0; i < 100; i++ {
b.Start()
b.Stop()
}
}
func TestBatcherPushNotStarted(t *testing.T) {
b := &Batcher{
Func: func(batch []interface{}) {},
}
expectPanic(t, func() { b.Push(123) })
}
func TestBatcherStopNotStarted(t *testing.T) {
b := &Batcher{
Func: func(batch []interface{}) {},
}
expectPanic(t, func() { b.Stop() })
}
func TestBatcherDoubleStop(t *testing.T) {
b := &Batcher{
Func: func(batch []interface{}) {},
}
b.Start()
b.Stop()
expectPanic(t, func() { b.Stop() })
}
func TestBatcherDoubleStart(t *testing.T) {
b := &Batcher{
Func: func(batch []interface{}) {},
}
b.Start()
expectPanic(t, func() { b.Start() })
}
func TestBatcherPushStop(t *testing.T) {
n := 0
b := &Batcher{
Func: func(batch []interface{}) { n += len(batch) },
MaxDelay: time.Hour,
}
b.Start()
for i := 0; i < 10; i++ {
if !b.Push(i) {
t.Fatalf("cannot add item %d to batch", i)
}
}
b.Stop()
if n != 10 {
t.Fatalf("Unexpected n=%d. Expected 10", n)
}
}
func TestBatcherPushMaxBatchSize(t *testing.T) {
testBatcherPushMaxBatchSize(t, 1, 100)
testBatcherPushMaxBatchSize(t, 10, 100)
testBatcherPushMaxBatchSize(t, 100, 100)
testBatcherPushMaxBatchSize(t, 101, 100)
testBatcherPushMaxBatchSize(t, 1003, 15)
testBatcherPushMaxBatchSize(t, 1033, 17)
}
func TestBatcherPushMaxDelay(t *testing.T) {
testBatcherPushMaxDelay(t, 100, time.Millisecond)
testBatcherPushMaxDelay(t, 205, 10*time.Millisecond)
testBatcherPushMaxDelay(t, 313, 100*time.Millisecond)
}
func TestBatcherConcurrentPush(t *testing.T) {
s := uint32(0)
b := &Batcher{
Func: func(batch []interface{}) {
for _, v := range batch {
s += v.(uint32)
}
},
}
b.Start()
var wg sync.WaitGroup
ss := uint32(0)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
b.Push(uint32(i))
time.Sleep(time.Millisecond)
atomic.AddUint32(&ss, uint32(i))
}
wg.Done()
}()
}
wg.Wait()
b.Stop()
if s != ss {
t.Fatalf("Unepxected sum %d. Expecting %d", s, ss)
}
}
func TestBatcherQueueSize(t *testing.T) {
ch := make(chan struct{})
n := 0
b := &Batcher{
Func: func(batch []interface{}) {
<-ch
n += len(batch)
},
MaxDelay: time.Hour,
MaxBatchSize: 3,
QueueSize: 10,
}
b.Start()
for i := 0; i < 3; i++ {
if !b.Push(i) {
t.Fatalf("cannot add item %d to batch", i)
}
}
time.Sleep(time.Millisecond)
for i := 0; i < 10; i++ {
if !b.Push(i) {
t.Fatalf("cannot add item %d to batch", i)
}
}
if b.QueueLen() != b.QueueSize {
t.Fatalf("Unexpected queue size %d. Expecting %d", b.QueueLen(), b.QueueSize)
}
for i := 0; i < 10; i++ {
if b.Push(123) {
t.Fatalf("expecting queue overflow")
}
time.Sleep(time.Millisecond)
}
close(ch)
time.Sleep(time.Millisecond)
for i := 0; i < 5; i++ {
if !b.Push(i) {
t.Fatalf("cannot add item %d to batch", i)
}
}
b.Stop()
if n != 18 {
t.Fatalf("Unexpected number of items passed to batcher func: %d. Expected 18", n)
}
}
func testBatcherPushMaxDelay(t *testing.T, itemsCount int, maxDelay time.Duration) {
lastTime := time.Now()
n := 0
nn := 0
b := &Batcher{
Func: func(batch []interface{}) {
if time.Since(lastTime) > maxDelay+10*time.Millisecond {
t.Fatalf("Unexpected delay between batches: %s. Expected no more than %s. itemsCount=%d",
time.Since(lastTime), maxDelay, itemsCount)
}
lastTime = time.Now()
nn += len(batch)
n++
},
MaxDelay: maxDelay,
MaxBatchSize: 100500,
}
b.Start()
for i := 0; i < itemsCount; i++ {
if !b.Push(i) {
t.Fatalf("cannot add item %d to batch", i)
}
time.Sleep(time.Millisecond)
}
b.Stop()
batchSize := 1000 * maxDelay.Seconds()
expectedN := int(1.2 * (float64(itemsCount) + batchSize - 1) / batchSize)
if n > expectedN {
t.Fatalf("Unexpected number of batch func calls: %d. Expected no more than %d. itemsCount=%d, maxDelay=%s",
n, expectedN, itemsCount, maxDelay)
}
if itemsCount != nn {
t.Fatalf("Unexpected number of items passed to batcher func: %d. Expected %d. maxDelay=%s", nn, itemsCount, maxDelay)
}
}
func testBatcherPushMaxBatchSize(t *testing.T, itemsCount, batchSize int) {
n := 0
nn := 0
b := &Batcher{
Func: func(batch []interface{}) {
if len(batch) > batchSize {
t.Fatalf("Unexpected batch size=%d. Must not exceed %d. itemsCount=%d", len(batch), batchSize, itemsCount)
}
if len(batch) == 0 {
t.Fatalf("Empty batch. itemsCount=%d, batchSize=%d", itemsCount, batchSize)
}
nn += len(batch)
n++
},
MaxDelay: time.Hour,
MaxBatchSize: batchSize,
}
b.Start()
for i := 0; i < itemsCount; i++ {
if !b.Push(i) {
t.Fatalf("cannot add item %d to batch", i)
}
}
b.Stop()
expectedN := (itemsCount + batchSize - 1) / batchSize
if n != expectedN {
t.Fatalf("Unexpected number of batcher func calls: %d. Expected %d. itemsCount=%d, batchSize=%d",
n, expectedN, itemsCount, batchSize)
}
if nn != itemsCount {
t.Fatalf("Unexpected number of items in all batches: %d. Expected %d. batchSize=%d", nn, itemsCount, batchSize)
}
}
func expectPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("Expecting panic")
}
}()
f()
}