Repository navigation
Expand file tree
/
Copy pathMachineToolNotificationClientTests.cs
More file actions
230 lines (219 loc) · 13.2 KB
/
Copy pathMachineToolNotificationClientTests.cs
File metadata and controls
230 lines (219 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL
// SPDX-License-Identifier: MIT
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Client;
using Opc.Ua.Client.Subscriptions;
using Opc.Ua.Client.TestFramework;
using Opc.Ua.MachineTool.Client;
using Opc.Ua.MachineTool.Server;
using Opc.Ua.Tests;
namespace Opc.Ua.MachineTool.Tests
{
/// <summary>
/// Verifies bounded passive event workflows using actual Publish and ConditionRefresh services.
/// </summary>
[TestFixture]
[NonParallelizable]
[Category("MachineTool")]
[Category("Integration")]
public sealed class MachineToolNotificationClientTests
{
/// <summary>
/// A retained refresh completes at RefreshEnd, preserves standard fields and never implicitly acknowledges.
/// </summary>
[Test]
public async Task RetainedRefreshUsesNativeConditionDecoderAndReleasesSubscriptionAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
var sample = new MachineToolSampleInfo(
new DateTime(2026, 6, 8, 10, 11, 12, DateTimeKind.Utc), StatusCodes.UncertainLastUsableValue);
var provider = new MachineToolNotificationTests.NotificationProvider(new MachineToolNotificationSnapshot
{
Alerts =
[
new MachineToolAlert
{
Key = "Coolant", ErrorCode = "E91", Message = new LocalizedText("Coolant empty"),
Severity = 800, IsActive = true, Sample = sample
}
]
});
await using MachineToolRuntime<MachineToolState> runtime =
await MachineToolNotificationTests.BuildAsync(fixture, provider).ConfigureAwait(false);
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
await using var transport = new ClientFixture(telemetry);
transport.UseSubscriptionEngineFactory(DefaultSubscriptionEngineFactory.Instance);
await transport.LoadClientConfigurationAsync(fixture.Root, "NotificationObserver").ConfigureAwait(false);
using ISession session = await transport.ConnectAsync(fixture.Endpoint, SecurityPolicies.None)
.ConfigureAwait(false);
await using MachineToolClient client = session.CreateMachineToolClient(telemetry);
Assert.That(session.TryGetSubscriptionManager(out ISubscriptionManager? subscriptions), Is.True);
int before = subscriptions!.Count;
MachineToolEventObservation result = await client.ObserveConditionsAsync(runtime.NodeId,
new MachineToolObservationOptions { Window = TimeSpan.FromSeconds(10), MaximumNotifications = 1 })
.ConfigureAwait(false);
Assert.That(result.RefreshCompleted, Is.True);
Assert.That(result.Events.Count, Is.EqualTo(1));
Assert.That(result.Events[0].Event, Is.TypeOf<AlertTypeRecord>());
var alert = (AlertTypeRecord)result.Events[0].Event;
Assert.That(alert.ErrorCode, Is.EqualTo("E91"));
Assert.That(alert.Time, Is.EqualTo(sample.SourceTimestamp));
Assert.That(runtime.Notifications!.TryGetAlert("Coolant", out AlertState? state), Is.True);
Assert.That(alert.ConditionId, Is.EqualTo(state!.NodeId));
Assert.That(state.AckedState!.Id!.Value, Is.False);
Assert.That(subscriptions.Count, Is.EqualTo(before));
await session.CloseAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Live messages use concrete types, propagate once, preserve fields and report server overflow.
/// </summary>
[Test]
public async Task LiveEventsSupportGeneratedVendorDecodersAndExplicitOverflowAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
var provider = new MachineToolNotificationTests.NotificationProvider(new MachineToolNotificationSnapshot());
await using MachineToolRuntime<MachineToolState> runtime =
await MachineToolNotificationTests.BuildAsync(fixture, provider).ConfigureAwait(false);
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
await using var transport = new ClientFixture(telemetry);
transport.UseSubscriptionEngineFactory(DefaultSubscriptionEngineFactory.Instance);
await transport.LoadClientConfigurationAsync(fixture.Root, "LiveEvents").ConfigureAwait(false);
using ISession session = await transport.ConnectAsync(fixture.Endpoint, SecurityPolicies.None)
.ConfigureAwait(false);
await using MachineToolClient client = session.CreateMachineToolClient(telemetry);
var options = new MachineToolObservationOptions
{
Window = TimeSpan.FromSeconds(10),
MaximumNotifications = 1
};
Task<MachineToolEventObservation> first = client.ObserveEventsAsync(runtime.NodeId, options).AsTask();
await WaitForMonitoringAsync(runtime.State).ConfigureAwait(false);
MachineToolMessage message = MachineToolNotificationValidationTests.Message(1);
provider.Snapshot = new MachineToolNotificationSnapshot { Revision = 1, Messages = [message] };
await runtime.RefreshNotificationsAsync().ConfigureAwait(false);
MachineToolEventObservation initial = await first.ConfigureAwait(false);
Assert.That(initial.Events.Count, Is.EqualTo(1));
Assert.That(initial.Events[0].Event, Is.TypeOf<NotificationEventTypeRecord>());
var record = (NotificationEventTypeRecord)initial.Events[0].Event;
Assert.That(record.Identifier, Is.EqualTo("M1"));
Assert.That(record.Time, Is.EqualTo(message.Time));
Assert.That(record.EventType.NamespaceIndex, Is.EqualTo(fixture.Context.InstanceNamespaceIndex));
Assert.That(initial.DroppedNotifications, Is.Zero);
QualifiedName[][] generated = NotificationEventTypeRecord.Decoder.GetStandardFields(session.NamespaceUris);
ArrayOf<ArrayOf<QualifiedName>> fields =
Array.ConvertAll(generated, static path => path.ToArrayOf()).ToArrayOf();
int decoded = 0;
client.WithEventDecoder(new ExpandedNodeId(record.EventType), fields, values =>
{
Interlocked.Increment(ref decoded);
return NotificationEventTypeRecord.Decoder.Decode(values.ToArray()!);
});
Task<MachineToolEventObservation> next = client.ObserveEventsAsync(runtime.NodeId, options).AsTask();
await WaitForMonitoringAsync(runtime.State).ConfigureAwait(false);
Assert.Throws<ServiceResultException>(() => client.WithEventDecoder(
new ExpandedNodeId(new NodeId(Guid.NewGuid(), fixture.Context.InstanceNamespaceIndex)), fields,
values => NotificationEventTypeRecord.Decoder.Decode(values.ToArray()!)));
var overflow = new EventQueueOverflowEventState(null);
overflow.Initialize(
fixture.Context.Context, runtime.State, EventSeverity.Low, new LocalizedText("Overflow"));
await runtime.State.ReportEventAsync(fixture.Context.Context, overflow).ConfigureAwait(false);
provider.Snapshot = new MachineToolNotificationSnapshot
{
Revision = 2,
Messages = [message with { Sequence = 2, Identifier = "M2" }]
};
await runtime.RefreshNotificationsAsync().ConfigureAwait(false);
MachineToolEventObservation observed = await next.ConfigureAwait(false);
Assert.That(decoded, Is.EqualTo(1));
Assert.That(((NotificationEventTypeRecord)observed.Events[0].Event).Identifier, Is.EqualTo("M2"));
Assert.That(observed.DroppedNotifications, Is.EqualTo(1));
Assert.That(observed.RefreshCompleted, Is.False);
Assert.That(observed.LimitReached, Is.True);
await session.CloseAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// A retained refresh reports truncation instead of claiming a complete one-condition inventory.
/// </summary>
[Test]
public async Task RetainedConditionCountLimitDoesNotClaimCompletenessAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolAlert alert = MachineToolNotificationValidationTests.Alert();
var provider = new MachineToolNotificationTests.NotificationProvider(
new MachineToolNotificationSnapshot { Alerts = [alert, alert with { Key = "Second" }] });
await using MachineToolRuntime<MachineToolState> runtime =
await MachineToolNotificationTests.BuildAsync(fixture, provider).ConfigureAwait(false);
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
await using var transport = new ClientFixture(telemetry);
transport.UseSubscriptionEngineFactory(DefaultSubscriptionEngineFactory.Instance);
await transport.LoadClientConfigurationAsync(fixture.Root, "TruncatedConditions").ConfigureAwait(false);
using ISession session = await transport.ConnectAsync(fixture.Endpoint, SecurityPolicies.None)
.ConfigureAwait(false);
await using MachineToolClient client = session.CreateMachineToolClient(telemetry);
MachineToolEventObservation result = await client.ObserveConditionsAsync(runtime.NodeId,
new MachineToolObservationOptions { Window = TimeSpan.FromSeconds(10), MaximumNotifications = 1 })
.ConfigureAwait(false);
Assert.That(result.Events.Count, Is.EqualTo(1));
Assert.That(result.RefreshCompleted, Is.False);
Assert.That(result.LimitReached, Is.True);
Assert.That(result.DroppedNotifications, Is.GreaterThanOrEqualTo(1));
await session.CloseAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Caller cancellation and facade disposal both join subscription deletion and preserve the borrowed session.
/// </summary>
[TestCase(false)]
[TestCase(true)]
public async Task EventObservationCancellationReleasesOnlyItsOwnedSubscriptionAsync(bool disposeFacade)
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
var provider = new MachineToolNotificationTests.NotificationProvider(new MachineToolNotificationSnapshot());
await using MachineToolRuntime<MachineToolState> runtime =
await MachineToolNotificationTests.BuildAsync(fixture, provider).ConfigureAwait(false);
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
await using var transport = new ClientFixture(telemetry);
transport.UseSubscriptionEngineFactory(DefaultSubscriptionEngineFactory.Instance);
await transport.LoadClientConfigurationAsync(fixture.Root, "CancelledEvents").ConfigureAwait(false);
using ISession session = await transport.ConnectAsync(fixture.Endpoint, SecurityPolicies.None)
.ConfigureAwait(false);
await using MachineToolClient client = session.CreateMachineToolClient(telemetry);
Assert.That(session.TryGetSubscriptionManager(out ISubscriptionManager? subscriptions), Is.True);
int before = subscriptions!.Count;
using var cancellation = new CancellationTokenSource();
Task<MachineToolEventObservation> pending = client.ObserveEventsAsync(runtime.NodeId,
new MachineToolObservationOptions { Window = TimeSpan.FromSeconds(30) }, cancellation.Token).AsTask();
await WaitForMonitoringAsync(runtime.State).ConfigureAwait(false);
if (disposeFacade)
{
await client.DisposeAsync().ConfigureAwait(false);
}
else
{
await cancellation.CancelAsync().ConfigureAwait(false);
}
await Assert.ThatAsync(() => pending,
Throws.InstanceOf<OperationCanceledException>()).ConfigureAwait(false);
Assert.That(subscriptions.Count, Is.EqualTo(before));
Assert.That(session.Connected, Is.True);
await session.CloseAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Waits on the real server's public event-monitoring state, not a guessed subscription setup delay.
/// </summary>
private static async Task WaitForMonitoringAsync(NodeState node)
{
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
while (!node.AreEventsMonitored)
{
await Task.Delay(20, timeout.Token).ConfigureAwait(false);
}
}
}
}