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