107 lines
4.6 KiB
C#
107 lines
4.6 KiB
C#
using System.Runtime.CompilerServices;
|
|
using System.Threading.Channels;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Runtime;
|
|
|
|
/// <summary>
|
|
/// #991 G-5 — every data-change event used to allocate, in the subscriber lookup alone, a result list, a LINQ
|
|
/// <c>Distinct</c> iterator with its own hash set, and a boxed immutable-set enumerator, plus a new subscription
|
|
/// handle per subscriber per event. The reverse map now stores each item handle's subscribers as a pre-built
|
|
/// array (lookup is allocation-free) and the pump reuses one handle object per subscription.
|
|
/// </summary>
|
|
public sealed class FanOutAllocationTests
|
|
{
|
|
/// <summary>Resolving a handle's subscribers allocates nothing once the map is built.</summary>
|
|
[Fact]
|
|
public void ResolveSubscribers_does_not_allocate()
|
|
{
|
|
var registry = new SubscriptionRegistry();
|
|
registry.Register(1, [new TagBinding("Tank.Level", 7)]);
|
|
registry.Register(2, [new TagBinding("Tank.Level", 7), new TagBinding("Tank.Temp", 8)]);
|
|
registry.ResolveSubscribers(7).Count.ShouldBe(2); // warm-up (JIT)
|
|
|
|
var before = GC.GetAllocatedBytesForCurrentThread();
|
|
var total = 0;
|
|
for (var i = 0; i < 1_000; i++) total += registry.ResolveSubscribers(7).Count;
|
|
var allocated = GC.GetAllocatedBytesForCurrentThread() - before;
|
|
|
|
total.ShouldBe(2_000);
|
|
allocated.ShouldBe(0L, "the per-event subscriber lookup must not allocate");
|
|
}
|
|
|
|
/// <summary>Semantics kept: one entry per subscription, the subscription's own reference, removals honoured.</summary>
|
|
[Fact]
|
|
public void ResolveSubscribers_keeps_its_semantics()
|
|
{
|
|
var registry = new SubscriptionRegistry();
|
|
registry.Register(1, [new TagBinding("A.X", 5), new TagBinding("A.X", 5)]); // a duplicate binding stays one entry
|
|
registry.Register(2, [new TagBinding("B.X", 5)]);
|
|
|
|
registry.ResolveSubscribers(5).OrderBy(s => s.SubscriptionId).ShouldBe([(1L, "A.X"), (2L, "B.X")]);
|
|
|
|
registry.Rebind(1, [new TagBinding("A.X", 6)]);
|
|
registry.ResolveSubscribers(5).ShouldBe([(2L, "B.X")]);
|
|
registry.ResolveSubscribers(6).ShouldBe([(1L, "A.X")]);
|
|
|
|
registry.Remove(2);
|
|
registry.ResolveSubscribers(5).ShouldBeEmpty();
|
|
registry.TrackedItemHandleCount.ShouldBe(1);
|
|
}
|
|
|
|
/// <summary>The pump hands every event of one subscription the same handle instance.</summary>
|
|
[Fact]
|
|
public async Task The_pump_reuses_one_handle_per_subscription()
|
|
{
|
|
var subscriber = new ChannelSubscriber();
|
|
var registry = new SubscriptionRegistry();
|
|
registry.Register(1, [new TagBinding("Tank.Level", 7)]);
|
|
var handles = new List<ISubscriptionHandle>();
|
|
var twoSeen = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
await using var pump = new EventPump(subscriber, registry, channelCapacity: 8, clientName: "G5");
|
|
pump.OnDataChange += (_, e) =>
|
|
{
|
|
handles.Add(e.SubscriptionHandle);
|
|
if (handles.Count == 2) twoSeen.TrySetResult();
|
|
};
|
|
pump.Start();
|
|
|
|
await subscriber.EmitAsync(7, 1.0);
|
|
await subscriber.EmitAsync(7, 2.0);
|
|
await TestWait.ForAsync(twoSeen.Task, "both events must be dispatched");
|
|
|
|
handles[1].ShouldBeSameAs(handles[0]);
|
|
((GalaxySubscriptionHandle)handles[0]).SubscriptionId.ShouldBe(1);
|
|
}
|
|
|
|
private sealed class ChannelSubscriber : IGalaxySubscriber
|
|
{
|
|
private readonly Channel<MxEvent> _events = Channel.CreateUnbounded<MxEvent>();
|
|
|
|
public Task<IReadOnlyList<SubscribeResult>> SubscribeBulkAsync(
|
|
IReadOnlyList<string> fullReferences, int bufferedUpdateIntervalMs, CancellationToken cancellationToken)
|
|
=> Task.FromResult<IReadOnlyList<SubscribeResult>>([]);
|
|
|
|
public Task UnsubscribeBulkAsync(IReadOnlyList<int> itemHandles, CancellationToken cancellationToken) => Task.CompletedTask;
|
|
|
|
public async IAsyncEnumerable<MxEvent> StreamEventsAsync([EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
await foreach (var ev in _events.Reader.ReadAllAsync(cancellationToken).ConfigureAwait(false))
|
|
yield return ev;
|
|
}
|
|
|
|
public ValueTask EmitAsync(int handle, double value) => _events.Writer.WriteAsync(new MxEvent
|
|
{
|
|
Family = MxEventFamily.OnDataChange,
|
|
ItemHandle = handle,
|
|
Value = new MxValue { DoubleValue = value },
|
|
Quality = 192,
|
|
});
|
|
}
|
|
}
|