Skip to content
Myr

test_concurrency.py

This file

Path

tests/test_concurrency.py

119 lines · 3.9 KiB · Python

Last changed by

Drop 'fork' (#619)Tom Christiebefore adoptioneba29da

History of this file

Take it

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

lines 1–2why, one step back
1import asyncio
2

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

line 3why, one step back
3import pytest

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

line 4why, one step back
4import trio

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

line 5why, one step back
5

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

lines 6–7why, one step back
6from httpx import Timeout
7from httpx.concurrency.asyncio import AsyncioBackend

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 8why, one step back
8from httpx.concurrency.base import lookup_backend

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

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

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

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

Drop 'fork' (#619)Tom Christiebefore adoptioneba29da

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

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

lines 12–13why, one step back
12
13

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

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

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

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

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

lines 32–33why, one step back
32async def test_start_tls_on_tcp_socket_stream(https_server, backend):
33 backend = lookup_backend(backend)

Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1

line 34why, one step back
34 ctx = SSLConfig().load_ssl_context_no_verify()

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

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

line 36why, one step back
36

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 37–39why, one step back
37 stream = await backend.open_tcp_stream(
38 https_server.url.host, https_server.url.port, None, timeout39 )

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

line 40why, one step back
40

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

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

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

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

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

line 44why, one step back
44

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

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

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

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

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

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

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

line 48why, one step back
48

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

line 49why, one step back
49 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 50why, one step back
50

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 51–52why, one step back
51 response = await read_response(stream, timeout, should_contain=b"Hello, world")
52 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 53why, one step back
53

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 54–57why, one step back
54 finally:
55 await stream.close()5657

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

lines 58–59why, one step back
58async def test_start_tls_on_uds_socket_stream(https_uds_server, backend):
59 backend = lookup_backend(backend)

Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1

line 60why, one step back
60 ctx = SSLConfig().load_ssl_context_no_verify()

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

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

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 62–68why, one step back
62
63 stream = await backend.open_uds_stream(64 https_uds_server.config.uds, https_uds_server.url.host, None, timeout65 )6667 try:68 assert stream.is_connection_dropped() is False

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

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

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 70–72why, one step back
70
71 stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)72 assert stream.is_connection_dropped() is False

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

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

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

line 74why, one step back
74

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

line 75why, one step back
75 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)

Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35

lines 76–78why, one step back
76
77 response = await read_response(stream, timeout, should_contain=b"Hello, world")78 assert response.startswith(b"HTTP/1.1 200 OK\r\n")

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

line 79why, one step back
79

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

lines 80–87why, one step back
80 finally:
81 await stream.close()828384async def test_concurrent_read(server, backend):85 """86 Regression test for: https://github.com/encode/httpx/issues/52787 """

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 88why, one step back
88 backend = lookup_backend(backend)

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

line 89why, one step back
89 stream = await backend.open_tcp_stream(

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 90why, one step back
90 server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)

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

line 91why, one step back
91 )

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

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

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

line 93why, one step back
93 try:

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

line 94why, one step back
94 await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)

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

line 95why, one step back
95 await run_concurrently(

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

line 96why, one step back
96 backend, lambda: stream.read(10, timeout), lambda: stream.read(10, timeout)

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

line 97why, one step back
97 )

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

lines 98–99why, one step back
98 finally:
99 await stream.close()

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

lines 100–119why, one step back
100
101102def test_lookup_backend():103 assert isinstance(lookup_backend("asyncio"), AsyncioBackend)104 assert isinstance(lookup_backend("trio"), TrioBackend)105 assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)106107 async def get_backend_from_auto():108 auto_backend = lookup_backend("auto")109 return auto_backend.backend110111 loop = asyncio.get_event_loop()112 backend = loop.run_until_complete(get_backend_from_auto())113 assert isinstance(backend, AsyncioBackend)114115 backend = trio.run(get_backend_from_auto)116 assert isinstance(backend, TrioBackend)117118 with pytest.raises(Exception, match="unknownio"):119 lookup_backend("unknownio")