@@ -109,42 +109,55 @@ async def main():
109109                    tg.create_task(c1(task), name="sub_main_1") 
110110                    tg.create_task(c1(task), name="sub_main_2") 
111111
112-             asyncio.run(main()) 
112+             def new_eager_loop(): 
113+                 loop = asyncio.new_event_loop() 
114+                 eager_task_factory = asyncio.create_eager_task_factory(asyncio.Task) 
115+                 loop.set_task_factory(eager_task_factory) 
116+                 return loop 
117+ 
118+             asyncio.run(main(), loop_factory={TASK_FACTORY}) 
113119            """ )
114120        stack_trace  =  None 
115-         with  os_helper .temp_dir () as  work_dir :
116-             script_dir  =  os .path .join (work_dir , "script_pkg" )
117-             os .mkdir (script_dir )
118-             fifo  =  f"{ work_dir }  
119-             os .mkfifo (fifo )
120-             script_name  =  _make_test_script (script_dir , 'script' , script )
121-             try :
122-                 p  =  subprocess .Popen ([sys .executable , script_name ,  str (fifo )])
123-                 with  open (fifo , "r" ) as  fifo_file :
124-                     response  =  fifo_file .read ()
125-                 self .assertEqual (response , "ready" )
126-                 stack_trace  =  get_async_stack_trace (p .pid )
127-             except  PermissionError :
128-                 self .skipTest ("Insufficient permissions to read the stack trace" )
129-             finally :
130-                 os .remove (fifo )
131-                 p .kill ()
132-                 p .terminate ()
133-                 p .wait (timeout = SHORT_TIMEOUT )
134- 
135-             # sets are unordered, so we want to sort "awaited_by"s 
136-             stack_trace [2 ].sort (key = lambda  x : x [1 ])
137- 
138-             expected_stack_trace  =  [
139-                 ["c5" , "c4" , "c3" , "c2" ],
140-                 "c2_root" ,
141-                 [
142-                     [["main" ], "Task-1" , []],
143-                     [["c1" ], "sub_main_1" , [[["main" ], "Task-1" , []]]],
144-                     [["c1" ], "sub_main_2" , [[["main" ], "Task-1" , []]]],
145-                 ],
146-             ]
147-             self .assertEqual (stack_trace , expected_stack_trace )
121+         for  task_factory_variant  in  "asyncio.new_event_loop" , "new_eager_loop" :
122+             with  (
123+                 self .subTest (task_factory_variant = task_factory_variant ),
124+                 os_helper .temp_dir () as  work_dir ,
125+             ):
126+                 script_dir  =  os .path .join (work_dir , "script_pkg" )
127+                 os .mkdir (script_dir )
128+                 fifo  =  f"{ work_dir }  
129+                 os .mkfifo (fifo )
130+                 script_name  =  _make_test_script (script_dir , 'script' , script .format (TASK_FACTORY = task_factory_variant ))
131+                 try :
132+                     p  =  subprocess .Popen ([sys .executable , script_name ,  str (fifo )])
133+                     with  open (fifo , "r" ) as  fifo_file :
134+                         response  =  fifo_file .read ()
135+                     self .assertEqual (response , "ready" )
136+                     stack_trace  =  get_async_stack_trace (p .pid )
137+                 except  PermissionError :
138+                     self .skipTest ("Insufficient permissions to read the stack trace" )
139+                 finally :
140+                     os .remove (fifo )
141+                     p .kill ()
142+                     p .terminate ()
143+                     p .wait (timeout = SHORT_TIMEOUT )
144+ 
145+                 # sets are unordered, so we want to sort "awaited_by"s 
146+                 stack_trace [2 ].sort (key = lambda  x : x [1 ])
147+ 
148+                 root_task  =  "Task-1" 
149+                 if  task_factory_variant  ==  "new_eager_loop" :
150+                     root_task  =  "None" 
151+                 expected_stack_trace  =  [
152+                     ["c5" , "c4" , "c3" , "c2" ],
153+                     "c2_root" ,
154+                     [
155+                         [["main" ], root_task , []],
156+                         [["c1" ], "sub_main_1" , [[["main" ], root_task , []]]],
157+                         [["c1" ], "sub_main_2" , [[["main" ], root_task , []]]],
158+                     ],
159+                 ]
160+                 self .assertEqual (stack_trace , expected_stack_trace )
148161
149162    @unittest .skipIf (sys .platform  !=  "darwin"  and  sys .platform  !=  "linux" , "Test only runs on Linux and MacOS" ) 
150163    @unittest .skipIf (sys .platform  ==  "linux"  and  not  PROCESS_VM_READV_SUPPORTED , "Test only runs on Linux with process_vm_readv support" ) 
0 commit comments