Files
UA-.NETStandard/tests/Opc.Ua.Server.Tests/SubscriptionStateMachineRegressionTests.cs
T
romanett efad0ddeff Server subscriptions: fix spec defects in Subscription/MonitoredItem handling found by audit (#4629)
# Description

Fixes defects found in a spec-grounded audit of the server
**Subscription / MonitoredItem** implementation
(`src/Opc.Ua.Server/Subscription/**`, `NodeManager/MonitoredItem/**`,
`EventManager`, monitored-item service dispatch and filter validation).
Each behaviour change was checked against OPC 10000-4 (§5.13, §5.14,
§7.21, §7.22), Part 5 (subscription diagnostics) and Part 9
(ConditionRefresh). Every fix has a regression test.

The audit found 56 defects (1 Critical, 3 High, 20 Medium, 32 Low). A
follow-up max-effort code review of the fix branch found 27 more issues,
mostly introduced by the first round of fixes. All but one are fixed
here.

### Most important fixes
- **Critical — remote hang:** an `AggregateFilter` with
`processingInterval = NaN` was not revised, and `AggregateCalculator`
looped forever while holding the node-manager lock, so one
CreateMonitoredItems request blocked the node manager. Non-finite, ≤0
and sub-tick intervals are now revised (Part 4 §5.13.2.1), and the
calculator rejects them as a backstop.
- **Monitored-item limits:** new
`ServerConfiguration.MaxMonitoredItemCount` /
`MaxMonitoredItemsPerSubscription`; Bad_TooManyMonitoredItems is
returned when a limit is hit, and ServerCapabilities now report the
limits. Both default to `0` (no limit), so existing behaviour is
unchanged.
- **Sampling groups:** a client could get one dedicated thread per
distinct sampling interval above 1 h; intervals above the last rate
group are now rounded, and the loop is async. An item could also end up
in two sampling groups and keep being sampled after it was deleted.
- **Subscription state machine** (Part 4 §5.14.1.2):
- the lifetime counter resets while a Publish is queued and counts
otherwise;
  - the lifetime and keep-alive stay within `MaxSubscriptionLifetime`;
  - LatePublishRequestCount is counted once per entry into LATE;
- messages held while publishing is disabled are no longer sent, and are
not offered by Transfer/Republish either;
- ConditionRefresh returns Bad_RefreshInProgress for any refresh already
queued or running on the subscription.
- **Triggering** (Part 4 §5.13.1.6):
- linked items are triggered by an item still draining a backlog, by the
first value after re-enable, and by an item created Disabled;
  - links are persisted for durable subscriptions;
- Disabled/Sampling items follow the publish rules in the shared
pipeline, so custom `IMonitoredItem` implementations follow them too.
- **Queues** (Part 4 §5.13.1.5, §7.21):
  - no duplicate value after a queue-size increase;
- EventQueueOverflow events are placed where the events were lost,
including on queue shrink;
  - the duplicate-event check scans the newest entries;
- EventQueueOverflowCount and MonitoringQueueOverflowCount are
maintained correctly;
- changing select clauses rebuilds queued events in the new layout, and
is safe on durable queues and on failure.
- **Filters:**
- per-clause EventFilterResult instead of rejecting the whole item (Part
4 §7.22.3);
- corrected status codes: Bad_DeadbandFilterInvalid,
Bad_MonitoredItemFilterInvalid, Bad_EventFilterInvalid,
Bad_MonitoringModeInvalid, Bad_TimestampsToReturnInvalid;
- aggregate filter in subscriptions: processing interval at least 2× the
revised sampling interval, revised StartTime on the requested grid,
ServerTimestamp = end of interval (Part 4 §7.22.4).
- **Lifecycle and races:**
- expired subscriptions are always cleaned up, even when the session
closes during cleanup;
  - Create/Transfer racing CloseSession returns Bad_SessionClosed;
  - session diagnostics counts no longer drift;
  - a large but valid Publish TimeoutHint no longer fails the request;
- a ready subscription is served by queued Publish requests instead of
going Late;
  - restored persisted state is validated.
- **Access:** a value change notification is checked against each
subscriber's UserAccessLevel, not the writer's.
- **Redundancy / durable store:**
  - the first-unsent sequence number is mirrored;
  - sequence numbers are no longer reused after a second failover;
- triggering links are stored (definition format 6, written only when
links exist, so older replicas can still read link-less subscriptions).

### Behaviour changes reviewers should know
- With the default config, publishing intervals above
`MaxSubscriptionLifetime / 3` (1,200,000 ms) are revised down, so
lifetime ≥ 3×keep-alive fits the configured maximum.
- The revised keep-alive is capped at a third of the max lifetime.
- Idle subscriptions expire after exactly MaxLifetimeCount cycles
without a queued Publish.
- A partly invalid EventFilter select list is accepted with per-clause
errors. The public `EventFilter.Validate` keeps Status Good if at least
one clause is valid; callers that cannot return per-clause results must
also check the new `EventFilter.Result.HasSelectClauseErrors`. There is
a migration note in `docs/migrate/2.0.x/sessions-subscriptions.md`.
- Aggregate processing intervals may be revised higher (≥ 2× sampling
interval); aggregate ServerTimestamp is now the interval end.
- Sampling intervals above 3,600,000 ms are rounded to multiples of
900,000 ms (default rate groups).

### Public API
Additive only:
- `ServerConfiguration.MaxMonitoredItemCount` and
`MaxMonitoredItemsPerSubscription`;
- `EventFilter.Result.HasSelectClauseErrors`;
- `IStoredSubscriptionTriggering`,
`ISubscriptionRetransmissionSendStateStore`,
`SubscriptionRetransmissionState.FirstUnsentSequenceNumber` (optional
store capabilities, detected with `as`);
- new `EventQueueHandler` constructor overloads.

### Known open items (not in this PR)
- **Part 4 §5.13.2.1:** CreateMonitoredItems should create an item even
when the user lacks Read permission and report the error in Publish.
This is not done: the create-time RolePermissions check is the only role
check on the initial value, so following the spec would leak values
until every node manager re-checks per sample.
- **Sampling loop:** it runs on thread-pool threads; node managers with
blocking synchronous reads should not use sampling groups heavily. The
alternative, a dedicated thread per group, was the thread-exhaustion
issue fixed here.
- **Restarts:** a pending QueueSize-1 value and a pending overflow flag
are not kept across a restart; that needs persistence interface
extensions.
- **Limit defaults:** decide whether the new limits should get finite
defaults (currently 0 = unlimited).

## Related Issues

- No tracking issue; the findings come from an internal audit of the
server subscription code.

## Checklist

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [ ] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

### Local verification (net10.0, all green)
| Suite | Passed | Failed |
|---|---|---|
| Opc.Ua.Server.Tests | 7963 | 0 |
| Opc.Ua.Subscriptions.Tests | 643 | 0 |
| Opc.Ua.Subscriptions.Classic.Tests | 57 | 0 |
| Opc.Ua.Subscriptions.Durable.Tests | 36 | 0 |
| Opc.Ua.Redundancy.Server.Tests | 847 | 0 |
| Opc.Ua.Core.Tests (filter tests) | 283 | 0 |

net48 build of `Opc.Ua.Server.Tests`: 0 errors. net48 tests and the full
UA.slnx were not run locally.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>
2026-10-03 05:43:16 +02:00

860 lines
41 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Time.Testing;
using Moq;
using NUnit.Framework;
using Opc.Ua.Tests;
namespace Opc.Ua.Server.Tests
{
/// <summary>
/// Regression tests for the subscription publishing state machine, triggering,
/// diagnostics and condition refresh behaviour (OPC 10000-4 §5.13/§5.14,
/// OPC 10000-5 §12.15, OPC 10000-9 §5.5.7).
/// </summary>
[TestFixture]
[Category("Subscription")]
[Parallelizable]
public class SubscriptionStateMachineRegressionTests
{
private Mock<IServerInternal> m_serverMock;
private Mock<ISession> m_sessionMock;
private Mock<IDiagnosticsNodeManager> m_diagnosticsNodeManagerMock;
private Mock<IMasterNodeManager> m_nodeManagerMock;
private Mock<IMonitoredItemQueueFactory> m_queueFactoryMock;
private ITelemetryContext m_telemetry;
[SetUp]
public void SetUp()
{
m_telemetry = NUnitTelemetryContext.Create();
m_serverMock = new Mock<IServerInternal>();
m_sessionMock = new Mock<ISession>();
m_diagnosticsNodeManagerMock = new Mock<IDiagnosticsNodeManager>();
m_nodeManagerMock = new Mock<IMasterNodeManager>();
m_queueFactoryMock = new Mock<IMonitoredItemQueueFactory>();
m_serverMock.Setup(s => s.Telemetry).Returns(m_telemetry);
m_serverMock.Setup(s => s.DiagnosticsNodeManager).Returns(m_diagnosticsNodeManagerMock.Object);
m_serverMock.Setup(s => s.NodeManager).Returns(m_nodeManagerMock.Object);
m_serverMock.Setup(s => s.MonitoredItemQueueFactory).Returns(m_queueFactoryMock.Object);
var namespaceUris = new NamespaceTable();
m_serverMock.Setup(s => s.NamespaceUris).Returns(namespaceUris);
m_serverMock.Setup(s => s.ServerUris).Returns(new StringTable());
m_serverMock.Setup(s => s.TypeTree).Returns(new TypeTable(namespaceUris));
m_serverMock.Setup(s => s.Factory).Returns(new Mock<IEncodeableFactory>().Object);
m_serverMock.Setup(s => s.DefaultSystemContext).Returns(new ServerSystemContext(m_serverMock.Object));
var identity = new UserIdentity(new AnonymousIdentityToken());
m_sessionMock.Setup(s => s.Id).Returns(new NodeId(Guid.NewGuid()));
m_sessionMock.Setup(s => s.Identity).Returns(identity);
m_sessionMock.Setup(s => s.IdentityToken).Returns(identity.TokenHandler);
m_sessionMock.Setup(s => s.ClientApplicationUri)
.Returns("urn:localhost:opcfoundation.org:SubscriptionStateMachineRegressionTests");
m_diagnosticsNodeManagerMock
.Setup(d => d.CreateSubscriptionDiagnosticsAsync(
It.IsAny<ServerSystemContext>(),
It.IsAny<SubscriptionDiagnosticsDataType>(),
It.IsAny<NodeValueSimpleEventHandler>()))
.ReturnsAsync(new NodeId(1));
}
/// <summary>
/// M1-1: OPC 10000-4 §5.14.1.4 StartPublishingTimer() advances the lifetime counter
/// on every expiry, so an idle subscription expires after MaxLifetimeCount cycles
/// even while it is in the KEEPALIVE state.
/// </summary>
[Test]
public void IdleSubscriptionExpiresAfterMaxLifetimeCountCycles()
{
var clock = new FakeTimeProvider();
using Subscription subscription = CreateSubscription(
clock,
maxLifetimeCount: 15,
maxKeepAliveCount: 5);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
// the first cycle sends the initial keep-alive.
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(subscription.PublishTimerExpired(), Is.EqualTo(PublishingState.NotificationsAvailable));
Assert.That(subscription.Publish(context, out _, out _), Is.Not.Null);
// the client then stops sending Publish requests.
for (int cycle = 1; cycle < 15; cycle++)
{
clock.Advance(TimeSpan.FromMilliseconds(100));
Assert.That(subscription.PublishTimerExpired(), Is.Not.EqualTo(PublishingState.Expired),
$"cycle {cycle}");
}
Assert.That(subscription.Diagnostics.CurrentLifetimeCount, Is.EqualTo(14u));
clock.Advance(TimeSpan.FromMilliseconds(100));
Assert.That(subscription.PublishTimerExpired(), Is.EqualTo(PublishingState.Expired));
}
/// <summary>
/// M1-1: a client that keeps answering keep-alives never lets the lifetime expire.
/// </summary>
[Test]
public void IdleSubscriptionServedByKeepAlivesDoesNotExpire()
{
var clock = new FakeTimeProvider();
using Subscription subscription = CreateSubscription(
clock,
maxLifetimeCount: 15,
maxKeepAliveCount: 5);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
for (int cycle = 0; cycle < 100; cycle++)
{
clock.Advance(TimeSpan.FromMilliseconds(100));
PublishingState state = subscription.PublishTimerExpired();
Assert.That(state, Is.Not.EqualTo(PublishingState.Expired), $"cycle {cycle}");
if (state == PublishingState.NotificationsAvailable)
{
Assert.That(subscription.Publish(context, out _, out _), Is.Not.Null);
}
}
}
/// <summary>
/// M1-6: latePublishRequestCount counts the entries into the LATE state, not the
/// publishing cycles spent in it (OPC 10000-5 §12.15).
/// </summary>
[Test]
public void LatePublishRequestCountIncrementsOncePerLateEpisode()
{
var clock = new FakeTimeProvider();
using Subscription subscription = CreateSubscription(
clock,
maxLifetimeCount: 1000,
maxKeepAliveCount: 2);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
// initial keep-alive due but not served -> LATE for 20 cycles.
for (int cycle = 0; cycle < 20; cycle++)
{
clock.Advance(TimeSpan.FromMilliseconds(100));
subscription.PublishTimerExpired();
}
Assert.That(subscription.Diagnostics.LatePublishRequestCount, Is.EqualTo(1u));
// served, then late again.
Assert.That(subscription.Publish(context, out _, out _), Is.Not.Null);
for (int cycle = 0; cycle < 20; cycle++)
{
clock.Advance(TimeSpan.FromMilliseconds(100));
subscription.PublishTimerExpired();
}
Assert.That(subscription.Diagnostics.LatePublishRequestCount, Is.EqualTo(2u));
}
/// <summary>
/// M1-2: a triggering item draining a backlog over several Publish calls triggers its
/// linked items for every notification it reports (OPC 10000-4 §5.13.1.6).
/// </summary>
[Test]
public void TriggeringItemDrainingBacklogKeepsTriggeringLinkedItems()
{
var clock = new FakeTimeProvider();
using var subscription = new Subscription(
m_serverMock.Object,
m_sessionMock.Object,
subscriptionId: 1,
publishingInterval: 100,
maxLifetimeCount: 1000,
maxKeepAliveCount: 10,
maxNotificationsPerPublish: 1,
priority: 0,
publishingEnabled: true,
maxMessageCount: 1,
timeProvider: clock);
ResetKeepAlive(subscription);
Mock<IDataChangeMonitoredItem> triggering = CreateDataChangeItem(1, 3);
triggering.SetupProperty(i => i.IsReadyToTrigger, true);
(Mock<IDataChangeMonitoredItem> linked, Mock<ITriggeredMonitoredItem> linkedTrigger) =
CreateTriggeredItem(2);
AddMonitoredItem(subscription, triggering.Object);
AddMonitoredItem(subscription, linked.Object);
AddTriggerLink(subscription, 1, linkedTrigger.Object);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(subscription.PublishTimerExpired(), Is.EqualTo(PublishingState.NotificationsAvailable));
linkedTrigger.Verify(i => i.SetTriggered(), Times.Once);
Assert.That(subscription.Publish(context, out _, out bool moreNotifications), Is.Not.Null);
Assert.That(moreNotifications, Is.True);
// the triggering item samples a new value while it still drains its backlog.
triggering.Object.IsReadyToTrigger = true;
clock.Advance(TimeSpan.FromMilliseconds(100));
subscription.PublishTimerExpired();
Assert.That(subscription.Publish(context, out _, out _), Is.Not.Null);
linkedTrigger.Verify(i => i.SetTriggered(), Times.Exactly(2));
Assert.That(triggering.Object.IsReadyToTrigger, Is.False);
}
/// <summary>
/// M1-4: a notification queued before the first link was created must not trigger the
/// new link (OPC 10000-4 §5.13.1.6).
/// </summary>
[Test]
public void SetTriggeringDiscardsTriggerQueuedBeforeFirstLink()
{
var clock = new FakeTimeProvider();
using Subscription subscription = CreateSubscription(clock, 1000, 10);
ResetKeepAlive(subscription);
var triggering = new Mock<IMonitoredItem>();
triggering.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
triggering.SetupGet(i => i.Id).Returns(1);
triggering.SetupProperty(i => i.IsReadyToTrigger, true);
(Mock<IDataChangeMonitoredItem> linked, Mock<ITriggeredMonitoredItem> linkedTrigger) =
CreateTriggeredItem(2);
AddMonitoredItem(subscription, triggering.Object);
AddMonitoredItem(subscription, linked.Object);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
subscription.SetTriggering(
context,
1,
[2u],
[],
out ArrayOf<StatusCode> addResults,
out _,
out _,
out _);
Assert.That(addResults[0], Is.EqualTo((StatusCode)StatusCodes.Good));
Assert.That(triggering.Object.IsReadyToTrigger, Is.False);
clock.Advance(TimeSpan.FromMilliseconds(101));
subscription.PublishTimerExpired();
linkedTrigger.Verify(i => i.SetTriggered(), Times.Never);
// a notification queued after the link exists triggers it.
triggering.Object.IsReadyToTrigger = true;
clock.Advance(TimeSpan.FromMilliseconds(100));
subscription.PublishTimerExpired();
linkedTrigger.Verify(i => i.SetTriggered(), Times.Once);
}
/// <summary>
/// M1-5: while publishing is disabled a Publish request yields only a keep-alive; the
/// messages already built stay queued until publishing is enabled again
/// (OPC 10000-4 §5.14.1.2).
/// </summary>
[Test]
public void QueuedMessagesAreHeldWhilePublishingIsDisabled()
{
var clock = new FakeTimeProvider();
using var subscription = new Subscription(
m_serverMock.Object,
m_sessionMock.Object,
subscriptionId: 1,
publishingInterval: 100,
maxLifetimeCount: 1000,
maxKeepAliveCount: 10,
maxNotificationsPerPublish: 1,
priority: 0,
publishingEnabled: true,
maxMessageCount: 10,
timeProvider: clock);
AddMonitoredItem(subscription, CreateDataChangeItem(1, 3).Object);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(subscription.PublishTimerExpired(), Is.EqualTo(PublishingState.NotificationsAvailable));
NotificationMessage first = subscription.Publish(context, out _, out bool moreNotifications);
Assert.That(first.SequenceNumber, Is.EqualTo(1u));
Assert.That(moreNotifications, Is.True);
subscription.SetPublishingMode(context, false);
NotificationMessage keepAlive = subscription.Publish(
context,
out ArrayOf<uint> available,
out moreNotifications);
Assert.Multiple(() =>
{
Assert.That(keepAlive.NotificationData, Is.Empty);
Assert.That(keepAlive.SequenceNumber, Is.EqualTo(2u),
"The keep-alive announces the next message that will be sent.");
Assert.That(available.ToArray(), Is.EqualTo(new uint[] { 1 }));
Assert.That(moreNotifications, Is.False);
});
subscription.SetPublishingMode(context, true);
NotificationMessage second = subscription.Publish(context, out _, out moreNotifications);
Assert.Multiple(() =>
{
Assert.That(second.SequenceNumber, Is.EqualTo(2u));
Assert.That(second.NotificationData, Has.Count.EqualTo(1));
Assert.That(moreNotifications, Is.True);
});
}
/// <summary>
/// Review U9: the lifetime counter is reset while a Publish request is queued and
/// only advances while none is (OPC 10000-4 §5.14.1.2 row 27), so a subscription
/// with a queued request never expires.
/// </summary>
[Test]
public void LifetimeCounterIsResetWhileAPublishRequestIsQueued()
{
var clock = new FakeTimeProvider();
using Subscription subscription = CreateSubscription(clock, maxLifetimeCount: 3, maxKeepAliveCount: 1);
ISubscriptionPublishPipeline pipeline = subscription.AsPipeline();
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
for (int tick = 0; tick < 10; tick++)
{
clock.Advance(TimeSpan.FromMilliseconds(101));
PublishingState state = pipeline.PublishTimerExpired(publishRequestQueued: true);
Assert.That(state, Is.Not.EqualTo(PublishingState.Expired));
Assert.That(subscription.Diagnostics.CurrentLifetimeCount, Is.Zero);
// the queued request receives the keep-alive.
if (state == PublishingState.NotificationsAvailable)
{
subscription.Publish(context, out _, out _);
}
}
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(pipeline.PublishTimerExpired(publishRequestQueued: false), Is.Not.EqualTo(PublishingState.Expired));
Assert.That(subscription.Diagnostics.CurrentLifetimeCount, Is.EqualTo(1u));
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(pipeline.PublishTimerExpired(publishRequestQueued: false), Is.Not.EqualTo(PublishingState.Expired));
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(pipeline.PublishTimerExpired(publishRequestQueued: false), Is.EqualTo(PublishingState.Expired));
}
/// <summary>
/// Review U21: messages held while publishing is disabled are not in the
/// retransmission queue yet, so TransferSubscriptions does not advertise them and
/// Republish does not serve them; Publish delivers each of them exactly once.
/// </summary>
[Test]
public void HeldMessagesAreNotOfferedForRetransmission()
{
var clock = new FakeTimeProvider();
using var subscription = new Subscription(
m_serverMock.Object,
m_sessionMock.Object,
subscriptionId: 1,
publishingInterval: 100,
maxLifetimeCount: 1000,
maxKeepAliveCount: 10,
maxNotificationsPerPublish: 1,
priority: 0,
publishingEnabled: true,
maxMessageCount: 10,
timeProvider: clock);
AddMonitoredItem(subscription, CreateDataChangeItem(1, 3).Object);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(subscription.PublishTimerExpired(), Is.EqualTo(PublishingState.NotificationsAvailable));
NotificationMessage first = subscription.Publish(context, out _, out _);
Assert.That(first.SequenceNumber, Is.EqualTo(1u));
subscription.SetPublishingMode(context, false);
Assert.That(subscription.AvailableSequenceNumbersForRetransmission().ToArray(), Is.EqualTo(new uint[] { 1 }));
Assert.That(subscription.Republish(context, 1).SequenceNumber, Is.EqualTo(1u));
ServiceResultException error = Assert.Throws<ServiceResultException>(
() => subscription.Republish(context, 2));
Assert.That(error.StatusCode, Is.EqualTo(StatusCodes.BadMessageNotAvailable));
subscription.SetPublishingMode(context, true);
NotificationMessage second = subscription.Publish(context, out ArrayOf<uint> available, out _);
Assert.That(second.SequenceNumber, Is.EqualTo(2u));
Assert.That(available.ToArray(), Is.EqualTo(new uint[] { 1, 2 }));
Assert.That(subscription.Republish(context, 2).SequenceNumber, Is.EqualTo(2u));
}
/// <summary>
/// M1-7: EventQueueOverflowCount counts the EventQueueOverflowEventType events
/// generated by the subscription's items (OPC 10000-5 §12.15).
/// </summary>
[Test]
public void EventQueueOverflowHandlerIncrementsEventQueueOverflowCount()
{
using Subscription subscription = CreateSubscription(new FakeTimeProvider(), 1000, 10);
subscription.AsPipeline().EventQueueOverflowHandler();
subscription.AsPipeline().EventQueueOverflowHandler();
Assert.That(subscription.Diagnostics.EventQueueOverflowCount, Is.EqualTo(2u));
Assert.That(subscription.Diagnostics.MonitoringQueueOverflowCount, Is.Zero);
}
/// <summary>
/// M1-8: a refresh is in progress from the RefreshStartEvent until its
/// RefreshEndEvent is queued (OPC 10000-9 §5.5.7).
/// </summary>
[Test]
public async Task ConditionRefreshIsInProgressUntilRefreshEndIsQueuedAsync()
{
using Subscription subscription = CreateSubscription(new FakeTimeProvider(), 1000, 10);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
var observations = new List<(string Event, bool Rejected)>();
var eventItem = new Mock<IEventMonitoredItem>();
eventItem.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
eventItem.SetupGet(i => i.Id).Returns(1);
eventItem.SetupGet(i => i.EventFilter).Returns(new EventFilter());
eventItem
.Setup(i => i.QueueEvent(It.IsAny<IFilterTarget>(), It.IsAny<bool>()))
.Callback<IFilterTarget, bool>((e, _) =>
{
bool rejected;
try
{
subscription.ValidateConditionRefresh(context);
rejected = false;
}
catch (ServiceResultException sre) when (sre.StatusCode == StatusCodes.BadRefreshInProgress)
{
rejected = true;
}
observations.Add((e.GetType().Name, rejected));
});
AddMonitoredItem(subscription, eventItem.Object);
bool rejectedDuringNodeManagerCall = false;
m_nodeManagerMock
.Setup(n => n.ConditionRefreshAsync(
It.IsAny<OperationContext>(),
It.IsAny<IList<IEventMonitoredItem>>(),
It.IsAny<CancellationToken>()))
.Callback(() =>
{
try
{
subscription.ValidateConditionRefresh(context);
}
catch (ServiceResultException sre) when (sre.StatusCode == StatusCodes.BadRefreshInProgress)
{
rejectedDuringNodeManagerCall = true;
}
})
.Returns(default(ValueTask));
await subscription.ConditionRefreshAsync().ConfigureAwait(false);
Assert.Multiple(() =>
{
Assert.That(observations, Is.EqualTo(new[]
{
(nameof(RefreshStartEventState), true),
(nameof(RefreshEndEventState), true)
}));
Assert.That(rejectedDuringNodeManagerCall, Is.True);
Assert.DoesNotThrow(() => subscription.ValidateConditionRefresh(context),
"The refresh ends once its RefreshEndEvent has been queued.");
});
}
/// <summary>
/// M1-8: a failing NodeManager refresh does not leave the subscription stuck in
/// the in-progress state.
/// </summary>
[Test]
public void FailedConditionRefreshClearsTheInProgressState()
{
using Subscription subscription = CreateSubscription(new FakeTimeProvider(), 1000, 10);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
var eventItem = new Mock<IEventMonitoredItem>();
eventItem.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
eventItem.SetupGet(i => i.Id).Returns(1);
eventItem.SetupGet(i => i.EventFilter).Returns(new EventFilter());
AddMonitoredItem(subscription, eventItem.Object);
m_nodeManagerMock
.Setup(n => n.ConditionRefreshAsync(
It.IsAny<OperationContext>(),
It.IsAny<IList<IEventMonitoredItem>>(),
It.IsAny<CancellationToken>()))
.Throws(new InvalidOperationException("refresh failed"));
Assert.ThrowsAsync<InvalidOperationException>(
async () => await subscription.ConditionRefreshAsync().ConfigureAwait(false));
Assert.DoesNotThrow(() => subscription.ValidateConditionRefresh(context));
}
/// <summary>
/// Review U20: the pipeline applies the reporting rule to every IMonitoredItem
/// implementation. A pending resend of a DISABLED or SAMPLING item is not
/// published (OPC 10000-4 §5.12.1.3), a triggered SAMPLING item is.
/// </summary>
[TestCase(MonitoringMode.Disabled, false, false)]
[TestCase(MonitoringMode.Sampling, false, false)]
[TestCase(MonitoringMode.Disabled, true, false)]
[TestCase(MonitoringMode.Sampling, true, true)]
[TestCase(MonitoringMode.Reporting, false, true)]
public void PipelinePublishesAResendOnlyForReportingItems(
MonitoringMode mode,
bool readyToPublish,
bool expectPublished)
{
var clock = new FakeTimeProvider();
using Subscription subscription = CreateSubscription(clock, 1000, 10);
Mock<IDataChangeMonitoredItem> item = CreateResendItem(1, () => mode, readyToPublish);
AddMonitoredItem(subscription, item.Object);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
clock.Advance(TimeSpan.FromMilliseconds(101));
subscription.PublishTimerExpired();
NotificationMessage message = subscription.Publish(context, out _, out _);
Assert.That(message?.NotificationData.Count ?? 0, Is.EqualTo(expectPublished ? 1 : 0));
VerifyPublished(item, expectPublished ? Times.Once() : Times.Never());
}
/// <summary>
/// Review U20: an item that leaves REPORTING after it was queued for publishing
/// does not report its pending resend.
/// </summary>
[TestCase(MonitoringMode.Disabled)]
[TestCase(MonitoringMode.Sampling)]
public void ItemLeavingReportingAfterItWasQueuedIsNotPublished(MonitoringMode newMode)
{
var clock = new FakeTimeProvider();
using Subscription subscription = CreateSubscription(clock, 1000, 10);
MonitoringMode mode = MonitoringMode.Reporting;
Mock<IDataChangeMonitoredItem> item = CreateResendItem(1, () => mode, readyToPublish: false);
AddMonitoredItem(subscription, item.Object);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
clock.Advance(TimeSpan.FromMilliseconds(101));
Assert.That(subscription.PublishTimerExpired(), Is.EqualTo(PublishingState.NotificationsAvailable));
mode = newMode;
NotificationMessage message = subscription.Publish(context, out _, out _);
Assert.That(message == null || message.NotificationData.Count == 0, Is.True);
VerifyPublished(item, Times.Never());
}
private static Mock<IDataChangeMonitoredItem> CreateResendItem(
uint id,
Func<MonitoringMode> mode,
bool readyToPublish)
{
// like the sample DataChangeMonitoredItem: the resend flag survives a mode
// change and Publish reports the last value whenever it is set.
var item = new Mock<IDataChangeMonitoredItem>();
item.SetupGet(i => i.Id).Returns(id);
item.SetupGet(i => i.MonitoringMode).Returns(mode);
item.SetupGet(i => i.IsResendData).Returns(true);
item.SetupGet(i => i.IsReadyToPublish).Returns(readyToPublish);
item.SetupGet(i => i.MonitoredItemType).Returns(MonitoredItemTypeMask.DataChange);
item.Setup(i => i.Publish(
It.IsAny<OperationContext>(),
It.IsAny<Queue<MonitoredItemNotification>>(),
It.IsAny<Queue<DiagnosticInfo>>(),
It.IsAny<uint>(),
It.IsAny<ILogger>()))
.Returns<OperationContext, Queue<MonitoredItemNotification>, Queue<DiagnosticInfo>, uint, ILogger>(
(_, notifications, diagnostics, _, _) =>
{
notifications.Enqueue(new MonitoredItemNotification { ClientHandle = id, Value = new DataValue(1) });
diagnostics.Enqueue(new DiagnosticInfo());
return false;
});
return item;
}
private static void VerifyPublished(Mock<IDataChangeMonitoredItem> item, Times times)
{
item.Verify(
i => i.Publish(
It.IsAny<OperationContext>(),
It.IsAny<Queue<MonitoredItemNotification>>(),
It.IsAny<Queue<DiagnosticInfo>>(),
It.IsAny<uint>(),
It.IsAny<ILogger>()),
times);
}
/// <summary>
/// M1-9: an item removed from the subscription is no longer counted in the
/// diagnostics even when its NodeManager reports a failure.
/// </summary>
[Test]
public async Task DeletedItemIsUncountedWhenNodeManagerReportsFailureAsync()
{
using Subscription subscription = CreateSubscription(new FakeTimeProvider(), 1000, 10);
var item = new Mock<IMonitoredItem>();
item.SetupGet(i => i.Id).Returns(7);
item.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Disabled);
AddMonitoredItem(subscription, item.Object);
subscription.UpdateDiagnostics(d =>
{
d.MonitoredItemCount = 1;
d.DisabledMonitoredItemCount = 1;
});
m_nodeManagerMock
.Setup(n => n.DeleteMonitoredItemsAsync(
It.IsAny<OperationContext>(),
It.IsAny<uint>(),
It.IsAny<IList<IMonitoredItem>>(),
It.IsAny<IList<ServiceResult>>(),
It.IsAny<CancellationToken>()))
.Callback<OperationContext, uint, IList<IMonitoredItem>, IList<ServiceResult>, CancellationToken>(
(_, _, _, errors, _) => errors[0] = StatusCodes.BadMonitoredItemIdInvalid)
.Returns(default(ValueTask));
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
DeleteMonitoredItemsResponse response = await subscription.DeleteMonitoredItemsAsync(
context,
[7u]).ConfigureAwait(false);
Assert.Multiple(() =>
{
Assert.That(response.Results[0], Is.EqualTo((StatusCode)StatusCodes.BadMonitoredItemIdInvalid));
Assert.That(subscription.MonitoredItemCount, Is.Zero);
Assert.That(subscription.Diagnostics.MonitoredItemCount, Is.Zero);
Assert.That(subscription.Diagnostics.DisabledMonitoredItemCount, Is.Zero);
});
}
/// <summary>
/// M1-3: triggering links are part of the stored subscription and rebuilt on restore
/// for the items that were restored.
/// </summary>
[Test]
public async Task TriggeringLinksSurviveStoreAndRestoreAsync()
{
StoredSubscription stored;
using (Subscription subscription = CreateSubscription(new FakeTimeProvider(), 1000, 10))
{
var triggering = new Mock<IMonitoredItem>();
triggering.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
triggering.SetupGet(i => i.Id).Returns(1);
triggering.Setup(i => i.ToStorableMonitoredItem())
.Returns(new StoredMonitoredItem { Id = 1, SubscriptionId = 1 });
(Mock<IDataChangeMonitoredItem> linked, _) = CreateTriggeredItem(2);
linked.Setup(i => i.ToStorableMonitoredItem())
.Returns(new StoredMonitoredItem { Id = 2, SubscriptionId = 1 });
AddMonitoredItem(subscription, triggering.Object);
AddMonitoredItem(subscription, linked.Object);
using var context = new OperationContext(m_sessionMock.Object, DiagnosticsMasks.None);
subscription.SetTriggering(context, 1, [2u], [], out _, out _, out _, out _);
stored = (StoredSubscription)subscription.ToStorableSubscription();
}
Assert.That(stored.TriggeringLinks, Is.Not.Null);
Assert.That(stored.TriggeringLinks[1], Is.EqualTo(new uint[] { 2 }));
var restoredTriggering = new Mock<IMonitoredItem>();
restoredTriggering.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
restoredTriggering.SetupGet(i => i.Id).Returns(1);
(Mock<IDataChangeMonitoredItem> restoredLinked, _) = CreateTriggeredItem(2);
m_nodeManagerMock
.Setup(n => n.RestoreMonitoredItemsAsync(
It.IsAny<IList<IStoredMonitoredItem>>(),
It.IsAny<IList<IMonitoredItem>>(),
It.IsAny<IUserIdentity>(),
It.IsAny<CancellationToken>()))
.Callback<IList<IStoredMonitoredItem>, IList<IMonitoredItem>, IUserIdentity, CancellationToken>(
(_, items, _, _) =>
{
items[0] = restoredTriggering.Object;
items[1] = restoredLinked.Object;
})
.Returns(default(ValueTask));
stored.SentMessages = [];
using Subscription restored = await Subscription.RestoreAsync(m_serverMock.Object, stored)
.ConfigureAwait(false);
IReadOnlyDictionary<uint, IReadOnlyList<uint>> restoredLinks =
((IStoredSubscriptionTriggering)restored.ToStorableSubscription()).TriggeringLinks;
Assert.That(restoredLinks, Is.Not.Null);
Assert.That(restoredLinks[1], Is.EqualTo(new uint[] { 2 }));
}
/// <summary>
/// M1-3: links to items that could not be restored are dropped.
/// </summary>
[Test]
public async Task TriggeringLinksToUnrestoredItemsAreDroppedAsync()
{
var stored = new StoredSubscription
{
Id = 5,
IsDurable = true,
PublishingInterval = 1000,
MaxKeepaliveCount = 10,
MaxLifetimeCount = 30,
MaxMessageCount = 10,
SequenceNumber = 1,
SentMessages = [],
MonitoredItems =
[
new StoredMonitoredItem { Id = 1, SubscriptionId = 5 },
new StoredMonitoredItem { Id = 2, SubscriptionId = 5 }
],
TriggeringLinks = new Dictionary<uint, IReadOnlyList<uint>> { [1] = [2u] }
};
var restoredTriggering = new Mock<IMonitoredItem>();
restoredTriggering.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
restoredTriggering.SetupGet(i => i.Id).Returns(1);
m_nodeManagerMock
.Setup(n => n.RestoreMonitoredItemsAsync(
It.IsAny<IList<IStoredMonitoredItem>>(),
It.IsAny<IList<IMonitoredItem>>(),
It.IsAny<IUserIdentity>(),
It.IsAny<CancellationToken>()))
.Callback<IList<IStoredMonitoredItem>, IList<IMonitoredItem>, IUserIdentity, CancellationToken>(
(_, items, _, _) => items[0] = restoredTriggering.Object)
.Returns(default(ValueTask));
using Subscription restored = await Subscription.RestoreAsync(m_serverMock.Object, stored)
.ConfigureAwait(false);
Assert.That(
((IStoredSubscriptionTriggering)restored.ToStorableSubscription()).TriggeringLinks,
Is.Null);
}
private Subscription CreateSubscription(
TimeProvider timeProvider,
uint maxLifetimeCount,
uint maxKeepAliveCount)
{
return new Subscription(
m_serverMock.Object,
m_sessionMock.Object,
subscriptionId: 1,
publishingInterval: 100,
maxLifetimeCount: maxLifetimeCount,
maxKeepAliveCount: maxKeepAliveCount,
maxNotificationsPerPublish: 0,
priority: 0,
publishingEnabled: true,
maxMessageCount: 10,
timeProvider: timeProvider);
}
private static Mock<IDataChangeMonitoredItem> CreateDataChangeItem(uint id, int notificationCount)
{
var item = new Mock<IDataChangeMonitoredItem>();
item.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
var pending = new Queue<MonitoredItemNotification>();
for (int index = 0; index < notificationCount; index++)
{
pending.Enqueue(new MonitoredItemNotification { ClientHandle = id, Value = new DataValue(index) });
}
item.SetupGet(i => i.Id).Returns(id);
item.SetupGet(i => i.IsReadyToPublish).Returns(() => pending.Count > 0);
item.SetupGet(i => i.MonitoredItemType).Returns(MonitoredItemTypeMask.DataChange);
item.Setup(i => i.Publish(
It.IsAny<OperationContext>(),
It.IsAny<Queue<MonitoredItemNotification>>(),
It.IsAny<Queue<DiagnosticInfo>>(),
It.IsAny<uint>(),
It.IsAny<ILogger>()))
.Returns<OperationContext, Queue<MonitoredItemNotification>, Queue<DiagnosticInfo>, uint, ILogger>(
(_, notifications, diagnostics, maxNotificationsPerPublish, _) =>
{
for (uint published = 0; pending.Count > 0 && published < maxNotificationsPerPublish; published++)
{
notifications.Enqueue(pending.Dequeue());
diagnostics.Enqueue(new DiagnosticInfo());
}
return pending.Count > 0;
});
return item;
}
private static (Mock<IDataChangeMonitoredItem> Item, Mock<ITriggeredMonitoredItem> Trigger)
CreateTriggeredItem(uint id)
{
var item = new Mock<IDataChangeMonitoredItem>();
item.SetupGet(i => i.MonitoringMode).Returns(MonitoringMode.Reporting);
item.SetupGet(i => i.Id).Returns(id);
item.SetupGet(i => i.IsReadyToPublish).Returns(false);
item.SetupGet(i => i.MonitoredItemType).Returns(MonitoredItemTypeMask.DataChange);
Mock<ITriggeredMonitoredItem> trigger = item.As<ITriggeredMonitoredItem>();
trigger.SetupGet(i => i.Id).Returns(id);
trigger.Setup(i => i.SetTriggered()).Returns(false);
return (item, trigger);
}
private static void ResetKeepAlive(Subscription subscription)
{
typeof(Subscription)
.GetField("m_keepAliveCounter", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(subscription, 0u);
}
private static void AddMonitoredItem(Subscription subscription, IMonitoredItem item)
{
var monitoredItems = (System.Collections.IDictionary)typeof(Subscription)
.GetField("m_monitoredItems", BindingFlags.NonPublic | BindingFlags.Instance)!
.GetValue(subscription);
var itemsToCheck = (LinkedList<IMonitoredItem>)typeof(Subscription)
.GetField("m_itemsToCheck", BindingFlags.NonPublic | BindingFlags.Instance)!
.GetValue(subscription);
monitoredItems.Add(item.Id, itemsToCheck.AddLast(item));
}
private static void AddTriggerLink(
Subscription subscription,
uint triggeringId,
ITriggeredMonitoredItem triggeredItem)
{
var itemsToTrigger = (Dictionary<uint, List<ITriggeredMonitoredItem>>)typeof(Subscription)
.GetField("m_itemsToTrigger", BindingFlags.NonPublic | BindingFlags.Instance)!
.GetValue(subscription);
if (!itemsToTrigger.TryGetValue(triggeringId, out List<ITriggeredMonitoredItem> list))
{
itemsToTrigger[triggeringId] = list = [];
}
list.Add(triggeredItem);
}
}
}