fix(opcua): preserve repeated alarm methods and owned write reverts
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Server;
|
||||
@@ -15,6 +16,7 @@ public sealed class Sdk2EquipmentVariableTests : IAsyncLifetime
|
||||
private OtOpcUaSdkServer _server = null!;
|
||||
private OtOpcUaNodeManager _manager = null!;
|
||||
private readonly RecordingGateway _gateway = new();
|
||||
private readonly OutcomeLogger _outcomeLogger = new();
|
||||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
@@ -24,7 +26,7 @@ public sealed class Sdk2EquipmentVariableTests : IAsyncLifetime
|
||||
{
|
||||
ApplicationName = "SDK2.ValueContracts", ApplicationUri = "urn:SDK2.ValueContracts",
|
||||
PublicHostname = "localhost", OpcUaPort = port, PkiStoreRoot = _pki,
|
||||
}, NullLogger<OpcUaApplicationHost>.Instance), () => new OtOpcUaSdkServer(), Ct);
|
||||
}, NullLogger<OpcUaApplicationHost>.Instance), () => new OtOpcUaSdkServer(_outcomeLogger), Ct);
|
||||
_host = started.Host;
|
||||
_server = started.Server;
|
||||
_manager = _server.NodeManager!;
|
||||
@@ -214,6 +216,123 @@ public sealed class Sdk2EquipmentVariableTests : IAsyncLifetime
|
||||
scalarBytes.TypeInfo.ValueRank.ShouldBe(ValueRanks.Scalar);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(AddressSpaceRealm.Raw)]
|
||||
[InlineData(AddressSpaceRealm.Uns)]
|
||||
public async Task An_immediate_failed_array_outcome_reverts_an_actual_node_write(AddressSpaceRealm realm)
|
||||
{
|
||||
var node = Node("Int16", array: true, realm: realm);
|
||||
_manager.WriteValue("v", new short[] { 7, 8 }, OpcUaStatus.Good, DateTime.UtcNow, realm);
|
||||
_manager.NodeWriteGateway = new OutcomeGateway(_ => Task.FromResult(new NodeWriteOutcome(false, "refused")));
|
||||
Write(node, new short[] { 1, 2 }).ShouldBe(ServiceResult.Good);
|
||||
var deadline = DateTime.UtcNow.AddSeconds(2);
|
||||
while (DateTime.UtcNow < deadline && !SdkValueBoundary.ToClr(node.Value).ShouldBeOfType<short[]>().SequenceEqual(new short[] { 7, 8 }))
|
||||
await Task.Delay(10, Ct);
|
||||
SdkValueBoundary.ToClr(node.Value).ShouldBeOfType<short[]>().ShouldBe([7, 8]);
|
||||
node.StatusCode.ShouldBe(StatusCodes.Good);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("equal-client-write")]
|
||||
[InlineData("equal-driver-poll")]
|
||||
[InlineData("status-only")]
|
||||
[InlineData("same-id-recreated")]
|
||||
[InlineData("shape-update")]
|
||||
public async Task An_old_failed_outcome_cannot_replace_a_newer_actual_node_tuple(string change)
|
||||
{
|
||||
var node = Node("Int16", array: true);
|
||||
_manager.WriteValue("v", new short[] { 7, 8 }, OpcUaStatus.Good, DateTime.UtcNow, AddressSpaceRealm.Raw);
|
||||
var first = new TaskCompletionSource<NodeWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var calls = 0;
|
||||
_manager.NodeWriteGateway = new OutcomeGateway(_ => ++calls == 1
|
||||
? first.Task : Task.FromResult(new NodeWriteOutcome(true, null)));
|
||||
Write(node, new short[] { 1, 2 }).ShouldBe(ServiceResult.Good);
|
||||
var stamp = new DateTime(2026, 11, 10, 12, 0, 0, DateTimeKind.Utc);
|
||||
switch (change)
|
||||
{
|
||||
case "equal-client-write": Write(node, new short[] { 1, 2 }).ShouldBe(ServiceResult.Good); break;
|
||||
case "equal-driver-poll":
|
||||
_manager.WriteValue("v", new short[] { 1, 2 }, StatusCodes.UncertainLastUsableValue.Code, stamp, AddressSpaceRealm.Raw); break;
|
||||
case "status-only":
|
||||
_manager.WriteStatuses([new StatusWrite("v", StatusCodes.BadNoCommunication.Code, stamp, AddressSpaceRealm.Raw)]); break;
|
||||
case "same-id-recreated":
|
||||
_manager.RemoveVariableNode("v", AddressSpaceRealm.Raw).ShouldBeTrue();
|
||||
node = Node("Int16", array: true);
|
||||
Write(node, new short[] { 1, 2 }).ShouldBe(ServiceResult.Good); break;
|
||||
case "shape-update":
|
||||
_manager.UpdateTagAttributes("v", true, null, "Int32", true, 2, AddressSpaceRealm.Raw).ShouldBeTrue(); break;
|
||||
}
|
||||
var expected = node.Value;
|
||||
var status = node.StatusCode;
|
||||
var time = node.Timestamp;
|
||||
first.SetResult(new NodeWriteOutcome(false, "older write failed"));
|
||||
await _outcomeLogger.Ignored.Task.WaitAsync(TimeSpan.FromSeconds(5), Ct);
|
||||
node.Value.ShouldBe(expected);
|
||||
node.StatusCode.ShouldBe(status);
|
||||
node.Timestamp.ShouldBe(time);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_reentrant_driver_publication_wins_over_the_callback_optimistic_commit()
|
||||
{
|
||||
var node = Node("Int16", array: true);
|
||||
_manager.WriteValue("v", new short[] { 7, 8 }, OpcUaStatus.Good, DateTime.UtcNow, AddressSpaceRealm.Raw);
|
||||
var first = new TaskCompletionSource<NodeWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var stamp = new DateTime(2026, 11, 10, 12, 0, 0, DateTimeKind.Utc);
|
||||
_manager.NodeWriteGateway = new OutcomeGateway(_ =>
|
||||
{
|
||||
_manager.WriteValue("v", new short[] { 1, 2 }, StatusCodes.UncertainLastUsableValue.Code, stamp, AddressSpaceRealm.Raw);
|
||||
return first.Task;
|
||||
});
|
||||
Write(node, new short[] { 1, 2 }).ShouldBe(ServiceResult.Good);
|
||||
node.StatusCode.ShouldBe(StatusCodes.UncertainLastUsableValue);
|
||||
node.Timestamp.ToDateTime().ShouldBe(stamp);
|
||||
first.SetResult(new NodeWriteOutcome(false, "old write failed"));
|
||||
await _outcomeLogger.Ignored.Task.WaitAsync(TimeSpan.FromSeconds(5), Ct);
|
||||
SdkValueBoundary.ToClr(node.Value).ShouldBeOfType<short[]>().ShouldBe([1, 2]);
|
||||
node.StatusCode.ShouldBe(StatusCodes.UncertainLastUsableValue);
|
||||
node.Timestamp.ToDateTime().ShouldBe(stamp);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task A_refused_attempt_does_not_disown_an_older_committed_write(bool synchronousThrow)
|
||||
{
|
||||
var node = Node("Int16", array: true);
|
||||
_manager.WriteValue("v", new short[] { 7, 8 }, OpcUaStatus.Good, DateTime.UtcNow, AddressSpaceRealm.Raw);
|
||||
var first = new TaskCompletionSource<NodeWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
_manager.NodeWriteGateway = new OutcomeGateway(_ => first.Task);
|
||||
Write(node, new short[] { 1, 2 }).ShouldBe(ServiceResult.Good);
|
||||
if (synchronousThrow)
|
||||
{
|
||||
_manager.NodeWriteGateway = new OutcomeGateway(_ => throw new InvalidOperationException("gateway threw"));
|
||||
Write(node, new short[] { 3, 4 }).StatusCode.ShouldBe(StatusCodes.BadDeviceFailure);
|
||||
}
|
||||
else Write(node, new short[] { 3, 4 }, authorized: false).StatusCode.ShouldBe(StatusCodes.BadUserAccessDenied);
|
||||
first.SetResult(new NodeWriteOutcome(false, "first write failed"));
|
||||
var deadline = DateTime.UtcNow.AddSeconds(2);
|
||||
while (DateTime.UtcNow < deadline && !SdkValueBoundary.ToClr(node.Value).ShouldBeOfType<short[]>().SequenceEqual(new short[] { 7, 8 }))
|
||||
await Task.Delay(10, Ct);
|
||||
SdkValueBoundary.ToClr(node.Value).ShouldBeOfType<short[]>().ShouldBe([7, 8]);
|
||||
}
|
||||
|
||||
private sealed class OutcomeLogger : ILogger<OtOpcUaNodeManager>
|
||||
{
|
||||
internal TaskCompletionSource Ignored { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
public bool IsEnabled(LogLevel level) => true;
|
||||
public void Log<TState>(LogLevel level, EventId id, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
if (formatter(state, exception).Contains("ignored superseded write outcome", StringComparison.Ordinal)) Ignored.TrySetResult();
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class OutcomeGateway(Func<object?, Task<NodeWriteOutcome>> outcome) : IOpcUaNodeWriteGateway
|
||||
{
|
||||
public Task<NodeWriteOutcome> WriteAsync(string nodeId, object? value, AddressSpaceRealm realm, CancellationToken ct) => outcome(value);
|
||||
}
|
||||
|
||||
private BaseDataVariableState Node(string type, bool array = false, bool refuse = false,
|
||||
bool writable = true, AddressSpaceRealm realm = AddressSpaceRealm.Raw)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user