def message_receive(args): def callback(message): print(message.body) return True receiver = Receiver(callback, args.address, args.broker_url, args.count) receiver.receive()
class TestFailoverReceiver(TestCase): def setUp(self): configure(CONFIG) self.received_messages = [] queue_address = uuid4().hex create_queue(queue_address, durable=False, auto_delete=True, priorities=5, extra_properties={'qpid.auto_delete_timeout': 10}) self.sender = Sender(queue_address) def handle_received_message(message: Message): self.received_messages.append(message) return True self.receiver = Receiver(handle_received_message, queue_address) def test_failover_receive(self): expected_messages = (create_message(b'FOOBAR'), create_message(b'FOOBAR'), create_message(b'FOOBAR')) self.sender.queue(expected_messages) self.sender.send() self.receiver.limit = 3 self.receiver.receive(timeout=timedelta(seconds=2)) for received, expected in zip(self.received_messages, expected_messages): self.assertEqual(received.id, expected.id) self.assertEqual(received.body, expected.body)
def main(): try_create_queue(QUEUE_NAME) send_messages() receiver = Receiver(consumer, QUEUE_NAME, SERVER_URL, container_class=AsyncioContainer) receiver.run() loop = asyncio.get_event_loop() loop.run_forever()
def setUp(self): configure(CONFIG) self.received_messages = [] queue_address = uuid4().hex create_queue(queue_address, durable=False, auto_delete=True, priorities=5, extra_properties={'qpid.auto_delete_timeout': 10}) self.sender = Sender(queue_address) def handle_received_message(message: Message): self.received_messages.append(message) return True self.receiver = Receiver(handle_received_message, queue_address)
class RemoveService(Thread): def __init__(self): super().__init__() self.receiver = Receiver(self.remote_service_worker, QUEUE_NAME, SERVER_URL) def run(self): self.receiver.run() @staticmethod def remote_service_worker(message: Message) -> bool: print( f'Remote service: received RPC message "{message.body}", sending ' f'result back') reply_message = create_reply(message, f'Test RPC response"') REPLY_SENDER.queue((reply_message,)) REPLY_SENDER.send() return True
class MessagingTestBase(TestCase): def setUp(self): configure(CONFIG) self.received_messages = [] self.expected_messages = [] queue_address = uuid4().hex create_queue(queue_address, durable=False, auto_delete=True, priorities=10, extra_properties={'qpid.auto_delete_timeout': 10}) self.sender = Sender(queue_address) def handle_received_message(message: Message): self.received_messages.append(message) return True self.receiver = Receiver(handle_received_message, queue_address) def send_messages(self, messages: Iterable[Message]): self.expected_messages.extend(messages) self.sender.queue(messages) self.sender.send() def receive_messages(self): with suppress(TimeoutReached): self.receiver.receive(timeout=timedelta(seconds=2)) def check_messages(self, check_identity: bool = True, expected_messages: Optional[Iterable[Message]] = None): expected_messages = expected_messages or self.expected_messages self.assertEqual(len(self.received_messages), len(expected_messages)) for received, expected in zip(self.received_messages, expected_messages): self.assertEqual(received.body, expected.body) if check_identity: # Check identity 100% self.assertEqual(received.id, expected.id) self.assertEqual(received.properties, expected.properties)
def __init__(self): super().__init__() self.receiver = Receiver(self.remote_service_worker, QUEUE_NAME, SERVER_URL)