diff --git a/tests/E2E.Mobile.Tests/Engine/ObservationTests.cs b/tests/E2E.Mobile.Tests/Engine/ObservationTests.cs index 659ce22ab..8d154cb41 100644 --- a/tests/E2E.Mobile.Tests/Engine/ObservationTests.cs +++ b/tests/E2E.Mobile.Tests/Engine/ObservationTests.cs @@ -658,23 +658,31 @@ public async Task Rethrows_OPERATION_TIMEOUT_from_capturePixels_during_observe() public async Task Shares_one_budget_across_observe_steps_so_screenshot_cannot_take_a_fresh_budget() { var h = Harness.Create(); + h.Clock.ManualTimers = true; var session = await OpenAttempt(h); - // The snapshot carries its geometry, so the only screenshot is the observation's pixels. - h.Fake.RespondAsync(DeviceMethods.CaptureSnapshot, async (_, _) => + // The snapshot carries its geometry, so the only screenshot is the observation's pixels. The snapshot takes 150 ms of the test's + // clock, so a loaded runner cannot make it use up the 500 ms budget before the screenshot starts. + h.Fake.RespondAsync(DeviceMethods.CaptureSnapshot, (_, _) => { - await Task.Delay(150); - return WithViewport(402, 874); + h.Clock.Spend(TimeSpan.FromMilliseconds(150)); + return Task.FromResult(WithViewport(402, 874)); }); var screenshots = 0; + var shot = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); h.Fake.RespondAsync(DeviceMethods.CaptureScreenshot, (_, _) => { Interlocked.Increment(ref screenshots); + shot.TrySetResult(); return Never(); }); - var started = DateTimeOffset.UtcNow; - var failure = await Assert.ThrowsAsync(() => session.ObserveAsync(new ObserveOptions { Pixels = true }, TimeSpan.FromMilliseconds(500), CancellationToken.None)); + var started = h.Clock.GetUtcNow(); + var observing = Assert.ThrowsAsync(() => session.ObserveAsync(new ObserveOptions { Pixels = true }, TimeSpan.FromMilliseconds(500), CancellationToken.None)); + await shot.Task; + await h.Clock.SpendUntil(observing, TimeSpan.FromMilliseconds(350)); + var failure = await observing; Assert.Equal("OPERATION_TIMEOUT", failure.Code); Assert.Equal(1, screenshots); - Assert.True(DateTimeOffset.UtcNow - started < TimeSpan.FromSeconds(3)); + // The test may spend several steps before the engine makes the timer that ends the wait, so only the lower bound is exact: the budget never ends early. + Assert.True(h.Clock.GetUtcNow() - started >= TimeSpan.FromMilliseconds(500)); } }