Files
syops/backend/tests/test_freight_rpa_heartbeat.py
T
hugoandCursor ffc0d4a430 feat(freight-rpa): 批次R解耦编排层并切换 onhand/inbound 契约
将 RPA 编排收编至 freight_rpa,回调迁 /api/freight-rpa/*。
共享目录与 Worker 契约一次切换至 onhand/inbound/upload,
并补 outbound 骨架与 tt16 排程 job key 迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-17 23:18:48 +08:00

120 lines
4.4 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_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)