@@ -381,55 +381,6 @@ async def test_span_origin(
381381 assert event ["spans" ][0 ]["origin" ] == "auto.function.asyncio"
382382
383383
384- @minimum_python_38
385- def test_loop_close_patching (sentry_init ):
386- sentry_init (integrations = [AsyncioIntegration ()])
387-
388- loop = asyncio .new_event_loop ()
389- asyncio .set_event_loop (loop )
390-
391- try :
392- with patch ("asyncio.get_running_loop" , return_value = loop ):
393- assert not hasattr (loop , "_sentry_flush_patched" )
394- AsyncioIntegration .setup_once ()
395- assert hasattr (loop , "_sentry_flush_patched" )
396- assert loop ._sentry_flush_patched is True
397-
398- finally :
399- if not loop .is_closed ():
400- loop .close ()
401-
402-
403- @minimum_python_38
404- def test_loop_close_flushes_async_transport (sentry_init ):
405- from sentry_sdk .transport import AsyncHttpTransport
406- from unittest .mock import Mock , AsyncMock
407-
408- sentry_init (integrations = [AsyncioIntegration ()])
409-
410- loop = asyncio .new_event_loop ()
411- asyncio .set_event_loop (loop )
412-
413- try :
414- with patch ("asyncio.get_running_loop" , return_value = loop ):
415- AsyncioIntegration .setup_once ()
416-
417- mock_client = Mock ()
418- mock_transport = Mock (spec = AsyncHttpTransport )
419- mock_client .transport = mock_transport
420- mock_client .close = AsyncMock (return_value = None )
421-
422- with patch ("sentry_sdk.get_client" , return_value = mock_client ):
423- loop .close ()
424-
425- mock_client .close .assert_called_once ()
426- mock_client .close .assert_awaited_once ()
427-
428- except Exception :
429- if not loop .is_closed ():
430- loop .close ()
431-
432-
433384@minimum_python_38
434385@pytest .mark .asyncio (loop_scope = "module" )
435386async def test_internal_tasks_not_wrapped (sentry_init , capture_events ):
@@ -477,3 +428,60 @@ async def internal_task():
477428 assert (
478429 len (internal_spans ) == 0
479430 ), f"Internal task should NOT have been traced. All spans: { [s .get ('description' ) for s in transaction .get ('spans' , [])]} "
431+
432+
433+ @minimum_python_38
434+ def test_loop_close_patching (sentry_init ):
435+ sentry_init (integrations = [AsyncioIntegration ()])
436+
437+ loop = asyncio .new_event_loop ()
438+ asyncio .set_event_loop (loop )
439+
440+ try :
441+ with patch ("asyncio.get_running_loop" , return_value = loop ):
442+ assert not hasattr (loop , "_sentry_flush_patched" )
443+ AsyncioIntegration .setup_once ()
444+ assert hasattr (loop , "_sentry_flush_patched" )
445+ assert loop ._sentry_flush_patched is True
446+
447+ finally :
448+ if not loop .is_closed ():
449+ loop .close ()
450+
451+
452+ @minimum_python_38
453+ def test_loop_close_flushes_async_transport (sentry_init ):
454+ from sentry_sdk .transport import AsyncHttpTransport
455+ from unittest .mock import Mock , AsyncMock
456+
457+ sentry_init (integrations = [AsyncioIntegration ()])
458+
459+ # Save the current event loop to restore it later
460+ try :
461+ original_loop = asyncio .get_event_loop ()
462+ except RuntimeError :
463+ original_loop = None
464+
465+ loop = asyncio .new_event_loop ()
466+ asyncio .set_event_loop (loop )
467+
468+ try :
469+ with patch ("asyncio.get_running_loop" , return_value = loop ):
470+ AsyncioIntegration .setup_once ()
471+
472+ mock_client = Mock ()
473+ mock_transport = Mock (spec = AsyncHttpTransport )
474+ mock_client .transport = mock_transport
475+ mock_client .close = AsyncMock (return_value = None )
476+
477+ with patch ("sentry_sdk.get_client" , return_value = mock_client ):
478+ loop .close ()
479+
480+ mock_client .close .assert_called_once ()
481+ mock_client .close .assert_awaited_once ()
482+
483+ finally :
484+ if not loop .is_closed ():
485+ loop .close ()
486+ if original_loop :
487+ asyncio .set_event_loop (original_loop )
0 commit comments