Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Services/SingleFlightMemoTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

183 lines
7.3 KiB
C#

using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Services;
/// <summary>
/// Unit tests for <see cref="SingleFlightMemo{T}"/> — the primitive behind the process-level
/// KPI snapshot cache and the shared Alarm Summary fan-out. The two
/// properties the whole optimisation rests on are tested here: <b>single-flight</b> (N
/// concurrent circuits asking the same question produce ONE query) and <b>TTL</b> (a query
/// answered inside the freshness window is served from memory, and one outside it is not).
/// </summary>
public class SingleFlightMemoTests
{
private static readonly DateTimeOffset T0 = new(2026, 8, 14, 9, 0, 0, TimeSpan.Zero);
/// <summary>Verifies that concurrent callers share a single in-flight query.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ConcurrentCallers_ShareOneFlight()
{
var released = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var invocations = 0;
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(10), () => T0);
// Ten circuits ask while the first flight is still in progress.
var waiters = Enumerable.Range(0, 10)
.Select(_ => memo.GetAsync(() =>
{
Interlocked.Increment(ref invocations);
return released.Task;
}))
.ToArray();
released.SetResult(42);
var results = await Task.WhenAll(waiters);
Assert.Equal(1, invocations);
Assert.Equal(1, memo.FlightCount);
Assert.All(results, r => Assert.Equal(42, r));
}
/// <summary>Verifies that a call within the TTL serves the memoized value without querying again.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task WithinTtl_ServesTheMemoizedValue_WithoutRequerying()
{
var now = T0;
var invocations = 0;
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(8), () => now);
var first = await memo.GetAsync(() => Task.FromResult(++invocations));
now = T0.AddSeconds(7);
var second = await memo.GetAsync(() => Task.FromResult(++invocations));
Assert.Equal(1, first);
Assert.Equal(1, second);
Assert.Equal(1, memo.FlightCount);
}
/// <summary>Verifies that a call after the TTL expires queries exactly once more.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task AfterTtl_RequeriesOnce()
{
var now = T0;
var invocations = 0;
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(8), () => now);
await memo.GetAsync(() => Task.FromResult(++invocations));
now = T0.AddSeconds(9);
var refreshed = await memo.GetAsync(() => Task.FromResult(++invocations));
Assert.Equal(2, refreshed);
Assert.Equal(2, memo.FlightCount);
}
/// <summary>Verifies that a forced refresh bypasses a value that is still fresh.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ForceRefresh_BypassesAFreshValue()
{
var now = T0;
var invocations = 0;
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(60), () => now);
await memo.GetAsync(() => Task.FromResult(++invocations));
var forced = await memo.GetAsync(() => Task.FromResult(++invocations), forceRefresh: true);
Assert.Equal(2, forced);
Assert.Equal(2, memo.FlightCount);
}
/// <summary>Verifies that a forced refresh still joins a query that is already in flight.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ForceRefresh_StillJoinsAnInFlightRound()
{
var released = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var invocations = 0;
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(60), () => T0);
var first = memo.GetAsync(() =>
{
Interlocked.Increment(ref invocations);
return released.Task;
});
// A burst of operator "Refresh" clicks must not multiply the query.
var forcedA = memo.GetAsync(() => Task.FromResult(-1), forceRefresh: true);
var forcedB = memo.GetAsync(() => Task.FromResult(-1), forceRefresh: true);
released.SetResult(7);
Assert.Equal(7, await first);
Assert.Equal(7, await forcedA);
Assert.Equal(7, await forcedB);
Assert.Equal(1, invocations);
}
/// <summary>Verifies that a failure is not memoized and the next caller retries.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Failure_IsNotMemoized_AndTheNextCallerRetries()
{
var now = T0;
var invocations = 0;
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(60), () => now);
await Assert.ThrowsAsync<InvalidOperationException>(() =>
memo.GetAsync(() =>
{
invocations++;
return Task.FromException<int>(new InvalidOperationException("KPI query failed"));
}));
// Same instant, well inside the TTL — a cached fault would starve the tiles for a
// whole window, so the next caller must produce a new flight.
var recovered = await memo.GetAsync(() => Task.FromResult(++invocations));
Assert.Equal(2, recovered);
Assert.Equal(2, memo.FlightCount);
}
/// <summary>Verifies that a TTL override applies to the flight it was supplied for.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task TtlOverride_AppliesToThatFlight()
{
var now = T0;
var invocations = 0;
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(5), () => now);
// Produced under a 60s override — still fresh 30s later despite the 5s default.
await memo.GetAsync(() => Task.FromResult(++invocations), ttlOverride: TimeSpan.FromSeconds(60));
now = T0.AddSeconds(30);
var second = await memo.GetAsync(() => Task.FromResult(++invocations));
Assert.Equal(1, second);
Assert.Equal(1, memo.FlightCount);
}
/// <summary>Verifies that cancelling one caller does not cancel the shared flight.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task CallerCancellation_DoesNotCancelTheSharedFlight()
{
var released = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var memo = new SingleFlightMemo<int>(TimeSpan.FromSeconds(60), () => T0);
using var giveUp = new CancellationTokenSource();
var impatient = memo.GetAsync(() => released.Task, cancellationToken: giveUp.Token);
var patient = memo.GetAsync(() => Task.FromResult(-1));
giveUp.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => impatient);
// The abandoned wait must not have taken the round down with it.
released.SetResult(99);
Assert.Equal(99, await patient);
}
}