Skip to content

Commit 265ac16

Browse files
authored
Merge pull request #151 from kernel/hypeship/assert-transport-cancel
test: assert stream cancellation reaches transport
2 parents 98659ce + d281b8f commit 265ac16

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

tests/test_browser_routing.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,30 @@ def test_telemetry_stream_routes_directly_to_vm(monkeypatch: pytest.MonkeyPatch)
122122

123123

124124
@pytest.mark.asyncio
125-
async def test_async_telemetry_stream_cancellation_survives_direct_routing(
125+
async def test_async_telemetry_stream_cancellation_reaches_transport(
126126
monkeypatch: pytest.MonkeyPatch,
127127
) -> None:
128128
monkeypatch.setenv("KERNEL_BROWSER_ROUTING_SUBRESOURCES", "telemetry/stream")
129129
read_started = asyncio.Event()
130-
read_stopped = asyncio.Event()
130+
transport_cancelled = asyncio.Event()
131+
chunks: asyncio.Queue[bytes] = asyncio.Queue()
131132

132133
class BlockingSSEStream(httpx.AsyncByteStream):
133134
@override
134135
async def __aiter__(self) -> AsyncIterator[bytes]:
135136
read_started.set()
136137
try:
137-
await asyncio.Event().wait()
138-
finally:
139-
read_stopped.set()
140-
yield b""
138+
while True:
139+
yield await chunks.get()
140+
except asyncio.CancelledError:
141+
transport_cancelled.set()
142+
raise
141143

142144
@override
143145
async def aclose(self) -> None:
144-
read_stopped.set()
146+
pass
145147

146-
async def handle_request(request: httpx.Request) -> httpx.Response:
147-
assert request.url.path == "/browser/kernel/telemetry/stream"
148+
async def handle_request(_request: httpx.Request) -> httpx.Response:
148149
return httpx.Response(
149150
200,
150151
headers={"content-type": "text/event-stream"},
@@ -168,7 +169,7 @@ async def handle_request(request: httpx.Request) -> httpx.Response:
168169
consumer.cancel()
169170
with pytest.raises(asyncio.CancelledError):
170171
await asyncio.wait_for(consumer, timeout=1)
171-
await asyncio.wait_for(read_stopped.wait(), timeout=1)
172+
await asyncio.wait_for(transport_cancelled.wait(), timeout=1)
172173

173174

174175
@respx.mock

0 commit comments

Comments
 (0)