-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_request_reply.py
More file actions
71 lines (50 loc) · 1.88 KB
/
test_request_reply.py
File metadata and controls
71 lines (50 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import asyncio
import uuid
from natsrpy import Message, Nats
async def test_request_sends_with_reply(nats: Nats) -> None:
subj = uuid.uuid4().hex
received_msgs: list[Message] = []
async def responder() -> None:
sub = await nats.subscribe(subject=subj)
msg = await anext(sub)
received_msgs.append(msg)
if msg.reply:
await nats.publish(msg.reply, b"reply-data")
task = asyncio.create_task(responder())
await asyncio.sleep(0.1)
response = await nats.request(subj, b"request-payload")
await task
assert response.payload == b"reply-data"
assert received_msgs
assert received_msgs[0].payload == b"request-payload"
assert received_msgs[0].reply is not None
async def test_request_with_headers(nats: Nats) -> None:
subj = uuid.uuid4().hex
received_msgs: list[Message] = []
async def responder() -> None:
sub = await nats.subscribe(subject=subj)
msg = await anext(sub)
received_msgs.append(msg)
if msg.reply:
await nats.publish(msg.reply, b"reply")
task = asyncio.create_task(responder())
await asyncio.sleep(0.1)
resp = await nats.request(subj, b"data", headers={"x-custom": "value"})
await task
assert resp.payload == b"reply"
assert received_msgs[0].headers == {"x-custom": "value"}
async def test_request_none_payload(nats: Nats) -> None:
subj = uuid.uuid4().hex
received_msgs: list[Message] = []
async def responder() -> None:
sub = await nats.subscribe(subject=subj)
msg = await anext(sub)
received_msgs.append(msg)
if msg.reply:
await nats.publish(msg.reply, b"reply")
task = asyncio.create_task(responder())
await asyncio.sleep(0.1)
response = await nats.request(subj, b"")
await task
assert response.payload == b"reply"
assert received_msgs[0].payload == b""