-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_event_advanced.py
55 lines (38 loc) · 1.57 KB
/
test_event_advanced.py
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
from dataclasses import dataclass
import pytest
from mediator.common.factory import CallableHandlerPolicy, MethodHandlerPolicy
from mediator.event import EventHandlerRegistry, LocalEventBus
# Create local event registry with given policies.
# Single policy tells the handler inspector where to find callable
# which handles particular action.
# - add policy that suggest to get object attribute named "handle"
# and try to analyze as event handler
# - add policy that suggest given object is event handler callable
registry = EventHandlerRegistry(
policies=[MethodHandlerPolicy(name="handle"), CallableHandlerPolicy()],
)
@dataclass
class MessageEvent:
message: str
class MessageEventHandler:
async def handle(self, event: MessageEvent):
print(f"from object handler: {event.message}")
# ... MethodHandlerPolicy(...) gives ability to inspect handler object
handler = MessageEventHandler()
registry.register(handler)
# ... CallableHandlerPolicy(...) gives ability to inspect plain function
@registry.register
async def message_event_handler(some_event: MessageEvent):
print(f"from function handler: {some_event.message}")
@pytest.mark.asyncio
async def test_event_advanced():
# create local event bus and include handlers from registry
bus = LocalEventBus()
# registry usage is optional - used for example purposes,
# LocalEventBus implements same registry interface
bus.include(registry)
# publish event
await bus.publish(MessageEvent(message="test"))
# output:
# from object handler: test
# from function handler: test