@@ -18,14 +18,16 @@ import (
18
18
"github.com/elgopher/batch"
19
19
)
20
20
21
+ var noTimeout = context .Background ()
22
+
21
23
func TestProcessor_Run (t * testing.T ) {
22
24
t .Run ("should run callback on zero-value resource when LoadResource was not provided" , func (t * testing.T ) {
23
25
futureValue := FutureValue [* resource ]()
24
26
25
27
processor := batch .StartProcessor (batch.Options [* resource ]{})
26
28
defer processor .Stop ()
27
29
// when
28
- err := processor .Run ("key" , func (c * resource ) {
30
+ err := processor .Run (noTimeout , "key" , func (c * resource ) {
29
31
futureValue .Set (c )
30
32
})
31
33
require .NoError (t , err )
@@ -47,7 +49,7 @@ func TestProcessor_Run(t *testing.T) {
47
49
})
48
50
defer processor .Stop ()
49
51
// when
50
- err := processor .Run (key , func (r * resource ) {
52
+ err := processor .Run (noTimeout , key , func (r * resource ) {
51
53
futureValue .Set (r )
52
54
})
53
55
require .NoError (t , err )
@@ -68,7 +70,7 @@ func TestProcessor_Run(t *testing.T) {
68
70
})
69
71
defer processor .Stop ()
70
72
// when
71
- err := processor .Run (key , func (r * resource ) {
73
+ err := processor .Run (noTimeout , key , func (r * resource ) {
72
74
r .value = 2
73
75
})
74
76
require .NoError (t , err )
@@ -90,7 +92,7 @@ func TestProcessor_Run(t *testing.T) {
90
92
91
93
started := time .Now ()
92
94
// when
93
- err := processor .Run ("" , func (empty ) {})
95
+ err := processor .Run (noTimeout , "" , func (empty ) {})
94
96
require .NoError (t , err )
95
97
96
98
elapsed := time .Now ().Sub (started )
@@ -103,7 +105,7 @@ func TestProcessor_Run(t *testing.T) {
103
105
104
106
started := time .Now ()
105
107
// when
106
- err := processor .Run ("" , func (empty ) {})
108
+ err := processor .Run (noTimeout , "" , func (empty ) {})
107
109
require .NoError (t , err )
108
110
109
111
elapsed := time .Now ().Sub (started )
@@ -126,12 +128,12 @@ func TestProcessor_Run(t *testing.T) {
126
128
127
129
key := ""
128
130
129
- err := processor .Run (key , func (empty ) {
131
+ err := processor .Run (noTimeout , key , func (empty ) {
130
132
time .Sleep (100 * time .Millisecond )
131
133
})
132
134
require .NoError (t , err )
133
135
134
- err = processor .Run (key , func (empty ) {})
136
+ err = processor .Run (noTimeout , key , func (empty ) {})
135
137
require .NoError (t , err )
136
138
137
139
assert .Equal (t , int32 (2 ), atomic .LoadInt32 (& batchCount ))
@@ -154,7 +156,7 @@ func TestProcessor_Run(t *testing.T) {
154
156
)
155
157
defer processor .Stop ()
156
158
// when
157
- err := processor .Run (key , func (* resource ) {})
159
+ err := processor .Run (noTimeout , key , func (* resource ) {})
158
160
// then
159
161
assert .ErrorIs (t , err , customError )
160
162
// and
@@ -181,7 +183,7 @@ func TestProcessor_Run(t *testing.T) {
181
183
)
182
184
defer processor .Stop ()
183
185
// when
184
- err := processor .Run (key , func (* resource ) {})
186
+ err := processor .Run (noTimeout , key , func (* resource ) {})
185
187
// then
186
188
assert .ErrorIs (t , err , timeoutError )
187
189
// and
@@ -206,7 +208,7 @@ func TestProcessor_Run(t *testing.T) {
206
208
)
207
209
defer processor .Stop ()
208
210
// when
209
- err := processor .Run (key , func (empty ) {})
211
+ err := processor .Run (noTimeout , key , func (empty ) {})
210
212
// then
211
213
assert .ErrorIs (t , err , timeoutError )
212
214
})
@@ -229,7 +231,7 @@ func TestProcessor_Run(t *testing.T) {
229
231
230
232
for i := 0 ; i < iterations ; i ++ {
231
233
go func () {
232
- err := processor .Run ("key" , func (r * resource ) {
234
+ err := processor .Run (noTimeout , "key" , func (r * resource ) {
233
235
r .value ++ // value is not guarded so data race should be reported by `go test`
234
236
})
235
237
require .NoError (t , err )
@@ -240,14 +242,43 @@ func TestProcessor_Run(t *testing.T) {
240
242
241
243
group .Wait ()
242
244
})
245
+
246
+ t .Run ("should cancel operation if operation is still waiting to be run" , func (t * testing.T ) {
247
+ processor := batch .StartProcessor (batch.Options [empty ]{})
248
+ defer processor .Stop ()
249
+
250
+ var slowOperationStarted sync.WaitGroup
251
+ slowOperationStarted .Add (1 )
252
+
253
+ var slowOperationStopped sync.WaitGroup
254
+ slowOperationStopped .Add (1 )
255
+
256
+ key := "key"
257
+
258
+ go processor .Run (noTimeout , key , func (empty ) {
259
+ slowOperationStarted .Done ()
260
+ slowOperationStopped .Wait ()
261
+ })
262
+
263
+ slowOperationStarted .Wait ()
264
+
265
+ ctx , cancel := context .WithCancel (context .Background ())
266
+ // when
267
+ cancel ()
268
+ err := processor .Run (ctx , key , func (empty ) {})
269
+ // then
270
+ assert .ErrorIs (t , err , batch .OperationCancelled )
271
+ // cleanup
272
+ slowOperationStopped .Done ()
273
+ })
243
274
}
244
275
245
276
func TestProcessor_Stop (t * testing.T ) {
246
277
t .Run ("after Stop no new operation can be run" , func (t * testing.T ) {
247
278
processor := batch .StartProcessor (batch.Options [empty ]{})
248
279
processor .Stop ()
249
280
250
- err := processor .Run ("key" , func (empty ) {})
281
+ err := processor .Run (noTimeout , "key" , func (empty ) {})
251
282
assert .ErrorIs (t , err , batch .ProcessorStopped )
252
283
})
253
284
@@ -269,7 +300,7 @@ func TestProcessor_Stop(t *testing.T) {
269
300
started := time .Now ()
270
301
271
302
go func () {
272
- err := processor .Run ("key" , func (empty ) {
303
+ err := processor .Run (noTimeout , "key" , func (empty ) {
273
304
operationExecuted .Done ()
274
305
})
275
306
require .NoError (t , err )
@@ -301,7 +332,7 @@ func TestProcessor_Stop(t *testing.T) {
301
332
},
302
333
)
303
334
go func () {
304
- _ = processor .Run ("key" , func (empty ) {
335
+ _ = processor .Run (noTimeout , "key" , func (empty ) {
305
336
operationExecuted .Done ()
306
337
})
307
338
}()
0 commit comments