Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Side by Side Diff: Lib/test/test_asyncio/test_tasks.py

Issue 29271: Task.current_task(None) returns unexpected result
Patch Set: Created 3 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Modules/_asynciomodule.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Tests for tasks.py.""" 1 """Tests for tasks.py."""
2 2
3 import collections 3 import collections
4 import contextlib 4 import contextlib
5 import functools 5 import functools
6 import io 6 import io
7 import os 7 import os
8 import re 8 import re
9 import sys 9 import sys
10 import types 10 import types
(...skipping 1442 matching lines...) Expand 10 before | Expand all | Expand 10 after
1453 self.assertIsNone(t2.result()) 1453 self.assertIsNone(t2.result())
1454 1454
1455 def test_current_task(self): 1455 def test_current_task(self):
1456 Task = self.__class__.Task 1456 Task = self.__class__.Task
1457 1457
1458 self.assertIsNone(Task.current_task(loop=self.loop)) 1458 self.assertIsNone(Task.current_task(loop=self.loop))
1459 1459
1460 @asyncio.coroutine 1460 @asyncio.coroutine
1461 def coro(loop): 1461 def coro(loop):
1462 self.assertTrue(Task.current_task(loop=loop) is task) 1462 self.assertTrue(Task.current_task(loop=loop) is task)
1463
1464 # See http://bugs.python.org/issue29271 for details:
1465 asyncio.set_event_loop(loop)
1466 try:
1467 self.assertTrue(Task.current_task(None) is task)
1468 self.assertTrue(Task.current_task() is task)
1469 finally:
1470 asyncio.set_event_loop(None)
1463 1471
1464 task = self.new_task(self.loop, coro(self.loop)) 1472 task = self.new_task(self.loop, coro(self.loop))
1465 self.loop.run_until_complete(task) 1473 self.loop.run_until_complete(task)
1466 self.assertIsNone(Task.current_task(loop=self.loop)) 1474 self.assertIsNone(Task.current_task(loop=self.loop))
1467 1475
1468 def test_current_task_with_interleaving_tasks(self): 1476 def test_current_task_with_interleaving_tasks(self):
1469 Task = self.__class__.Task 1477 Task = self.__class__.Task
1470 1478
1471 self.assertIsNone(Task.current_task(loop=self.loop)) 1479 self.assertIsNone(Task.current_task(loop=self.loop))
1472 1480
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 # the Task._wakeup() method in future._callbacks 1806 # the Task._wakeup() method in future._callbacks
1799 raise Exception("code never reached") 1807 raise Exception("code never reached")
1800 1808
1801 mock_handler = mock.Mock() 1809 mock_handler = mock.Mock()
1802 self.loop.set_debug(True) 1810 self.loop.set_debug(True)
1803 self.loop.set_exception_handler(mock_handler) 1811 self.loop.set_exception_handler(mock_handler)
1804 1812
1805 # schedule the task 1813 # schedule the task
1806 coro = kill_me(self.loop) 1814 coro = kill_me(self.loop)
1807 task = asyncio.ensure_future(coro, loop=self.loop) 1815 task = asyncio.ensure_future(coro, loop=self.loop)
1816
1808 self.assertEqual(Task.all_tasks(loop=self.loop), {task}) 1817 self.assertEqual(Task.all_tasks(loop=self.loop), {task})
1818
1819 # See http://bugs.python.org/issue29271 for details:
1820 asyncio.set_event_loop(self.loop)
1821 try:
1822 self.assertEqual(Task.all_tasks(), {task})
1823 self.assertEqual(Task.all_tasks(None), {task})
1824 finally:
1825 asyncio.set_event_loop(None)
1809 1826
1810 # execute the task so it waits for future 1827 # execute the task so it waits for future
1811 self.loop._run_once() 1828 self.loop._run_once()
1812 self.assertEqual(len(self.loop._ready), 0) 1829 self.assertEqual(len(self.loop._ready), 0)
1813 1830
1814 # remove the future used in kill_me(), and references to the task 1831 # remove the future used in kill_me(), and references to the task
1815 del coro.gi_frame.f_locals['future'] 1832 del coro.gi_frame.f_locals['future']
1816 coro = None 1833 coro = None
1817 source_traceback = task._source_traceback 1834 source_traceback = task._source_traceback
1818 task = None 1835 task = None
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after
2519 num = yield from asyncio.sleep(0, loop=self.loop, result=10) 2536 num = yield from asyncio.sleep(0, loop=self.loop, result=10)
2520 self.assertEqual(result, 1) # inc'ed by call_soon 2537 self.assertEqual(result, 1) # inc'ed by call_soon
2521 inc_result(num) # num should be 11 2538 inc_result(num) # num should be 11
2522 2539
2523 self.loop.run_until_complete(coro()) 2540 self.loop.run_until_complete(coro())
2524 self.assertEqual(result, 11) 2541 self.assertEqual(result, 11)
2525 2542
2526 2543
2527 if __name__ == '__main__': 2544 if __name__ == '__main__':
2528 unittest.main() 2545 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | Modules/_asynciomodule.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+