-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional_event_system.py
More file actions
213 lines (118 loc) · 2.74 KB
/
Copy pathfunctional_event_system.py
File metadata and controls
213 lines (118 loc) · 2.74 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
from __future__ import annotations
from bitgenesis.events.event_bus import (
EventBus,
)
from bitgenesis.events.event import (
Event,
)
from bitgenesis.events.enums import (
EventCategory,
EventType,
)
print("=== Event System Functional Test ===")
# --------------------------------------------------
# Setup
# --------------------------------------------------
bus = EventBus()
received = {
"count": 0,
"event": None,
}
def event_listener(
event,
):
received["count"] += 1
received["event"] = event
# --------------------------------------------------
# Subscribe
# --------------------------------------------------
bus.subscribe(
EventType.RUNTIME_STARTED,
event_listener,
)
assert bus.subscriber_count(
EventType.RUNTIME_STARTED
) == 1
print(
"Subscriber registered"
)
# --------------------------------------------------
# Emit Event
# --------------------------------------------------
event = Event(
category=EventCategory.RUNTIME,
type=EventType.RUNTIME_STARTED,
source="functional_test",
payload={
"status": "running",
"test": True,
},
)
bus.emit(
event
)
print(
"Event emitted"
)
# --------------------------------------------------
# Validate delivery
# --------------------------------------------------
assert received["count"] == 1
assert received["event"] is not None
assert received["event"].type == EventType.RUNTIME_STARTED
assert received["event"].source == "functional_test"
assert received["event"].payload["status"] == "running"
assert received["event"].payload["test"] is True
print(
"Event received correctly"
)
# --------------------------------------------------
# Category subscription
# --------------------------------------------------
category_received = {
"value": False,
}
def category_listener(
event,
):
category_received["value"] = True
bus.subscribe(
EventCategory.RUNTIME,
category_listener,
)
bus.emit(
Event(
category=EventCategory.RUNTIME,
type=EventType.RUNTIME_STOPPED,
source="functional_test",
payload={},
)
)
assert category_received["value"] is True
print(
"Category routing OK"
)
# --------------------------------------------------
# Unsubscribe
# --------------------------------------------------
bus.unsubscribe(
EventType.RUNTIME_STARTED,
event_listener,
)
assert bus.subscriber_count(
EventType.RUNTIME_STARTED
) == 0
print(
"Unsubscribe OK"
)
# --------------------------------------------------
# Clear
# --------------------------------------------------
bus.clear()
assert bus.subscriber_count() == 0
print(
"Clear OK"
)
print(
"=== Event System Test OK ==="
)