feat(ops): track broker balance worker failure lifecycle and backoff

Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-09-12 13:33:12 +00:00
committed by GitHub
co-authored by genichin
parent 59bb492883
commit 084e05a97d
9 changed files with 453 additions and 27 deletions
+27 -3
View File
@@ -59,7 +59,8 @@ def test_account_scope_hash_separates_endpoint_and_application_identity():
(403, "authorization_failed"),
(422, "broker_rejected"),
(429, "transport_failed"),
(500, "transport_failed"),
(500, "http_5xx"),
(504, "timeout"),
(None, "broker_rejected"),
],
)
@@ -350,8 +351,8 @@ def test_get_balance_fails_closed_on_later_transport_and_http_errors(monkeypatch
request = httpx.Request("GET", "https://example.test/balance")
response = httpx.Response(503, request=request)
for failure, expected_code in [
(httpx.ConnectTimeout("unavailable"), "transport_failed"),
(httpx.HTTPStatusError("bad gateway", request=request, response=response), "transport_failed"),
(httpx.ConnectTimeout("unavailable"), "timeout"),
(httpx.HTTPStatusError("bad gateway", request=request, response=response), "http_5xx"),
]:
broker, calls = _balance_broker(monkeypatch, [
_BalanceResponse(_balance_payload([{"pdno": "005930", "hldg_qty": "2"}], fk="A", nk="1"), "F"),
@@ -407,6 +408,29 @@ def test_get_available_cash_uses_cash_only_amount(monkeypatch):
assert b.get_available_cash() == 695_591
@pytest.mark.parametrize(
("failure", "expected_code"),
[
(httpx.ReadTimeout("timed out"), "timeout"),
(
httpx.HTTPStatusError(
"bad gateway",
request=httpx.Request("GET", "https://example.test"),
response=httpx.Response(502, request=httpx.Request("GET", "https://example.test")),
),
"http_5xx",
),
],
)
def test_get_available_cash_classifies_timeout_and_http_5xx(monkeypatch, failure, expected_code):
b = KISBroker("12345678-01", app_key="k", app_secret="s", base_url=_MOCK_URL)
monkeypatch.setattr(b, "_headers", lambda _tr: {})
monkeypatch.setattr("trex_core.adapters.broker.kis.httpx.get", lambda *a, **kw: (_ for _ in ()).throw(failure))
with pytest.raises(BrokerBalanceError) as raised:
b.get_available_cash()
assert raised.value.code == expected_code
def test_submit_transport_failure_blocks_automatic_duplicate(monkeypatch):
b = KISBroker("12345678-01", app_key="k", app_secret="s", base_url=_MOCK_URL)
monkeypatch.setattr(b, "_headers", lambda _tr: {})