|
| 1 | +import asyncio |
| 2 | +import decimal |
| 3 | +import random |
| 4 | +import sys |
| 5 | +import unittest |
| 6 | + |
| 7 | +from uvloop import _testbase as tb |
| 8 | + |
| 9 | + |
| 10 | +PY37 = sys.version_info >= (3, 7, 0) |
| 11 | + |
| 12 | + |
| 13 | +class _ContextBaseTests: |
| 14 | + |
| 15 | + @unittest.skipUnless(PY37, 'requires Python 3.7') |
| 16 | + def test_task_decimal_context(self): |
| 17 | + async def fractions(t, precision, x, y): |
| 18 | + with decimal.localcontext() as ctx: |
| 19 | + ctx.prec = precision |
| 20 | + a = decimal.Decimal(x) / decimal.Decimal(y) |
| 21 | + await asyncio.sleep(t, loop=self.loop) |
| 22 | + b = decimal.Decimal(x) / decimal.Decimal(y ** 2) |
| 23 | + return a, b |
| 24 | + |
| 25 | + async def main(): |
| 26 | + r1, r2 = await asyncio.gather( |
| 27 | + fractions(0.1, 3, 1, 3), fractions(0.2, 6, 1, 3), |
| 28 | + loop=self.loop) |
| 29 | + |
| 30 | + return r1, r2 |
| 31 | + |
| 32 | + r1, r2 = self.loop.run_until_complete(main()) |
| 33 | + |
| 34 | + self.assertEqual(str(r1[0]), '0.333') |
| 35 | + self.assertEqual(str(r1[1]), '0.111') |
| 36 | + |
| 37 | + self.assertEqual(str(r2[0]), '0.333333') |
| 38 | + self.assertEqual(str(r2[1]), '0.111111') |
| 39 | + |
| 40 | + @unittest.skipUnless(PY37, 'requires Python 3.7') |
| 41 | + def test_task_context_1(self): |
| 42 | + import contextvars |
| 43 | + cvar = contextvars.ContextVar('cvar', default='nope') |
| 44 | + |
| 45 | + async def sub(): |
| 46 | + await asyncio.sleep(0.01, loop=self.loop) |
| 47 | + self.assertEqual(cvar.get(), 'nope') |
| 48 | + cvar.set('something else') |
| 49 | + |
| 50 | + async def main(): |
| 51 | + self.assertEqual(cvar.get(), 'nope') |
| 52 | + subtask = self.loop.create_task(sub()) |
| 53 | + cvar.set('yes') |
| 54 | + self.assertEqual(cvar.get(), 'yes') |
| 55 | + await subtask |
| 56 | + self.assertEqual(cvar.get(), 'yes') |
| 57 | + |
| 58 | + task = self.loop.create_task(main()) |
| 59 | + self.loop.run_until_complete(task) |
| 60 | + |
| 61 | + @unittest.skipUnless(PY37, 'requires Python 3.7') |
| 62 | + def test_task_context_2(self): |
| 63 | + import contextvars |
| 64 | + cvar = contextvars.ContextVar('cvar', default='nope') |
| 65 | + |
| 66 | + async def main(): |
| 67 | + def fut_on_done(fut): |
| 68 | + # This change must not pollute the context |
| 69 | + # of the "main()" task. |
| 70 | + cvar.set('something else') |
| 71 | + |
| 72 | + self.assertEqual(cvar.get(), 'nope') |
| 73 | + |
| 74 | + for j in range(2): |
| 75 | + fut = self.loop.create_future() |
| 76 | + fut.add_done_callback(fut_on_done) |
| 77 | + cvar.set('yes{}'.format(j)) |
| 78 | + self.loop.call_soon(fut.set_result, None) |
| 79 | + await fut |
| 80 | + self.assertEqual(cvar.get(), 'yes{}'.format(j)) |
| 81 | + |
| 82 | + for i in range(3): |
| 83 | + # Test that task passed its context to add_done_callback: |
| 84 | + cvar.set('yes{}-{}'.format(i, j)) |
| 85 | + await asyncio.sleep(0.001, loop=self.loop) |
| 86 | + self.assertEqual(cvar.get(), 'yes{}-{}'.format(i, j)) |
| 87 | + |
| 88 | + task = self.loop.create_task(main()) |
| 89 | + self.loop.run_until_complete(task) |
| 90 | + |
| 91 | + self.assertEqual(cvar.get(), 'nope') |
| 92 | + |
| 93 | + @unittest.skipUnless(PY37, 'requires Python 3.7') |
| 94 | + def test_task_context_3(self): |
| 95 | + import contextvars |
| 96 | + cvar = contextvars.ContextVar('cvar', default=-1) |
| 97 | + |
| 98 | + # Run 100 Tasks in parallel, each modifying cvar. |
| 99 | + |
| 100 | + async def sub(num): |
| 101 | + for i in range(10): |
| 102 | + cvar.set(num + i) |
| 103 | + await asyncio.sleep( |
| 104 | + random.uniform(0.001, 0.05), loop=self.loop) |
| 105 | + self.assertEqual(cvar.get(), num + i) |
| 106 | + |
| 107 | + async def main(): |
| 108 | + tasks = [] |
| 109 | + for i in range(100): |
| 110 | + task = self.loop.create_task(sub(random.randint(0, 10))) |
| 111 | + tasks.append(task) |
| 112 | + |
| 113 | + await asyncio.gather( |
| 114 | + *tasks, loop=self.loop, return_exceptions=True) |
| 115 | + |
| 116 | + self.loop.run_until_complete(main()) |
| 117 | + |
| 118 | + self.assertEqual(cvar.get(), -1) |
| 119 | + |
| 120 | + |
| 121 | +class Test_UV_Context(_ContextBaseTests, tb.UVTestCase): |
| 122 | + |
| 123 | + @unittest.skipIf(PY37, 'requires Python <3.6') |
| 124 | + def test_context_arg(self): |
| 125 | + def cb(): |
| 126 | + pass |
| 127 | + |
| 128 | + with self.assertRaisesRegex(RuntimeError, 'requires Python 3.7'): |
| 129 | + self.loop.call_soon(cb, context=1) |
| 130 | + |
| 131 | + with self.assertRaisesRegex(RuntimeError, 'requires Python 3.7'): |
| 132 | + self.loop.call_soon_threadsafe(cb, context=1) |
| 133 | + |
| 134 | + with self.assertRaisesRegex(RuntimeError, 'requires Python 3.7'): |
| 135 | + self.loop.call_later(0.1, cb, context=1) |
| 136 | + |
| 137 | + |
| 138 | +class Test_AIO_Context(_ContextBaseTests, tb.AIOTestCase): |
| 139 | + pass |
0 commit comments