|
1 | 1 | from rabbitmq_amqp_python_client import ( |
2 | 2 | Connection, |
| 3 | + Converter, |
3 | 4 | DirectReplyToConsumerOptions, |
| 5 | + Environment, |
| 6 | + Message, |
| 7 | + OutcomeState, |
4 | 8 | ) |
5 | 9 |
|
6 | 10 |
|
7 | 11 | def test_consumer_create_reply_name(connection: Connection) -> None: |
8 | 12 | consumer = connection.consumer("", consumer_options=DirectReplyToConsumerOptions()) |
9 | 13 | assert "/queues/amq.rabbitmq.reply-to." in consumer.get_queue_address() |
| 14 | + consumer.close() |
| 15 | + |
| 16 | + |
| 17 | +def test_direct_reply_to_send_and_receive( |
| 18 | + environment: Environment, connection: Connection |
| 19 | +) -> None: |
| 20 | + """Test that messages can be published to and consumed from a direct reply-to queue.""" |
| 21 | + messages_to_send = 10 |
| 22 | + |
| 23 | + # Create a consumer using DirectReplyToConsumerOptions |
| 24 | + consumer = connection.consumer("", consumer_options=DirectReplyToConsumerOptions()) |
| 25 | + |
| 26 | + # Get the queue address from the consumer |
| 27 | + addr = consumer.get_queue_address() |
| 28 | + assert addr is not None |
| 29 | + assert "/queues/amq.rabbitmq.reply-to." in addr |
| 30 | + |
| 31 | + # Create a new connection and publisher to publish to the reply-to address |
| 32 | + publisher_connection = environment.connection() |
| 33 | + publisher_connection.dial() |
| 34 | + publisher = publisher_connection.publisher(addr) |
| 35 | + |
| 36 | + # Publish messages to the direct reply-to queue |
| 37 | + for i in range(messages_to_send): |
| 38 | + msg = Message(body=Converter.string_to_bytes("test message {}".format(i))) |
| 39 | + status = publisher.publish(msg) |
| 40 | + assert status.remote_state == OutcomeState.ACCEPTED |
| 41 | + |
| 42 | + # Consume messages synchronously |
| 43 | + consumed = 0 |
| 44 | + for i in range(messages_to_send): |
| 45 | + message = consumer.consume() |
| 46 | + if Converter.bytes_to_string(message.body) == "test message {}".format(i): |
| 47 | + consumed = consumed + 1 |
| 48 | + |
| 49 | + # Clean up |
| 50 | + publisher.close() |
| 51 | + publisher_connection.close() |
| 52 | + consumer.close() |
| 53 | + |
| 54 | + # Verify all messages were received |
| 55 | + assert consumed == messages_to_send |
0 commit comments