138 lines
6.1 KiB
C#
138 lines
6.1 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
/// <summary>A1 <see cref="ConditionStateHub"/>: bounded partitions, per-reader reset, readiness, F5 diagnostic.</summary>
|
|
[Trait("Feature", "GrpcData")]
|
|
[Trait("Phase", "A1")]
|
|
[Trait("Category", "Unit")]
|
|
public sealed class ConditionStateHubTests : IDisposable
|
|
{
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
private readonly string _pkiRoot = Path.Combine(Path.GetTempPath(), $"otopcua-a1hub-{Guid.NewGuid():N}");
|
|
|
|
private static ConditionStateRecord Rec(string id, bool active = false, bool known = true) => new(
|
|
AddressSpaceRealm.Raw, id, true, 0, false, known, active, true, true, true, AlarmShelvingKind.Unshelved, 500, "m",
|
|
0, null, DateTime.UtcNow, active, new ConditionActivationMarker("i", 0));
|
|
|
|
private sealed class Primary(bool value) : IDriverPrimaryRoleSource
|
|
{
|
|
public bool ShouldActAsPrimary { get; set; } = value;
|
|
public void PublishPrimaryRole(bool shouldActAsPrimary) => ShouldActAsPrimary = shouldActAsPrimary;
|
|
}
|
|
|
|
[Fact]
|
|
public void Versions_are_global_and_reads_return_records_after_the_cursor()
|
|
{
|
|
var hub = new ConditionStateHub(partitionCount: 4);
|
|
var v1 = hub.Append(Rec("a"));
|
|
var v2 = hub.Append(Rec("b"));
|
|
v2.ShouldBe(v1 + 1);
|
|
hub.Watermark.ShouldBe(v2);
|
|
hub.ReadAll(0)!.Select(r => r.ConditionId).ShouldBe(["a", "b"]);
|
|
hub.ReadAll(v1)!.Single().ConditionId.ShouldBe("b");
|
|
}
|
|
|
|
[Fact]
|
|
public void An_overflow_resets_only_a_reader_that_fell_behind_the_evicted_record()
|
|
{
|
|
var hub = new ConditionStateHub(partitionCount: 1, partitionCapacity: 3);
|
|
for (var i = 0; i < 3; i++) hub.Append(Rec($"c{i}"));
|
|
var caughtUp = hub.Watermark;
|
|
hub.Append(Rec("c3")); // evicts version 1
|
|
|
|
hub.Read(0, afterVersion: 0).ResetRequired.ShouldBeTrue(); // lost record 1
|
|
hub.Read(0, afterVersion: 1).ResetRequired.ShouldBeFalse(); // record 1 already applied
|
|
hub.Read(0, caughtUp).Records.Single().ConditionId.ShouldBe("c3");
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshot_complete_needs_apply_complete_and_the_Primary_verdict()
|
|
{
|
|
var primary = new Primary(false);
|
|
var hub = new ConditionStateHub(primary);
|
|
hub.IsSnapshotComplete.ShouldBeFalse();
|
|
hub.OnApplyStateChanged(true);
|
|
hub.IsSnapshotComplete.ShouldBeFalse(); // not Primary
|
|
primary.ShouldActAsPrimary = true;
|
|
hub.IsSnapshotComplete.ShouldBeTrue();
|
|
hub.OnApplyStateChanged(false); // a rebuild began
|
|
hub.IsSnapshotComplete.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void The_unsignalled_diagnostic_is_raised_and_cleared()
|
|
{
|
|
var hub = new ConditionStateHub();
|
|
hub.ReportUnsignalled("drv", TimeSpan.FromSeconds(120), 3);
|
|
hub.Unsignalled["drv"].Conditions.ShouldBe(3);
|
|
hub.ClearUnsignalled("drv").ShouldBeTrue();
|
|
hub.Unsignalled.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The falsely-empty negative test (A1): an origin restart with standing alarms. Until the apply completes the
|
|
/// hub is not snapshot-complete, and no record claims a condition known-inactive before its source projected.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task An_origin_restart_is_never_a_falsely_empty_or_falsely_normal_snapshot()
|
|
{
|
|
var primary = new Primary(true);
|
|
var hub = new ConditionStateHub(primary);
|
|
var (host, nm) = await BootAsync();
|
|
nm.ConditionStateSink = hub;
|
|
hub.IsSnapshotComplete.ShouldBeFalse(); // nothing applied yet: the cold node manager is not complete
|
|
|
|
nm.EnsureFolder("Plant/dev", null, "dev", AddressSpaceRealm.Raw);
|
|
nm.MaterialiseAlarmCondition("Plant/dev/A", "Plant/dev", "A", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true);
|
|
nm.MaterialiseAlarmCondition("Plant/dev/B", "Plant/dev", "B", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true);
|
|
hub.IsSnapshotComplete.ShouldBeFalse(); // mid-apply: the set may still be incomplete
|
|
nm.CompleteAddressSpaceApply();
|
|
hub.IsSnapshotComplete.ShouldBeTrue();
|
|
|
|
// A is a standing alarm whose replay has not arrived yet; nothing may say "known, inactive" about it.
|
|
hub.ReadAll(0)!.ShouldNotContain(r => r.StateKnown && !r.Active);
|
|
|
|
nm.WriteAlarmCondition("Plant/dev/A", new AlarmConditionSnapshot(true, false, true, true, AlarmShelvingKind.Unshelved, 700, "A"),
|
|
DateTime.UtcNow, AddressSpaceRealm.Raw);
|
|
nm.MarkConditionsKnown("drv", ["Plant/dev/A", "Plant/dev/B"], AddressSpaceRealm.Raw);
|
|
|
|
var records = hub.ReadAll(0)!;
|
|
records.Where(r => r.ConditionId == "Plant/dev/A" && r.StateKnown).ShouldAllBe(r => r.Active);
|
|
records.Last(r => r.ConditionId == "Plant/dev/B").StateKnown.ShouldBeTrue();
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
private async Task<(OpcUaApplicationHost Host, OtOpcUaNodeManager Nm)> BootAsync()
|
|
{
|
|
var started = await OpcUaHostStart.StartAsync(
|
|
port => new OpcUaApplicationHost(
|
|
new OpcUaApplicationHostOptions
|
|
{
|
|
ApplicationName = "OtOpcUa.A1HubTest",
|
|
ApplicationUri = $"urn:OtOpcUa.A1HubTest:{Guid.NewGuid():N}",
|
|
OpcUaPort = port,
|
|
PublicHostname = "localhost",
|
|
PkiStoreRoot = _pkiRoot,
|
|
},
|
|
Microsoft.Extensions.Logging.Abstractions.NullLogger<OpcUaApplicationHost>.Instance),
|
|
() => new OtOpcUaSdkServer(), Ct);
|
|
return (started.Host, started.Server.NodeManager!);
|
|
}
|
|
|
|
/// <summary>Cleans up the PKI root directory.</summary>
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(_pkiRoot))
|
|
{
|
|
try { Directory.Delete(_pkiRoot, recursive: true); }
|
|
catch { /* best-effort cleanup */ }
|
|
}
|
|
}
|
|
}
|