@@ -688,7 +688,7 @@ def test_health_check_fails_when_ping_returns_none(self, mock_inspect):
688688 with pytest .raises (SystemExit ) as exc_info :
689689 celery_command .worker_health_check (args )
690690
691- assert exc_info .value . code != 0
691+ assert "did not respond to ping" in str ( exc_info .value )
692692 # active_queues must NOT be called if ping already failed
693693 mock_instance .active_queues .assert_not_called ()
694694
@@ -707,9 +707,43 @@ def test_health_check_fails_when_worker_not_in_ping_result(self, mock_inspect):
707707 with pytest .raises (SystemExit ) as exc_info :
708708 celery_command .worker_health_check (args )
709709
710- assert exc_info .value . code != 0
710+ assert "did not respond to ping" in str ( exc_info .value )
711711 mock_instance .active_queues .assert_not_called ()
712712
713+ @pytest .mark .db_test
714+ @mock .patch ("airflow.providers.celery.executors.celery_executor.app.control.inspect" )
715+ def test_health_check_fails_when_broker_unreachable (self , mock_inspect ):
716+ """Broker is down — inspect.ping() raises an exception. Health check must fail cleanly."""
717+ hostname = "celery@disconnected-host"
718+ args = self .parser .parse_args (["celery" , "worker-health-check" , "-H" , hostname ])
719+
720+ mock_instance = MagicMock ()
721+ mock_instance .ping .side_effect = ConnectionError ("Connection refused" )
722+ mock_inspect .return_value = mock_instance
723+
724+ with pytest .raises (SystemExit ) as exc_info :
725+ celery_command .worker_health_check (args )
726+
727+ assert "broker connection error" in str (exc_info .value )
728+ mock_instance .active_queues .assert_not_called ()
729+
730+ @pytest .mark .db_test
731+ @mock .patch ("airflow.providers.celery.executors.celery_executor.app.control.inspect" )
732+ def test_health_check_fails_when_active_queues_raises (self , mock_inspect ):
733+ """Broker fails during active_queues check — health check must fail cleanly."""
734+ hostname = "celery@flaky-host"
735+ args = self .parser .parse_args (["celery" , "worker-health-check" , "-H" , hostname ])
736+
737+ mock_instance = MagicMock ()
738+ mock_instance .ping .return_value = {hostname : {"ok" : "pong" }}
739+ mock_instance .active_queues .side_effect = ConnectionError ("Connection lost" )
740+ mock_inspect .return_value = mock_instance
741+
742+ with pytest .raises (SystemExit ) as exc_info :
743+ celery_command .worker_health_check (args )
744+
745+ assert "broker connection error" in str (exc_info .value )
746+
713747 @pytest .mark .db_test
714748 @mock .patch ("airflow.providers.celery.executors.celery_executor.app.control.inspect" )
715749 def test_health_check_fails_when_active_queues_returns_none (self , mock_inspect ):
@@ -735,7 +769,7 @@ def test_health_check_fails_when_active_queues_returns_none(self, mock_inspect):
735769 with pytest .raises (SystemExit ) as exc_info :
736770 celery_command .worker_health_check (args )
737771
738- assert exc_info .value . code != 0
772+ assert "catatonic state" in str ( exc_info .value )
739773 mock_instance .ping .assert_called_once ()
740774 mock_instance .active_queues .assert_called_once ()
741775
@@ -760,7 +794,7 @@ def test_health_check_fails_when_worker_not_in_active_queues(self, mock_inspect)
760794 with pytest .raises (SystemExit ) as exc_info :
761795 celery_command .worker_health_check (args )
762796
763- assert exc_info .value . code != 0
797+ assert "catatonic state" in str ( exc_info .value )
764798
765799 @pytest .mark .db_test
766800 @mock .patch ("airflow.providers.celery.executors.celery_executor.app.control.inspect" )
@@ -783,7 +817,7 @@ def test_health_check_fails_when_active_queues_list_is_empty(self, mock_inspect)
783817 with pytest .raises (SystemExit ) as exc_info :
784818 celery_command .worker_health_check (args )
785819
786- assert exc_info .value . code != 0
820+ assert "catatonic state" in str ( exc_info .value )
787821
788822 @pytest .mark .db_test
789823 @mock .patch ("socket.gethostname" )
@@ -805,7 +839,7 @@ def test_health_check_defaults_hostname_from_socket(self, mock_inspect, mock_get
805839 celery_command .worker_health_check (args )
806840
807841 # Verify inspect was targeted at the auto-resolved hostname
808- mock_inspect .assert_called_once_with (destination = [expected_hostname ])
842+ mock_inspect .assert_called_once_with (destination = [expected_hostname ], timeout = 5.0 )
809843
810844
811845class TestLoggerSetupHandler :
0 commit comments