|
1 | 1 | import asyncio |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
3 | 5 | from hyperbrowser.client.async_client import AsyncHyperbrowser |
4 | 6 | from hyperbrowser.client.managers.async_manager.session import SessionEventLogsManager |
5 | 7 | from hyperbrowser.client.sync import Hyperbrowser |
@@ -43,6 +45,45 @@ async def tracked_close() -> None: |
43 | 45 | asyncio.run(run()) |
44 | 46 |
|
45 | 47 |
|
| 48 | +def test_sync_client_context_manager_closes_transport_on_exception(): |
| 49 | + client = Hyperbrowser(api_key="test-key") |
| 50 | + close_calls = {"count": 0} |
| 51 | + original_close = client.transport.close |
| 52 | + |
| 53 | + def tracked_close() -> None: |
| 54 | + close_calls["count"] += 1 |
| 55 | + original_close() |
| 56 | + |
| 57 | + client.transport.close = tracked_close |
| 58 | + |
| 59 | + with pytest.raises(RuntimeError, match="sync boom"): |
| 60 | + with client: |
| 61 | + raise RuntimeError("sync boom") |
| 62 | + |
| 63 | + assert close_calls["count"] == 1 |
| 64 | + |
| 65 | + |
| 66 | +def test_async_client_context_manager_closes_transport_on_exception(): |
| 67 | + async def run() -> None: |
| 68 | + client = AsyncHyperbrowser(api_key="test-key") |
| 69 | + close_calls = {"count": 0} |
| 70 | + original_close = client.transport.close |
| 71 | + |
| 72 | + async def tracked_close() -> None: |
| 73 | + close_calls["count"] += 1 |
| 74 | + await original_close() |
| 75 | + |
| 76 | + client.transport.close = tracked_close |
| 77 | + |
| 78 | + with pytest.raises(RuntimeError, match="async boom"): |
| 79 | + async with client: |
| 80 | + raise RuntimeError("async boom") |
| 81 | + |
| 82 | + assert close_calls["count"] == 1 |
| 83 | + |
| 84 | + asyncio.run(run()) |
| 85 | + |
| 86 | + |
46 | 87 | def test_async_session_event_logs_annotation_is_response_model(): |
47 | 88 | assert ( |
48 | 89 | SessionEventLogsManager.list.__annotations__["return"] |
|
0 commit comments