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
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace Febe.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Keeps the commands that <see cref="Sql.Command"/> creates on this thread while a read runs, and disposes them when the
|
||||
/// read ends. Reads build their commands inline (<c>c.Command(...).ExecuteReader()</c>); left to the garbage collector,
|
||||
/// a command keeps its SQLite statement alive until the finalizer runs, so the connection closed by
|
||||
/// <see cref="FebeDatabase.DisposeAsync"/> would only really close then, keeping the files open (#46).
|
||||
/// </summary>
|
||||
internal sealed class CommandScope : IDisposable
|
||||
{
|
||||
[ThreadStatic]
|
||||
private static CommandScope? t_current;
|
||||
|
||||
private readonly CommandScope? _outer;
|
||||
private readonly List<SqliteCommand> _commands = [];
|
||||
|
||||
private CommandScope(CommandScope? outer) => _outer = outer;
|
||||
|
||||
/// <summary>Starts tracking the commands created on this thread until the scope is disposed.</summary>
|
||||
public static CommandScope Begin() => t_current = new CommandScope(t_current);
|
||||
|
||||
/// <summary>Adds <paramref name="command"/> to the open scope of this thread, if any.</summary>
|
||||
public static void Track(SqliteCommand command) => t_current?._commands.Add(command);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
t_current = _outer;
|
||||
foreach (var command in _commands)
|
||||
{
|
||||
command.Dispose();
|
||||
}
|
||||
|
||||
_commands.Clear();
|
||||
}
|
||||
}
|
||||
@@ -118,16 +118,16 @@ public sealed class FebeDatabase : IFebeStorage, IDatabaseMaintenance
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
var mode = connection.Command("PRAGMA journal_mode = WAL").ExecuteScalar() as string;
|
||||
var mode = connection.Scalar("PRAGMA journal_mode = WAL") as string;
|
||||
if (!string.Equals(mode, "wal", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new DatabaseMigrationException(
|
||||
$"Febe non riesce a mettere il database in modalità WAL (modalità attuale: {mode}). La cartella dei dati deve stare su un disco locale.");
|
||||
}
|
||||
|
||||
connection.Command("PRAGMA synchronous = NORMAL").ExecuteNonQuery();
|
||||
connection.Command($"PRAGMA busy_timeout = {BusyTimeoutMs}").ExecuteNonQuery();
|
||||
connection.Command("PRAGMA foreign_keys = ON").ExecuteNonQuery();
|
||||
connection.Execute("PRAGMA synchronous = NORMAL");
|
||||
connection.Execute($"PRAGMA busy_timeout = {BusyTimeoutMs}");
|
||||
connection.Execute("PRAGMA foreign_keys = ON");
|
||||
var version = MigrationRunner.Apply(connection, migrations);
|
||||
return new FebeDatabase(path, connection, readConnectionString, version);
|
||||
}
|
||||
@@ -155,6 +155,7 @@ public sealed class FebeDatabase : IFebeStorage, IDatabaseMaintenance
|
||||
try
|
||||
{
|
||||
using var connection = OpenReadConnection();
|
||||
using var commands = CommandScope.Begin(); // disposed first: the connection then really closes
|
||||
return Task.FromResult(work(connection));
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
@@ -184,7 +185,8 @@ public sealed class FebeDatabase : IFebeStorage, IDatabaseMaintenance
|
||||
connection =>
|
||||
{
|
||||
// VACUUM INTO reads a consistent snapshot; it works on a read-only connection and does not block the writer.
|
||||
connection.Command("VACUUM INTO @destination").With("@destination", destination).ExecuteNonQuery();
|
||||
using var vacuum = connection.Command("VACUUM INTO @destination").With("@destination", destination);
|
||||
vacuum.ExecuteNonQuery();
|
||||
return true;
|
||||
},
|
||||
ct);
|
||||
@@ -210,9 +212,17 @@ public sealed class FebeDatabase : IFebeStorage, IDatabaseMaintenance
|
||||
}
|
||||
|
||||
Writer.Dispose();
|
||||
|
||||
// The idle read connections close first, so the writer is the last connection: only the writer can checkpoint
|
||||
// and delete febe.sqlite-wal and -shm when it closes (a read-only connection leaves them behind).
|
||||
using (var pooled = new SqliteConnection(_readConnectionString))
|
||||
{
|
||||
SqliteConnection.ClearPool(pooled);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_writerConnection.Command("PRAGMA wal_checkpoint(TRUNCATE)").ExecuteNonQuery();
|
||||
_writerConnection.Execute("PRAGMA wal_checkpoint(TRUNCATE)");
|
||||
}
|
||||
catch (SqliteException)
|
||||
{
|
||||
@@ -220,11 +230,6 @@ public sealed class FebeDatabase : IFebeStorage, IDatabaseMaintenance
|
||||
}
|
||||
|
||||
_writerConnection.Dispose();
|
||||
using (var pooled = new SqliteConnection(_readConnectionString))
|
||||
{
|
||||
SqliteConnection.ClearPool(pooled);
|
||||
}
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ internal static partial class MigrationRunner
|
||||
}
|
||||
|
||||
public static int GetVersion(SqliteConnection connection) =>
|
||||
(int)connection.Command("PRAGMA user_version").ScalarInt64();
|
||||
Convert.ToInt32(connection.Scalar("PRAGMA user_version"), CultureInfo.InvariantCulture);
|
||||
|
||||
/// <summary>Applies the pending migrations. Returns the resulting version.</summary>
|
||||
public static int Apply(SqliteConnection connection, IReadOnlyList<Migration> migrations)
|
||||
@@ -97,7 +97,7 @@ internal static partial class MigrationRunner
|
||||
}
|
||||
|
||||
// Table rebuilds in later migrations need foreign keys off; they are checked before each commit instead.
|
||||
connection.Command("PRAGMA foreign_keys = OFF").ExecuteNonQuery();
|
||||
connection.Execute("PRAGMA foreign_keys = OFF");
|
||||
try
|
||||
{
|
||||
foreach (var migration in migrations.Where(m => m.Version > current))
|
||||
@@ -105,8 +105,9 @@ internal static partial class MigrationRunner
|
||||
using var transaction = connection.BeginTransaction(deferred: false);
|
||||
try
|
||||
{
|
||||
connection.Command(migration.Sql, transaction).ExecuteNonQuery();
|
||||
using (var check = connection.Command("PRAGMA foreign_key_check", transaction).ExecuteReader())
|
||||
connection.Execute(migration.Sql, transaction);
|
||||
using (var checkCommand = connection.Command("PRAGMA foreign_key_check", transaction))
|
||||
using (var check = checkCommand.ExecuteReader())
|
||||
{
|
||||
if (check.Read())
|
||||
{
|
||||
@@ -115,8 +116,7 @@ internal static partial class MigrationRunner
|
||||
}
|
||||
}
|
||||
|
||||
connection.Command(string.Create(CultureInfo.InvariantCulture, $"PRAGMA user_version = {migration.Version}"), transaction)
|
||||
.ExecuteNonQuery();
|
||||
connection.Execute(string.Create(CultureInfo.InvariantCulture, $"PRAGMA user_version = {migration.Version}"), transaction);
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex) when (ex is SqliteException or DatabaseMigrationException)
|
||||
@@ -130,7 +130,7 @@ internal static partial class MigrationRunner
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Command("PRAGMA foreign_keys = ON").ExecuteNonQuery();
|
||||
connection.Execute("PRAGMA foreign_keys = ON");
|
||||
}
|
||||
|
||||
return GetVersion(connection);
|
||||
|
||||
@@ -75,9 +75,28 @@ internal static class Sql
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = sql;
|
||||
command.Transaction = transaction;
|
||||
CommandScope.Track(command);
|
||||
return command;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a statement and disposes its command at once. A command left to the garbage collector keeps its SQLite
|
||||
/// statement alive until the finalizer runs, and a connection closed meanwhile only closes then: the WAL and its
|
||||
/// handles outlive <see cref="FebeDatabase.DisposeAsync"/> (#46).
|
||||
/// </summary>
|
||||
public static int Execute(this SqliteConnection connection, string sql, SqliteTransaction? transaction = null)
|
||||
{
|
||||
using var command = connection.Command(sql, transaction);
|
||||
return command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>Runs a query for one value and disposes its command at once (see <see cref="Execute"/>).</summary>
|
||||
public static object? Scalar(this SqliteConnection connection, string sql)
|
||||
{
|
||||
using var command = connection.Command(sql);
|
||||
return command.ExecuteScalar();
|
||||
}
|
||||
|
||||
/// <summary>Sets a parameter, adding it the first time: a prepared command can be run again with new values.</summary>
|
||||
public static SqliteCommand With(this SqliteCommand command, string name, object? value)
|
||||
{
|
||||
|
||||
@@ -221,14 +221,21 @@ internal sealed class AccountStore(FebeDatabase db) : IAccountStore
|
||||
}
|
||||
}
|
||||
|
||||
var bytes = c.Command("""
|
||||
SELECT COALESCE(SUM(m.bytes), 0) FROM media m JOIN entry e ON e.id = m.entry_id
|
||||
WHERE e.account_id = @a AND m.state IN ('Downloaded', 'Imported')
|
||||
""").With("@a", accountId).ScalarInt64();
|
||||
var pending = c.Command("""
|
||||
SELECT COUNT(*) FROM media m JOIN entry e ON e.id = m.entry_id
|
||||
WHERE e.account_id = @a AND m.state IN ('ToDownload', 'Expired')
|
||||
""").With("@a", accountId).ScalarInt64();
|
||||
// One pass over the account's files, driven from its entries (CROSS JOIN keeps entry as the outer loop):
|
||||
// without ANALYZE statistics SQLite otherwise starts from the media state index and reads the files of
|
||||
// the whole library for one account's card (#46).
|
||||
long bytes, pending;
|
||||
using (var r = c.Command("""
|
||||
SELECT COALESCE(SUM(CASE WHEN m.state IN ('Downloaded', 'Imported') THEN m.bytes END), 0),
|
||||
COALESCE(SUM(m.state IN ('ToDownload', 'Expired')), 0)
|
||||
FROM entry e CROSS JOIN media m ON m.entry_id = e.id
|
||||
WHERE e.account_id = @a
|
||||
""").With("@a", accountId).ExecuteReader())
|
||||
{
|
||||
r.Read();
|
||||
bytes = r.GetInt64(0);
|
||||
pending = r.GetInt64(1);
|
||||
}
|
||||
var collections = c.Command("SELECT COUNT(*) FROM highlight WHERE account_id = @a AND removed_at IS NULL")
|
||||
.With("@a", accountId).ScalarInt64();
|
||||
|
||||
|
||||
Reference in new issue
Block a user