# tests/test_coros.py import asyncio def test_coro(): loop = asyncio.get_event_loop() @asyncio.coroutine def do_test(): yield from asyncio.sleep(0.1, loop=loop) assert 0 # onoes! loop.run_until_complete(do_test())
# tests/test_coros.py @asyncio.coroutine def test_coro(loop): yield from asyncio.sleep(0.1, loop=loop) assert 0
# tests/conftest.py import asyncio import pytest @pytest.yield_fixture def loop(): # loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) yield loop # loop.close() # tests/test_coros.py def test_coro(loop): @asyncio.coroutine def do_test(): yield from asyncio.sleep(0.1, loop=loop) assert 0 # onoes! loop.run_until_complete(do_test())
# tests/conftest.py def pytest_pycollect_makeitem(collector, name, obj): """ asyncio , .” if collector.funcnamefilter(name) and asyncio.iscoroutinefunction(obj): # # , # , "pytest.mark.parametrize()". return list(collector._genfunctions(name, obj)) # else: # None, pytest' “obj” def pytest_pyfunc_call(pyfuncitem): """ ``pyfuncitem.obj`` - asyncio , """ testfunction = pyfuncitem.obj if not asyncio.iscoroutinefunction(testfunction): # None, . Pytest # return # : funcargs = pyfuncitem.funcargs # argnames = pyfuncitem._fixtureinfo.argnames # testargs = {arg: funcargs[arg] for arg in argnames} # - ( !) coro = testfunction(**testargs) # loop = testargs['loop'] if loop in testargs else asyncio.get_event_loop() loop.run_until_complete(coro) return True # pytest',
import asyncio import time import pytest @pytest.mark.asyncio async def test_coro(event_loop): before = time.monotonic() await asyncio.sleep(0.1, loop=event_loop) after = time.monotonic() assert after - before >= 0.1
import asyncio import time def test_coro(): loop = asyncio.new_event_loop() try: asyncio.set_event_loop(loop) before = time.monotonic() loop.run_until_complete(asyncio.sleep(0.1, loop=loop)) after = time.monotonic() assert after - before >= 0.1 finally: loop.close()
import aiomas async def handle_client(channel): """ """ req = await channel.recv() print(req.content) await req.reply('cya') await channel.close() async def client(): """ : """ channel = await aiomas.channel.open_connection(('localhost', 5555)) rep = await channel.send('ohai') print(rep) await channel.close() server = aiomas.run(aiomas.channel.start_server( ('localhost', 5555), handle_client)) aiomas.run(client()) server.close() aiomas.run(server.wait_closed())
import tempfile import py import pytest class Context: def __init__(self, loop, addr): self.loop = loop self.addr = addr @pytest.fixture(params=['tcp', 'unix']) def ctx(request, event_loop, unused_tcp_port, short_tmpdir): """ TCP Unix.""" addr_type = request.param if addr_type == 'tcp': addr = ('127.0.0.1', unused_tcp_port) elif addr_type == 'unix': addr = short_tmpdir.join('sock').strpath else: raise RuntimeError('Unknown addr type: %s' % addr_type) ctx = Context(event_loop, addr) return ctx @pytest.yield_fixture() def short_tmpdir(): """ Unix. , pytest' tmpdir, """ with tempfile.TemporaryDirectory() as tdir: yield py.path.local(tdir)
import aiomas @pytest.mark.asyncio async def test_channel(ctx): results = [] async def handle_client(channel): req = await channel.recv() results.append(req.content) await req.reply('cya') await channel.close() server = await aiomas.channel.start_server(ctx.addr, handle_client) try: channel = await aiomas.channel.open_connection(ctx.addr) rep = await channel.send('ohai') results.append(rep) await channel.close() finally: server.close() await server.wait_closed() assert results == ['ohai', 'cya']
@pytest.fixture(params=['tcp', 'unix']) def ctx(request, event_loop): """ TCP Unix.""" addr_type = request.param if addr_type == 'tcp': port = request.getfuncargvalue('unused_tcp_port') addr = ('127.0.0.1', port) elif addr_type == 'unix': tmpdir = request.getfuncargvalue('short_tmpdir') addr = tmpdir.join('sock').strpath else: raise RuntimeError('Unknown addr type: %s' % addr_type) ctx = Context(event_loop, addr) return ctx
import asyncio import tempfile import py import pytest class Context: def __init__(self, loop, addr): self.loop = loop self.addr = addr self.server = None async def connect(self, **kwargs): """ "self.addr".""" return (await aiomas.channel.open_connection( self.addr, loop=self.loop, **kwargs)) async def start_server(self, handle_client, **kwargs): """ *handle_client*, "self.addr".""" self.server = await aiomas.channel.start_server( self.addr, handle_client, loop=self.loop, **kwargs) async def start_server_and_connect(self, handle_client, server_kwargs=None, client_kwargs=None): """ :: await ctx.start_server(...) channel = await ctx.connect()" """ if server_kwargs is None: server_kwargs = {} if client_kwargs is None: client_kwargs = {} await self.start_server(handle_client, **server_kwargs) return (await self.connect(**client_kwargs)) async def close_server(self): """ .""" if self.server is not None: server, self.server = self.server, None server.close() await server.wait_closed() @pytest.yield_fixture(params=['tcp', 'unix']) def ctx(request, event_loop): """ TCP Unix.""" addr_type = request.param if addr_type == 'tcp': port = request.getfuncargvalue('unused_tcp_port') addr = ('127.0.0.1', port) elif addr_type == 'unix': tmpdir = request.getfuncargvalue('short_tmpdir') addr = tmpdir.join('sock').strpath else: raise RuntimeError('Unknown addr type: %s' % addr_type) ctx = Context(event_loop, addr) yield ctx # : aiomas.run(ctx.close_server()) aiomas.run(asyncio.gather(*asyncio.Task.all_tasks(event_loop), return_exceptions=True))
import aiomas @pytest.mark.asyncio async def test_channel(ctx): results = [] async def handle_client(channel): req = await channel.recv() results.append(req.content) await req.reply('cya') await channel.close() channel = await ctx.start_server_and_connect(handle_client) rep = await channel.send('ohai') results.append(rep) await channel.close() assert results == ['ohai', 'cya']
Source: https://habr.com/ru/post/337108/
All Articles