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>
68 lines
3.0 KiB
C#
68 lines
3.0 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace Febe.Tests.Support;
|
|
|
|
/// <summary>
|
|
/// Waits by condition and timings that a loaded runner does not fail by chance (#46). A wait returns as soon as its
|
|
/// condition holds; its limit only bounds a hang, so it is generous. A timing keeps the best of repeated runs: load only
|
|
/// adds time, so the minimum is the cost of the operation itself.
|
|
/// </summary>
|
|
internal static class TestWait
|
|
{
|
|
/// <summary>The default hang guard of a condition wait.</summary>
|
|
public static readonly TimeSpan Patience = TimeSpan.FromSeconds(30);
|
|
|
|
/// <summary>Polls <paramref name="condition"/> until it holds; fails with <paramref name="what"/> after the limit.</summary>
|
|
public static Task UntilAsync(Func<bool> condition, string what, TimeSpan? limit = null, CancellationToken ct = default) =>
|
|
UntilAsync(() => Task.FromResult(condition()), what, limit, ct);
|
|
|
|
/// <summary>Polls <paramref name="condition"/> until it holds; fails with <paramref name="what"/> after the limit.</summary>
|
|
public static async Task UntilAsync(Func<Task<bool>> condition, string what, TimeSpan? limit = null, CancellationToken ct = default)
|
|
{
|
|
var patience = limit ?? Patience;
|
|
var watch = Stopwatch.StartNew();
|
|
while (!await condition())
|
|
{
|
|
if (watch.Elapsed > patience)
|
|
{
|
|
Assert.Fail($"Tempo scaduto ({patience.TotalSeconds:0} s): {what}.");
|
|
}
|
|
|
|
await Task.Delay(20, ct);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Times <paramref name="run"/> after one warm-up run, at least three times and then until the best time is under
|
|
/// <paramref name="boundMs"/> or the budget (<paramref name="maxRuns"/> runs, <paramref name="budget"/> of wall time)
|
|
/// runs out. A load spike only delays the result; an operation that is really slower stays over the bound.
|
|
/// </summary>
|
|
public static async Task<Timing<T>> BestTimeAsync<T>(
|
|
Func<Task<T>> run, double boundMs, int maxRuns = 15, TimeSpan? budget = null, CancellationToken ct = default)
|
|
{
|
|
var limit = budget ?? TimeSpan.FromSeconds(20);
|
|
var result = await run(); // warm the caches
|
|
var best = double.MaxValue;
|
|
var runs = 0;
|
|
var wall = Stopwatch.StartNew();
|
|
while (runs < 3 || (best >= boundMs && runs < maxRuns && wall.Elapsed < limit))
|
|
{
|
|
ct.ThrowIfCancellationRequested();
|
|
if (runs >= 3)
|
|
{
|
|
await Task.Delay(100, ct); // let a burst of load pass before trying again
|
|
}
|
|
|
|
var watch = Stopwatch.StartNew();
|
|
result = await run();
|
|
best = Math.Min(best, watch.Elapsed.TotalMilliseconds);
|
|
runs++;
|
|
}
|
|
|
|
return new Timing<T>(best, runs, result);
|
|
}
|
|
}
|
|
|
|
/// <summary>The best time of <see cref="TestWait.BestTimeAsync"/>, how many runs it took and the last result.</summary>
|
|
internal readonly record struct Timing<T>(double BestMs, int Runs, T Result);
|