-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_queue_test.go
More file actions
386 lines (316 loc) · 8.54 KB
/
worker_queue_test.go
File metadata and controls
386 lines (316 loc) · 8.54 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package workerqueue
import (
"context"
"errors"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestBasicExecution(t *testing.T) {
wq := New(2, 10)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
defer wq.Stop()
var counter atomic.Int32
task := func(ctx context.Context) error {
counter.Add(1)
return nil
}
// Submit 5 tasks
for range 5 {
if err := wq.Submit(context.Background(), task); err != nil {
t.Fatalf("failed to submit task: %v", err)
}
}
// Wait a bit for tasks to complete
time.Sleep(100 * time.Millisecond)
if got := counter.Load(); got != 5 {
t.Errorf("expected 5 tasks executed, got %d", got)
}
}
func TestBackpressure(t *testing.T) {
// Create queue with 1 worker and capacity of 2
wq := New(1, 2)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
defer wq.Stop()
// Block the worker with a long-running task
blockCh := make(chan struct{})
blockTask := func(ctx context.Context) error {
<-blockCh
return nil
}
// Submit blocking task
if err := wq.Submit(context.Background(), blockTask); err != nil {
t.Fatalf("failed to submit blocking task: %v", err)
}
// Fill the queue (capacity 2, one is being processed)
quickTask := func(ctx context.Context) error { return nil }
if err := wq.Submit(context.Background(), quickTask); err != nil {
t.Fatalf("failed to submit task 1: %v", err)
}
if err := wq.Submit(context.Background(), quickTask); err != nil {
t.Fatalf("failed to submit task 2: %v", err)
}
// Next submit should block - use context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err := wq.Submit(ctx, quickTask)
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("expected deadline exceeded due to backpressure, got: %v", err)
}
// Unblock and verify tasks complete
close(blockCh)
time.Sleep(100 * time.Millisecond)
}
func TestContextCancellation(t *testing.T) {
wq := New(2, 10)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
var started atomic.Int32
var cancelled atomic.Int32
task := func(ctx context.Context) error {
started.Add(1)
select {
case <-time.After(5 * time.Second):
return nil
case <-ctx.Done():
cancelled.Add(1)
return ctx.Err()
}
}
// Submit tasks
for range 5 {
if err := wq.Submit(context.Background(), task); err != nil {
t.Fatalf("failed to submit task: %v", err)
}
}
// Give tasks time to start
time.Sleep(50 * time.Millisecond)
// Stop immediately (cancels context)
wq.Stop()
// Verify some tasks were cancelled
if started.Load() == 0 {
t.Error("no tasks started")
}
if cancelled.Load() == 0 {
t.Error("expected some tasks to be cancelled")
}
}
func TestGracefulShutdown(t *testing.T) {
wq := New(2, 10)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
var counter atomic.Int32
task := func(ctx context.Context) error {
time.Sleep(50 * time.Millisecond)
counter.Add(1)
return nil
}
// Submit 5 tasks
taskCount := 5
for range taskCount {
if err := wq.Submit(context.Background(), task); err != nil {
t.Fatalf("failed to submit task: %v", err)
}
}
// Graceful shutdown with sufficient timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := wq.Shutdown(ctx); err != nil {
t.Errorf("shutdown failed: %v", err)
}
// All tasks should complete
if got := counter.Load(); got != int32(taskCount) {
t.Errorf("expected %d tasks completed, got %d", taskCount, got)
}
}
func TestShutdownTimeout(t *testing.T) {
wq := New(1, 10)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
// Submit a long-running task
longTask := func(ctx context.Context) error {
time.Sleep(2 * time.Second)
return nil
}
if err := wq.Submit(context.Background(), longTask); err != nil {
t.Fatalf("failed to submit task: %v", err)
}
// Shutdown with short timeout
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err := wq.Shutdown(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("expected deadline exceeded, got: %v", err)
}
}
func TestNoGoroutineLeaks(t *testing.T) {
initialGoroutines := runtime.NumGoroutine()
wq := New(5, 20)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
// Submit some tasks
task := func(ctx context.Context) error {
time.Sleep(10 * time.Millisecond)
return nil
}
for range 10 {
if err := wq.Submit(context.Background(), task); err != nil {
t.Fatalf("failed to submit task: %v", err)
}
}
// Graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := wq.Shutdown(ctx); err != nil {
t.Fatalf("shutdown failed: %v", err)
}
// Wait a bit for goroutines to clean up
time.Sleep(100 * time.Millisecond)
finalGoroutines := runtime.NumGoroutine()
if finalGoroutines > initialGoroutines+1 { // +1 for test goroutine tolerance
t.Errorf("goroutine leak detected: initial=%d, final=%d", initialGoroutines, finalGoroutines)
}
}
func TestSubmitBeforeStart(t *testing.T) {
wq := New(2, 10)
task := func(ctx context.Context) error { return nil }
err := wq.Submit(context.Background(), task)
if err == nil {
t.Error("expected error when submitting before start")
}
}
func TestDoubleStart(t *testing.T) {
wq := New(2, 10)
if err := wq.Start(); err != nil {
t.Fatalf("first start failed: %v", err)
}
defer wq.Stop()
err := wq.Start()
if err == nil {
t.Error("expected error on double start")
}
}
func TestSubmitAfterStop(t *testing.T) {
wq := New(2, 10)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
wq.Stop()
// Small delay to ensure context cancellation propagates
time.Sleep(10 * time.Millisecond)
task := func(ctx context.Context) error { return nil }
err := wq.Submit(context.Background(), task)
if err == nil {
t.Error("expected error when submitting after stop")
}
}
func TestConcurrentSubmit(t *testing.T) {
wq := New(5, 100)
if err := wq.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
defer wq.Stop()
var counter atomic.Int32
task := func(ctx context.Context) error {
counter.Add(1)
return nil
}
// Concurrent submissions
var wg sync.WaitGroup
submitters := 10
tasksPerSubmitter := 10
for range submitters {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < tasksPerSubmitter; j++ {
if err := wq.Submit(context.Background(), task); err != nil {
t.Errorf("submit failed: %v", err)
}
}
}()
}
wg.Wait()
// Wait for tasks to complete
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := wq.Shutdown(ctx); err != nil {
t.Fatalf("shutdown failed: %v", err)
}
expected := int32(submitters * tasksPerSubmitter)
if got := counter.Load(); got != expected {
t.Errorf("expected %d tasks executed, got %d", expected, got)
}
}
// Benchmarks
func BenchmarkSubmit(b *testing.B) {
wq := New(4, 1000)
if err := wq.Start(); err != nil {
b.Fatalf("failed to start: %v", err)
}
defer wq.Stop()
task := func(ctx context.Context) error { return nil }
ctx := context.Background()
for b.Loop() {
if err := wq.Submit(ctx, task); err != nil {
b.Fatalf("submit failed: %v", err)
}
}
b.StopTimer()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = wq.Shutdown(shutdownCtx)
}
func BenchmarkWorkerThroughput(b *testing.B) {
wq := New(runtime.NumCPU(), 10000)
if err := wq.Start(); err != nil {
b.Fatalf("failed to start: %v", err)
}
defer wq.Stop()
var counter atomic.Int32
task := func(ctx context.Context) error {
counter.Add(1)
return nil
}
ctx := context.Background()
for b.Loop() {
if err := wq.Submit(ctx, task); err != nil {
b.Fatalf("submit failed: %v", err)
}
}
b.StopTimer()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_ = wq.Shutdown(shutdownCtx)
}
func BenchmarkConcurrentSubmit(b *testing.B) {
wq := New(runtime.NumCPU(), 10000)
if err := wq.Start(); err != nil {
b.Fatalf("failed to start: %v", err)
}
defer wq.Stop()
task := func(ctx context.Context) error { return nil }
ctx := context.Background()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := wq.Submit(ctx, task); err != nil {
b.Fatalf("submit failed: %v", err)
}
}
})
b.StopTimer()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_ = wq.Shutdown(shutdownCtx)
}