Commit 20a0ed2
Fix flaky
* Make `OnIdle` tests deterministic by polling instead of sleeping
`CanRunOnIdleTask` (and its twin `CanRunOnIdleInProfileTask`) were flaky on
the net462 (Windows PowerShell 5.1) CI leg — the former was just caught
failing on PR #2298's Windows job.
The root cause is that `PsesInternalHost.OnPowerShellIdle` calls
`Events.GenerateEvent(PSEngineEvent.OnIdle, ...)`, which only *enqueues* the
event. For a subscriber registered with `-Action {...}`, PowerShell doesn't
run the action scriptblock inline; it becomes a pending action that the
engine dispatches asynchronously on the pipeline thread, around subsequent
pipeline invocations. So the action's execution was never synchronized with
the test's `$handled` read, and the fixed `Thread.Sleep(2000)` was just a
timing guess — sometimes too short on the slower WinPS leg, leaving
`$global:handled` still `$false` at the assertion.
The key realization is that each *additional* pipeline execution gives the
engine another chance to drain the pending action, so re-reading the handler
variable in a loop both waits for *and* drives completion. I replaced the
sleep with a shared `WaitForHandledAsync` helper that polls the variable
(~200ms apart, ~15s ceiling) until it reports `$true`, returning the last
observed value on timeout so the assertion still fails loudly. This keeps the
tests' intent intact and isn't merely a longer sleep.
I validated both tests on net8.0 (green across repeated runs, ~0.4s each vs.
the old fixed 2s); net462 can't run on macOS, but the mechanism is identical
across targets and the 15s ceiling self-terminates on success, so it's
strictly safer on the slow leg without slowing the fast one.
Drafted by Copilot (Claude Opus 4.8).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Move `OnIdle` assertion into helper and drop list return
Small follow-up to review feedback: both call sites only ever asserted the
handler variable became `$true`, so the `IReadOnlyList<bool>` return was
needless ceremony. `AssertHandledAsync` now owns the assertion — it returns
once the variable reports `$true` and otherwise fails via `Assert.Fail` when
the ~15s poll window elapses, which reads as "the OnIdle handler never ran."
`Assert.Fail` is fine here — we're on xUnit 2.9.3 and already use it in the
E2E tests. No behavior change to what's being verified; the call sites just
shrink to a single `await OnIdleTestHelpers.AssertHandledAsync(...)`. Still
green on net8.0.
Drafted by Copilot (Claude Opus 4.8).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Assert `OnIdle` poll result with `Assert.True`
Follow-up to review feedback: the helper short-circuited with a bare `return`
on success and only asserted (`Assert.Fail`) on timeout, so the happy path had
no explicit assertion. Restructure the loop to poll until the handler variable
is `$true` or the ~15s window elapses, then assert the outcome once with
`Assert.True(handled, ...)`. Same behavior, but the success and timeout paths
now share a single, self-describing assertion.
Still green on net8.0.
Drafted by Copilot (Claude Opus 4.8).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>CanRunOnIdleTask by polling instead of sleeping (#2314)1 parent 4067880 commit 20a0ed2
1 file changed
Lines changed: 33 additions & 16 deletions
Lines changed: 33 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
22 | 53 | | |
23 | 54 | | |
24 | 55 | | |
| |||
203 | 234 | | |
204 | 235 | | |
205 | 236 | | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
| 237 | + | |
214 | 238 | | |
215 | 239 | | |
216 | 240 | | |
| |||
303 | 327 | | |
304 | 328 | | |
305 | 329 | | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
| 330 | + | |
314 | 331 | | |
315 | 332 | | |
316 | 333 | | |
0 commit comments