-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrobatch_test.go
239 lines (185 loc) · 5.7 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
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
package microbatch
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_New_errors_on_nil_bp(t *testing.T) {
mb, err := New[any, any](nil)
assert.Nil(t, mb)
assert.NotNil(t, err)
assert.Equal(t, ErrBatchProcessorRequired, err)
}
func Test_New_creates_MicroBatch(t *testing.T) {
mb, err := newTestMicroBatch()
assert.NotNil(t, mb)
assert.Nil(t, err)
}
func Test_New_applies_Limit(t *testing.T) {
expectedLimit := 42
mb, err := newTestMicroBatch(Limit[*JobInput, *JobOutput](expectedLimit))
assert.NotNil(t, mb)
assert.Nil(t, err)
assert.Equal(t, expectedLimit, mb.limit)
}
func Test_New_applies_Cycle(t *testing.T) {
expectedCycle := 42 * time.Second
mb, err := newTestMicroBatch(Cycle[*JobInput, *JobOutput](expectedCycle))
assert.NotNil(t, mb)
assert.Nil(t, err)
assert.Equal(t, expectedCycle, mb.cycle)
}
func Test_New_does_not_start_MicroBatch(t *testing.T) {
mb, _ := newTestMicroBatch()
assert.False(t, mb.isRunning.Load())
}
func Test_NewRunning_starts_MicroBatch(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
mb, _ := NewRunning(c, newTestBatchProcessor())
time.Sleep(1 * time.Millisecond) // give it a tick so that Start routine is triggered
assert.True(t, mb.isRunning.Load())
}
func Test_setIsRunning_updates_state(t *testing.T) {
mb, _ := newTestMicroBatch()
err := mb.setIsRunning(true)
assert.Nil(t, err)
assert.True(t, mb.isRunning.Load())
}
func Test_setIsRunning_erros_same_value(t *testing.T) {
mb, _ := newTestMicroBatch()
mb.setIsRunning(true)
err := mb.setIsRunning(true)
assert.NotNil(t, err)
assert.Equal(t, ErrSameRunningState, err)
}
func Test_Start_runs_MicroBatch(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
mb, _ := newTestMicroBatch()
go mb.Start(c)
assert.Eventually(t, func() bool {
return mb.isRunning.Load()
}, 1*time.Second, 1*time.Millisecond)
}
func Test_Start_errors_on_second_call(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
mb, _ := newTestMicroBatch()
go mb.Start(c)
time.Sleep(1 * time.Millisecond) // give it a tick so that Start routine is triggered
err := mb.Start(c)
assert.NotNil(t, err)
assert.Equal(t, ErrSameRunningState, err)
}
func Test_Submit_errors_on_not_running(t *testing.T) {
mb, _ := newTestMicroBatch()
job := func(t *JobInput) *JobResult[*JobOutput] {
return nil
}
err := mb.Submit(job)
assert.NotNil(t, err)
assert.Equal(t, ErrIsNotRunning, err)
}
func Test_Submit_errors_on_nil_job(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
mb, _ := newTestMicroBatch()
go mb.Start(c)
time.Sleep(1 * time.Millisecond) // give it a tick so that Start routine is triggered
err := mb.Submit(nil)
assert.NotNil(t, err)
assert.Equal(t, ErrNilJob, err)
}
func Test_Submit_submits_job(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
mb, _ := newTestMicroBatch()
go mb.Start(c)
time.Sleep(1 * time.Millisecond) // give it a tick so that Start routine is triggered
job := func(t *JobInput) *JobResult[*JobOutput] {
return &JobResult[*JobOutput]{}
}
err := mb.Submit(job)
assert.Nil(t, err)
}
func Test_Submit_processes_job_eventually(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
mb, _ := newTestMicroBatch(Limit[*JobInput, *JobOutput](1))
go mb.Start(c)
time.Sleep(1 * time.Millisecond) // give it a tick so that Start routine is triggered
var wg sync.WaitGroup
var check atomic.Bool
wg.Add(1)
job := func(t *JobInput) *JobResult[*JobOutput] {
check.Store(true)
wg.Done()
return &JobResult[*JobOutput]{}
}
assert.False(t, check.Load())
mb.Submit(job)
wg.Wait()
assert.True(t, check.Load())
}
func Test_Submit_submits_and_completes_all_the_jobs(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
bp := newTestBatchProcessor()
limit := Limit[*JobInput, *JobOutput](1) // set batch size to 1
mb, _ := New(bp, limit)
go mb.Start(c)
time.Sleep(1 * time.Millisecond) // give it a tick so that Start routine is triggered
var wg sync.WaitGroup
wg.Add(3)
// submit 3 jobs, which is more than the batch size 1
mb.Submit(func(t *JobInput) *JobResult[*JobOutput] {
defer wg.Done()
return &JobResult[*JobOutput]{}
})
mb.Submit(func(t *JobInput) *JobResult[*JobOutput] {
defer wg.Done()
return &JobResult[*JobOutput]{}
})
mb.Submit(func(t *JobInput) *JobResult[*JobOutput] {
defer wg.Done()
return &JobResult[*JobOutput]{}
})
// make sure to wait for the jobs completion
wg.Wait()
assert.Equal(t, 3, bp.getJobsDone())
}
func Test_Submit_submits_and_completes_all_the_jobs_eventually(t *testing.T) {
c, cancel := context.WithCancel(context.TODO())
defer cancel()
bp := newTestBatchProcessor()
limit := Limit[*JobInput, *JobOutput](5) // set batch size to 5
cycle := Cycle[*JobInput, *JobOutput](1 * time.Millisecond)
mb, _ := New(bp, limit, cycle)
go mb.Start(c)
time.Sleep(1 * time.Millisecond) // give it a tick so that Start routine is triggered
var wg sync.WaitGroup
wg.Add(3)
// submit only 3 jobs, which is less than the batch size 5
mb.Submit(func(t *JobInput) *JobResult[*JobOutput] {
defer wg.Done()
return &JobResult[*JobOutput]{}
})
mb.Submit(func(t *JobInput) *JobResult[*JobOutput] {
defer wg.Done()
return &JobResult[*JobOutput]{}
})
mb.Submit(func(t *JobInput) *JobResult[*JobOutput] {
defer wg.Done()
return &JobResult[*JobOutput]{}
})
// make sure to wait for the jobs completion
wg.Wait()
assert.Equal(t, 3, bp.getJobsDone())
}
func newTestMicroBatch(options ...Option[*JobInput, *JobOutput]) (*MicroBatch[*JobInput, *JobOutput], error) {
return New(newTestBatchProcessor(), options...)
}