Repository navigation
Expand file tree
/
Copy pathtest_nats_client.py
More file actions
169 lines (116 loc) · 4.49 KB
/
Copy pathtest_nats_client.py
File metadata and controls
169 lines (116 loc) · 4.49 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import uuid
import pytest
from natsrpy import Nats
async def test_nats_default_constructor() -> None:
nats = Nats()
await nats.startup()
await nats.shutdown()
async def test_nats_custom_addrs() -> None:
nats = Nats(addrs=["localhost:4222"])
await nats.startup()
await nats.shutdown()
async def test_nats_flush(nats: Nats) -> None:
subj = uuid.uuid4().hex
payload = b"flush-test"
async with nats.subscribe(subject=subj) as sub:
await nats.publish(subj, payload)
await nats.flush()
message = await anext(sub)
assert message.payload == payload
async def test_nats_drain(nats_url: str) -> None:
client = Nats(addrs=[nats_url])
await client.startup()
await client.drain()
async def test_nats_startup_shutdown_cycle(nats_url: str) -> None:
client = Nats(addrs=[nats_url])
await client.startup()
subj = uuid.uuid4().hex
payload = b"cycle-test"
async with client.subscribe(subject=subj) as sub:
await client.publish(subj, payload)
message = await anext(sub)
assert message.payload == payload
await client.shutdown()
async def test_nats_multiple_connections(nats_url: str) -> None:
client1 = Nats(addrs=[nats_url])
client2 = Nats(addrs=[nats_url])
await client1.startup()
await client2.startup()
subj = uuid.uuid4().hex
payload = b"cross-client"
async with client2.subscribe(subject=subj) as sub:
await client1.publish(subj, payload)
message = await anext(sub)
assert message.payload == payload
await client1.shutdown()
await client2.shutdown()
async def test_nats_publish_str_payload(nats: Nats) -> None:
subj = uuid.uuid4().hex
payload_str = "hello-string"
async with nats.subscribe(subject=subj) as sub:
await nats.publish(subj, payload_str)
message = await anext(sub)
assert message.payload == payload_str.encode()
async def test_nats_publish_empty_payload(nats: Nats) -> None:
subj = uuid.uuid4().hex
async with nats.subscribe(subject=subj) as sub:
await nats.publish(subj, b"")
message = await anext(sub)
assert message.payload == b""
async def test_nats_publish_with_reply(nats: Nats) -> None:
subj = uuid.uuid4().hex
reply_subj = uuid.uuid4().hex
async with nats.subscribe(subject=subj) as sub:
await nats.publish(subj, b"with-reply", reply=reply_subj)
message = await anext(sub)
assert message.payload == b"with-reply"
assert message.reply == reply_subj
async def test_nats_connection_failure() -> None:
nats = Nats(addrs=["localhost:19999"])
with pytest.raises(Exception):
await nats.startup()
async def test_nats_addr_property(nats_url: str) -> None:
nats = Nats(addrs=[nats_url])
assert nats.addr == [nats_url]
async def test_nats_addr_default() -> None:
nats = Nats()
assert nats.addr == ["nats://localhost:4222"]
async def test_nats_token_property() -> None:
nats = Nats(token="secret-token") # noqa: S106
assert nats.token == "secret-token" # noqa: S105
async def test_nats_token_default() -> None:
nats = Nats()
assert nats.token is None
async def test_nats_nkey_property() -> None:
nats = Nats()
assert nats.nkey is None
async def test_nats_user_and_pass_property() -> None:
nats = Nats(user_and_pass=("user", "pass"))
assert nats.user_and_pass == ("user", "pass")
async def test_nats_user_and_pass_default() -> None:
nats = Nats()
assert nats.user_and_pass is None
async def test_nats_custom_inbox_prefix_property() -> None:
nats = Nats(custom_inbox_prefix="_custom")
assert nats.custom_inbox_prefix == "_custom"
async def test_nats_custom_inbox_prefix_default() -> None:
nats = Nats()
assert nats.custom_inbox_prefix is None
async def test_nats_read_buffer_capacity_property() -> None:
nats = Nats(read_buffer_capacity=1024)
assert nats.read_buffer_capacity == 1024
async def test_nats_read_buffer_capacity_default() -> None:
nats = Nats()
assert nats.read_buffer_capacity == 65535
async def test_nats_sender_capacity_property() -> None:
nats = Nats(sender_capacity=64)
assert nats.sender_capacity == 64
async def test_nats_sender_capacity_default() -> None:
nats = Nats()
assert nats.sender_capacity == 128
async def test_nats_max_reconnects_property() -> None:
nats = Nats(max_reconnects=5)
assert nats.max_reconnects == 5
async def test_nats_max_reconnects_default() -> None:
nats = Nats()
assert nats.max_reconnects is None