Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataSampleOwnershipTests.cs
T
2026-11-23 09:55:03 -05:00

92 lines
4.5 KiB
C#

using Akka.Actor;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
[Trait("Feature", "GrpcData")]
[Trait("Phase", "P04")]
[Trait("Category", "Unit")]
public sealed class GrpcDataSampleOwnershipTests : RuntimeActorTestBase
{
[Fact]
public async Task Single_callback_copies_bytes_before_the_producer_reuses_them()
{
var driver = new CallbackDriver();
var parent = CreateTestProbe();
parent.IgnoreMessages(m => m is DriverInstanceActor.ConnectivityChanged);
var actor = parent.ChildActorOf(DriverInstanceActor.Props(driver), TestContext.Current.CancellationToken);
await Subscribe(actor, driver);
var bytes = new byte[] { 1, 2, 3 };
driver.Fire("source", new DataValueSnapshot(bytes, OpcUaStatus.Good, null, null));
Array.Fill(bytes, (byte)99);
var item = parent.ExpectMsg<DriverInstanceActor.AttributeValueBatchPublished>(
PresenceBudget, cancellationToken: TestContext.Current.CancellationToken).Items.Single();
item.Value.ShouldBeOfType<byte[]>().ShouldBe(new byte[] { 1, 2, 3 });
}
[Fact]
public async Task Batch_callback_copies_the_collection_and_each_mutable_array()
{
var driver = new BatchCallbackDriver();
var parent = CreateTestProbe();
parent.IgnoreMessages(m => m is DriverInstanceActor.ConnectivityChanged);
var actor = parent.ChildActorOf(DriverInstanceActor.Props(driver), TestContext.Current.CancellationToken);
await Subscribe(actor, driver);
var numbers = new[] { 10, 20 };
var bytes = new byte[] { 4, 5 };
var items = new[]
{
new DataChangeItem("numbers", new DataValueSnapshot(numbers, OpcUaStatus.Good, null, null)),
new DataChangeItem("bytes", new DataValueSnapshot(bytes, OpcUaStatus.Good, null, null)),
};
driver.Fire(items);
numbers[0] = 999;
bytes[0] = 99;
items[1] = new DataChangeItem("replacement", new DataValueSnapshot(0, OpcUaStatus.Good, null, null));
var batch = parent.ExpectMsg<DriverInstanceActor.AttributeValueBatchPublished>(
PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
batch.Items.Count.ShouldBe(2);
batch.Items[0].Value.ShouldBeOfType<int[]>().ShouldBe(new[] { 10, 20 });
batch.Items[1].FullReference.ShouldBe("bytes");
batch.Items[1].Value.ShouldBeOfType<byte[]>().ShouldBe(new byte[] { 4, 5 });
}
private async Task Subscribe(IActorRef actor, StubDriver driver)
{
actor.Tell(new DriverInstanceActor.InitializeRequested("{}"));
AwaitCondition(() => driver.InitializeCount > 0, PresenceBudget, TestContext.Current.CancellationToken);
await actor.Ask<DriverInstanceActor.SubscriptionEstablished>(
new DriverInstanceActor.Subscribe(new[] { "source", "numbers", "bytes" }, TimeSpan.FromMilliseconds(100)),
PresenceBudget, TestContext.Current.CancellationToken);
}
private sealed class CallbackDriver : StubDriver, ISubscribable
{
private readonly StubHandle _handle = new();
public event EventHandler<DataChangeEventArgs>? OnDataChange;
public Task<ISubscriptionHandle> SubscribeAsync(IReadOnlyList<string> refs, TimeSpan interval, CancellationToken ct) => Task.FromResult<ISubscriptionHandle>(_handle);
public Task UnsubscribeAsync(ISubscriptionHandle handle, CancellationToken ct) => Task.CompletedTask;
public void Fire(string reference, DataValueSnapshot snapshot) => OnDataChange?.Invoke(this, new DataChangeEventArgs(_handle, reference, snapshot));
}
private sealed class BatchCallbackDriver : StubDriver, IBatchSubscribable
{
private readonly StubHandle _handle = new();
public event EventHandler<DataChangeEventArgs>? OnDataChange { add { } remove { } }
public event EventHandler<DataChangeBatchEventArgs>? OnDataChangeBatch;
public Task<ISubscriptionHandle> SubscribeAsync(IReadOnlyList<string> refs, TimeSpan interval, CancellationToken ct) => Task.FromResult<ISubscriptionHandle>(_handle);
public Task UnsubscribeAsync(ISubscriptionHandle handle, CancellationToken ct) => Task.CompletedTask;
public void Fire(IReadOnlyList<DataChangeItem> items) => OnDataChangeBatch?.Invoke(this, new DataChangeBatchEventArgs(_handle, items));
}
}