|
1 | 1 | import asyncio |
2 | 2 | import uuid |
3 | 3 |
|
4 | | -from natsrpy import Nats |
| 4 | +from natsrpy import Message, Nats |
5 | 5 |
|
6 | 6 |
|
7 | 7 | async def test_request_sends_with_reply(nats: Nats) -> None: |
8 | 8 | subj = uuid.uuid4().hex |
9 | 9 |
|
10 | | - received_payload: list[bytes] = [] |
11 | | - received_reply: list[str | None] = [] |
| 10 | + received_msgs: list[Message] = [] |
12 | 11 |
|
13 | 12 | async def responder() -> None: |
14 | 13 | sub = await nats.subscribe(subject=subj) |
15 | 14 | msg = await anext(sub) |
16 | | - received_payload.append(msg.payload) |
17 | | - received_reply.append(msg.reply) |
| 15 | + received_msgs.append(msg) |
18 | 16 | if msg.reply: |
19 | 17 | await nats.publish(msg.reply, b"reply-data") |
20 | 18 |
|
21 | 19 | task = asyncio.create_task(responder()) |
22 | 20 | await asyncio.sleep(0.1) |
23 | 21 |
|
24 | | - # request() sends a message and waits for a reply (though the response |
25 | | - # is not returned by the current implementation) |
26 | | - await nats.request(subj, b"request-payload") |
| 22 | + response = await nats.request(subj, b"request-payload") |
27 | 23 | await task |
28 | 24 |
|
29 | | - assert received_payload == [b"request-payload"] |
30 | | - # request() should set a reply subject automatically |
31 | | - assert received_reply[0] is not None |
| 25 | + assert response.payload == b"reply-data" |
| 26 | + assert received_msgs |
| 27 | + assert received_msgs[0].payload == b"request-payload" |
| 28 | + assert received_msgs[0].reply is not None |
32 | 29 |
|
33 | 30 |
|
34 | 31 | async def test_request_with_headers(nats: Nats) -> None: |
35 | 32 | subj = uuid.uuid4().hex |
36 | 33 |
|
37 | | - received_headers: list[dict[str, str]] = [] |
| 34 | + received_msgs: list[Message] = [] |
38 | 35 |
|
39 | 36 | async def responder() -> None: |
40 | 37 | sub = await nats.subscribe(subject=subj) |
41 | 38 | msg = await anext(sub) |
42 | | - received_headers.append(msg.headers) |
| 39 | + received_msgs.append(msg) |
43 | 40 | if msg.reply: |
44 | 41 | await nats.publish(msg.reply, b"reply") |
45 | 42 |
|
46 | 43 | task = asyncio.create_task(responder()) |
47 | 44 | await asyncio.sleep(0.1) |
48 | 45 |
|
49 | | - await nats.request(subj, b"data", headers={"x-custom": "value"}) |
| 46 | + resp = await nats.request(subj, b"data", headers={"x-custom": "value"}) |
50 | 47 | await task |
51 | | - |
52 | | - assert received_headers[0] == {"x-custom": "value"} |
| 48 | + assert resp.payload == b"reply" |
| 49 | + assert received_msgs[0].headers == {"x-custom": "value"} |
53 | 50 |
|
54 | 51 |
|
55 | 52 | async def test_request_none_payload(nats: Nats) -> None: |
56 | 53 | subj = uuid.uuid4().hex |
57 | 54 |
|
58 | | - received_payload: list[bytes] = [] |
| 55 | + received_msgs: list[Message] = [] |
59 | 56 |
|
60 | 57 | async def responder() -> None: |
61 | 58 | sub = await nats.subscribe(subject=subj) |
62 | 59 | msg = await anext(sub) |
63 | | - received_payload.append(msg.payload) |
| 60 | + received_msgs.append(msg) |
64 | 61 | if msg.reply: |
65 | 62 | await nats.publish(msg.reply, b"reply") |
66 | 63 |
|
67 | 64 | task = asyncio.create_task(responder()) |
68 | 65 | await asyncio.sleep(0.1) |
69 | 66 |
|
70 | | - await nats.request(subj, b"") |
| 67 | + response = await nats.request(subj, b"") |
71 | 68 | await task |
| 69 | + assert response.payload == b"reply" |
72 | 70 |
|
73 | | - assert received_payload[0] == b"" |
| 71 | + assert received_msgs[0].payload == b"" |
0 commit comments