-
Notifications
You must be signed in to change notification settings - Fork 41
Add timeout support #469
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
base: master
Are you sure you want to change the base?
Add timeout support #469
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import asyncio | ||
| import sys | ||
| from typing import Awaitable, Callable, List | ||
| from typing import Awaitable, Callable, List, Optional | ||
|
|
||
| import pytest | ||
| from aiohttp import web | ||
|
|
@@ -559,3 +559,47 @@ async def handler(request: web.Request) -> EventSourceResponse: | |
|
|
||
| async with client.get("/") as response: | ||
| assert 200 == response.status | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("timeout", (None, 0.1)) | ||
| async def test_with_timeout( | ||
| aiohttp_client: ClientFixture, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| timeout: Optional[float], | ||
| ) -> None: | ||
| """Test write timeout. | ||
|
|
||
| Relates to this issue: | ||
| https://github.com/sysid/sse-starlette/issues/89 | ||
| """ | ||
| timeout_raised = False | ||
|
|
||
| async def frozen_write(_data: bytes) -> None: | ||
| await asyncio.sleep(42) | ||
|
||
|
|
||
| async def handler(request: web.Request) -> EventSourceResponse: | ||
| sse = EventSourceResponse(timeout=timeout) | ||
| sse.ping_interval = 42 | ||
| await sse.prepare(request) | ||
| monkeypatch.setattr(sse, "write", frozen_write) | ||
|
|
||
| async with sse: | ||
| try: | ||
| await sse.send("foo") | ||
| except TimeoutError: | ||
| nonlocal timeout_raised | ||
| timeout_raised = True | ||
| raise | ||
|
|
||
| return sse # pragma: no cover | ||
|
|
||
| app = web.Application() | ||
| app.router.add_route("GET", "/", handler) | ||
|
|
||
| client = await aiohttp_client(app) | ||
| async with client.get("/") as resp: | ||
| assert resp.status == 200 | ||
| await asyncio.sleep(0.5) | ||
| assert resp.connection and resp.connection.closed is bool(timeout) | ||
|
|
||
| assert timeout_raised is bool(timeout) | ||
Uh oh!
There was an error while loading. Please reload this page.