mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	asyncio: Refactor tests: add a base TestCase class
This commit is contained in:
		
							parent
							
								
									d6f02fc649
								
							
						
					
					
						commit
						c73701de72
					
				
					 13 changed files with 145 additions and 219 deletions
				
			
		| 
						 | 
				
			
			@ -30,15 +30,10 @@ def __call__(self, *args):
 | 
			
		|||
        pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TaskTests(unittest.TestCase):
 | 
			
		||||
class TaskTests(test_utils.TestCase):
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        self.loop = test_utils.TestLoop()
 | 
			
		||||
        asyncio.set_event_loop(None)
 | 
			
		||||
 | 
			
		||||
    def tearDown(self):
 | 
			
		||||
        self.loop.close()
 | 
			
		||||
        gc.collect()
 | 
			
		||||
        self.loop = self.new_test_loop()
 | 
			
		||||
 | 
			
		||||
    def test_task_class(self):
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
| 
						 | 
				
			
			@ -51,6 +46,7 @@ def notmuch():
 | 
			
		|||
        self.assertIs(t._loop, self.loop)
 | 
			
		||||
 | 
			
		||||
        loop = asyncio.new_event_loop()
 | 
			
		||||
        self.set_event_loop(loop)
 | 
			
		||||
        t = asyncio.Task(notmuch(), loop=loop)
 | 
			
		||||
        self.assertIs(t._loop, loop)
 | 
			
		||||
        loop.close()
 | 
			
		||||
| 
						 | 
				
			
			@ -66,6 +62,7 @@ def notmuch():
 | 
			
		|||
        self.assertIs(t._loop, self.loop)
 | 
			
		||||
 | 
			
		||||
        loop = asyncio.new_event_loop()
 | 
			
		||||
        self.set_event_loop(loop)
 | 
			
		||||
        t = asyncio.async(notmuch(), loop=loop)
 | 
			
		||||
        self.assertIs(t._loop, loop)
 | 
			
		||||
        loop.close()
 | 
			
		||||
| 
						 | 
				
			
			@ -81,6 +78,7 @@ def test_async_future(self):
 | 
			
		|||
        self.assertIs(f, f_orig)
 | 
			
		||||
 | 
			
		||||
        loop = asyncio.new_event_loop()
 | 
			
		||||
        self.set_event_loop(loop)
 | 
			
		||||
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            f = asyncio.async(f_orig, loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -102,6 +100,7 @@ def notmuch():
 | 
			
		|||
        self.assertIs(t, t_orig)
 | 
			
		||||
 | 
			
		||||
        loop = asyncio.new_event_loop()
 | 
			
		||||
        self.set_event_loop(loop)
 | 
			
		||||
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            t = asyncio.async(t_orig, loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -220,8 +219,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(10.0, when)
 | 
			
		||||
            yield 0
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
        def task():
 | 
			
		||||
| 
						 | 
				
			
			@ -346,7 +344,7 @@ def task():
 | 
			
		|||
 | 
			
		||||
    def test_cancel_current_task(self):
 | 
			
		||||
        loop = asyncio.new_event_loop()
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        self.set_event_loop(loop)
 | 
			
		||||
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
        def task():
 | 
			
		||||
| 
						 | 
				
			
			@ -374,8 +372,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.3, when)
 | 
			
		||||
            yield 0.1
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        x = 0
 | 
			
		||||
        waiters = []
 | 
			
		||||
| 
						 | 
				
			
			@ -410,8 +407,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.1, when)
 | 
			
		||||
            when = yield 0.1
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        foo_running = None
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -436,8 +432,7 @@ def foo():
 | 
			
		|||
        self.assertEqual(foo_running, False)
 | 
			
		||||
 | 
			
		||||
    def test_wait_for_blocking(self):
 | 
			
		||||
        loop = test_utils.TestLoop()
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop()
 | 
			
		||||
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
        def coro():
 | 
			
		||||
| 
						 | 
				
			
			@ -457,8 +452,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.01, when)
 | 
			
		||||
            yield 0.01
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
        def foo():
 | 
			
		||||
| 
						 | 
				
			
			@ -486,8 +480,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.15, when)
 | 
			
		||||
            yield 0.15
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
 | 
			
		||||
        b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -517,8 +510,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.015, when)
 | 
			
		||||
            yield 0.015
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(0.01, loop=loop), loop=loop)
 | 
			
		||||
        b = asyncio.Task(asyncio.sleep(0.015, loop=loop), loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -531,11 +523,8 @@ def foo():
 | 
			
		|||
            return 42
 | 
			
		||||
 | 
			
		||||
        asyncio.set_event_loop(loop)
 | 
			
		||||
        try:
 | 
			
		||||
            res = loop.run_until_complete(
 | 
			
		||||
                asyncio.Task(foo(), loop=loop))
 | 
			
		||||
        finally:
 | 
			
		||||
            asyncio.set_event_loop(None)
 | 
			
		||||
        res = loop.run_until_complete(
 | 
			
		||||
            asyncio.Task(foo(), loop=loop))
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(res, 42)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -573,8 +562,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.1, when)
 | 
			
		||||
            yield 0.1
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
 | 
			
		||||
        b = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -629,8 +617,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(10.0, when)
 | 
			
		||||
            yield 0
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        # first_exception, task already has exception
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -663,8 +650,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.01, when)
 | 
			
		||||
            yield 0.01
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        # first_exception, exception during waiting
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -696,8 +682,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.15, when)
 | 
			
		||||
            yield 0.15
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -733,8 +718,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.11, when)
 | 
			
		||||
            yield 0.11
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
 | 
			
		||||
        b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -764,8 +748,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.1, when)
 | 
			
		||||
            yield 0.1
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
 | 
			
		||||
        b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -789,8 +772,7 @@ def gen():
 | 
			
		|||
            yield 0.01
 | 
			
		||||
            yield 0
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
        completed = set()
 | 
			
		||||
        time_shifted = False
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -833,8 +815,7 @@ def gen():
 | 
			
		|||
            yield 0
 | 
			
		||||
            yield 0.1
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.sleep(0.1, 'a', loop=loop)
 | 
			
		||||
        b = asyncio.sleep(0.15, 'b', loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -870,8 +851,7 @@ def gen():
 | 
			
		|||
            yield 0
 | 
			
		||||
            yield 0.01
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.sleep(0.01, 'a', loop=loop)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -890,8 +870,7 @@ def gen():
 | 
			
		|||
            yield 0.05
 | 
			
		||||
            yield 0
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.sleep(0.05, 'a', loop=loop)
 | 
			
		||||
        b = asyncio.sleep(0.10, 'b', loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -916,8 +895,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.05, when)
 | 
			
		||||
            yield 0.05
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        a = asyncio.sleep(0.05, 'a', loop=loop)
 | 
			
		||||
        b = asyncio.sleep(0.05, 'b', loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -958,8 +936,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(0.1, when)
 | 
			
		||||
            yield 0.05
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
        def sleeper(dt, arg):
 | 
			
		||||
| 
						 | 
				
			
			@ -980,8 +957,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(10.0, when)
 | 
			
		||||
            yield 0
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        t = asyncio.Task(asyncio.sleep(10.0, 'yeah', loop=loop),
 | 
			
		||||
                         loop=loop)
 | 
			
		||||
| 
						 | 
				
			
			@ -1012,8 +988,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(5000, when)
 | 
			
		||||
            yield 0.1
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
        def sleep(dt):
 | 
			
		||||
| 
						 | 
				
			
			@ -1123,8 +1098,7 @@ def gen():
 | 
			
		|||
            self.assertAlmostEqual(10.0, when)
 | 
			
		||||
            yield 0
 | 
			
		||||
 | 
			
		||||
        loop = test_utils.TestLoop(gen)
 | 
			
		||||
        self.addCleanup(loop.close)
 | 
			
		||||
        loop = self.new_test_loop(gen)
 | 
			
		||||
 | 
			
		||||
        @asyncio.coroutine
 | 
			
		||||
        def sleeper():
 | 
			
		||||
| 
						 | 
				
			
			@ -1536,12 +1510,9 @@ def foo(): yield from []
 | 
			
		|||
class GatherTestsBase:
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        self.one_loop = test_utils.TestLoop()
 | 
			
		||||
        self.other_loop = test_utils.TestLoop()
 | 
			
		||||
 | 
			
		||||
    def tearDown(self):
 | 
			
		||||
        self.one_loop.close()
 | 
			
		||||
        self.other_loop.close()
 | 
			
		||||
        self.one_loop = self.new_test_loop()
 | 
			
		||||
        self.other_loop = self.new_test_loop()
 | 
			
		||||
        self.set_event_loop(self.one_loop, cleanup=False)
 | 
			
		||||
 | 
			
		||||
    def _run_loop(self, loop):
 | 
			
		||||
        while loop._ready:
 | 
			
		||||
| 
						 | 
				
			
			@ -1633,7 +1604,7 @@ def test_env_var_debug(self):
 | 
			
		|||
        self.assertEqual(stdout.rstrip(), b'False')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FutureGatherTests(GatherTestsBase, unittest.TestCase):
 | 
			
		||||
class FutureGatherTests(GatherTestsBase, test_utils.TestCase):
 | 
			
		||||
 | 
			
		||||
    def wrap_futures(self, *futures):
 | 
			
		||||
        return futures
 | 
			
		||||
| 
						 | 
				
			
			@ -1717,16 +1688,12 @@ def test_result_exception_one_cancellation(self):
 | 
			
		|||
        cb.assert_called_once_with(fut)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CoroutineGatherTests(GatherTestsBase, unittest.TestCase):
 | 
			
		||||
class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        super().setUp()
 | 
			
		||||
        asyncio.set_event_loop(self.one_loop)
 | 
			
		||||
 | 
			
		||||
    def tearDown(self):
 | 
			
		||||
        asyncio.set_event_loop(None)
 | 
			
		||||
        super().tearDown()
 | 
			
		||||
 | 
			
		||||
    def wrap_futures(self, *futures):
 | 
			
		||||
        coros = []
 | 
			
		||||
        for fut in futures:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue