diff --git a/.github/workflows/BuildAndTest.yml b/.github/workflows/BuildAndTest.yml index 8049ae9c..045db2cb 100644 --- a/.github/workflows/BuildAndTest.yml +++ b/.github/workflows/BuildAndTest.yml @@ -141,6 +141,7 @@ jobs: sccache --version # ENABLE_CACHE controls automatic ccache discovery; the explicit sccache launcher stays enabled. # The heavy unit tests are compiled four to a file (docs/BUILD_TIME_STUDY.md); the portability and sanitizer jobs keep one object per test file. + # The unit tests run as four Catch2 shards instead of one process per scenario (docs/TEST_TIERS.md). - name: Configure run: >- cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} @@ -148,6 +149,7 @@ jobs: -DENABLE_CACHE=OFF -DCMAKE_CXX_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_SCAN_FOR_MODULES=OFF -DWEBFRONT_TEST_UNITY_BATCH=4 + -DWEBFRONT_TEST_UNIT_SHARDS=4 -DWEBFRONT_TEST_REPRODUCIBILITY=${{ env.WEBFRONT_REPRODUCIBILITY }} ${{ env.FULL_TIER == 'true' && '' || '-DWEBFRONT_MSVC_ANALYZE=OFF -DWEBFRONT_BUILD_EXAMPLES=OFF' }} ${{ matrix.options }} @@ -183,18 +185,21 @@ jobs: # The fast tier (docs/TEST_TIERS.md): unit tests only, so a plain regression is reported before the browser suite starts. - name: Fast tests shell: bash - run: ctest --test-dir build -C ${{ matrix.build_type }} -L fast --parallel 4 --output-on-failure --no-tests=error + run: >- + ctest --test-dir build -C ${{ matrix.build_type }} -L fast --parallel 4 --output-on-failure --no-tests=error + --output-junit "${{ github.workspace }}/build/fast-results.xml" - name: Capture CTest dependency graph shell: bash run: ctest --test-dir build --show-only=json-v1 > build/ctest-graph.json - name: Test C++ and native browser integration + # The fast tier has just run: a single pass does not run it again; the three passes of the complete tier do. # Only Linux trusted fixtures need the sandbox opt-out on hosted runners. env: ${{ matrix.test_env }} run: >- python3 test/tools/MeasureCommand.py --output build/suite-resources.json -- cmake -DBUILD_DIR=build -DCONFIG=${{ matrix.build_type }} -DREPEAT=${{ (github.event_name == 'schedule' || (github.event_name == 'pull_request' && github.base_ref == 'master') || (github.event_name == 'workflow_dispatch' && inputs.repeat_desktop_tests)) && '3' || '1' }} - -DLABEL_EXCLUDE=${{ (github.event_name == 'schedule' || (github.event_name == 'pull_request' && github.base_ref == 'master') || (github.event_name == 'workflow_dispatch' && inputs.repeat_desktop_tests)) && 'none' || 'portability' }} + '-DLABEL_EXCLUDE=${{ (github.event_name == 'schedule' || (github.event_name == 'pull_request' && github.base_ref == 'master') || (github.event_name == 'workflow_dispatch' && inputs.repeat_desktop_tests)) && 'none' || '^(portability|fast)$' }}' -P test/cmake/RunTestSuite.cmake - name: Repeat WebKitGTK pointer tests with suite display sharing enabled if: runner.os == 'Linux' @@ -277,6 +282,8 @@ jobs: name: test-log-${{ runner.os }} path: | build/test-results-*.xml + build/fast-results.xml + build/unit-results-*.xml build/ctest-graph.json build/embedded-cache-probe.txt build/test/unit/consumer-build/.ninja_log diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2db31faa..867d81ac 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -13,6 +13,7 @@ these entries stays in the GitHub releases and the Git history. ### Changed - Open the developer page on the interface, over the whole window, once the project has a preview; the terminal moves to a floating, movable window behind a tab on the right edge that shows the session's state, and keeps its session when collapsed. Its shortcut is F9, or the manifest's new optional `developer.terminalShortcut`. Environment sources 0.9.1, not published yet (#539, `docs/DEVELOPER_ENVIRONMENT.md`). +- Run the unit tests as a few processes instead of one per scenario with `-DWEBFRONT_TEST_UNIT_SHARDS=`: each of the `n` CTest cases is a Catch2 shard that reports every scenario in `unit-results-.xml`. The default still registers one CTest case per scenario; the desktop CI jobs pass 4 and no longer run the fast tier twice on a pull request (#543). ### Fixed diff --git a/docs/TEST_TIERS.md b/docs/TEST_TIERS.md index 47fbfb0f..958ef01a 100644 --- a/docs/TEST_TIERS.md +++ b/docs/TEST_TIERS.md @@ -16,8 +16,17 @@ ctest --test-dir build -L portability # the portability tie cmake -DBUILD_DIR=build -DLABEL_EXCLUDE=portability -P test/cmake/RunTestSuite.cmake ``` -`RunTestSuite.cmake` runs every test unless `LABEL_EXCLUDE` names a label; the CI standard tier passes -`portability`. A local run before a change is declared complete (see `AGENTS.md`) runs everything. +`RunTestSuite.cmake` runs every test unless `LABEL_EXCLUDE` gives a regular expression of labels to leave out. The CI +standard tier passes `^(portability|fast)$`, because its fast step has just run the fast tier. A local run before a +change is declared complete (see `AGENTS.md`) runs everything. + +By default every Catch2 scenario is a CTest case of its own, which `ctest -R` selects by name, and a process of its +own: more than 500 of them. `-DWEBFRONT_TEST_UNIT_SHARDS=4`, which the desktop CI jobs pass, registers four cases +`UnitTests-0` to `UnitTests-3` instead, each a Catch2 shard that runs a quarter of the scenarios in one process and +writes the result of each to `unit-results-.xml` in the build directory. The shards run the scenarios in their +declaration order, which is what makes four processes started separately split them without overlap. Scenarios then +share a process: `test/unit/ScenarioIsolation.cpp` restores the process-wide state a scenario may change. Run directly, +`tests` draws a random order and prints its seed, which `tests --rng-seed ` replays. ## Following a browser test live @@ -81,8 +90,8 @@ A pull request into `master` must pass all of: the desktop jobs, `portability` ( leg), `sanitizers` and `web-integration`. GitHub only enforces this once the checks are marked required in the branch 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 fast tier runs once on a pull request: the step that follows it runs everything else, and a failed fast step +fails the job before the browser suite starts. The three passes of the complete tier run the fast tier again each time. 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 diff --git a/test/cmake/CheckTestSuiteRepetition.cmake b/test/cmake/CheckTestSuiteRepetition.cmake index 2d5b07d1..87e1a29c 100644 --- a/test/cmake/CheckTestSuiteRepetition.cmake +++ b/test/cmake/CheckTestSuiteRepetition.cmake @@ -118,15 +118,23 @@ enable_testing() add_test(NAME Standard COMMAND "${CMAKE_COMMAND}" -E true) add_test(NAME Portable COMMAND "${CMAKE_COMMAND}" -E false) set_tests_properties(Portable PROPERTIES LABELS portability) +add_test(NAME Quick COMMAND "${CMAKE_COMMAND}" -E false) +set_tests_properties(Quick PROPERTIES LABELS fast) ]=]) run_bounded(COMMAND "${CMAKE_COMMAND}" -S "${WORK}/labels/source" -B "${WORK}/labels/build") if(NOT result EQUAL 0) message(FATAL_ERROR "Label fixture configure failed: ${output}${error}") endif() +# The exclusion is a regular expression of labels: the standard tier of the CI leaves two of them out. +run_bounded(COMMAND "${CMAKE_COMMAND}" -DBUILD_DIR=${WORK}/labels/build -DCONFIG=Release "-DLABEL_EXCLUDE=^(portability|fast)$" + -P "${ROOT}/test/cmake/RunTestSuite.cmake") +if(NOT result EQUAL 0 OR "${output}${error}" MATCHES "Portable|Quick") + message(FATAL_ERROR "The excluded labels must not run: ${output}${error}") +endif() run_bounded(COMMAND "${CMAKE_COMMAND}" -DBUILD_DIR=${WORK}/labels/build -DCONFIG=Release -DLABEL_EXCLUDE=portability -P "${ROOT}/test/cmake/RunTestSuite.cmake") -if(NOT result EQUAL 0 OR "${output}${error}" MATCHES "Portable") - message(FATAL_ERROR "The excluded label must not run: ${output}${error}") +if(result EQUAL 0 OR "${output}${error}" MATCHES "Portable" OR NOT "${output}${error}" MATCHES "Quick") + message(FATAL_ERROR "Only the excluded label must be left out: ${output}${error}") endif() run_bounded(COMMAND "${CMAKE_COMMAND}" -DBUILD_DIR=${WORK}/labels/build -DCONFIG=Release -P "${ROOT}/test/cmake/RunTestSuite.cmake") diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 236f5564..e5fe5e9d 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -30,7 +30,7 @@ list(APPEND TESTS_LIST FileSystemTests.cpp NativeFSTests.cpp DownloadsTests.cpp list(APPEND TESTS_LIST DefaultFSTests.cpp StdnetShutdownTests.cpp SafePathTests.cpp UISubscribersTests.cpp) list(APPEND TESTS_LIST ReadBindingTests.cpp ViewAssociationTests.cpp BroadcastTests.cpp WebFrontTests.cpp WebLinkTests.cpp CEFPathsTests.cpp CEFMacrosTests.cpp) list(APPEND TESTS_LIST DeveloperTests.cpp WfDriverTests.cpp AutomationTests.cpp AutomationChannelTests.cpp AutomationStdioTests.cpp AutomationDiscoveryTests.cpp JasmineConsumerTests.cpp HostTests.cpp LifeTests.cpp StructuredShutdownTests.cpp JsFunctionSenderTests.cpp ApplicationTaskTests.cpp DeferredCallTests.cpp) -list(APPEND TESTS_LIST FrontendUrlTests.cpp ApplicationUrlTests.cpp FrontendContractTests.cpp UIExecutorTests.cpp NativeWindowTests.cpp CommandsTests.cpp EmbeddedCoreAssetsTests.cpp) +list(APPEND TESTS_LIST ScenarioIsolation.cpp FrontendUrlTests.cpp ApplicationUrlTests.cpp FrontendContractTests.cpp UIExecutorTests.cpp NativeWindowTests.cpp CommandsTests.cpp EmbeddedCoreAssetsTests.cpp) if(WEBFRONT_USE_MDDLOG) list(APPEND TESTS_LIST MddlogLoggerTests.cpp MddlogTransportTests.cpp) endif() @@ -84,7 +84,27 @@ endif() include(${Catch2_SOURCE_DIR}/extras/Catch.cmake) # Labels select the tiers (docs/TEST_TIERS.md): fast unit tests, browser integration, portability across toolchains. -catch_discover_tests(tests PROPERTIES TIMEOUT 60 LABELS fast) +# One CTest case per scenario starts one process per scenario: more than 500, which is most of the run time of the unit +# tests (docs/TEST_PERFORMANCE.md). WEBFRONT_TEST_UNIT_SHARDS runs them as that many processes instead, each a Catch2 +# shard that writes the result of every scenario to a JUnit report. The default keeps one case per scenario, which +# `ctest -R` filters by name. +set(WEBFRONT_TEST_UNIT_SHARDS 0 CACHE STRING "Run the unit tests as this many CTest cases; 0 registers one case per scenario") +if(NOT WEBFRONT_TEST_UNIT_SHARDS MATCHES "^[0-9]+$") + message(FATAL_ERROR "WEBFRONT_TEST_UNIT_SHARDS must be a non-negative integer, not '${WEBFRONT_TEST_UNIT_SHARDS}'") +endif() +if(WEBFRONT_TEST_UNIT_SHARDS GREATER 0) + math(EXPR _last_shard "${WEBFRONT_TEST_UNIT_SHARDS} - 1") + foreach(_shard RANGE ${_last_shard}) + # Catch2 shards the sorted list: the processes must sort alike, which a random order drawn by each would not. + add_test(NAME UnitTests-${_shard} COMMAND tests --order decl --shard-count ${WEBFRONT_TEST_UNIT_SHARDS} --shard-index ${_shard} + --reporter console::out=-::colour-mode=none + --reporter "junit::out=${CMAKE_BINARY_DIR}/unit-results-${_shard}.xml") + # The scenarios of a shard share its time limit; the slowest of them takes a few seconds. + set_tests_properties(UnitTests-${_shard} PROPERTIES TIMEOUT 600 LABELS fast) + endforeach() +else() + catch_discover_tests(tests PROPERTIES TIMEOUT 60 LABELS fast) +endif() # The developer journey type-checks the declaration this tool writes from the hand-described settings (#383). add_executable(forms_declaration FormsDeclaration.cpp) diff --git a/test/unit/ScenarioIsolation.cpp b/test/unit/ScenarioIsolation.cpp new file mode 100644 index 00000000..0833090d --- /dev/null +++ b/test/unit/ScenarioIsolation.cpp @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: Apache-2.0 OR CECILL-B +/// @brief Gives every scenario the process-wide state a process of its own would have, so that the scenarios pass in any +/// order within one process (WEBFRONT_TEST_UNIT_SHARDS, #543). +#include + +#include +#include +#include + +#include + +namespace { + +/// The enabled severities are global: a scenario that raises them makes the next one log, and a connected browser is +/// sent what is logged at the debug level. +class ScenarioIsolation : public Catch::EventListenerBase { +public: + using Catch::EventListenerBase::EventListenerBase; + + void testRunStarting(const Catch::TestRunInfo&) override { + for (std::size_t index = 0; index != severities.size(); ++index) + initial[index] = webfront::log::is(severities[index]); + } + void testCaseStarting(const Catch::TestCaseInfo&) override { + for (std::size_t index = 0; index != severities.size(); ++index) + webfront::log::set(severities[index], initial[index]); + } + +private: + static constexpr std::array severities{webfront::log::Error, webfront::log::Warn, webfront::log::Info, webfront::log::Debug}; + std::array initial{}; +}; + +} // namespace + +CATCH_REGISTER_LISTENER(ScenarioIsolation)