Skip to content
Myr

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 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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 4why, one step back
4from httpx import AsyncioBackend, SSLConfig, Timeout

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

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

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

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

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

lines 7–8why, one step back
7
8

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 9–34why, one step back
9def 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 response3334

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

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

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

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

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

line 41why, one step back
41)

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

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

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

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

line 45why, one step back
45

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

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

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

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

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

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

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

line 53why, one step back
53

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

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

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

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

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

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

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

lines 57–58why, one step back
57
58 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 back
59

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

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

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 63–74why, one step back
63 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 back
75 ctx = SSLConfig().load_ssl_context_no_verify()

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 77–93why, one step back
77
78 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 back
94

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

lines 95–103why, one step back
95 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 back
104 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 back
105 )
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 back
111 finally:
112 await stream.close()