Skip to content

Commit

Permalink
Test suite refactor using async server context manager instead of bac…
Browse files Browse the repository at this point in the history
…kground thread (#917)

* Used async context manager instead of thread

* Refactored test_ssl

* Refactored test_main , last test become irrelevant

* Same with test_websockets, now exceptions are bubbled !

* Update requirements.txt

* Refactor test_trace_logging.py as well

* Removed what appears to be dead code since we dont use threads

* asynccontextmanager appears in 3.7, just removing it to check if CI passes

* trace logging reworked

* Attempt at 3.6

* Should be better with correct lib name

* Pin new requirements

* Move run_server from conftest to utils

* Revert "Removed what appears to be dead code since we dont use threads"

This reverts commit eb45afc
  • Loading branch information
euri10 committed Dec 28, 2020
1 parent c6b80fc commit 70ec20f
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 339 deletions.
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ mypy
trustme
cryptography
coverage
httpx==0.16.*
pytest-asyncio==0.14.*
async_generator; python_version < '3.7'


# Documentation
mkdocs
Expand Down
47 changes: 14 additions & 33 deletions tests/middleware/test_trace_logging.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import platform
import sys
import threading
import time

import httpx
import pytest
import requests

from uvicorn import Config, Server
from tests.utils import run_server
from uvicorn import Config

test_logging_config = {
"version": 1,
Expand Down Expand Up @@ -54,52 +50,37 @@ async def app(scope, receive, send):
await send({"type": "http.response.body", "body": b"", "more_body": False})


@pytest.mark.skipif(
sys.platform.startswith("win") or platform.python_implementation() == "PyPy",
reason="Skipping test on Windows and PyPy",
)
def test_trace_logging(capsys):
@pytest.mark.asyncio
async def test_trace_logging(capsys):
config = Config(
app=app,
loop="asyncio",
limit_max_requests=1,
log_config=test_logging_config,
log_level="trace",
)
server = Server(config=config)
thread = threading.Thread(target=server.run)
thread.start()
while not server.started:
time.sleep(0.01)
response = requests.get("http://127.0.0.1:8000")
async with run_server(config):
async with httpx.AsyncClient() as client:
response = await client.get("http://127.0.0.1:8000")
assert response.status_code == 204
thread.join()
captured = capsys.readouterr()
assert '"GET / HTTP/1.1" 204' in captured.out
assert "[TEST_ACCESS] TRACE" not in captured.out


@pytest.mark.skipif(
sys.platform.startswith("win") or platform.python_implementation() == "PyPy",
reason="Skipping test on Windows and PyPy",
)
@pytest.mark.parametrize("http_protocol", [("h11"), ("httptools")])
def test_access_logging(capsys, http_protocol):
@pytest.mark.asyncio
async def test_access_logging(capsys):
config = Config(
app=app,
loop="asyncio",
http=http_protocol,
limit_max_requests=1,
log_config=test_logging_config,
)
server = Server(config=config)
thread = threading.Thread(target=server.run)
thread.start()
while not server.started:
time.sleep(0.01)
response = requests.get("http://127.0.0.1:8000")
async with run_server(config):
async with httpx.AsyncClient() as client:
response = await client.get("http://127.0.0.1:8000")

assert response.status_code == 204
thread.join()
captured = capsys.readouterr()
assert '"GET / HTTP/1.1" 204' in captured.out
assert "uvicorn.access" in captured.out

0 comments on commit 70ec20f

Please sign in to comment.