summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_asyncio/test_tasks.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2013-12-06 20:57:40 (GMT)
committerGuido van Rossum <guido@python.org>2013-12-06 20:57:40 (GMT)
commit1a605ed5a33dbeec6b98d2a073fbbe3fcfdd84c0 (patch)
treefce1163198c3e6f531b8cd0e03413f589ada61c0 /Lib/test/test_asyncio/test_tasks.py
parent2f8c83568ca5850601e92e315c4a1c840e94b1cb (diff)
downloadcpython-1a605ed5a33dbeec6b98d2a073fbbe3fcfdd84c0.zip
cpython-1a605ed5a33dbeec6b98d2a073fbbe3fcfdd84c0.tar.gz
cpython-1a605ed5a33dbeec6b98d2a073fbbe3fcfdd84c0.tar.bz2
asyncio: Add Task.current_task() class method.
Diffstat (limited to 'Lib/test/test_asyncio/test_tasks.py')
-rw-r--r--Lib/test/test_asyncio/test_tasks.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 8f0d081..5470da1 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -1113,6 +1113,42 @@ class TaskTests(unittest.TestCase):
self.assertEqual(res, 'test')
self.assertIsNone(t2.result())
+ def test_current_task(self):
+ self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+ @tasks.coroutine
+ def coro(loop):
+ self.assertTrue(tasks.Task.current_task(loop=loop) is task)
+
+ task = tasks.Task(coro(self.loop), loop=self.loop)
+ self.loop.run_until_complete(task)
+ self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+
+ def test_current_task_with_interleaving_tasks(self):
+ self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+
+ fut1 = futures.Future(loop=self.loop)
+ fut2 = futures.Future(loop=self.loop)
+
+ @tasks.coroutine
+ def coro1(loop):
+ self.assertTrue(tasks.Task.current_task(loop=loop) is task1)
+ yield from fut1
+ self.assertTrue(tasks.Task.current_task(loop=loop) is task1)
+ fut2.set_result(True)
+
+ @tasks.coroutine
+ def coro2(loop):
+ self.assertTrue(tasks.Task.current_task(loop=loop) is task2)
+ fut1.set_result(True)
+ yield from fut2
+ self.assertTrue(tasks.Task.current_task(loop=loop) is task2)
+
+ task1 = tasks.Task(coro1(self.loop), loop=self.loop)
+ task2 = tasks.Task(coro2(self.loop), loop=self.loop)
+
+ self.loop.run_until_complete(tasks.wait((task1, task2), loop=self.loop))
+ self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+
# Some thorough tests for cancellation propagation through
# coroutines, tasks and wait().