using CliFx.Infrastructure; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli.Commands; namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli.Tests; /// /// Review 07 U7: the AbCip probe/read/write commands had no arm, so a /// Ctrl-C printed a stack trace. A command cancelled by the operator now exits cleanly with Cancelled.. /// The gateway is TEST-NET (never answers), so nothing is sent to a real device. /// [Trait("Category", "Unit")] public sealed class CancellationHandlingTests { private const string Blackhole = "ab://192.0.2.1/1,0"; private static async Task RunCancelledAsync(AbCipCommandBase command) { using var console = new FakeInMemoryConsole(); console.RequestCancellation(); await Should.NotThrowAsync(async () => await command.ExecuteAsync(console)); return console.ReadErrorString(); } /// A cancelled read prints Cancelled. and does not throw. [Fact] public async Task A_cancelled_read_prints_Cancelled() { (await RunCancelledAsync(new ReadCommand { Gateway = Blackhole, TagPath = "Motor01" })) .ShouldContain("Cancelled."); } /// A cancelled probe prints Cancelled. and does not throw. [Fact] public async Task A_cancelled_probe_prints_Cancelled() { (await RunCancelledAsync(new ProbeCommand { Gateway = Blackhole, TagPath = "Motor01" })) .ShouldContain("Cancelled."); } /// A cancelled write prints Cancelled. and does not throw. [Fact] public async Task A_cancelled_write_prints_Cancelled() { (await RunCancelledAsync(new WriteCommand { Gateway = Blackhole, TagPath = "Motor01", Value = "1" })) .ShouldContain("Cancelled."); } }