test_concurrency.py
This file
Path
tests/test_concurrency.py
122 lines · 3.9 KiB · Python
Last changed by
Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da
History of this fileTake it
1import asyncio23import pytest4import trio56from httpx import Timeout7from httpx.backends.asyncio import AsyncioBackend8from httpx.backends.base import lookup_backend9from httpx.backends.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 response303132@pytest.mark.usefixtures("async_environment")33async def test_start_tls_on_tcp_socket_stream(https_server):34 backend = lookup_backend()35 ctx = SSLConfig().load_ssl_context_no_verify()36 timeout = Timeout(5)3738 stream = await backend.open_tcp_stream(39 https_server.url.host, https_server.url.port, None, timeout40 )4142 try:43 assert stream.is_connection_dropped() is False44 assert get_cipher(stream) is None4546 stream = await stream.start_tls(https_server.url.host, ctx, timeout)47 assert stream.is_connection_dropped() is False48 assert get_cipher(stream) is not None4950 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)5152 response = await read_response(stream, timeout, should_contain=b"Hello, world")53 assert response.startswith(b"HTTP/1.1 200 OK\r\n")5455 finally:56 await stream.close()575859@pytest.mark.usefixtures("async_environment")60async def test_start_tls_on_uds_socket_stream(https_uds_server):61 backend = lookup_backend()62 ctx = SSLConfig().load_ssl_context_no_verify()63 timeout = Timeout(5)6465 stream = await backend.open_uds_stream(66 https_uds_server.config.uds, https_uds_server.url.host, None, timeout67 )6869 try:70 assert stream.is_connection_dropped() is False71 assert get_cipher(stream) is None7273 stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)74 assert stream.is_connection_dropped() is False75 assert get_cipher(stream) is not None7677 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)7879 response = await read_response(stream, timeout, should_contain=b"Hello, world")80 assert response.startswith(b"HTTP/1.1 200 OK\r\n")8182 finally:83 await stream.close()848586@pytest.mark.usefixtures("async_environment")87async def test_concurrent_read(server):88 """89 Regression test for: https://github.com/encode/httpx/issues/52790 """91 backend = lookup_backend()92 stream = await backend.open_tcp_stream(93 server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)94 )95 timeout = Timeout(5)96 try:97 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)98 await run_concurrently(99 lambda: stream.read(10, timeout), lambda: stream.read(10, timeout)100 )101 finally:102 await stream.close()103104105def test_lookup_backend():106 assert isinstance(lookup_backend("asyncio"), AsyncioBackend)107 assert isinstance(lookup_backend("trio"), TrioBackend)108 assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)109110 async def get_backend_from_auto():111 auto_backend = lookup_backend("auto")112 return auto_backend.backend113114 loop = asyncio.get_event_loop()115 backend = loop.run_until_complete(get_backend_from_auto())116 assert isinstance(backend, AsyncioBackend)117118 backend = trio.run(get_backend_from_auto)119 assert isinstance(backend, TrioBackend)120121 with pytest.raises(Exception, match="unknownio"):122 lookup_backend("unknownio")