19
19
from celery import current_app
20
20
from celery .apps import worker as cd
21
21
from celery .bin .celeryd import WorkerCommand , main as celeryd_main
22
- from celery .exceptions import ImproperlyConfigured
22
+ from celery .exceptions import ImproperlyConfigured , SystemTerminate
23
23
from celery .utils .log import ensure_process_aware_logger
24
24
from celery .worker import state
25
25
@@ -32,12 +32,17 @@ def disable_stdouts(fun):
32
32
33
33
@wraps (fun )
34
34
def disable (* args , ** kwargs ):
35
- sys .stdout , sys .stderr = WhateverIO (), WhateverIO ()
35
+ prev_out , prev_err = sys .stdout , sys .stderr
36
+ prev_rout , prev_rerr = sys .__stdout__ , sys .__stderr__
37
+ sys .stdout = sys .__stdout__ = WhateverIO ()
38
+ sys .stderr = sys .__stderr__ = WhateverIO ()
36
39
try :
37
40
return fun (* args , ** kwargs )
38
41
finally :
39
- sys .stdout = sys .__stdout__
40
- sys .stderr = sys .__stderr__
42
+ sys .stdout = prev_out
43
+ sys .stderr = prev_err
44
+ sys .__stdout__ = prev_rout
45
+ sys .__stderr__ = prev_rerr
41
46
42
47
return disable
43
48
@@ -58,6 +63,9 @@ class Worker(cd.Worker):
58
63
class test_Worker (AppCase ):
59
64
Worker = Worker
60
65
66
+ def teardown (self ):
67
+ self .app .conf .CELERY_INCLUDE = ()
68
+
61
69
@disable_stdouts
62
70
def test_queues_string (self ):
63
71
celery = Celery (set_as_current = False )
@@ -402,19 +410,33 @@ class Signals(platforms.Signals):
402
410
def __setitem__ (self , sig , handler ):
403
411
next_handlers [sig ] = handler
404
412
405
- p , platforms .signals = platforms .signals , Signals ()
406
- try :
407
- handlers ["SIGINT" ]("SIGINT" , object ())
408
- self .assertTrue (state .should_stop )
409
- finally :
410
- platforms .signals = p
411
- state .should_stop = False
413
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
414
+ c .return_value = 3
415
+ p , platforms .signals = platforms .signals , Signals ()
416
+ try :
417
+ handlers ["SIGINT" ]("SIGINT" , object ())
418
+ self .assertTrue (state .should_stop )
419
+ finally :
420
+ platforms .signals = p
421
+ state .should_stop = False
412
422
413
- try :
414
- next_handlers ["SIGINT" ]("SIGINT" , object ())
415
- self .assertTrue (state .should_terminate )
416
- finally :
417
- state .should_terminate = False
423
+ try :
424
+ next_handlers ["SIGINT" ]("SIGINT" , object ())
425
+ self .assertTrue (state .should_terminate )
426
+ finally :
427
+ state .should_terminate = False
428
+
429
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
430
+ c .return_value = 1
431
+ p , platforms .signals = platforms .signals , Signals ()
432
+ try :
433
+ with self .assertRaises (SystemExit ):
434
+ handlers ["SIGINT" ]("SIGINT" , object ())
435
+ finally :
436
+ platforms .signals = p
437
+
438
+ with self .assertRaises (SystemTerminate ):
439
+ next_handlers ["SIGINT" ]("SIGINT" , object ())
418
440
419
441
@disable_stdouts
420
442
def test_worker_int_handler_only_stop_MainProcess (self ):
@@ -424,14 +446,27 @@ def test_worker_int_handler_only_stop_MainProcess(self):
424
446
raise SkipTest ("only relevant for multiprocessing" )
425
447
process = current_process ()
426
448
name , process .name = process .name , "OtherProcess"
427
- try :
428
- worker = self ._Worker ()
429
- handlers = self .psig (cd .install_worker_int_handler , worker )
430
- handlers ["SIGINT" ]("SIGINT" , object ())
431
- self .assertTrue (state .should_stop )
432
- finally :
433
- process .name = name
434
- state .should_stop = False
449
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
450
+ c .return_value = 3
451
+ try :
452
+ worker = self ._Worker ()
453
+ handlers = self .psig (cd .install_worker_int_handler , worker )
454
+ handlers ["SIGINT" ]("SIGINT" , object ())
455
+ self .assertTrue (state .should_stop )
456
+ finally :
457
+ process .name = name
458
+ state .should_stop = False
459
+
460
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
461
+ c .return_value = 1
462
+ try :
463
+ worker = self ._Worker ()
464
+ handlers = self .psig (cd .install_worker_int_handler , worker )
465
+ with self .assertRaises (SystemExit ):
466
+ handlers ["SIGINT" ]("SIGINT" , object ())
467
+ finally :
468
+ process .name = name
469
+ state .should_stop = False
435
470
436
471
@disable_stdouts
437
472
def test_install_HUP_not_supported_handler (self ):
@@ -448,25 +483,49 @@ def test_worker_term_hard_handler_only_stop_MainProcess(self):
448
483
process = current_process ()
449
484
name , process .name = process .name , "OtherProcess"
450
485
try :
486
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
487
+ c .return_value = 3
488
+ worker = self ._Worker ()
489
+ handlers = self .psig (
490
+ cd .install_worker_term_hard_handler , worker )
491
+ try :
492
+ handlers ["SIGQUIT" ]("SIGQUIT" , object ())
493
+ self .assertTrue (state .should_terminate )
494
+ finally :
495
+ state .should_terminate = False
496
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
497
+ c .return_value = 1
498
+ worker = self ._Worker ()
499
+ handlers = self .psig (
500
+ cd .install_worker_term_hard_handler , worker )
501
+ with self .assertRaises (SystemTerminate ):
502
+ handlers ["SIGQUIT" ]("SIGQUIT" , object ())
503
+ finally :
504
+ process .name = name
505
+
506
+ @disable_stdouts
507
+ def test_worker_term_handler_when_threads (self ):
508
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
509
+ c .return_value = 3
451
510
worker = self ._Worker ()
452
- handlers = self .psig (cd .install_worker_term_hard_handler , worker )
511
+ handlers = self .psig (cd .install_worker_term_handler , worker )
453
512
try :
454
- handlers ["SIGQUIT " ]("SIGQUIT " , object ())
455
- self .assertTrue (state .should_terminate )
513
+ handlers ["SIGTERM " ]("SIGTERM " , object ())
514
+ self .assertTrue (state .should_stop )
456
515
finally :
457
- state .should_terminate = False
458
- finally :
459
- process .name = name
516
+ state .should_stop = False
460
517
461
518
@disable_stdouts
462
- def test_worker_term_handler (self ):
463
- worker = self ._Worker ()
464
- handlers = self .psig (cd .install_worker_term_handler , worker )
465
- try :
466
- handlers ["SIGTERM" ]("SIGTERM" , object ())
467
- self .assertTrue (state .should_stop )
468
- finally :
469
- state .should_stop = False
519
+ def test_worker_term_handler_when_single_thread (self ):
520
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
521
+ c .return_value = 1
522
+ worker = self ._Worker ()
523
+ handlers = self .psig (cd .install_worker_term_handler , worker )
524
+ try :
525
+ with self .assertRaises (SystemExit ):
526
+ handlers ["SIGTERM" ]("SIGTERM" , object ())
527
+ finally :
528
+ state .should_stop = False
470
529
471
530
@patch ("sys.__stderr__" )
472
531
def test_worker_cry_handler (self , stderr ):
@@ -490,10 +549,18 @@ def test_worker_term_handler_only_stop_MainProcess(self):
490
549
process = current_process ()
491
550
name , process .name = process .name , "OtherProcess"
492
551
try :
493
- worker = self ._Worker ()
494
- handlers = self .psig (cd .install_worker_term_handler , worker )
495
- handlers ["SIGTERM" ]("SIGTERM" , object ())
496
- self .assertTrue (state .should_stop )
552
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
553
+ c .return_value = 3
554
+ worker = self ._Worker ()
555
+ handlers = self .psig (cd .install_worker_term_handler , worker )
556
+ handlers ["SIGTERM" ]("SIGTERM" , object ())
557
+ self .assertTrue (state .should_stop )
558
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
559
+ c .return_value = 1
560
+ worker = self ._Worker ()
561
+ handlers = self .psig (cd .install_worker_term_handler , worker )
562
+ with self .assertRaises (SystemExit ):
563
+ handlers ["SIGTERM" ]("SIGTERM" , object ())
497
564
finally :
498
565
process .name = name
499
566
state .should_stop = False
@@ -521,11 +588,22 @@ def _execv(*args):
521
588
state .should_stop = False
522
589
523
590
@disable_stdouts
524
- def test_worker_term_hard_handler (self ):
525
- worker = self ._Worker ()
526
- handlers = self .psig (cd .install_worker_term_hard_handler , worker )
527
- try :
528
- handlers ["SIGQUIT" ]("SIGQUIT" , object ())
529
- self .assertTrue (state .should_terminate )
530
- finally :
531
- state .should_terminate = False
591
+ def test_worker_term_hard_handler_when_threaded (self ):
592
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
593
+ c .return_value = 3
594
+ worker = self ._Worker ()
595
+ handlers = self .psig (cd .install_worker_term_hard_handler , worker )
596
+ try :
597
+ handlers ["SIGQUIT" ]("SIGQUIT" , object ())
598
+ self .assertTrue (state .should_terminate )
599
+ finally :
600
+ state .should_terminate = False
601
+
602
+ @disable_stdouts
603
+ def test_worker_term_hard_handler_when_single_threaded (self ):
604
+ with patch ("celery.apps.worker.active_thread_count" ) as c :
605
+ c .return_value = 1
606
+ worker = self ._Worker ()
607
+ handlers = self .psig (cd .install_worker_term_hard_handler , worker )
608
+ with self .assertRaises (SystemTerminate ):
609
+ handlers ["SIGQUIT" ]("SIGQUIT" , object ())
0 commit comments