using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.AuditLog.Site;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Site;
///
/// The backlog reporter's poll cadence is configurable via
/// instead of the
/// old hard-coded 30 s constant.
///
public class SiteAuditBacklogReporterCadenceTests
{
private static SiteAuditBacklogReporter Create(
IOptions? options, TimeSpan? explicitInterval = null) =>
new(
Substitute.For(),
Substitute.For(),
NullLogger.Instance,
explicitInterval,
options);
/// Verifies that stopping the reporter while a probe is in flight lets the loop complete cleanly.
/// A task that represents the asynchronous operation.
[Fact]
public async Task StopAsync_WhileProbeInFlight_LoopCompletesCleanly()
{
// SafeProbeAsync rethrows OperationCanceledException by design so a shutdown
// aborts the probe promptly — but RunLoopAsync must absorb it, because StopAsync
// hands _loop straight to the host and a canceled task there throws out of
// Host.StopAsync. Mirrors the guard SiteAuditRetentionService already carries
// (arch-review 04 round 2, R7).
var probeStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var queue = Substitute.For();
queue.GetBacklogStatsAsync(Arg.Any())
.Returns(ci => BlockUntilCancelledAsync(probeStarted, ci.Arg()));
var reporter = new SiteAuditBacklogReporter(
queue,
Substitute.For(),
NullLogger.Instance,
TimeSpan.FromHours(1),
null);
await reporter.StartAsync(CancellationToken.None);
await probeStarted.Task; // the immediate first probe is now in flight
// The assertion is that awaiting the loop task does not throw.
await reporter.StopAsync(CancellationToken.None);
reporter.Dispose();
}
private static async Task BlockUntilCancelledAsync(
TaskCompletionSource started, CancellationToken ct)
{
started.TrySetResult();
await Task.Delay(Timeout.Infinite, ct);
throw new UnreachableException("the delay above always throws on cancellation");
}
/// Verifies that stopping the reporter after it has been disposed does not throw.
/// A task that represents the asynchronous operation.
[Fact]
public async Task StopAsync_AfterDispose_DoesNotThrow()
{
// Regression (follow-up): Dispose tears down the CTS StopAsync
// cancels, and the host does not guarantee StopAsync is driven before the DI
// container is disposed. Cancel() on a disposed CTS throws, and letting that
// escape an IHostedService aborts the host's whole shutdown sequence.
var reporter = Create(Options.Create(new SqliteAuditWriterOptions()), TimeSpan.FromHours(1));
await reporter.StartAsync(CancellationToken.None);
reporter.Dispose();
// The assertion is the absence of ObjectDisposedException.
await reporter.StopAsync(CancellationToken.None);
reporter.Dispose();
}
/// Verifies that the poll cadence comes from the options when one is configured.
[Fact]
public void Cadence_ComesFromOptions_WhenConfigured()
{
var options = Options.Create(new SqliteAuditWriterOptions { BacklogPollIntervalSeconds = 12 });
var reporter = Create(options);
Assert.Equal(TimeSpan.FromSeconds(12), reporter.RefreshInterval);
}
/// Verifies that a non-positive configured interval falls back to the default cadence.
[Fact]
public void Cadence_FallsBackToDefault_WhenOptionsNonPositive()
{
var options = Options.Create(new SqliteAuditWriterOptions { BacklogPollIntervalSeconds = 0 });
var reporter = Create(options);
Assert.Equal(SiteAuditBacklogReporter.DefaultRefreshInterval, reporter.RefreshInterval);
}
/// Verifies that the default cadence is used when no options are supplied.
[Fact]
public void Cadence_FallsBackToDefault_WhenNoOptions()
{
var reporter = Create(options: null);
Assert.Equal(SiteAuditBacklogReporter.DefaultRefreshInterval, reporter.RefreshInterval);
}
/// Verifies that an explicitly supplied interval takes precedence over the options value.
[Fact]
public void ExplicitInterval_WinsOverOptions()
{
var options = Options.Create(new SqliteAuditWriterOptions { BacklogPollIntervalSeconds = 12 });
var reporter = Create(options, explicitInterval: TimeSpan.FromSeconds(3));
Assert.Equal(TimeSpan.FromSeconds(3), reporter.RefreshInterval);
}
// ----- Stale-Pending signal (review F5) ----- //
/// Verifies that a stale pending backlog is logged as a warning and that repeat warnings are then rate-limited.
[Fact]
public void StalePendingBacklog_IsWarned_ThenRateLimited()
{
// Pending rows are exempt from the retention purge by design, so a standing Pending
// backlog is the one site-store condition that never self-heals on age — it clears
// only when central acknowledges the rows. It is worth a log line, not just a number
// on the health report, and the warning must not spam every 30 s poll.
var logger = new CapturingLogger();
var reporter = new SiteAuditBacklogReporter(
Substitute.For(),
Substitute.For(),
logger,
TimeSpan.FromHours(1),
null);
var stale = DateTime.UtcNow - SiteAuditBacklogReporter.StalePendingThreshold - TimeSpan.FromHours(1);
reporter.WarnIfPendingIsStale(stale, pendingCount: 4321);
reporter.WarnIfPendingIsStale(stale, pendingCount: 4321); // same poll cycle-ish
var warning = Assert.Single(logger.Entries, e => e.Level == LogLevel.Warning);
Assert.Contains("4321", warning.Message);
Assert.Contains("pending", warning.Message, StringComparison.OrdinalIgnoreCase);
}
/// Verifies that a fresh or empty pending backlog does not log a warning.
[Fact]
public void FreshOrEmptyPendingBacklog_IsNotWarned()
{
var logger = new CapturingLogger();
var reporter = new SiteAuditBacklogReporter(
Substitute.For(),
Substitute.For(),
logger,
TimeSpan.FromHours(1),
null);
reporter.WarnIfPendingIsStale(null, pendingCount: 0); // nothing pending
reporter.WarnIfPendingIsStale(DateTime.UtcNow.AddMinutes(-5), 12); // a normal drain lag
Assert.DoesNotContain(logger.Entries, e => e.Level == LogLevel.Warning);
}
/// Captures log entries so the stale-pending signal can be asserted.
private sealed class CapturingLogger : ILogger
{
/// Gets the log entries captured so far.
public List<(LogLevel Level, Exception? Exception, string Message)> Entries { get; } = new();
/// Returns no scope; the fake logger does not track scopes.
/// The type of the scope state.
/// The scope state, ignored.
/// Always null or a no-op scope.
public IDisposable? BeginScope(TState state) where TState : notnull => null;
/// Reports every log level as enabled.
/// The log level being queried.
/// Always true.
public bool IsEnabled(LogLevel logLevel) => true;
/// Records the log entry for later assertions.
/// The type of the log state.
/// The severity of the entry.
/// The event identifier.
/// The log state.
/// The associated exception, if any.
/// Formats the state and exception into the message.
public void Log(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func formatter)
{
Entries.Add((logLevel, exception, formatter(state, exception)));
}
}
}