mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-28 22:55:49 +08:00
# Description Adds an opt-in `SubscriptionRecoveryPolicy` for both the Classic V1 and V2 subscription engines that lets the client recover from server-emitted `Good_SubscriptionTransferred` notifications which arrive against a freshly created subscription (observed against Kepware after a server re-initialise, very likely a leak from the pre-restart session's pending status notifications onto the new session — abetted by Kepware re-using `SubscriptionId = 1` across generations). Per [OPC UA Part 4 §5.14.7](https://reference.opcfoundation.org/Core/Part4/v105/docs/5.14.7), `Good_SubscriptionTransferred` is supposed to be delivered to the *old* Session whose subscription was transferred away via `TransferSubscriptions`. The existing V1 behaviour of stopping the per-subscription publish loop on receipt is therefore **spec-compliant**; the issue is that some servers deliver the notification on a Session that is still actively using the subscription, leaving the per-subscription dispatch silently disabled. The fix is **opt-in** and **non-breaking**: - New `SubscriptionRecoveryPolicy` enum (`ReportOnly` default, `RecreateOnUnsolicitedTransfer`). - V1 Classic `Subscription.RecoveryPolicy` property; V2 `Subscriptions.SubscriptionOptions.RecoveryPolicy` init property. - When the policy is `RecreateOnUnsolicitedTransfer`, the SDK drops queued acks for the dead subscription id (avoiding `BadSubscriptionIdInvalid` ack errors on servers that re-use ids) and recreates the subscription in place on the same Session. ## What changed **Public API additions (non-breaking, defaults preserve current behaviour):** - `SubscriptionRecoveryPolicy` enum (`ReportOnly`, `RecreateOnUnsolicitedTransfer`). - V1 `Subscription.RecoveryPolicy` property. - V2 `Subscriptions.SubscriptionOptions.RecoveryPolicy` init property. - Internal `IMessageAckQueue.DropPendingForSubscription(uint)` and protected `MessageProcessor.AckQueue` accessor. **V1 Classic — `Libraries/Opc.Ua.Client/Subscription/Classic/Subscription.cs`:** - Replaces the inline `Good_SubscriptionTransferred` branch with `HandleGoodSubscriptionTransferred`. `ReportOnly` preserves the original behaviour (stop publish timer/worker); `RecreateOnUnsolicitedTransfer` triggers `TriggerUnsolicitedTransferRecovery`, guarded by `m_recreateAfterTransferInProgress` for idempotency. - Recovery drains pending acks via the new `ClassicSubscriptionEngine.RemoveAcknowledgementsForSubscription` **before** resetting the subscription, then runs `DeleteSubscription()` + `CreateAsync()` on the same session so the publish worker restarts via `StartKeepAliveTimer`. - Discriminator (`Session != null && !Session.Reconnecting && Created`) defers to the reconnect pipeline if it is already running. **V2 — `Libraries/Opc.Ua.Client/Subscription/Subscription.cs`:** - Completes the `OnStatusChangeNotificationAsync` TODO: now always fires `OnPublishStateChanged(mask)` so `PublishState.Transferred` actually reaches handlers (previously the no-op override silently swallowed the flag). - Under `RecreateOnUnsolicitedTransfer`, calls `AckQueue.DropPendingForSubscription(deadId)` then existing `ResetToRecreateAsync` on a worker task — no service calls from the dispatch path. - V2 `SubscriptionManager` implements `DropPendingForSubscription` via drain-and-refill on the prioritised ack channel. **Tests — `Tests/Opc.Ua.Client.Tests/Subscription`:** - V1 in `Classic/SubscriptionUnitTests.cs`: `ReportOnlyPolicyDoesNotRecreateOnUnsolicitedTransferAsync`, `RecreatePolicyRecreatesSubscriptionOnUnsolicitedTransferAsync`, `RecreatePolicyCollapsesConcurrentNotificationsAsync`, `RecreatePolicyDoesNotRecreateWhileSessionReconnectingAsync`. - V2 in `SubscriptionTests.cs`: `GoodSubscriptionTransferredRaisesPublishStateTransferredAsync`, `GoodSubscriptionTransferredRecreatesSubscriptionWhenPolicyEnabledAsync`, `GoodSubscriptionTransferredCollapsesConcurrentRecreatesAsync`. - `FakeMessageAckQueue` gained a `DroppedSubscriptions` recorder so the V2 test can assert ack pruning ran. **Docs — `Docs/TransferSubscription.md`:** - New "Recovering from unsolicited `Good_SubscriptionTransferred`" section with spec context, usage examples for both engines, and the caveat that recovery is recreate-based — **not** lossless failover; the server-side retransmission queue and triggering relationships tied to the invalidated subscription id are not preserved. ## Validation | Scope | Result | | --- | --- | | `Libraries/Opc.Ua.Client` build (`net10.0`, `net48`) | 0 warnings, 0 errors | | `Tests/Opc.Ua.Client.Tests` build (`net10.0`, `net472`) | 0 warnings, 0 errors | | New tests on `net10.0` and `net472` | 7/7 pass on each | | Full subscription unit-test suite (`SubscriptionUnitTests` + `SubscriptionTests` + `MessageProcessorTests` + `SubscriptionManagerTests`) | 110/110 pass on each TFM | | Entire `Opc.Ua.Client.Tests` suite (`net10.0`) | 1268/1268 pass | | Classic transfer integration tests (`Tests/Opc.Ua.Subscriptions.Classic.Tests`, `net10.0`) | 20/20 pass | | V2 transfer compliance tests (`Tests/Opc.Ua.Subscriptions.Tests`, `net10.0`) | 9/9 pass | > Marked as **draft** because the maintainer wanted to first confirm whether the stack is spec-compliant before deciding which knobs to ship; this PR documents the investigation (stack is compliant), and surfaces an opt-in recovery policy that addresses the field symptom without changing the default behaviour. Convert to ready-for-review once the design intent is acknowledged. ## Related Issues - Fixes #3540 ## Checklist - [x] 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. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [x] 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. --------- Co-authored-by: marcschier <marc.schier@github.com>
640 lines
30 KiB
C#
640 lines
30 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.Threading;
|
|
using System.Threading.Tasks;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.Client.Tests
|
|
{
|
|
[TestFixture]
|
|
[Parallelizable]
|
|
public class SubscriptionUnitTests
|
|
{
|
|
private const PublishStateChangedMask kSessionNotConnected = PublishStateChangedMask.Stopped | PublishStateChangedMask.SessionNotConnected;
|
|
|
|
public record KeepAliveTestDataProvider(PublishStateChangedMask ExpectedPublishState) : IFormattable
|
|
{
|
|
public bool SessionConnected { get; init; }
|
|
public bool SessionReconnecting { get; init; }
|
|
public bool SessionKeepAliveStopped { get; init; }
|
|
public string ToString(string format, IFormatProvider formatProvider)
|
|
{
|
|
return $"Connected={SessionConnected}, " +
|
|
$"reconnecting={SessionReconnecting}, " +
|
|
$"keepAlive={SessionKeepAliveStopped}. " +
|
|
$"Expected status:{ExpectedPublishState}";
|
|
}
|
|
}
|
|
|
|
private sealed class SubscriptionContainer : IDisposable
|
|
{
|
|
private readonly CancellationTokenRegistration m_tokedCancellation;
|
|
public Subscription Subscription { get; }
|
|
public Task[] ProcessedMessages { get; }
|
|
|
|
public SubscriptionContainer(Subscription subscription, TaskCompletionSource<bool>[] awaiters, CancellationToken token)
|
|
{
|
|
Subscription = subscription;
|
|
ProcessedMessages = [.. awaiters.Select(x => x.Task)];
|
|
m_tokedCancellation = token.Register(() =>
|
|
{
|
|
foreach (TaskCompletionSource<bool> awaiter in awaiters)
|
|
{
|
|
awaiter.TrySetCanceled();
|
|
}
|
|
});
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
m_tokedCancellation.Dispose();
|
|
Subscription.Dispose();
|
|
}
|
|
}
|
|
|
|
private static Task AwaitForRepublishTimeout(CancellationToken ct)
|
|
{
|
|
return Task.Delay(Subscription.RepublishMessageTimeout + 100, ct);
|
|
}
|
|
|
|
private static ISession BuildSessionMock(Func<uint, uint, bool> republishHandler = null, Action<Mock<ISession>> setup = null)
|
|
{
|
|
uint subscriptionIdSeed = 0u;
|
|
|
|
var session = new Mock<ISession>();
|
|
if (republishHandler is not null)
|
|
{
|
|
session
|
|
.Setup(x => x.RepublishAsync(It.IsAny<uint>(), It.IsAny<uint>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync<uint, uint, CancellationToken, ISession, (bool, ServiceResult)>((
|
|
subscriptionId,
|
|
sequenceNumber,
|
|
ct) =>
|
|
{
|
|
if (subscriptionId > subscriptionIdSeed)
|
|
{
|
|
return (true, StatusCodes.BadSubscriptionIdInvalid);
|
|
}
|
|
if (republishHandler(subscriptionId, sequenceNumber))
|
|
{
|
|
return (true, ServiceResult.Good);
|
|
}
|
|
return (true, StatusCodes.BadMessageNotAvailable);
|
|
});
|
|
}
|
|
session
|
|
.Setup(x => x
|
|
.CreateSubscriptionAsync(
|
|
It.IsAny<RequestHeader>(),
|
|
It.IsAny<double>(),
|
|
It.IsAny<uint>(),
|
|
It.IsAny<uint>(),
|
|
It.IsAny<uint>(),
|
|
It.IsAny<bool>(),
|
|
It.IsAny<byte>(),
|
|
It.IsAny<CancellationToken>()
|
|
)
|
|
)
|
|
.ReturnsAsync<
|
|
RequestHeader,
|
|
double,
|
|
uint,
|
|
uint,
|
|
uint,
|
|
bool,
|
|
byte,
|
|
CancellationToken,
|
|
ISession,
|
|
CreateSubscriptionResponse
|
|
>((
|
|
requestHeader,
|
|
requestedPublishingInterval,
|
|
requestedLifetimeCount,
|
|
requestedMaxKeepAliveCount,
|
|
maxNotificationsPerPublish,
|
|
publishingEnabled,
|
|
priority,
|
|
ct
|
|
) => new() { SubscriptionId = ++subscriptionIdSeed });
|
|
session
|
|
.Setup(x => x.SetPublishingModeAsync(
|
|
It.IsAny<RequestHeader>(),
|
|
It.IsAny<bool>(),
|
|
It.IsAny<ArrayOf<uint>>(),
|
|
It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync<
|
|
RequestHeader,
|
|
bool,
|
|
ArrayOf<uint>,
|
|
CancellationToken,
|
|
ISession,
|
|
SetPublishingModeResponse
|
|
>((requestHeader, publishingEnabled, subscriptionIds, ct) => new()
|
|
{
|
|
Results = [.. subscriptionIds.ConvertAll(id => id > subscriptionIdSeed ?
|
|
StatusCodes.BadSubscriptionIdInvalid :
|
|
StatusCodes.Good)],
|
|
DiagnosticInfos = [.. subscriptionIds.ConvertAll(_ => new DiagnosticInfo())]
|
|
});
|
|
setup?.Invoke(session);
|
|
return session.Object;
|
|
}
|
|
|
|
private static NotificationMessage[] BuildMessages(int count)
|
|
{
|
|
return [.. Enumerable
|
|
.Range(1, count)
|
|
.Select(sequenceNumber => new NotificationMessage
|
|
{
|
|
SequenceNumber = (uint)sequenceNumber,
|
|
NotificationData = [new(new DataChangeNotification { SequenceNumber = (uint)sequenceNumber })]
|
|
})
|
|
.Prepend(new())];
|
|
}
|
|
|
|
private static async Task<SubscriptionContainer> BuildSubscriptionAsync(
|
|
NotificationMessage[] messagesToProcess,
|
|
bool sequentialPublishing,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
TaskCompletionSource<bool>[] messageAwaiters =
|
|
[.. messagesToProcess.Select(_ => new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously))];
|
|
messageAwaiters[0].SetResult(true);
|
|
List<uint> availableSequenceNumbers = [.. messagesToProcess.Skip(1).Select(x => x.SequenceNumber)];
|
|
|
|
var subscription = new Subscription(
|
|
NUnitTelemetryContext.Create(),
|
|
new()
|
|
{
|
|
PublishingEnabled = true,
|
|
SequentialPublishing = sequentialPublishing,
|
|
MaxMessageCount = messagesToProcess.Length
|
|
})
|
|
{
|
|
FastDataChangeCallback = (_, message, _) => messageAwaiters[message.SequenceNumber].TrySetResult(true)
|
|
};
|
|
subscription.Session = BuildSessionMock((subscriptionId, sequenceNumber) =>
|
|
{
|
|
//simplified republish emulation
|
|
if (subscription.Id == subscriptionId && availableSequenceNumbers.Remove(sequenceNumber))
|
|
{
|
|
subscription.SaveMessageInCache(default, messagesToProcess[sequenceNumber]);
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
await subscription.CreateAsync(cancellationToken).ConfigureAwait(false);
|
|
return new(subscription, messageAwaiters, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set up a Server and a Client instance.
|
|
/// </summary>
|
|
[OneTimeSetUp]
|
|
public void OneTimeSetUp()
|
|
{
|
|
TestContext.AddFormatter<NotificationMessage>((obj) =>
|
|
{
|
|
if (obj is NotificationMessage other)
|
|
{
|
|
return $"{nameof(NotificationMessage)}: {other.SequenceNumber}";
|
|
}
|
|
return null;
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Regression test for the documented sequential-publishing trade-off:
|
|
/// when <c>sequentialPublishing == true</c> the subscription tolerates
|
|
/// at most one message of look-ahead before abandoning the next
|
|
/// expected sequence number. A late-arriving message whose republish
|
|
/// timeout has already elapsed is dropped instead of being re-inserted
|
|
/// out of order, in exchange for the strict in-order delivery
|
|
/// guarantee on the remaining sequence.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Marked <see cref="ExplicitAttribute"/> because the current
|
|
/// subscription implementation delivers the late <c>messages[1]</c>
|
|
/// out-of-order (5 notifications observed where 4 are expected)
|
|
/// instead of abandoning it. The test is retained as a manual probe
|
|
/// until the underlying sequential-publishing abandonment logic is
|
|
/// fixed; see follow-up tracking issue.
|
|
/// </remarks>
|
|
[Test]
|
|
[Explicit("Tracks the sequential-publishing late-message abandonment bug; un-mark once the product code drops messages past the republish timeout.")]
|
|
[CancelAfter(Subscription.RepublishMessageTimeout * 6)]
|
|
public async Task OldMessagesAbandonedAfterRepublishTimeoutInSequentialModeAsync(CancellationToken ct)
|
|
{
|
|
NotificationMessage[] messages = BuildMessages(5);
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(messages, sequentialPublishing: true, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
|
|
subscription.SaveMessageInCache([], messages[3]);
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache([], messages[2]);//this one will be processed, because one message out of order is always awaited
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache([], messages[4]);
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache([], messages[1]);//this one should be lost due to expiration timout
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache([], messages[5]);
|
|
|
|
await Task.WhenAll(
|
|
container.ProcessedMessages[2],
|
|
container.ProcessedMessages[3],
|
|
container.ProcessedMessages[4],
|
|
container.ProcessedMessages[5]
|
|
).ConfigureAwait(false);
|
|
|
|
Assert.That(subscription.Notifications, Is.EqualTo([messages[2], messages[3], messages[4], messages[5]]));
|
|
}
|
|
|
|
[Test]
|
|
[CancelAfter(Subscription.RepublishMessageTimeout * 3)]
|
|
public async Task WillRestoreOrderOfTwoMessagesForSequentialPublishingAsync(CancellationToken ct)
|
|
{
|
|
NotificationMessage[] messages = BuildMessages(3);
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(messages, sequentialPublishing: true, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
|
|
subscription.SaveMessageInCache([], messages[2]);
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache([], messages[1]);
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache([], messages[3]);
|
|
|
|
await Task.WhenAll(container.ProcessedMessages).ConfigureAwait(false);
|
|
Assert.That(subscription.Notifications, Is.EqualTo(messages.Skip(1)));
|
|
}
|
|
|
|
[Theory]
|
|
[CancelAfter(Subscription.RepublishMessageTimeout * 7)]
|
|
public async Task WillAbandonMissedMessagesIfThereAreNoAvailableSequenceNumbersAsync(bool sequentialPublishing, CancellationToken ct)
|
|
{
|
|
int[] sequenceNumbersToPublish = [/*1..2 gap*/ 3, 4, /*5..7 gap*/ 8, 9, 10];
|
|
NotificationMessage[] messages = BuildMessages(sequenceNumbersToPublish[^1]);
|
|
NotificationMessage[] messagesToPublish = [.. sequenceNumbersToPublish.Select(x => messages[x])];
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(messages, sequentialPublishing, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
|
|
foreach (NotificationMessage message in messagesToPublish)
|
|
{
|
|
subscription.SaveMessageInCache([], message);
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
}
|
|
await Task.WhenAll(sequenceNumbersToPublish.Select(x => container.ProcessedMessages[x])).ConfigureAwait(false);
|
|
|
|
if (sequentialPublishing)
|
|
{
|
|
Assert.That(subscription.Notifications, Is.EqualTo(messagesToPublish));
|
|
}
|
|
else
|
|
{
|
|
Assert.That(subscription.Notifications, Is.EquivalentTo(messagesToPublish));
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[CancelAfter(Subscription.RepublishMessageTimeout * 3)]
|
|
public async Task WillRepublishIfMissedMessagesOnFirstPublishAsync(
|
|
bool sequentialPublishing,
|
|
[Values(2, 5, 8, 11)] int gapEnd,
|
|
CancellationToken ct)
|
|
{
|
|
int finalMessage = gapEnd + 1;
|
|
NotificationMessage[] messages = BuildMessages(finalMessage);
|
|
uint[] sequenceNumbersOfGap = [.. messages.Skip(1).Take(gapEnd).Select(x => x.SequenceNumber)];
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(messages, sequentialPublishing, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
|
|
subscription.SaveMessageInCache(sequenceNumbersOfGap, messages[gapEnd]);
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache(sequenceNumbersOfGap, messages[finalMessage]);
|
|
await Task.WhenAll(container.ProcessedMessages).ConfigureAwait(false);
|
|
|
|
if (sequentialPublishing)
|
|
{
|
|
Assert.That(subscription.Notifications, Is.EqualTo(messages.Skip(1)));
|
|
}
|
|
else
|
|
{
|
|
Assert.That(subscription.Notifications, Is.EquivalentTo(messages.Skip(1)));
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[CancelAfter(Subscription.RepublishMessageTimeout * 3)]
|
|
public async Task WillRepublishIfMissedMessagesInBetweenOfPublishesAsync(
|
|
bool sequentialPublishing,
|
|
[Values(1, 6, 9)] int gapStart,
|
|
[Values(1, 6, 9)] int gapSize,
|
|
CancellationToken ct)
|
|
{
|
|
int gapEnd = gapStart + gapSize + 1;
|
|
int finalMessage = gapEnd + 1;
|
|
NotificationMessage[] messages = BuildMessages(finalMessage);
|
|
uint[] sequenceNumbersOfGap = [.. messages.Skip(1).Skip(gapStart).Take(gapSize).Select(x => x.SequenceNumber)];
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(messages, sequentialPublishing, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
|
|
for (int i = 1; i <= gapStart; i++)
|
|
{
|
|
subscription.SaveMessageInCache([], messages[i]);
|
|
}
|
|
subscription.SaveMessageInCache(sequenceNumbersOfGap, messages[gapEnd]);
|
|
await AwaitForRepublishTimeout(ct).ConfigureAwait(false);
|
|
subscription.SaveMessageInCache(sequenceNumbersOfGap, messages[finalMessage]);
|
|
await Task.WhenAll(container.ProcessedMessages).ConfigureAwait(false);
|
|
|
|
if (sequentialPublishing)
|
|
{
|
|
Assert.That(subscription.Notifications, Is.EqualTo(messages.Skip(1)));
|
|
}
|
|
else
|
|
{
|
|
Assert.That(subscription.Notifications, Is.EquivalentTo(messages.Skip(1)));
|
|
}
|
|
}
|
|
|
|
[DatapointSource]
|
|
public IEnumerable<KeepAliveTestDataProvider> SubscriptionKeepAliveValues()
|
|
{
|
|
yield return new(PublishStateChangedMask.Stopped) { SessionConnected = true, SessionReconnecting = false, SessionKeepAliveStopped = false };
|
|
|
|
yield return new(kSessionNotConnected) { SessionConnected = false, SessionReconnecting = false, SessionKeepAliveStopped = false };
|
|
yield return new(kSessionNotConnected) { SessionConnected = false, SessionReconnecting = true, SessionKeepAliveStopped = false };
|
|
yield return new(kSessionNotConnected) { SessionConnected = false, SessionReconnecting = false, SessionKeepAliveStopped = true };
|
|
yield return new(kSessionNotConnected) { SessionConnected = false, SessionReconnecting = true, SessionKeepAliveStopped = true };
|
|
|
|
yield return new(kSessionNotConnected) { SessionConnected = true, SessionReconnecting = true, SessionKeepAliveStopped = false };
|
|
yield return new(kSessionNotConnected) { SessionConnected = true, SessionReconnecting = true, SessionKeepAliveStopped = true };
|
|
|
|
yield return new(kSessionNotConnected) { SessionConnected = true, SessionReconnecting = false, SessionKeepAliveStopped = true };
|
|
}
|
|
|
|
[Theory]
|
|
[CancelAfter(Subscription.MinKeepAliveTimerInterval * 10)]
|
|
public async Task RespectsStateOfSessionDuringKeepAliveCalls(KeepAliveTestDataProvider testData, CancellationToken ct)
|
|
{
|
|
var keepAliveCompleted = new TaskCompletionSource<PublishStateChangedMask>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
void KeepAliveHasTriggered(Subscription x, PublishStateChangedEventArgs y) => keepAliveCompleted.TrySetResult(y.Status);
|
|
ISession session = BuildSessionMock(
|
|
setup: mock =>
|
|
{
|
|
mock.Setup(x => x.Connected).Returns(testData.SessionConnected);
|
|
mock.Setup(x => x.Reconnecting).Returns(testData.SessionReconnecting);
|
|
mock.Setup(x => x.KeepAliveStopped).Returns(testData.SessionKeepAliveStopped);
|
|
});
|
|
|
|
using var subscription = new Subscription(
|
|
NUnitTelemetryContext.Create(),
|
|
new() { PublishingEnabled = true })
|
|
{
|
|
Session = session
|
|
};
|
|
subscription.PublishStatusChanged += KeepAliveHasTriggered;
|
|
await subscription.CreateAsync(ct).ConfigureAwait(false);
|
|
await Task.WhenAny(keepAliveCompleted.Task, Task.Delay(-1, ct)).ConfigureAwait(false);
|
|
|
|
Assert.That(await keepAliveCompleted.Task.ConfigureAwait(false), Is.EqualTo(testData.ExpectedPublishState));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Regression coverage for OPCFoundation/UA-.NETStandard#3540. When
|
|
/// <see cref="SubscriptionRecoveryPolicy.ReportOnly"/> is in effect
|
|
/// (default) and an unsolicited <c>Good_SubscriptionTransferred</c>
|
|
/// arrives, the subscription must remain on its original server-side
|
|
/// identifier — no recreate is attempted — preserving the
|
|
/// spec-strict, backwards-compatible behaviour. The
|
|
/// <c>Transferred</c> publish-state flag is raised so listeners
|
|
/// can still react.
|
|
/// </summary>
|
|
[Test]
|
|
[CancelAfter(5000)]
|
|
public async Task ReportOnlyPolicyDoesNotRecreateOnUnsolicitedTransferAsync(CancellationToken ct)
|
|
{
|
|
NotificationMessage[] messages = BuildMessages(2);
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(
|
|
messages, sequentialPublishing: false, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
uint originalId = subscription.Id;
|
|
|
|
Assert.That(originalId, Is.GreaterThan(0u));
|
|
Assert.That(subscription.RecoveryPolicy,
|
|
Is.EqualTo(SubscriptionRecoveryPolicy.ReportOnly),
|
|
"ReportOnly must be the spec-strict default to preserve backwards compatibility.");
|
|
|
|
PublishStateChangedMask observedMask = PublishStateChangedMask.None;
|
|
var observed = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
subscription.PublishStatusChanged += (_, args) =>
|
|
{
|
|
if ((args.Status & PublishStateChangedMask.Transferred) != 0)
|
|
{
|
|
observedMask = args.Status;
|
|
observed.TrySetResult(true);
|
|
}
|
|
};
|
|
|
|
subscription.SaveMessageInCache(default, BuildStatusChangeMessage(
|
|
sequenceNumber: 1, StatusCodes.GoodSubscriptionTransferred));
|
|
|
|
await Task.WhenAny(observed.Task, Task.Delay(2000, ct)).ConfigureAwait(false);
|
|
|
|
Assert.That(observed.Task.Status, Is.EqualTo(TaskStatus.RanToCompletion),
|
|
"Transferred publish-state flag was never raised.");
|
|
Assert.That(observedMask & PublishStateChangedMask.Transferred,
|
|
Is.EqualTo(PublishStateChangedMask.Transferred));
|
|
Assert.That(subscription.Id, Is.EqualTo(originalId),
|
|
"ReportOnly must not recreate the subscription on the server.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Regression coverage for OPCFoundation/UA-.NETStandard#3540. With
|
|
/// <see cref="SubscriptionRecoveryPolicy.RecreateOnUnsolicitedTransfer"/>
|
|
/// enabled, an unsolicited <c>Good_SubscriptionTransferred</c> must
|
|
/// drive an in-place recreate against the same session and the
|
|
/// subscription must end up with a fresh server-side identifier.
|
|
/// This is the "I just want my data back" path that fixes the
|
|
/// Kepware-after-reinitialise symptom reported in the issue.
|
|
/// </summary>
|
|
[Test]
|
|
[CancelAfter(5000)]
|
|
public async Task RecreatePolicyRecreatesSubscriptionOnUnsolicitedTransferAsync(CancellationToken ct)
|
|
{
|
|
NotificationMessage[] messages = BuildMessages(2);
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(
|
|
messages, sequentialPublishing: false, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
uint originalId = subscription.Id;
|
|
|
|
Assert.That(originalId, Is.GreaterThan(0u));
|
|
|
|
subscription.RecoveryPolicy = SubscriptionRecoveryPolicy.RecreateOnUnsolicitedTransfer;
|
|
|
|
subscription.SaveMessageInCache(default, BuildStatusChangeMessage(
|
|
sequenceNumber: 1, StatusCodes.GoodSubscriptionTransferred));
|
|
|
|
// Recovery runs on a background Task — poll for Id change.
|
|
await WaitForAsync(
|
|
() => subscription.Id != 0 && subscription.Id != originalId,
|
|
TimeSpan.FromSeconds(3),
|
|
ct).ConfigureAwait(false);
|
|
|
|
Assert.That(subscription.Id, Is.Not.EqualTo(originalId),
|
|
"Recovery must assign a fresh server-side subscription id.");
|
|
Assert.That(subscription.Id, Is.Not.Zero,
|
|
"Subscription must end up Created after recovery.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Multiple unsolicited Good_SubscriptionTransferred dispatches must
|
|
/// collapse into a single recovery attempt — guarded by the
|
|
/// <c>m_recreateAfterTransferInProgress</c> flag.
|
|
/// </summary>
|
|
[Test]
|
|
[CancelAfter(5000)]
|
|
public async Task RecreatePolicyCollapsesConcurrentNotificationsAsync(CancellationToken ct)
|
|
{
|
|
NotificationMessage[] messages = BuildMessages(2);
|
|
using SubscriptionContainer container = await BuildSubscriptionAsync(
|
|
messages, sequentialPublishing: false, ct).ConfigureAwait(false);
|
|
Subscription subscription = container.Subscription;
|
|
uint originalId = subscription.Id;
|
|
|
|
subscription.RecoveryPolicy = SubscriptionRecoveryPolicy.RecreateOnUnsolicitedTransfer;
|
|
|
|
// Stack multiple status notifications back-to-back. Only the
|
|
// first should drive a recreate; the rest hit the idempotency
|
|
// guard and become no-ops.
|
|
for (uint i = 1; i <= 3; i++)
|
|
{
|
|
subscription.SaveMessageInCache(default, BuildStatusChangeMessage(
|
|
sequenceNumber: i, StatusCodes.GoodSubscriptionTransferred));
|
|
}
|
|
|
|
await WaitForAsync(
|
|
() => subscription.Id != 0 && subscription.Id != originalId,
|
|
TimeSpan.FromSeconds(3),
|
|
ct).ConfigureAwait(false);
|
|
|
|
uint afterFirstRecovery = subscription.Id;
|
|
|
|
// Give late recovery tasks a chance to run; assert nothing
|
|
// else triggered.
|
|
await Task.Delay(300, ct).ConfigureAwait(false);
|
|
|
|
Assert.That(subscription.Id, Is.EqualTo(afterFirstRecovery),
|
|
"Idempotency guard must prevent duplicate recreates from the burst.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// When the owning session reports itself as reconnecting the
|
|
/// discriminator must classify the notification as solicited and
|
|
/// fall back to the spec-strict ReportOnly path even if the
|
|
/// caller asked for auto-recreate. Avoids fighting the reconnect
|
|
/// pipeline that may itself be running TransferSubscriptions.
|
|
/// </summary>
|
|
[Test]
|
|
[CancelAfter(5000)]
|
|
public async Task RecreatePolicyDoesNotRecreateWhileSessionReconnectingAsync(CancellationToken ct)
|
|
{
|
|
uint subscriptionIdSeed = 0;
|
|
var session = new Mock<ISession>();
|
|
session.Setup(x => x.Connected).Returns(true);
|
|
session.Setup(x => x.Reconnecting).Returns(true);
|
|
session
|
|
.Setup(x => x.CreateSubscriptionAsync(
|
|
It.IsAny<RequestHeader>(),
|
|
It.IsAny<double>(),
|
|
It.IsAny<uint>(),
|
|
It.IsAny<uint>(),
|
|
It.IsAny<uint>(),
|
|
It.IsAny<bool>(),
|
|
It.IsAny<byte>(),
|
|
It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(() => new CreateSubscriptionResponse
|
|
{
|
|
SubscriptionId = ++subscriptionIdSeed
|
|
});
|
|
|
|
using var subscription = new Subscription(
|
|
NUnitTelemetryContext.Create(),
|
|
new() { PublishingEnabled = false, MaxMessageCount = 4 })
|
|
{
|
|
Session = session.Object,
|
|
RecoveryPolicy = SubscriptionRecoveryPolicy.RecreateOnUnsolicitedTransfer
|
|
};
|
|
await subscription.CreateAsync(ct).ConfigureAwait(false);
|
|
uint originalId = subscription.Id;
|
|
Assert.That(originalId, Is.GreaterThan(0u));
|
|
|
|
subscription.SaveMessageInCache(default, BuildStatusChangeMessage(
|
|
sequenceNumber: 1, StatusCodes.GoodSubscriptionTransferred));
|
|
|
|
// Allow the dispatch loop to observe the notification.
|
|
await Task.Delay(500, ct).ConfigureAwait(false);
|
|
|
|
Assert.That(subscription.Id, Is.EqualTo(originalId),
|
|
"Reconnecting session must keep the spec-strict path.");
|
|
}
|
|
|
|
private static NotificationMessage BuildStatusChangeMessage(
|
|
uint sequenceNumber, StatusCode status)
|
|
{
|
|
return new NotificationMessage
|
|
{
|
|
SequenceNumber = sequenceNumber,
|
|
NotificationData =
|
|
[
|
|
new ExtensionObject(new StatusChangeNotification
|
|
{
|
|
Status = status
|
|
})
|
|
]
|
|
};
|
|
}
|
|
|
|
private static async Task WaitForAsync(
|
|
Func<bool> predicate, TimeSpan timeout, CancellationToken ct)
|
|
{
|
|
DateTime deadline = DateTime.UtcNow + timeout;
|
|
while (!predicate() && DateTime.UtcNow < deadline)
|
|
{
|
|
await Task.Delay(25, ct).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
}
|