def test_identfy_task_on_loop(self): @coroutine def other_task(): return identify_future() fut = asyncio.ensure_future(other_task()) yield from fut self.assertNotEqual(fut.result(), identify_future())
def test_coroutine_local(self): ctx = AsyncLocal() @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) self.assertIn(identify_future(fut), ctx.__storage__) ctx.__release_local__(fut) self.assertNotIn(identify_future(fut), ctx.__storage__)
def test_coroutine_localstack(self): ctx = AsyncLocalStack() @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) self.assertIn(identify_future(fut), ctx._local.__storage__) ctx.__release_local__(fut) self.assertNotIn(identify_future(fut), ctx._local.__storage__)
async def test_make_task_with_ctx_factory(self): local = AsyncLocal() local_man = AsyncLocalManager(locals=[local]) ctxman = self.CtxManager(local) self.callback = False async def other_context(): return local.test async_with_context = local_man.make_task_with_ctx_factory(ctx=lambda: ctxman, loop=self.loop) fut = async_with_context(other_context()) await fut self.assertFalse(hasattr(local, 'test')) self.assertEqual(fut.result(), 45) self.assertNotIn(identify_future(fut), local.__storage__)
async def test_make_task_with_ctx_factory(self): local = AsyncLocal() local_man = AsyncLocalManager(locals=[local]) ctxman = self.CtxManager(local) self.callback = False async def other_context(): return local.test async_with_context = local_man.make_task_with_ctx_factory( ctx=lambda: ctxman, loop=self.loop) fut = async_with_context(other_context()) await fut self.assertFalse(hasattr(local, 'test')) self.assertEqual(fut.result(), 45) self.assertNotIn(identify_future(fut), local.__storage__)
def other_task(): return identify_future()
def test_identfy_future(self): fut = Future() self.assertEqual(id(fut), identify_future(fut))
def test_identfy_current_task(self): self.assertEqual(id(Task.current_task()), identify_future())