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
The walk stopped after 200 commits: lines older than e1da6b9 point at it.
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 1–2why, one step back1import asyncio2Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
line 3why, one step back3import pytestAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 4why, one step back4import trioAdd tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
line 5why, one step back5Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 6why, one step back6from httpx import TimeoutRename `concurrency` directory to `backends` (#662)Andrés Álvarezbefore adoptione34df7a
lines 7–9why, one step back7from httpx.backends.asyncio import AsyncioBackend8from httpx.backends.base import lookup_backend9from httpx.backends.trio import TrioBackendTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 10why, one step back10from httpx.config import SSLConfigDrop 'fork' (#619)Tom Christiebefore adoptioneba29da
line 11why, one step back11from tests.concurrency import get_cipher, run_concurrentlyAdd tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
lines 12–13why, one step back1213Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 14why, one step back14async def read_response(stream, timeout: Timeout, should_contain: bytes) -> bytes:Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 15–31why, one step back15 # 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 response3031Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 32–34why, one step back32@pytest.mark.usefixtures("async_environment")33async def test_start_tls_on_tcp_socket_stream(https_server):34 backend = lookup_backend()Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 35why, one step back35 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 36why, one step back36 timeout = Timeout(5)Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
line 37why, one step back37Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 38–40why, one step back38 stream = await backend.open_tcp_stream(39 https_server.url.host, https_server.url.port, None, timeout40 )Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
lines 41–43why, one step back4142 try:43 assert stream.is_connection_dropped() is FalseDrop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da
line 44why, one step back44 assert get_cipher(stream) is NoneAdd tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
line 45why, one step back45Make start_tls a method on streams & return a new stream (#484)Jamie Hewlandbefore adoption644e8fc
line 46why, one step back46 stream = await stream.start_tls(https_server.url.host, ctx, timeout)Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
line 47why, one step back47 assert stream.is_connection_dropped() is FalseDrop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da
line 48why, one step back48 assert get_cipher(stream) is not NoneAdd tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
line 49why, one step back49Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 50why, one step back50 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)asyncio: Wait for the stream to close when closing (#494)Jamie Hewlandbefore adoption88d73de
line 51why, one step back51Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 52–53why, one step back52 response = await read_response(stream, timeout, should_contain=b"Hello, world")53 assert response.startswith(b"HTTP/1.1 200 OK\r\n")asyncio: Wait for the stream to close when closing (#494)Jamie Hewlandbefore adoption88d73de
line 54why, one step back54Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 55–58why, one step back55 finally:56 await stream.close()5758Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 59–61why, one step back59@pytest.mark.usefixtures("async_environment")60async def test_start_tls_on_uds_socket_stream(https_uds_server):61 backend = lookup_backend()Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 62why, one step back62 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 63why, one step back63 timeout = Timeout(5)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 64–70why, one step back6465 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 FalseDrop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da
line 71why, one step back71 assert get_cipher(stream) is NoneRefactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 72–74why, one step back7273 stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)74 assert stream.is_connection_dropped() is FalseDrop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da
line 75why, one step back75 assert get_cipher(stream) is not NoneRefactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
line 76why, one step back76Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 77why, one step back77 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 78–80why, one step back7879 response = await read_response(stream, timeout, should_contain=b"Hello, world")80 assert response.startswith(b"HTTP/1.1 200 OK\r\n")Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
line 81why, one step back81Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 82–85why, one step back82 finally:83 await stream.close()8485Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 86–87why, one step back86@pytest.mark.usefixtures("async_environment")87async def test_concurrent_read(server):Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 88–90why, one step back88 """89 Regression test for: https://github.com/encode/httpx/issues/52790 """Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 91why, one step back91 backend = lookup_backend()Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 92why, one step back92 stream = await backend.open_tcp_stream(TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 93why, one step back93 server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 94why, one step back94 )Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 95why, one step back95 timeout = Timeout(5)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 96why, one step back96 try:Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 97why, one step back97 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 98why, one step back98 await run_concurrently(Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da
line 99why, one step back99 lambda: stream.read(10, timeout), lambda: stream.read(10, timeout)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 100why, one step back100 )Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9
lines 101–102why, one step back101 finally:102 await stream.close()Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 103–122why, one step back103104105def 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")