-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_message.py
More file actions
103 lines (80 loc) · 3.06 KB
/
test_message.py
File metadata and controls
103 lines (80 loc) · 3.06 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import uuid
from natsrpy import Nats
async def test_message_subject(nats: Nats) -> None:
subj = uuid.uuid4().hex
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"subject-test")
msg = await anext(sub)
assert msg.subject == subj
async def test_message_payload(nats: Nats) -> None:
subj = uuid.uuid4().hex
payload = b"payload-test-data"
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, payload)
msg = await anext(sub)
assert msg.payload == payload
assert isinstance(msg.payload, bytes)
async def test_message_headers_empty(nats: Nats) -> None:
subj = uuid.uuid4().hex
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"no-headers")
msg = await anext(sub)
assert msg.headers == {}
async def test_message_headers_string(nats: Nats) -> None:
subj = uuid.uuid4().hex
headers = {"content-type": "application/json", "x-id": "12345"}
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"with-headers", headers=headers)
msg = await anext(sub)
assert msg.headers == headers
async def test_message_headers_multi_value(nats: Nats) -> None:
subj = uuid.uuid4().hex
headers = {"x-values": ["a", "b", "c"]}
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"multi-headers", headers=headers)
msg = await anext(sub)
assert msg.headers == headers
async def test_message_reply_present(nats: Nats) -> None:
subj = uuid.uuid4().hex
reply_to = uuid.uuid4().hex
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"reply-test", reply=reply_to)
msg = await anext(sub)
assert msg.reply == reply_to
async def test_message_reply_absent(nats: Nats) -> None:
subj = uuid.uuid4().hex
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"no-reply")
msg = await anext(sub)
assert msg.reply is None
async def test_message_length(nats: Nats) -> None:
subj = uuid.uuid4().hex
payload = b"length-check"
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, payload)
msg = await anext(sub)
# length is the total message length (includes subject + overhead), not just payload
assert msg.length >= len(payload)
async def test_message_length_empty(nats: Nats) -> None:
subj = uuid.uuid4().hex
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"")
msg = await anext(sub)
# Even with empty payload, length includes overhead
assert msg.length >= 0
async def test_message_repr(nats: Nats) -> None:
subj = uuid.uuid4().hex
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, b"repr-test")
msg = await anext(sub)
r = repr(msg)
assert isinstance(r, str)
assert len(r) > 0
async def test_message_large_payload(nats: Nats) -> None:
subj = uuid.uuid4().hex
payload = b"x" * 65536
sub = await nats.subscribe(subject=subj)
await nats.publish(subj, payload)
msg = await anext(sub)
assert msg.payload == payload
assert msg.length >= 65536