Skip to content
Myr

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 file

Take it

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 1why, one step back
1import pytest

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 2why, one step back
2import trio

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 3why, one step back
3

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

lines 4–5why, one step back
4from httpx import Timeout
5from httpx.concurrency.asyncio import AsyncioBackend

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 6why, one step back
6from httpx.concurrency.trio import TrioBackend

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 7why, one step back
7from httpx.config import SSLConfig

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

line 8why, one step back
8from tests.concurrency import run_concurrently, sleep

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

lines 9–10why, one step back
9
10

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 11–18why, one step back
11def 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 None1718

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 19why, one step back
19async def read_response(stream, timeout: Timeout, should_contain: bytes) -> bytes:

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 20–36why, one step back
20 # stream.read() only gives us *up to* as much data as we ask for. In order to
21 # 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 response3536

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

lines 37–39why, one step back
37@pytest.mark.parametrize(
38 "backend, get_cipher",39 [

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 40–41why, one step back
40 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 back
42 ],

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 43why, one step back
43)

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

line 44why, one step back
44async 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 back
45 ctx = SSLConfig().load_ssl_context_no_verify()

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 46why, one step back
46 timeout = Timeout(5)

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 47why, one step back
47

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 48–50why, one step back
48 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 back
51

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 52–53why, one step back
52 try:
53 assert stream.is_connection_dropped() is False

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 54why, one step back
54 assert get_cipher(stream) is None

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 55why, one step back
55

Make start_tls a method on streams & return a new stream (#484)Jamie Hewlandbefore adoption644e8fc

line 56why, one step back
56 stream = await stream.start_tls(https_server.url.host, ctx, timeout)

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

line 57why, one step back
57 assert stream.is_connection_dropped() is False

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 58why, one step back
58 assert get_cipher(stream) is not None

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

line 59why, one step back
59

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 60why, one step back
60 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 back
61

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 62–63why, one step back
62 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 back
64

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 65–76why, one step back
65 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 back
77 ctx = SSLConfig().load_ssl_context_no_verify()

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 78why, one step back
78 timeout = Timeout(5)

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 79–91why, one step back
79
80 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 None91

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 92why, one step back
92 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 back
93
94 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 back
96

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

lines 97–105why, one step back
97 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 back
106 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 back
107 )

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 108why, one step back
108 timeout = Timeout(5)

Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e

line 109why, one step back
109 try:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 110why, one step back
110 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 back
111 await run_concurrently(

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 112why, one step back
112 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 back
113 )

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 114–115why, one step back
114 finally:
115 await stream.close()

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 116–161why, one step back
116
117118async 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])