From a0db07311bf94608bd3b892270334ad5e3a93088 Mon Sep 17 00:00:00 2001 From: SoftFever <103989404+SoftFever@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:38:37 +0800 Subject: [PATCH] Fix two unit tests that failed at random on CI The plugin-unload test of the automation suite and the in-flight tool call test of the Python script runner each failed now and then on a busy runner, macOS arm64 and Linux aarch64 in the last run, with nothing wrong in the app. Both pass however the runner schedules their threads. --- tests/automation/test_mcp_core.cpp | 6 +++++- tests/slic3rutils/test_python_script_runner.cpp | 6 ++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/automation/test_mcp_core.cpp b/tests/automation/test_mcp_core.cpp index 661f560f31..42016032c8 100644 --- a/tests/automation/test_mcp_core.cpp +++ b/tests/automation/test_mcp_core.cpp @@ -1672,6 +1672,7 @@ TEST_CASE("a plugin's calls are refused once it unloads, before the block comes, { Harness h; std::atomic loaded{true}; + std::atomic started{false}; std::atomic release{false}; std::atomic looks{0}; ToolResult inline_child; @@ -1683,6 +1684,7 @@ TEST_CASE("a plugin's calls are refused once it unloads, before the block comes, h.registry.add(look); h.registry.add(tool("make", GuardClass::Mutate, [](const json&, ToolContext&) { return ToolResult::ok(); })); h.registry.add(tool("slow", GuardClass::Mutate, [&](const json&, ToolContext& ctx) { + started = true; while (!release) std::this_thread::sleep_for(std::chrono::milliseconds(5)); inline_child = ctx.call_child("look", json::object()); @@ -1691,8 +1693,10 @@ TEST_CASE("a plugin's calls are refused once it unloads, before the block comes, })); h.timing.soft_budget = std::chrono::milliseconds(10000); h.start(); + // A call that is running when the plugin unloads. Its job is listed before its handler starts, and an unload + // between the two refuses the call itself, so the test waits for its handler. std::thread running([&] { h.core->call_plugin(*h.registry.find("slow"), json::object(), "measure"); }); - while (h.jobs.running_owned_by("measure").empty()) + while (!started) std::this_thread::sleep_for(std::chrono::milliseconds(5)); loaded = false; diff --git a/tests/slic3rutils/test_python_script_runner.cpp b/tests/slic3rutils/test_python_script_runner.cpp index 2309be227b..0911c310bb 100644 --- a/tests/slic3rutils/test_python_script_runner.cpp +++ b/tests/slic3rutils/test_python_script_runner.cpp @@ -973,7 +973,7 @@ TEST_CASE("a tool call in flight when the run ends finishes before the run does" if (!harness.initialized) SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error()); - SharedModule shared("_late", {"in_call"}); + SharedModule shared("_late", {"in_call", "done"}); FakeBridge bridge; bridge.hook = [&](const std::string&, const json&) -> std::optional { shared.set("in_call"); @@ -983,10 +983,12 @@ TEST_CASE("a tool call in flight when the run ends finishes before the run does" ScopedToolBridge bound(bridge); const auto start = std::chrono::steady_clock::now(); REQUIRE(run("import orca, threading, _late\n" - "def work(orca=orca, late=_late):\n late.result = orca.host.tools.call('app_state')\n" + "def work(orca=orca, late=_late):\n late.result = orca.host.tools.call('app_state')\n late.done.set()\n" "threading.Thread(target=work, daemon=True).start()\n_late.in_call.wait(10)\n") .status == "finished"); CHECK(std::chrono::steady_clock::now() - start >= std::chrono::seconds(1)); + // The run waits for the call, not for the daemon thread that made it, which keeps the result once it runs again. + REQUIRE(shared.wait("done", 5)); CHECK(shared.get("result") == json{{"ready", true}}); }