Prove che cadevano con il runner carico (#46)
Le prove cadevano a ogni carico della CI, ogni volta prove diverse. Dietro alcune c'erano difetti veri: FebeDatabase lasciava i file aperti dopo DisposeAsync (comandi SQLite non chiusi, pool di lettura chiuso dopo lo scrittore), e il WAL spariva durante lo spostamento della cartella Dati; la scheda dell'account senza statistiche leggeva i file di tutta la libreria; l'arresto di FebeApp non aspettava i lavori aggiunti da un avvio in corso. Chromium ha 120 s per avviarsi e aprire la prima scheda, e il ricollegamento smette appena il processo cambia. Nelle prove i limiti di tempo diventano protezioni contro un blocco: attese per condizione con un aiuto comune, limiti per classe nella prova di scala con il tempo migliore, porta 0 e stderr di Chromium nel browser delle prove, e la prova di «Scollega» di WebView2 controlla che il processo esca davvero. Provato su Windows con tutti i core occupati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e2e64c09e8
commit
0775a1460f
34 files changed
+875
-245
No files matched your search
@@ -29,7 +29,12 @@ public sealed record ChromiumLaunchOptions
|
||||
/// <summary>Start page: always the rest page.</summary>
|
||||
public string StartUrl { get; init; } = "about:blank";
|
||||
|
||||
public TimeSpan StartupTimeout { get; init; } = TimeSpan.FromSeconds(30);
|
||||
/// <summary>
|
||||
/// How long a Chromium that is alive may take to open its debugging port and its first tab. A crash fails the start
|
||||
/// at once; this only bounds a browser that hangs, so it is generous for a loaded machine (docs/decisioni.md, #46).
|
||||
/// </summary>
|
||||
public TimeSpan StartupTimeout { get; init; } = TimeSpan.FromSeconds(120);
|
||||
|
||||
|
||||
public RestartPolicy Restart { get; init; } = RestartPolicy.Default;
|
||||
|
||||
|
||||
@@ -255,27 +255,37 @@ public sealed class ChromiumProcessHost : IBrowserHost
|
||||
$"La porta di debug {port} non è limitata a 127.0.0.1 ({listeners.Count} punti di ascolto): avvio annullato (specifica §6.1).");
|
||||
}
|
||||
|
||||
await ConnectAsync(port, ct).ConfigureAwait(false);
|
||||
await ConnectAsync(
|
||||
port,
|
||||
deadline,
|
||||
() => process.HasExited ? $"Chromium si è chiuso all'avvio (codice {process.ExitCode}). {_supervisor?.StderrTail()}".TrimEnd() : null,
|
||||
ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ConnectAsync(int port, CancellationToken ct)
|
||||
/// <summary>
|
||||
/// Connects to the first page target. The first tab may appear a while after the port opens on a loaded machine:
|
||||
/// the wait shares the start deadline, and ends at once when <paramref name="gone"/> says the process is no longer the
|
||||
/// one to connect to (it exited, or the supervisor replaced it): nobody waits on the connect lock for a dead browser.
|
||||
/// </summary>
|
||||
private async Task ConnectAsync(int port, DateTimeOffset deadline, Func<string?> gone, CancellationToken ct)
|
||||
{
|
||||
await _connectLock.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
CdpTarget? page = null;
|
||||
for (var attempt = 0; attempt < 50 && page is null; attempt++)
|
||||
CdpTarget? page;
|
||||
while ((page = await TryFindPageAsync(port, ct).ConfigureAwait(false)) is null)
|
||||
{
|
||||
page = await CdpTargetDiscovery.FindPageAsync(port, ct).ConfigureAwait(false);
|
||||
if (page is null)
|
||||
if (gone() is { } reason)
|
||||
{
|
||||
await _clock.Delay(TimeSpan.FromMilliseconds(100), ct).ConfigureAwait(false);
|
||||
throw new InvalidOperationException(reason);
|
||||
}
|
||||
}
|
||||
|
||||
if (page is null)
|
||||
{
|
||||
throw new InvalidOperationException("Chromium non ha nessuna scheda aperta.");
|
||||
if (_clock.Now >= deadline)
|
||||
{
|
||||
throw new InvalidOperationException("Chromium non ha nessuna scheda aperta.");
|
||||
}
|
||||
|
||||
await _clock.Delay(TimeSpan.FromMilliseconds(100), ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var client = await CdpClient.ConnectAsync(page.WebSocketDebuggerUrl, _log, null, ct).ConfigureAwait(false);
|
||||
@@ -304,6 +314,23 @@ public sealed class ChromiumProcessHost : IBrowserHost
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The page target, or null when there is none yet or the endpoint did not answer this time.</summary>
|
||||
private static async Task<CdpTarget?> TryFindPageAsync(int port, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await CdpTargetDiscovery.FindPageAsync(port, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
catch (TaskCanceledException) when (!ct.IsCancellationRequested)
|
||||
{
|
||||
return null; // a slow answer of /json/list, not a missing one
|
||||
}
|
||||
}
|
||||
|
||||
private void OnClientDisconnected(object? sender, Exception? reason)
|
||||
{
|
||||
if (Volatile.Read(ref _stopping) == 1 || Volatile.Read(ref _restarting) == 1 || !ReferenceEquals(sender, Volatile.Read(ref _client)))
|
||||
@@ -330,11 +357,19 @@ public sealed class ChromiumProcessHost : IBrowserHost
|
||||
return;
|
||||
}
|
||||
|
||||
for (var attempt = 0; attempt < 3; attempt++)
|
||||
// The process that dropped the connection: if it exits or the supervisor restarts it, stop at once and leave
|
||||
// the connect lock to the restart, which connects by itself.
|
||||
var processId = supervisor.ProcessId;
|
||||
string? Gone() => supervisor.State != SupervisedState.Running || supervisor.ProcessId != processId
|
||||
? "Chromium è stato riavviato o si è chiuso durante il ricollegamento."
|
||||
: null;
|
||||
|
||||
var deadline = _clock.Now + _launch.StartupTimeout;
|
||||
for (var attempt = 0; attempt < 3 && Gone() is null; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ConnectAsync(_options.DebugPort, ct).ConfigureAwait(false);
|
||||
await ConnectAsync(_options.DebugPort, deadline, Gone, ct).ConfigureAwait(false);
|
||||
_log.Info("Collegamento con Chromium ripristinato.");
|
||||
return;
|
||||
}
|
||||
@@ -344,6 +379,11 @@ public sealed class ChromiumProcessHost : IBrowserHost
|
||||
}
|
||||
}
|
||||
|
||||
if (Gone() is not null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_log.Warn("Collegamento con Chromium perso: riavvio del browser.");
|
||||
supervisor.KillCurrent();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user