Skip to content

Commit

Permalink
fix: breaking changes in uvicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
sysid committed Jun 15, 2024
1 parent 4844ce8 commit 15e55d5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 32 deletions.
29 changes: 15 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,29 @@ tox: ## Run tox
################################################################################
# Code Quality \
QUALITY: ## ############################################################
.PHONY: style
style: isort format ## perform code style format (black, isort)

.PHONY: format
format: ## perform black formatting
black $(pkg_src) tests
format: ## perform ruff formatting
@ruff format $(pkg_src) $(tests_src)

.PHONY: isort
isort: ## apply import sort ordering
isort . --profile black
.PHONY: format-check
format-check: ## perform black formatting
@ruff format --check $(pkg_src) $(tests_src)

.PHONY: lint
lint: flake8 mypy ## lint code with all static code checks
.PHONY: sort-imports
sort-imports: ## apply import sort ordering
isort $(pkg_src) $(tests_src) --profile black

.PHONY: flake8
flake8: ## check style with flake8
@flake8 $(pkg_src)
.PHONY: style
style: sort-imports format ## perform code style format (black, isort)

.PHONY: lint
lint: ## check style with ruff
ruff $(pkg_src) $(tests_src)

.PHONY: mypy
mypy: ## check type hint annotations
# keep config in pyproject.toml for integration with PyCharm
mypy --config-file pyproject.toml $(pkg_src)
@mypy --config-file pyproject.toml --install-types --non-interactive $(pkg_src)

################################################################################
# Clean \
Expand Down
8 changes: 4 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ classifiers = [
]
dependencies = [
"starlette",
"uvicorn==0.28.1", # 0.29 breaks testing
"uvicorn",
"anyio",
]
[project.urls]
Expand Down
4 changes: 4 additions & 0 deletions readme_uvicorn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Fix

Breaking:
https://github.com/encode/uvicorn/compare/0.28.1...0.29.0
6 changes: 5 additions & 1 deletion tests/integration/main_endless.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


async def endless(req: Request):
"""Simulates an endless stream, events sent every 0.3 seconds"""

async def event_publisher():
i = 0
try:
Expand All @@ -23,7 +25,9 @@ async def event_publisher():
yield dict(data=i)
await asyncio.sleep(0.3)
except asyncio.CancelledError as e:
_log.info(f"Disconnected from client (via refresh/close) {req.client}")
_log.info(
f"Disconnected from client (via refresh/close) {req.client} after {i} events"
)
# Do any other cleanup, if any
raise e

Expand Down
7 changes: 0 additions & 7 deletions tests/integration/test_conditional_yielding_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import asyncio
import logging

import pytest
from asgi_lifespan import LifespanManager
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.routing import Route

from sse_starlette import EventSourceResponse

_log = logging.getLogger(__name__)
20 changes: 16 additions & 4 deletions tests/integration/test_multiple_consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ def terminate_server():
for child in parent.children(recursive=True):
child.terminate()
parent.terminate()
parent.wait()
try:
# fix uvicorn breaking change: https://github.com/encode/uvicorn/compare/0.28.1...0.29.0
parent.wait(timeout=1)
except psutil.TimeoutExpired:
_log.info(
"Server process did not terminate after 1 second, killing it."
)
parent.kill()
parent.wait()
server_process.wait()
_log.debug("Server process terminated.")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
Expand Down Expand Up @@ -109,7 +117,9 @@ async def make_arequest(url, expected_lines=2):
finally:
assert (
i == expected_lines
), f"Expected {expected_lines} lines" # not part of test runner, failure is not reported
), (
f"Expected {expected_lines} lines"
) # not part of test runner, failure is not reported

_log.info(
f"{threading.current_thread().ident}: Stopping making requests to {url=}, finished after {i=} responses."
Expand All @@ -120,7 +130,9 @@ async def make_arequest(url, expected_lines=2):
# ...
assert (
i == expected_lines
), f"Expected {expected_lines} lines" # not part of test runner, failure is not reported
), (
f"Expected {expected_lines} lines"
) # not part of test runner, failure is not reported


@pytest.mark.skipif(os.name == "nt", reason="Skip on Windows")
Expand All @@ -130,7 +142,7 @@ async def make_arequest(url, expected_lines=2):
[
(
"uvicorn tests.integration.main_endless:app --host localhost --port {port} --log-level {log_level}",
8,
14,
),
(
"uvicorn tests.integration.main_endless_conditional:app --host localhost --port {port} --log-level {log_level}",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_event_source_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def receive():
return {"type": "something"}

response = EventSourceResponse(event_publisher(), ping=1)
with pytest.raises(anyio.WouldBlock) as e:
with pytest.raises(anyio.WouldBlock):
with collapse_excgroups():
await response({}, receive, send)

Expand Down

0 comments on commit 15e55d5

Please sign in to comment.