Skip to content

Test helper: Elapse holds at least the time it is given - #6708

Merged
erikdarlingdata merged 1 commit into
devfrom
fix/test-elapse-at-least
Oct 24, 2026
Merged

erikdarlingdata merged 1 commit into
devfrom
fix/test-elapse-at-least

Conversation

@erikdarlingdata

Copy link
Copy Markdown
Owner

Claude posting for Erik Darling.

No issue.

Why

CI failed on PR #6704 in Darling.Tests.ReadScopeCapacityTests.OnPending_MeasuresTheWaitInsideAScope_AndACreateInsideItIsNotAddedTwice: Assert.InRange(scope.Scope.WaitMs, 50, 4_000) got 47 after TestWait.Elapse(TimeSpan.FromMilliseconds(60)). The product measures the wait with Stopwatch (ReadConnectionWait.cs:80,91), so the test's input was shorter than the test asked for. That failure is unrelated to #6704's change.

TestWait.Elapse was one timed wait on a never-set ManualResetEventSlim. On Windows a timed wait can return up to one timer tick (about 15.6 ms) early, so a single wait can hold for less than the duration it is given.

What changes

  • Darling/Darling.Tests/TestTimeHelpers.cs: Elapse now waits again for whatever time is left, measured with TimeProvider.System, until the full duration has passed. It holds for at least the duration it is given. The diff is 10 insertions and 1 deletion in that one file.
  • Test-only. No product code and no CHANGELOG entry.
  • Lite.Tests has no Elapse of its own. It compiles this same file through a link (Lite.Tests/Lite.Tests.csproj:77), so the one edit covers both projects.
  • WaitCore (behind WaitUntil and TryWaitUntil) already re-reads the clock on every pass, so an early return only adds a pass there. It is not changed.

Behaviour notes:

  • A zero duration returns at once, as before.
  • A negative duration now returns at once. Before, it threw ArgumentOutOfRangeException, or for -1 ms waited forever. No caller passes a negative value.

Test plan

  • Not run here: the laptop is at its build limit, so there is no build and no test run. The lane brief says CI decides.
  • CI Stage=Guard, plus the classes that name Elapse(: ClipboardTextRetryBudgetTests, DarlingPasswordKeyStoreLiveTests, ManagedConfFileTests, ReadWindowBandTests and StaTestThread (Darling), and InstallIdStoreTests (Lite).

Considered, not added

  • A unit test that fails on the old code. The early return is a timer effect that cannot be forced on demand, so no test here could fail on the old code deterministically. The check that failed is in Alert texts: the word agrees with the count (#6703) #6704's ReadScopeCapacityTests, which is not on dev.
  • A loop inside Pause. That would change the WaitUntil path too, and WaitCore already tolerates an early return. Only Elapse needs the floor.

For the coordinator

  • Check that the one-file diff matches the description above.
  • CI is the first compile of this change. The loop uses TimeProvider.System.GetTimestamp and GetElapsedTime, both already used in the same file.

@erikdarlingdata
erikdarlingdata marked this pull request as ready for review October 24, 2026 09:55

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: LGTM

Claims

  • VERIFIED — Elapse now waits again for whatever time is left, measured with TimeProvider.System, until the full duration has passed — matches the diff: Darling/Darling.Tests/TestTimeHelpers.cs:65-74, the for loop recomputing left from GetElapsedTime(started) on each pass.
  • VERIFIED — the diff is 10 insertions and 1 deletion in that one file — gh pr view 6708 reports additions: 10, deletions: 1, and gh pr diff 6708 --name-only shows the single file.
  • VERIFIED — Lite.Tests has no Elapse of its own and compiles this same file through a link — Lite.Tests/Lite.Tests.csproj:77: <Compile Include="..\Darling\Darling.Tests\TestTimeHelpers.cs" Link="TestTimeHelpers.cs" />.
  • VERIFIED — WaitCore already re-reads the clock on every pass and is not changed by this diff — confirmed by the diff hunk (no change to WaitCore, TestTimeHelpers.cs:77-100) and its own re-entrant loop reading elapsed() each iteration.
  • VERIFIED — a zero duration returns at once, as before — the new loop's condition left > TimeSpan.Zero is false on entry for duration == TimeSpan.Zero, so Pause is never called, same net effect (no wait) as the old Pause(TimeSpan.Zero).
  • VERIFIED — a negative duration now returns at once, where before it threw (or waited forever for exactly -1 ms) — same reasoning: left > TimeSpan.Zero is false for any negative duration, so the loop body — and the exception ManualResetEventSlim.Wait would have thrown for most negative values — never runs.
  • VERIFIED — no caller passes a negative value — grepped every Elapse( call site (InstallIdStoreTests.cs:194, StaTestThread.cs:365 via s_sampleWindow = TimeSpan.FromMilliseconds(250), ReadWindowBandTests.cs:179, ManagedConfFileTests.cs:788, DarlingPasswordKeyStoreLiveTests.cs:523, ClipboardTextRetryBudgetTests.cs:217); all are positive literals.
  • UNVERIFIED — the specific CI failure numbers from PR #6704 (Assert.InRange(scope.Scope.WaitMs, 50, 4_000) got 47 after Elapse(TimeSpan.FromMilliseconds(60))) — a live CI run's log, not checkable from this read-only tree.

Riskiest lines

  1. Darling/Darling.Tests/TestTimeHelpers.cs:70 — the recomputed left drives every subsequent Pause; if it undercounted elapsed time the loop could return early again, reproducing the exact bug being fixed — no behaviour test covers it (see below) | UNCOVERED
  2. Darling/Darling.Tests/TestTimeHelpers.cs:66-74 — a pathological early-return pattern from the OS timer could in principle spin many iterations before the remaining time shrinks to zero; not a hang (wall-clock time is monotonic and strictly advances GetElapsedTime), just a possible brief tight loop at the tail — UNCOVERED
  3. Darling/Darling.Tests/TestTimeHelpers.cs:65 — the removed one-line Pause(duration) is now multi-statement; any caller relying on Elapse returning in bounded/short real time (e.g., a CI timeout) would be affected by the loop running longer under a very coarse timer — all six call sites use small literal durations (≤2.7 s), so this is low risk | UNCOVERED

Traps near this diff

  • "A test that reads the clock twice measures the CI scheduler: the delta-seeder tests failed at 123 and 243 on unrelated PRs (#3676). One anchor owns both ends of an interval." — does not apply: Elapse takes one started timestamp via GetTimestamp() and repeatedly measures elapsed time FROM that single anchor via GetElapsedTime(started) (TestTimeHelpers.cs:69-70); it never takes a second independent anchor, so this is the same single-anchor pattern WaitUntil/TryWaitUntil already use, not the double-read shape the trap warns about.
  • "Lite and Darling ship in parity... a fix to one SKU's reader... without the counterpart's is a finding." — does not apply: this is one physical file, linked into both Darling.Tests and Lite.Tests (Lite.Tests.csproj:77), so there is no counterpart to miss — the single edit reaches both SKUs' test runs.

Divergence (blind read vs PR body)

None — the body's account matches the diff on every claim graded above, including the negative-duration behaviour change, which the body discloses itself ("A negative duration now returns at once. Before, it threw ArgumentOutOfRangeException, or for -1 ms waited forever. No caller passes a negative value.") and which I independently derived from the loop condition before reading the body.

Reviewer checklist

  • Confirm CI (Guard stage plus the named test classes: ClipboardTextRetryBudgetTests, DarlingPasswordKeyStoreLiveTests, ManagedConfFileTests, ReadWindowBandTests, StaTestThread, InstallIdStoreTests) actually goes green on a real Windows runner, since this fix targets a timer-granularity effect that can't be forced deterministically in a read-only review.

@erikdarlingdata
erikdarlingdata merged commit 471707e into dev Oct 24, 2026
27 of 30 checks passed
@erikdarlingdata
erikdarlingdata deleted the fix/test-elapse-at-least branch October 24, 2026 10:15
@github-actions github-actions Bot mentioned this pull request Oct 24, 2026
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