Files
syops/backend/tests/test_freight_rpa_heartbeat.py
T
hugo abf3d48927 feat(in-transit-transfer): 自动调拨联动收料就绪并分层来源错误
- 调度整合:退役 in_transit_receipt_file_job,收料归档收敛 shipping_receipt_rpa_archive,排程注册表统一
- 自动调拨 Job:调拨前确保各来源当日收料文件就绪(缺失自动触发收料 RPA),严格等量匹配后生成 ERP 调拨单
- worker 手动唤醒:排程工作台支持手动唤醒与 runs 透视,heartbeat 协议同步
- 收料缺失幸存 T2:parse_failed 状态落库(迁移 q4c5)
- 来源错误分层与 stale 时钟换源:改挂 inventory_checked_at 库存查得时刻,展示阈值与执行门禁解耦(迁移 t5a6)
- 新增 wecom_sender 企微通知:每非空 Run 汇总一条,通知失败不影响 ERP 结果
- 同步排程工作台/调拨卡片前端、chat/admin 文案四语言、RPA 脚本与测试全家
2026-09-30 00:20:06 +08:00

120 lines
4.5 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\inventory_triggers",
drop_dir=r"C:\HxopsRpaTest\data\inventory_drop",
)
assert payload["shared_scope_id"] == "hxops-rpa-test-data"
assert payload["trigger_dir"] == r"C:\HxopsRpaTest\data\inventory_triggers"
assert payload["capabilities"] == ["inventory_export"]
def test_build_heartbeat_payload_receipt_worker_declares_receipt_export(monkeypatch):
"""收料 Worker(*-receipt 或 receipt trigger 路径)须声明 receipt_export。"""
module = _load_freight_rpa_heartbeat_module()
monkeypatch.setenv("FREIGHT_RPA_WORKER_ID", "WIN-RPA-01-receipt")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\freight_rpa\receipt\triggers",
drop_dir=r"C:\HxopsRpaTest\data\freight_rpa\receipt\drop",
)
assert payload["capabilities"] == ["receipt_export"]
assert payload["worker_id"].endswith("-receipt")
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_INVENTORY_TRIGGER_DIR",
r"C:\HxopsRpaTest\data\inventory_triggers",
)
assert module._resolve_shared_scope_id() == ""
monkeypatch.setenv(
"FREIGHT_RPA_SHARED_INVENTORY_TRIGGER_DIR",
r"C:\mount\hxops-rpa-test-data\inventory_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 / "inventory_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)