Skip to content

tests: replace timed register-protection checks with coordinated probes - #1500

Open
jmass-ggg wants to merge 1 commit into
p4lang:mainfrom
jmass-ggg:fix/1484-register-protection-tests
Open

jmass-ggg wants to merge 1 commit into
p4lang:mainfrom
jmass-ggg:fix/1484-register-protection-tests

Conversation

@jmass-ggg

@jmass-ggg jmass-ggg commented Dec 3, 2026 •

Copy link
Copy Markdown

Summary

Addresses #1484.

The existing register-protection tests rely on one-second sleeps and elapsed-time assertions to verify sequential and parallel execution. This introduces approximately eight seconds of test execution time and can cause false failures in heavily loaded CI environments.

This change replaces timing-based checks with coordinated test primitives, explicit synchronization, and register mutex probes.

Problem

The original implementation has several limitations:

  • Unnecessary execution time caused by artificial sleeps.
  • Scheduling-dependent elapsed-time assertions.
  • False failures under CPU contention and scheduling delays.
  • Limited visibility into whether the required register mutexes are held.

Previous Execution Flow

flowchart TD
    A[Start register-protection test] --> B[Execute concurrent actions]
    B --> C[Acquire register locks]
    C --> D[Sleep for one second]
    D --> E[Complete actions]
    E --> F[Measure elapsed time]
    F --> G{Within expected duration?}
    G -->|Yes| H[Test passes]
    G -->|No| I[Test fails]
    J[CPU contention or scheduling delays] -.-> F
Loading

A correct locking implementation can fail when scheduling delays cause the elapsed time to exceed the expected threshold.

Proposed Solution

  • Replace artificial sleeps with condition-variable-based coordination.
  • Introduce reusable test helpers for worker synchronization and register mutex probing.
  • Observe register protection while actions or parser methods are executing.
  • Verify concurrent progress for actions operating on independent register arrays.
  • Use bounded progress waits and RAII-based worker cleanup.
  • Remove the obsolete RegisterSpin test primitive.
  • Keep production locking behavior unchanged.

New Execution Flow

flowchart TD
    A[Start register-protection test] --> B[Launch worker thread]
    B --> C[Acquire production register locks]
    C --> D[Enter coordinated test primitive]
    D --> E[Notify test controller]
    E --> F[Probe required register mutexes]
    F --> G[Release synchronization gate]
    G --> H[Join worker thread]
    H --> I[Evaluate test assertions]
    I -->|Expected behavior| J[Test passes]
    I -->|Unexpected behavior| K[Test fails]
Loading

Independent Register Concurrency

sequenceDiagram
    participant T as Test Controller
    participant A as Action 1
    participant B as Action 2

    par Independent execution
        T->>A: Start action
        A->>A: Acquire register A lock
        A-->>T: Primitive entered
    and
        T->>B: Start action
        B->>B: Acquire register B lock
        B-->>T: Primitive entered
    end

    Note over A,B: Both primitives must enter before release

    T->>A: Release gate
    T->>B: Release gate
    A-->>T: Worker completed
    B-->>T: Worker completed
    T->>T: Validate concurrent progress
Loading

Performance Comparison

Measured locally on x86_64:

Test Before After (median)
Sequential 2,005.7 ms 5.83 ms
Parallel 1,007.0 ms 5.98 ms
SequentialAdvanced 3,007.5 ms 6.15 ms
Parser RegisterSync 2,007.2 ms 5.62 ms
Combined 8,027.4 ms 23.58 ms

The replacement reduces the combined measured runtime by approximately 99.7%, providing an approximately 340× speed improvement in the tested environment.

Files Changed

File Description
tests/register_protection.h Introduce reusable synchronization and mutex-probing helpers
tests/test_actions.cpp Replace timing-based action register-protection tests
tests/test_parser.cpp Replace timing-based parser register-protection test
tests/primitives.cpp Remove obsolete sleep-based test primitive
tests/Makefile.am Include the new helper in source distributions

No production implementation files are modified.

Validation

  • All 31 action tests passed.
  • All 60 parser tests passed.
  • Full CTest suite passed (51/51).
  • GCC and Clang validation.
  • Repeated execution under normal conditions.
  • Repeated execution under CPU contention.
  • Controlled scheduling-delay experiments.
  • Mutation testing for missing register locks.
  • Mutation testing for unnecessary global serialization.
  • Repository style checks.
  • git diff --check.
  • Resolve explicit register-locking coverage gap.
  • Strengthen independent-array lock ownership coverage.
  • Correct failed worker-entry handling.
  • Repeat final regression validation after the remaining fixes.

Known Limitations

  • ARM64 execution has not been verified.
  • Progress safeguards remain subject to extreme scheduling delays.
  • Nonblocking mutex probes have residual uncertainty because try_lock() permits spurious failures.
  • The latest ThreadSanitizer attempt was blocked by an environment-specific memory-mapping error.
  • An independent audit identified an explicit-locking coverage gap that must be resolved before this change is ready for merge.

The objective is to reduce test runtime and scheduling sensitivity while preserving meaningful register-protection regression coverage.

Related issue: #1484

Signed-off-by: jmass-ggg <jamesgurung690@gmail.com>
@jmass-ggg
jmass-ggg force-pushed the fix/1484-register-protection-tests branch from cc48270 to 759faac Compare December 3, 2026 07:41
@jmass-ggg

Copy link
Copy Markdown
Author

Hi @chreekat,

I've submitted PR #1500 addressing issue #1484. All 11 CI checks have passed, including the DCO check.

The synchronization-based approach reduces the combined test runtime from approximately 8 seconds to 24 milliseconds in my local environment.

I'd appreciate your review and feedback when you have time. I'm happy to address any concerns or suggested improvements before merging.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant