2ed5c6c379
Concurrency hazards, DI lifetime hygiene, and one verify-only confirmation
across 8 modules. Highlights:
Concurrency:
- CentralUI-030: SandboxConsoleCapture writes routed through WriteSynchronized
locking on the captured StringWriter — intra-script Task fan-out can no
longer corrupt the per-call buffer.
- Commons-021: ExternalCallResult.Response now backed by Lazy<dynamic?>
(ExecutionAndPublication) — no more benign double-parse race.
- CD-017: DeploymentManagerRepository.DeleteDeploymentRecordAsync now takes
an expected RowVersion and seeds entry.OriginalValues so EF emits
DELETE ... WHERE Id=@id AND RowVersion=@prior; stale RowVersion now
throws DbUpdateConcurrencyException instead of silent overwrite.
- Transport-009: AuditCorrelationContext.BundleImportId backed by
AsyncLocal<Guid?> so concurrent imports get per-logical-call isolation
(was a scoped instance shared via AuditService across runs).
DI / lifetime:
- AuditLog-003: All 3 AuditLog actor handlers switched to CreateAsyncScope
+ await using — async EF disposal no longer swallowed.
- AuditLog-007: INodeIdentityProvider resolution standardised on
GetRequiredService<>() (was mixed with GetService<>()).
- AuditLog-011: AddAuditLogHealthMetricsBridge guarded by sentinel
descriptor check — calling twice no longer double-registers the hosted
service.
Shutdown / supervision:
- SiteCallAudit-002: AkkaHostedService adds a CoordinatedShutdown
cluster-leave task (drain-site-call-audit-singleton) that issues a
bounded GracefulStop(10s) so failover waits for in-flight upserts.
Registration safety:
- NS-020: AkkaHostedService now guards NotificationForwarder S&F
registration with _notificationDeliveryHandlerRegistered + throws
InvalidOperationException on double-register to make the regression loud.
VERIFY-only closures:
- NotifOutbox-005: Confirmed already closed by CD-015 fix (ac96b83) —
NotificationOutboxRepository.InsertIfNotExistsAsync uses the same
raw-SQL IF NOT EXISTS + 2601/2627 swallow pattern; race eliminated.
5+ new regression tests (CentralUI sandbox WhenAll, ExternalCallResult
64-reader Barrier, AuditLog DI idempotency, RowVersion stale-throw,
SiteCallAudit-002 shutdown drain). Build clean; affected suites all green.
README regenerated: 65 open (was 75).
311 lines
11 KiB
C#
311 lines
11 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using ScadaLink.AuditLog.Central;
|
|
using ScadaLink.AuditLog.Configuration;
|
|
using ScadaLink.AuditLog.Site;
|
|
using ScadaLink.AuditLog.Site.Telemetry;
|
|
using ScadaLink.AuditLog.Tests.TestSupport;
|
|
using ScadaLink.Commons.Interfaces.Services;
|
|
using ScadaLink.HealthMonitoring;
|
|
|
|
namespace ScadaLink.AuditLog.Tests;
|
|
|
|
/// <summary>
|
|
/// Bundle E (M2 Task E1) DI surface tests for <c>AddAuditLog</c>. M1 shipped
|
|
/// the options-only scaffold; M2 extends it with the site writer chain
|
|
/// (<see cref="SqliteAuditWriter"/> + <see cref="RingBufferFallback"/> +
|
|
/// <see cref="FallbackAuditWriter"/>) and the telemetry collaborators
|
|
/// (<see cref="ISiteAuditQueue"/>, <see cref="ISiteStreamAuditClient"/>,
|
|
/// <see cref="IAuditWriteFailureCounter"/>).
|
|
/// </summary>
|
|
public class AddAuditLogTests
|
|
{
|
|
private static ServiceProvider BuildProvider(IDictionary<string, string?>? settings = null)
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(settings ?? new Dictionary<string, string?>())
|
|
.Build();
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
|
|
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
|
|
// INodeIdentityProvider is registered by the Host's
|
|
// SiteServiceRegistration in production; AddAuditLog assumes its
|
|
// presence so SqliteAuditWriter and CentralAuditWriter can resolve.
|
|
services.AddSingleton<INodeIdentityProvider>(new FakeNodeIdentityProvider());
|
|
services.AddAuditLog(config);
|
|
return services.BuildServiceProvider();
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_RegistersAuditLogOptions()
|
|
{
|
|
using var provider = BuildProvider();
|
|
|
|
var opts = provider.GetService<IOptions<AuditLogOptions>>();
|
|
|
|
Assert.NotNull(opts);
|
|
Assert.NotNull(opts!.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_NullServices_Throws()
|
|
{
|
|
var config = new ConfigurationBuilder().Build();
|
|
|
|
Assert.Throws<ArgumentNullException>(
|
|
() => ServiceCollectionExtensions.AddAuditLog(null!, config));
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_NullConfig_Throws()
|
|
{
|
|
var services = new ServiceCollection();
|
|
|
|
Assert.Throws<ArgumentNullException>(
|
|
() => services.AddAuditLog(null!));
|
|
}
|
|
|
|
// -- Bundle E (M2 Task E1) ---------------------------------------------
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Registers_SqliteAuditWriter_Singleton_FromDI()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
// In-memory database keeps the writer's owned connection portable
|
|
// across tests; the per-instance Cache=Shared in the writer's
|
|
// default connection string ensures no on-disk file is touched.
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var writer = provider.GetService<SqliteAuditWriter>();
|
|
|
|
Assert.NotNull(writer);
|
|
// Singleton — same instance on a second resolve.
|
|
Assert.Same(writer, provider.GetService<SqliteAuditWriter>());
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Registers_IAuditWriter_AsFallbackAuditWriter()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var writer = provider.GetService<IAuditWriter>();
|
|
|
|
Assert.NotNull(writer);
|
|
Assert.IsType<FallbackAuditWriter>(writer);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Registers_ISiteAuditQueue_AsSameInstance_As_SqliteAuditWriter()
|
|
{
|
|
// The telemetry actor reads from ISiteAuditQueue while scripts write
|
|
// through IAuditWriter → SqliteAuditWriter. Both surfaces MUST resolve
|
|
// to the same instance or pending rows will never be visible.
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var queue = provider.GetService<ISiteAuditQueue>();
|
|
var writer = provider.GetService<SqliteAuditWriter>();
|
|
|
|
Assert.NotNull(queue);
|
|
Assert.NotNull(writer);
|
|
Assert.Same(writer, queue);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Registers_RingBufferFallback_Singleton()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var ring = provider.GetService<RingBufferFallback>();
|
|
Assert.NotNull(ring);
|
|
Assert.Same(ring, provider.GetService<RingBufferFallback>());
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Registers_AuditWriteFailureCounter_AsNoOpDefault()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var counter = provider.GetService<IAuditWriteFailureCounter>();
|
|
Assert.NotNull(counter);
|
|
Assert.IsType<NoOpAuditWriteFailureCounter>(counter);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Registers_SiteStreamAuditClient_AsNoOpDefault()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var client = provider.GetService<ISiteStreamAuditClient>();
|
|
Assert.NotNull(client);
|
|
Assert.IsType<NoOpSiteStreamAuditClient>(client);
|
|
}
|
|
|
|
// -- M4 Bundle B (B1) central direct-write audit writer -----------------
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Registers_ICentralAuditWriter_AsCentralAuditWriter()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var writer = provider.GetService<ICentralAuditWriter>();
|
|
Assert.NotNull(writer);
|
|
Assert.IsType<CentralAuditWriter>(writer);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_ICentralAuditWriter_IsSingleton()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
});
|
|
|
|
var w1 = provider.GetService<ICentralAuditWriter>();
|
|
var w2 = provider.GetService<ICentralAuditWriter>();
|
|
Assert.Same(w1, w2);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Options_Bind_RoundTrip_SqliteWriter()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = "/tmp/test-audit.db",
|
|
["AuditLog:SiteWriter:ChannelCapacity"] = "8192",
|
|
["AuditLog:SiteWriter:BatchSize"] = "128",
|
|
["AuditLog:SiteWriter:FlushIntervalMs"] = "75",
|
|
});
|
|
|
|
var opts = provider.GetRequiredService<IOptions<SqliteAuditWriterOptions>>().Value;
|
|
Assert.Equal("/tmp/test-audit.db", opts.DatabasePath);
|
|
Assert.Equal(8192, opts.ChannelCapacity);
|
|
Assert.Equal(128, opts.BatchSize);
|
|
Assert.Equal(75, opts.FlushIntervalMs);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLog_Options_Bind_RoundTrip_SiteTelemetry()
|
|
{
|
|
using var provider = BuildProvider(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteTelemetry:BatchSize"] = "512",
|
|
["AuditLog:SiteTelemetry:BusyIntervalSeconds"] = "3",
|
|
["AuditLog:SiteTelemetry:IdleIntervalSeconds"] = "60",
|
|
});
|
|
|
|
var opts = provider.GetRequiredService<IOptions<SiteAuditTelemetryOptions>>().Value;
|
|
Assert.Equal(512, opts.BatchSize);
|
|
Assert.Equal(3, opts.BusyIntervalSeconds);
|
|
Assert.Equal(60, opts.IdleIntervalSeconds);
|
|
}
|
|
|
|
// -- Bundle G (M2 Task G1) Site Health Monitoring bridge ----------------
|
|
|
|
[Fact]
|
|
public void AddAuditLogHealthMetricsBridge_Swaps_FailureCounter_To_HealthMetrics_Implementation()
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
})
|
|
.Build();
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
|
|
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
|
|
services.AddAuditLog(config);
|
|
// The bridge depends on ISiteHealthCollector; AddHealthMonitoring is
|
|
// what registers it on the site (and the central self-host).
|
|
services.AddHealthMonitoring();
|
|
services.AddAuditLogHealthMetricsBridge();
|
|
using var provider = services.BuildServiceProvider();
|
|
|
|
var counter = provider.GetRequiredService<IAuditWriteFailureCounter>();
|
|
|
|
Assert.IsType<HealthMetricsAuditWriteFailureCounter>(counter);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLogHealthMetricsBridge_Without_HealthMonitoring_Still_Resolves_But_Errors_On_Use()
|
|
{
|
|
// The bridge replaces the registration unconditionally; resolving the
|
|
// counter when ISiteHealthCollector is missing throws at GetRequiredService
|
|
// time. This documents the contract — callers must register
|
|
// AddHealthMonitoring() before the bridge.
|
|
var config = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
})
|
|
.Build();
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
|
|
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
|
|
services.AddAuditLog(config);
|
|
services.AddAuditLogHealthMetricsBridge();
|
|
using var provider = services.BuildServiceProvider();
|
|
|
|
Assert.Throws<InvalidOperationException>(
|
|
() => provider.GetRequiredService<IAuditWriteFailureCounter>());
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuditLogHealthMetricsBridge_IsIdempotent_DoesNotDoubleRegister_HostedService()
|
|
{
|
|
// AuditLog-011: AddHostedService has no TryAdd variant, so a second
|
|
// call without the sentinel guard would spin up a second
|
|
// SiteAuditBacklogReporter on the same SQLite file. The helper must
|
|
// be a no-op on the second call — exactly one hosted-service
|
|
// descriptor for SiteAuditBacklogReporter survives.
|
|
var config = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:SiteWriter:DatabasePath"] = ":memory:",
|
|
})
|
|
.Build();
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
|
|
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
|
|
services.AddSingleton<INodeIdentityProvider>(new FakeNodeIdentityProvider());
|
|
services.AddAuditLog(config);
|
|
services.AddHealthMonitoring();
|
|
|
|
services.AddAuditLogHealthMetricsBridge();
|
|
services.AddAuditLogHealthMetricsBridge();
|
|
|
|
var reporterCount = services.Count(d =>
|
|
d.ServiceType == typeof(IHostedService) &&
|
|
d.ImplementationType == typeof(SiteAuditBacklogReporter));
|
|
|
|
Assert.Equal(1, reporterCount);
|
|
}
|
|
}
|