217 lines
15 KiB
C#
217 lines
15 KiB
C#
using System.Threading.Channels;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Client;
|
|
using SdkServerMonitoredItem = Opc.Ua.Server.MonitoredItem;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
public sealed partial class GrpcDataConfiguredDiscoveryWireTests
|
|
{
|
|
[Fact(Timeout = 90000), Trait("Phase", "P11Wire")]
|
|
public async Task Sdk_older_duplicate_cannot_consume_newer_timestamp_evidence()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
LocalDataFixture? peer = null;
|
|
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
|
|
using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions);
|
|
peer = origin;
|
|
subscriptions.SampleClock = origin.Clock;
|
|
var published = Channel.CreateUnbounded<ProjectedValueUpdate>();
|
|
await WithComposedServer(origin, origin.Raw, async (session, sink, _, _, _, _, anchor) =>
|
|
{
|
|
var client = (SessionClient)session;
|
|
await BrowseAll(client, anchor, 1);
|
|
var sub = (await client.CreateSubscriptionAsync(new RequestHeader(), 100, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
var created = await client.CreateMonitoredItemsAsync(new RequestHeader(), sub, TimestampsToReturn.Both,
|
|
[new MonitoredItemCreateRequest { ItemToMonitor = new ReadValueId { NodeId = anchor, AttributeId = Attributes.Value },
|
|
MonitoringMode = MonitoringMode.Reporting, RequestedParameters = new MonitoringParameters
|
|
{ ClientHandle = 1, SamplingInterval = 100, QueueSize = 3, DiscardOldest = true,
|
|
Filter = new ExtensionObject(new DataChangeFilter { Trigger = Opc.Ua.DataChangeTrigger.StatusValueTimestamp }) } }], ct);
|
|
created.Results[0].StatusCode.ShouldBe(StatusCodes.Good);
|
|
var stream = await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), false, [sub], ct);
|
|
var timestamp = origin.Clock.GetUtcNow().AddMinutes(-1);
|
|
stream.EmitValue(2, 42, timestamp, TimeSpan.Zero);
|
|
await NextPublished(published.Reader, x => !x.IsCompleteSnapshot &&
|
|
x.Snapshot.SourceTimestampUtc == timestamp.UtcDateTime && Equals(x.Snapshot.Value, 42), ct);
|
|
var later = timestamp.AddSeconds(1);
|
|
stream.EmitValue(3, 42, later, TimeSpan.Zero);
|
|
var newer = await NextPublished(published.Reader, x => !x.IsCompleteSnapshot && x.Snapshot.SourceTimestampUtc == later.UtcDateTime, ct);
|
|
|
|
// Force duplicate notifications at the SDK's public queue boundary. This is
|
|
// an identity-contract injection, not a claim that normal filtering duplicates them.
|
|
var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
|
|
var manager = sink.GetType().GetField("_nodeManager", flags)!.GetValue(sink)!;
|
|
var live = manager.GetType().GetField("_liveItems", flags)!.GetValue(manager)!;
|
|
var keys = (System.Collections.IEnumerable)live.GetType().GetProperty("Keys")!.GetValue(live)!;
|
|
var item = keys.Cast<object>().OfType<SdkServerMonitoredItem>().Single(x => x.ClientHandle == 1);
|
|
item.SetMonitoringMode(MonitoringMode.Disabled);
|
|
item.SetMonitoringMode(MonitoringMode.Reporting);
|
|
var queue = typeof(SdkServerMonitoredItem).GetMethods().Single(x => x.Name == "QueueValue" && x.GetParameters().Length == 3);
|
|
void Inject(DateTimeOffset sourceTime)
|
|
{
|
|
var injected = new Opc.Ua.DataValue(new Variant(42), StatusCodes.Good,
|
|
(DateTimeUtc)sourceTime.UtcDateTime, DateTimeUtc.MinValue);
|
|
queue.Invoke(item, [injected, ServiceResult.Good, true]);
|
|
}
|
|
await client.SetPublishingModeAsync(new RequestHeader(), true, [sub], ct);
|
|
Inject(timestamp);
|
|
var first = await PublishComposedValue(client, 1, ct);
|
|
first.SourceTimestamp.ShouldBe((DateTimeUtc)timestamp.UtcDateTime);
|
|
first.StatusCode.ShouldBe(StatusCodes.Good);
|
|
subscriptions.SnapshotVersion = 3;
|
|
subscriptions.SnapshotValue = 42;
|
|
subscriptions.HoldNextSnapshotEnd = true;
|
|
Inject(timestamp);
|
|
var duplicate = await PublishComposedValue(client, 1, ct);
|
|
StatusCode.IsGood(duplicate.StatusCode).ShouldBeFalse("an old duplicate must not acquire newer evidence");
|
|
var held = await subscriptions.SnapshotEndHeld.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
try
|
|
{
|
|
var candidates = (System.Collections.IEnumerable)item.GetType().GetField("_candidates", flags)!.GetValue(item)!;
|
|
var retained = candidates.Cast<object>().Select(candidate =>
|
|
(ProjectedValueWrite)candidate.GetType().GetProperty("Sample")!.GetValue(candidate)!).ToArray();
|
|
var surviving = retained.Single(candidate => candidate.Snapshot.SourceTimestampUtc == later.UtcDateTime);
|
|
surviving.Proof.ShouldBeSameAs(newer.SourceProof);
|
|
surviving.OriginAgeBounds.ShouldBe(newer.OriginAgeBounds);
|
|
surviving.ReceivedTimestamp.ShouldBe(newer.ReceivedTimestamp);
|
|
}
|
|
finally { held.ReleaseSnapshotEnd(); }
|
|
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good,
|
|
"a completed replacement baseline restores delivery after the rejection");
|
|
}, onPublished: update => published.Writer.TryWrite(update));
|
|
}
|
|
|
|
[Fact(Timeout = 90000), Trait("Phase", "P11Wire")]
|
|
public async Task Sdk_rejects_ambiguous_evidence_when_origin_source_timestamps_are_absent()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
LocalDataFixture? peer = null;
|
|
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
|
|
using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions);
|
|
peer = origin;
|
|
subscriptions.SampleClock = origin.Clock;
|
|
await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) =>
|
|
{
|
|
var client = (SessionClient)session;
|
|
await BrowseAll(client, anchor, 1);
|
|
var slow = (await client.CreateSubscriptionAsync(new RequestHeader(), 200, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
await AddComposedItem(client, slow, anchor, 1, 200, ct);
|
|
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), false, [slow], ct);
|
|
var fast = (await client.CreateSubscriptionAsync(new RequestHeader(), 50, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
await AddComposedItem(client, fast, anchor, 2, 50, ct);
|
|
var fastStream = await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
foreach (var (version, value) in new[] { (2L, 42), (3L, 44), (4L, 42) })
|
|
{
|
|
fastStream.EmitValueWithoutSourceTimestamp(version, value, TimeSpan.Zero);
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
}
|
|
await client.SetPublishingModeAsync(new RequestHeader(), false, [fast], ct);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), true, [slow], ct);
|
|
var valueWithoutProof = await PublishComposedValue(client, 1, ct);
|
|
StatusCode.IsGood(valueWithoutProof.StatusCode).ShouldBeFalse("absent timestamps cannot distinguish the two retained 42 acquisitions");
|
|
});
|
|
}
|
|
|
|
[Theory(Timeout = 90000), Trait("Phase", "P11Wire")]
|
|
[InlineData(TimestampsToReturn.Server)]
|
|
[InlineData(TimestampsToReturn.Neither)]
|
|
public async Task Sdk_rejects_ambiguous_evidence_when_source_timestamp_was_not_requested(TimestampsToReturn mode)
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
LocalDataFixture? peer = null;
|
|
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
|
|
using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions);
|
|
peer = origin;
|
|
subscriptions.SampleClock = origin.Clock;
|
|
await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) =>
|
|
{
|
|
var client = (SessionClient)session;
|
|
await BrowseAll(client, anchor, 1);
|
|
var slow = (await client.CreateSubscriptionAsync(new RequestHeader(), 200, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
var created = await client.CreateMonitoredItemsAsync(new RequestHeader(), slow, mode,
|
|
[new MonitoredItemCreateRequest { ItemToMonitor = new ReadValueId { NodeId = anchor, AttributeId = Attributes.Value },
|
|
MonitoringMode = MonitoringMode.Reporting, RequestedParameters = new MonitoringParameters
|
|
{ ClientHandle = 1, SamplingInterval = 200, QueueSize = 1, DiscardOldest = true,
|
|
Filter = new ExtensionObject(new DataChangeFilter { Trigger = Opc.Ua.DataChangeTrigger.StatusValueTimestamp }) } }], ct);
|
|
created.Results[0].StatusCode.ShouldBe(StatusCodes.Good);
|
|
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), false, [slow], ct);
|
|
var fast = (await client.CreateSubscriptionAsync(new RequestHeader(), 50, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
await AddComposedItem(client, fast, anchor, 2, 50, ct);
|
|
var fastStream = await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
var timestamp = origin.Clock.GetUtcNow().AddMinutes(-1);
|
|
fastStream.EmitValue(2, 42, timestamp, TimeSpan.Zero);
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
fastStream.EmitValue(3, 42, timestamp.AddSeconds(1), TimeSpan.Zero);
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), false, [fast], ct);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), true, [slow], ct);
|
|
var value = await PublishComposedValue(client, 1, ct);
|
|
StatusCode.IsGood(value.StatusCode).ShouldBeFalse("an omitted source timestamp cannot identify either retained acquisition");
|
|
});
|
|
}
|
|
|
|
[Fact(Timeout = 90000), Trait("Phase", "P11Wire")]
|
|
public async Task Sdk_uses_unique_source_timestamp_to_select_coalesced_value_evidence()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
LocalDataFixture? peer = null;
|
|
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
|
|
using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions);
|
|
peer = origin;
|
|
subscriptions.SampleClock = origin.Clock;
|
|
var published = Channel.CreateUnbounded<ProjectedValueUpdate>();
|
|
await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) =>
|
|
{
|
|
var client = (SessionClient)session;
|
|
await BrowseAll(client, anchor, 1);
|
|
var slow = (await client.CreateSubscriptionAsync(new RequestHeader(), 200, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
await AddComposedItem(client, slow, anchor, 1, 200, ct);
|
|
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), false, [slow], ct);
|
|
|
|
var fast = (await client.CreateSubscriptionAsync(new RequestHeader(), 50, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
await AddComposedItem(client, fast, anchor, 2, 50, ct);
|
|
var fastStream = await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
|
|
var earlier = origin.Clock.GetUtcNow().AddMinutes(-1);
|
|
var later = earlier.AddSeconds(1);
|
|
fastStream.EmitValue(2, 42, earlier, TimeSpan.FromSeconds(28));
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
origin.Clock.Advance(TimeSpan.FromSeconds(3));
|
|
fastStream.EmitValue(3, 42, later, TimeSpan.FromSeconds(28));
|
|
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
var selected = await NextPublished(published.Reader,
|
|
x => !x.IsCompleteSnapshot && Equals(x.Snapshot.Value, 42) && x.Snapshot.SourceTimestampUtc == later.UtcDateTime, ct);
|
|
|
|
await client.SetPublishingModeAsync(new RequestHeader(), false, [fast], ct);
|
|
await client.SetPublishingModeAsync(new RequestHeader(), true, [slow], ct);
|
|
var delivered = await PublishComposedNotification(client, 1, ct);
|
|
delivered.Value.StatusCode.ShouldBe(StatusCodes.Good);
|
|
delivered.Value.WrappedValue.AsBoxedObject(Variant.BoxingBehavior.Legacy).ShouldBe(42);
|
|
delivered.Value.SourceTimestamp.ShouldBe((DateTimeUtc)later.UtcDateTime);
|
|
selected.SourceProof.IsValid.ShouldBeTrue();
|
|
|
|
var replay = await client.RepublishAsync(new RequestHeader(), slow, delivered.Sequence, ct);
|
|
StatusCode.IsGood(ComposedChanges(replay.NotificationMessage).Single(x => x.ClientHandle == 1).Value.StatusCode).ShouldBeTrue();
|
|
origin.Clock.Advance(TimeSpan.FromSeconds(3));
|
|
var expiredReplay = await client.RepublishAsync(new RequestHeader(), slow, delivered.Sequence, ct);
|
|
StatusCode.IsGood(ComposedChanges(expiredReplay.NotificationMessage).Single(x => x.ClientHandle == 1).Value.StatusCode)
|
|
.ShouldBeFalse("the selected acquisition's original 28-second age must not be refreshed by delivery or Republish");
|
|
}, onPublished: update => published.Writer.TryWrite(update));
|
|
}
|
|
}
|