Merge branch 'hugo-auto-transfer' into dev
This commit is contained in:
commit
a3078a4415
11 files changed
+225
-26
No files matched your search
@@ -205,3 +205,66 @@ def test_receipt_trigger_is_published_atomically(app, tmp_path):
|
||||
assert path
|
||||
assert (tmp_path / 'receipt_export_batch_RUN-ATOMIC.json').exists()
|
||||
assert not list(tmp_path.glob('.*.tmp'))
|
||||
|
||||
|
||||
def test_stale_receipt_full_scan_recovers_without_task_trigger_or_restart(app):
|
||||
"""全量扫描(每轮 tick 路径)无需任务再次执行/进程重启即可收口超时收料 run。"""
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptRpaUnitRun
|
||||
from app.services.schedule.freight_receipt_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
|
||||
|
||||
with app.app_context():
|
||||
app.config['FREIGHT_RPA_RECEIPT_TIMEOUT_SECONDS'] = 60
|
||||
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
task = ScheduleTask(schedule_no='SCH-RECEIPT-ORPHAN', name='Receipt orphan', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
|
||||
db.session.add(task); db.session.flush()
|
||||
run = ScheduleRun(run_no='RUN-RECEIPT-ORPHAN', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=5), response_summary_json={'awaiting_receipt_rpa': True})
|
||||
db.session.add(run); db.session.flush()
|
||||
db.session.add(ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-orphan', status='awaiting_rpa', config_snapshot_json={}))
|
||||
db.session.commit()
|
||||
|
||||
# [断言意图] 全量扫描不依赖 task 入参、进程重启或同排程再次执行(点B 修复口径);
|
||||
# 超时窗(60s)过后扫描即收口为 failed 并写 recovered_reason。
|
||||
assert recover_all_stale_receipt_rpa_schedule_runs() == 1
|
||||
db.session.refresh(run)
|
||||
assert run.status == 'failed'
|
||||
assert run.response_summary_json.get('recovered_reason') == 'receipt_rpa_callback_timeout'
|
||||
|
||||
|
||||
def test_receipt_recovery_respects_fresh_activity_of_long_batch(app):
|
||||
"""长批次健康在跑不得被误杀:超时锚取最后单元活动而非 run 开始时间。"""
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptRpaUnitRun
|
||||
from app.services.schedule.freight_receipt_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
|
||||
|
||||
with app.app_context():
|
||||
app.config['FREIGHT_RPA_RECEIPT_TIMEOUT_SECONDS'] = 60
|
||||
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
task = ScheduleTask(schedule_no='SCH-RECEIPT-LONG', name='Receipt long batch', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
|
||||
db.session.add(task); db.session.flush()
|
||||
run = ScheduleRun(run_no='RUN-RECEIPT-LONG', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=30), response_summary_json={'awaiting_receipt_rpa': True})
|
||||
db.session.add(run); db.session.flush()
|
||||
# 单元1:30 秒前终态(笔间活动新鲜);单元2:45 秒前进入 processing(在飞,started_at 为锚)
|
||||
db.session.add(ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-long-done', status='success', config_snapshot_json={}, finished_at=datetime.utcnow() - timedelta(seconds=30)))
|
||||
db.session.add(ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='B', callback_id='receipt-long-run', status='processing', config_snapshot_json={}, started_at=datetime.utcnow() - timedelta(seconds=45)))
|
||||
db.session.commit()
|
||||
|
||||
# [断言意图] run.started_at(30min 前) 已远超 60s 窗,但最后活动仅 30~45s——
|
||||
# 笔间静默口径下健康长批次必须不被回收(旧口径按 started_at 会误杀)。
|
||||
assert recover_all_stale_receipt_rpa_schedule_runs() == 0
|
||||
db.session.refresh(run)
|
||||
assert run.status == 'running'
|
||||
|
||||
|
||||
def test_receipt_recovery_is_wired_into_per_tick_maintenance_not_startup_only():
|
||||
"""哨兵回归:收料超时对账必须挂在每轮领取维护区,不得回流启动一次性分支。"""
|
||||
import inspect
|
||||
|
||||
from app.services.schedule import queue_service
|
||||
|
||||
source = inspect.getsource(queue_service)
|
||||
marker_init = source.index("_worker_initialized = True")
|
||||
# [断言意图] 调用点必须位于一次性分支竖旗之后(每轮维护区);
|
||||
# 回流旧写法(receipt_recovered = ...)会让长驻进程永不回收(RUN20261001131705E5E5 根因)。
|
||||
assert source.index("receipt_stale = recover_all_stale_receipt_rpa_schedule_runs()") > marker_init
|
||||
assert "receipt_recovered = recover_all_stale_receipt_rpa_schedule_runs()" not in source
|
||||
@@ -203,7 +203,7 @@ def test_assert_inventory_rpa_worker_ready_includes_reason_details(app, tmp_path
|
||||
|
||||
|
||||
def test_assert_receipt_rpa_worker_ready_includes_reason_details(app, tmp_path, monkeypatch):
|
||||
"""收料 fail-fast 应附带 reasons,且不要求共享哨兵。"""
|
||||
"""收料 fail-fast 应附带 reasons,且与库存同口径要求共享哨兵。"""
|
||||
from app.core.errors import ServiceUnavailableError
|
||||
from app.services.shipping_notice import rpa_worker_heartbeat as heartbeat_module
|
||||
from app.services.shipping_notice.rpa_worker_heartbeat import (
|
||||
@@ -225,11 +225,12 @@ def test_assert_receipt_rpa_worker_ready_includes_reason_details(app, tmp_path,
|
||||
with app.app_context():
|
||||
with pytest.raises(ServiceUnavailableError) as exc_info:
|
||||
assert_receipt_rpa_worker_ready()
|
||||
# [断言意图] 与库存同构的用户可见文案与 reasons;收料不因缺哨兵失败。
|
||||
# [断言意图] 与库存同构的用户可见文案与 reasons;缺哨兵与无心跳同时暴露,
|
||||
# 对应部署侧必须由 ps1 注入 FREIGHT_RPA_SHARED_SCOPE_ID 写哨兵。
|
||||
assert exc_info.value.message == RPA_WORKER_OFFLINE_MESSAGE
|
||||
reasons = (exc_info.value.details or {}).get('reasons', [])
|
||||
assert 'no_worker_heartbeat' in reasons
|
||||
assert 'shared_sentinel_missing' not in reasons
|
||||
assert 'shared_sentinel_missing' in reasons
|
||||
|
||||
|
||||
def test_check_inventory_rpa_worker_ready_rejects_stale_heartbeat(app, tmp_path, monkeypatch):
|
||||
|
||||
Reference in new issue
Block a user