Skip to content

Commit d6925ce

Browse files
Test fixes
1 parent 826ed39 commit d6925ce

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

tests/support/asyncio_utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,3 @@ def handle_message(message):
2929
raise asyncio.exceptions.TimeoutError(
3030
f"{message_checks[0]} hasn't been received within timeout"
3131
) from exc
32-
33-
34-
async def cancel(task):
35-
task.cancel()
36-
try:
37-
await task
38-
except asyncio.exceptions.CancelledError:
39-
pass

tests/support/env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
FISHJAM_ID = os.environ["FISHJAM_ID"]
44
FISHJAM_MANAGEMENT_TOKEN = os.environ["FISHJAM_MANAGEMENT_TOKEN"]
5-
WEBHOOK_SERVER_URL = os.getenv("WEBHOOK_SERVER_URL", "http://localhost:5000/")
6-
WEBHOOK_URL = f"{WEBHOOK_SERVER_URL}webhook"
5+
WEBHOOK_SERVER_URL = os.getenv("WEBHOOK_SERVER_URL", "http://localhost:5000")
6+
WEBHOOK_URL = f"{WEBHOOK_SERVER_URL}/webhook"

tests/test_notifier.py

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
ServerMessageRoomCreated,
1919
ServerMessageRoomDeleted,
2020
)
21-
from tests.support.asyncio_utils import assert_events, cancel
21+
from tests.support.asyncio_utils import assert_events
2222
from tests.support.env import (
2323
FISHJAM_ID,
2424
FISHJAM_MANAGEMENT_TOKEN,
@@ -65,13 +65,14 @@ async def test_valid_credentials(self):
6565
def handle_notitifcation(_notification):
6666
pass
6767

68-
notifier_task = asyncio.create_task(notifier.connect())
69-
await notifier.wait_ready()
70-
assert (
71-
notifier._websocket and notifier._websocket.state == websockets.State.OPEN
72-
)
68+
async with asyncio.TaskGroup() as tg:
69+
tg.create_task(notifier.connect())
70+
await notifier.wait_ready()
7371

74-
await cancel(notifier_task)
72+
assert (
73+
notifier._websocket
74+
and notifier._websocket.state == websockets.State.OPEN
75+
)
7576

7677

7778
@pytest.fixture
@@ -95,18 +96,19 @@ async def test_room_created_deleted(
9596
self, room_api: FishjamClient, notifier: FishjamNotifier
9697
):
9798
event_checks = [ServerMessageRoomCreated, ServerMessageRoomDeleted]
98-
assert_task = asyncio.create_task(assert_events(notifier, event_checks.copy()))
9999

100-
notifier_task = asyncio.create_task(notifier.connect())
101-
await notifier.wait_ready()
100+
async with asyncio.TaskGroup() as tg:
101+
assert_task = tg.create_task(assert_events(notifier, event_checks.copy()))
102+
103+
tg.create_task(notifier.connect())
104+
await notifier.wait_ready()
102105

103-
options = RoomOptions(webhook_url=WEBHOOK_URL)
104-
room = room_api.create_room(options=options)
106+
options = RoomOptions(webhook_url=WEBHOOK_URL)
107+
room = room_api.create_room(options=options)
105108

106-
room_api.delete_room(room.id)
109+
room_api.delete_room(room.id)
107110

108-
await assert_task
109-
await cancel(notifier_task)
111+
await assert_task
110112

111113
for event in event_checks:
112114
self.assert_event(event)
@@ -123,26 +125,26 @@ async def test_peer_connected_disconnected(
123125
ServerMessagePeerDeleted,
124126
ServerMessageRoomDeleted,
125127
]
126-
assert_task = asyncio.create_task(assert_events(notifier, event_checks.copy()))
127128

128-
notifier_task = asyncio.create_task(notifier.connect())
129-
await notifier.wait_ready()
129+
async with asyncio.TaskGroup() as tg:
130+
assert_task = tg.create_task(assert_events(notifier, event_checks.copy()))
130131

131-
options = RoomOptions(webhook_url=WEBHOOK_URL)
132-
room = room_api.create_room(options=options)
132+
tg.create_task(notifier.connect())
133+
await notifier.wait_ready()
133134

134-
peer, token = room_api.create_peer(room.id)
135-
peer_socket = PeerSocket(fishjam_url=FISHJAM_ID)
136-
peer_task = asyncio.create_task(peer_socket.connect(token))
135+
options = RoomOptions(webhook_url=WEBHOOK_URL)
136+
room = room_api.create_room(options=options)
137137

138-
await peer_socket.wait_ready()
138+
peer, token = room_api.create_peer(room.id)
139+
peer_socket = PeerSocket(fishjam_url=FISHJAM_ID)
140+
tg.create_task(peer_socket.connect(token))
139141

140-
room_api.delete_peer(room.id, peer.id)
141-
room_api.delete_room(room.id)
142+
await peer_socket.wait_ready()
142143

143-
await assert_task
144-
await cancel(peer_task)
145-
await cancel(notifier_task)
144+
room_api.delete_peer(room.id, peer.id)
145+
room_api.delete_room(room.id)
146+
147+
await assert_task
146148

147149
for event in event_checks:
148150
self.assert_event(event)
@@ -158,25 +160,25 @@ async def test_peer_connected_room_deleted(
158160
ServerMessagePeerDeleted,
159161
ServerMessageRoomDeleted,
160162
]
161-
assert_task = asyncio.create_task(assert_events(notifier, event_checks.copy()))
162163

163-
notifier_task = asyncio.create_task(notifier.connect())
164-
await notifier.wait_ready()
164+
async with asyncio.TaskGroup() as tg:
165+
assert_task = tg.create_task(assert_events(notifier, event_checks.copy()))
166+
167+
tg.create_task(notifier.connect())
168+
await notifier.wait_ready()
165169

166-
options = RoomOptions(webhook_url=WEBHOOK_URL)
167-
room = room_api.create_room(options=options)
168-
_peer, token = room_api.create_peer(room.id)
170+
options = RoomOptions(webhook_url=WEBHOOK_URL)
171+
room = room_api.create_room(options=options)
172+
_peer, token = room_api.create_peer(room.id)
169173

170-
peer_socket = PeerSocket(fishjam_url=FISHJAM_ID)
171-
peer_task = asyncio.create_task(peer_socket.connect(token))
174+
peer_socket = PeerSocket(fishjam_url=FISHJAM_ID)
175+
tg.create_task(peer_socket.connect(token))
172176

173-
await peer_socket.wait_ready()
177+
await peer_socket.wait_ready()
174178

175-
room_api.delete_room(room.id)
179+
room_api.delete_room(room.id)
176180

177-
await assert_task
178-
await cancel(peer_task)
179-
await cancel(notifier_task)
181+
await assert_task
180182

181183
for event in event_checks:
182184
self.assert_event(event)

0 commit comments

Comments
 (0)