Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sdks/python/pmxt/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def _ensure_connected(self) -> None:

# Connection attempts default to CONNECT_ATTEMPTS; callers may override
# via the "maxReconnectAttempts" config key.
max_attempts = self._config.get("maxReconnectAttempts") or CONNECT_ATTEMPTS
max_attempts = self._config.get("maxReconnectAttempts")
if max_attempts is None:
max_attempts = CONNECT_ATTEMPTS
# When "reconnectInterval" (ms) is not configured, preserve the fast
# default backoff (0.25s, 0.5s, ...). Only a longer, explicitly
# configured interval overrides it.
Expand Down
31 changes: 31 additions & 0 deletions sdks/python/tests/test_ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import sys
import types

import pytest

import pmxt.ws_client as ws_client
from pmxt.errors import PmxtError
from pmxt.ws_client import SidecarWsClient, _WsSubscription, _connect_websocket


Expand Down Expand Up @@ -147,6 +150,34 @@ def test_ws_config_custom_url_and_reconnect_interval(monkeypatch):
assert sleeps == [2.0]


def test_ws_none_reconnect_attempts_uses_default(monkeypatch):
urls = []
_install_failing_websocket(monkeypatch, urls)

client = SidecarWsClient(
"http://localhost:3847", config={"maxReconnectAttempts": None}
)
with pytest.raises(OSError, match="handshake failure"):
with client._lock:
client._ensure_connected()

assert len(urls) == 3


def test_ws_zero_reconnect_attempts_is_honored(monkeypatch):
urls = []
_install_failing_websocket(monkeypatch, urls)

client = SidecarWsClient(
"http://localhost:3847", config={"maxReconnectAttempts": 0}
)
with pytest.raises(PmxtError, match="WebSocket connection failed"):
with client._lock:
client._ensure_connected()

assert urls == []


def test_ws_default_backoff_is_fast(monkeypatch):
"""Regression: the default reconnect backoff must stay 0.25s/0.5s, not a
flat multi-second sleep, unless an interval is explicitly configured."""
Expand Down