You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
CopilotAI
changed the title
[WIP] Fix System.Diagnostics.Tests.StopwatchTests.GetTimestamp test in CIMake browser StopwatchTests robust to coarse clock resolutionJul 13, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes#62021
StopwatchTests.GetTimestampandStartNewAndResetfail intermittently on the browser/WASM target: consecutiveGetTimestamp()reads return equal values andElapsedmeasures zero.Root cause
On browser,
Stopwatch.GetTimestamp()→minipal_hires_ticks()→ emscriptenclock_gettime(CLOCK_MONOTONIC), backed byperformance.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.cstestSleephelper: on browser only, spin (re-issuing the existingWaitOne) until the Stopwatch timestamp advances by at least oneTimeSpantick. Non-browser platforms are unchanged.This guarantees a measurable delta before dependent asserts (
Assert.NotEqual(ts1, ts2),Elapsed > TimeSpan.Zero), coveringGetTimestamp,StartNewAndReset,ConstructStartAndStop, andOverridesToString. No tests are disabled.Note
This PR was generated with the assistance of GitHub Copilot.