fix(client-python): keepalive options, typed error mapping with status code, open-enum names, malformed-reply checks (CPY-001..004, #257)

This commit is contained in:
Joseph Doherty
2026-12-03 09:46:02 -05:00
parent fafd79fa12
commit 3173ad7083
9 changed files with 365 additions and 14 deletions
+55
View File
@@ -9,6 +9,14 @@ from zb_mom_ww_mxgateway import options as options_module
from zb_mom_ww_mxgateway.errors import MxGatewayTransportError
from zb_mom_ww_mxgateway.options import ClientOptions, create_channel
#: Default HTTP/2 keepalive channel options (30 s ping, 10 s timeout, idle pings).
KEEPALIVE_OPTIONS = [
("grpc.keepalive_time_ms", 30_000),
("grpc.keepalive_timeout_ms", 10_000),
("grpc.keepalive_permit_without_calls", 1),
("grpc.http2.max_pings_without_data", 0),
]
def test_auth_metadata_adds_bearer_api_key() -> None:
assert auth_metadata("mxgw_test_secret") == (
@@ -70,6 +78,7 @@ def test_create_channel_uses_plaintext_channel(monkeypatch: pytest.MonkeyPatch)
[
("grpc.max_receive_message_length", 16 * 1024 * 1024),
("grpc.max_send_message_length", 16 * 1024 * 1024),
*KEEPALIVE_OPTIONS,
],
),
]
@@ -122,6 +131,7 @@ def test_create_channel_uses_tls_channel_tofu_default(monkeypatch: pytest.Monkey
[
("grpc.max_receive_message_length", 16 * 1024 * 1024),
("grpc.max_send_message_length", 16 * 1024 * 1024),
*KEEPALIVE_OPTIONS,
("grpc.ssl_target_name_override", "localhost"),
],
),
@@ -171,6 +181,7 @@ def test_create_channel_uses_tls_channel_tofu_respects_server_name_override(
[
("grpc.max_receive_message_length", 16 * 1024 * 1024),
("grpc.max_send_message_length", 16 * 1024 * 1024),
*KEEPALIVE_OPTIONS,
# Explicit override from ClientOptions — not the auto-localhost one
("grpc.ssl_target_name_override", "gateway.test"),
],
@@ -224,6 +235,7 @@ def test_create_channel_uses_tls_channel_require_cert_validation(
[
("grpc.max_receive_message_length", 16 * 1024 * 1024),
("grpc.max_send_message_length", 16 * 1024 * 1024),
*KEEPALIVE_OPTIONS,
],
),
]
@@ -278,6 +290,7 @@ def test_create_channel_uses_tls_channel_ca_file(
[
("grpc.max_receive_message_length", 16 * 1024 * 1024),
("grpc.max_send_message_length", 16 * 1024 * 1024),
*KEEPALIVE_OPTIONS,
],
),
]
@@ -324,3 +337,45 @@ def test_tofu_probe_timeout_raises_transport_error(
with pytest.raises(MxGatewayTransportError) as excinfo:
create_channel(options)
assert options.endpoint in str(excinfo.value)
def test_keepalive_defaults_are_configurable_and_can_be_disabled() -> None:
from zb_mom_ww_mxgateway.options import ClientOptions
defaults = ClientOptions(endpoint="localhost:5000")
assert defaults.keepalive_time == 30.0
assert defaults.keepalive_timeout == 10.0
assert defaults.keepalive_permit_without_calls is True
tuned = ClientOptions(
endpoint="localhost:5000",
keepalive_time=45.0,
keepalive_timeout=5.0,
keepalive_permit_without_calls=False,
)
assert (tuned.keepalive_time, tuned.keepalive_timeout) == (45.0, 5.0)
assert tuned.keepalive_permit_without_calls is False
with pytest.raises(ValueError):
ClientOptions(endpoint="localhost:5000", keepalive_time=0)
with pytest.raises(ValueError):
ClientOptions(endpoint="localhost:5000", keepalive_timeout=0)
def test_keepalive_disabled_adds_no_keepalive_channel_options(monkeypatch) -> None:
from zb_mom_ww_mxgateway import options as options_module
calls: list[list[tuple[str, object]]] = []
monkeypatch.setattr(
options_module.grpc.aio,
"insecure_channel",
lambda endpoint, options: calls.append(options) or "plain-channel",
)
options_module.create_channel(
options_module.ClientOptions(
endpoint="localhost:5000", plaintext=True, keepalive_time=None
)
)
assert not any(name.startswith("grpc.keepalive") for name, _ in calls[0])