Skip to content

Commit 2d67a32

Browse files
author
Alan Christie
committed
feat: Add simple message publisher and client test utilities
1 parent 034ae54 commit 2d67a32

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

simple_message_publisher.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
"""A simple RabbitMQ message publisher for local testing.
3+
Takes a 'routing key' and sends a message to the 'expected' AS exchange
4+
on a localhost RabbitMQ server.
5+
6+
Usage: simple_message_publisher.py <routing_key>
7+
"""
8+
import asyncio
9+
import sys
10+
11+
import aio_pika
12+
13+
_ROUTING_KEY: str = sys.argv[1]
14+
15+
_AMPQ_EXCHANGE: str = "event-streams"
16+
17+
18+
async def main() -> None:
19+
"""Publish a message."""
20+
connection = await aio_pika.connect_robust(
21+
"amqp://es:cheddar1963@localhost:5672",
22+
)
23+
24+
async with connection:
25+
channel = await connection.channel()
26+
27+
es_exchange = await channel.declare_exchange(
28+
_AMPQ_EXCHANGE,
29+
aio_pika.ExchangeType.DIRECT,
30+
)
31+
await es_exchange.publish(
32+
aio_pika.Message(body=f"Hello {_ROUTING_KEY}".encode()),
33+
routing_key=_ROUTING_KEY,
34+
)
35+
36+
37+
if __name__ == "__main__":
38+
asyncio.run(main())

simple_ws_client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
"""A simple WebSocket client for local testing.
3+
4+
Usage: simple_ws_client.py <location>
5+
"""
6+
import asyncio
7+
import sys
8+
9+
from simple_websocket import AioClient, ConnectionClosed
10+
11+
_LOCATION: str = sys.argv[1]
12+
13+
14+
async def main():
15+
"""Connect to the WebSocket and just read messages."""
16+
ws = await AioClient.connect(_LOCATION)
17+
try:
18+
while True:
19+
data = await ws.receive()
20+
print(f"< {data}")
21+
except (KeyboardInterrupt, EOFError, ConnectionClosed):
22+
await ws.close()
23+
24+
25+
if __name__ == "__main__":
26+
asyncio.run(main())

0 commit comments

Comments
 (0)