-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix import of HTTP/1.1 connection causing thread-safety bug on 3.9 (#237
- Loading branch information
1 parent
76f6e91
commit 0fce6d3
Showing
3 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import concurrent.futures | ||
|
||
import pytest | ||
|
||
import httpcore | ||
|
||
from .utils import Server | ||
|
||
|
||
def read_body(stream: httpcore.SyncByteStream) -> bytes: | ||
try: | ||
return b"".join(chunk for chunk in stream) | ||
finally: | ||
stream.close() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"http2", [pytest.param(False, id="h11"), pytest.param(True, id="h2")] | ||
) | ||
def test_threadsafe_basic(server: Server, http2: bool) -> None: | ||
""" | ||
The sync connection pool can be used to perform requests concurrently using | ||
threads. | ||
Also a regression test for: https://github.com/encode/httpx/issues/1393 | ||
""" | ||
with httpcore.SyncConnectionPool(http2=http2) as http: | ||
|
||
def request(http: httpcore.SyncHTTPTransport) -> int: | ||
method = b"GET" | ||
url = (b"http", *server.netloc, b"/") | ||
headers = [server.host_header] | ||
status_code, headers, stream, ext = http.request(method, url, headers) | ||
read_body(stream) | ||
return status_code | ||
|
||
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: | ||
futures = [executor.submit(request, http) for _ in range(10)] | ||
num_results = 0 | ||
|
||
for future in concurrent.futures.as_completed(futures): | ||
status_code = future.result() | ||
assert status_code == 200 | ||
num_results += 1 | ||
|
||
assert num_results == 10 |