@@ -81,18 +81,16 @@ def handle_notitifcation(_notification):
8181 pass
8282
8383 notifier_task = asyncio .ensure_future (notifier .connect ())
84- await notifier .wait_ready ()
85-
86- assert (
87- notifier ._websocket
88- and notifier ._websocket .state == websockets .State .OPEN
89- )
90-
91- notifier_task .cancel ()
9284 try :
93- await notifier_task
94- except asyncio .CancelledError :
95- pass
85+ await notifier .wait_ready ()
86+
87+ assert (
88+ notifier ._websocket
89+ and notifier ._websocket .state == websockets .State .OPEN
90+ )
91+ finally :
92+ notifier_task .cancel ()
93+ await asyncio .gather (notifier_task , return_exceptions = True )
9694
9795
9896@pytest .fixture
@@ -117,22 +115,23 @@ async def test_room_created_deleted(
117115 ):
118116 event_checks = [ServerMessageRoomCreated , ServerMessageRoomDeleted ]
119117
120- assert_task = asyncio .ensure_future (assert_events (notifier , event_checks .copy ()))
118+ assert_task = asyncio .ensure_future (
119+ assert_events (notifier , event_checks .copy ())
120+ )
121121 notifier_task = asyncio .ensure_future (notifier .connect ())
122- await notifier .wait_ready ()
122+ try :
123+ await notifier .wait_ready ()
123124
124- options = RoomOptions (webhook_url = WEBHOOK_URL )
125- room = room_api .create_room (options = options )
125+ options = RoomOptions (webhook_url = WEBHOOK_URL )
126+ room = room_api .create_room (options = options )
126127
127- room_api .delete_room (room .id )
128+ room_api .delete_room (room .id )
128129
129- await assert_task
130-
131- notifier_task .cancel ()
132- try :
133- await notifier_task
134- except asyncio .CancelledError :
135- pass
130+ await assert_task
131+ finally :
132+ assert_task .cancel ()
133+ notifier_task .cancel ()
134+ await asyncio .gather (assert_task , notifier_task , return_exceptions = True )
136135
137136 self .assert_webhook_events (event_checks , event_queue , room .id )
138137
@@ -149,27 +148,32 @@ async def test_peer_connected_disconnected(
149148 ServerMessageRoomDeleted ,
150149 ]
151150
152- assert_task = asyncio .ensure_future (assert_events (notifier , event_checks .copy ()))
151+ assert_task = asyncio .ensure_future (
152+ assert_events (notifier , event_checks .copy ())
153+ )
153154 notifier_task = asyncio .ensure_future (notifier .connect ())
154- await notifier .wait_ready ()
155-
156- options = RoomOptions (webhook_url = WEBHOOK_URL )
157- room = room_api .create_room (options = options )
155+ tasks = [assert_task , notifier_task ]
156+ try :
157+ await notifier .wait_ready ()
158158
159- peer , token = room_api .create_peer (room .id )
160- peer_socket = PeerSocket (fishjam_url = FISHJAM_ID )
161- peer_socket_task = asyncio .ensure_future (peer_socket .connect (token ))
159+ options = RoomOptions (webhook_url = WEBHOOK_URL )
160+ room = room_api .create_room (options = options )
162161
163- await peer_socket .wait_ready ()
162+ peer , token = room_api .create_peer (room .id )
163+ peer_socket = PeerSocket (fishjam_url = FISHJAM_ID )
164+ peer_socket_task = asyncio .ensure_future (peer_socket .connect (token ))
165+ tasks .append (peer_socket_task )
164166
165- room_api .delete_peer (room .id , peer .id )
166- room_api .delete_room (room .id )
167+ await peer_socket .wait_ready ()
167168
168- await assert_task
169+ room_api .delete_peer (room .id , peer .id )
170+ room_api .delete_room (room .id )
169171
170- notifier_task .cancel ()
171- peer_socket_task .cancel ()
172- await asyncio .gather (notifier_task , peer_socket_task , return_exceptions = True )
172+ await assert_task
173+ finally :
174+ for task in tasks :
175+ task .cancel ()
176+ await asyncio .gather (* tasks , return_exceptions = True )
173177
174178 self .assert_webhook_events (event_checks , event_queue , room .id )
175179
@@ -185,26 +189,31 @@ async def test_peer_connected_room_deleted(
185189 ServerMessageRoomDeleted ,
186190 ]
187191
188- assert_task = asyncio .ensure_future (assert_events (notifier , event_checks .copy ()))
192+ assert_task = asyncio .ensure_future (
193+ assert_events (notifier , event_checks .copy ())
194+ )
189195 notifier_task = asyncio .ensure_future (notifier .connect ())
190- await notifier .wait_ready ()
191-
192- options = RoomOptions (webhook_url = WEBHOOK_URL )
193- room = room_api .create_room (options = options )
194- _peer , token = room_api .create_peer (room .id )
196+ tasks = [assert_task , notifier_task ]
197+ try :
198+ await notifier .wait_ready ()
195199
196- peer_socket = PeerSocket (fishjam_url = FISHJAM_ID )
197- peer_socket_task = asyncio .ensure_future (peer_socket .connect (token ))
200+ options = RoomOptions (webhook_url = WEBHOOK_URL )
201+ room = room_api .create_room (options = options )
202+ _peer , token = room_api .create_peer (room .id )
198203
199- await peer_socket .wait_ready ()
204+ peer_socket = PeerSocket (fishjam_url = FISHJAM_ID )
205+ peer_socket_task = asyncio .ensure_future (peer_socket .connect (token ))
206+ tasks .append (peer_socket_task )
200207
201- room_api . delete_room ( room . id )
208+ await peer_socket . wait_ready ( )
202209
203- await assert_task
210+ room_api . delete_room ( room . id )
204211
205- notifier_task .cancel ()
206- peer_socket_task .cancel ()
207- await asyncio .gather (notifier_task , peer_socket_task , return_exceptions = True )
212+ await assert_task
213+ finally :
214+ for task in tasks :
215+ task .cancel ()
216+ await asyncio .gather (* tasks , return_exceptions = True )
208217
209218 self .assert_webhook_events (event_checks , event_queue , room .id )
210219
0 commit comments