2026-03-06 17:55:14 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2026-04-03 14:59:58 +08:00
|
|
|
from src.services.proxy_node.gateway_tunnel_transport import TunnelRelayResponseStream
|
2026-03-06 17:55:14 +08:00
|
|
|
|
|
|
|
|
|
2026-03-17 20:22:12 +08:00
|
|
|
class _FakeResponse:
|
|
|
|
|
def __init__(self, chunks: list[bytes]) -> None:
|
|
|
|
|
self._chunks = chunks
|
|
|
|
|
self.closed = False
|
2026-03-06 17:55:14 +08:00
|
|
|
|
2026-03-17 20:22:12 +08:00
|
|
|
async def aiter_raw(self): # type: ignore[override]
|
|
|
|
|
for chunk in self._chunks:
|
|
|
|
|
yield chunk
|
|
|
|
|
|
|
|
|
|
async def aclose(self) -> None:
|
|
|
|
|
self.closed = True
|
2026-03-06 17:55:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-04-03 14:59:58 +08:00
|
|
|
async def test_tunnel_relay_response_stream_closes_after_iteration() -> None:
|
2026-03-17 20:22:12 +08:00
|
|
|
response = _FakeResponse([b"hello", b"world"])
|
2026-04-03 14:59:58 +08:00
|
|
|
stream = TunnelRelayResponseStream(response) # type: ignore[arg-type]
|
2026-03-17 20:22:12 +08:00
|
|
|
|
2026-03-06 17:55:14 +08:00
|
|
|
chunks = []
|
|
|
|
|
async for chunk in stream:
|
|
|
|
|
chunks.append(chunk)
|
|
|
|
|
|
2026-03-17 20:22:12 +08:00
|
|
|
assert chunks == [b"hello", b"world"]
|
|
|
|
|
assert response.closed is True
|
2026-03-06 17:55:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-04-03 14:59:58 +08:00
|
|
|
async def test_tunnel_relay_response_stream_aclose_closes_response() -> None:
|
2026-03-17 20:22:12 +08:00
|
|
|
response = _FakeResponse([])
|
2026-04-03 14:59:58 +08:00
|
|
|
stream = TunnelRelayResponseStream(response) # type: ignore[arg-type]
|
2026-03-06 17:55:14 +08:00
|
|
|
|
2026-03-17 20:22:12 +08:00
|
|
|
await stream.aclose()
|
2026-03-06 17:55:14 +08:00
|
|
|
|
2026-03-17 20:22:12 +08:00
|
|
|
assert response.closed is True
|