test: add focused grpc data phase gates and discovery isolation
This commit is contained in:
@@ -65,16 +65,20 @@ public sealed class AdminUiSqlServerFixture : IDisposable
|
||||
private readonly SqlServerContextFactory? _factory;
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="AdminUiSqlServerFixture"/> class.</summary>
|
||||
public AdminUiSqlServerFixture()
|
||||
public AdminUiSqlServerFixture() : this(AdminUiSqlServer.Instance)
|
||||
{
|
||||
}
|
||||
|
||||
internal AdminUiSqlServerFixture(AdminUiSqlServerProbe server)
|
||||
{
|
||||
// Probe FIRST — see the remarks. A dead server must leave this constructor without throwing,
|
||||
// because xunit builds collection fixtures before it discovers that every test is skipped.
|
||||
SkipReason = AdminUiSqlServer.SkipReason;
|
||||
SkipReason = server.SkipReason;
|
||||
if (SkipReason is not null) return;
|
||||
|
||||
// A killed test process never reaches Dispose, so its database outlives the run. Reap the ones
|
||||
// old enough to be certainly nobody's (once per process, and only after the probe answered).
|
||||
AdminUiSqlServer.ReapOrphanedTestDatabasesOnce();
|
||||
server.ReapOrphanedTestDatabasesOnce();
|
||||
|
||||
// Mint from the same constant the reaper's LIKE anchors on — a rename that touched only one of the
|
||||
// two would leave every future database unreapable, with nothing to signal it.
|
||||
@@ -146,19 +150,17 @@ internal static class AdminUiSqlServer
|
||||
/// <summary>How long a test database must have existed before the reaper considers it certainly orphaned.</summary>
|
||||
private static readonly TimeSpan OrphanAge = TimeSpan.FromHours(24);
|
||||
|
||||
private static readonly Lazy<string?> Probe = new(RunProbe, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
|
||||
private static readonly Lazy<int> Reaper = new(RunReaper, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
internal static readonly AdminUiSqlServerProbe Instance = new(RunProbe, RunReaper);
|
||||
|
||||
/// <summary>Gets the reason SQL Server is unusable, or <see langword="null"/> when it answered.</summary>
|
||||
public static string? SkipReason => Probe.Value;
|
||||
public static string? SkipReason => Instance.SkipReason;
|
||||
|
||||
/// <summary>
|
||||
/// Reaps orphaned AdminUI test databases older than 24 hours, at most once per process. Call only
|
||||
/// after <see cref="SkipReason"/> came back <see langword="null"/> — the reaper never throws, but
|
||||
/// against a dead server it would just burn a connect timeout for nothing.
|
||||
/// </summary>
|
||||
public static void ReapOrphanedTestDatabasesOnce() => _ = Reaper.Value;
|
||||
public static void ReapOrphanedTestDatabasesOnce() => Instance.ReapOrphanedTestDatabasesOnce();
|
||||
|
||||
/// <summary>Builds a connection string for the named database on the configured test server.</summary>
|
||||
/// <param name="database">The database to connect to.</param>
|
||||
@@ -201,6 +203,29 @@ internal static class AdminUiSqlServer
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Guards and caches the discovery probe and fixture reaper.</summary>
|
||||
internal sealed class AdminUiSqlServerProbe
|
||||
{
|
||||
private const string UnitOnlyReason = "SQL Server is unavailable in unit-only discovery mode.";
|
||||
private readonly Lazy<string?> _probe;
|
||||
private readonly Lazy<int> _reaper;
|
||||
|
||||
internal AdminUiSqlServerProbe(Func<string?> probe, Func<int> reaper)
|
||||
{
|
||||
_probe = new Lazy<string?>(probe, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
_reaper = new Lazy<int>(reaper, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
}
|
||||
|
||||
private static bool UnitOnly => Environment.GetEnvironmentVariable("OTOPCUA_UNIT_ONLY_DISCOVERY") == "1";
|
||||
|
||||
internal string? SkipReason => UnitOnly ? UnitOnlyReason : _probe.Value;
|
||||
|
||||
internal void ReapOrphanedTestDatabasesOnce()
|
||||
{
|
||||
if (!UnitOnly) _ = _reaper.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops test databases that a killed test process left behind on the <b>shared</b> SQL Server.
|
||||
/// </summary>
|
||||
@@ -392,7 +417,17 @@ public sealed class SqlServerFactAttribute : FactAttribute
|
||||
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = -1)
|
||||
: base(sourceFilePath, sourceLineNumber)
|
||||
{
|
||||
if (AdminUiSqlServer.SkipReason is { } reason) Skip = reason;
|
||||
SetSkipReason(AdminUiSqlServer.Instance);
|
||||
}
|
||||
|
||||
internal SqlServerFactAttribute(AdminUiSqlServerProbe server)
|
||||
{
|
||||
SetSkipReason(server);
|
||||
}
|
||||
|
||||
private void SetSkipReason(AdminUiSqlServerProbe server)
|
||||
{
|
||||
if (server.SkipReason is { } reason) Skip = reason;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.DataConnections;
|
||||
|
||||
[CollectionDefinition(nameof(GrpcDataDiscoveryEnvironmentCollection), DisableParallelization = true)]
|
||||
public sealed class GrpcDataDiscoveryEnvironmentCollection
|
||||
{
|
||||
}
|
||||
|
||||
[Collection(nameof(GrpcDataDiscoveryEnvironmentCollection))]
|
||||
[Trait("Feature", "GrpcData")]
|
||||
[Trait("Phase", "P00")]
|
||||
public sealed class GrpcDataUnitDiscoveryIsolationTests
|
||||
{
|
||||
private const string Switch = "OTOPCUA_UNIT_ONLY_DISCOVERY";
|
||||
|
||||
[Fact]
|
||||
public void UnitModeAttributeAndFixtureDoNotProbeOrReap()
|
||||
{
|
||||
var previous = Environment.GetEnvironmentVariable(Switch);
|
||||
try
|
||||
{
|
||||
Environment.SetEnvironmentVariable(Switch, "1");
|
||||
var probes = 0;
|
||||
var reaps = 0;
|
||||
var server = new AdminUiSqlServerProbe(() => { probes++; return null; }, () => { reaps++; return 0; });
|
||||
|
||||
var attribute = new SqlServerFactAttribute(server);
|
||||
using var fixture = new AdminUiSqlServerFixture(server);
|
||||
server.ReapOrphanedTestDatabasesOnce();
|
||||
|
||||
Assert.Contains("unit-only", attribute.Skip, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Equal(attribute.Skip, fixture.SkipReason);
|
||||
Assert.Equal(0, probes);
|
||||
Assert.Equal(0, reaps);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable(Switch, previous);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalModeAttributeUsesProbeAndReaperCallbackRunsOnce()
|
||||
{
|
||||
var previous = Environment.GetEnvironmentVariable(Switch);
|
||||
try
|
||||
{
|
||||
Environment.SetEnvironmentVariable(Switch, null);
|
||||
var probes = 0;
|
||||
var reaps = 0;
|
||||
var server = new AdminUiSqlServerProbe(() => { probes++; return "fake unavailable"; }, () => { reaps++; return 0; });
|
||||
|
||||
var attribute = new SqlServerFactAttribute(server);
|
||||
Assert.Equal("fake unavailable", attribute.Skip);
|
||||
Assert.Equal("fake unavailable", server.SkipReason);
|
||||
server.ReapOrphanedTestDatabasesOnce();
|
||||
server.ReapOrphanedTestDatabasesOnce();
|
||||
|
||||
Assert.Equal(1, probes);
|
||||
Assert.Equal(1, reaps);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable(Switch, previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user