|
1 | 1 | # pylint: disable=locally-disabled, missing-class-docstring, missing-function-docstring, redefined-outer-name, too-few-public-methods, missing-module-docstring |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +import queue |
| 5 | +import time |
4 | 6 | import uuid |
5 | 7 | from multiprocessing import Manager, Process |
6 | 8 |
|
@@ -127,8 +129,7 @@ async def test_room_created_deleted( |
127 | 129 |
|
128 | 130 | notifier_task.cancel() |
129 | 131 |
|
130 | | - for event in event_checks: |
131 | | - self.assert_event(event, event_queue, room.id) |
| 132 | + self.assert_webhook_events(event_checks, event_queue, room.id) |
132 | 133 |
|
133 | 134 | @pytest.mark.asyncio |
134 | 135 | async def test_peer_connected_disconnected( |
@@ -166,8 +167,7 @@ async def test_peer_connected_disconnected( |
166 | 167 | notifier_task.cancel() |
167 | 168 | peer_socket_task.cancel() |
168 | 169 |
|
169 | | - for event in event_checks: |
170 | | - self.assert_event(event, event_queue, room.id) |
| 170 | + self.assert_webhook_events(event_checks, event_queue, room.id) |
171 | 171 |
|
172 | 172 | @pytest.mark.asyncio |
173 | 173 | async def test_peer_connected_room_deleted( |
@@ -203,14 +203,31 @@ async def test_peer_connected_room_deleted( |
203 | 203 | notifier_task.cancel() |
204 | 204 | peer_socket_task.cancel() |
205 | 205 |
|
206 | | - for event in event_checks: |
207 | | - self.assert_event(event, event_queue, room.id) |
| 206 | + self.assert_webhook_events(event_checks, event_queue, room.id) |
208 | 207 |
|
209 | | - def assert_event(self, event, event_queue, room_id=None): |
210 | | - for _ in range(20): |
211 | | - data = event_queue.get(timeout=10) |
| 208 | + def assert_webhook_events(self, event_checks, event_queue, room_id, timeout=30): |
| 209 | + deadline = time.monotonic() + timeout |
| 210 | + received = [] |
| 211 | + |
| 212 | + pos = 0 |
| 213 | + while pos < len(event_checks) and time.monotonic() < deadline: |
| 214 | + remaining = deadline - time.monotonic() |
| 215 | + |
| 216 | + try: |
| 217 | + data = event_queue.get(timeout=remaining) |
| 218 | + except queue.Empty: |
| 219 | + continue |
212 | 220 | if room_id and data.room_id != room_id: |
213 | 221 | continue |
214 | | - if data == event or isinstance(data, event): |
215 | | - return |
216 | | - raise AssertionError(f"Expected {event} but last received: {data}") |
| 222 | + |
| 223 | + received.append(data) |
| 224 | + if data == event_checks[pos] or isinstance(data, event_checks[pos]): |
| 225 | + pos += 1 |
| 226 | + |
| 227 | + if pos >= len(event_checks): |
| 228 | + return |
| 229 | + |
| 230 | + raise AssertionError( |
| 231 | + f"Expected event {event_checks[pos]} not found. " |
| 232 | + f"Received: {[type(e).__name__ for e in received]}" |
| 233 | + ) |
0 commit comments