f2efeb37b7
Tasks 5 and 6 of the Phase 2 plan, committed together because their test
fallout is entangled — several fixtures construct both stores.
StoreAndForwardStorage and SiteStorageService now take ILocalDb. Connections
come from ILocalDb.CreateConnection(), which hands out an already-open,
pragma-configured connection carrying the zb_hlc_next() UDF the capture triggers
call; a raw connection would lack the UDF and every write to a replicated table
would fail closed. Deleted with the connection strings: S&F's
EnsureDatabaseDirectoryExists and its per-open busy_timeout pragma, and the site
service's BusyTimeoutFloorSeconds normalization — LocalDb owns all of it now.
DI: AddSiteRuntime's string overload is gone (nothing left to supply), so the
Host calls the no-arg form. ScadaBridge:Database:SiteDbPath and
StoreAndForwardOptions.SqliteDbPath survive only as the migrator's source
locations in Tasks 8/9.
Two things the plan did not anticipate, both worth reading:
1. FOUND A REAL LATENT DEFECT, from Phase 1, now fixed. The plan assumed
directory creation simply moved to LocalDb along with file ownership. It did
not: the LocalDb library never creates the parent directory, and
SqliteLocalDb opens the file eagerly in its constructor — so a missing
directory is a hard boot failure ("SQLite Error 14: unable to open database
file"), not a degraded start. The default site config points at the RELATIVE
path ./data/site-localdb.db, so any site node without a pre-existing data/
directory fails to boot. The docker rig escapes only because its volume mount
happens to create /app/data — a coincidence that would have hidden this until
a bare-metal or fresh deployment. This has been latent since Phase 1 made
LocalDb:Path required; deleting S&F's EnsureDatabaseDirectoryExists here
would have widened it. Re-established the guarantee at the layer that now
owns the path (SiteLocalDbDirectory.Ensure, called before AddZbLocalDb) and
pinned it with SiteLocalDbDirectoryTests. Non-vacuity is not assumed: two
tests written against the wrong assumption failed with exactly this
SQLite Error 14 before the fix existed.
2. Test fallout was ~7x the plan's estimate. The plan named "fixtures" in one
project; the constructor change actually reaches 40 files across 7 test
projects, and most used Mode=Memory;Cache=Shared — which LocalDb has no
equivalent for, so every one had to move to a real temp file. Rather than
copy the Phase 1 TestLocalDb fixture into 7 projects, added a shared
tests/ZB.MOM.WW.ScadaBridge.TestSupport library (not a test project) so the
WAL-sidecar cleanup and the "real, not stubbed" rationale live in one place.
Retargeted rather than deleted, in both directions: the S&F WAL test now asserts
against the LocalDb-backed store (WAL genuinely is LocalDb's job), while the
directory-creation test moved to Host.Tests (that guarantee is NOT LocalDb's).
SiteStorageServiceTests.Initialize_EnablesWalJournalMode got the same treatment.
DeploymentManagerMediumFindingsTests induced a persistence failure via an
unopenable path, which no longer reaches the assertion since the fixture now
throws first; it induces the same failure shape via an uninitialized store.
Verified: full solution build 0 warnings; SiteRuntime 532, Host 318,
AuditLog 355, ExternalSystemGateway 142, HealthMonitoring 97,
StoreAndForward 153 — 1597 passed, 0 failed.
Claude-Session: https://claude.ai/code/session_01BL2Vu1ESDQ9SCN4gVKkdts
434 lines
17 KiB
C#
434 lines
17 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.StoreAndForward;
|
|
using ZB.MOM.WW.ScadaBridge.TestSupport;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.HealthMonitoring.Tests;
|
|
|
|
public class HealthReportSenderTests
|
|
{
|
|
private class FakeTransport : IHealthReportTransport
|
|
{
|
|
public List<SiteHealthReport> SentReports { get; } = [];
|
|
public Task SendAsync(SiteHealthReport report, CancellationToken cancellationToken)
|
|
{
|
|
SentReports.Add(report);
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private class FakeSiteIdentityProvider : ISiteIdentityProvider
|
|
{
|
|
public string SiteId { get; set; } = "test-site";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Captures emitted log entries so tests can assert that non-fatal failures
|
|
/// are surfaced (HealthMonitoring-010) rather than silently swallowed.
|
|
/// </summary>
|
|
private sealed class CapturingLogger<T> : ILogger<T>
|
|
{
|
|
public sealed record Entry(LogLevel Level, string Message, Exception? Exception);
|
|
|
|
public List<Entry> Entries { get; } = [];
|
|
|
|
public IDisposable BeginScope<TState>(TState state) where TState : notnull => NullScope.Instance;
|
|
public bool IsEnabled(LogLevel logLevel) => true;
|
|
|
|
public void Log<TState>(
|
|
LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
|
Func<TState, Exception?, string> formatter)
|
|
{
|
|
lock (Entries)
|
|
{
|
|
Entries.Add(new Entry(logLevel, formatter(state, exception), exception));
|
|
}
|
|
}
|
|
|
|
private sealed class NullScope : IDisposable
|
|
{
|
|
public static readonly NullScope Instance = new();
|
|
public void Dispose() { }
|
|
}
|
|
}
|
|
|
|
/// <summary>An <see cref="IClusterNodeProvider"/> whose query always throws.</summary>
|
|
private sealed class ThrowingClusterNodeProvider : IClusterNodeProvider
|
|
{
|
|
public bool SelfIsPrimary => true;
|
|
public IReadOnlyList<NodeStatus> GetClusterNodes() =>
|
|
throw new InvalidOperationException("cluster query failed");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SendsReportsWithMonotonicSequenceNumbers()
|
|
{
|
|
var transport = new FakeTransport();
|
|
var collector = new SiteHealthCollector();
|
|
collector.SetActiveNode(true);
|
|
var options = Options.Create(new HealthMonitoringOptions
|
|
{
|
|
ReportInterval = TimeSpan.FromMilliseconds(50)
|
|
});
|
|
|
|
var sender = new HealthReportSender(
|
|
collector,
|
|
transport,
|
|
options,
|
|
NullLogger<HealthReportSender>.Instance,
|
|
new FakeSiteIdentityProvider { SiteId = "site-A" });
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
|
|
try
|
|
{
|
|
await sender.StartAsync(cts.Token);
|
|
await Task.Delay(280, CancellationToken.None);
|
|
await sender.StopAsync(CancellationToken.None);
|
|
}
|
|
catch (OperationCanceledException) { }
|
|
|
|
// Should have sent several reports
|
|
Assert.True(transport.SentReports.Count >= 2,
|
|
$"Expected at least 2 reports, got {transport.SentReports.Count}");
|
|
|
|
// Verify strictly-monotonic sequence numbers and matching site id
|
|
for (int i = 0; i < transport.SentReports.Count; i++)
|
|
{
|
|
if (i > 0)
|
|
{
|
|
Assert.True(
|
|
transport.SentReports[i].SequenceNumber > transport.SentReports[i - 1].SequenceNumber,
|
|
$"Sequence numbers not strictly increasing at index {i}");
|
|
}
|
|
Assert.Equal("site-A", transport.SentReports[i].SiteId);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FirstReportSequenceExceedsStartupUnixMs()
|
|
{
|
|
// Reports are seeded with Unix-ms at construction so a freshly-active
|
|
// node always sorts after the prior active. Verify the first emitted
|
|
// sequence is at least the startup epoch.
|
|
var transport = new FakeTransport();
|
|
var collector = new SiteHealthCollector();
|
|
collector.SetActiveNode(true);
|
|
var options = Options.Create(new HealthMonitoringOptions
|
|
{
|
|
ReportInterval = TimeSpan.FromMilliseconds(50)
|
|
});
|
|
|
|
var beforeCtor = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
var sender = new HealthReportSender(
|
|
collector,
|
|
transport,
|
|
options,
|
|
NullLogger<HealthReportSender>.Instance,
|
|
new FakeSiteIdentityProvider());
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(150));
|
|
try
|
|
{
|
|
await sender.StartAsync(cts.Token);
|
|
await Task.Delay(120, CancellationToken.None);
|
|
await sender.StopAsync(CancellationToken.None);
|
|
}
|
|
catch (OperationCanceledException) { }
|
|
|
|
Assert.True(transport.SentReports.Count >= 1);
|
|
Assert.True(
|
|
transport.SentReports[0].SequenceNumber >= beforeCtor,
|
|
$"First sequence {transport.SentReports[0].SequenceNumber} should be >= startup epoch {beforeCtor}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReportsIncludeUtcTimestamp()
|
|
{
|
|
var transport = new FakeTransport();
|
|
var collector = new SiteHealthCollector();
|
|
collector.SetActiveNode(true);
|
|
var options = Options.Create(new HealthMonitoringOptions
|
|
{
|
|
ReportInterval = TimeSpan.FromMilliseconds(50)
|
|
});
|
|
|
|
var sender = new HealthReportSender(
|
|
collector,
|
|
transport,
|
|
options,
|
|
NullLogger<HealthReportSender>.Instance,
|
|
new FakeSiteIdentityProvider());
|
|
|
|
var before = DateTimeOffset.UtcNow;
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(150));
|
|
try
|
|
{
|
|
await sender.StartAsync(cts.Token);
|
|
await Task.Delay(120, CancellationToken.None);
|
|
await sender.StopAsync(CancellationToken.None);
|
|
}
|
|
catch (OperationCanceledException) { }
|
|
var after = DateTimeOffset.UtcNow;
|
|
|
|
Assert.True(transport.SentReports.Count >= 1);
|
|
foreach (var report in transport.SentReports)
|
|
{
|
|
Assert.InRange(report.ReportTimestamp, before, after);
|
|
Assert.Equal(TimeSpan.Zero, report.ReportTimestamp.Offset);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// HealthMonitoring-001 regression: the documented "store-and-forward buffer
|
|
/// depth" metric (pending messages by category) must actually be populated in
|
|
/// the emitted report. Previously SetStoreAndForwardDepths had no callers, so
|
|
/// StoreAndForwardBufferDepths was always empty. The sender must query the S&F
|
|
/// engine's per-category depth API and include it alongside the parked count.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task ReportsIncludeStoreAndForwardBufferDepthsFromStorage()
|
|
{
|
|
// LocalDb has no in-memory mode, so the store runs over a real temp file.
|
|
var localDb = TestLocalDb.CreateTemp("HealthSfDepth");
|
|
try
|
|
{
|
|
var storage = new StoreAndForwardStorage(localDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
|
await storage.InitializeAsync();
|
|
|
|
// Two pending ExternalSystem messages and one pending Notification message.
|
|
await storage.EnqueueAsync(MakePendingMessage("m1", StoreAndForwardCategory.ExternalSystem));
|
|
await storage.EnqueueAsync(MakePendingMessage("m2", StoreAndForwardCategory.ExternalSystem));
|
|
await storage.EnqueueAsync(MakePendingMessage("m3", StoreAndForwardCategory.Notification));
|
|
|
|
var transport = new FakeTransport();
|
|
var collector = new SiteHealthCollector();
|
|
collector.SetActiveNode(true);
|
|
var options = Options.Create(new HealthMonitoringOptions
|
|
{
|
|
ReportInterval = TimeSpan.FromMilliseconds(50)
|
|
});
|
|
|
|
var sender = new HealthReportSender(
|
|
collector,
|
|
transport,
|
|
options,
|
|
NullLogger<HealthReportSender>.Instance,
|
|
new FakeSiteIdentityProvider(),
|
|
sfStorage: storage);
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
|
|
try
|
|
{
|
|
await sender.StartAsync(cts.Token);
|
|
await Task.Delay(250, CancellationToken.None);
|
|
await sender.StopAsync(CancellationToken.None);
|
|
}
|
|
catch (OperationCanceledException) { }
|
|
|
|
Assert.True(transport.SentReports.Count >= 1);
|
|
var depths = transport.SentReports[^1].StoreAndForwardBufferDepths;
|
|
Assert.Equal(2, depths[nameof(StoreAndForwardCategory.ExternalSystem)]);
|
|
Assert.Equal(1, depths[nameof(StoreAndForwardCategory.Notification)]);
|
|
Assert.False(depths.ContainsKey(nameof(StoreAndForwardCategory.CachedDbWrite)));
|
|
}
|
|
finally
|
|
{
|
|
// The master connection anchors the WAL — dispose before deleting.
|
|
localDb.Dispose();
|
|
TestLocalDb.DeleteFiles(localDb.Path);
|
|
}
|
|
}
|
|
|
|
private static StoreAndForwardMessage MakePendingMessage(string id, StoreAndForwardCategory category) =>
|
|
new()
|
|
{
|
|
Id = id,
|
|
Category = category,
|
|
Target = "target",
|
|
PayloadJson = "{}",
|
|
RetryCount = 0,
|
|
MaxRetries = 50,
|
|
RetryIntervalMs = 30_000,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
Status = StoreAndForwardMessageStatus.Pending
|
|
};
|
|
|
|
[Fact]
|
|
public void InitialSequenceNumberSeededWithUnixMs()
|
|
{
|
|
var transport = new FakeTransport();
|
|
var collector = new SiteHealthCollector();
|
|
var options = Options.Create(new HealthMonitoringOptions());
|
|
|
|
var beforeCtor = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
var sender = new HealthReportSender(
|
|
collector,
|
|
transport,
|
|
options,
|
|
NullLogger<HealthReportSender>.Instance,
|
|
new FakeSiteIdentityProvider());
|
|
var afterCtor = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
|
Assert.InRange(sender.CurrentSequenceNumber, beforeCtor, afterCtor);
|
|
}
|
|
|
|
/// <summary>
|
|
/// HealthMonitoring-010 regression: a failure refreshing cluster nodes is
|
|
/// non-fatal (the report still ships) but must no longer be swallowed by a
|
|
/// bare <c>catch {}</c> — it must be logged as a warning with the exception so
|
|
/// persistent degradation is diagnosable.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task ClusterNodeRefreshFailure_IsLoggedNotSwallowed()
|
|
{
|
|
var transport = new FakeTransport();
|
|
var collector = new SiteHealthCollector();
|
|
collector.SetActiveNode(true);
|
|
var logger = new CapturingLogger<HealthReportSender>();
|
|
var options = Options.Create(new HealthMonitoringOptions
|
|
{
|
|
ReportInterval = TimeSpan.FromMilliseconds(50)
|
|
});
|
|
|
|
var sender = new HealthReportSender(
|
|
collector,
|
|
transport,
|
|
options,
|
|
logger,
|
|
new FakeSiteIdentityProvider(),
|
|
clusterNodeProvider: new ThrowingClusterNodeProvider());
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
|
|
try
|
|
{
|
|
await sender.StartAsync(cts.Token);
|
|
await Task.Delay(250, CancellationToken.None);
|
|
await sender.StopAsync(CancellationToken.None);
|
|
}
|
|
catch (OperationCanceledException) { }
|
|
|
|
// The report loop continues despite the failure...
|
|
Assert.NotEmpty(transport.SentReports);
|
|
// ...but the failure is surfaced as a warning carrying the exception.
|
|
CapturingLogger<HealthReportSender>.Entry[] warnings;
|
|
lock (logger.Entries)
|
|
{
|
|
warnings = logger.Entries
|
|
.Where(e => e.Level == LogLevel.Warning && e.Exception is InvalidOperationException)
|
|
.ToArray();
|
|
}
|
|
Assert.NotEmpty(warnings);
|
|
Assert.Contains(warnings, w => w.Message.Contains("cluster nodes", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// HealthMonitoring-017 regression: when the transport's <c>Send</c> throws,
|
|
/// the per-interval counters that <see cref="SiteHealthCollector.CollectReport"/>
|
|
/// just drained via <c>Interlocked.Exchange</c> must be restored back into the
|
|
/// collector so they roll forward into the next interval rather than being
|
|
/// silently lost. Before the fix, a transport failure left the counts in the
|
|
/// un-sent report only, and the next successful report shipped with the
|
|
/// counters at zero.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task SendFailure_PreservesIntervalCountersForNextReport()
|
|
{
|
|
var transport = new FailingThenSucceedingTransport();
|
|
var collector = new SiteHealthCollector();
|
|
collector.SetActiveNode(true);
|
|
// Pre-populate every per-interval counter so the restore path on each
|
|
// field is exercised — script error, alarm error, dead letter, site
|
|
// audit write failure, audit redaction failure.
|
|
collector.IncrementScriptError();
|
|
collector.IncrementScriptError();
|
|
collector.IncrementAlarmError();
|
|
collector.IncrementDeadLetter();
|
|
collector.IncrementDeadLetter();
|
|
collector.IncrementDeadLetter();
|
|
collector.IncrementSiteAuditWriteFailures();
|
|
collector.IncrementAuditRedactionFailure();
|
|
collector.IncrementAuditRedactionFailure();
|
|
|
|
var options = Options.Create(new HealthMonitoringOptions
|
|
{
|
|
ReportInterval = TimeSpan.FromMilliseconds(50)
|
|
});
|
|
|
|
var sender = new HealthReportSender(
|
|
collector,
|
|
transport,
|
|
options,
|
|
NullLogger<HealthReportSender>.Instance,
|
|
new FakeSiteIdentityProvider());
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
|
|
try
|
|
{
|
|
await sender.StartAsync(cts.Token);
|
|
await Task.Delay(450, CancellationToken.None);
|
|
await sender.StopAsync(CancellationToken.None);
|
|
}
|
|
catch (OperationCanceledException) { }
|
|
|
|
// The first interval's Send threw, then later intervals succeeded. The
|
|
// first successful report must include the previously-failed interval's
|
|
// accumulated counts.
|
|
Assert.NotEmpty(transport.SentReports);
|
|
var firstSuccess = transport.SentReports[0];
|
|
Assert.Equal(2, firstSuccess.ScriptErrorCount);
|
|
Assert.Equal(1, firstSuccess.AlarmEvaluationErrorCount);
|
|
Assert.Equal(3, firstSuccess.DeadLetterCount);
|
|
Assert.Equal(1, firstSuccess.SiteAuditWriteFailures);
|
|
Assert.Equal(2, firstSuccess.AuditRedactionFailure);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <see cref="IHealthReportTransport"/> that throws on the first
|
|
/// <c>Send</c> call (and only the first), then succeeds. Used by
|
|
/// <see cref="SendFailure_PreservesIntervalCountersForNextReport"/> to
|
|
/// verify the HealthMonitoring-017 restore-on-failure path.
|
|
/// </summary>
|
|
private sealed class FailingThenSucceedingTransport : IHealthReportTransport
|
|
{
|
|
private int _callCount;
|
|
public List<SiteHealthReport> SentReports { get; } = [];
|
|
|
|
public Task SendAsync(SiteHealthReport report, CancellationToken cancellationToken)
|
|
{
|
|
var n = Interlocked.Increment(ref _callCount);
|
|
if (n == 1)
|
|
{
|
|
throw new InvalidOperationException("transport temporarily unavailable");
|
|
}
|
|
SentReports.Add(report);
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// HealthMonitoring-006 regression: the sequence-number seed must be derived
|
|
/// from the injected <see cref="TimeProvider"/> so the Unix-ms seeding strategy
|
|
/// is deterministically testable and the clock dependency is explicit, rather
|
|
/// than reading <c>DateTimeOffset.UtcNow</c> directly at field initialization.
|
|
/// </summary>
|
|
[Fact]
|
|
public void SequenceNumberSeed_UsesInjectedTimeProvider()
|
|
{
|
|
var fixedInstant = new DateTimeOffset(2026, 5, 16, 12, 0, 0, TimeSpan.Zero);
|
|
var timeProvider = new TestTimeProvider(fixedInstant);
|
|
|
|
var sender = new HealthReportSender(
|
|
new SiteHealthCollector(),
|
|
new FakeTransport(),
|
|
Options.Create(new HealthMonitoringOptions()),
|
|
NullLogger<HealthReportSender>.Instance,
|
|
new FakeSiteIdentityProvider(),
|
|
timeProvider: timeProvider);
|
|
|
|
Assert.Equal(fixedInstant.ToUnixTimeMilliseconds(), sender.CurrentSequenceNumber);
|
|
}
|
|
}
|