Files
syops/backend/tests/test_freight_rpa_heartbeat.py
T
hugo 911afa0f73 feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务
- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。
- 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。
- 更新 ORM 模型,增加出站报表相关的持久化模型。
- 统一命名规范,确保在手、入站与出站的概念一致性。
2026-10-19 18:45:16 +08:00

143 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""freight_rpa_heartbeat 共享哨兵与 payload 单测。"""
from __future__ import annotations
import importlib.util
import sys
import threading
import time
from pathlib import Path
def _load_freight_rpa_heartbeat_module():
rpa_dir = Path(__file__).resolve().parents[2] / "scripts" / "freight_rpa"
if str(rpa_dir) not in sys.path:
sys.path.insert(0, str(rpa_dir))
module_path = rpa_dir / "common" / "rpa_heartbeat.py"
spec = importlib.util.spec_from_file_location("freight_rpa_heartbeat_test", module_path)
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def test_build_heartbeat_payload_includes_shared_scope_id(monkeypatch):
module = _load_freight_rpa_heartbeat_module()
monkeypatch.delenv("FREIGHT_RPA_WORKER_ID", raising=False)
monkeypatch.setenv("FREIGHT_RPA_SHARED_SCOPE_ID", "hxops-rpa-test-data")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\onhand_triggers",
drop_dir=r"C:\HxopsRpaTest\data\onhand_drop",
)
assert payload["shared_scope_id"] == "hxops-rpa-test-data"
assert payload["trigger_dir"] == r"C:\HxopsRpaTest\data\onhand_triggers"
assert payload["capabilities"] == ["onhand_export"]
def test_build_heartbeat_payload_inbound_worker_declares_inbound_export(monkeypatch):
"""inbound Worker(*-inbound 或 inbound trigger 路径)须声明 inbound_export。"""
module = _load_freight_rpa_heartbeat_module()
monkeypatch.setenv("FREIGHT_RPA_WORKER_ID", "WIN-RPA-01-inbound")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\freight_rpa\inbound\triggers",
drop_dir=r"C:\HxopsRpaTest\data\freight_rpa\inbound\drop",
)
assert payload["capabilities"] == ["inbound_export"]
assert payload["worker_id"].endswith("-inbound")
def test_build_heartbeat_payload_outbound_worker_declares_outbound_export(monkeypatch):
"""outbound Worker(*-outbound 或 outbound trigger 路径)须声明 outbound_export。"""
module = _load_freight_rpa_heartbeat_module()
monkeypatch.setenv("FREIGHT_RPA_WORKER_ID", "WIN-RPA-01-outbound")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\freight_rpa\outbound\triggers",
drop_dir=r"C:\HxopsRpaTest\data\freight_rpa\outbound\drop",
)
assert payload["capabilities"] == ["outbound_export"]
assert payload["worker_id"].endswith("-outbound")
def test_build_heartbeat_payload_adds_freight_rpa_upload_when_upload_enabled(monkeypatch):
"""开启上传时心跳能力是 freight_rpa_upload,不再上报 freight_upload。"""
module = _load_freight_rpa_heartbeat_module()
monkeypatch.setenv("FREIGHT_RPA_WORKER_UPLOAD_ENABLED", "true")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\onhand_triggers",
drop_dir=r"C:\HxopsRpaTest\data\onhand_drop",
)
assert payload["capabilities"] == ["onhand_export", "freight_rpa_upload"]
def test_resolve_shared_scope_id_from_trigger_path(monkeypatch):
module = _load_freight_rpa_heartbeat_module()
monkeypatch.delenv("FREIGHT_RPA_SHARED_SCOPE_ID", raising=False)
monkeypatch.delenv("FREIGHT_RPA_SHARED_SCOPE_ID", raising=False)
monkeypatch.setenv(
"FREIGHT_RPA_SHARED_ONHAND_TRIGGER_DIR",
r"C:\HxopsRpaTest\data\onhand_triggers",
)
assert module._resolve_shared_scope_id() == ""
monkeypatch.setenv(
"FREIGHT_RPA_SHARED_ONHAND_TRIGGER_DIR",
r"C:\mount\hxops-rpa-test-data\onhand_triggers",
)
assert module._resolve_shared_scope_id() == "hxops-rpa-test-data"
def test_write_shared_mount_sentinel_writes_scope_file(tmp_path, monkeypatch):
module = _load_freight_rpa_heartbeat_module()
trigger_dir = tmp_path / "onhand_triggers"
trigger_dir.mkdir()
monkeypatch.setenv("FREIGHT_RPA_SHARED_SCOPE_ID", "hxops-rpa-test-data")
module.write_shared_mount_sentinel(trigger_dir=trigger_dir, hostname="WIN-RPA-01")
sentinel = trigger_dir / ".hxops_rpa_shared_mount"
assert sentinel.is_file()
content = sentinel.read_text(encoding="utf-8")
assert "scope_id=hxops-rpa-test-data" in content
assert "host=WIN-RPA-01" in content
def test_maybe_send_heartbeat_does_not_block_trigger_loop_when_http_stalls(tmp_path, monkeypatch):
"""断言底层 HTTP 卡住时,主 Worker 仍能立即继续扫描 Trigger。"""
module = _load_freight_rpa_heartbeat_module()
release_request = threading.Event()
request_started = threading.Event()
calls: list[dict] = []
def stalled_post(payload):
calls.append(payload)
request_started.set()
release_request.wait(timeout=2)
return False
monkeypatch.setattr(module, "post_heartbeat", stalled_post)
monkeypatch.setenv("FREIGHT_RPA_WORKER_HEARTBEAT_SECONDS", "15")
started = time.monotonic()
sent_at = module.maybe_send_heartbeat(
trigger_dir=tmp_path,
drop_dir=tmp_path,
last_sent_mono=0.0,
force=True,
)
# 核心断言:投递而非等待卡住的 HTTP 请求,扫描循环可立即继续。
assert time.monotonic() - started < 0.2
assert sent_at > 0
assert request_started.wait(timeout=0.5)
module.maybe_send_heartbeat(
trigger_dir=tmp_path,
drop_dir=tmp_path,
last_sent_mono=0.0,
force=True,
)
# 同一时刻只允许一个后台请求,避免后端异常时线程无限累积。
assert len(calls) == 1
release_request.set()
assert module._HEARTBEAT_THREAD is not None
module._HEARTBEAT_THREAD.join(timeout=1)