dcy-010(safety-sell 영속 거부 재시도 백오프) 전체 SDLC: draft-req → confirm-req → draft-batch(BAT-014 batch-lite) → run-batch-delivery → confirm-req-implemented. 2026-06-07 실 KIS LIVE SL/TP e2e에서 발견한 KIS 주문 spam 위험(영속 거부 시 매도 무한 재제출) 차단. - REQ-052 (Integration): SafetySellService per-slot 인메모리 쿨다운 (_in_cooldown/_attempt_sell/_prune_cooldown) — forced_close·stop-loss·take-profit 트리거에 min_retry_interval_sec(600s) 이내 재시도 skip(가격 poll 포함) + max_retry_warn_count(3) 도달 시 WARNING + holding 이탈 정리. market_close 제외. - REQ-053 (Configuration): SafetySellConfig min_retry_interval_sec/max_retry_warn_count + TREX_SAFETY_SELL_MIN_RETRY_INTERVAL/MAX_RETRY_WARN_COUNT env (additive) - REQ-054 (Documentation): runtime-flows 쿨다운 flow + 가격체크 tr_id(FHKST01010100) 정정 인메모리 only(DB/스레드/인터페이스 변경 없음), default 미설정 시 기존 동작 불변. 검증: 신규 8 단위(쿨다운 6 + config 2), 전체 278 passed, ruff clean. REQ-052~054 Implemented, BAT-014 release-candidate. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
"""SafetySellConfig env-var parsing tests (REQ-041 / BAT-010)."""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from trex_core.runtime.config import DaemonConfig, SafetySellConfig
|
|
|
|
_SAFETY_KEYS = (
|
|
"TREX_WATCHDOG_TIMEOUT",
|
|
"TREX_WATCHDOG_GRACE_PERIOD",
|
|
"TREX_FORCED_CLOSE_CHECK_INTERVAL",
|
|
"TREX_STOP_LOSS_POLL_INTERVAL",
|
|
"TREX_MARKET_CLOSE_LIQUIDATION_TIME",
|
|
"TREX_SAFETY_SELL_MIN_RETRY_INTERVAL",
|
|
"TREX_SAFETY_SELL_MAX_RETRY_WARN_COUNT",
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_env(monkeypatch):
|
|
for k in _SAFETY_KEYS:
|
|
monkeypatch.delenv(k, raising=False)
|
|
|
|
|
|
@pytest.fixture
|
|
def _base_env(monkeypatch):
|
|
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "dbname=x")
|
|
|
|
|
|
def test_safety_sell_config_defaults(tmp_path, monkeypatch, _base_env):
|
|
"""AC-1: unset env vars produce documented defaults."""
|
|
monkeypatch.chdir(tmp_path)
|
|
cfg = DaemonConfig.from_env()
|
|
ss = cfg.safety_sell
|
|
assert ss.watchdog_timeout_sec == 120
|
|
assert ss.watchdog_grace_period_sec == 60
|
|
assert ss.forced_close_check_interval_sec == 60
|
|
assert ss.stop_loss_poll_interval_sec == 30
|
|
assert ss.market_close_liquidation_time is None
|
|
# BAT-014 / REQ-053
|
|
assert ss.min_retry_interval_sec == 600
|
|
assert ss.max_retry_warn_count == 3
|
|
|
|
|
|
def test_retry_cooldown_env_tuning(tmp_path, monkeypatch, _base_env):
|
|
"""REQ-053 AC-2: retry cooldown env vars are parsed."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.setenv("TREX_SAFETY_SELL_MIN_RETRY_INTERVAL", "300")
|
|
monkeypatch.setenv("TREX_SAFETY_SELL_MAX_RETRY_WARN_COUNT", "5")
|
|
ss = DaemonConfig.from_env().safety_sell
|
|
assert ss.min_retry_interval_sec == 300
|
|
assert ss.max_retry_warn_count == 5
|
|
|
|
|
|
def test_safety_sell_config_env_tuning(tmp_path, monkeypatch, _base_env):
|
|
"""AC-2~5: all five env vars are parsed correctly."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.setenv("TREX_WATCHDOG_TIMEOUT", "180")
|
|
monkeypatch.setenv("TREX_WATCHDOG_GRACE_PERIOD", "90")
|
|
monkeypatch.setenv("TREX_FORCED_CLOSE_CHECK_INTERVAL", "45")
|
|
monkeypatch.setenv("TREX_STOP_LOSS_POLL_INTERVAL", "15")
|
|
monkeypatch.setenv("TREX_MARKET_CLOSE_LIQUIDATION_TIME", "15:30")
|
|
cfg = DaemonConfig.from_env()
|
|
ss = cfg.safety_sell
|
|
assert ss.watchdog_timeout_sec == 180
|
|
assert ss.watchdog_grace_period_sec == 90
|
|
assert ss.forced_close_check_interval_sec == 45
|
|
assert ss.stop_loss_poll_interval_sec == 15
|
|
assert ss.market_close_liquidation_time == "15:30"
|
|
|
|
|
|
def test_market_close_liquidation_time_empty_string_becomes_none(tmp_path, monkeypatch, _base_env):
|
|
"""Empty string for optional field collapses to None."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.setenv("TREX_MARKET_CLOSE_LIQUIDATION_TIME", "")
|
|
cfg = DaemonConfig.from_env()
|
|
assert cfg.safety_sell.market_close_liquidation_time is None
|
|
|
|
|
|
def test_safety_sell_config_is_frozen():
|
|
"""SafetySellConfig is a frozen dataclass."""
|
|
ss = SafetySellConfig()
|
|
with pytest.raises((AttributeError, TypeError)):
|
|
ss.watchdog_timeout_sec = 999 # type: ignore[misc]
|