-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathtest_hooks.py
More file actions
96 lines (70 loc) · 2.52 KB
/
test_hooks.py
File metadata and controls
96 lines (70 loc) · 2.52 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
import asyncio
import pytest
from taskiq.abc.middleware import TaskiqMiddleware
from taskiq.brokers.inmemory_broker import InMemoryBroker
from taskiq.exceptions import SendTaskError
from taskiq.message import BrokerMessage, TaskiqMessage
@pytest.mark.anyio
async def test_on_send_error() -> None:
caught = []
class _TestMiddleware(TaskiqMiddleware):
def on_send_error(
self,
message: "TaskiqMessage",
broker_message: "BrokerMessage",
exception: BaseException,
) -> bool:
caught.append(1)
return True
broker = InMemoryBroker().with_middlewares(_TestMiddleware())
broker.kick = lambda *args, **kwargs: (_ for _ in ()).throw(Exception("test")) # type: ignore
await broker.startup()
await broker.task(lambda: None).kiq()
await broker.shutdown()
assert caught == [1]
@pytest.mark.anyio
async def test_on_send_error_raise() -> None:
caught = []
class _TestMiddleware(TaskiqMiddleware):
def on_send_error(
self,
message: "TaskiqMessage",
broker_message: "BrokerMessage",
exception: BaseException,
) -> None:
caught.append(0)
broker = InMemoryBroker().with_middlewares(_TestMiddleware())
broker.kick = lambda *args, **kwargs: (_ for _ in ()).throw(Exception("test")) # type: ignore
await broker.startup()
with pytest.raises(SendTaskError):
await broker.task(lambda: None).kiq()
await broker.shutdown()
assert caught == [0]
@pytest.mark.anyio
async def test_on_send_error_inverted() -> None:
caught = []
class _TestMiddleware1(TaskiqMiddleware):
def on_send_error(
self,
message: "TaskiqMessage",
broker_message: "BrokerMessage",
exception: BaseException,
) -> bool:
caught.append(1)
return True
class _TestMiddleware2(TaskiqMiddleware):
async def on_send_error(
self,
message: "TaskiqMessage",
broker_message: "BrokerMessage",
exception: BaseException,
) -> bool:
await asyncio.sleep(0)
caught.append(2)
return True
broker = InMemoryBroker().with_middlewares(_TestMiddleware1(), _TestMiddleware2())
broker.kick = lambda *args, **kwargs: (_ for _ in ()).throw(Exception("test")) # type: ignore
await broker.startup()
await broker.task(lambda: None).kiq()
await broker.shutdown()
assert caught == [2, 1]