Skip to content

Commit daf0b4c

Browse files
Timeout on all events, not on each
1 parent e412c78 commit daf0b4c

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

tests/test_notifier.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# pylint: disable=locally-disabled, missing-class-docstring, missing-function-docstring, redefined-outer-name, too-few-public-methods, missing-module-docstring
22

33
import asyncio
4+
import queue
5+
import time
46
import uuid
57
from multiprocessing import Manager, Process
68

@@ -127,8 +129,7 @@ async def test_room_created_deleted(
127129

128130
notifier_task.cancel()
129131

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)
132133

133134
@pytest.mark.asyncio
134135
async def test_peer_connected_disconnected(
@@ -166,8 +167,7 @@ async def test_peer_connected_disconnected(
166167
notifier_task.cancel()
167168
peer_socket_task.cancel()
168169

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)
171171

172172
@pytest.mark.asyncio
173173
async def test_peer_connected_room_deleted(
@@ -203,14 +203,31 @@ async def test_peer_connected_room_deleted(
203203
notifier_task.cancel()
204204
peer_socket_task.cancel()
205205

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)
208207

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
212220
if room_id and data.room_id != room_id:
213221
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

Comments
 (0)