@@ -84,12 +84,6 @@ def __await__(self):
8484 return self
8585
8686
87- # The following value can be used as a very small timeout:
88- # it passes check "timeout > 0", but has almost
89- # no effect on the test performance
90- _EPSILON = 0.0001
91-
92-
9387class BaseTaskTests :
9488
9589 Task = None
@@ -107,7 +101,6 @@ def setUp(self):
107101 self .loop .set_task_factory (self .new_task )
108102 self .loop .create_future = lambda : self .new_future (self .loop )
109103
110-
111104 def test_generic_alias (self ):
112105 task = self .__class__ .Task [str ]
113106 self .assertEqual (task .__args__ , (str ,))
@@ -971,251 +964,6 @@ async def coro():
971964 task ._log_traceback = True
972965 self .loop .run_until_complete (task )
973966
974- def test_wait_for_timeout_less_then_0_or_0_future_done (self ):
975- def gen ():
976- when = yield
977- self .assertAlmostEqual (0 , when )
978-
979- loop = self .new_test_loop (gen )
980-
981- fut = self .new_future (loop )
982- fut .set_result ('done' )
983-
984- ret = loop .run_until_complete (asyncio .wait_for (fut , 0 ))
985-
986- self .assertEqual (ret , 'done' )
987- self .assertTrue (fut .done ())
988- self .assertAlmostEqual (0 , loop .time ())
989-
990- def test_wait_for_timeout_less_then_0_or_0_coroutine_do_not_started (self ):
991- def gen ():
992- when = yield
993- self .assertAlmostEqual (0 , when )
994-
995- loop = self .new_test_loop (gen )
996-
997- foo_started = False
998-
999- async def foo ():
1000- nonlocal foo_started
1001- foo_started = True
1002-
1003- with self .assertRaises (asyncio .TimeoutError ):
1004- loop .run_until_complete (asyncio .wait_for (foo (), 0 ))
1005-
1006- self .assertAlmostEqual (0 , loop .time ())
1007- self .assertEqual (foo_started , False )
1008-
1009- def test_wait_for_timeout_less_then_0_or_0 (self ):
1010- def gen ():
1011- when = yield
1012- self .assertAlmostEqual (0.2 , when )
1013- when = yield 0
1014- self .assertAlmostEqual (0 , when )
1015-
1016- for timeout in [0 , - 1 ]:
1017- with self .subTest (timeout = timeout ):
1018- loop = self .new_test_loop (gen )
1019-
1020- foo_running = None
1021-
1022- async def foo ():
1023- nonlocal foo_running
1024- foo_running = True
1025- try :
1026- await asyncio .sleep (0.2 )
1027- finally :
1028- foo_running = False
1029- return 'done'
1030-
1031- fut = self .new_task (loop , foo ())
1032-
1033- with self .assertRaises (asyncio .TimeoutError ):
1034- loop .run_until_complete (asyncio .wait_for (fut , timeout ))
1035- self .assertTrue (fut .done ())
1036- # it should have been cancelled due to the timeout
1037- self .assertTrue (fut .cancelled ())
1038- self .assertAlmostEqual (0 , loop .time ())
1039- self .assertEqual (foo_running , False )
1040-
1041- def test_wait_for (self ):
1042-
1043- def gen ():
1044- when = yield
1045- self .assertAlmostEqual (0.2 , when )
1046- when = yield 0
1047- self .assertAlmostEqual (0.1 , when )
1048- when = yield 0.1
1049-
1050- loop = self .new_test_loop (gen )
1051-
1052- foo_running = None
1053-
1054- async def foo ():
1055- nonlocal foo_running
1056- foo_running = True
1057- try :
1058- await asyncio .sleep (0.2 )
1059- finally :
1060- foo_running = False
1061- return 'done'
1062-
1063- fut = self .new_task (loop , foo ())
1064-
1065- with self .assertRaises (asyncio .TimeoutError ):
1066- loop .run_until_complete (asyncio .wait_for (fut , 0.1 ))
1067- self .assertTrue (fut .done ())
1068- # it should have been cancelled due to the timeout
1069- self .assertTrue (fut .cancelled ())
1070- self .assertAlmostEqual (0.1 , loop .time ())
1071- self .assertEqual (foo_running , False )
1072-
1073- def test_wait_for_blocking (self ):
1074- loop = self .new_test_loop ()
1075-
1076- async def coro ():
1077- return 'done'
1078-
1079- res = loop .run_until_complete (asyncio .wait_for (coro (), timeout = None ))
1080- self .assertEqual (res , 'done' )
1081-
1082- def test_wait_for_race_condition (self ):
1083-
1084- def gen ():
1085- yield 0.1
1086- yield 0.1
1087- yield 0.1
1088-
1089- loop = self .new_test_loop (gen )
1090-
1091- fut = self .new_future (loop )
1092- task = asyncio .wait_for (fut , timeout = 0.2 )
1093- loop .call_later (0.1 , fut .set_result , "ok" )
1094- res = loop .run_until_complete (task )
1095- self .assertEqual (res , "ok" )
1096-
1097- def test_wait_for_cancellation_race_condition (self ):
1098- async def inner ():
1099- with contextlib .suppress (asyncio .CancelledError ):
1100- await asyncio .sleep (1 )
1101- return 1
1102-
1103- async def main ():
1104- result = await asyncio .wait_for (inner (), timeout = .01 )
1105- self .assertEqual (result , 1 )
1106-
1107- asyncio .run (main ())
1108-
1109- def test_wait_for_waits_for_task_cancellation (self ):
1110- loop = asyncio .new_event_loop ()
1111- self .addCleanup (loop .close )
1112-
1113- task_done = False
1114-
1115- async def foo ():
1116- async def inner ():
1117- nonlocal task_done
1118- try :
1119- await asyncio .sleep (0.2 )
1120- except asyncio .CancelledError :
1121- await asyncio .sleep (_EPSILON )
1122- raise
1123- finally :
1124- task_done = True
1125-
1126- inner_task = self .new_task (loop , inner ())
1127-
1128- await asyncio .wait_for (inner_task , timeout = _EPSILON )
1129-
1130- with self .assertRaises (asyncio .TimeoutError ) as cm :
1131- loop .run_until_complete (foo ())
1132-
1133- self .assertTrue (task_done )
1134- chained = cm .exception .__context__
1135- self .assertEqual (type (chained ), asyncio .CancelledError )
1136-
1137- def test_wait_for_waits_for_task_cancellation_w_timeout_0 (self ):
1138- loop = asyncio .new_event_loop ()
1139- self .addCleanup (loop .close )
1140-
1141- task_done = False
1142-
1143- async def foo ():
1144- async def inner ():
1145- nonlocal task_done
1146- try :
1147- await asyncio .sleep (10 )
1148- except asyncio .CancelledError :
1149- await asyncio .sleep (_EPSILON )
1150- raise
1151- finally :
1152- task_done = True
1153-
1154- inner_task = self .new_task (loop , inner ())
1155- await asyncio .sleep (_EPSILON )
1156- await asyncio .wait_for (inner_task , timeout = 0 )
1157-
1158- with self .assertRaises (asyncio .TimeoutError ) as cm :
1159- loop .run_until_complete (foo ())
1160-
1161- self .assertTrue (task_done )
1162- chained = cm .exception .__context__
1163- self .assertEqual (type (chained ), asyncio .CancelledError )
1164-
1165- def test_wait_for_reraises_exception_during_cancellation (self ):
1166- loop = asyncio .new_event_loop ()
1167- self .addCleanup (loop .close )
1168-
1169- class FooException (Exception ):
1170- pass
1171-
1172- async def foo ():
1173- async def inner ():
1174- try :
1175- await asyncio .sleep (0.2 )
1176- finally :
1177- raise FooException
1178-
1179- inner_task = self .new_task (loop , inner ())
1180-
1181- await asyncio .wait_for (inner_task , timeout = _EPSILON )
1182-
1183- with self .assertRaises (FooException ):
1184- loop .run_until_complete (foo ())
1185-
1186- def test_wait_for_self_cancellation (self ):
1187- loop = asyncio .new_event_loop ()
1188- self .addCleanup (loop .close )
1189-
1190- async def foo ():
1191- async def inner ():
1192- try :
1193- await asyncio .sleep (0.3 )
1194- except asyncio .CancelledError :
1195- try :
1196- await asyncio .sleep (0.3 )
1197- except asyncio .CancelledError :
1198- await asyncio .sleep (0.3 )
1199-
1200- return 42
1201-
1202- inner_task = self .new_task (loop , inner ())
1203-
1204- wait = asyncio .wait_for (inner_task , timeout = 0.1 )
1205-
1206- # Test that wait_for itself is properly cancellable
1207- # even when the initial task holds up the initial cancellation.
1208- task = self .new_task (loop , wait )
1209- await asyncio .sleep (0.2 )
1210- task .cancel ()
1211-
1212- with self .assertRaises (asyncio .CancelledError ):
1213- await task
1214-
1215- self .assertEqual (await inner_task , 42 )
1216-
1217- loop .run_until_complete (foo ())
1218-
1219967 def test_wait (self ):
1220968
1221969 def gen ():
0 commit comments