diff --git a/cmake/Frontends.cmake b/cmake/Frontends.cmake index e47802ba..e8b4d304 100644 --- a/cmake/Frontends.cmake +++ b/cmake/Frontends.cmake @@ -435,7 +435,7 @@ function(webfront_add_browser_tests target frontend assets_directory lifecycle_t if(WEBFRONT_FRONTEND_${frontend}_XVFB_LINUX AND CMAKE_SYSTEM_NAME STREQUAL "Linux") set(lifecycle_command "${XVFB_RUN_EXECUTABLE}" -a ${lifecycle_command}) endif() - set(lifecycle_modes no-bridge early-close rejected-ready navigation-failure path-encoding content-policy navigation-allow-list view-association) + set(lifecycle_modes no-bridge early-close rejected-ready navigation-failure path-encoding content-policy navigation-allow-list view-association stage-report) list(APPEND lifecycle_modes ui-executor native-window second-open) foreach(mode ${lifecycle_modes}) set(lifecycle_name "${frontend}Lifecycle-${mode}") diff --git a/docs/TEST_TIERS.md b/docs/TEST_TIERS.md index a1cc42d3..47fbfb0f 100644 --- a/docs/TEST_TIERS.md +++ b/docs/TEST_TIERS.md @@ -83,3 +83,13 @@ protection rules of `master`; the workflow cannot set that itself. The fast tier does not replace the others: the unit tests are also part of the standard run, so a pull request still reports one complete result. + +The shared frontend lifecycle runner has a 45-second watchdog which reports the last observed stage and exits with a failure +status when the interface thread cannot be unwound. Forced process exit does not itself terminate browser subprocesses; they +can remain orphaned until the surrounding CI runner or operating system cleans them up. Do not kill the process group from +the watchdog: a shared Xvfb display and other tests may belong to that group. The 45-second deadline can preempt the +more specific errors of scenarios that make multiple successive 30-second waits. Forced exit also loses the process's +unflushed gcov coverage data. Page stage reports are best effort and can be lost during navigation or disconnection. + +The `FrontendLifecycleWatchdog` probe bypasses frontend initialization to test the deadline without a display. The ordinary +`CEFLifecycle-*` CTest cases exercise CEF subprocess dispatch and frontend initialization before the watchdog is started. diff --git a/test/cmake/CheckFrontendGenerators.cmake b/test/cmake/CheckFrontendGenerators.cmake index 435935e2..10f3e680 100644 --- a/test/cmake/CheckFrontendGenerators.cmake +++ b/test/cmake/CheckFrontendGenerators.cmake @@ -20,7 +20,7 @@ foreach(generator Ninja "Ninja Multi-Config" Ninja) endif() webfront_read_frontend_tests("${directory}" tests) string(JSON count LENGTH "${tests}" tests) - if(NOT count EQUAL 13) - message(FATAL_ERROR "${generator}: expected Jasmine, eleven shared lifecycle cases and the engine test, got ${count}") + if(NOT count EQUAL 14) + message(FATAL_ERROR "${generator}: expected Jasmine, twelve shared lifecycle cases and the engine test, got ${count}") endif() endforeach() diff --git a/test/cmake/CheckFrontendSelection.cmake b/test/cmake/CheckFrontendSelection.cmake index d4400996..99d274f8 100644 --- a/test/cmake/CheckFrontendSelection.cmake +++ b/test/cmake/CheckFrontendSelection.cmake @@ -437,9 +437,9 @@ foreach(case webkitgtk-display webkitgtk-linux cef-linux cef-macos cef-windows c continue() endif() if(case STREQUAL "extension" OR case STREQUAL "webview2-windows") - set(expected_count 13) + set(expected_count 14) else() - set(expected_count 12) + set(expected_count 13) endif() if(NOT count EQUAL expected_count) list(APPEND failures "${case}: expected ${expected_count} tests, got ${count}") diff --git a/test/cmake/CheckLifecycleStageValues.cmake b/test/cmake/CheckLifecycleStageValues.cmake new file mode 100644 index 00000000..92de73d0 --- /dev/null +++ b/test/cmake/CheckLifecycleStageValues.cmake @@ -0,0 +1,26 @@ +file(READ "${SOURCE_DIR}/test/web/FrontendLifecycle.cpp" cpp) +foreach(page view-association.html content-policy.html policy-escaped.html lifecycle-stage-probe.html) + file(READ "${SOURCE_DIR}/test/web/${page}" html) + string(REGEX MATCH "const stage = \\{[^}]+\\}" declaration "${html}") + if(NOT declaration) + message(FATAL_ERROR "Missing stage declaration in ${page}") + endif() + string(REGEX MATCHALL "[A-Za-z][A-Za-z0-9]*: *[0-9]+" entries "${declaration}") + foreach(entry IN LISTS entries) + string(REGEX REPLACE "^([A-Za-z][A-Za-z0-9]*): *([0-9]+)$" "\\1" name "${entry}") + string(REGEX REPLACE "^([A-Za-z][A-Za-z0-9]*): *([0-9]+)$" "\\2" value "${entry}") + if(NOT cpp MATCHES "(^|[\n\r])[ \t]*${name}[ \t]*=[ \t]*${value}[ \t]*,") + message(FATAL_ERROR "${page}: ${entry} does not match LifecycleStage in FrontendLifecycle.cpp") + endif() + endforeach() + if(NOT entries) + message(FATAL_ERROR "No stage values found in ${page}") + endif() + string(REGEX MATCHALL "stage\\.[A-Za-z][A-Za-z0-9]*" references "${html}") + foreach(reference IN LISTS references) + string(REGEX REPLACE "^stage\\." "" name "${reference}") + if(NOT declaration MATCHES "(^|[{,])[ \t]*${name}[ \t]*:") + message(FATAL_ERROR "${page}: ${reference} has no declared value") + endif() + endforeach() +endforeach() diff --git a/test/cmake/CheckLifecycleWatchdog.cmake b/test/cmake/CheckLifecycleWatchdog.cmake new file mode 100644 index 00000000..74d6de29 --- /dev/null +++ b/test/cmake/CheckLifecycleWatchdog.cmake @@ -0,0 +1,9 @@ +execute_process(COMMAND "${PROBE}" watchdog-probe RESULT_VARIABLE result ERROR_VARIABLE error TIMEOUT 7) +if(NOT result STREQUAL "1" OR NOT error MATCHES "Lifecycle timeout at stage: association: waiting for second WebSocket") + message(FATAL_ERROR "Watchdog did not report the stalled stage and fail: result=${result}, stderr=${error}") +endif() + +execute_process(COMMAND "${PROBE}" watchdog-uninstrumented-probe RESULT_VARIABLE result ERROR_VARIABLE error TIMEOUT 7) +if(NOT result STREQUAL "1" OR NOT error MATCHES "Lifecycle timeout at stage: uninstrumented mode or frontend initialization") + message(FATAL_ERROR "Watchdog did not report an uninstrumented mode: result=${result}, stderr=${error}") +endif() diff --git a/test/cmake/frontends/CMakeLists.txt b/test/cmake/frontends/CMakeLists.txt index 00491096..505bbf4c 100644 --- a/test/cmake/frontends/CMakeLists.txt +++ b/test/cmake/frontends/CMakeLists.txt @@ -88,7 +88,7 @@ elseif(CASE STREQUAL "webkitgtk-linux") include("${ROOT}/test/cmake/WebKitGTKTestEnvironment.cmake") function(webfront_add_webkitgtk_tests) list(LENGTH ARGN shared_count) - if(NOT shared_count EQUAL 12 OR NOT "WebFrontWebKitGTKIntegration" IN_LIST ARGN) + if(NOT shared_count EQUAL 13 OR NOT "WebFrontWebKitGTKIntegration" IN_LIST ARGN) message(FATAL_ERROR "WebKitGTK setup must receive every shared test, including Jasmine") endif() foreach(test IN LISTS ARGN) @@ -176,7 +176,7 @@ if(CASE STREQUAL "extension" AND NOT TEST EngineLifecycle) message(FATAL_ERROR "The new engine's lifecycle tests were not registered") endif() -foreach(mode no-bridge early-close rejected-ready navigation-failure path-encoding view-association) +foreach(mode no-bridge early-close rejected-ready navigation-failure path-encoding view-association stage-report) set(lifecycle_name "${frontend}Lifecycle-${mode}") if(NOT TEST ${lifecycle_name}) message(FATAL_ERROR "Missing shared lifecycle case ${lifecycle_name}") diff --git a/test/web/CMakeLists.txt b/test/web/CMakeLists.txt index 7f3694ca..8c7ebc7c 100644 --- a/test/web/CMakeLists.txt +++ b/test/web/CMakeLists.txt @@ -20,8 +20,16 @@ if(${WEBFRONT_FRONTEND_${WEBFRONT_WEBTEST_FRONTEND}_OPTION} target_compile_definitions(frontend_lifecycle PRIVATE WEBFRONT_TEST_FRONTEND=${WEBFRONT_WEBTEST_FRONTEND_TYPE}) webfront_setup_frontends_for_target(frontend_lifecycle) add_dependencies(webtest frontend_lifecycle) + add_test(NAME FrontendLifecycleWatchdog COMMAND "${CMAKE_COMMAND}" + "-DPROBE=$" -P "${PROJECT_SOURCE_DIR}/test/cmake/CheckLifecycleWatchdog.cmake") + set_tests_properties(FrontendLifecycleWatchdog PROPERTIES TIMEOUT 10 LABELS fast) endif() +# Keep the stage values in the HTML probes synchronized with the C++ enum, including in CEF-off builds. +add_test(NAME FrontendLifecycleStageValues COMMAND "${CMAKE_COMMAND}" "-DSOURCE_DIR=${PROJECT_SOURCE_DIR}" + -P "${PROJECT_SOURCE_DIR}/test/cmake/CheckLifecycleStageValues.cmake") +set_tests_properties(FrontendLifecycleStageValues PROPERTIES TIMEOUT 10 LABELS fast) + # Manual validation of the interface-thread executor and the native window (#450) with a real browser, window and native # dialog: never built or run by CTest/CI. See docs/validation/native-integration-450.md. add_executable(native_integration_manual EXCLUDE_FROM_ALL NativeIntegrationManual.cpp) diff --git a/test/web/FrontendLifecycle.cpp b/test/web/FrontendLifecycle.cpp index 5e44da83..93b9e8ea 100644 --- a/test/web/FrontendLifecycle.cpp +++ b/test/web/FrontendLifecycle.cpp @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include #include @@ -26,6 +28,72 @@ using TestFS = webfront::fs::Multi>; static_assert(Frontend::action == webfront::frontend::Action::closeServerAfterOpen); +// Values reported by the lifecycle pages; keep their named constants in sync with this enum. +enum class LifecycleStage : std::int32_t { + uninstrumented = 0, + bridgeConnected = 1, + associationOrigins = 2, + associationSecondSocket = 3, + associationRegistration = 4, + policyPopup = 5, + policyNavigation = 6, + policyDestination = 7, + pageCompleted = 8, + windowClosed = 9, + startingInstrumented = 10, +}; +std::atomic lifecycleStage = LifecycleStage::uninstrumented; +constexpr const char* stageName(LifecycleStage stage) { + switch (stage) { + case LifecycleStage::uninstrumented: return "uninstrumented mode or frontend initialization"; + case LifecycleStage::bridgeConnected: return "bridge connected"; + case LifecycleStage::associationOrigins: return "association: checking origins"; + case LifecycleStage::associationSecondSocket: return "association: waiting for second WebSocket"; + case LifecycleStage::associationRegistration: return "association: waiting for second connection registration"; + case LifecycleStage::policyPopup: return "policy: bridge ready, opening popup"; + case LifecycleStage::policyNavigation: return "policy: waiting for navigation or cancellation"; + case LifecycleStage::policyDestination: return "policy: destination bridge ready"; + case LifecycleStage::pageCompleted: return "page reported completion, waiting for window close"; + case LifecycleStage::windowClosed: return "window closed, checking results or shutting down"; + case LifecycleStage::startingInstrumented: return "starting frontend or waiting for first bridge connection"; + } + return "unknown stage"; +} + +// A stuck UI loop or a page which never settles must fail before CTest's outer timeout. The watchdog +// terminates the process because the owning thread cannot safely be unwound; EXIT_FAILURE makes CTest fail. +class LifecycleWatchdog { + std::mutex mutex; + std::condition_variable changed; + bool finished = false; + std::thread worker; +public: + explicit LifecycleWatchdog(std::chrono::seconds timeout) : worker([this, timeout] { + std::unique_lock lock{mutex}; + if (!changed.wait_for(lock, timeout, [this] { return finished; })) { + std::cerr << "Lifecycle timeout at stage: " << stageName(lifecycleStage.load()) << std::endl; + std::_Exit(EXIT_FAILURE); + } + }) { } + ~LifecycleWatchdog() { + { + std::lock_guard lock{mutex}; + finished = true; + } + changed.notify_one(); + worker.join(); + } +}; + +template +void registerLifecycleStage(Application& app) { + app.cppFunction("lifecycleStage", [](std::int32_t stage) { + if (stage >= static_cast(LifecycleStage::associationOrigins) + && stage <= static_cast(LifecycleStage::pageCompleted)) + lifecycleStage = static_cast(stage); + }); +} + int show(std::string_view page) { App app(webfront::WebFrontConfig{.documentRoot = std::filesystem::current_path()}); std::atomic connected = false; @@ -46,8 +114,12 @@ int checkViewAssociation() { std::vector embedded; std::vector local; std::atomic originsChecked = false; + lifecycleStage = LifecycleStage::startingInstrumented; + registerLifecycleStage(app); app.onUIStarted([&](App::UI ui) { std::scoped_lock lock{mutex}; + if (embedded.empty()) + lifecycleStage = LifecycleStage::bridgeConnected; embedded.push_back(ui.isEmbeddedView()); local.push_back(ui.isLocalClient()); }); @@ -60,6 +132,7 @@ int checkViewAssociation() { }); app.cppFunction("originsChecked", [&] { originsChecked = true; }); app.show("view-association.html"); + lifecycleStage = LifecycleStage::windowClosed; if (!originsChecked) throw std::runtime_error("The view association script did not match the browser's serialized IPv6 origins"); std::scoped_lock lock{mutex}; @@ -72,6 +145,20 @@ int checkViewAssociation() { return 0; } +// Exercise a page stage report through the real bridge without making the other scenarios depend on telemetry. +int checkStageReport() { + App app(webfront::WebFrontConfig{.documentRoot = std::filesystem::current_path()}); + registerLifecycleStage(app); + std::atomic observed = false; + app.cppFunction("stageProbeFinished", [&] { + observed = lifecycleStage == LifecycleStage::associationOrigins; + }); + app.show("lifecycle-stage-probe.html"); + if (!observed) + throw std::runtime_error("The page stage report did not reach the lifecycle runner"); + return 0; +} + // What the interface thread's executor reports for one operation. enum class Outcome { ran, stopped, unavailable, failed }; @@ -442,19 +529,29 @@ int checkContentPolicy(bool allowNavigation) { policy.navigationAllowedOrigins = {"http://localhost:9002"}; using PolicyApp = webfront::BasicWFWithFrontend; PolicyApp app(webfront::WebFrontConfig{.documentRoot = std::filesystem::current_path(), .contentPolicy = policy}); + lifecycleStage = LifecycleStage::startingInstrumented; + registerLifecycleStage(app); + app.onUIStarted([](PolicyApp::UI) { + auto initial = LifecycleStage::startingInstrumented; + lifecycleStage.compare_exchange_strong(initial, LifecycleStage::bridgeConnected); + }); std::atomic completed = false; std::atomic escaped = false; std::atomic navigated = false; app.cppFunction("policyCompleted", [&] { + lifecycleStage = LifecycleStage::pageCompleted; completed = true; }); app.cppFunction("policyEscaped", [&] { + lifecycleStage = LifecycleStage::pageCompleted; escaped = true; }); app.cppFunction("policyNavigated", [&] { + lifecycleStage = LifecycleStage::pageCompleted; navigated = true; }); app.show("content-policy.html"); + lifecycleStage = LifecycleStage::windowClosed; if (escaped) throw std::runtime_error("Frontend allowed an unmanaged popup"); if (completed == allowNavigation) @@ -501,6 +598,7 @@ int run(std::string_view mode) { cases.emplace("native-window", checkNativeWindow); cases.emplace("second-open", checkSecondOpen); cases.emplace("view-association", checkViewAssociation); + cases.emplace("stage-report", checkStageReport); const auto selected = cases.find(mode); if (selected == cases.end()) throw std::runtime_error("Unknown lifecycle scenario"); @@ -511,8 +609,21 @@ int run(std::string_view mode) { int main(int argc, char** argv) { try { - // CEF subprocesses have their own argument list and must dispatch before mode parsing. + if (argc == 2 && (std::string_view(argv[1]) == "watchdog-probe" || std::string_view(argv[1]) == "watchdog-uninstrumented-probe")) { + LifecycleWatchdog watchdog(std::chrono::seconds(1)); + if (std::string_view(argv[1]) == "watchdog-probe") + lifecycleStage = LifecycleStage::associationSecondSocket; + std::this_thread::sleep_for(std::chrono::seconds(5)); + return 0; + } +#ifdef WEBFRONT_EMBED_CEF + // CEF subprocesses must dispatch before starting any test threads. webfront::detail::ensureFrontendInitialized(); +#endif + LifecycleWatchdog watchdog(std::chrono::seconds(45)); +#ifndef WEBFRONT_EMBED_CEF + webfront::detail::ensureFrontendInitialized(); +#endif return argc == 2 ? run(argv[1]) : 1; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; diff --git a/test/web/content-policy.html b/test/web/content-policy.html index 3d3ed8d0..b0035db9 100644 --- a/test/web/content-policy.html +++ b/test/web/content-policy.html @@ -1,7 +1,13 @@ diff --git a/test/web/lifecycle-stage-probe.html b/test/web/lifecycle-stage-probe.html new file mode 100644 index 00000000..dcee793e --- /dev/null +++ b/test/web/lifecycle-stage-probe.html @@ -0,0 +1,14 @@ + + + diff --git a/test/web/policy-escaped.html b/test/web/policy-escaped.html index d190ebee..c96c5537 100644 --- a/test/web/policy-escaped.html +++ b/test/web/policy-escaped.html @@ -1,8 +1,14 @@