- 스윙 체결가 확인 전 executed_price NULL + fill_sweep, 진입가 resolve(ActiveTrade↔Portfolio), 0체결/부분체결 매수 장부 원복 - DT 고아 감지(근거 한정)·0체결 매도 복원·stale 점검 확대, 시간손절 -0.3% 원복 - 폴백 손절 ATR 캐시, 거래량부족-차단 신호 매수 차단, 현금 5% 유보 재적용, MDD peak/reset 수정 - KIS 서킷브레이커 청산 우회·경보, db_worker 재큐잉 순서, heartbeat·health-watch 사이드카 - 실시간 WS 재구독·41건 우선순위·다건 프레임 분할, 차트 샘플 제거·당일봉 합성, WS 서브프로토콜 인증 - 프론트: 미관리 그룹·확인 모달·차트 실시간 반영·예수금 표시 정정 - 전문가 에이전트 6종 추가(.claude/agents) - 유닛 스위트 2667 passed (소켓 차단 하), tsc OK Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y83Za98XsNw9zuA4FTUytN
88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
"""하드닝 R2: db_worker 크래시 재시작 백오프/한도, stock_analysis 보존기간."""
|
|
import asyncio
|
|
import time
|
|
import types
|
|
|
|
import pytest
|
|
|
|
from app import main as m
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean(monkeypatch):
|
|
monkeypatch.setattr(m, "_db_worker_restarts", [])
|
|
monkeypatch.setattr(m, "_db_worker_restart_total", 0)
|
|
monkeypatch.setattr(m, "_db_worker_restart_limit_hit", False)
|
|
monkeypatch.setattr(m, "_db_worker_restart_handle", None)
|
|
monkeypatch.setattr(m, "_shutting_down", False)
|
|
monkeypatch.setattr(m, "_DB_WORKER_RESTART_BACKOFF_SEC", (0.0,))
|
|
calls = []
|
|
monkeypatch.setattr(m, "_notify_system_warning_bg", lambda c, msg: calls.append((c, msg)))
|
|
return calls
|
|
|
|
|
|
async def _crashed():
|
|
async def boom():
|
|
raise RuntimeError("boom")
|
|
t = asyncio.create_task(boom())
|
|
await asyncio.sleep(0)
|
|
await asyncio.sleep(0)
|
|
return t
|
|
|
|
|
|
def test_backoff_plan_grows_and_limits(monkeypatch):
|
|
monkeypatch.setattr(m, "_DB_WORKER_RESTART_BACKOFF_SEC", (1.0, 5.0))
|
|
assert m._db_worker_restart_plan_v2(1000.0) == (1.0, False)
|
|
m._db_worker_restarts.append(990.0)
|
|
assert m._db_worker_restart_plan_v2(1000.0) == (5.0, False)
|
|
m._db_worker_restarts[:] = [990.0 + i for i in range(m._DB_WORKER_MAX_RESTARTS_PER_HOUR)]
|
|
wait, hit = m._db_worker_restart_plan_v2(1000.0)
|
|
assert hit is True and wait >= m._DB_WORKER_LIMIT_COOLDOWN_SEC
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_restart_is_deferred_not_immediate(_clean):
|
|
app = types.SimpleNamespace(state=types.SimpleNamespace())
|
|
started = []
|
|
|
|
async def factory():
|
|
started.append(1)
|
|
await asyncio.sleep(10)
|
|
|
|
t = await _crashed()
|
|
m._on_db_worker_done_v2(app, t, factory)
|
|
assert started == [] # call_later 로 예약만, 즉시 재시작 아님
|
|
assert m._db_worker_restart_handle is not None
|
|
assert _clean and _clean[0][0] == "DB_WORKER"
|
|
await asyncio.sleep(0.05)
|
|
assert started == [1] and m._db_worker_restart_total == 1
|
|
app.state.db_worker_task.cancel()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_limit_hit_flagged_in_health():
|
|
app = types.SimpleNamespace(state=types.SimpleNamespace())
|
|
m._db_worker_restarts[:] = [time.monotonic()] * m._DB_WORKER_MAX_RESTARTS_PER_HOUR
|
|
|
|
async def factory():
|
|
await asyncio.sleep(10)
|
|
|
|
t = await _crashed()
|
|
m._on_db_worker_done_v2(app, t, factory)
|
|
h = m._db_worker_health_v2(app)
|
|
assert h["restart_limit_hit"] is True
|
|
m._db_worker_restart_handle.cancel()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_restart_when_shutting_down(monkeypatch):
|
|
monkeypatch.setattr(m, "_shutting_down", True)
|
|
app = types.SimpleNamespace(state=types.SimpleNamespace())
|
|
t = await _crashed()
|
|
m._on_db_worker_done_v2(app, t, lambda: None)
|
|
assert m._db_worker_restart_handle is None
|
|
|
|
|
|
def test_stock_analysis_retention_beyond_a_week():
|
|
assert m._STOCK_ANALYSIS_RETENTION_DAYS >= 90
|