22
33import asyncio
44
5- from asyncio import test_utils
65from uvloop import _testbase as tb
76
87
@@ -30,59 +29,6 @@ def format_coroutine(qualname, state, src, source_traceback, generator=False):
3029
3130class _TestTasks :
3231
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-
8632 def test_task_basics (self ):
8733 @asyncio .coroutine
8834 def outer ():
@@ -109,7 +55,7 @@ def task():
10955 return 12
11056
11157 t = self .create_task (task ())
112- test_utils .run_briefly (self .loop ) # start coro
58+ tb .run_briefly (self .loop ) # start coro
11359 t .cancel ()
11460 self .assertRaises (
11561 asyncio .CancelledError , self .loop .run_until_complete , t )
@@ -126,7 +72,7 @@ def task():
12672 return 12
12773
12874 t = self .create_task (task ())
129- test_utils .run_briefly (self .loop ) # start task
75+ tb .run_briefly (self .loop ) # start task
13076 f .cancel ()
13177 with self .assertRaises (asyncio .CancelledError ):
13278 self .loop .run_until_complete (t )
@@ -143,7 +89,7 @@ def task():
14389
14490 t = self .create_task (task ())
14591 self .assertEqual (asyncio .Task .all_tasks (loop = self .loop ), {t })
146- test_utils .run_briefly (self .loop )
92+ tb .run_briefly (self .loop )
14793
14894 f .cancel ()
14995 t .cancel ()
@@ -168,10 +114,10 @@ def task():
168114 return 42
169115
170116 t = self .create_task (task ())
171- test_utils .run_briefly (self .loop )
117+ tb .run_briefly (self .loop )
172118 self .assertIs (t ._fut_waiter , fut1 ) # White-box test.
173119 fut1 .set_result (None )
174- test_utils .run_briefly (self .loop )
120+ tb .run_briefly (self .loop )
175121 self .assertIs (t ._fut_waiter , fut2 ) # White-box test.
176122 t .cancel ()
177123 self .assertTrue (fut2 .cancelled ())
@@ -195,14 +141,14 @@ def task():
195141 return res
196142
197143 t = self .create_task (task ())
198- test_utils .run_briefly (self .loop )
144+ tb .run_briefly (self .loop )
199145 self .assertIs (t ._fut_waiter , fut1 ) # White-box test.
200146 fut1 .set_result (None )
201- test_utils .run_briefly (self .loop )
147+ tb .run_briefly (self .loop )
202148 self .assertIs (t ._fut_waiter , fut2 ) # White-box test.
203149 t .cancel ()
204150 self .assertTrue (fut2 .cancelled ())
205- test_utils .run_briefly (self .loop )
151+ tb .run_briefly (self .loop )
206152 self .assertIs (t ._fut_waiter , fut3 ) # White-box test.
207153 fut3 .set_result (42 )
208154 res = self .loop .run_until_complete (t )
@@ -232,7 +178,8 @@ def notmutch():
232178 raise BaseException ()
233179
234180 task = self .create_task (notmutch ())
235- self .assertRaises (BaseException , task ._step )
181+ with self .assertRaises (BaseException ):
182+ tb .run_briefly (self .loop )
236183
237184 self .assertTrue (task .done ())
238185 self .assertIsInstance (task .exception (), BaseException )
@@ -245,7 +192,7 @@ def __init__(self, *args, **kwds):
245192 self .cb_added = False
246193 super ().__init__ (* args , ** kwds )
247194
248- def add_done_callback (self , fn ):
195+ def add_done_callback (self , fn , context = None ):
249196 self .cb_added = True
250197 super ().add_done_callback (fn )
251198
@@ -258,12 +205,12 @@ def wait_for_future():
258205 result = yield from fut
259206
260207 t = self .create_task (wait_for_future ())
261- test_utils .run_briefly (self .loop )
208+ tb .run_briefly (self .loop )
262209 self .assertTrue (fut .cb_added )
263210
264211 res = object ()
265212 fut .set_result (res )
266- test_utils .run_briefly (self .loop )
213+ tb .run_briefly (self .loop )
267214 self .assertIs (res , result )
268215 self .assertTrue (t .done ())
269216 self .assertIsNone (t .result ())
@@ -356,7 +303,7 @@ def outer():
356303 proof += 10
357304
358305 f = asyncio .ensure_future (outer (), loop = self .loop )
359- test_utils .run_briefly (self .loop )
306+ tb .run_briefly (self .loop )
360307 f .cancel ()
361308 self .loop .run_until_complete (f )
362309 self .assertEqual (proof , 101 )
@@ -381,12 +328,12 @@ def outer():
381328 proof += 100
382329
383330 f = asyncio .ensure_future (outer (), loop = self .loop )
384- test_utils .run_briefly (self .loop )
331+ tb .run_briefly (self .loop )
385332 f .cancel ()
386333 self .assertRaises (
387334 asyncio .CancelledError , self .loop .run_until_complete , f )
388335 waiter .set_result (None )
389- test_utils .run_briefly (self .loop )
336+ tb .run_briefly (self .loop )
390337 self .assertEqual (proof , 1 )
391338
392339
0 commit comments