diff --git a/src/Febe.Core/Engine/EngineHost.cs b/src/Febe.Core/Engine/EngineHost.cs index 0e0fedd..40534de 100644 --- a/src/Febe.Core/Engine/EngineHost.cs +++ b/src/Febe.Core/Engine/EngineHost.cs @@ -74,7 +74,10 @@ public sealed class EngineHost : IAsyncDisposable internal IReadOnlyList RecentActions(int limit) => _recent.Snapshot(limit); - /// Starts the background loop. + /// + /// Starts the background loop. It counts as running from here (), not only once + /// the thread pool gets to it: whoever started it (Febe's start, «Scollega») says so right away. + /// public Task StartAsync(CancellationToken ct = default) { lock (_gate) @@ -86,6 +89,7 @@ public sealed class EngineHost : IAsyncDisposable _loopCts = CancellationTokenSource.CreateLinkedTokenSource(ct); var token = _loopCts.Token; + _s.Runtime.IsRunning = true; _loop = Task.Run(() => RunAsync(token), CancellationToken.None); } @@ -739,8 +743,11 @@ public sealed class EngineHost : IAsyncDisposable { try { - await _runner.RestAsync(ct).ConfigureAwait(false); - s.PageTurns.NoteRested(); + if (!KeepsPageAfterWaitingForTurn()) + { + await _runner.RestAsync(ct).ConfigureAwait(false); + s.PageTurns.NoteRested(); + } } catch (Exception ex) when (ex is not OutOfMemoryException) { @@ -797,6 +804,23 @@ public sealed class EngineHost : IAsyncDisposable s.Events.Publish(EngineNotificationKind.StateChanged); } + /// + /// Looked at again with the page turn in hand, right before a rest of the engine: the turn may have come only after + /// Alberto's own navigation, which takes it too (he took the browser again right after letting go of it), or after a + /// verification. Then the page is his, or must stay as it is (spec §6.1, §7.6): no rest, and the rest is owed for + /// when he lets go. The profile search looks again in the same way before its own rest. + /// + private bool KeepsPageAfterWaitingForTurn() + { + if (_s.Runtime.UserIsDriving) + { + Volatile.Write(ref _restAfterDriving, 1); + return true; + } + + return _s.Breaker.State.Status is BreakerStatus.Challenge or BreakerStatus.Disconnected; + } + /// /// Alberto let go of the browser and no session runs: the page goes back to rest (spec §6.1), so that nothing he or a /// cancelled session left open stays on the site — a story viewer moves on by itself to other accounts (§6.3). @@ -821,6 +845,11 @@ public sealed class EngineHost : IAsyncDisposable await using (turn.ConfigureAwait(false)) { + if (KeepsPageAfterWaitingForTurn()) + { + return; + } + await _runner.RestAsync(ct).ConfigureAwait(false); _s.PageTurns.NoteRested(); } diff --git a/tests/Febe.Tests/Engine/EngineHostTests.cs b/tests/Febe.Tests/Engine/EngineHostTests.cs index f15c7f2..0588e1a 100644 --- a/tests/Febe.Tests/Engine/EngineHostTests.cs +++ b/tests/Febe.Tests/Engine/EngineHostTests.cs @@ -519,6 +519,49 @@ public sealed class EngineHostTests Assert.AreEqual(restsWhenTaken + 1, runner.Rests, "The verification page is left as it is."); } + [TestMethod] + [Timeout(60_000, CooperativeCancellation = true)] + public async Task ARestThatGetsThePageOnlyAfterAlbertoTookTheBrowserAgainLeavesHisPage() + { + var driving = false; + await using var bed = await EngineTestBed.CreateAsync(Morning, userIsDriving: () => driving); + await bed.AddAccountAsync("uno.demo", followed: true); + var runner = new ScriptedRunner(bed.Governor, bed.Clock); + var host = bed.Start(runner); + await host.PauseAsync(); + driving = true; + host.NotifyUserDrivingChanged(); + await bed.RunForAsync(TimeSpan.FromSeconds(1)); + var rests = runner.Rests; + + // He lets go while his own navigation still holds the page (the turn of «Usa il browser»): the rest waits for it. + var turns = bed.Services.PageTurns; + var albertos = await turns.TakeAsync(PageUser.Engine, CancellationToken.None); + driving = false; + host.NotifyUserDrivingChanged(); + // Real time: the loop, woken, waits for the turn and for no timer of the virtual clock. + var watch = System.Diagnostics.Stopwatch.StartNew(); + while (!turns.IsWaiting(PageUser.Engine)) + { + Assert.IsLessThan(TimeSpan.FromSeconds(10), watch.Elapsed, "the rest waits for the page turn"); + await Task.Delay(TimeSpan.FromMilliseconds(10)); + } + + // He takes the browser again and his page opens before the rest gets the turn: the page is his, not rested. + driving = true; + host.NotifyUserDrivingChanged(); + albertos.Dispose(); + await bed.RunForAsync(TimeSpan.FromSeconds(1)); + Assert.AreEqual(rests, runner.Rests, "the page he took is not put to rest under him"); + Assert.IsFalse(turns.IsWaiting(PageUser.Engine)); + + // The rest is still owed: when he lets go again, the page goes back to rest. + driving = false; + host.NotifyUserDrivingChanged(); + await bed.RunForAsync(TimeSpan.FromSeconds(1)); + Assert.AreEqual(rests + 1, runner.Rests, "back to rest once he lets go"); + } + [TestMethod] public async Task ChangingTheAccountsDuringARecipeDoesNotBringItsNextCheckForward() {