Correzione: aspettare il browser dopo «Scollega» (#67) e il riposo del motore sotto la guida di Alberto #77

Merged
Alby96 merged 5 commits from correzione/browser-senza-risposta into main 2026-12-01 12:57:15 +01:00
3 changed files with 56 additions and 1 deletions
Showing only changes of commit 8d1b344366 - Show all commits

No files matched your search

+13 -1
View File
@@ -1136,7 +1136,19 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
return; return;
} }
var result = await _browser.Cdp.NavigateAsync(target, ct).ConfigureAwait(false); // A browser still silent (restarting after «Scollega», issue #67) usually refuses the navigation too: the window
// comes to the front all the same, as when its page was unknown before, never an internal error.
NavigationResult result;
try
{
result = await _browser.Cdp.NavigateAsync(target, ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is CdpDisconnectedException or BrowserUnavailableException or CdpCommandException or TimeoutException)
{
_log.Info("collegamento", $"Alberto usa il browser: {what} non aperta (il browser non risponde).");
return;
}
_log.Info("collegamento", result.Succeeded _log.Info("collegamento", result.Succeeded
? $"Alberto usa il browser: {what} aperta." ? $"Alberto usa il browser: {what} aperta."
: $"Alberto usa il browser: {what} non aperta (errore di rete del browser)."); : $"Alberto usa il browser: {what} non aperta (errore di rete del browser).");
@@ -149,6 +149,33 @@ public sealed class BrowserStartPageTests
Assert.AreEqual(Login, browser.FakeCdp.Url); Assert.AreEqual(Login, browser.FakeCdp.Url);
} }
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task ABrowserThatAnswersNothingStillComesToTheFrontWithoutAnError()
{
var ct = TestContext.CancellationToken;
await using var bed = await DesktopTestBed.StartAsync();
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
var browser = bed.Browser!;
bed.App.Connection!.AddressWait = TimeSpan.FromMilliseconds(300);
// Still restarting (issue #67): no page and no navigation either. The window comes all the same, never a 500.
browser.FakeCdp.Silent = true;
browser.FakeCdp.RefusesNavigation = true;
var shown = browser.Shown;
await BeginAsync(bed, openLogin: false);
Assert.AreEqual(shown + 1, browser.Shown, "«Usa il browser»: the window came to the front");
var result = await bed.JsonAsync(HttpMethod.Post, "/api/browser/show");
StringAssert.Contains(result.GetProperty("message").GetString(), "primo piano");
Assert.AreEqual(shown + 2, browser.Shown, "«Apri il browser»: the window came to the front");
Assert.AreEqual("about:blank", browser.FakeCdp.Url, "nothing could open");
Assert.DoesNotContain(Login, browser.FakeCdp.Visited);
browser.FakeCdp.Silent = false;
browser.FakeCdp.RefusesNavigation = false;
}
[TestMethod] [TestMethod]
[Timeout(60_000, CooperativeCancellation = true)] [Timeout(60_000, CooperativeCancellation = true)]
public async Task ApriIlBrowserBringsTheWindowWithItsStartPageAndTheLoginAfterADisconnection() public async Task ApriIlBrowserBringsTheWindowWithItsStartPageAndTheLoginAfterADisconnection()
@@ -265,6 +265,7 @@ internal sealed class FakeCdp : ICdpSession
private int _cookieReads; private int _cookieReads;
private bool _silent; private bool _silent;
private int _silentAnswers; private int _silentAnswers;
private bool _refusesNavigation;
public bool LoggedIn { get; set; } public bool LoggedIn { get; set; }
@@ -328,6 +329,16 @@ internal sealed class FakeCdp : ICdpSession
/// <summary>How many times <c>Page.getFrameTree</c> was asked while <see cref="Silent"/>.</summary> /// <summary>How many times <c>Page.getFrameTree</c> was asked while <see cref="Silent"/>.</summary>
public int SilentAnswers => Volatile.Read(ref _silentAnswers); public int SilentAnswers => Volatile.Read(ref _silentAnswers);
/// <summary>
/// While true <c>Page.navigate</c> fails as a closed connection too, like a browser that has not finished restarting
/// (issue #67): with <see cref="Silent"/>, a browser that answers nothing at all.
/// </summary>
public bool RefusesNavigation
{
get => Volatile.Read(ref _refusesNavigation);
set => Volatile.Write(ref _refusesNavigation, value);
}
public ConcurrentQueue<string> Methods { get; } = new(); public ConcurrentQueue<string> Methods { get; } = new();
/// <summary>Every address a <c>Page.navigate</c> asked for, in order.</summary> /// <summary>Every address a <c>Page.navigate</c> asked for, in order.</summary>
@@ -350,6 +361,11 @@ internal sealed class FakeCdp : ICdpSession
return Task.FromException<JsonElement>(new CdpDisconnectedException()); return Task.FromException<JsonElement>(new CdpDisconnectedException());
} }
if (method == "Page.navigate" && RefusesNavigation)
{
return Task.FromException<JsonElement>(new CdpDisconnectedException());
}
JsonNode result = method switch JsonNode result = method switch
{ {
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = Url } } }, "Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = Url } } },