def test_the_pump_should_limit_unacceptable_messages(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) unacceptable_message_limit = 3 message_pump = MessagePump(command_processor, channel, map_my_command_to_request, unacceptable_message_limit=unacceptable_message_limit) header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.MT_UNACCEPTABLE) body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json) message_one = BrightsideMessage(header, body) message_two = BrightsideMessage(header, body) message_three = BrightsideMessage(header, body) message_four = 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_one, message_two, message_three, message_four, quit_message] channel_spec = {"receive.side_effect": response_queue} channel.configure_mock(**channel_spec) message_pump.run() # Should acknowledge first three so that a 'poison pill' message cannot block our queue self.assertEqual(channel.acknowledge.call_count, unacceptable_message_limit) # We should dispose of the channel, by sending ourselves a quit messages self.assertEqual(channel.end.call_count, 1) # Does not send the message, just discards it self.assertEqual(command_processor.send.call_count, 0)
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_hellworldcommand_to_message(request: HelloWorldCommand) -> BrightsideMessage: message_body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json()) message = BrightsideMessage(BrightsideMessageHeader(request.id, "hello_world", BrightsideMessageType.MT_COMMAND), message_body) return message
def map_my_event_to_request(message: BrightsideMessage) -> Request: return JsonRequestSerializer(request=MyEvent(), serialized_request=message.body.value)\ .deserialize_from_json()
def map_my_command_to_request(message: BrightsideMessage) -> Request: return JsonRequestSerializer( request=LongRunningCommand(), 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())