def thread(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) c = 0 @asyncio.coroutine def coro(): nonlocal c yield from asyncio.sleep(SLEEP) c = 1 assert len(ArmorMeta.objects) == 0 task = armor(coro()) assert len(ArmorMeta.objects) == 1 task.cancel() armor.close() loop.run_until_complete(armor.wait_closed()) assert len(ArmorMeta.objects) == 1 del task assert len(ArmorMeta.objects) == 0 return c
def test_deco_cls_kwargs_loop(loop): asyncio.set_event_loop(loop) @armor(cls=True, kwargs=True, loop='loop') @asyncio.coroutine def coro(): pass with pytest.raises(AssertionError): yield from coro() armor.close() yield from armor.wait_closed()
def test_shield_cls_kwargs_loop(loop): asyncio.set_event_loop(loop) @asyncio.coroutine def coro(): pass obj = coro() with pytest.raises(AssertionError): yield from armor(obj, cls=True, kwargs=True) yield from obj armor.close() yield from armor.wait_closed()
def test_kwargs_loop(loop): c = 0 @armor(kwargs=True, loop='_loop') @asyncio.coroutine def coro(*, _loop): nonlocal c yield from asyncio.sleep(SLEEP, loop=_loop) c = 1 task = ensure_future(coro(_loop=loop), loop=loop) task.cancel() armor.close() yield from armor.wait_closed(loop=loop) assert c == 1
def test_explicit_loop(loop): c = 0 @armor(loop=loop) @asyncio.coroutine def coro(): nonlocal c yield from asyncio.sleep(SLEEP, loop=loop) c = 1 task = ensure_future(coro(), loop=loop) task.cancel() armor.close() yield from armor.wait_closed(loop=loop) assert c == 1
def test_deco_reload(loop): asyncio.set_event_loop(loop) @armor @asyncio.coroutine def coro(): pass armor.close() yield from armor.wait_closed() armor.reload() yield from coro() armor.close() yield from armor.wait_closed()
def test_deco_closed(loop): asyncio.set_event_loop(loop) @armor @asyncio.coroutine def coro(): pass armor.close() with pytest.raises(RuntimeError): yield from coro() yield from armor.wait_closed() with pytest.raises(RuntimeError): yield from coro()
def test_shield(loop): asyncio.set_event_loop(loop) c = 0 @asyncio.coroutine def coro(): nonlocal c yield from asyncio.sleep(SLEEP) c = 1 task = armor(coro()) task.cancel() armor.close() yield from armor.wait_closed() assert c == 1
def test_deco_partial(loop): asyncio.set_event_loop(loop) c = 0 @armor @asyncio.coroutine def coro(): nonlocal c yield from asyncio.sleep(SLEEP) c = 1 task = ensure_future(partial(coro)()) task.cancel() armor.close() yield from armor.wait_closed() assert c == 1
def test_cls_loop(loop): c = 0 class Obj: def __init__(self, *, loop): self._loop = loop @armor(cls=True, loop='_loop') @asyncio.coroutine def coro(self): nonlocal c yield from asyncio.sleep(SLEEP, loop=self._loop) c = 1 task = ensure_future(Obj(loop=loop).coro(), loop=loop) task.cancel() armor.close() yield from armor.wait_closed(loop=loop) assert c == 1
def test_shield_inner(loop): asyncio.set_event_loop(loop) c = 0 @asyncio.coroutine def coro(): nonlocal c yield from asyncio.sleep(SLEEP) c = 1 @asyncio.coroutine def inner(): yield from armor(coro()) task = ensure_future(inner()) yield from asyncio.sleep(SLEEP / 2) task.cancel() armor.close() yield from armor.wait_closed() assert c == 1
def test_shield_many(loop): asyncio.set_event_loop(loop) c = 0 n = 100 @asyncio.coroutine def coro(): nonlocal c yield from asyncio.sleep(SLEEP) c += 1 coros = [] for _ in range(n): coros.append(armor(coro())) task = asyncio.gather(*coros) task.cancel() armor.close() yield from armor.wait_closed() assert c == n
async def close_armor(app): armor.close() await armor.wait_closed()