From 4b74a3f3894992eacd26ec53958031f11d106c48 Mon Sep 17 00:00:00 2001 From: Aaron James Long Date: Wed, 14 Oct 2026 13:03:20 +0100 Subject: [PATCH] ci: gate branch coverage and test every backend The reader retry branches were reached only when a concurrent writer happened to interleave, so branch coverage could not be gated. A single-threaded test now drives each retry deterministically, the no-atomics backend is built and tested alongside C11, and CI gates on 100% branch coverage. The pull_request branch filter is removed so stacked PRs get CI. --- .github/workflows/ci.yml | 11 ++-- CHANGELOG.md | 2 + CONTRIBUTING.md | 13 ++-- tests/meson.build | 45 ++++++++++--- tests/test_seqlock_backend.c | 49 +++++++++++++++ tests/test_seqlock_c11.c | 51 --------------- tests/test_seqlock_retry.c | 118 +++++++++++++++++++++++++++++++++++ 7 files changed, 218 insertions(+), 71 deletions(-) create mode 100644 tests/test_seqlock_backend.c delete mode 100644 tests/test_seqlock_c11.c create mode 100644 tests/test_seqlock_retry.c diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f317c28..6cd5d7f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,6 @@ on: push: branches: ["master","main"] pull_request: - branches: ["master","main"] jobs: test: @@ -86,13 +85,13 @@ jobs: - name: Test run: meson test -C build-cov - # Gate on 100% line coverage of the library sources. Branch coverage is - # reported but not gated: some branches are concurrency-dependent and - # cannot be forced deterministically on every run. - - name: Coverage report (100% line gate) + # Gate on 100% line and branch coverage of the library sources, merged + # across every backend build. The reader retry branches are reached + # deterministically by test_seqlock_retry, not by the concurrent tests. + - name: Coverage report (100% line and branch gate) run: | gcovr --root . --filter 'src/' --filter 'include/' \ - --fail-under-line 100 --print-summary + --fail-under-line 100 --fail-under-branch 100 --print-summary release-build: name: Release build diff --git a/CHANGELOG.md b/CHANGELOG.md index 32e6862..d9333b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Changed +- CI gates on 100% branch coverage as well as line coverage. The reader retry branches are reached deterministically by a single-threaded test, and the no-atomics backend is built and tested alongside the C11 backend. + - Added security policy, SPDX headers, and cleaned ignore rules while leaving the existing special coverage/concurrency handling unchanged. ## [0.1.1] - 2026-06-03 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ac061ef..7eadaa8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -22,12 +22,12 @@ meson setup build-tsan --buildtype=debug -Dbuild_tests=true \ meson compile -C build-tsan meson test -C build-tsan --verbose -# Coverage (CI gates on 100% line coverage of src/ + include/) +# Coverage (CI gates on 100% line and branch coverage of src/ + include/) meson setup build-cov --buildtype=debug -Dbuild_tests=true -Db_coverage=true \ -Dc_args=-fprofile-update=atomic meson compile -C build-cov && meson test -C build-cov gcovr --root . --filter 'src/' --filter 'include/' \ - --fail-under-line 100 --print-summary + --fail-under-line 100 --fail-under-branch 100 --print-summary ``` Run the TSAN build on an x86-64 host (or a 4K-page AArch64). TSAN cannot @@ -119,10 +119,13 @@ have only advisory deviations and want to keep it that way. - The type-erased core (`seqlock_core_*`) is exercised directly by `tests/test_seqlock_core.c`, including its argument-validation paths, which the typed shims cannot reach. New core behaviour needs a test there. -- CI gates on **100% line coverage** of `src/` and `include/`. Branch coverage - is reported but not gated, because some branches are concurrency-dependent - and cannot be forced deterministically every run. New code without tests +- CI gates on **100% line and branch coverage** of `src/` and `include/`, + merged across every backend build. Reach new branches deterministically, + not through the concurrent tests: `tests/test_seqlock_retry.c` shows how + the reader retry paths are driven single-threaded. New code without tests fails the gate. +- Each non-default backend (C11, no-atomics) is built and run by + `tests/test_seqlock_backend.c`. - All tests must pass on every CI configuration: Linux with ASan/UBSan, the dedicated TSAN job, the coverage gate, and the release build. diff --git a/tests/meson.build b/tests/meson.build index 7c2119e..1781bb3 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -37,17 +37,44 @@ test_wrap_exe = executable( ) test('seqlock wrap', test_wrap_exe) -# C11 backend. The default on GCC/Clang is the __atomic path, so -# this forces the C11 backend for both the test and the core (compiled from -# library_sources with the same -D) to exercise that path and its lock-free -# static assertion. -test_c11_exe = executable( - 'test_seqlock_c11', - files('test_seqlock_c11.c') + library_sources, - c_args: ['-DSEQLOCK_USE_C11_ATOMICS=1'], +# Non-default backends. The default on GCC/Clang is the __atomic path, so each +# remaining rung of the ladder is forced for both the test and the core +# (compiled from library_sources with the same -D) to exercise that path and, +# for C11, its lock-free static assertion. +foreach backend : [['c11', 'SEQLOCK_USE_C11_ATOMICS'], ['no_atomics', 'SEQLOCK_USE_NO_ATOMICS']] + test( + 'seqlock ' + backend[0], + executable( + 'test_seqlock_' + backend[0], + files('test_seqlock_backend.c') + library_sources, + c_args: ['-D@0@=1'.format(backend[1])], + include_directories: [public_headers, generated_headers], + ), + ) +endforeach + +# Reader retry branches, reached deterministically so branch coverage can be +# gated. The first build reaches them in the library itself. The second +# replaces SEQLOCK_CPU_RELAX with a test hook to reach the odd-counter retry, +# so its core is compiled from library_sources with the same definition. +test_retry_exe = executable( + 'test_seqlock_retry', + files('test_seqlock_retry.c'), + dependencies: [seqlock_dep], + include_directories: [public_headers, generated_headers], +) +test('seqlock retry', test_retry_exe) + +test_retry_relax_exe = executable( + 'test_seqlock_retry_relax', + files('test_seqlock_retry.c') + library_sources, + c_args: [ + '-DSEQLOCK_TEST_RELAX_HOOK=1', + '-DSEQLOCK_CPU_RELAX()=do { extern void seqlock_test_relax(void); seqlock_test_relax(); } while (0)', + ], include_directories: [public_headers, generated_headers], ) -test('seqlock c11', test_c11_exe) +test('seqlock retry relax', test_retry_relax_exe) # Concurrent torn-read detection + wait-free-writer liveness + large payload. # This is also the executable rebuilt under -Db_sanitize=thread for the diff --git a/tests/test_seqlock_backend.c b/tests/test_seqlock_backend.c new file mode 100644 index 0000000..4b3437c --- /dev/null +++ b/tests/test_seqlock_backend.c @@ -0,0 +1,49 @@ +/* + * @file test_seqlock_backend.c + * @brief Exercises a forced atomic backend. + * + * On GCC/Clang the default backend is the `__atomic` builtin path, so the C11 + * and no-atomics paths would otherwise never be compiled in CI. + * tests/meson.build builds this executable once per non-default backend, + * passing the backend define for both this TU and the core so the separately + * compiled seqlock.c agrees on the backend and ABI. A failure here is most + * likely a compile error: the C11 lock-free assertion rejecting the counter + * type, or a backend's atomic macros not expanding cleanly. + */ + +#include "seqlock.h" +#include "seqlock_test.h" + +#include + +SEQLOCK_DEFINE(backend_cell, uint64_t) + +TEST_CASE(test_backend_round_trip) +{ + backend_cell_t cell; + uint64_t out = 0U; + uint64_t in = 0x0123456789ABCDEFULL; + + backend_cell_init(&cell); + backend_cell_load(&cell, &out); + TEST_ASSERT(out == 0U); + + backend_cell_store(&cell, &in); + backend_cell_load(&cell, &out); + TEST_ASSERT(out == in); + + out = 0U; + TEST_ASSERT(backend_cell_try_load(&cell, &out, 8U)); + TEST_ASSERT(out == in); +} + +int +main(void) +{ + (void)fprintf(stdout, "\n=== seqlock forced-backend tests ===\n\n"); + + run_test(test_backend_round_trip, "backend_round_trip"); + + (void)fprintf(stdout, "\n=== all forced-backend tests passed ===\n\n"); + return EXIT_SUCCESS; +} diff --git a/tests/test_seqlock_c11.c b/tests/test_seqlock_c11.c deleted file mode 100644 index fd3aa79..0000000 --- a/tests/test_seqlock_c11.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * @file test_seqlock_c11.c - * @brief Exercises the C11 `` atomic backend. - * - * On GCC/Clang the default backend is the `__atomic` builtin path, so the C11 - * path (and its lock-free static assertion) would otherwise never be compiled - * in CI. This executable forces SEQLOCK_USE_C11_ATOMICS for both this TU and - * the core (the define is passed on the command line in tests/meson.build, so - * the separately compiled seqlock.c agrees on the backend and ABI). A failure - * here is most likely a compile error: the C11 lock-free assertion rejecting - * the counter type, or the C11 atomic macros not expanding cleanly. - */ - -#include "seqlock.h" -#include "seqlock_test.h" - -#include - -SEQLOCK_DEFINE(c11_cell, uint64_t) - -TEST_CASE(test_c11_round_trip) -{ - c11_cell_t cell; - uint64_t out = 0U; - uint64_t in = 0x0123456789ABCDEFULL; - - c11_cell_init(&cell); - c11_cell_load(&cell, &out); - TEST_ASSERT(out == 0U); - - c11_cell_store(&cell, &in); - c11_cell_load(&cell, &out); - TEST_ASSERT(out == in); - - out = 0U; - TEST_ASSERT(c11_cell_try_load(&cell, &out, 8U)); - TEST_ASSERT(out == in); -} - -int -main(void) -{ - (void)fprintf(stdout, - "\n=== seqlock C11-atomics backend tests ===\n\n"); - - run_test(test_c11_round_trip, "c11_round_trip"); - - (void)fprintf(stdout, - "\n=== all C11-atomics backend tests passed ===\n\n"); - return EXIT_SUCCESS; -} diff --git a/tests/test_seqlock_retry.c b/tests/test_seqlock_retry.c new file mode 100644 index 0000000..f0d7995 --- /dev/null +++ b/tests/test_seqlock_retry.c @@ -0,0 +1,118 @@ +/* + * @file test_seqlock_retry.c + * @brief Drives every reader retry branch deterministically. + * + * The concurrent tests reach the retry paths only when a writer happens to + * interleave, which is not enough to gate branch coverage. This executable + * reaches them on every run, single-threaded: + * - The output buffer aliases the counter, so the copy itself moves the + * counter between the reader's two loads. + * - With SEQLOCK_TEST_RELAX_HOOK, SEQLOCK_CPU_RELAX is replaced + * (tests/meson.build) with a hook that completes a pending write, so a + * blocking load that starts on an odd counter retries exactly once. + * + * tests/meson.build builds this file twice: against the library, and with the + * hook. The hook adds a call to the core's control flow, so gcov numbers that + * build's branches separately, and each build must reach its own. + */ + +#include "seqlock.h" +#include "seqlock_test.h" + +#include +#include + +static seqlock_hdr_t hdr; + +#if defined(SEQLOCK_TEST_RELAX_HOOK) + +static uint32_t relax_calls; +static bool complete_write; + +void seqlock_test_relax(void); + +void +seqlock_test_relax(void) +{ + ++relax_calls; + if (complete_write) { + hdr.seq = (SEQLOCK_COUNTER_TYPE)(hdr.seq + 1U); + } +} + +#define RELAX_CALLS_RESET() (relax_calls = 0U) +#define RELAX_CALLS_EQUAL(n) (relax_calls == (n)) + +TEST_CASE(test_load_retries_while_odd) +{ + const uint64_t payload = 0x1122334455667788ULL; + uint64_t out = 0U; + + hdr.seq = (SEQLOCK_COUNTER_TYPE)1U; + RELAX_CALLS_RESET(); + complete_write = true; + seqlock_core_load(&hdr, &payload, &out, sizeof out); + complete_write = false; + + TEST_ASSERT(RELAX_CALLS_EQUAL(1U)); + TEST_ASSERT(hdr.seq == (SEQLOCK_COUNTER_TYPE)2U); + TEST_ASSERT(out == payload); +} + +#else + +/* The default relax hint is not observable, so only outcomes are checked. */ +#define RELAX_CALLS_RESET() ((void)0) +#define RELAX_CALLS_EQUAL(n) ((void)(n), true) + +#endif + +TEST_CASE(test_load_retries_when_counter_moves) +{ + const SEQLOCK_COUNTER_TYPE next = (SEQLOCK_COUNTER_TYPE)4U; + + hdr.seq = (SEQLOCK_COUNTER_TYPE)2U; + RELAX_CALLS_RESET(); + seqlock_core_load(&hdr, &next, (void *)&hdr.seq, sizeof hdr.seq); + + TEST_ASSERT(RELAX_CALLS_EQUAL(1U)); + TEST_ASSERT(hdr.seq == next); +} + +TEST_CASE(test_try_load_retries_when_counter_moves) +{ + const SEQLOCK_COUNTER_TYPE next = (SEQLOCK_COUNTER_TYPE)4U; + + /* One attempt: the counter moves during the copy, so the bound is + * exhausted. */ + hdr.seq = (SEQLOCK_COUNTER_TYPE)2U; + RELAX_CALLS_RESET(); + TEST_ASSERT(!seqlock_core_try_load(&hdr, &next, (void *)&hdr.seq, + sizeof hdr.seq, 1U)); + TEST_ASSERT(RELAX_CALLS_EQUAL(1U)); + + /* Two attempts: the second sees a stable counter and succeeds. */ + hdr.seq = (SEQLOCK_COUNTER_TYPE)2U; + RELAX_CALLS_RESET(); + TEST_ASSERT(seqlock_core_try_load(&hdr, &next, (void *)&hdr.seq, + sizeof hdr.seq, 2U)); + TEST_ASSERT(RELAX_CALLS_EQUAL(1U)); + TEST_ASSERT(hdr.seq == next); +} + +int +main(void) +{ + (void)fprintf(stdout, "\n=== seqlock reader retry tests ===\n\n"); + +#if defined(SEQLOCK_TEST_RELAX_HOOK) + run_test(test_load_retries_while_odd, "load_retries_while_odd"); +#endif + run_test(test_load_retries_when_counter_moves, + "load_retries_when_counter_moves"); + run_test(test_try_load_retries_when_counter_moves, + "try_load_retries_when_counter_moves"); + + (void)fprintf(stdout, "\n=== all reader retry tests passed ===\n\n"); + return EXIT_SUCCESS; +}