mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 16:36:53 +08:00
fix(runtime): 隔离线程任务上下文 (#6420)
* fix(runtime): isolate thread task contexts * test: use canonical message schema import
This commit is contained in:
@@ -5,8 +5,9 @@ import time
|
||||
from types import SimpleNamespace
|
||||
|
||||
from app.chain import workflow as workflow_module
|
||||
from app.schemas import Action, ActionContext, ActionResult
|
||||
from app.runtime.correlation import correlation_scope, get_correlation_id
|
||||
from app.schemas.types import EventType
|
||||
from app.schemas.workflow import Action, ActionContext, ActionResult
|
||||
from app import workflow as workflow_package
|
||||
|
||||
|
||||
@@ -127,6 +128,58 @@ class _OpaqueValue:
|
||||
return "opaque-value"
|
||||
|
||||
|
||||
def test_workflow_executor_preserves_trigger_context(monkeypatch):
|
||||
"""工作流节点及其完成回调应保留触发链路的关联 ID。"""
|
||||
observed = []
|
||||
release = threading.Event()
|
||||
|
||||
def run_action(_action, context):
|
||||
observed.append(("node", get_correlation_id()))
|
||||
assert release.wait(timeout=1)
|
||||
return ActionResult(success=True, message="ok", context=context)
|
||||
|
||||
fake_manager = _FakeWorkflowManager(
|
||||
[],
|
||||
results={"A": run_action},
|
||||
)
|
||||
monkeypatch.setattr(workflow_module, "WorkFlowManager", lambda: fake_manager)
|
||||
monkeypatch.setattr(
|
||||
workflow_module.global_vars,
|
||||
"workflow_resume",
|
||||
lambda _workflow_id: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
workflow_module.global_vars,
|
||||
"is_workflow_stopped",
|
||||
lambda _workflow_id: False,
|
||||
)
|
||||
|
||||
executor = workflow_module.WorkflowExecutor(
|
||||
_build_workflow(
|
||||
actions=[
|
||||
{"id": "A", "type": "FakeAction", "name": "动作A", "data": {}}
|
||||
],
|
||||
flows=[],
|
||||
),
|
||||
step_callback=lambda _action, _context: observed.append(
|
||||
("completion", get_correlation_id())
|
||||
),
|
||||
)
|
||||
timer = threading.Timer(0.05, release.set)
|
||||
try:
|
||||
with correlation_scope("workflow-request"):
|
||||
timer.start()
|
||||
executor.execute()
|
||||
finally:
|
||||
release.set()
|
||||
timer.join(timeout=1)
|
||||
|
||||
assert observed == [
|
||||
("node", "workflow-request"),
|
||||
("completion", "workflow-request"),
|
||||
]
|
||||
|
||||
|
||||
def test_workflow_executor_resumes_downstream_nodes(monkeypatch):
|
||||
"""恢复执行时应释放已完成节点的后继节点。"""
|
||||
calls = []
|
||||
|
||||
Reference in New Issue
Block a user