-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_routing.py
More file actions
232 lines (204 loc) · 7.01 KB
/
test_routing.py
File metadata and controls
232 lines (204 loc) · 7.01 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import asyncio
import uuid
from collections.abc import AsyncGenerator
import aio_pika
import pytest
from aio_pika import Channel, Message
from aio_pika.abc import ExchangeType
from taskiq import BrokerMessage
from taskiq.utils import maybe_awaitable
from taskiq_aio_pika import AioPikaBroker, Queue
from taskiq_aio_pika.exchange import Exchange
from tests.conftest import _cleanup_amqp_resources
from tests.utils import get_first_task
class TestRouting:
broker: AioPikaBroker | None = None
@pytest.fixture(autouse=True)
async def cleanup_class_broker(self, amqp_url: str) -> AsyncGenerator[None, None]:
yield
if self.broker is not None:
await self.broker.shutdown()
queue_names = (
[queue.name for queue in self.broker._task_queues]
+ [queue.delay_queue_name for queue in self.broker._task_queues]
+ [self.broker._dead_letter_queue.name]
)
await _cleanup_amqp_resources(
amqp_url,
[self.broker._exchange.name],
queue_names,
)
async def test_when_message_has_wrong_format__then_message_still_can_be_received(
self,
broker: AioPikaBroker,
queue_name: str,
test_channel: Channel,
) -> None:
# given & when
await test_channel.default_exchange.publish(
Message(b"wrong"),
routing_key=queue_name,
)
# then
message = await asyncio.wait_for(get_first_task(broker), 0.4)
assert message.data == b"wrong"
async def test_when_broker_has_only_default_settings__then_task_can_be_passed(
self,
amqp_url: str,
) -> None:
# given
self.broker = AioPikaBroker(
url=amqp_url,
)
self.broker.is_worker_process = True
await self.broker.startup()
task_id = uuid.uuid4().hex
message = BrokerMessage(
task_id=task_id,
task_name="task_name",
message=b"my_msg",
labels={
"label1": "val1",
},
)
# when
await self.broker.kick(message)
# then
received_message = await asyncio.wait_for(
get_first_task(self.broker),
timeout=0.4,
)
assert received_message.data == message.message
await maybe_awaitable(received_message.ack())
async def test_when_broker_has_two_queues_and_default_exchange__then_task_should_be_put_only_to_right_queue(
self,
amqp_url: str,
test_channel: Channel,
) -> None:
# given
self.broker = AioPikaBroker(
url=amqp_url,
task_queues=[
Queue(
name="queue1",
declare=True,
),
Queue(
name="queue2",
declare=True,
),
],
)
self.broker.is_worker_process = True
await self.broker.startup()
queue_1 = await test_channel.get_queue("queue1")
queue_2 = await test_channel.get_queue("queue2")
message_to_queue_1 = BrokerMessage(
task_id=uuid.uuid4().hex,
task_name="task_name",
message=b"my_msg",
labels={
"queue_name": "queue1",
},
)
# when
await self.broker.kick(message_to_queue_1)
# then
received_message = await queue_1.get()
assert received_message is not None
await received_message.nack(requeue=True)
with pytest.raises(aio_pika.exceptions.QueueEmpty):
received_message = await queue_2.get()
assert received_message is None
async def test_when_queue_bind_with_pattern_and_exchange_type_topic__when_message_published_in_right_queue(
self,
amqp_url: str,
test_channel: Channel,
) -> None:
# given
self.broker = AioPikaBroker(
url=amqp_url,
exchange=Exchange(
name="test_topic_exchange",
type=ExchangeType.TOPIC,
declare=True,
),
task_queues=[
Queue(
name="service.task.queue1",
declare=True,
routing_key="service.task.*",
),
Queue(
name="service.task.queue2",
declare=True,
routing_key="service.task.only_specific_task",
),
],
)
self.broker.is_worker_process = True
await self.broker.startup()
queue_1 = await test_channel.get_queue("service.task.queue1")
queue_2 = await test_channel.get_queue("service.task.queue2")
message_with_specific_task = BrokerMessage(
task_id=uuid.uuid4().hex,
task_name="task_name",
message=b"my_msg",
labels={"queue_name": "service.task.only_specific_task"},
)
# when
await self.broker.kick(message_with_specific_task)
# then
received_message_1 = await queue_1.get()
assert (
received_message_1 is not None
), "Message was not routed to queue, but should be by pattern"
await received_message_1.ack()
received_message_2 = await queue_2.get()
assert (
received_message_2 is not None
), "Message was not routed to queue, but should be by specific name"
await received_message_2.ack()
async def test_when_exchange_fanout__when_message_published_in_all_queues(
self,
amqp_url: str,
test_channel: Channel,
) -> None:
# given
self.broker = AioPikaBroker(
url=amqp_url,
exchange=Exchange(
name="test_topic_exchange",
type=ExchangeType.FANOUT,
declare=True,
),
task_queues=[
Queue(
name="service.task.queue1",
declare=True,
),
Queue(
name="service.task.queue2",
declare=True,
),
],
)
self.broker.is_worker_process = True
await self.broker.startup()
queue_1 = await test_channel.get_queue("service.task.queue1")
queue_2 = await test_channel.get_queue("service.task.queue2")
message_for_all_queues = BrokerMessage(
task_id=uuid.uuid4().hex,
task_name="task_name",
message=b"my_msg",
labels={"queue_name": "service.task.only_specific_task"},
)
# when
await self.broker.kick(message_for_all_queues)
# then
received_message_1 = await queue_1.get()
assert received_message_1 is not None
await received_message_1.ack()
received_message_2 = await queue_2.get()
assert received_message_2 is not None
await received_message_2.ack()