test_concurrency.py
This file
Path
tests/test_concurrency.py
112 lines · 3.6 KiB · Python
Last changed by
TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
History of this fileTake it
Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 1why, one step back1import pytestAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 2why, one step back2import trioAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 3why, one step back3TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 4why, one step back4from httpx import AsyncioBackend, SSLConfig, TimeoutAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 5why, one step back5from httpx.concurrency.trio import TrioBackendFix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 6why, one step back6from tests.concurrency import run_concurrentlyAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
lines 7–8why, one step back78Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 9–34why, one step back9def get_asyncio_cipher(stream):10 return stream.stream_writer.get_extra_info("cipher", default=None)111213def get_trio_cipher(stream):14 return stream.stream.cipher() if isinstance(stream.stream, trio.SSLStream) else None151617async def read_response(stream, timeout: float, should_contain: bytes) -> bytes:18 # stream.read() only gives us *up to* as much data as we ask for. In order to19 # cleanly close the stream, we must read until the end of the HTTP response.20 response = b""21 ended = False2223 for _ in range(5): # Try read some (not too large) number of times...24 response += await stream.read(8192, timeout)25 # We know we're at the end of the response when we've received the body plus26 # the terminating CRLFs.27 if should_contain in response and response.endswith(b"\r\n\r\n"):28 ended = True29 break3031 assert ended32 return response3334Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
lines 35–37why, one step back35@pytest.mark.parametrize(36 "backend, get_cipher",37 [Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 38–39why, one step back38 pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),39 pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 40why, one step back40 ],Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 41why, one step back41)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
line 42why, one step back42async def test_start_tls_on_tcp_socket_stream(https_server, backend, get_cipher):Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 43why, one step back43 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 44why, one step back44 timeout = Timeout(5)Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 45why, one step back45Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 46–48why, one step back46 stream = await backend.open_tcp_stream(47 https_server.url.host, https_server.url.port, None, timeout48 )Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 49why, one step back49Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 50–51why, one step back50 try:51 assert stream.is_connection_dropped() is FalseAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 52why, one step back52 assert get_cipher(stream) is NoneAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 53why, one step back53Make start_tls a method on streams & return a new stream (#484)Jamie Hewlandbefore adoption644e8fc
line 54why, one step back54 stream = await stream.start_tls(https_server.url.host, ctx, timeout)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 55why, one step back55 assert stream.is_connection_dropped() is FalseAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 56why, one step back56 assert get_cipher(stream) is not NoneServe test server in thread (#292)Florimond Mancabefore adoption994403b
lines 57–58why, one step back5758 await stream.write(b"GET / HTTP/1.1\r\n\r\n")asyncio: Wait for the stream to close when closing (#494)Jamie Hewlandbefore adoption88d73de
line 59why, one step back59Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 60–61why, one step back60 response = await read_response(stream, timeout, should_contain=b"Hello, world")61 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 62why, one step back62Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 63–74why, one step back63 finally:64 await stream.close()656667@pytest.mark.parametrize(68 "backend, get_cipher",69 [70 pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),71 pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),72 ],73)74async def test_start_tls_on_uds_socket_stream(https_uds_server, backend, get_cipher):Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 75why, one step back75 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 76why, one step back76 timeout = Timeout(5)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 77–93why, one step back7778 stream = await backend.open_uds_stream(79 https_uds_server.config.uds, https_uds_server.url.host, None, timeout80 )8182 try:83 assert stream.is_connection_dropped() is False84 assert get_cipher(stream) is None8586 stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)87 assert stream.is_connection_dropped() is False88 assert get_cipher(stream) is not None8990 await stream.write(b"GET / HTTP/1.1\r\n\r\n")9192 response = await read_response(stream, timeout, should_contain=b"Hello, world")93 assert response.startswith(b"HTTP/1.1 200 OK\r\n")Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 94why, one step back94Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 95–103why, one step back95 finally:96 await stream.close()979899async def test_concurrent_read(server, backend):100 """101 Regression test for: https://github.com/encode/httpx/issues/527102 """103 stream = await backend.open_tcp_stream(TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 104why, one step back104 server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 105–110why, one step back105 )106 try:107 await stream.write(b"GET / HTTP/1.1\r\n\r\n")108 await run_concurrently(109 backend, lambda: stream.read(10), lambda: stream.read(10)110 )Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 111–112why, one step back111 finally:112 await stream.close()