def run(): pipeline = Queue() connection = Connection("amqp://*****:*****@localhost:5672//", "paramore.brightside.exchange", is_durable=True) configuration = BrightsideConsumerConfiguration( pipeline, "examples_long_running_queue", "long_running", is_long_running_handler=True) consumer = ConsumerConfiguration(connection, configuration, consumer_factory, command_processor_factory, map_my_command_to_request) dispatcher = Dispatcher({"HelloWorldCommand": consumer}) dispatcher.receive() # poll for keyboard input to allow the user to quit monitoring while True: try: # just sleep unless we receive an interrupt i.e. CTRL+C time.sleep(KEYBOARD_INTERRUPT_SLEEP) except KeyboardInterrupt: dispatcher.end() sys.exit(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_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_a_message(self): """Given that I have an RMQ message producer when I send that message via the producer then I should be able to read that message via the consumer """ test_topic = "kombu_gateway_tests" + str(uuid4()) header = BrightsideMessageHeader(uuid4(), test_topic, BrightsideMessageType.MT_COMMAND) body = BrightsideMessageBody("test content") 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) self.assertEqual(message.id, read_message.id) self.assertEqual(message.body.value, read_message.body.value) self.assertTrue(consumer.has_acknowledged(read_message))
def run(): pipeline = Queue() amqp_uri = os.getenv('BROKER') connection = Connection(amqp_uri, "future.stack.exchange", is_durable=False) configuration = BrightsideConsumerConfiguration(pipeline, "taskcreated.event", "taskcreated.event") consumer = ConsumerConfiguration(connection, configuration, consumer_factory, command_processor_factory, map_my_command_to_request) dispatcher = Dispatcher({"ToDoCreatedEvent": consumer}) dispatcher.receive() # poll for keyboard input to allow the user to quit monitoring while True: try: # just sleep unless we receive an interrupt i.e. CTRL+C time.sleep(KEYBOARD_INTERRUPT_SLEEP) except KeyboardInterrupt: dispatcher.end() sys.exit(1)
def setUp(self): config = TestConfig() self._connection = Connection(config.broker_uri, "paramore.brightside.exchange", is_durable=True) self._producer = ArameProducer(self._connection) self._pipeline = Queue() self._consumer = ArameConsumer( self._connection, BrightsideConsumerConfiguration(self._pipeline, "brightside_tests", self.test_topic))
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 run(): pipeline = Queue() connection = Connection(amqp_uri, "paramore.brighter.exchange", is_durable=False) configuration = BrightsideConsumerConfiguration(pipeline, "greetings_queue", "greeting.event") consumer = ConsumerConfiguration(connection, configuration, consumer_factory, command_processor_factory, map_my_command_to_request) dispatcher = Dispatcher({"HelloWorldCommand": consumer}) dispatcher.receive() # poll for keyboard input to allow the user to quit monitoring while True: try: # just sleep unless we receive an interrupt i.e. CTRL+C time.sleep(KEYBOARD_INTERRUPT_SLEEP) except KeyboardInterrupt: dispatcher.end() sys.exit(1)
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 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())