Skip to content

Commit 0979d00

Browse files
committed
add testcase for ctx cancel
1 parent 481f338 commit 0979d00

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

workerpool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"sync"
99
)
1010

11-
// ShutdownMode defines how the worker pool behaves during shutdown
11+
// ShutdownMode defines how the worker pool behaves during shutdown.
1212
type ShutdownMode int
1313

1414
const (

workerpool_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ func testConcurrentSubmitsAndClose(t *testing.T, testSize, bufferSize int) {
348348
}
349349

350350
type WorkerPoolTestCaseMidFlight struct {
351-
enabled bool
352351
onSenderID int
353352
onSendCount int
354353
fn func(ctx context.Context, ctxCancelFn func(), subject *WorkerPool[int])
@@ -399,8 +398,7 @@ func (tc WorkerPoolTestCase) Test(t *testing.T) {
399398

400399
countSent.Add(1)
401400

402-
if tc.midFlight.enabled &&
403-
tc.midFlight.fn != nil &&
401+
if tc.midFlight.fn != nil &&
404402
tc.midFlight.onSenderID == senderID &&
405403
tc.midFlight.onSendCount == i {
406404
tc.midFlight.fn(ctx, ctxCancel, subject)
@@ -487,7 +485,6 @@ func TestFlow(t *testing.T) {
487485
sendsPerSender: -1,
488486
stop: false,
489487
midFlight: WorkerPoolTestCaseMidFlight{
490-
enabled: true,
491488
onSenderID: 2,
492489
onSendCount: 15000,
493490
fn: func(ctx context.Context, ctxCancelFn func(), subject *WorkerPool[int]) {
@@ -507,7 +504,6 @@ func TestFlow(t *testing.T) {
507504
sendsPerSender: -1,
508505
stop: false,
509506
midFlight: WorkerPoolTestCaseMidFlight{
510-
enabled: true,
511507
onSenderID: 2,
512508
onSendCount: 15000,
513509
fn: func(ctx context.Context, ctxCancelFn func(), subject *WorkerPool[int]) {
@@ -519,6 +515,25 @@ func TestFlow(t *testing.T) {
519515
assert.Less(t, itemsProcessed, itemsSent)
520516
},
521517
},
518+
"5 workers 5 sender - stop because of ctx cancel ShutdownModeImmediate": {
519+
opts: []func(*config){WithChannelBufferSize(100), WithShutdownMode(ShutdownModeImmediate)},
520+
callback: noop,
521+
workers: 5,
522+
senders: 5,
523+
sendsPerSender: -1,
524+
stop: false,
525+
midFlight: WorkerPoolTestCaseMidFlight{
526+
onSenderID: 2,
527+
onSendCount: 15000,
528+
fn: func(ctx context.Context, ctxCancelFn func(), subject *WorkerPool[int]) {
529+
ctxCancelFn()
530+
},
531+
},
532+
assertErrorOnSubmit: ifExistsIs(context.Canceled),
533+
asserts: func(t *testing.T, itemsSent, itemsProcessed uint64) {
534+
assert.Less(t, itemsProcessed, itemsSent)
535+
},
536+
},
522537
}
523538

524539
for name, tc := range tests {

0 commit comments

Comments
 (0)