@@ -791,47 +791,21 @@ async def mycoro():
791791 loop .run_until_complete (mycoro ())
792792 assert not loop .is_running ()
793793
794- def test_task_recursion_fails (loop , application ):
795- """Re-entering the event loop from a Task will fail if there is another
796- runnable task."""
797- async_called = False
798- main_called = False
799-
800- async def async_job ():
801- nonlocal async_called
802- async_called = True
803-
804- def sync_callback ():
805- asyncio .create_task (async_job ())
806- assert not async_called
807- application .processEvents ()
808- assert not async_called
809- return 1
810-
811- async def main ():
812- nonlocal main_called
813- res = sync_callback ()
814- assert res == 1
815- main_called = True
816-
817- exceptions = []
818- loop .set_exception_handler (lambda loop , context : exceptions .append (context ))
819-
820- loop .run_until_complete (main ())
821- assert main_called , "The main function should have been called"
822-
823- # We will now have an error in there, because the task 'async_job' could not
824- # be entered, because the task 'main' was still being executed by the event loop.
825- assert len (exceptions ) == 1
826- assert isinstance (exceptions [0 ]["exception" ], RuntimeError )
827-
828794
829- def test_call_sync (loop , application ):
830- """Re-entering the event loop from a Task will fail if there is another
831- runnable task."""
795+ @pytest .mark .parametrize (
796+ "async_wrap, expect_async_called, expect_exception" ,
797+ [(False , False , True ), (True , True , False )],
798+ )
799+ def test_async_wrap (
800+ loop , application , async_wrap , expect_async_called , expect_exception
801+ ):
802+ """
803+ Re-entering the event loop from a Task will fail if there is another
804+ runnable task.
805+ """
832806 async_called = False
833807 main_called = False
834-
808+
835809 async def async_job ():
836810 nonlocal async_called
837811 async_called = True
@@ -840,24 +814,31 @@ def sync_callback():
840814 asyncio .create_task (async_job ())
841815 assert not async_called
842816 application .processEvents ()
843- assert async_called
817+ assert async_called if expect_async_called else not async_called
844818 return 1
845819
846820 async def main ():
847821 nonlocal main_called
848- res = await qasync .call_sync (sync_callback )
822+ if async_wrap :
823+ res = await qasync .asyncWrap (sync_callback )
824+ else :
825+ res = sync_callback ()
849826 assert res == 1
850827 main_called = True
851828
852- exceptions = []
829+ exceptions = []
853830 loop .set_exception_handler (lambda loop , context : exceptions .append (context ))
854831
855832 loop .run_until_complete (main ())
856833 assert main_called , "The main function should have been called"
857-
858- # We will now have an error in there, because the task 'async_job' could not
859- # be entered, because the task 'main' was still being executed by the event loop.
860- assert len (exceptions ) == 0
834+
835+ if expect_exception :
836+ # We will now have an error in there, because the task 'async_job' could not
837+ # be entered, because the task 'main' was still being executed by the event loop.
838+ assert len (exceptions ) == 1
839+ assert isinstance (exceptions [0 ]["exception" ], RuntimeError )
840+ else :
841+ assert len (exceptions ) == 0
861842
862843
863844def test_slow_callback_duration_logging (loop , caplog ):
0 commit comments