Skip to content

Commit 6a9fa4e

Browse files
committed
test: fix flaky AvaloniaScheduler zero-delay test
Replace the fixed Thread.Sleep(50) + bool flag with a TaskCompletionSource awaited via WaitAsync(10s). When the scheduler runs off the dispatcher's UI thread it posts the action at Background priority; on loaded macOS CI runners 50ms was occasionally too short before the assertion, causing intermittent failures. The action now completes instantly on the synchronous path and gets ample time (failing loudly via timeout) on the posted path.
1 parent 89c3cfd commit 6a9fa4e

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/tests/ReactiveUI.Avalonia.Tests/AvaloniaUIThreadTestsMain.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,19 @@ public async Task AvaloniaScheduler_Now_ReturnsCurrentTime()
8181
public async Task AvaloniaScheduler_Schedule_WithZeroDelay_ExecutesAction()
8282
{
8383
var scheduler = AvaloniaScheduler.Instance;
84-
var actionExecuted = false;
84+
var tcs = new TaskCompletionSource();
8585

8686
var disposable = scheduler.Schedule("test", TimeSpan.Zero, (s, state) =>
8787
{
88-
actionExecuted = true;
88+
tcs.TrySetResult();
8989
return Disposable.Empty;
9090
});
9191

92-
Thread.Sleep(50);
93-
94-
await Assert.That(actionExecuted).IsTrue();
92+
// When invoked on the UI thread the action runs synchronously; otherwise it is posted to the
93+
// dispatcher. Await its completion with a timeout rather than a fixed sleep so the test is not
94+
// sensitive to dispatcher scheduling latency on busy CI machines (the previous fixed 50ms sleep
95+
// was occasionally too short on loaded macOS runners, causing intermittent failures).
96+
await tcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
9597
await Assert.That(disposable).IsNotNull();
9698
}
9799

0 commit comments

Comments
 (0)