-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrobatch_test.go
98 lines (82 loc) · 2.3 KB
/
microbatch_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
package microbatch
import (
"reflect"
"testing"
)
func waitForJobsToRun[J any, R any](mb *MicroBatcher[J, R]) {
// We need to stop because this is the last opportunity before blocking indefinitely.
mb.Stop()
// We need to wait to make sure everything has processed before testing.
mb.WaitForResults()
}
func TestSimpleBatch(t *testing.T) {
fakeBatchProcessor := NewFakeBatchProcessor()
fakeResultHandler := NewFakeResultHandler()
config := Config[int, string]{
BatchProcessor: fakeBatchProcessor,
ResultHandler: fakeResultHandler,
Frequency: 100,
MaxSize: 10,
}
simpleTicker := NewSimpleTicker()
mb := StartWithTicker(config, simpleTicker)
mb.SubmitJob(0)
mb.SubmitJob(1)
mb.SubmitJob(2)
simpleTicker.Tick()
waitForJobsToRun(mb)
if !reflect.DeepEqual(fakeBatchProcessor.calls[0], []int{0, 1, 2}) {
t.Fatalf("should have called fakeBatchProcessor with all input data")
}
if fakeResultHandler.calls[0] != "some result" {
t.Fatalf("should have called fakeResultHandler with result")
}
}
func TestTimeCycles(t *testing.T) {
fakeBatchProcessor := NewFakeBatchProcessor()
fakeResultHandler := NewFakeResultHandler()
config := Config[int, string]{
BatchProcessor: fakeBatchProcessor,
ResultHandler: fakeResultHandler,
Frequency: 100,
MaxSize: 10,
}
simpleTicker := NewSimpleTicker()
mb := StartWithTicker(config, simpleTicker)
mb.SubmitJob(0)
mb.SubmitJob(1)
mb.SubmitJob(2)
simpleTicker.Tick()
simpleTicker.Tick() // should not trigger an additional batch
mb.SubmitJob(0)
mb.SubmitJob(1)
mb.SubmitJob(2)
simpleTicker.Tick()
waitForJobsToRun(mb)
if len(fakeBatchProcessor.calls) != 2 {
t.Fatalf("should have created 2 batches")
}
}
func TestMaxSize(t *testing.T) {
fakeBatchProcessor := NewFakeBatchProcessor()
fakeResultHandler := NewFakeResultHandler()
config := Config[int, string]{
BatchProcessor: fakeBatchProcessor,
ResultHandler: fakeResultHandler,
Frequency: 100,
MaxSize: 3,
}
simpleTicker := NewSimpleTicker()
mb := StartWithTicker(config, simpleTicker)
mb.SubmitJob(0)
mb.SubmitJob(1)
mb.SubmitJob(2)
mb.SubmitJob(3)
mb.SubmitJob(4)
mb.SubmitJob(5)
mb.SubmitJob(6)
waitForJobsToRun(mb)
if len(fakeBatchProcessor.calls) != 3 {
t.Fatalf("should have hit the maxSize limit twice and batched the remaining job")
}
}