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>
148 lines
5.8 KiB
C#
148 lines
5.8 KiB
C#
using Febe.Core.Abstractions;
|
|
using Febe.Core.Domain;
|
|
using Febe.Data;
|
|
using Febe.Tests.Support;
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
namespace Febe.Tests.Data;
|
|
|
|
[TestClass]
|
|
public sealed class SingleWriterTests
|
|
{
|
|
[TestMethod]
|
|
public async Task ConcurrentWritesAreSerializedAndAllCommitted()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var account = await db.Accounts.AddAsync(Make.Account("marta.demo"));
|
|
const int writers = 24;
|
|
const int perWriter = 50;
|
|
|
|
using var stopReaders = new CancellationTokenSource();
|
|
var readers = Enumerable.Range(0, 4).Select(_ => Task.Run(async () =>
|
|
{
|
|
var reads = 0;
|
|
while (!stopReaders.IsCancellationRequested)
|
|
{
|
|
await db.ActionLog.SumAsync(ActionKind.StoryView, Make.T0.AddDays(-1), Make.T0.AddDays(1));
|
|
reads++;
|
|
}
|
|
|
|
return reads;
|
|
})).ToList();
|
|
|
|
var tasks = Enumerable.Range(0, writers).Select(w => Task.Run(async () =>
|
|
{
|
|
for (var i = 0; i < perWriter; i++)
|
|
{
|
|
await db.ActionLog.AppendAsync(new ActionLogRecord
|
|
{
|
|
At = Make.T0.AddSeconds((w * perWriter) + i),
|
|
Category = ActionKind.StoryView,
|
|
AccountId = account.Id,
|
|
});
|
|
}
|
|
}));
|
|
await Task.WhenAll(tasks);
|
|
await stopReaders.CancelAsync();
|
|
var readCounts = await Task.WhenAll(readers);
|
|
|
|
var total = await db.ActionLog.SumAsync(ActionKind.StoryView, Make.T0.AddDays(-1), Make.T0.AddDays(1));
|
|
Assert.AreEqual((long)writers * perWriter, total);
|
|
Assert.AreEqual(1, db.Writer.MaxObservedConcurrency, "Le scritture devono passare una alla volta.");
|
|
Assert.IsTrue(readCounts.All(c => c > 0), "Le letture proseguono mentre si scrive.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task WritesRunOnTheWriterThread()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var threads = await Task.WhenAll(Enumerable.Range(0, 20).Select(_ => Task.Run(() => db.WriteAsync(_ => Environment.CurrentManagedThreadId, default))));
|
|
Assert.IsTrue(threads.All(t => t == db.Writer.ThreadId));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AFailingWriteDoesNotUndoTheOthers()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
using var gate = new ManualResetEventSlim(false);
|
|
|
|
// Hold the writer so that the next writes are queued together and share one transaction.
|
|
var blocker = db.WriteAsync(_ => gate.Wait(TimeSpan.FromSeconds(10)), default);
|
|
var good1 = db.Settings.SetAsync("a", "1");
|
|
var bad = db.WriteAsync(w => w.Command("INSERT INTO media (entry_id, position, kind, state) VALUES (424242, 1, 'Photo', 'ToDownload')").ExecuteNonQuery(), default);
|
|
var good2 = db.Settings.SetAsync("b", "2");
|
|
gate.Set();
|
|
|
|
await blocker;
|
|
await good1;
|
|
await good2;
|
|
await Assert.ThrowsExactlyAsync<SqliteException>(() => bad);
|
|
Assert.AreEqual("1", await db.Settings.GetAsync("a"));
|
|
Assert.AreEqual("2", await db.Settings.GetAsync("b"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AWriteCancelledWhileQueuedIsNotExecuted()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
using var gate = new ManualResetEventSlim(false);
|
|
using var cancel = new CancellationTokenSource();
|
|
|
|
var blocker = db.WriteAsync(_ => gate.Wait(TimeSpan.FromSeconds(10)), default);
|
|
var cancelled = db.Settings.SetAsync("annullata", "1", cancel.Token);
|
|
await cancel.CancelAsync();
|
|
gate.Set();
|
|
await blocker;
|
|
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => cancelled);
|
|
await db.Settings.SetAsync("dopo", "1");
|
|
Assert.IsNull(await db.Settings.GetAsync("annullata"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ReadsAreNotBlockedByALongWrite()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
await db.Settings.SetAsync("prima", "1");
|
|
using var inside = new ManualResetEventSlim(false);
|
|
using var gate = new ManualResetEventSlim(false);
|
|
|
|
// The write holds the writer and its transaction until the gate opens (at most 60 s, only a hang guard).
|
|
var longWrite = db.WriteAsync(
|
|
w =>
|
|
{
|
|
w.Command("INSERT INTO setting (key, value) VALUES ('durante', '1')").ExecuteNonQuery();
|
|
inside.Set();
|
|
gate.Wait(TimeSpan.FromSeconds(60));
|
|
},
|
|
default);
|
|
Assert.IsTrue(inside.Wait(TimeSpan.FromSeconds(60)), "La scrittura lunga non è partita.");
|
|
|
|
// An ordering, not a time (#46): the reads end while the write still holds its uncommitted transaction.
|
|
var value = await db.Settings.GetAsync("prima");
|
|
var during = await db.Settings.GetAsync("durante");
|
|
Assert.IsFalse(longWrite.IsCompleted, "Le letture sono finite solo dopo la scrittura lunga.");
|
|
gate.Set();
|
|
await longWrite;
|
|
|
|
Assert.AreEqual("1", value);
|
|
Assert.IsNull(during, "Una lettura non vede una scrittura non ancora confermata.");
|
|
Assert.AreEqual("1", await db.Settings.GetAsync("durante"));
|
|
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task WritesAfterDisposeFail()
|
|
{
|
|
using var temp = new TempFolder();
|
|
var db = await TestDatabase.OpenAsync(temp);
|
|
await db.DisposeAsync();
|
|
await Assert.ThrowsExactlyAsync<ObjectDisposedException>(() => db.Settings.SetAsync("x", "1"));
|
|
}
|
|
}
|