def test_callback(self, mock_reactor): """Assert when the last consumer calls back, the reactor stops.""" consumers = (consumer.Consumer(), consumer.Consumer()) cli._consume_callback(consumers) consumers[0].result.callback(consumers[0]) self.assertEqual(0, mock_reactor.stop.call_count) consumers[1].result.callback(consumers[1]) self.assertEqual(1, mock_reactor.stop.call_count)
def test_errback_canceled(self, mock_reactor): """Assert exit code is 2 when the consumer is canceled.""" consumers = (consumer.Consumer(),) e = exceptions.ConsumerCanceled() f = failure.Failure(e, exceptions.ConsumerCanceled) cli._consume_callback(consumers) consumers[0].result.errback(f) self.assertEqual(1, mock_reactor.stop.call_count) self.assertEqual(12, cli._exit_code)
def test_errback_halt_consumer(self, mock_reactor): """Assert _exit_code is set with the HaltConsumer code.""" consumers = (consumer.Consumer(),) e = exceptions.HaltConsumer() f = failure.Failure(e, exceptions.HaltConsumer) cli._consume_callback(consumers) consumers[0].result.errback(f) self.assertEqual(1, mock_reactor.stop.call_count) self.assertEqual(0, cli._exit_code)
def test_callback_reactor_stopped(self, mock_reactor): """Assert already-stopped reactor is handled.""" consumers = (consumer.Consumer(), consumer.Consumer()) mock_reactor.stop.side_effect = error.ReactorNotRunning() cli._consume_callback(consumers) try: consumers[0].result.callback(consumers[0]) consumers[1].result.callback(consumers[1]) self.assertEqual(1, mock_reactor.stop.call_count) except error.ReactorNotRunning: self.fail("ReactorNotRunning exception wasn't handled.")
def test_errback_general_exception(self, mock_log, mock_reactor): """Assert exit code is 1 when an unexpected error occurs.""" consumers = (consumer.Consumer(), ) e = Exception("boom") f = failure.Failure(e, Exception) cli._consume_callback(consumers) consumers[0].result.errback(f) self.assertEqual(1, mock_reactor.stop.call_count) self.assertEqual(13, cli._exit_code) mock_log.error.assert_called_once_with( "Unexpected error occurred in consumer %r: %r", consumers[0], f)
def test_errback_halt_consumer_nonzero(self, mock_log, mock_reactor): """Assert _exit_code is set with the HaltConsumer code and logged if non-zero""" consumers = (consumer.Consumer(), ) e = exceptions.HaltConsumer(exit_code=42) f = failure.Failure(e, exceptions.HaltConsumer) cli._consume_callback(consumers) consumers[0].result.errback(f) self.assertEqual(1, mock_reactor.stop.call_count) self.assertEqual(42, cli._exit_code) mock_log.error.assert_called_with( "Consumer halted with non-zero exit code (%d): %s", 42, "None")