Skip to content

Make browser StopwatchTests robust to coarse clock resolution#130605

Merged
pavelsavara merged 2 commits into
mainfrom
copilot/fix-stopwatch-tests-issue
Jul 13, 2026
Merged

Make browser StopwatchTests robust to coarse clock resolution#130605
pavelsavara merged 2 commits into
mainfrom
copilot/fix-stopwatch-tests-issue

Conversation

Copilot AI commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #62021

StopwatchTests.GetTimestamp and StartNewAndReset fail intermittently on the browser/WASM target: consecutive GetTimestamp() reads return equal values and Elapsed measures zero.

Root cause

On browser, Stopwatch.GetTimestamp()minipal_hires_ticks() → emscripten clock_gettime(CLOCK_MONOTONIC), backed by performance.now(), which is security-clamped to a coarse resolution. Blocking waits (ManualResetEvent.WaitOne(timeout)) also don't advance real time on the single browser thread. A fixed-timeout sleep therefore isn't guaranteed to move the timestamp past a resolution boundary. The monotonic hi-res clock in use is already the best clock the browser exposes — there is no higher-resolution alternative to switch to — so the test must tolerate coarse resolution rather than assume a fixed sleep crosses a boundary.

Changes

  • Stopwatch.cs test Sleep helper: on browser only, spin (re-issuing the existing WaitOne) until the Stopwatch timestamp advances by at least one TimeSpan tick. Non-browser platforms are unchanged.
private static void Sleep(int milliseconds)
{
    if (PlatformDetection.IsBrowser)
    {
        long start = Stopwatch.GetTimestamp();
        long minTicks = Math.Max(1, Stopwatch.Frequency / TimeSpan.TicksPerSecond);
        do
        {
            s_sleepEvent.WaitOne(milliseconds);
        }
        while (Stopwatch.GetTimestamp() - start < minTicks);

        return;
    }

    s_sleepEvent.WaitOne(milliseconds);
}

This guarantees a measurable delta before dependent asserts (Assert.NotEqual(ts1, ts2), Elapsed > TimeSpan.Zero), covering GetTimestamp, StartNewAndReset, ConstructStartAndStop, and OverridesToString. No tests are disabled.

Note

This PR was generated with the assistance of GitHub Copilot.

Co-authored-by: pavelsavara <271576+pavelsavara@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot July 13, 2026 08:59
Copilot AI changed the title [WIP] Fix System.Diagnostics.Tests.StopwatchTests.GetTimestamp test in CI Make browser StopwatchTests robust to coarse clock resolution Jul 13, 2026
Copilot AI requested a review from pavelsavara July 13, 2026 09:00
@pavelsavara pavelsavara marked this pull request as ready for review July 13, 2026 09:55
Copilot AI review requested due to automatic review settings July 13, 2026 09:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts System.Diagnostics.Tests.StopwatchTests to reduce intermittent failures on browser/WASM by making the test’s sleep helper wait until Stopwatch.GetTimestamp() observably advances, rather than assuming a single fixed wait crosses the clock’s resolution boundary.

Changes:

  • Add a browser-specific loop in the Sleep(int milliseconds) helper to retry waiting until the stopwatch timestamp advances.

@pavelsavara

Copy link
Copy Markdown
Member

/ba-g CI failure is #130618

Copilot AI temporarily deployed to copilot-pat-pool July 13, 2026 15:06 Inactive
Copilot AI temporarily deployed to copilot-pat-pool July 13, 2026 15:06 Inactive
@pavelsavara pavelsavara merged commit 8a53018 into main Jul 13, 2026
96 of 100 checks passed
@pavelsavara pavelsavara deleted the copilot/fix-stopwatch-tests-issue branch July 13, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Diagnostics.Tests.StopwatchTests.GetTimestamp test fails in the CI

6 participants