Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests/TestSupport/ScopeSpyServiceProvider.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

54 lines
2.3 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.ScadaBridge.SiteEventLogging;
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.TestSupport;
/// <summary>
/// An <see cref="IServiceProvider"/> that counts
/// <see cref="IServiceScopeFactory.CreateScope"/> calls and each scope's
/// <see cref="IDisposable.Dispose"/>, so a test can assert that one script run creates
/// exactly one DI scope and disposes it exactly once — on the success, failure, AND timeout
/// paths alike. Removing the per-run execution actor moved the scope's <c>finally</c> into
/// <see cref="ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts.ScriptRunLauncher"/>; a scope leaked
/// there would silently leak every scoped service a script touches.
/// </summary>
public sealed class ScopeSpyServiceProvider(ISiteEventLogger? logger = null)
: IServiceProvider, IServiceScopeFactory
{
private int _scopesCreated;
private int _scopesDisposed;
/// <summary>Scopes created so far.</summary>
public int ScopesCreated => Volatile.Read(ref _scopesCreated);
/// <summary>Scope disposals observed so far (double-disposal is counted twice, deliberately).</summary>
public int ScopesDisposed => Volatile.Read(ref _scopesDisposed);
/// <summary>Resolves the site event logger and this scope factory; every other service resolves to <c>null</c>.</summary>
/// <param name="serviceType">The requested service type.</param>
/// <returns>The matching service, or <c>null</c> when unsupported.</returns>
public object? GetService(Type serviceType)
{
if (serviceType == typeof(ISiteEventLogger)) return logger;
if (serviceType == typeof(IServiceScopeFactory)) return this;
return null;
}
/// <summary>Creates a counted scope whose disposal is also counted.</summary>
/// <returns>A spy scope.</returns>
public IServiceScope CreateScope()
{
Interlocked.Increment(ref _scopesCreated);
return new SpyScope(this);
}
private sealed class SpyScope(ScopeSpyServiceProvider owner) : IServiceScope
{
/// <summary>Gets the owning provider as the scope's service provider.</summary>
public IServiceProvider ServiceProvider => owner;
/// <summary>Releases the resources held by this instance.</summary>
public void Dispose() => Interlocked.Increment(ref owner._scopesDisposed);
}
}