55f46e7c92
Well-localised perf fixes across 8 modules.
Lock decoupling / SQL streaming:
- AuditLog-005: SqliteAuditWriter gains dedicated read-only _readConnection
(+ _readLock) backed by WAL journal mode. GetBacklogStatsAsync,
ReadPendingAsync, ReadPendingSinceAsync, ReadForwardedAsync no longer
contend with the hot-path INSERT lock — backlog probes on a 30s timer
can't stall the writer under multi-hundred-K Pending backlog.
- SEL-022: dropped Cache=Shared from SiteEventLogger's default connection
string (single-connection logger; mode was dormant config).
Memory / streaming:
- CLI-019: bundle export streams base64 in 1 MB-aligned chunks via
Convert.TryFromBase64Chars straight into the FileStream — no more
full-bundle byte[] allocation.
- CentralUI-031: TransportImport now stages the upload to a per-session
temp file under Path.GetTempPath() (replaces in-memory byte[] field);
page implements IDisposable to delete the temp file on reset / new
upload / dispose. Per-circuit working set drops from ~100 MB to ~80 KB.
N+1 hoisting:
- Transport-008: added ITemplateEngineRepository.GetTemplatesWithChildrenAsync
bulk method; BundleImporter.PreviewAsync calls it once instead of per-
template-name. Single query with .Include(...).AsSplitQuery().
- DM-023: BuildDeployArtifactsCommandAsync's per-site loop now references
a pre-fetched GlobalArtifactSnapshot (shared scripts, external systems,
DB connections, notification lists, SMTP) instead of re-querying per site.
- MgmtSvc-023: HandleQueryDeployments unfiltered branch uses one
GetAllInstancesAsync bulk load + Dictionary<int,int?> lookup (was a
GetInstanceByIdAsync per record).
Small allocations / per-tick rebuilds:
- InboundAPI-019: AuditWriteMiddleware gates EnableBuffering() on
RequestHasBody() so GET/HEAD/DELETE/TRACE/OPTIONS and Content-Length:0
requests skip the FileBufferingReadStream allocation.
- NotifOutbox-006: ResolveAdapters dictionary now cached on
_adaptersCache (built lazily on first sweep) + actor-lifetime
_adaptersScope; ResolveAdapters no longer rebuilds per dispatch tick.
Verify-only:
- Comm-017: Confirmed _inProgressDeployments was deleted by Comm-016 in
commit ac96b83 — marked Resolved with that attribution. No code change.
Doc-correction:
- NS-022: Updated MailKitSmtpClientWrapper XML doc to spell out single-
connection / per-delivery-factory contract (option (b) — transient
client per Send — rejected because it re-handshakes TLS per email).
10+ new regression tests across 8 test projects. Build clean; affected
suites all green. README regenerated: 54 open (was 65).
192 lines
7.4 KiB
C#
192 lines
7.4 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using ScadaLink.AuditLog.Site;
|
|
using ScadaLink.AuditLog.Tests.TestSupport;
|
|
using ScadaLink.Commons.Entities.Audit;
|
|
using ScadaLink.Commons.Types.Enums;
|
|
|
|
namespace ScadaLink.AuditLog.Tests.Site;
|
|
|
|
/// <summary>
|
|
/// Bundle E (M6-T6) tests for <see cref="SqliteAuditWriter.GetBacklogStatsAsync"/>.
|
|
/// Exercises the health-metric surface that <c>SiteAuditBacklogReporter</c>
|
|
/// polls every 30 s and pushes onto the site health report as
|
|
/// <c>SiteAuditBacklog</c>.
|
|
/// </summary>
|
|
public class SqliteAuditWriterBacklogStatsTests : IDisposable
|
|
{
|
|
private readonly string _dbPath;
|
|
|
|
public SqliteAuditWriterBacklogStatsTests()
|
|
{
|
|
// OnDiskBytes assertions only make sense against a real file — the
|
|
// shared-cache in-memory mode returns 0 for the file size, so this
|
|
// suite is opinionated about file-backed storage. Tests in
|
|
// SqliteAuditWriterWriteTests use in-memory for performance reasons.
|
|
_dbPath = Path.Combine(Path.GetTempPath(),
|
|
$"audit-backlog-stats-{Guid.NewGuid():N}.db");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (File.Exists(_dbPath))
|
|
{
|
|
try { File.Delete(_dbPath); } catch { /* test cleanup best-effort */ }
|
|
}
|
|
}
|
|
|
|
private SqliteAuditWriter CreateWriter()
|
|
{
|
|
var options = new SqliteAuditWriterOptions { DatabasePath = _dbPath };
|
|
return new SqliteAuditWriter(
|
|
Options.Create(options),
|
|
NullLogger<SqliteAuditWriter>.Instance,
|
|
new FakeNodeIdentityProvider());
|
|
}
|
|
|
|
private static AuditEvent NewEvent(DateTime? occurredAtUtc = null) => new()
|
|
{
|
|
EventId = Guid.NewGuid(),
|
|
OccurredAtUtc = occurredAtUtc ?? DateTime.UtcNow,
|
|
Channel = AuditChannel.ApiOutbound,
|
|
Kind = AuditKind.ApiCall,
|
|
Status = AuditStatus.Delivered,
|
|
PayloadTruncated = false,
|
|
};
|
|
|
|
[Fact]
|
|
public async Task EmptyDb_Returns_Zero_Null_AndZeroBytes()
|
|
{
|
|
// No file exists yet — the writer ctor creates one but no rows are
|
|
// inserted; the snapshot should report a clean queue. OnDiskBytes is
|
|
// allowed to be zero (fresh ftruncate) OR small (page header) — the
|
|
// contract only requires non-negative; we assert >= 0 and exercise
|
|
// the pending fields strictly.
|
|
await using var writer = CreateWriter();
|
|
|
|
var snapshot = await writer.GetBacklogStatsAsync();
|
|
|
|
Assert.Equal(0, snapshot.PendingCount);
|
|
Assert.Null(snapshot.OldestPendingUtc);
|
|
Assert.True(snapshot.OnDiskBytes >= 0,
|
|
$"OnDiskBytes must be non-negative, got {snapshot.OnDiskBytes}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Pending_5_Returns_5()
|
|
{
|
|
await using var writer = CreateWriter();
|
|
|
|
for (var i = 0; i < 5; i++)
|
|
{
|
|
await writer.WriteAsync(NewEvent());
|
|
}
|
|
|
|
var snapshot = await writer.GetBacklogStatsAsync();
|
|
|
|
Assert.Equal(5, snapshot.PendingCount);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OldestPending_Is_Earliest_OccurredAtUtc()
|
|
{
|
|
await using var writer = CreateWriter();
|
|
|
|
var t1 = new DateTime(2026, 5, 20, 10, 0, 0, DateTimeKind.Utc);
|
|
var t2 = new DateTime(2026, 5, 20, 10, 1, 0, DateTimeKind.Utc);
|
|
var t3 = new DateTime(2026, 5, 20, 10, 2, 0, DateTimeKind.Utc);
|
|
|
|
// Insert out of order so the snapshot is not "the last write" by
|
|
// accident — the OldestPendingUtc must come from a column-min, not
|
|
// an insertion-order proxy.
|
|
await writer.WriteAsync(NewEvent(t2));
|
|
await writer.WriteAsync(NewEvent(t1));
|
|
await writer.WriteAsync(NewEvent(t3));
|
|
|
|
var snapshot = await writer.GetBacklogStatsAsync();
|
|
|
|
Assert.Equal(3, snapshot.PendingCount);
|
|
Assert.NotNull(snapshot.OldestPendingUtc);
|
|
// The DB round-trips OccurredAtUtc through the "o" format which
|
|
// preserves Kind=Utc — assert tick-equality.
|
|
Assert.Equal(t1, snapshot.OldestPendingUtc!.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetBacklogStatsAsync_DoesNotBlockOnConcurrentWriteLoad()
|
|
{
|
|
// AuditLog-005: GetBacklogStatsAsync previously took _writeLock, the
|
|
// same lock that serialises every batch INSERT in FlushBatch. Under a
|
|
// backlog growing to hundreds of thousands of rows a COUNT(*)+MIN
|
|
// index scan could park the hot-path writer for hundreds of ms. The
|
|
// fix adds a dedicated read-only connection in WAL mode so the probe
|
|
// never contends with the writer.
|
|
//
|
|
// This test demonstrates the lock decoupling by saturating the writer
|
|
// with a burst of concurrent writes and asserting that a probe issued
|
|
// while those writes are in flight returns inside a tight time bound.
|
|
// Without the fix the probe would be queued behind FlushBatch under
|
|
// the same _writeLock; with the fix it reads through _readConnection
|
|
// and is not gated by the writer.
|
|
await using var writer = CreateWriter();
|
|
|
|
// Seed a baseline so MIN(OccurredAtUtc) has a row to find — the
|
|
// important assertion is timing, but a non-empty result also confirms
|
|
// the read connection sees the writer's commits via WAL.
|
|
for (var i = 0; i < 100; i++)
|
|
{
|
|
await writer.WriteAsync(NewEvent());
|
|
}
|
|
|
|
// Kick off a sustained write burst on a background task. The writes
|
|
// are fire-and-forget — we only need the writer to be busy enough
|
|
// that any reuse of _writeLock by the probe would be observable.
|
|
var burst = Task.Run(async () =>
|
|
{
|
|
for (var i = 0; i < 2_000; i++)
|
|
{
|
|
await writer.WriteAsync(NewEvent()).ConfigureAwait(false);
|
|
}
|
|
});
|
|
|
|
// Race the probe against the write burst. The probe must return
|
|
// promptly even though the writer is actively flushing batches.
|
|
var sw = System.Diagnostics.Stopwatch.StartNew();
|
|
var snapshot = await writer.GetBacklogStatsAsync();
|
|
sw.Stop();
|
|
|
|
// Drain the burst before disposing so we don't observe a flake when
|
|
// pending writes race with dispose.
|
|
await burst;
|
|
|
|
Assert.True(sw.ElapsedMilliseconds < 1_000,
|
|
$"GetBacklogStatsAsync must not block on the writer's _writeLock; took {sw.ElapsedMilliseconds} ms");
|
|
Assert.True(snapshot.PendingCount >= 100,
|
|
$"backlog probe should see at least the seeded rows; got {snapshot.PendingCount}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OnDiskBytes_ReturnsFileSize()
|
|
{
|
|
await using var writer = CreateWriter();
|
|
|
|
// Insert enough rows to grow the file past the empty schema baseline.
|
|
for (var i = 0; i < 100; i++)
|
|
{
|
|
await writer.WriteAsync(NewEvent());
|
|
}
|
|
|
|
var snapshot = await writer.GetBacklogStatsAsync();
|
|
|
|
// The exact size depends on SQLite page allocation, but a file-backed
|
|
// db with 100 inserted rows MUST be larger than the empty schema
|
|
// (a few pages, ~4 KB). The implementation should return the
|
|
// FileInfo.Length value verbatim.
|
|
Assert.True(File.Exists(_dbPath), $"DB file should exist at {_dbPath}");
|
|
var expected = new FileInfo(_dbPath).Length;
|
|
Assert.Equal(expected, snapshot.OnDiskBytes);
|
|
Assert.True(snapshot.OnDiskBytes > 0,
|
|
$"after 100 inserts OnDiskBytes must be > 0, got {snapshot.OnDiskBytes}");
|
|
}
|
|
}
|