test_concurrency.py
This file
Path
tests/test_concurrency.py
119 lines · 3.9 KiB · Python
Last changed by
Rename `concurrency` directory to `backends` (#662)Andrés Álvarezbefore adoptione34df7a
History of this fileTake it
The walk stopped after 200 commits: lines older than 994403b point at it.
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 1–2why, one step back1import asyncio2Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 3why, one step back3import pytestAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 4why, one step back4import trioServe test server in thread (#292)Florimond Mancabefore adoption994403b
line 5why, one step back5Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 6why, one step back6from httpx import TimeoutRename `concurrency` directory to `backends` (#662)Andrés Álvarezbefore adoptione34df7a
lines 7–9why, one step back7from httpx.backends.asyncio import AsyncioBackend8from httpx.backends.base import lookup_backend9from httpx.backends.trio import TrioBackendTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 10why, one step back10from httpx.config import SSLConfigDrop 'fork' (#619)Tom Christiebefore adoptioneba29da
line 11why, one step back11from tests.concurrency import get_cipher, run_concurrentlyServe test server in thread (#292)Florimond Mancabefore adoption994403b
lines 12–13why, one step back1213Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 14why, one step back14async def read_response(stream, timeout: Timeout, should_contain: bytes) -> bytes:Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 15–31why, one step back15 # stream.read() only gives us *up to* as much data as we ask for. In order to16 # 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 response3031Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 32–33why, one step back32async 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 back34 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 35why, one step back35 timeout = Timeout(5)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 36why, one step back36Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 37–39why, one step back37 stream = await backend.open_tcp_stream(38 https_server.url.host, https_server.url.port, None, timeout39 )Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 40–42why, one step back4041 try:42 assert stream.is_connection_dropped() is FalseRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 43why, one step back43 assert get_cipher(backend, stream) is NoneServe test server in thread (#292)Florimond Mancabefore adoption994403b
line 44why, one step back44Make start_tls a method on streams & return a new stream (#484)Jamie Hewlandbefore adoption644e8fc
line 45why, one step back45 stream = await stream.start_tls(https_server.url.host, ctx, timeout)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 46why, one step back46 assert stream.is_connection_dropped() is FalseRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 47why, one step back47 assert get_cipher(backend, stream) is not NoneServe test server in thread (#292)Florimond Mancabefore adoption994403b
line 48why, one step back48Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 49why, one step back49 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 back50Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 51–52why, one step back51 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 back53Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 54–57why, one step back54 finally:55 await stream.close()5657Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 58–59why, one step back58async 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 back60 ctx = SSLConfig().load_ssl_context_no_verify()TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 61why, one step back61 timeout = Timeout(5)Refactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 62–68why, one step back6263 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 FalseRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 69why, one step back69 assert get_cipher(backend, stream) is NoneRefactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
lines 70–72why, one step back7071 stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)72 assert stream.is_connection_dropped() is FalseRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 73why, one step back73 assert get_cipher(backend, stream) is not NoneRefactor start_tls tests (#567)Florimond Mancabefore adoption3218c35
line 74why, one step back74Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 75why, one step back75 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 back7677 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 back79Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
lines 80–87why, one step back80 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 back88 backend = lookup_backend(backend)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 89why, one step back89 stream = await backend.open_tcp_stream(TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
line 90why, one step back90 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 back91 )Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 92why, one step back92 timeout = Timeout(5)Fix race condition on stream.read (#535)Florimond Mancabefore adoptiona05ba2e
line 93why, one step back93 try:Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 94why, one step back94 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 back95 await run_concurrently(Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0
line 96why, one step back96 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 back97 )Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 98–99why, one step back98 finally:99 await stream.close()Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 100–119why, one step back100101102def 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")