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>
66 lines
2.9 KiB
C#
66 lines
2.9 KiB
C#
using System.Runtime.CompilerServices;
|
|
using Febe.Data;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Data;
|
|
|
|
/// <summary>
|
|
/// #46: <see cref="FebeDatabase.DisposeAsync"/> must really close the database, even when the finalizer thread is behind.
|
|
/// A command left to the garbage collector keeps its SQLite statement until it is finalized; a connection closed
|
|
/// meanwhile only closes then, and its WAL disappeared under the move of the Data folder. The class runs alone because
|
|
/// it holds the process-wide finalizer thread for a moment.
|
|
/// </summary>
|
|
[TestClass]
|
|
[DoNotParallelize]
|
|
public sealed class DatabaseCloseTests
|
|
{
|
|
public TestContext TestContext { get; set; } = null!;
|
|
|
|
[TestMethod]
|
|
[Timeout(120_000, CooperativeCancellation = true)]
|
|
public async Task DisposeClosesTheFilesEvenWhenTheFinalizerIsBehind()
|
|
{
|
|
using var temp = new TempFolder();
|
|
using var finalizing = new ManualResetEventSlim();
|
|
using var release = new ManualResetEventSlim();
|
|
var folder = temp.Combine("dati");
|
|
try
|
|
{
|
|
// Holds the finalizer thread: whatever the next collections queue for finalization waits behind it.
|
|
BlockTheFinalizer(finalizing, release);
|
|
GC.Collect();
|
|
Assert.IsTrue(finalizing.Wait(TimeSpan.FromSeconds(30), TestContext.CancellationToken), "Il finalizzatore non è partito.");
|
|
|
|
var db = await FebeDatabase.OpenAsync(folder, TestContext.CancellationToken);
|
|
await db.Settings.SetAsync("prova", "1", TestContext.CancellationToken);
|
|
Assert.AreEqual("1", await db.Settings.GetAsync("prova", TestContext.CancellationToken));
|
|
GC.Collect(); // commands nobody disposed are collected now and wait for the finalizer
|
|
await db.DisposeAsync();
|
|
|
|
Assert.IsFalse(File.Exists(Path.Combine(folder, "febe.sqlite-wal")), "Il WAL resta dopo la chiusura.");
|
|
Assert.IsFalse(File.Exists(Path.Combine(folder, "febe.sqlite-shm")), "Il file -shm resta dopo la chiusura.");
|
|
await using var again = await FebeDatabase.OpenAsync(folder, TestContext.CancellationToken);
|
|
Assert.AreEqual("1", await again.Settings.GetAsync("prova", TestContext.CancellationToken));
|
|
}
|
|
finally
|
|
{
|
|
release.Set();
|
|
GC.Collect();
|
|
GC.WaitForPendingFinalizers();
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
private static void BlockTheFinalizer(ManualResetEventSlim finalizing, ManualResetEventSlim release) =>
|
|
_ = new FinalizerBlocker(finalizing, release);
|
|
|
|
private sealed class FinalizerBlocker(ManualResetEventSlim finalizing, ManualResetEventSlim release)
|
|
{
|
|
~FinalizerBlocker()
|
|
{
|
|
finalizing.Set();
|
|
release.Wait(TimeSpan.FromSeconds(30)); // bounded: the finalizer thread never stays blocked for good
|
|
}
|
|
}
|
|
}
|