|
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | import base64 |
| 5 | +import json |
5 | 6 | from collections.abc import AsyncIterator |
6 | 7 | from typing import Literal |
7 | 8 |
|
|
11 | 12 | from websockets.http11 import Request |
12 | 13 |
|
13 | 14 | from polymarket_us import AuthenticationError, PolymarketUS |
14 | | -from polymarket_us.errors import PolymarketUSError |
| 15 | +from polymarket_us.errors import PolymarketUSError, WebSocketError |
15 | 16 | from polymarket_us.websocket import MarketsWebSocket, PrivateWebSocket |
16 | 17 | from polymarket_us.websocket.base import BaseWebSocket |
| 18 | +from polymarket_us.websocket.types import WebSocketErrorMessage |
17 | 19 |
|
18 | 20 | TEST_SECRET_KEY = "nWGxne/9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A=" |
19 | 21 |
|
@@ -84,6 +86,50 @@ async def handler(peer: ServerConnection) -> None: |
84 | 86 | finally: |
85 | 87 | await asyncio.wait_for(ws.close(), timeout=5) |
86 | 88 |
|
| 89 | + @pytest.mark.parametrize( |
| 90 | + "subscription_type", |
| 91 | + ["known", "SUBSCRIPTION_TYPE_UNSPECIFIED", "SUBSCRIPTION_TYPE_FUTURE", None], |
| 92 | + ) |
| 93 | + @pytest.mark.parametrize("request_id", ["req-123", None]) |
| 94 | + async def test_error_callback_preserves_subscription_context( |
| 95 | + self, |
| 96 | + connection: tuple[BaseWebSocket, ServerConnection], |
| 97 | + stream: Literal["markets", "private"], |
| 98 | + subscription_type: str | None, |
| 99 | + request_id: str | None, |
| 100 | + ) -> None: |
| 101 | + ws, peer = connection |
| 102 | + if subscription_type == "known": |
| 103 | + subscription_type = ( |
| 104 | + "SUBSCRIPTION_TYPE_MARKET_DATA" |
| 105 | + if stream == "markets" |
| 106 | + else "SUBSCRIPTION_TYPE_ORDER" |
| 107 | + ) |
| 108 | + frame: WebSocketErrorMessage = {"error": "subscription failed"} |
| 109 | + if subscription_type is not None: |
| 110 | + frame["subscriptionType"] = subscription_type |
| 111 | + if request_id is not None: |
| 112 | + frame["requestId"] = request_id |
| 113 | + errors: list[PolymarketUSError] = [] |
| 114 | + messages: list[object] = [] |
| 115 | + heartbeat = asyncio.Event() |
| 116 | + ws.on("error", errors.append) |
| 117 | + ws.on("message", messages.append) |
| 118 | + ws.on("heartbeat", heartbeat.set) |
| 119 | + |
| 120 | + await asyncio.wait_for(peer.send(json.dumps(frame)), timeout=5) |
| 121 | + await asyncio.wait_for(peer.send('{"heartbeat": {}}'), timeout=5) |
| 122 | + await asyncio.wait_for(heartbeat.wait(), timeout=5) |
| 123 | + |
| 124 | + assert messages == [frame, {"heartbeat": {}}] |
| 125 | + assert len(errors) == 1 |
| 126 | + error = errors[0] |
| 127 | + assert isinstance(error, WebSocketError) |
| 128 | + assert str(error) == "subscription failed" |
| 129 | + assert error.request_id == request_id |
| 130 | + assert error.subscription_type == subscription_type |
| 131 | + assert ws.is_connected |
| 132 | + |
87 | 133 | @pytest.mark.parametrize("close_code", [1000, 1001, 1011]) |
88 | 134 | async def test_remote_close_emits_once( |
89 | 135 | self, connection: tuple[BaseWebSocket, ServerConnection], close_code: int |
|
0 commit comments