简体   繁体   English

从 pytest 夹具运行 asyncio 服务器

[英]Running an asyncio server from a pytest fixture

I'm trying to run a server using asyncio in a pytest fixture我正在尝试在 pytest 夹具中使用 asyncio 运行服务器

@pytest.fixture(autouse=True)
@pytest.mark.asyncio
async def start_endpoints(
    endpoint1: ServerEndpoint,
    endpoint2: ServerEndpoint
):
    pool = ThreadPoolExecutor(max_workers=2)
    loop = asyncio.get_running_loop()

    await loop.run_in_executor(pool, endpoint1.start)
    await loop.run_in_executor(pool, endpoint2.start)

The start method is like the following start方法如下

async def start(self):
        try:

            server = await asyncio.start_server(self.handle_req, self.addr, self.port)
            addr = server.sockets[0].getsockname()
            print(f'{self.name}: serving on {addr}')

            async with server:
                await server.serve_forever()

Whereas the test prints this error once it tries to open a connection with the server而一旦尝试打开与服务器的连接,测试就会打印此错误

self = <_WindowsSelectorEventLoop running=False closed=False debug=False>
fut = <Future finished exception=ConnectionRefusedError(10061, "Connect call failed ('127.0.0.1', 9000)")>
sock = <socket.socket [closed] fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>
address = ('127.0.0.1', 9000)

    def _sock_connect_cb(self, fut, sock, address):
        if fut.cancelled():
            return
    
        try:
            err = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
            if err != 0:
                # Jump to any except clause below.
>               raise OSError(err, f'Connect call failed {address}')
E               ConnectionRefusedError: [Errno 10061] Connect call failed ('127.0.0.1', 9000)

EDIT: The problem is that the event loop is closed right after so I tried to mark all my fixture with (scope="module") but now I get编辑:问题是事件循环在之后立即关闭,所以我尝试用(scope="module")标记我的所有装置,但现在我得到了

ScopeMismatch: You tried to access the 'function' scoped fixture 'event_loop' with a 'module' scoped request object, involved factories
test\e2e\test_peer.py:380:  def start_endpoints

EDIT2:编辑2:

So I added the event_loop fixture所以我添加了event_loop夹具

@pytest.fixture(scope="module")
def event_loop():
    loop = asyncio.get_event_loop()
    yield loop
    loop.close()

that should override the default loop for each fixture using @pytest.mark.asyncio .这应该使用@pytest.mark.asyncio覆盖每个夹具的默认循环。

@pytest.fixture(autouse=True, scope="module")
@pytest.mark.asyncio
async def start_endpoints(
    event_loop,
    endpoint1: ServerEndpoint,
    endpoint2: ServerEndpoint
):
    pool = ThreadPoolExecutor(max_workers=2)

    await event_loop.run_in_executor(pool, endpoint1.start)
    await event_loop.run_in_executor(pool, endpoint2.start)

By debugging inside my test the event_loop is equal to the loop that I'm storing inside the ServerEndpoint (that is asyncio.get_running_loop() ) but I'm still getting the ConnectionRefusedError通过在我的测试中调试event_loop等于我存储在ServerEndpoint的循环(即asyncio.get_running_loop() ),但我仍然收到ConnectionRefusedError

Probably it should works like this:可能它应该像这样工作:

async def handle(reader, writer):
    data = await reader.read(100)

    message = data.decode()
    print(f"SERVER: Received {message!r}")
    writer.write(data)
    await writer.drain()
    print(f"SERVER: Sent: {message!r}")

    writer.close()
    print("SERVER: Closed the connection")

async def start():
    server = await asyncio.start_server(handle, host, port)

    addr = server.sockets[0].getsockname()
    print(f'Server is running on {addr[0:2]}')

    async with server:
        await server.serve_forever()


async def _async_wait_for_server(event_loop, host, port):
    while True:
        a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            await event_loop.sock_connect(a_socket, (host, port))
            return
        except ConnectionRefusedError:
            await asyncio.sleep(0.01)

        finally:
            a_socket.close()

@pytest.fixture()
def server(event_loop, host, port):
    cancel_handle = asyncio.ensure_future(start(host, port), loop=event_loop)
    event_loop.run_until_complete(
        asyncio.wait_for(_async_wait_for_server(event_loop, host, port), 5.0)
    )

    try:
        yield
    finally:
        cancel_handle.cancel()

But I recommend you make functional tests in another way:但我建议您以另一种方式进行功能测试:

  • create docker image for server and image for tests为服务器创建 docker 镜像,为测试创建镜像
  • create docker-compose.yml file (use depends_on and healthcheck for tests container)创建 docker-compose.yml 文件(对测试容器使用depends_on 和healthcheck)
  • run tests after starting server启动服务器后运行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM