Skip to content
Myr

test_concurrency.py

This file

Path

tests/test_concurrency.py

119 lines · 3.9 KiB · Python

Last changed by

Drop 'fork' (#619)Tom Christiebefore adoptioneba29da

History of this file

Take it

1import asyncio23import pytest4import trio56from httpx import Timeout7from httpx.concurrency.asyncio import AsyncioBackend8from httpx.concurrency.base import lookup_backend9from httpx.concurrency.trio import TrioBackend10from httpx.config import SSLConfig11from tests.concurrency import get_cipher, run_concurrently121314async def read_response(stream, timeout: Timeout, should_contain: bytes) -> bytes:15    # stream.read() only gives us *up to* as much data as we ask for. In order to16    # cleanly close the stream, we must read until the end of the HTTP response.17    response = b""18    ended = False1920    for _ in range(5):  # Try read some (not too large) number of times...21        response += await stream.read(8192, timeout)22        # We know we're at the end of the response when we've received the body plus23        # the terminating CRLFs.24        if should_contain in response and response.endswith(b"\r\n\r\n"):25            ended = True26            break2728    assert ended29    return response303132async def test_start_tls_on_tcp_socket_stream(https_server, backend):33    backend = lookup_backend(backend)34    ctx = SSLConfig().load_ssl_context_no_verify()35    timeout = Timeout(5)3637    stream = await backend.open_tcp_stream(38        https_server.url.host, https_server.url.port, None, timeout39    )4041    try:42        assert stream.is_connection_dropped() is False43        assert get_cipher(backend, stream) is None4445        stream = await stream.start_tls(https_server.url.host, ctx, timeout)46        assert stream.is_connection_dropped() is False47        assert get_cipher(backend, stream) is not None4849        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)5051        response = await read_response(stream, timeout, should_contain=b"Hello, world")52        assert response.startswith(b"HTTP/1.1 200 OK\r\n")5354    finally:55        await stream.close()565758async def test_start_tls_on_uds_socket_stream(https_uds_server, backend):59    backend = lookup_backend(backend)60    ctx = SSLConfig().load_ssl_context_no_verify()61    timeout = Timeout(5)6263    stream = await backend.open_uds_stream(64        https_uds_server.config.uds, https_uds_server.url.host, None, timeout65    )6667    try:68        assert stream.is_connection_dropped() is False69        assert get_cipher(backend, stream) is None7071        stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)72        assert stream.is_connection_dropped() is False73        assert get_cipher(backend, stream) is not None7475        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)7677        response = await read_response(stream, timeout, should_contain=b"Hello, world")78        assert response.startswith(b"HTTP/1.1 200 OK\r\n")7980    finally:81        await stream.close()828384async def test_concurrent_read(server, backend):85    """86    Regression test for: https://github.com/encode/httpx/issues/52787    """88    backend = lookup_backend(backend)89    stream = await backend.open_tcp_stream(90        server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)91    )92    timeout = Timeout(5)93    try:94        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)95        await run_concurrently(96            backend, lambda: stream.read(10, timeout), lambda: stream.read(10, timeout)97        )98    finally:99        await stream.close()100101102def test_lookup_backend():103    assert isinstance(lookup_backend("asyncio"), AsyncioBackend)104    assert isinstance(lookup_backend("trio"), TrioBackend)105    assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)106107    async def get_backend_from_auto():108        auto_backend = lookup_backend("auto")109        return auto_backend.backend110111    loop = asyncio.get_event_loop()112    backend = loop.run_until_complete(get_backend_from_auto())113    assert isinstance(backend, AsyncioBackend)114115    backend = trio.run(get_backend_from_auto)116    assert isinstance(backend, TrioBackend)117118    with pytest.raises(Exception, match="unknownio"):119        lookup_backend("unknownio")