@@ -265,9 +265,11 @@ async def main(req, context):
265265 app .response_code = 200
266266 req = self ._generate_func_request ()
267267 ctx = self ._generate_func_context ()
268- response = asyncio .get_event_loop ().run_until_complete (
269- AsgiMiddleware (app ).handle_async (req , ctx )
270- )
268+
269+ async def run_test ():
270+ return await AsgiMiddleware (app ).handle_async (req , ctx )
271+
272+ response = asyncio .run (run_test ())
271273
272274 # Verify asserted
273275 self .assertEqual (response .status_code , 200 )
@@ -276,15 +278,15 @@ async def main(req, context):
276278 def test_function_app_lifecycle_events (self ):
277279 mock_app = MockAsgiApplication ()
278280 middleware = AsgiMiddleware (mock_app )
279- asyncio .get_event_loop ().run_until_complete (
280- middleware .notify_startup ()
281- )
282- assert mock_app .startup_called
283281
284- asyncio .get_event_loop ().run_until_complete (
285- middleware .notify_shutdown ()
286- )
287- assert mock_app .shutdown_called
282+ async def run_test ():
283+ await middleware .notify_startup ()
284+ assert mock_app .startup_called
285+
286+ await middleware .notify_shutdown ()
287+ assert mock_app .shutdown_called
288+
289+ asyncio .run (run_test ())
288290
289291 def test_function_app_lifecycle_events_with_failures (self ):
290292 apps = [
@@ -295,22 +297,24 @@ def test_function_app_lifecycle_events_with_failures(self):
295297 MockAsgiApplication (False , "bork" ),
296298 MockAsgiApplication ("bork" , "bork" ),
297299 ]
298- for mock_app in apps :
300+
301+ async def run_test (mock_app ):
299302 middleware = AsgiMiddleware (mock_app )
300- asyncio .get_event_loop ().run_until_complete (
301- middleware .notify_startup ()
302- )
303+ await middleware .notify_startup ()
303304 assert mock_app .startup_called
304305
305- asyncio .get_event_loop ().run_until_complete (
306- middleware .notify_shutdown ()
307- )
306+ await middleware .notify_shutdown ()
308307 assert mock_app .shutdown_called
309308
309+ for mock_app in apps :
310+ asyncio .run (run_test (mock_app ))
311+
310312 def test_calling_shutdown_without_startup_errors (self ):
311313 mock_app = MockAsgiApplication ()
312314 middleware = AsgiMiddleware (mock_app )
315+
316+ async def run_test ():
317+ await middleware .notify_shutdown ()
318+
313319 with pytest .raises (RuntimeError ):
314- asyncio .get_event_loop ().run_until_complete (
315- middleware .notify_shutdown ()
316- )
320+ asyncio .run (run_test ())
0 commit comments