def test_posting_object_state(self): """Given that I have an RMQ producer when I deserialize an object via the producer then I should be able to re-hydrate it via the consumer """ test_topic = "kombu_gateway_tests" + str(uuid4()) request = TestMessage() header = BrightsideMessageHeader(uuid4(), test_topic, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) queue_name = "brightside_tests" + str(uuid4()) consumer = ArameConsumer( self._connection, BrightsideConsumerConfiguration(self._pipeline, queue_name, test_topic)) self._producer.send(message) read_message = consumer.receive(3) consumer.acknowledge(read_message) deserialized_request = JsonRequestSerializer(request=TestMessage(), serialized_request=read_message.body.value)\ .deserialize_from_json() self.assertIsNotNone(deserialized_request) self.assertEqual(request.bool_value, deserialized_request.bool_value) self.assertEqual(request.float_value, deserialized_request.float_value) self.assertEqual(request.integer_value, deserialized_request.integer_value) self.assertEqual(request.id, deserialized_request.id)
def test_posting_object_state(self): """Given that I have an RMQ producer when I deserialize an object via the producer then I should be able to re-hydrate it via the consumer """ request = TestMessage() header = BrightsideMessageHeader(uuid4(), self.test_topic, BrightsideMessageType.command, uuid4()) body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) self._consumer.purge() self._producer.send(message) read_message = self._consumer.receive(3) self._consumer.acknowledge(read_message) deserialized_request = JsonRequestSerializer(request=TestMessage(), serialized_request=read_message.body.value)\ .deserialize_from_json() self.assertIsNotNone(deserialized_request) self.assertEqual(request.bool_value, deserialized_request.bool_value) self.assertEqual(request.float_value, deserialized_request.float_value) self.assertEqual(request.integer_value, deserialized_request.integer_value) self.assertEqual(request.id, deserialized_request.id)
def test_requeueing_a_message(self): """Given that I have an RMQ consumer when I requeue a message then it should return to the end of the queue """ request = TestMessage() header = BrightsideMessageHeader(uuid4(), self.test_topic, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) self._consumer.purge( ) # if errors with akes 1 positional argument but 2 were given then manually purge self._producer.send(message) read_message = self._consumer.receive(3) self._consumer.requeue(read_message) # should now be able to receive it again reread_message = self._consumer.receive(3) self._consumer.acknowledge(reread_message) self.assertEqual(message.id, reread_message.id) self.assertEqual(message.body.value, reread_message.body.value) self.assertTrue(self._consumer.has_acknowledged(reread_message))
def test_the_pump_should_dispatch_a_command_processor(self): """ Given that I have a message pump for a channel When I read a message from that channel Then the message should be dispatched to a handler """ handler = MyCommandHandler() request = MyCommand() channel = Mock(spec=Channel) command_processor = Mock(spec=CommandProcessor) message_pump = MessagePump(command_processor, channel, map_my_command_to_request) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) quit_message = create_quit_message() # add messages to that when channel is called it returns first message then quit message response_queue = [message, quit_message] channel_spec = {"receive.side_effect": response_queue} channel.configure_mock(**channel_spec) message_pump.run() channel.receive.assert_called_with(0.5) self.assertEqual(channel.receive.call_count, 2) self.assertTrue(command_processor.send.call_count, 1) self.assertEqual(channel.acknowledge.call_count, 1)
def test_the_pump_should_fail_on_a_missing_message_mapper(self): """ Given that I have a message pump for a channel When there is no message mapper for that channel Then we shhould throw an exception to indicate a configuration error """ handler = MyCommandHandler() request = MyCommand() channel = Mock(spec=Channel) command_processor = Mock(spec=CommandProcessor) message_pump = MessagePump(command_processor, channel, None) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) quit_message = create_quit_message() # add messages to that when channel is called it returns first message then qui tmessage response_queue = [message, quit_message] channel_spec = {"receive.side_effect": response_queue} channel.configure_mock(**channel_spec) excepton_caught = False try: message_pump.run() except ConfigurationException: excepton_caught = True self.assertTrue(excepton_caught)
def test_the_pump_should_ack_failed_messages(self): """ Given that I have a message pump for a channel When the handler raises an application exception for that message Then ack the message to prevent 'poison pill' message """ handler = MyCommandHandler() request = MyCommand() channel = Mock(spec=Channel) command_processor = Mock(spec=CommandProcessor) message_pump = MessagePump(command_processor, channel, map_my_command_to_request) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) quit_message = create_quit_message() # add messages to that when channel is called it returns first message then qui tmessage response_queue = [message, quit_message] channel_spec = {"receive.side_effect": response_queue} channel.configure_mock(**channel_spec) app_error_spec = {"send.side_effect": ZeroDivisionError()} command_processor.configure_mock(**app_error_spec) message_pump.run() channel.receive.assert_called_with(0.5) self.assertEqual(channel.receive.call_count, 2) self.assertTrue(command_processor.send.call_count, 1) self.assertEqual(channel.acknowledge.call_count, 1)
def test_requeueing_a_message(self): """Given that I have an RMQ consumer when I requeue a message then it should return to the end of the queue """ test_topic = "kombu_gateway_tests" + str(uuid4()) request = TestMessage() header = BrightsideMessageHeader(uuid4(), test_topic, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) queue_name = "brightside_tests" + str(uuid4()) consumer = ArameConsumer( self._connection, BrightsideConsumerConfiguration(self._pipeline, queue_name, test_topic)) self._producer.send(message) read_message = consumer.receive(3) consumer.requeue(read_message) # should now be able to receive it again reread_message = consumer.receive(3) consumer.acknowledge(reread_message) self.assertEqual(message.id, reread_message.id) self.assertEqual(message.body.value, reread_message.body.value) self.assertTrue(consumer.has_acknowledged(reread_message))
def test_the_pump_should_acknowledge_and_discard_an_unacceptable_message(self): """ Given that I have a message pump for a channel When I cannot read the message received from that channel Then I should acknowledge the message to dicard it """ handler = MyCommandHandler() request = MyCommand() channel = Mock(spec=Channel) command_processor = Mock(spec=CommandProcessor) message_pump = MessagePump(command_processor, channel, map_my_command_to_request) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_UNACCEPTABLE) body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) quit_message = create_quit_message() # add messages to that when channel is called it returns first message then qui tmessage response_queue = [message, quit_message] channel_spec = {"receive.side_effect": response_queue} channel.configure_mock(**channel_spec) message_pump.run() channel.receive.assert_called_with(0.5) self.assertEqual(channel.receive.call_count, 2) # We acknowledge so that a 'poison pill' message cannot block our queue self.assertEqual(channel.acknowledge.call_count, 1) # Does not send the message, just discards it self.assertEqual(command_processor.send.call_count, 0)
def map_myevent_to_message(request: Request) -> BrightsideMessage: message_body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json()) message = BrightsideMessage( BrightsideMessageHeader(request.id, "my_event", BrightsideMessageType.MT_EVENT), message_body) return message
def map_mycommand_to_message(request: Request) -> BrightsideMessage: message_body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json()) message = BrightsideMessage( BrightsideMessageHeader(request.id, "my_command", BrightsideMessageType.MT_COMMAND), message_body) return message
def map_hellworldcommand_to_message( request: HelloWorldCommand) -> BrightsideMessage: message_body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json()) message = BrightsideMessage( BrightsideMessageHeader(request.id, "greeting.event", BrightsideMessageType.MT_EVENT), message_body) return message
def map_longrunningcommand_to_message( request: LongRunningCommand) -> BrightsideMessage: message_body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json()) message = BrightsideMessage( BrightsideMessageHeader(identity=request.id, topic="long_running", message_type=BrightsideMessageType.MT_COMMAND), message_body) return message
def map_hellworldcommand_to_message( request: HelloWorldCommand) -> BrightsideMessage: message_body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json()) message = BrightsideMessage( BrightsideMessageHeader(identity=request.id, topic="hello_world", message_type=BrightsideMessageType.MT_COMMAND), message_body) return message
def test_handle_requeue_has_upper_bound(self): """ Given that I have a channel When I receive a requeue on that channel I should ask the consumer to requeue, up to a retry limit So that poison messages do not fill our queues """ handler = MyCommandHandler() request = MyCommand() channel = FakeChannel(name="MyCommand") command_processor = Mock(spec=CommandProcessor) message_pump = MessagePump(command_processor, channel, map_my_command_to_request, requeue_count=3) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) channel.add(message) requeue_spec = {"send.side_effect": DeferMessageException()} command_processor.configure_mock(**requeue_spec) started_event = Event() t = Thread(target=message_pump.run, args=(started_event,)) t.start() started_event.wait() time.sleep(1) channel.stop() t.join() self.assertTrue(command_processor.send.call_count, 3)
def test_stop_performer(self): """ Given that I have started a performer When I stop the performer Then it should terminate the pump :return: """ request = MyCommand() pipeline = Queue() connection = Connection(config.broker_uri, "examples.perfomer.exchange") configuration = BrightsideConsumerConfiguration( pipeline, "performer.test.queue", "examples.tests.mycommand") performer = Performer("test_channel", connection, configuration, mock_consumer_factory, mock_command_processor_factory, map_my_command_to_request) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) pipeline.put(message) started_event = Event() p = performer.run(started_event) started_event.wait() time.sleep(1) performer.stop() p.join() self.assertTrue(True)
def test_stop_consumer(self): """Given that I have a dispatcher When I stop a consumer Then the performer should terminate """ request = MyCommand() pipeline = Queue() connection = Connection(config.broker_uri, "examples.perfomer.exchange") configuration = BrightsideConsumerConfiguration( pipeline, "dispatcher.test.queue", "examples.tests.mycommand") consumer = ConsumerConfiguration(connection, configuration, mock_consumer_factory, mock_command_processor_factory, map_my_command_to_request) dispatcher = Dispatcher({"MyCommand": consumer}) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message = BrightsideMessage(header, body) pipeline.put(message) self.assertEqual(dispatcher.state, DispatcherState.ds_awaiting) dispatcher.receive() time.sleep(1) dispatcher.end() self.assertEqual(dispatcher.state, DispatcherState.ds_stopped) self.assertTrue(pipeline.empty())
def map_my_command_to_request(message: BrightsideMessage) -> Request: return JsonRequestSerializer( request=LongRunningCommand(), serialized_request=message.body.value).deserialize_from_json()
def map_my_event_to_request(message: BrightsideMessage) -> Request: return JsonRequestSerializer(request=MyEvent(), serialized_request=message.body.value)\ .deserialize_from_json()
def test_restart_consumer(self): """Given that I have a dispatcher with all consumers stopped When I restart a consumer Then the dispatcher should have one running consumer """ connection = Connection(config.broker_uri, "examples.perfomer.exchange") # First consumer request = MyCommand() pipeline_one = Queue() configuration_one = BrightsideConsumerConfiguration( pipeline_one, "restart_command.test.queue", "examples.tests.mycommand") consumer_one = ConsumerConfiguration(connection, configuration_one, mock_consumer_factory, mock_command_processor_factory, map_my_command_to_request) header_one = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_COMMAND) body_one = BrightsideMessageBody( JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message_one = BrightsideMessage(header_one, body_one) pipeline_one.put(message_one) # Second consumer event = MyEvent() pipeline_two = Queue() configuration_two = BrightsideConsumerConfiguration( pipeline_two, "restart_event.test.queue", "examples.tests.myevent") consumer_two = ConsumerConfiguration(connection, configuration_two, mock_consumer_factory, mock_command_processor_factory, map_my_event_to_request) header_two = BrightsideMessageHeader(uuid4(), event.__class__.__name__, BrightsideMessageType.MT_EVENT) body_two = BrightsideMessageBody( JsonRequestSerializer(request=event).serialize_to_json(), BrightsideMessageBodyType.application_json) message_two = BrightsideMessage(header_two, body_two) pipeline_two.put_nowait(message_two) # Dispatcher dispatcher = Dispatcher({ "consumer_one": consumer_one, "consumer_two": consumer_two }) # Consume the messages and stop self.assertEqual(dispatcher.state, DispatcherState.ds_awaiting) dispatcher.receive() time.sleep(1) dispatcher.end() self.assertEqual(dispatcher.state, DispatcherState.ds_stopped) self.assertTrue(pipeline_one.empty()) #Now add a new message, restart a consumer, and eat event_three = MyEvent() header_three = BrightsideMessageHeader(uuid4(), event.__class__.__name__, BrightsideMessageType.MT_EVENT) body_three = BrightsideMessageBody( JsonRequestSerializer(request=event_three).serialize_to_json(), BrightsideMessageBodyType.application_json) message_three = BrightsideMessage(header_three, body_three) pipeline_two.put_nowait(message_three) dispatcher.open("consumer_two") time.sleep(1) dispatcher.end() self.assertEqual(dispatcher.state, DispatcherState.ds_stopped) self.assertTrue(pipeline_two.empty())