@@ -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 ,))
@@ -1031,251 +1024,6 @@ async def coro():
1031
1024
task ._log_traceback = True
1032
1025
self .loop .run_until_complete (task )
1033
1026
1034
- def test_wait_for_timeout_less_then_0_or_0_future_done (self ):
1035
- def gen ():
1036
- when = yield
1037
- self .assertAlmostEqual (0 , when )
1038
-
1039
- loop = self .new_test_loop (gen )
1040
-
1041
- fut = self .new_future (loop )
1042
- fut .set_result ('done' )
1043
-
1044
- ret = loop .run_until_complete (asyncio .wait_for (fut , 0 ))
1045
-
1046
- self .assertEqual (ret , 'done' )
1047
- self .assertTrue (fut .done ())
1048
- self .assertAlmostEqual (0 , loop .time ())
1049
-
1050
- def test_wait_for_timeout_less_then_0_or_0_coroutine_do_not_started (self ):
1051
- def gen ():
1052
- when = yield
1053
- self .assertAlmostEqual (0 , when )
1054
-
1055
- loop = self .new_test_loop (gen )
1056
-
1057
- foo_started = False
1058
-
1059
- async def foo ():
1060
- nonlocal foo_started
1061
- foo_started = True
1062
-
1063
- with self .assertRaises (asyncio .TimeoutError ):
1064
- loop .run_until_complete (asyncio .wait_for (foo (), 0 ))
1065
-
1066
- self .assertAlmostEqual (0 , loop .time ())
1067
- self .assertEqual (foo_started , False )
1068
-
1069
- def test_wait_for_timeout_less_then_0_or_0 (self ):
1070
- def gen ():
1071
- when = yield
1072
- self .assertAlmostEqual (0.2 , when )
1073
- when = yield 0
1074
- self .assertAlmostEqual (0 , when )
1075
-
1076
- for timeout in [0 , - 1 ]:
1077
- with self .subTest (timeout = timeout ):
1078
- loop = self .new_test_loop (gen )
1079
-
1080
- foo_running = None
1081
-
1082
- async def foo ():
1083
- nonlocal foo_running
1084
- foo_running = True
1085
- try :
1086
- await asyncio .sleep (0.2 )
1087
- finally :
1088
- foo_running = False
1089
- return 'done'
1090
-
1091
- fut = self .new_task (loop , foo ())
1092
-
1093
- with self .assertRaises (asyncio .TimeoutError ):
1094
- loop .run_until_complete (asyncio .wait_for (fut , timeout ))
1095
- self .assertTrue (fut .done ())
1096
- # it should have been cancelled due to the timeout
1097
- self .assertTrue (fut .cancelled ())
1098
- self .assertAlmostEqual (0 , loop .time ())
1099
- self .assertEqual (foo_running , False )
1100
-
1101
- def test_wait_for (self ):
1102
-
1103
- def gen ():
1104
- when = yield
1105
- self .assertAlmostEqual (0.2 , when )
1106
- when = yield 0
1107
- self .assertAlmostEqual (0.1 , when )
1108
- when = yield 0.1
1109
-
1110
- loop = self .new_test_loop (gen )
1111
-
1112
- foo_running = None
1113
-
1114
- async def foo ():
1115
- nonlocal foo_running
1116
- foo_running = True
1117
- try :
1118
- await asyncio .sleep (0.2 )
1119
- finally :
1120
- foo_running = False
1121
- return 'done'
1122
-
1123
- fut = self .new_task (loop , foo ())
1124
-
1125
- with self .assertRaises (asyncio .TimeoutError ):
1126
- loop .run_until_complete (asyncio .wait_for (fut , 0.1 ))
1127
- self .assertTrue (fut .done ())
1128
- # it should have been cancelled due to the timeout
1129
- self .assertTrue (fut .cancelled ())
1130
- self .assertAlmostEqual (0.1 , loop .time ())
1131
- self .assertEqual (foo_running , False )
1132
-
1133
- def test_wait_for_blocking (self ):
1134
- loop = self .new_test_loop ()
1135
-
1136
- async def coro ():
1137
- return 'done'
1138
-
1139
- res = loop .run_until_complete (asyncio .wait_for (coro (), timeout = None ))
1140
- self .assertEqual (res , 'done' )
1141
-
1142
- def test_wait_for_race_condition (self ):
1143
-
1144
- def gen ():
1145
- yield 0.1
1146
- yield 0.1
1147
- yield 0.1
1148
-
1149
- loop = self .new_test_loop (gen )
1150
-
1151
- fut = self .new_future (loop )
1152
- task = asyncio .wait_for (fut , timeout = 0.2 )
1153
- loop .call_later (0.1 , fut .set_result , "ok" )
1154
- res = loop .run_until_complete (task )
1155
- self .assertEqual (res , "ok" )
1156
-
1157
- def test_wait_for_cancellation_race_condition (self ):
1158
- async def inner ():
1159
- with contextlib .suppress (asyncio .CancelledError ):
1160
- await asyncio .sleep (1 )
1161
- return 1
1162
-
1163
- async def main ():
1164
- result = await asyncio .wait_for (inner (), timeout = .01 )
1165
- self .assertEqual (result , 1 )
1166
-
1167
- asyncio .run (main ())
1168
-
1169
- def test_wait_for_waits_for_task_cancellation (self ):
1170
- loop = asyncio .new_event_loop ()
1171
- self .addCleanup (loop .close )
1172
-
1173
- task_done = False
1174
-
1175
- async def foo ():
1176
- async def inner ():
1177
- nonlocal task_done
1178
- try :
1179
- await asyncio .sleep (0.2 )
1180
- except asyncio .CancelledError :
1181
- await asyncio .sleep (_EPSILON )
1182
- raise
1183
- finally :
1184
- task_done = True
1185
-
1186
- inner_task = self .new_task (loop , inner ())
1187
-
1188
- await asyncio .wait_for (inner_task , timeout = _EPSILON )
1189
-
1190
- with self .assertRaises (asyncio .TimeoutError ) as cm :
1191
- loop .run_until_complete (foo ())
1192
-
1193
- self .assertTrue (task_done )
1194
- chained = cm .exception .__context__
1195
- self .assertEqual (type (chained ), asyncio .CancelledError )
1196
-
1197
- def test_wait_for_waits_for_task_cancellation_w_timeout_0 (self ):
1198
- loop = asyncio .new_event_loop ()
1199
- self .addCleanup (loop .close )
1200
-
1201
- task_done = False
1202
-
1203
- async def foo ():
1204
- async def inner ():
1205
- nonlocal task_done
1206
- try :
1207
- await asyncio .sleep (10 )
1208
- except asyncio .CancelledError :
1209
- await asyncio .sleep (_EPSILON )
1210
- raise
1211
- finally :
1212
- task_done = True
1213
-
1214
- inner_task = self .new_task (loop , inner ())
1215
- await asyncio .sleep (_EPSILON )
1216
- await asyncio .wait_for (inner_task , timeout = 0 )
1217
-
1218
- with self .assertRaises (asyncio .TimeoutError ) as cm :
1219
- loop .run_until_complete (foo ())
1220
-
1221
- self .assertTrue (task_done )
1222
- chained = cm .exception .__context__
1223
- self .assertEqual (type (chained ), asyncio .CancelledError )
1224
-
1225
- def test_wait_for_reraises_exception_during_cancellation (self ):
1226
- loop = asyncio .new_event_loop ()
1227
- self .addCleanup (loop .close )
1228
-
1229
- class FooException (Exception ):
1230
- pass
1231
-
1232
- async def foo ():
1233
- async def inner ():
1234
- try :
1235
- await asyncio .sleep (0.2 )
1236
- finally :
1237
- raise FooException
1238
-
1239
- inner_task = self .new_task (loop , inner ())
1240
-
1241
- await asyncio .wait_for (inner_task , timeout = _EPSILON )
1242
-
1243
- with self .assertRaises (FooException ):
1244
- loop .run_until_complete (foo ())
1245
-
1246
- def test_wait_for_self_cancellation (self ):
1247
- loop = asyncio .new_event_loop ()
1248
- self .addCleanup (loop .close )
1249
-
1250
- async def foo ():
1251
- async def inner ():
1252
- try :
1253
- await asyncio .sleep (0.3 )
1254
- except asyncio .CancelledError :
1255
- try :
1256
- await asyncio .sleep (0.3 )
1257
- except asyncio .CancelledError :
1258
- await asyncio .sleep (0.3 )
1259
-
1260
- return 42
1261
-
1262
- inner_task = self .new_task (loop , inner ())
1263
-
1264
- wait = asyncio .wait_for (inner_task , timeout = 0.1 )
1265
-
1266
- # Test that wait_for itself is properly cancellable
1267
- # even when the initial task holds up the initial cancellation.
1268
- task = self .new_task (loop , wait )
1269
- await asyncio .sleep (0.2 )
1270
- task .cancel ()
1271
-
1272
- with self .assertRaises (asyncio .CancelledError ):
1273
- await task
1274
-
1275
- self .assertEqual (await inner_task , 42 )
1276
-
1277
- loop .run_until_complete (foo ())
1278
-
1279
1027
def test_wait (self ):
1280
1028
1281
1029
def gen ():
0 commit comments