Skip to content

Commit 316a4d3

Browse files
committed
Fix tests
1 parent b084cbd commit 316a4d3

4 files changed

Lines changed: 82 additions & 26 deletions

File tree

tests/conftest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring, redefined-outer-name
2+
3+
import pytest
4+
5+
from fishjam import FishjamClient, Room, RoomOptions
6+
from fishjam.errors import HTTPError
7+
from tests.support.env import FISHJAM_ID, FISHJAM_MANAGEMENT_TOKEN
8+
9+
10+
class _TrackingFishjamClient(FishjamClient):
11+
def __init__(self, *args, **kwargs):
12+
super().__init__(*args, **kwargs)
13+
self._tracked_room_ids: list[str] = []
14+
15+
def create_room(self, options: RoomOptions | None = None) -> Room:
16+
room = super().create_room(options)
17+
self._tracked_room_ids.append(room.id)
18+
return room
19+
20+
def cleanup_tracked_rooms(self) -> None:
21+
for room_id in self._tracked_room_ids:
22+
try:
23+
self.delete_room(room_id)
24+
except HTTPError:
25+
pass
26+
self._tracked_room_ids.clear()
27+
28+
29+
@pytest.fixture
30+
def room_api():
31+
client = _TrackingFishjamClient(FISHJAM_ID, FISHJAM_MANAGEMENT_TOKEN)
32+
yield client
33+
client.cleanup_tracked_rooms()

tests/support/asyncio_utils.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,54 @@
77
ASSERTION_TIMEOUT = 15.0
88

99

10-
async def assert_events(notifier: FishjamNotifier, event_checks: list):
11-
await _assert_messages(notifier.on_server_notification, event_checks)
12-
13-
14-
async def _assert_messages(notifier_callback, message_checks):
10+
async def assert_events(
11+
notifier: FishjamNotifier,
12+
event_checks: list,
13+
*,
14+
room_id_future: asyncio.Future | None = None,
15+
):
16+
await _assert_messages(
17+
notifier.on_server_notification, event_checks, room_id_future
18+
)
19+
20+
21+
async def _assert_messages(notifier_callback, message_checks, room_id_future):
1522
success_event = asyncio.Event()
23+
pending: list = []
24+
room_id_holder: dict = {"value": None, "set": False}
1625

17-
@notifier_callback
18-
def handle_message(message):
26+
def _consume(message):
1927
if len(message_checks) > 0:
2028
expected_msg = message_checks[0]
2129
if message == expected_msg or isinstance(message, expected_msg):
2230
message_checks.pop(0)
23-
2431
if message_checks == []:
2532
success_event.set()
2633

34+
@notifier_callback
35+
def handle_message(message):
36+
if not room_id_holder["set"]:
37+
pending.append(message)
38+
return
39+
40+
expected_room_id = room_id_holder["value"]
41+
if expected_room_id is not None:
42+
if getattr(message, "room_id", None) != expected_room_id:
43+
return
44+
45+
_consume(message)
46+
47+
async def _wait_for_success():
48+
if room_id_future is not None:
49+
room_id_holder["value"] = await room_id_future
50+
room_id_holder["set"] = True
51+
for msg in pending:
52+
handle_message(msg)
53+
pending.clear()
54+
await success_event.wait()
55+
2756
try:
28-
await asyncio.wait_for(success_event.wait(), ASSERTION_TIMEOUT)
57+
await asyncio.wait_for(_wait_for_success(), ASSERTION_TIMEOUT)
2958
except asyncio.exceptions.TimeoutError as exc:
3059
raise asyncio.exceptions.TimeoutError(
3160
f"{message_checks[0]} hasn't been received within timeout"

tests/test_notifier.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ def handle_notitifcation(_notification):
9393
await asyncio.gather(notifier_task, return_exceptions=True)
9494

9595

96-
@pytest.fixture
97-
def room_api():
98-
return FishjamClient(FISHJAM_ID, FISHJAM_MANAGEMENT_TOKEN)
99-
100-
10196
@pytest.fixture
10297
def notifier():
10398
notifier = FishjamNotifier(
@@ -115,15 +110,17 @@ async def test_room_created_deleted(
115110
):
116111
event_checks = [ServerMessageRoomCreated, ServerMessageRoomDeleted]
117112

113+
room_id_future: asyncio.Future = asyncio.get_event_loop().create_future()
118114
assert_task = asyncio.ensure_future(
119-
assert_events(notifier, event_checks.copy())
115+
assert_events(notifier, event_checks.copy(), room_id_future=room_id_future)
120116
)
121117
notifier_task = asyncio.ensure_future(notifier.connect())
122118
try:
123119
await notifier.wait_ready()
124120

125121
options = RoomOptions(webhook_url=WEBHOOK_URL)
126122
room = room_api.create_room(options=options)
123+
room_id_future.set_result(room.id)
127124

128125
room_api.delete_room(room.id)
129126

@@ -148,8 +145,9 @@ async def test_peer_connected_disconnected(
148145
ServerMessageRoomDeleted,
149146
]
150147

148+
room_id_future: asyncio.Future = asyncio.get_event_loop().create_future()
151149
assert_task = asyncio.ensure_future(
152-
assert_events(notifier, event_checks.copy())
150+
assert_events(notifier, event_checks.copy(), room_id_future=room_id_future)
153151
)
154152
notifier_task = asyncio.ensure_future(notifier.connect())
155153
tasks = [assert_task, notifier_task]
@@ -158,6 +156,7 @@ async def test_peer_connected_disconnected(
158156

159157
options = RoomOptions(webhook_url=WEBHOOK_URL)
160158
room = room_api.create_room(options=options)
159+
room_id_future.set_result(room.id)
161160

162161
peer, token = room_api.create_peer(room.id)
163162
peer_socket = PeerSocket(fishjam_url=FISHJAM_ID)
@@ -189,8 +188,9 @@ async def test_peer_connected_room_deleted(
189188
ServerMessageRoomDeleted,
190189
]
191190

191+
room_id_future: asyncio.Future = asyncio.get_event_loop().create_future()
192192
assert_task = asyncio.ensure_future(
193-
assert_events(notifier, event_checks.copy())
193+
assert_events(notifier, event_checks.copy(), room_id_future=room_id_future)
194194
)
195195
notifier_task = asyncio.ensure_future(notifier.connect())
196196
tasks = [assert_task, notifier_task]
@@ -199,6 +199,7 @@ async def test_peer_connected_room_deleted(
199199

200200
options = RoomOptions(webhook_url=WEBHOOK_URL)
201201
room = room_api.create_room(options=options)
202+
room_id_future.set_result(room.id)
202203
_peer, token = room_api.create_peer(room.id)
203204

204205
peer_socket = PeerSocket(fishjam_url=FISHJAM_ID)
@@ -217,7 +218,7 @@ async def test_peer_connected_room_deleted(
217218

218219
self.assert_webhook_events(event_checks, event_queue, room.id)
219220

220-
def assert_webhook_events(self, event_checks, event_queue, room_id, timeout=60):
221+
def assert_webhook_events(self, event_checks, event_queue, room_id, timeout=15):
221222
deadline = time.monotonic() + timeout
222223
received = []
223224

tests/test_room_api.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def test_invalid_token(self):
4545
with pytest.raises(UnauthorizedError):
4646
room_api.create_room()
4747

48-
def test_valid_token(self):
49-
room_api = FishjamClient(FISHJAM_ID, FISHJAM_MANAGEMENT_TOKEN)
50-
48+
def test_valid_token(self, room_api: FishjamClient):
5149
room = room_api.create_room()
5250
all_rooms = room_api.get_all_rooms()
5351

@@ -84,11 +82,6 @@ def mock_send(request, **kwargs):
8482
assert captured_headers["x-fishjam-api-client"] == expected_header_value
8583

8684

87-
@pytest.fixture
88-
def room_api():
89-
return FishjamClient(FISHJAM_ID, FISHJAM_MANAGEMENT_TOKEN)
90-
91-
9285
class TestCreateRoom:
9386
def test_no_params(self, room_api: FishjamClient):
9487
room = room_api.create_room()

0 commit comments

Comments
 (0)