@@ -808,6 +808,72 @@ async def mycoro():
808808 assert "seconds" in msg
809809
810810
811+ @pytest .mark .skipif (sys .version_info < (3 , 12 ), reason = "Requires Python 3.12+" )
812+ def test_asyncio_run (application ):
813+ """Test that QEventLoop is compatible with asyncio.run()"""
814+ done = False
815+ loop = None
816+
817+ async def main ():
818+ nonlocal done , loop
819+ assert loop .is_running ()
820+ assert asyncio .get_running_loop () is loop
821+ await asyncio .sleep (0.01 )
822+ done = True
823+
824+ def factory ():
825+ nonlocal loop
826+ loop = qasync .QEventLoop (application )
827+ return loop
828+
829+ asyncio .run (main (), loop_factory = factory )
830+ assert done
831+ assert loop .is_closed ()
832+ assert not loop .is_running ()
833+
834+
835+ @pytest .mark .skipif (sys .version_info < (3 , 12 ), reason = "Requires Python 3.12+" )
836+ def test_asyncio_run_cleanup (application ):
837+ """Test that running tasks are cleaned up"""
838+ task = None
839+ cancelled = False
840+
841+ async def main ():
842+ nonlocal task , cancelled
843+
844+ async def long_task ():
845+ nonlocal cancelled
846+ try :
847+ await asyncio .sleep (10 )
848+ except asyncio .CancelledError :
849+ cancelled = True
850+
851+ task = asyncio .create_task (long_task ())
852+ await asyncio .sleep (0.01 )
853+
854+ asyncio .run (main (), loop_factory = lambda : qasync .QEventLoop (application ))
855+ assert cancelled
856+
857+
858+ def test_qasync_run (application ):
859+ """Test running with qasync.run()"""
860+ done = False
861+ loop = None
862+
863+ async def main ():
864+ nonlocal done , loop
865+ loop = asyncio .get_running_loop ()
866+ assert loop .is_running ()
867+ await asyncio .sleep (0.01 )
868+ done = True
869+
870+ # qasync.run uses an EventLoopPolicy to create the loop
871+ qasync .run (main ())
872+ assert done
873+ assert loop .is_closed ()
874+ assert not loop .is_running ()
875+
876+
811877def teardown_module (module ):
812878 """
813879 Remove handlers from all loggers
0 commit comments