def callback(message): """Count to, 2, fire a deferred, then count to 3 and quit.""" messages_received.append(message) if len(messages_received) == 2: two_received.callback(None) if len(messages_received) == 3: raise exceptions.HaltConsumer()
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_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")
def test_consume_halt_without_exitcode(self, mock_consume, mock_importlib): """Assert user execution halt is reported.""" cli_options = {"callback": "mod:callable"} mock_mod_with_callable = mock.Mock(spec=["callable"]) mock_importlib.import_module.return_value = mock_mod_with_callable mock_consume.side_effect = exceptions.HaltConsumer(exit_code=0) result = self.runner.invoke( cli.cli, ["consume", "--callback=" + cli_options["callback"]]) mock_importlib.import_module.called_once_with("mod") mock_consume.assert_called_once_with(mock_mod_with_callable.callable, "b") self.assertEqual(0, result.exit_code)
def test_consume_halt_with_exitcode(self, mock_consume, mock_importlib): """Assert user execution halt with reason and exit_code is reported.""" cli_options = {"callback": "mod:callable"} halt_message = "User halted execution" halt_exit_code = 5 mock_mod_with_callable = mock.Mock(spec=["callable"]) mock_importlib.import_module.return_value = mock_mod_with_callable mock_consume.side_effect = exceptions.HaltConsumer( exit_code=halt_exit_code, reason=halt_message) with mock.patch("fedora_messaging.cli._log") as mock_log: result = self.runner.invoke( cli.cli, ["consume", "--callback=" + cli_options["callback"]]) mock_importlib.import_module.called_once_with("mod") mock_consume.assert_called_once_with(mock_mod_with_callable.callable, "b") mock_log.error.assert_called_once_with( "Consumer halted with non-zero exit code (%d): %s", 5, "User halted execution", ) self.assertEqual(halt_exit_code, result.exit_code)
def counting_callback(message, storage=defaultdict(int)): storage[message.topic] += 1 if storage[message.topic] == 3: raise exceptions.HaltConsumer()
def callback(message): messages_received.append(message) if len(messages_received) == 3: raise exceptions.HaltConsumer()
def callback(message): """Nack the message, then halt.""" nacked_messages.append(message) if len(nacked_messages) == 2: raise exceptions.HaltConsumer() raise exceptions.Nack()
def callback(message): """Drop 1 message and then halt on the second message.""" dropped_messages.append(message) if len(dropped_messages) == 2: raise exceptions.HaltConsumer() raise exceptions.Drop()
def callback(message): """Count to 3 and quit.""" raise exceptions.HaltConsumer(exit_code=1, requeue=True)
def callback(message): """Count to 3 and quit.""" messages_received.append(message) if len(messages_received) == 3: raise exceptions.HaltConsumer()
def halt_exit_42(message): """Exit with code 42 when it gets a message""" raise exceptions.HaltConsumer(exit_code=42, reason="Life, the universe, and everything")
def halt_exit_0(message): """Exit with code 0 when it gets a message""" raise exceptions.HaltConsumer()