-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathtest_receiver.py
More file actions
602 lines (470 loc) · 15 KB
/
test_receiver.py
File metadata and controls
602 lines (470 loc) · 15 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import asyncio
import contextvars
import random
import time
import unittest.mock
from collections.abc import Generator
from concurrent.futures import ThreadPoolExecutor
from functools import wraps
from typing import Any, ClassVar
import pytest
from taskiq_dependencies import Depends
from taskiq.abc.broker import AckableMessage, AsyncBroker
from taskiq.abc.middleware import TaskiqMiddleware
from taskiq.brokers.inmemory_broker import InMemoryBroker
from taskiq.exceptions import NoResultError, TaskiqResultTimeoutError
from taskiq.message import TaskiqMessage
from taskiq.receiver import Receiver
from taskiq.result import TaskiqResult
from tests.utils import AsyncQueueBroker
def get_receiver(
broker: AsyncBroker | None = None,
no_parse: bool = False,
max_async_tasks: int | None = None,
max_async_tasks_jitter: int = 0,
) -> Receiver:
"""
Returns receiver with custom broker and args.
:param broker: broker, defaults to None
:param no_parse: parameter to taskiq_args, defaults to False
:param max_async_tasks: maximum number of simultaneous async tasks.
:param max_async_tasks_jitter: random jitter to add to max_async_tasks.
:return: new receiver.
"""
if broker is None:
broker = InMemoryBroker()
return Receiver(
broker,
executor=ThreadPoolExecutor(max_workers=10),
validate_params=not no_parse,
max_async_tasks=max_async_tasks,
max_async_tasks_jitter=max_async_tasks_jitter,
)
async def test_run_task_successful_async() -> None:
"""Tests that run_task can run async tasks."""
async def test_func(param: int) -> int:
return param
receiver = get_receiver()
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={},
args=[1],
kwargs={},
),
)
assert result.return_value == 1
async def test_run_task_successful_sync() -> None:
"""Tests that run_task can run sync tasks."""
def test_func(param: int) -> int:
return param
receiver = get_receiver()
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={},
args=[1],
kwargs={},
),
)
assert result.return_value == 1
async def test_run_task_exception() -> None:
"""Tests that run_task can run sync tasks."""
def test_func() -> None:
raise ValueError
receiver = get_receiver()
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={},
args=[],
kwargs={},
),
)
assert result.return_value is None
assert result.is_err
async def test_run_timeouts() -> None:
async def test_func() -> None:
await asyncio.sleep(2)
receiver = get_receiver()
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={"timeout": "0.3"},
args=[],
kwargs={},
),
)
assert result.return_value is None
assert result.execution_time < 2
assert result.is_err
async def test_run_timeouts_sync() -> None:
def test_func() -> None:
time.sleep(2)
receiver = get_receiver()
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={"timeout": "0.3"},
args=[],
kwargs={},
),
)
assert result.return_value is None
assert result.execution_time < 2
assert result.is_err
async def test_run_task_exception_middlewares() -> None:
"""Tests that run_task can run sync tasks."""
class _TestMiddleware(TaskiqMiddleware):
found_exceptions: ClassVar[list[BaseException]] = []
def on_error(
self,
message: "TaskiqMessage",
result: "TaskiqResult[Any]",
exception: BaseException,
) -> None:
self.found_exceptions.append(exception)
def test_func() -> None:
raise ValueError
broker = InMemoryBroker().with_middlewares(_TestMiddleware())
receiver = get_receiver(broker)
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={},
args=[],
kwargs={},
),
)
assert result.return_value is None
assert result.is_err
assert len(_TestMiddleware.found_exceptions) == 1
assert _TestMiddleware.found_exceptions[0].__class__ is ValueError
async def test_callback_success() -> None:
"""Test that callback function works well."""
broker = InMemoryBroker()
called_times = 0
@broker.task
async def my_task() -> int:
nonlocal called_times
called_times += 1
return 1
receiver = get_receiver(broker)
broker_message = broker.formatter.dumps(
TaskiqMessage(
task_id="task_id",
task_name=my_task.task_name,
labels={},
args=[],
kwargs={},
),
)
await receiver.callback(broker_message.message)
assert called_times == 1
async def test_callback_no_dep_info() -> None:
"""Test that callback function works well."""
broker = InMemoryBroker()
expected = random.randint(1, 100)
ret_val = None
def dependency() -> int:
return expected
@broker.task
async def my_task(dep: int = Depends(dependency)) -> None:
nonlocal ret_val
ret_val = dep
receiver = get_receiver(broker)
receiver.known_tasks.remove(my_task.task_name)
receiver.dependency_graphs.pop(my_task.task_name, None)
receiver.task_signatures.pop(my_task.task_name, None)
receiver.task_hints.pop(my_task.task_name, None)
broker_message = broker.formatter.dumps(
TaskiqMessage(
task_id="task_id",
task_name=my_task.task_name,
labels={},
args=[],
kwargs={},
),
)
await receiver.callback(broker_message.message)
assert ret_val == expected
async def test_callback_success_ackable() -> None:
"""Test that acking works."""
broker = InMemoryBroker()
called_times = 0
acked = False
@broker.task
async def my_task() -> int:
nonlocal called_times
called_times += 1
return 1
def ack_callback() -> None:
nonlocal acked
acked = True
receiver = get_receiver(broker)
broker_message = broker.formatter.dumps(
TaskiqMessage(
task_id="task_id",
task_name=my_task.task_name,
labels={},
args=[],
kwargs={},
),
)
await receiver.callback(
AckableMessage(
data=broker_message.message,
ack=ack_callback,
),
)
assert called_times == 1
assert acked
async def test_callback_success_ackable_async() -> None:
"""Test that acks work with async functions."""
broker = InMemoryBroker()
called_times = 0
acked = False
@broker.task
async def my_task() -> int:
nonlocal called_times
called_times += 1
return 1
async def ack_callback() -> None:
nonlocal acked
acked = True
receiver = get_receiver(broker)
broker_message = broker.formatter.dumps(
TaskiqMessage(
task_id="task_id",
task_name=my_task.task_name,
labels={},
args=[],
kwargs={},
),
)
await receiver.callback(
AckableMessage(
data=broker_message.message,
ack=ack_callback,
),
)
assert called_times == 1
assert acked
async def test_callback_wrong_format() -> None:
"""Test that wrong format of a message won't throw an error."""
receiver = get_receiver()
await receiver.callback(
b"{some wrong bytes}",
)
async def test_callback_unknown_task() -> None:
"""Tests that running an unknown task won't throw an error."""
broker = InMemoryBroker()
receiver = get_receiver(broker)
broker_message = broker.formatter.dumps(
TaskiqMessage(
task_id="task_id",
task_name="unknown",
labels={},
args=[],
kwargs={},
),
)
await receiver.callback(broker_message.message)
async def test_custom_ctx() -> None:
"""Tests that run_task can run sync tasks."""
class MyTestClass:
"""Class to test injection."""
def __init__(self, val: int) -> None:
self.val = val
broker = InMemoryBroker()
# We register a task into broker,
# to build dependency graph on startup.
@broker.task
def test_func(tes_val: MyTestClass = Depends()) -> int:
return tes_val.val
# We add custom first-level dependency.
broker.add_dependency_context({MyTestClass: MyTestClass(11)})
# Create a receiver.
receiver = get_receiver(broker)
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name=test_func.task_name,
labels={},
args=[],
kwargs={},
),
)
# Check that the value is equal
# to the one we supplied.
assert result.return_value == 11
assert not result.is_err
async def test_callback_semaphore() -> None:
"""Test that callback function semaphore works well."""
max_async_tasks = 3
broker = AsyncQueueBroker()
sem_num = 0
@broker.task
async def task_sem() -> int:
nonlocal sem_num
sem_num += 1
await asyncio.sleep(1)
return 1
for _ in range(max_async_tasks + 2):
await task_sem.kiq()
receiver = get_receiver(broker, max_async_tasks=max_async_tasks)
listen_task = asyncio.create_task(receiver.listen(asyncio.Event()))
await asyncio.sleep(0.3)
assert sem_num == max_async_tasks
await broker.wait_tasks()
assert sem_num == max_async_tasks + 2
listen_task.cancel()
async def test_no_result_error() -> None:
broker = InMemoryBroker()
executed = asyncio.Event()
@broker.task
async def task_no_result() -> int:
executed.set()
raise NoResultError
task = await task_no_result.kiq()
with pytest.raises(TaskiqResultTimeoutError):
await task.wait_result(timeout=1)
assert executed.is_set()
assert not broker._running_tasks
async def test_result() -> None:
broker = InMemoryBroker()
@broker.task
async def task_no_result() -> str:
return "some value"
task = await task_no_result.kiq()
resp = await task.wait_result(timeout=1)
assert resp.return_value == "some value"
assert not broker._running_tasks
async def test_error_result() -> None:
broker = InMemoryBroker()
@broker.task
async def task_no_result() -> str:
raise ValueError("some error")
task = await task_no_result.kiq()
resp = await task.wait_result(timeout=1)
assert resp.return_value is None
assert not broker._running_tasks
assert isinstance(resp.error, ValueError)
EXPECTED_CTX_VALUE = 42
@pytest.fixture()
def ctxvar() -> Generator[contextvars.ContextVar[int], None, None]:
_ctx_variable: contextvars.ContextVar[int] = contextvars.ContextVar(
"taskiq_test_ctx_var",
)
token = _ctx_variable.set(EXPECTED_CTX_VALUE)
yield _ctx_variable
_ctx_variable.reset(token)
async def test_run_task_successful_sync_preserve_contextvars(
ctxvar: contextvars.ContextVar[int],
) -> None:
"""Running sync tasks should preserve context vars."""
def test_func() -> int:
return ctxvar.get()
receiver = get_receiver()
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={},
args=[],
kwargs={},
),
)
assert result.return_value == EXPECTED_CTX_VALUE
async def test_run_task_successful_async_preserve_contextvars(
ctxvar: contextvars.ContextVar[int],
) -> None:
"""Running async tasks should preserve context vars."""
async def test_func() -> int:
return ctxvar.get()
receiver = get_receiver()
result = await receiver.run_task(
test_func,
TaskiqMessage(
task_id="",
task_name="",
labels={},
args=[],
kwargs={},
),
)
assert result.return_value == EXPECTED_CTX_VALUE
async def test_sync_decorator_on_async_function() -> None:
broker = InMemoryBroker()
wrapper_call = False
def wrapper(f: Any) -> Any:
@wraps(f)
def wrapper_impl(*args: Any, **kwargs: Any) -> Any:
nonlocal wrapper_call
wrapper_call = True
return f(*args, **kwargs)
return wrapper_impl
@broker.task
@wrapper
async def task_no_result() -> str:
return "some value"
task = await task_no_result.kiq()
resp = await task.wait_result(timeout=1)
assert resp.return_value == "some value"
assert not broker._running_tasks
assert wrapper_call is True
async def test_jitter_applied_to_semaphore() -> None:
"""Test that jitter is correctly applied to max_async_tasks semaphore."""
max_async_tasks = 100
max_async_tasks_jitter = 10
# Test with jitter value of 0 (minimum)
with unittest.mock.patch("random.randint", return_value=0):
receiver = get_receiver(
max_async_tasks=max_async_tasks,
max_async_tasks_jitter=max_async_tasks_jitter,
)
assert receiver.sem is not None
assert receiver.sem._value == max_async_tasks
# Test with jitter value of 5 (middle)
with unittest.mock.patch("random.randint", return_value=5):
receiver = get_receiver(
max_async_tasks=max_async_tasks,
max_async_tasks_jitter=max_async_tasks_jitter,
)
assert receiver.sem is not None
assert receiver.sem._value == max_async_tasks + 5
# Test with jitter value of 10 (maximum)
with unittest.mock.patch("random.randint", return_value=10):
receiver = get_receiver(
max_async_tasks=max_async_tasks,
max_async_tasks_jitter=max_async_tasks_jitter,
)
assert receiver.sem is not None
assert receiver.sem._value == max_async_tasks + 10
async def test_jitter_zero_no_randomization() -> None:
"""Test with zero jitter, semaphore value matches max_async_tasks."""
max_async_tasks = 50
receiver = get_receiver(
max_async_tasks=max_async_tasks,
max_async_tasks_jitter=0,
)
assert receiver.sem is not None
assert receiver.sem._value == max_async_tasks
async def test_no_semaphore_without_max_async_tasks() -> None:
"""Test that semaphore is None when max_async_tasks is not set."""
receiver = get_receiver(max_async_tasks=None)
assert receiver.sem is None