Commit f1393db
Make
`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>OnIdle tests deterministic by polling instead of sleeping1 parent 6ad4f46 commit f1393db
1 file changed
Lines changed: 34 additions & 12 deletions
Lines changed: 34 additions & 12 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 | + | |
| 53 | + | |
22 | 54 | | |
23 | 55 | | |
24 | 56 | | |
| |||
203 | 235 | | |
204 | 236 | | |
205 | 237 | | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
| 238 | + | |
212 | 239 | | |
213 | 240 | | |
214 | 241 | | |
| |||
303 | 330 | | |
304 | 331 | | |
305 | 332 | | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
| 333 | + | |
312 | 334 | | |
313 | 335 | | |
314 | 336 | | |
| |||
0 commit comments