2
2
3
3
import asyncio
4
4
5
- from asyncio import test_utils
6
5
from uvloop import _testbase as tb
7
6
8
7
@@ -30,59 +29,6 @@ def format_coroutine(qualname, state, src, source_traceback, generator=False):
30
29
31
30
class _TestTasks :
32
31
33
- def test_task_repr (self ):
34
- self .loop .set_debug (False )
35
-
36
- @asyncio .coroutine
37
- def notmuch ():
38
- yield from []
39
- return 'abc'
40
-
41
- # test coroutine function
42
- self .assertEqual (notmuch .__name__ , 'notmuch' )
43
- self .assertEqual (notmuch .__qualname__ ,
44
- '_TestTasks.test_task_repr.<locals>.notmuch' )
45
- self .assertEqual (notmuch .__module__ , __name__ )
46
-
47
- filename , lineno = test_utils .get_function_source (notmuch )
48
- src = "%s:%s" % (filename , lineno )
49
-
50
- # test coroutine object
51
- gen = notmuch ()
52
- coro_qualname = '_TestTasks.test_task_repr.<locals>.notmuch'
53
- self .assertEqual (gen .__name__ , 'notmuch' )
54
- self .assertEqual (gen .__qualname__ , coro_qualname )
55
-
56
- # test pending Task
57
- t = asyncio .Task (gen , loop = self .loop )
58
- t .add_done_callback (Dummy ())
59
-
60
- coro = format_coroutine (coro_qualname , 'running' , src ,
61
- t ._source_traceback , generator = True )
62
- self .assertEqual (repr (t ),
63
- '<Task pending %s cb=[<Dummy>()]>' % coro )
64
-
65
- # test canceling Task
66
- t .cancel () # Does not take immediate effect!
67
- self .assertEqual (repr (t ),
68
- '<Task cancelling %s cb=[<Dummy>()]>' % coro )
69
-
70
- # test canceled Task
71
- self .assertRaises (asyncio .CancelledError ,
72
- self .loop .run_until_complete , t )
73
- coro = format_coroutine (coro_qualname , 'done' , src ,
74
- t ._source_traceback )
75
- self .assertEqual (repr (t ),
76
- '<Task cancelled %s>' % coro )
77
-
78
- # test finished Task
79
- t = asyncio .Task (notmuch (), loop = self .loop )
80
- self .loop .run_until_complete (t )
81
- coro = format_coroutine (coro_qualname , 'done' , src ,
82
- t ._source_traceback )
83
- self .assertEqual (repr (t ),
84
- "<Task finished %s result='abc'>" % coro )
85
-
86
32
def test_task_basics (self ):
87
33
@asyncio .coroutine
88
34
def outer ():
@@ -109,7 +55,7 @@ def task():
109
55
return 12
110
56
111
57
t = self .create_task (task ())
112
- test_utils .run_briefly (self .loop ) # start coro
58
+ tb .run_briefly (self .loop ) # start coro
113
59
t .cancel ()
114
60
self .assertRaises (
115
61
asyncio .CancelledError , self .loop .run_until_complete , t )
@@ -126,7 +72,7 @@ def task():
126
72
return 12
127
73
128
74
t = self .create_task (task ())
129
- test_utils .run_briefly (self .loop ) # start task
75
+ tb .run_briefly (self .loop ) # start task
130
76
f .cancel ()
131
77
with self .assertRaises (asyncio .CancelledError ):
132
78
self .loop .run_until_complete (t )
@@ -143,7 +89,7 @@ def task():
143
89
144
90
t = self .create_task (task ())
145
91
self .assertEqual (asyncio .Task .all_tasks (loop = self .loop ), {t })
146
- test_utils .run_briefly (self .loop )
92
+ tb .run_briefly (self .loop )
147
93
148
94
f .cancel ()
149
95
t .cancel ()
@@ -168,10 +114,10 @@ def task():
168
114
return 42
169
115
170
116
t = self .create_task (task ())
171
- test_utils .run_briefly (self .loop )
117
+ tb .run_briefly (self .loop )
172
118
self .assertIs (t ._fut_waiter , fut1 ) # White-box test.
173
119
fut1 .set_result (None )
174
- test_utils .run_briefly (self .loop )
120
+ tb .run_briefly (self .loop )
175
121
self .assertIs (t ._fut_waiter , fut2 ) # White-box test.
176
122
t .cancel ()
177
123
self .assertTrue (fut2 .cancelled ())
@@ -195,14 +141,14 @@ def task():
195
141
return res
196
142
197
143
t = self .create_task (task ())
198
- test_utils .run_briefly (self .loop )
144
+ tb .run_briefly (self .loop )
199
145
self .assertIs (t ._fut_waiter , fut1 ) # White-box test.
200
146
fut1 .set_result (None )
201
- test_utils .run_briefly (self .loop )
147
+ tb .run_briefly (self .loop )
202
148
self .assertIs (t ._fut_waiter , fut2 ) # White-box test.
203
149
t .cancel ()
204
150
self .assertTrue (fut2 .cancelled ())
205
- test_utils .run_briefly (self .loop )
151
+ tb .run_briefly (self .loop )
206
152
self .assertIs (t ._fut_waiter , fut3 ) # White-box test.
207
153
fut3 .set_result (42 )
208
154
res = self .loop .run_until_complete (t )
@@ -232,7 +178,8 @@ def notmutch():
232
178
raise BaseException ()
233
179
234
180
task = self .create_task (notmutch ())
235
- self .assertRaises (BaseException , task ._step )
181
+ with self .assertRaises (BaseException ):
182
+ tb .run_briefly (self .loop )
236
183
237
184
self .assertTrue (task .done ())
238
185
self .assertIsInstance (task .exception (), BaseException )
@@ -245,7 +192,7 @@ def __init__(self, *args, **kwds):
245
192
self .cb_added = False
246
193
super ().__init__ (* args , ** kwds )
247
194
248
- def add_done_callback (self , fn ):
195
+ def add_done_callback (self , fn , context = None ):
249
196
self .cb_added = True
250
197
super ().add_done_callback (fn )
251
198
@@ -258,12 +205,12 @@ def wait_for_future():
258
205
result = yield from fut
259
206
260
207
t = self .create_task (wait_for_future ())
261
- test_utils .run_briefly (self .loop )
208
+ tb .run_briefly (self .loop )
262
209
self .assertTrue (fut .cb_added )
263
210
264
211
res = object ()
265
212
fut .set_result (res )
266
- test_utils .run_briefly (self .loop )
213
+ tb .run_briefly (self .loop )
267
214
self .assertIs (res , result )
268
215
self .assertTrue (t .done ())
269
216
self .assertIsNone (t .result ())
@@ -356,7 +303,7 @@ def outer():
356
303
proof += 10
357
304
358
305
f = asyncio .ensure_future (outer (), loop = self .loop )
359
- test_utils .run_briefly (self .loop )
306
+ tb .run_briefly (self .loop )
360
307
f .cancel ()
361
308
self .loop .run_until_complete (f )
362
309
self .assertEqual (proof , 101 )
@@ -381,12 +328,12 @@ def outer():
381
328
proof += 100
382
329
383
330
f = asyncio .ensure_future (outer (), loop = self .loop )
384
- test_utils .run_briefly (self .loop )
331
+ tb .run_briefly (self .loop )
385
332
f .cancel ()
386
333
self .assertRaises (
387
334
asyncio .CancelledError , self .loop .run_until_complete , f )
388
335
waiter .set_result (None )
389
- test_utils .run_briefly (self .loop )
336
+ tb .run_briefly (self .loop )
390
337
self .assertEqual (proof , 1 )
391
338
392
339
0 commit comments