130 lines
6.4 KiB
C#
130 lines
6.4 KiB
C#
using Opc.Ua;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
|
|
using ZB.MOM.WW.OtOpcUa.Client.CLI.Tests.Fakes;
|
|
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
|
|
|
|
/// <summary>
|
|
/// The hand-paged (<c>--page-size</c> + pause / abandon / release / fresh-after-failure) and AtTime (<c>--at-time</c>)
|
|
/// modes of <c>historyread</c> the gRPC data H3 live gate drives against projected nodes.
|
|
/// </summary>
|
|
public class HistoryReadPagedCommandTests
|
|
{
|
|
private static readonly DateTime T0 = new(2026, 11, 20, 10, 0, 0, DateTimeKind.Utc);
|
|
|
|
private static DataValue Value(int v, int second) => new(new Variant(v), StatusCodes.Good, T0.AddSeconds(second), T0.AddSeconds(second));
|
|
|
|
[Fact]
|
|
public async Task Pages_print_each_status_and_resume_with_the_returned_continuation_point()
|
|
{
|
|
var fake = new FakeOpcUaClientService();
|
|
fake.HistoryPages.Enqueue(new HistoryPage(StatusCodes.Good, [Value(1, 0), Value(2, 1)], [1, 2, 3]));
|
|
fake.HistoryPages.Enqueue(new HistoryPage(StatusCodes.Good, [Value(3, 2)], null));
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
var command = new HistoryReadCommand(new FakeOpcUaClientServiceFactory(fake))
|
|
{
|
|
Url = "opc.tcp://localhost:4840", NodeId = "ns=3;s=p1.x", StartTime = "2026-11-20T09:00:00Z", EndTime = "2026-11-20T11:00:00Z", PageSize = 2,
|
|
};
|
|
await command.ExecuteAsync(console);
|
|
var output = TestConsoleHelper.GetOutput(console);
|
|
output.ShouldContain("Page 1: Good [0x00000000] values=2 continuation=yes");
|
|
output.ShouldContain("Page 2: Good [0x00000000] values=1 continuation=no resumed");
|
|
output.ShouldContain("3 values returned.");
|
|
fake.HistoryPageCalls.Count.ShouldBe(2);
|
|
fake.HistoryPageCalls[0].ContinuationPoint.ShouldBeNull();
|
|
fake.HistoryPageCalls[1].ContinuationPoint.ShouldBe(new byte[] { 1, 2, 3 });
|
|
((ReadRawModifiedDetails)fake.HistoryPageCalls[0].Details).NumValuesPerNode.ShouldBe(2u);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_refused_resume_is_printed_and_followed_by_one_fresh_read()
|
|
{
|
|
var fake = new FakeOpcUaClientService();
|
|
fake.HistoryPages.Enqueue(new HistoryPage(StatusCodes.Good, [Value(1, 0)], [9]));
|
|
fake.HistoryPages.Enqueue(new HistoryPage(StatusCodes.BadContinuationPointInvalid, [], null));
|
|
fake.HistoryPages.Enqueue(new HistoryPage(StatusCodes.Good, [Value(1, 0)], [7]));
|
|
var marker = Path.Combine(Path.GetTempPath(), "cli-resume-" + Guid.NewGuid().ToString("N"));
|
|
File.WriteAllText(marker, "");
|
|
try
|
|
{
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
var command = new HistoryReadCommand(new FakeOpcUaClientServiceFactory(fake))
|
|
{
|
|
Url = "opc.tcp://localhost:4840", NodeId = "ns=3;s=p1.x", PageSize = 1, PauseAfterPage = 1, ResumeWhenFile = marker,
|
|
FreshAfterFailure = true,
|
|
};
|
|
await command.ExecuteAsync(console);
|
|
var output = TestConsoleHelper.GetOutput(console);
|
|
output.ShouldContain("Paused after page 1");
|
|
output.ShouldContain("Page 2: BadContinuationPointInvalid [0x804A0000] values=0 continuation=no resumed");
|
|
output.ShouldContain("Fresh: Good [0x00000000] values=1 continuation=yes");
|
|
fake.HistoryPageCalls.Last().Release.ShouldBeTrue("the fresh read's own continuation is released");
|
|
}
|
|
finally { File.Delete(marker); }
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public async Task Abandon_leaves_the_point_to_the_session_close_and_release_sends_releaseContinuationPoints(bool release)
|
|
{
|
|
var fake = new FakeOpcUaClientService();
|
|
fake.HistoryPages.Enqueue(new HistoryPage(StatusCodes.Good, [Value(1, 0)], [5]));
|
|
var held = Path.Combine(Path.GetTempPath(), "cli-held-" + Guid.NewGuid().ToString("N"));
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
var command = new HistoryReadCommand(new FakeOpcUaClientServiceFactory(fake))
|
|
{
|
|
Url = "opc.tcp://localhost:4840", NodeId = "ns=3;s=p1.x", PageSize = 1,
|
|
AbandonAfterPage = release ? 0 : 1, ReleaseAfterPage = release ? 1 : 0, HeldMarkerFile = held,
|
|
};
|
|
await command.ExecuteAsync(console);
|
|
File.Exists(held).ShouldBeTrue("the held marker is written at the abandon / release point");
|
|
File.Delete(held);
|
|
var output = TestConsoleHelper.GetOutput(console);
|
|
if (release)
|
|
{
|
|
output.ShouldContain("Released after page 1: Good");
|
|
fake.HistoryPageCalls.Count.ShouldBe(2);
|
|
fake.HistoryPageCalls[1].Release.ShouldBeTrue();
|
|
fake.HistoryPageCalls[1].ContinuationPoint.ShouldBe(new byte[] { 5 });
|
|
}
|
|
else
|
|
{
|
|
output.ShouldContain("Abandoned after page 1");
|
|
fake.HistoryPageCalls.Count.ShouldBe(1);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AtTime_reads_the_requested_times_and_prints_the_status_and_value_types()
|
|
{
|
|
var fake = new FakeOpcUaClientService();
|
|
fake.HistoryPages.Enqueue(new HistoryPage(StatusCodes.Good, [new DataValue(new Variant((ushort)7), StatusCodes.Good, T0, T0)], null));
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
var command = new HistoryReadCommand(new FakeOpcUaClientServiceFactory(fake))
|
|
{
|
|
Url = "opc.tcp://localhost:4840", NodeId = "ns=3;s=p1.x", AtTimes = "2026-11-20T10:00:00Z, 2026-11-20T10:00:01Z",
|
|
};
|
|
await command.ExecuteAsync(console);
|
|
var output = TestConsoleHelper.GetOutput(console);
|
|
output.ShouldContain("Status: Good [0x00000000]");
|
|
output.ShouldContain("Type: UInt16");
|
|
var details = fake.HistoryPageCalls.Single().Details.ShouldBeOfType<ReadAtTimeDetails>();
|
|
details.ReqTimes.Count.ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Pause_without_a_resume_file_is_refused()
|
|
{
|
|
var command = new HistoryReadCommand(new FakeOpcUaClientServiceFactory(new FakeOpcUaClientService()))
|
|
{
|
|
Url = "opc.tcp://localhost:4840", NodeId = "ns=3;s=p1.x", PageSize = 1, PauseAfterPage = 1,
|
|
};
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
await Should.ThrowAsync<CliFx.Exceptions.CommandException>(async () => await command.ExecuteAsync(console));
|
|
}
|
|
}
|