def test_coroutine_context_local(self): ctx = Local() patch_local(ctx) ctxman = self.CtxManager(ctx) test_coroutine = partial(keep_context_factory, ctx=lambda: ctxman) @test_coroutine async def other_context(): return ctx.test fut = asyncio.ensure_future(other_context()) yield from fut self.assertFalse(hasattr(ctx, 'test')) self.assertEqual(fut.result(), 45)
def test_coroutine_localstack(self): ctx = LocalStack() patch_local(ctx) @coroutine def other_context(): ctx.push(45) return ctx.top ctx.push(40) fut = asyncio.ensure_future(other_context()) yield from fut self.assertEqual(ctx.top, 40) self.assertNotEqual(ctx.top, fut.result()) self.assertEqual(fut.result(), 45)
def test_coroutine_local(self): ctx = Local() patch_local(ctx) @coroutine def other_context(): ctx.test = 45 return ctx.test ctx.test = 40 fut = asyncio.ensure_future(other_context()) yield from fut self.assertEqual(ctx.test, 40) self.assertNotEqual(ctx.test, fut.result()) self.assertEqual(fut.result(), 45)
def test_generator_context_local(self): ctx = Local() patch_local(ctx) ctxman = self.CtxManager(ctx) test_coroutine = partial(context_coroutine, ctx=lambda: ctxman) @coroutine def gen(): yield return ctx.test @test_coroutine def other_context(): return gen() fut = asyncio.ensure_future(other_context()) yield from fut self.assertFalse(hasattr(ctx, 'test')) self.assertEqual(fut.result(), 45)
def test_send_context_local(self): ctx = Local() patch_local(ctx) ctxman = self.CtxManager(ctx) test_coroutine = partial(context_coroutine, ctx=lambda: ctxman) @test_coroutine def gen(): return ctx.test @test_coroutine def other_context(): res = yield from gen() return res gene = other_context() try: gene.send(None) except StopIteration as ex: self.assertEqual(ex.value, 45)