Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connect timeouts for anyio backend #236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions httpcore/_backends/anyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ async def open_tcp_stream(
connect_timeout = timeout.get("connect")
unicode_host = hostname.decode("utf-8")
exc_map = {
OSError: ConnectError,
TimeoutError: ConnectTimeout,
OSError: ConnectError,
BrokenResourceError: ConnectError,
}

Expand Down Expand Up @@ -168,8 +168,8 @@ async def open_uds_stream(
connect_timeout = timeout.get("connect")
unicode_host = hostname.decode("utf-8")
exc_map = {
OSError: ConnectError,
TimeoutError: ConnectTimeout,
OSError: ConnectError,
BrokenResourceError: ConnectError,
}

Expand Down
39 changes: 39 additions & 0 deletions tests/async_tests/test_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import platform
import sys

import pytest

Expand Down Expand Up @@ -466,3 +467,41 @@ async def test_cannot_connect_uds(backend: str) -> None:
async with httpcore.AsyncConnectionPool(backend=backend, uds=uds) as http:
with pytest.raises(httpcore.ConnectError):
await http.arequest(method, url)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)
@pytest.mark.anyio
async def test_connection_timeout_tcp(backend: str, server: Server) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside inside AnyIOBackend.open_tcp_stream
method = b"GET"
url = (b"https", *server.netloc, b"/")
headers = [server.host_header]
ext = {"timeout": {"connect": 0.1}}

async with httpcore.AsyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
await http.arequest(method, url, headers, ext=ext)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)
@pytest.mark.anyio
async def test_connection_timeout_uds(
backend: str, uds_server: Server, uds: str
) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside AnyIOBackend.open_uds_stream
method = b"GET"
url = (b"https", b"localhost", None, b"/")
headers = [(b"host", b"localhost")]
ext = {"timeout": {"connect": 0.1}}

async with httpcore.AsyncConnectionPool(uds=uds, backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
await http.arequest(method, url, headers, ext=ext)
39 changes: 39 additions & 0 deletions tests/sync_tests/test_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import platform
import sys

import pytest

Expand Down Expand Up @@ -466,3 +467,41 @@ def test_cannot_connect_uds(backend: str) -> None:
with httpcore.SyncConnectionPool(backend=backend, uds=uds) as http:
with pytest.raises(httpcore.ConnectError):
http.request(method, url)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)

def test_connection_timeout_tcp(backend: str, server: Server) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside inside AnyIOBackend.open_tcp_stream
method = b"GET"
url = (b"https", *server.netloc, b"/")
headers = [server.host_header]
ext = {"timeout": {"connect": 0.1}}

with httpcore.SyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
http.request(method, url, headers, ext=ext)


@pytest.mark.skipif(
sys.version_info[:2] < (3, 7),
reason="Hypercorn doesn't support python < 3.7 (this test is local-only)",
)

def test_connection_timeout_uds(
backend: str, uds_server: Server, uds: str
) -> None:
# we try to access http server using https. It caused some SSL timeouts
# in TLSStream.wrap inside AnyIOBackend.open_uds_stream
method = b"GET"
url = (b"https", b"localhost", None, b"/")
headers = [(b"host", b"localhost")]
ext = {"timeout": {"connect": 0.1}}

with httpcore.SyncConnectionPool(uds=uds, backend=backend) as http:
with pytest.raises(httpcore.ConnectTimeout):
http.request(method, url, headers, ext=ext)