Files
lmxopcua/tests/Client/ZB.MOM.WW.OtOpcUa.Client.CLI.Tests/AlarmsCommandTests.cs
T
Joseph Doherty 9496db9357 fix(client): #1048 Client.UI recursive subscribe + restore batch through SubscribeManyAsync; CLI subscribe/alarms exit 3 when a failover never recovers
- SubscriptionsViewModel collects the subtree's variables first (deduped, already-subscribed
  skipped) and creates them in one SubscribeManyAsync; per-node failures are named in
  StatusMessage, a whole-batch failure adds nothing. RestoreSubscriptionsAsync batches too.
- CLI: ConnectionLossWatch ends subscribe/alarms when the service lands Disconnected on its
  own, or stays out of Connected past --reconnect-timeout (default 300 s, 0 = no limit):
  WARNING line, summary (subscribe adds 'Connection: LOST'), exit code 3. A recovery inside
  the window re-arms the watch.
2026-12-03 15:07:30 -05:00

229 lines
9.8 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Tests.Fakes;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
public class AlarmsCommandTests
{
/// <summary>Verifies that Execute subscribes to alarms.</summary>
[Fact]
public async Task Execute_SubscribesToAlarms()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
Interval = 2000
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
fakeService.SubscribeAlarmsCalls.Count.ShouldBe(1);
fakeService.SubscribeAlarmsCalls[0].IntervalMs.ShouldBe(2000);
fakeService.SubscribeAlarmsCalls[0].SourceNodeId.ShouldBeNull();
}
/// <summary>Verifies that Execute with node passes source node ID.</summary>
[Fact]
public async Task Execute_WithNode_PassesSourceNodeId()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=AlarmSource"
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
fakeService.SubscribeAlarmsCalls.Count.ShouldBe(1);
fakeService.SubscribeAlarmsCalls[0].SourceNodeId.ShouldNotBeNull();
fakeService.SubscribeAlarmsCalls[0].SourceNodeId!.Value.IdentifierAsString.ShouldBe("AlarmSource");
}
/// <summary>Verifies that Execute with refresh requests condition refresh.</summary>
[Fact]
public async Task Execute_WithRefresh_RequestsConditionRefresh()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
Refresh = true
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
fakeService.RequestConditionRefreshCalled.ShouldBeTrue();
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Condition refresh requested.");
}
/// <summary>The printed alarm carries the ConditionNodeId and EventId that `ack` requires (hex round-trips).</summary>
[Fact]
public async Task Execute_PrintsConditionNodeIdAndEventId_ForAck()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory) { Url = "opc.tcp://localhost:4840" };
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
fakeService.RaiseAlarmEvent(new Client.Shared.Models.AlarmEventArgs(
"src", "cond", 500, "msg", true, true, false, DateTime.UtcNow,
eventId: [0xAB, 0x01, 0xFF], conditionNodeId: "ns=2;s=Plant/cond"));
await Task.Delay(500, TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("ConditionNodeId: ns=2;s=Plant/cond");
output.ShouldContain("EventId: AB01FF");
}
/// <summary>The printed alarm names the EventType, so a script can spot RefreshRequired (i=2789) from a consumer.</summary>
[Fact]
public async Task Execute_PrintsEventType_NamingRefreshRequired()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory) { Url = "opc.tcp://localhost:4840" };
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
fakeService.RaiseAlarmEvent(new Client.Shared.Models.AlarmEventArgs(
"Server", "", 100, "refresh", false, false, false, DateTime.UtcNow, eventType: "i=2789"));
fakeService.RaiseAlarmEvent(new Client.Shared.Models.AlarmEventArgs(
"src", "cond", 500, "msg", true, true, false, DateTime.UtcNow, eventType: "ns=0;i=10637"));
await Task.Delay(500, TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("EventType: RefreshRequiredEventType (i=2789)");
output.ShouldContain("EventType: ns=0;i=10637");
}
/// <summary>Verifies that refresh failure prints error.</summary>
[Fact]
public async Task Execute_RefreshFailure_PrintsError()
{
var fakeService = new FakeOpcUaClientService
{
ConditionRefreshException = new NotSupportedException("Not supported")
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
Refresh = true
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Condition refresh not supported:");
}
/// <summary>Verifies that Execute unsubscribes on cancellation.</summary>
[Fact]
public async Task Execute_UnsubscribesOnCancellation()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840"
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
fakeService.UnsubscribeAlarmsCalled.ShouldBeTrue();
}
/// <summary>Verifies that Execute disconnects in finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840"
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
console.RequestCancellation();
await task;
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
/// <summary>#1048: an outage that outlasts --reconnect-timeout ends the alarm watch non-zero, with a WARNING.</summary>
[Fact]
public async Task Execute_ExitsNonZero_WhenTheConnectionNeverRecovers()
{
var fakeService = new FakeOpcUaClientService();
var command = new AlarmsCommand(new FakeOpcUaClientServiceFactory(fakeService))
{
Url = "opc.tcp://localhost:4840",
ReconnectTimeoutSeconds = 1,
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); }, TestContext.Current.CancellationToken);
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
fakeService.RaiseConnectionStateChanged(Shared.Models.ConnectionState.Connected,
Shared.Models.ConnectionState.Reconnecting, "opc.tcp://localhost:4840");
var ex = await Should.ThrowAsync<CliFx.Exceptions.CommandException>(
() => task.WaitAsync(TimeSpan.FromSeconds(15), TestContext.Current.CancellationToken));
ex.ExitCode.ShouldNotBe(0);
TestConsoleHelper.GetOutput(console).ShouldContain("WARNING");
fakeService.DisconnectCalled.ShouldBeTrue();
}
}