Skip to content

Commit

Permalink
Reworked app(...) fixture. Now it accepts routing. Put slow_hello_app…
Browse files Browse the repository at this point in the history
…(...) and hello_app(...) inside it.
  • Loading branch information
cdeler committed Nov 25, 2020
1 parent 41a6f25 commit 4fe216e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 65 deletions.
21 changes: 13 additions & 8 deletions tests/async_tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,28 +473,33 @@ async def test_cannot_connect_uds(backend: str) -> None:
sys.version_info[:2] < (3, 7), reason="Hypercorn doesn't support python < 3.7"
)
@pytest.mark.anyio
async def test_connection_timeout_tcp(backend: str, slow_tcp_server: Server) -> None:
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", *slow_tcp_server.netloc, b"/delay/5")
headers = [slow_tcp_server.host_header]
url = (b"https", *server.netloc, b"/delay/1")
headers = [server.host_header]
ext = {"timeout": {"connect": 0.1}}

async with httpcore.AsyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.TimeoutException):
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"
)
@pytest.mark.anyio
async def test_connection_timeout_uds(backend: str, slow_uds_server: Server) -> None:
uds = slow_uds_server.uds
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"/delay/5")
url = (b"https", b"localhost", None, b"/delay/1")
headers = [(b"host", b"localhost")]
ext = {"timeout": {"connect": 0.1}}

async with httpcore.AsyncConnectionPool(uds=uds, backend=backend) as http:
with pytest.raises(httpcore.TimeoutException):
with pytest.raises(httpcore.ConnectTimeout):
await http.arequest(method, url, headers, ext=ext)
62 changes: 13 additions & 49 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import typing

import pytest
import trio
import trustme

from httpcore._types import URL

from .utils import HypercornServer, LiveServer, Server, http_proxy_server
from .utils import (
HypercornServer,
LiveServer,
Server,
hello_app,
http_proxy_server,
slow_hello_app,
)

try:
import hypercorn
Expand All @@ -26,9 +32,6 @@
SERVER_HTTPS_PORT = 8003
HTTPS_SERVER_URL = f"https://localhost:{SERVER_HTTPS_PORT}"

SLOW_SERVER_HOST = SERVER_HOST
SLOW_SERVER_PORT = 8004


@pytest.fixture(scope="session")
def proxy_server() -> typing.Iterator[URL]:
Expand All @@ -41,22 +44,13 @@ def proxy_server() -> typing.Iterator[URL]:

async def app(scope: dict, receive: typing.Callable, send: typing.Callable) -> None:
assert scope["type"] == "http"
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain"]],
}
)
await send({"type": "http.response.body", "body": b"Hello, world!"})

raw_path: bytes = scope["raw_path"]

async def slow_app(
scope: dict, receive: typing.Callable, send: typing.Callable
) -> None:
await trio.sleep(5)

await app(scope, receive, send)
if raw_path.startswith(b"/delay"):
await slow_hello_app(scope, receive, send)
else:
await hello_app(scope, receive, send)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -108,24 +102,6 @@ def serve_in_thread(self) -> typing.Iterator[None]:
yield server


@pytest.fixture(scope="session")
def slow_uds() -> typing.Iterator[str]:
uds = "test_slow_server.sock"
try:
yield uds
finally:
os.remove(uds)


@pytest.fixture(scope="session")
def slow_uds_server(uds: str) -> typing.Iterator[Server]:
assert hypercorn is not None

server = HypercornServer(app=slow_app, bind=f"unix:{uds}")
with server.serve_in_thread():
yield server


@pytest.fixture(scope="session")
def server() -> typing.Iterator[Server]: # pragma: no cover
server: Server # Please mypy.
Expand Down Expand Up @@ -191,18 +167,6 @@ def https_server(
yield server


@pytest.fixture(scope="session")
def slow_tcp_server() -> typing.Iterator[Server]:
assert hypercorn is not None

server = HypercornServer(
app=slow_app, bind=f"{SLOW_SERVER_HOST}:{SLOW_SERVER_PORT}"
)

with server.serve_in_thread():
yield server


@pytest.fixture(scope="function")
def too_many_open_files_minus_one() -> typing.Iterator[None]:
# Fixture for test regression on https://github.com/encode/httpcore/issues/182
Expand Down
21 changes: 13 additions & 8 deletions tests/sync_tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,28 +473,33 @@ def test_cannot_connect_uds(backend: str) -> None:
sys.version_info[:2] < (3, 7), reason="Hypercorn doesn't support python < 3.7"
)

def test_connection_timeout_tcp(backend: str, slow_tcp_server: Server) -> None:
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", *slow_tcp_server.netloc, b"/delay/5")
headers = [slow_tcp_server.host_header]
url = (b"https", *server.netloc, b"/delay/1")
headers = [server.host_header]
ext = {"timeout": {"connect": 0.1}}

with httpcore.SyncConnectionPool(backend=backend) as http:
with pytest.raises(httpcore.TimeoutException):
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"
)

def test_connection_timeout_uds(backend: str, slow_uds_server: Server) -> None:
uds = slow_uds_server.uds
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"/delay/5")
url = (b"https", b"localhost", None, b"/delay/1")
headers = [(b"host", b"localhost")]
ext = {"timeout": {"connect": 0.1}}

with httpcore.SyncConnectionPool(uds=uds, backend=backend) as http:
with pytest.raises(httpcore.TimeoutException):
with pytest.raises(httpcore.ConnectTimeout):
http.request(method, url, headers, ext=ext)
30 changes: 30 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile
import threading
import time
import typing
from typing import Callable, Iterator, List, Tuple

import sniffio
Expand Down Expand Up @@ -197,3 +198,32 @@ def create_proxy_block_file(blocked_domains: List[str]):
file.flush()

yield file.name


async def hello_app(
scope: dict, receive: typing.Callable, send: typing.Callable
) -> None:
assert scope["type"] == "http"
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain"]],
}
)
await send({"type": "http.response.body", "body": b"Hello, world!"})


async def slow_hello_app(
scope: dict, receive: typing.Callable, send: typing.Callable
) -> None:
assert scope["type"] == "http"

try:
delay = float(scope["raw_path"].split(b"/")[-1])
except ValueError:
delay = 1.0

await trio.sleep(delay)

await hello_app(scope, receive, send)

0 comments on commit 4fe216e

Please sign in to comment.