@@ -1332,17 +1332,23 @@ def coro():
13321332 self .assertIsNone (task ._fut_waiter )
13331333 self .assertTrue (fut .cancelled ())
13341334
1335- def test_step_in_completed_task (self ):
1335+ def test_task_set_methods (self ):
13361336 @asyncio .coroutine
13371337 def notmuch ():
13381338 return 'ko'
13391339
13401340 gen = notmuch ()
13411341 task = self .new_task (self .loop , gen )
1342- task .set_result ('ok' )
13431342
1344- self .assertRaises (AssertionError , task ._step )
1345- gen .close ()
1343+ with self .assertRaisesRegex (RuntimeError , 'not support set_result' ):
1344+ task .set_result ('ok' )
1345+
1346+ with self .assertRaisesRegex (RuntimeError , 'not support set_exception' ):
1347+ task .set_exception (ValueError ())
1348+
1349+ self .assertEqual (
1350+ self .loop .run_until_complete (task ),
1351+ 'ko' )
13461352
13471353 def test_step_result (self ):
13481354 @asyncio .coroutine
@@ -2231,10 +2237,59 @@ async def func():
22312237 return cls
22322238
22332239
2240+ class SetMethodsTest :
2241+
2242+ def test_set_result_causes_invalid_state (self ):
2243+ Future = type (self ).Future
2244+ self .loop .call_exception_handler = exc_handler = mock .Mock ()
2245+
2246+ async def foo ():
2247+ await asyncio .sleep (0.1 , loop = self .loop )
2248+ return 10
2249+
2250+ task = self .new_task (self .loop , foo ())
2251+ Future .set_result (task , 'spam' )
2252+
2253+ self .assertEqual (
2254+ self .loop .run_until_complete (task ),
2255+ 'spam' )
2256+
2257+ exc_handler .assert_called_once ()
2258+ exc = exc_handler .call_args [0 ][0 ]['exception' ]
2259+ with self .assertRaisesRegex (asyncio .InvalidStateError ,
2260+ r'step\(\): already done' ):
2261+ raise exc
2262+
2263+ def test_set_exception_causes_invalid_state (self ):
2264+ class MyExc (Exception ):
2265+ pass
2266+
2267+ Future = type (self ).Future
2268+ self .loop .call_exception_handler = exc_handler = mock .Mock ()
2269+
2270+ async def foo ():
2271+ await asyncio .sleep (0.1 , loop = self .loop )
2272+ return 10
2273+
2274+ task = self .new_task (self .loop , foo ())
2275+ Future .set_exception (task , MyExc ())
2276+
2277+ with self .assertRaises (MyExc ):
2278+ self .loop .run_until_complete (task )
2279+
2280+ exc_handler .assert_called_once ()
2281+ exc = exc_handler .call_args [0 ][0 ]['exception' ]
2282+ with self .assertRaisesRegex (asyncio .InvalidStateError ,
2283+ r'step\(\): already done' ):
2284+ raise exc
2285+
2286+
22342287@unittest .skipUnless (hasattr (futures , '_CFuture' ) and
22352288 hasattr (tasks , '_CTask' ),
22362289 'requires the C _asyncio module' )
2237- class CTask_CFuture_Tests (BaseTaskTests , test_utils .TestCase ):
2290+ class CTask_CFuture_Tests (BaseTaskTests , SetMethodsTest ,
2291+ test_utils .TestCase ):
2292+
22382293 Task = getattr (tasks , '_CTask' , None )
22392294 Future = getattr (futures , '_CFuture' , None )
22402295
@@ -2245,21 +2300,16 @@ class CTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase):
22452300@add_subclass_tests
22462301class CTask_CFuture_SubclassTests (BaseTaskTests , test_utils .TestCase ):
22472302
2248- class Task (tasks ._CTask ):
2249- pass
2250-
2251- class Future (futures ._CFuture ):
2252- pass
2303+ Task = getattr (tasks , '_CTask' , None )
2304+ Future = getattr (futures , '_CFuture' , None )
22532305
22542306
22552307@unittest .skipUnless (hasattr (tasks , '_CTask' ),
22562308 'requires the C _asyncio module' )
22572309@add_subclass_tests
22582310class CTaskSubclass_PyFuture_Tests (BaseTaskTests , test_utils .TestCase ):
22592311
2260- class Task (tasks ._CTask ):
2261- pass
2262-
2312+ Task = getattr (tasks , '_CTask' , None )
22632313 Future = futures ._PyFuture
22642314
22652315
@@ -2268,38 +2318,37 @@ class Task(tasks._CTask):
22682318@add_subclass_tests
22692319class PyTask_CFutureSubclass_Tests (BaseTaskTests , test_utils .TestCase ):
22702320
2271- class Future (futures ._CFuture ):
2272- pass
2273-
2321+ Future = getattr (futures , '_CFuture' , None )
22742322 Task = tasks ._PyTask
22752323
22762324
22772325@unittest .skipUnless (hasattr (tasks , '_CTask' ),
22782326 'requires the C _asyncio module' )
22792327class CTask_PyFuture_Tests (BaseTaskTests , test_utils .TestCase ):
2328+
22802329 Task = getattr (tasks , '_CTask' , None )
22812330 Future = futures ._PyFuture
22822331
22832332
22842333@unittest .skipUnless (hasattr (futures , '_CFuture' ),
22852334 'requires the C _asyncio module' )
22862335class PyTask_CFuture_Tests (BaseTaskTests , test_utils .TestCase ):
2336+
22872337 Task = tasks ._PyTask
22882338 Future = getattr (futures , '_CFuture' , None )
22892339
22902340
2291- class PyTask_PyFuture_Tests (BaseTaskTests , test_utils .TestCase ):
2341+ class PyTask_PyFuture_Tests (BaseTaskTests , SetMethodsTest ,
2342+ test_utils .TestCase ):
2343+
22922344 Task = tasks ._PyTask
22932345 Future = futures ._PyFuture
22942346
22952347
22962348@add_subclass_tests
22972349class PyTask_PyFuture_SubclassTests (BaseTaskTests , test_utils .TestCase ):
2298- class Task (tasks ._PyTask ):
2299- pass
2300-
2301- class Future (futures ._PyFuture ):
2302- pass
2350+ Task = tasks ._PyTask
2351+ Future = futures ._PyFuture
23032352
23042353
23052354@unittest .skipUnless (hasattr (tasks , '_CTask' ),
0 commit comments