test_concurrency.py
This file
Path
tests/test_concurrency.py
161 lines · 5.0 KiB · Python
Last changed by
Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
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 back3Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
lines 4–5why, one step back4from httpx import Timeout5from httpx.concurrency.asyncio import AsyncioBackendAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 6why, one step back6from httpx.concurrency.trio import TrioBackendTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 7why, one step back7from httpx.config import SSLConfigDrop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
line 8why, one step back8from tests.concurrency import run_concurrently, sleepAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
lines 9–10why, one step back910Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 11–18why, one step back11def get_asyncio_cipher(stream):12 return stream.stream_writer.get_extra_info("cipher", default=None)131415def get_trio_cipher(stream):16 return stream.stream.cipher() if isinstance(stream.stream, trio.SSLStream) else None1718Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 19why, one step back19async def read_response(stream, timeout: Timeout, should_contain: bytes) -> bytes:Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 20–36why, one step back20 # stream.read() only gives us *up to* as much data as we ask for. In order to21 # cleanly close the stream, we must read until the end of the HTTP response.22 response = b""23 ended = False2425 for _ in range(5): # Try read some (not too large) number of times...26 response += await stream.read(8192, timeout)27 # We know we're at the end of the response when we've received the body plus28 # the terminating CRLFs.29 if should_contain in response and response.endswith(b"\r\n\r\n"):30 ended = True31 break3233 assert ended34 return response3536Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
lines 37–39why, one step back37@pytest.mark.parametrize(38 "backend, get_cipher",39 [Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 40–41why, one step back40 pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),41 pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 42why, one step back42 ],Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 43why, one step back43)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
line 44why, one step back44async def test_start_tls_on_tcp_socket_stream(https_server, backend, get_cipher):Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 45why, one step back45 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 46why, one step back46 timeout = Timeout(5)Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 47why, one step back47Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 48–50why, one step back48 stream = await backend.open_tcp_stream(49 https_server.url.host, https_server.url.port, None, timeout50 )Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 51why, one step back51Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 52–53why, one step back52 try:53 assert stream.is_connection_dropped() is FalseAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 54why, one step back54 assert get_cipher(stream) is NoneAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 55why, one step back55Make start_tls a method on streams & return a new stream (#484)Jamie Hewlandbefore adoption644e8fc
line 56why, one step back56 stream = await stream.start_tls(https_server.url.host, ctx, timeout)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 57why, one step back57 assert stream.is_connection_dropped() is FalseAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 58why, one step back58 assert get_cipher(stream) is not NoneServe test server in thread (#292)Florimond Mancabefore adoption994403b
line 59why, one step back59Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 60why, one step back60 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 61why, one step back61Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 62–63why, one step back62 response = await read_response(stream, timeout, should_contain=b"Hello, world")63 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 64why, one step back64Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 65–76why, one step back65 finally:66 await stream.close()676869@pytest.mark.parametrize(70 "backend, get_cipher",71 [72 pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),73 pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),74 ],75)76async def test_start_tls_on_uds_socket_stream(https_uds_server, backend, get_cipher):Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 77why, one step back77 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 78why, one step back78 timeout = Timeout(5)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 79–91why, one step back7980 stream = await backend.open_uds_stream(81 https_uds_server.config.uds, https_uds_server.url.host, None, timeout82 )8384 try:85 assert stream.is_connection_dropped() is False86 assert get_cipher(stream) is None8788 stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)89 assert stream.is_connection_dropped() is False90 assert get_cipher(stream) is not None91Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 92why, one step back92 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 93–95why, one step back9394 response = await read_response(stream, timeout, should_contain=b"Hello, world")95 assert response.startswith(b"HTTP/1.1 200 OK\r\n")Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 96why, one step back96Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 97–105why, one step back97 finally:98 await stream.close()99100101async def test_concurrent_read(server, backend):102 """103 Regression test for: https://github.com/encode/httpx/issues/527104 """105 stream = await backend.open_tcp_stream(TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 106why, one step back106 server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 107why, one step back107 )Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 108why, one step back108 timeout = Timeout(5)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 109why, one step back109 try:Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 110why, one step back110 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 111why, one step back111 await run_concurrently(Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 112why, one step back112 backend, lambda: stream.read(10, timeout), lambda: stream.read(10, timeout)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 113why, one step back113 )Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 114–115why, one step back114 finally:115 await stream.close()Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
lines 116–161why, one step back116117118async def test_fork(backend):119 ok_counter = 0120121 async def ok(delay: int) -> None:122 nonlocal ok_counter123 await sleep(backend, delay)124 ok_counter += 1125126 async def fail(message: str, delay: int) -> None:127 await sleep(backend, delay)128 raise RuntimeError(message)129130 await backend.fork(ok, [0], ok, [0])131 assert ok_counter == 2132133 with pytest.raises(RuntimeError, match="Oops"):134 await backend.fork(ok, [0], fail, ["Oops", 0.01])135136 assert ok_counter == 3137138 with pytest.raises(RuntimeError, match="Oops"):139 await backend.fork(ok, [0.01], fail, ["Oops", 0])140141 assert ok_counter == 3142143 with pytest.raises(RuntimeError, match="Oops"):144 await backend.fork(fail, ["Oops", 0.01], ok, [0])145146 assert ok_counter == 4147148 with pytest.raises(RuntimeError, match="Oops"):149 await backend.fork(fail, ["Oops", 0], ok, [0.01])150151 assert ok_counter == 4152153 with pytest.raises(RuntimeError, match="My bad"):154 await backend.fork(fail, ["My bad", 0], fail, ["Oops", 0.01])155156 with pytest.raises(RuntimeError, match="Oops"):157 await backend.fork(fail, ["My bad", 0.01], fail, ["Oops", 0])158159 # No 'match', since we can't know which will be raised first.160 with pytest.raises(RuntimeError):161 await backend.fork(fail, ["My bad", 0], fail, ["Oops", 0])