@@ -221,6 +221,7 @@ func TestPersistableChannelQueue_Pause(t *testing.T) {
221
221
222
222
queueShutdown := []func (){}
223
223
queueTerminate := []func (){}
224
+ terminated := make (chan struct {})
224
225
225
226
tmpDir , err := os .MkdirTemp ("" , "persistable-channel-queue-pause-test-data" )
226
227
assert .NoError (t , err )
@@ -237,15 +238,18 @@ func TestPersistableChannelQueue_Pause(t *testing.T) {
237
238
}, & testData {})
238
239
assert .NoError (t , err )
239
240
240
- go queue .Run (func (shutdown func ()) {
241
- lock .Lock ()
242
- defer lock .Unlock ()
243
- queueShutdown = append (queueShutdown , shutdown )
244
- }, func (terminate func ()) {
245
- lock .Lock ()
246
- defer lock .Unlock ()
247
- queueTerminate = append (queueTerminate , terminate )
248
- })
241
+ go func () {
242
+ queue .Run (func (shutdown func ()) {
243
+ lock .Lock ()
244
+ defer lock .Unlock ()
245
+ queueShutdown = append (queueShutdown , shutdown )
246
+ }, func (terminate func ()) {
247
+ lock .Lock ()
248
+ defer lock .Unlock ()
249
+ queueTerminate = append (queueTerminate , terminate )
250
+ })
251
+ close (terminated )
252
+ }()
249
253
250
254
// Shutdown and Terminate in defer
251
255
defer func () {
@@ -417,14 +421,18 @@ func TestPersistableChannelQueue_Pause(t *testing.T) {
417
421
case <- handleChan :
418
422
assert .Fail (t , "Handler processing should have stopped" )
419
423
return
420
- default :
424
+ case <- terminated :
425
+ case <- time .After (10 * time .Second ):
426
+ assert .Fail (t , "Queue should have terminated" )
427
+ return
421
428
}
422
429
423
430
lock .Lock ()
424
431
pushBack = true
425
432
lock .Unlock ()
426
433
427
434
// Reopen queue
435
+ terminated = make (chan struct {})
428
436
queue , err = NewPersistableChannelQueue (handle , PersistableChannelQueueConfiguration {
429
437
DataDir : tmpDir ,
430
438
BatchLength : 1 ,
@@ -442,15 +450,18 @@ func TestPersistableChannelQueue_Pause(t *testing.T) {
442
450
443
451
paused , _ = pausable .IsPausedIsResumed ()
444
452
445
- go queue .Run (func (shutdown func ()) {
446
- lock .Lock ()
447
- defer lock .Unlock ()
448
- queueShutdown = append (queueShutdown , shutdown )
449
- }, func (terminate func ()) {
450
- lock .Lock ()
451
- defer lock .Unlock ()
452
- queueTerminate = append (queueTerminate , terminate )
453
- })
453
+ go func () {
454
+ queue .Run (func (shutdown func ()) {
455
+ lock .Lock ()
456
+ defer lock .Unlock ()
457
+ queueShutdown = append (queueShutdown , shutdown )
458
+ }, func (terminate func ()) {
459
+ lock .Lock ()
460
+ defer lock .Unlock ()
461
+ queueTerminate = append (queueTerminate , terminate )
462
+ })
463
+ close (terminated )
464
+ }()
454
465
455
466
select {
456
467
case <- handleChan :
@@ -510,4 +521,31 @@ func TestPersistableChannelQueue_Pause(t *testing.T) {
510
521
511
522
assert .Equal (t , test2 .TestString , result4 .TestString )
512
523
assert .Equal (t , test2 .TestInt , result4 .TestInt )
524
+
525
+ lock .Lock ()
526
+ callbacks = make ([]func (), len (queueShutdown ))
527
+ copy (callbacks , queueShutdown )
528
+ queueShutdown = queueShutdown [:0 ]
529
+ lock .Unlock ()
530
+ // Now shutdown the queue
531
+ for _ , callback := range callbacks {
532
+ callback ()
533
+ }
534
+
535
+ // terminate the queue
536
+ lock .Lock ()
537
+ callbacks = make ([]func (), len (queueTerminate ))
538
+ copy (callbacks , queueTerminate )
539
+ queueShutdown = queueTerminate [:0 ]
540
+ lock .Unlock ()
541
+ for _ , callback := range callbacks {
542
+ callback ()
543
+ }
544
+
545
+ select {
546
+ case <- time .After (10 * time .Second ):
547
+ assert .Fail (t , "Queue should have terminated" )
548
+ return
549
+ case <- terminated :
550
+ }
513
551
}
0 commit comments