test(grpc-data): A1 — real-driver signal rig (StartRig over IDriver), FOCAS end-to-end; driver test classes in the A1 manifest

This commit is contained in:
Joseph Doherty
2026-11-24 10:25:47 -05:00
parent 9167ea2323
commit 141e4412d2
3 changed files with 221 additions and 4 deletions
+50 -1
View File
@@ -974,7 +974,8 @@
"OpcUaPublishActorConditionKnownTests",
"NativeAlarmInitialStateSignalTests",
"ScriptedAlarmHostInputsEstablishedTests",
"GrpcDataConditionPlacementTests"
"GrpcDataConditionPlacementTests",
"RealDriverInitialStateSignalTests"
],
"existing_classes": [
"OpcUaPublishActorDeferredWriteTests",
@@ -1020,6 +1021,54 @@
"existing_classes": [
"GrpcDataV1RealOriginCompatibilityTests"
]
},
{
"project": "tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.csproj",
"filter": "(Feature=GrpcData&Phase=A1)",
"new_classes": [
"GalaxyInitialAlarmStateSignalTests"
],
"existing_classes": []
},
{
"project": "tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Tests/ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Tests.csproj",
"filter": "(Feature=GrpcData&Phase=A1)",
"new_classes": [
"OpcUaClientInitialAlarmStateSignalTests"
],
"existing_classes": []
},
{
"project": "tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests/ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests.csproj",
"filter": "(Feature=GrpcData&Phase=A1)",
"new_classes": [
"FocasInitialAlarmStateSignalTests"
],
"existing_classes": []
},
{
"project": "tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests/ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests.csproj",
"filter": "(Feature=GrpcData&Phase=A1)",
"new_classes": [
"AbCipInitialAlarmStateSignalTests"
],
"existing_classes": []
},
{
"project": "tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests/ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests.csproj",
"filter": "(Feature=GrpcData&Phase=A1)",
"new_classes": [
"HeidenhainInitialAlarmStateSignalTests"
],
"existing_classes": []
},
{
"project": "tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.MTConnect.Tests/ZB.MOM.WW.OtOpcUa.Driver.MTConnect.Tests.csproj",
"filter": "(Feature=GrpcData&Phase=A1)",
"new_classes": [
"MTConnectInitialAlarmStateSignalTests"
],
"existing_classes": []
}
]
}
@@ -55,10 +55,30 @@ public abstract class NativeAlarmSignalTestBase : RuntimeActorTestBase
/// <returns>The rig.</returns>
protected Rig StartRig(string driverType, IReadOnlyList<string> alarmTags,
Func<ConditionSignalOptions, ConditionSignalOptions>? configure = null, FakeSignallingAlarmDriver? driver = null)
{
driver ??= new FakeSignallingAlarmDriver(DriverId, driverType);
var fake = driver;
var (host, publish, hub) = StartRig(driver, driverType, alarmTags, configure, () => fake.Subscribed);
return new Rig(host, publish, driver, hub);
}
/// <summary>
/// The same rig over ANY <see cref="IDriver"/> (a REAL driver over a fake device): deploys
/// <paramref name="alarmTags"/> on it and waits for the apply, then for <paramref name="ready"/> (e.g. the real
/// driver's alarm subscription) when given.
/// </summary>
/// <param name="driver">The driver the host spawns (its <see cref="IDriver.DriverInstanceId"/> must be <see cref="DriverId"/>).</param>
/// <param name="driverType">The driver type.</param>
/// <param name="alarmTags">The alarm tag names (RawPaths per <see cref="RawPathOf"/>).</param>
/// <param name="configure">Adjusts the signal options (timeouts).</param>
/// <param name="ready">Optional readiness predicate awaited after the apply.</param>
/// <returns>The host, the publish probe and the hub.</returns>
protected (IActorRef Host, TestProbe Publish, ConditionStateHub Hub) StartRig(IDriver driver, string driverType,
IReadOnlyList<string> alarmTags, Func<ConditionSignalOptions, ConditionSignalOptions>? configure = null,
Func<bool>? ready = null)
{
var db = NewInMemoryDbFactory();
var deploymentId = Seed(db, driverType, alarmTags);
driver ??= new FakeSignallingAlarmDriver(DriverId, driverType);
var hub = new ConditionStateHub();
var options = new ConditionSignalOptions { Hub = hub };
options = configure?.Invoke(options) ?? options;
@@ -77,8 +97,8 @@ public abstract class NativeAlarmSignalTestBase : RuntimeActorTestBase
host.Tell(new DispatchDeployment(deploymentId, Rev, CorrelationId.NewId()));
coordinator.FishForMessage<ApplyAck>(_ => true, TimeSpan.FromSeconds(10), cancellationToken: TestContext.Current.CancellationToken).Outcome.ShouldBe(ApplyAckOutcome.Applied);
AwaitCondition(() => driver.Subscribed, TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
return new Rig(host, publish, driver, hub);
if (ready is not null) AwaitCondition(ready, TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
return (host, publish, hub);
}
/// <summary>Negative control: for <paramref name="duration"/>, the probe receives no
@@ -0,0 +1,148 @@
using System.Text.Json;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
/// <summary>
/// grpc-data A1 T2 (ii)/(viii) END TO END with a REAL driver: the real <see cref="FocasDriver"/> (over a fake CNC)
/// spawned by the real <see cref="DriverHostActor"/> + <c>DriverInstanceActor</c> through the native-alarm signal
/// rig. The initial replay (a standing alarm read after quality Good) projects Active BEFORE the condition is marked
/// known, and only by the driver's <see cref="IAlarmInitialStateSignal"/>; a second, unreachable CNC's condition is
/// never marked and its F5 diagnostic does not touch the reachable one.
/// </summary>
[Trait("Feature", "GrpcData")]
[Trait("Phase", "A1")]
public sealed class RealDriverInitialStateSignalTests : NativeAlarmSignalTestBase
{
private const string HostA = "focas://10.0.0.5:8193";
private const string HostB = "focas://10.0.0.6:8193";
private static readonly string Servo = RawPathOf("servo");
private static readonly string ServoB = RawPathOf("servo_b");
private static CancellationToken Ct => TestContext.Current.CancellationToken;
/// <summary>(ii) FOCAS: the standing alarm's Active projection precedes the mark; the mark is the only way known.</summary>
[Fact]
public async Task Focas_standing_alarm_projects_before_its_condition_is_marked_known()
{
var driver = Focas([("servo", HostA)], new CncFactory(standingOn: HostA, unreachable: null));
var rig = StartRig(driver, DriverTypeNames.FOCAS, ["servo"]);
var traffic = CollectAlarmTraffic(rig.Publish, m => m is OpcUaPublishActor.MarkConditionsKnown);
traffic[0].ShouldBeOfType<OpcUaPublishActor.AlarmStateUpdate>().AlarmNodeId.ShouldBe(Servo);
((OpcUaPublishActor.AlarmStateUpdate)traffic[0]).State.Active.ShouldBeTrue();
var mark = traffic[^1].ShouldBeOfType<OpcUaPublishActor.MarkConditionsKnown>();
mark.Source.ShouldBe(DriverId);
mark.Realm.ShouldBe(AddressSpaceRealm.Raw);
mark.ConditionIds.ShouldBe([Servo]);
await driver.ShutdownAsync(Ct);
}
/// <summary>
/// (viii) G1: CNC B never answers its alarm read. A's condition is marked known; B's never is; after the F5
/// timeout the diagnostic counts B alone, and A stays known (no unknown record for it).
/// </summary>
[Fact]
public async Task Focas_unreachable_cnc_stays_unknown_without_affecting_the_reachable_one()
{
var driver = Focas([("servo", HostA), ("servo_b", HostB)], new CncFactory(standingOn: null, unreachable: HostB));
var rig = StartRig(driver, DriverTypeNames.FOCAS, ["servo", "servo_b"],
o => o.WithSignalTimeout(DriverTypeNames.FOCAS, TimeSpan.FromSeconds(2)),
ready: null);
var first = rig.Publish.FishForMessage<OpcUaPublishActor.MarkConditionsKnown>(_ => true, TimeSpan.FromSeconds(10), cancellationToken: Ct);
first.ConditionIds.ShouldBe([Servo]);
AwaitCondition(() => rig.Hub.Unsignalled.ContainsKey(DriverId), TimeSpan.FromSeconds(10), Ct);
rig.Hub.Unsignalled[DriverId].Conditions.ShouldBe(1, "only B is still unsignalled");
// A keeps being re-established every poll; B never appears in any mark, and nothing marks A unknown.
var deadline = DateTime.UtcNow + TimeSpan.FromMilliseconds(500);
while (DateTime.UtcNow < deadline)
{
if (rig.Publish.ReceiveOne(deadline - DateTime.UtcNow, Ct) is OpcUaPublishActor.MarkConditionsKnown m)
{
m.ConditionIds.ShouldNotContain(ServoB);
m.Known.ShouldBeTrue();
}
}
await driver.ShutdownAsync(Ct);
}
private static FocasDriver Focas(IReadOnlyList<(string Tag, string Host)> tags, CncFactory factory) =>
new(new FocasDriverOptions
{
Devices = [.. tags.Select(t => t.Host).Distinct().Select(h => new FocasDeviceOptions(h, DeviceName: h))],
RawTags =
[
.. tags.Select(t => new RawTagEntry(RawPathOf(t.Tag), JsonSerializer.Serialize(new Dictionary<string, object>
{
["address"] = "ALARM:Servo",
["dataType"] = "Int32",
["alarm"] = new Dictionary<string, object> { ["alarmType"] = "AlarmCondition" },
}), WriteIdempotent: false, DeviceName: t.Host)),
],
Probe = new FocasProbeOptions { Enabled = false },
AlarmProjection = new FocasAlarmProjectionOptions { PollInterval = TimeSpan.FromMilliseconds(50) },
}, DriverId, factory);
/// <summary>Hands out one fake CNC per connect; the CNC learns its host on <c>ConnectAsync</c>.</summary>
private sealed class CncFactory(string? standingOn, string? unreachable) : IFocasClientFactory
{
public IFocasClient Create() => new Cnc(standingOn, unreachable);
public void EnsureUsable() { }
}
private sealed class Cnc(string? standingOn, string? unreachable) : IFocasClient
{
private string? _host;
public bool IsConnected { get; private set; }
public Task ConnectAsync(FocasHostAddress address, TimeSpan timeout, CancellationToken cancellationToken)
{
_host = $"focas://{address.Host}:{address.Port}";
IsConnected = true;
return Task.CompletedTask;
}
public Task<IReadOnlyList<FocasActiveAlarm>> ReadAlarmsAsync(CancellationToken cancellationToken)
{
if (_host == unreachable) throw new IOException("CNC unreachable (fake)");
IReadOnlyList<FocasActiveAlarm> alarms = _host == standingOn
? [new FocasActiveAlarm(401, FocasAlarmType.Servo, 1, "SERVO V-READY OFF")]
: [];
return Task.FromResult(alarms);
}
public Task<bool> ProbeAsync(CancellationToken cancellationToken) => Task.FromResult(true);
public void Dispose() => IsConnected = false;
public Task<(object? value, uint status)> ReadAsync(FocasAddress address, FocasDataType type, CancellationToken cancellationToken) => throw Unused();
public Task<uint> WriteAsync(FocasAddress address, FocasDataType type, object? value, CancellationToken cancellationToken) => throw Unused();
public Task<(IReadOnlyList<long>? slots, uint status)> ReadPmcSlotsAsync(string letter, FocasDataType slotType, int start, int end, CancellationToken cancellationToken) => throw Unused();
public Task<FocasSysInfo> GetSysInfoAsync(CancellationToken cancellationToken) => throw Unused();
public Task<IReadOnlyList<FocasAxisName>> GetAxisNamesAsync(CancellationToken cancellationToken) => throw Unused();
public Task<IReadOnlyList<FocasSpindleName>> GetSpindleNamesAsync(CancellationToken cancellationToken) => throw Unused();
public Task<FocasDynamicSnapshot> ReadDynamicAsync(int axisIndex, CancellationToken cancellationToken) => throw Unused();
public Task<FocasProgramInfo> GetProgramInfoAsync(CancellationToken cancellationToken) => throw Unused();
public Task<FocasStatus> GetStatusAsync(CancellationToken cancellationToken) => throw Unused();
public Task<FocasTimer> GetTimerAsync(FocasTimerKind kind, CancellationToken cancellationToken) => throw Unused();
public Task<IReadOnlyList<FocasServoLoad>> GetServoLoadsAsync(CancellationToken cancellationToken) => throw Unused();
public Task<IReadOnlyList<double?>> GetSpindleLoadsAsync(CancellationToken cancellationToken) => throw Unused();
public Task<IReadOnlyList<double?>> GetSpindleMotorSpeedsAsync(CancellationToken cancellationToken) => throw Unused();
private static NotSupportedException Unused() => new("not reached by the alarm signal test");
}
}