56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""真实启动冒烟测试: 执行 app.py 的完整启动序列并验证 HTTP 服务可响应。
|
|
唯一差异: start_rental_timer 被置空, 避免测试期间触发真实提醒邮件/自动还车。"""
|
|
import sys
|
|
import threading
|
|
import time
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # 项目根
|
|
|
|
import timer
|
|
timer.start_rental_timer = lambda *a, **k: None # 防止测试期间发出真实邮件
|
|
|
|
import app as app_module
|
|
|
|
app_module.init_db()
|
|
print("[smoke] init_db 完成")
|
|
|
|
app_module.restore_active_timers()
|
|
app_module.start_all_order_checkers()
|
|
app_module.start_snapshot_scheduler()
|
|
|
|
PORT = 9987
|
|
t = threading.Thread(
|
|
target=lambda: app_module.app.run(host="127.0.0.1", port=PORT, debug=False),
|
|
daemon=True,
|
|
)
|
|
t.start()
|
|
|
|
# 等待服务就绪
|
|
ok = False
|
|
for _ in range(30):
|
|
time.sleep(0.3)
|
|
try:
|
|
with urllib.request.urlopen(f"http://127.0.0.1:{PORT}/", timeout=3) as r:
|
|
body = r.read(200)
|
|
print(f"[smoke] GET / -> {r.status}, body[:60]={body[:60]!r}")
|
|
ok = r.status == 200
|
|
break
|
|
except Exception:
|
|
continue
|
|
|
|
# 再打两个关键接口
|
|
for path in ("/api/config", "/api/history/stats"):
|
|
try:
|
|
with urllib.request.urlopen(f"http://127.0.0.1:{PORT}{path}", timeout=5) as r:
|
|
print(f"[smoke] GET {path} -> {r.status}")
|
|
except Exception as e:
|
|
print(f"[smoke] GET {path} -> 异常 {e}")
|
|
ok = False
|
|
|
|
import os
|
|
os._exit(0 if ok else 1)
|