mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-21 09:06:37 +08:00
9b034f8291
# Description Nullable reference types are now enabled once, in `common.props`, and every project in the repository builds warning-clean with them. **Configuration** - `<Nullable>enable</Nullable>` lives in `common.props`. All per-project `<Nullable>` settings are removed: 178 project files (enable, annotations, and the `disable` in the three Aot test projects). - 419 per-file `#nullable enable`/`restore` directives and the 4 `#nullable disable` directives are removed. - Kept on purpose: - The 5 `<auto-generated>` schema files (`UANodeSet.cs`, `UA Type Dictionary.cs`, `OPCBinarySchema.cs`, `SecuredApplication.cs`, `PInvokeHelper.cs`) keep `#nullable enable`. The compiler ignores the project setting in generated code, so the directive is required there. - `tests/Opc.Ua.Interop.LegacyPeer` keeps `disable`. It builds against the 1.5.378 packages and does not import the repository props. - `docs/DeveloperGuide.md` and `.github/copilot-instructions.md` now describe the central setting. **Migration** (the 43 unset and 13 annotations-only projects, roughly 10k diagnostics) - **Production code: annotated at the declarations.** Covers `tools/Opc.Ua.SourceGeneration.Core`, `src/Opc.Ua.Lds.Server` and the LDS sample. - Members, parameters and returns are nullable where null actually flows. - Null-check helpers carry `[NotNullWhen]` (`IsNull`, `IsValidXmlName`, `IsMethodTypeDesign`). - `CoreUtils.Format`, `Utils.Format`, `ITemplateWriter.WriteLine` take `params object?[]`. `Template.AddReplacement` takes `string?` (rendering already skips null replacements). - `IRegisteredServerStore.RegisterAsync` returns `Task<RegistrationEntry?>`: it returns null when a registration is removed. - Dereferences backed by an invariant (for example, the validated model) use `!`, so runtime behaviour is unchanged. - **Behavioural changes in production code** (small, deliberate): - `NodeSetToModelDesign` throws `InvalidOperationException` when the file is not a NodeSet. Before, it threw a NullReferenceException on the next line. - `ModelDesignValidator` starts its lookup tables empty instead of null. A redundant `if (m_nodes == null) return;` guard is gone. - **Test code: minimal annotations.** - `?` on values that can be null, `!` where the test asserts non-null. - `= null!` for fixture members assigned in SetUp. - `TryGetStructure<T>(out T? x)` where the inferred `T?` broke the `IEncodeable` constraint. - A few redundant `== null` checks on structs (`NodeId`, `ArrayOf<T>`) folded into the adjacent `IsNull`/`Count` checks. **Notes for reviewers** - The diff is large (~1,500 files) but almost entirely mechanical: `?`, `!` and directive removal. The non-annotation changes are listed above. - Known follow-up, not done here: `IModelDesign.Namespaces/TargetNamespace/NamespaceUris` stay non-nullable even though the validator returns null before it loads a dictionary. Making them nullable would ripple through every generator call site. ## Related Issues - Fixes #4439 ## 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. - [ ] 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. - [ ] 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. The "no new warnings" box covers net10.0 and net48: full non-incremental builds of `UA.slnx`, `tools/SourceGeneration.slnx`, `tools/MigrationAnalyzer.slnx` and the Aot test projects finished with 0 warnings and 0 errors under the repository's warnings-as-errors settings. net9.0 and net8.0 also had no nullable diagnostics, but they were not rebuilt after the final fixes. Tests have not been run locally yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
645 lines
28 KiB
C#
645 lines
28 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.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Client;
|
|
using Opc.Ua.Client.TestFramework;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.Subscriptions.Classic.Tests
|
|
{
|
|
/// <summary>
|
|
/// Tests for subscription transfer scenarios.
|
|
/// Each fixture combines one <see cref="TransferType"/> with acknowledged or retained
|
|
/// origin notifications, initial values, and sequential publishing.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("Client")]
|
|
[Category("TransferSubscription")]
|
|
[SetCulture("en-us")]
|
|
[SetUICulture("en-us")]
|
|
[TestFixtureSource(nameof(TransferTypeArgs))]
|
|
[Parallelizable(ParallelScope.Fixtures)]
|
|
public class TransferSubscriptionTest : ClientTestFramework
|
|
{
|
|
/// <summary>
|
|
/// Fixture argument list: one fixture instance per <see cref="TransferType"/> value.
|
|
/// </summary>
|
|
public static readonly object[] TransferTypeArgs =
|
|
[
|
|
new object[] { TransferType.KeepOpen },
|
|
new object[] { TransferType.CloseSession },
|
|
new object[] { TransferType.DisconnectedAck },
|
|
new object[] { TransferType.DisconnectedRepublish },
|
|
new object[] { TransferType.DisconnectedRepublishDelayedAck }
|
|
];
|
|
|
|
/// <summary>
|
|
/// Describes how a subscription transfer scenario is set up.
|
|
/// </summary>
|
|
public enum TransferType
|
|
{
|
|
/// <summary>
|
|
/// The origin session remains open and gives up the subscription.
|
|
/// </summary>
|
|
KeepOpen,
|
|
|
|
/// <summary>
|
|
/// The origin session is gracefully closed with
|
|
/// DeleteSubscriptionsOnClose set to false.
|
|
/// </summary>
|
|
CloseSession,
|
|
|
|
/// <summary>
|
|
/// The origin session gets network disconnected; available sequence
|
|
/// numbers are just acknowledged.
|
|
/// </summary>
|
|
DisconnectedAck,
|
|
|
|
/// <summary>
|
|
/// The origin session gets network disconnected; available sequence
|
|
/// numbers are republished.
|
|
/// </summary>
|
|
DisconnectedRepublish,
|
|
|
|
/// <summary>
|
|
/// The origin session gets network disconnected; available sequence
|
|
/// numbers are republished but the target delays the acknowledgement.
|
|
/// </summary>
|
|
DisconnectedRepublishDelayedAck
|
|
}
|
|
|
|
[Test]
|
|
public void TransferredSequencesRemainAvailableForRepublish()
|
|
{
|
|
using var subscription = new Subscription(Session.DefaultSubscription) { RepublishAfterTransfer = true };
|
|
ArrayOf<uint> available = new uint[] { 9, 10, 11 }.ToArrayOf();
|
|
MethodInfo processTransfer = typeof(Subscription).GetMethod(
|
|
"ProcessTransferredSequenceNumbers", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
|
|
|
processTransfer.Invoke(subscription, [available]);
|
|
|
|
Assert.That(subscription.AvailableSequenceNumbers.ToArray(), Is.EquivalentTo(available.ToArray()!));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new fixture instance for the given <paramref name="transferType"/>.
|
|
/// </summary>
|
|
public TransferSubscriptionTest(TransferType transferType)
|
|
: base(Utils.UriSchemeOpcTcp)
|
|
{
|
|
m_transferType = transferType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Start one server per fixture instance and establish a single shared session.
|
|
/// Tests create their own sessions independently.
|
|
/// </summary>
|
|
[OneTimeSetUp]
|
|
public override Task OneTimeSetUpAsync()
|
|
{
|
|
// Use a shared session for namespace URI lookups; tests create their own sessions.
|
|
SingleSession = true;
|
|
MaxChannelCount = 1000;
|
|
ClientFixtureSubscriptionEngineFactory = ClassicSubscriptionEngineFactory.Instance;
|
|
return OneTimeSetUpCoreAsync(securityNone: true);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[OneTimeTearDown]
|
|
public override Task OneTimeTearDownAsync()
|
|
{
|
|
return base.OneTimeTearDownAsync();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[SetUp]
|
|
public override Task SetUpAsync()
|
|
{
|
|
return base.SetUpAsync();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[TearDown]
|
|
public override Task TearDownAsync()
|
|
{
|
|
return base.TearDownAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Transfer subscriptions for all combinations of
|
|
/// <paramref name="sendInitialValues"/>, <paramref name="sequentialPublishing"/>,
|
|
/// and <paramref name="retainOriginNotifications"/>.
|
|
/// </summary>
|
|
[Test]
|
|
[Combinatorial]
|
|
[Order(811)]
|
|
[Parallelizable]
|
|
public Task TransferSubscriptionAsync(
|
|
[Values] bool sendInitialValues,
|
|
[Values] bool sequentialPublishing,
|
|
[Values] bool retainOriginNotifications)
|
|
{
|
|
return InternalTransferSubscriptionAsync(
|
|
m_transferType,
|
|
sendInitialValues,
|
|
sequentialPublishing,
|
|
retainOriginNotifications);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Debug variant that repeats the transfer test 30 times (explicit, not for normal CI runs).
|
|
/// </summary>
|
|
[Test]
|
|
[Combinatorial]
|
|
[Order(812)]
|
|
[Explicit]
|
|
public async Task TransferSubscriptionDebugAsync(
|
|
[Values] bool sendInitialValues,
|
|
[Values] bool sequentialPublishing,
|
|
[Values] bool retainOriginNotifications)
|
|
{
|
|
const int loopCount = 30;
|
|
for (int i = 0; i < loopCount; i++)
|
|
{
|
|
await InternalTransferSubscriptionAsync(
|
|
m_transferType,
|
|
sendInitialValues,
|
|
sequentialPublishing,
|
|
retainOriginNotifications).ConfigureAwait(false);
|
|
|
|
TestContext.Out.WriteLine("===========================================");
|
|
TestContext.Out.WriteLine("===========================================");
|
|
TestContext.Out.WriteLine($"Completed iteration {i + 1} of {loopCount}.");
|
|
TestContext.Out.WriteLine("===========================================");
|
|
TestContext.Out.WriteLine("===========================================");
|
|
}
|
|
}
|
|
|
|
private async Task InternalTransferSubscriptionAsync(
|
|
TransferType transferType,
|
|
bool sendInitialValues,
|
|
bool sequentialPublishing,
|
|
bool retainOriginNotifications)
|
|
{
|
|
using ISession originSession = await ClientFixture
|
|
.ConnectAsync(ServerUrl, SecurityPolicies.Basic256Sha256)
|
|
.ConfigureAwait(false);
|
|
string filePath = Path.GetTempFileName();
|
|
try
|
|
{
|
|
await InternalTransferSubscriptionAsync(
|
|
originSession,
|
|
transferType,
|
|
sendInitialValues,
|
|
sequentialPublishing,
|
|
retainOriginNotifications,
|
|
filePath).ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
}
|
|
|
|
private async Task InternalTransferSubscriptionAsync(
|
|
ISession originSession,
|
|
TransferType transferType,
|
|
bool sendInitialValues,
|
|
bool sequentialPublishing,
|
|
bool retainOriginNotifications,
|
|
string filePath)
|
|
{
|
|
const int kTestSubscriptions = 5;
|
|
const int kDelay = 2_000;
|
|
const int kQueueSize = 10;
|
|
|
|
if (retainOriginNotifications)
|
|
{
|
|
originSession.PublishSequenceNumbersToAcknowledge += DeferSubscriptionAcknowledge;
|
|
}
|
|
|
|
bool originSessionOpen = transferType == TransferType.KeepOpen;
|
|
|
|
// create subscriptions
|
|
var originSubscriptions = new SubscriptionCollection(kTestSubscriptions);
|
|
int[] originSubscriptionCounters = new int[kTestSubscriptions];
|
|
int[] originSubscriptionFastDataCounters = new int[kTestSubscriptions];
|
|
int[] targetSubscriptionCounters = new int[kTestSubscriptions];
|
|
int[] targetSubscriptionFastDataCounters = new int[kTestSubscriptions];
|
|
int[] originSubscriptionTransferred = new int[kTestSubscriptions];
|
|
var staticTargetNotifications = new ConcurrentQueue<DataChangeNotification>();
|
|
int targetKeepAliveCount = 0;
|
|
bool expectRepublish = retainOriginNotifications &&
|
|
transferType >= TransferType.DisconnectedRepublish;
|
|
int expectedStaticBatchCount = (expectRepublish ? 1 : 0) + (sendInitialValues ? 1 : 0);
|
|
using var subscriptionTemplate = new TestableSubscription(originSession.DefaultSubscription)
|
|
{
|
|
PublishingInterval = 1_000,
|
|
LifetimeCount = 30,
|
|
KeepAliveCount = 1,
|
|
PublishingEnabled = false,
|
|
RepublishAfterTransfer = transferType >= TransferType.DisconnectedRepublish,
|
|
SequentialPublishing = sequentialPublishing
|
|
};
|
|
|
|
await CreateSubscriptionsAsync(
|
|
originSession,
|
|
subscriptionTemplate,
|
|
originSubscriptions,
|
|
originSubscriptionCounters,
|
|
originSubscriptionFastDataCounters,
|
|
kTestSubscriptions,
|
|
kQueueSize).ConfigureAwait(false);
|
|
|
|
if (TransferType.KeepOpen == transferType)
|
|
{
|
|
foreach (Subscription subscription in originSubscriptions)
|
|
{
|
|
subscription.PublishStatusChanged += (s, e) =>
|
|
{
|
|
TestContext.Out.WriteLine(
|
|
$"PublishStatusChanged: {s.Session!.SessionId}-{s.Id}-{e.Status}");
|
|
if ((e.Status & PublishStateChangedMask.Transferred) != 0)
|
|
{
|
|
// subscription transferred
|
|
Interlocked.Increment(ref originSubscriptionTransferred[(int)s.Handle!]);
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
foreach (Subscription subscription in originSubscriptions)
|
|
{
|
|
await subscription.SetPublishingModeAsync(true).ConfigureAwait(false);
|
|
}
|
|
await TestPolling.WaitUntilAsync(
|
|
() => Enumerable.Range(0, kTestSubscriptions).All(index =>
|
|
Volatile.Read(ref originSubscriptionCounters[index]) >=
|
|
originSubscriptions[index].MonitoredItemCount &&
|
|
Volatile.Read(ref originSubscriptionFastDataCounters[index]) > 0),
|
|
timeoutMessage: "The origin did not deliver every initial monitored-item value.")
|
|
.ConfigureAwait(false);
|
|
|
|
Subscription staticOrigin = originSubscriptions[0];
|
|
if (!retainOriginNotifications)
|
|
{
|
|
await TestPolling.WaitUntilAsync(
|
|
() => staticOrigin.AvailableSequenceNumbers.IsEmpty,
|
|
timeoutMessage: "The origin's initial notification was not acknowledged by the server.")
|
|
.ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
Assert.That(
|
|
staticOrigin.AvailableSequenceNumbers.ToArray(),
|
|
Is.EqualTo([staticOrigin.SequenceNumber]));
|
|
}
|
|
NotificationMessage originStaticMessage = staticOrigin.Notifications.Single();
|
|
Assert.That(originStaticMessage.NotificationData.Count, Is.EqualTo(1));
|
|
Assert.That(
|
|
originStaticMessage.NotificationData[0].TryGetValue(out DataChangeNotification? originStaticData),
|
|
Is.True);
|
|
Assert.That(originStaticData!.MonitoredItems.Count, Is.EqualTo(staticOrigin.MonitoredItemCount));
|
|
|
|
// close session, do not delete subscription
|
|
if (transferType != TransferType.KeepOpen)
|
|
{
|
|
originSession.DeleteSubscriptionsOnClose = false;
|
|
|
|
// save with custom Subscription subclass information
|
|
originSession.Save(filePath);
|
|
|
|
if (transferType == TransferType.CloseSession)
|
|
{
|
|
// graceful close
|
|
StatusCode close = await originSession.CloseAsync().ConfigureAwait(false);
|
|
Assert.That(ServiceResult.IsGood(close), Is.True);
|
|
}
|
|
else
|
|
{
|
|
// force a socket dispose, to emulate network disconnect
|
|
// without closing session on server
|
|
originSession.TransportChannel.Dispose();
|
|
}
|
|
}
|
|
|
|
// wait
|
|
await Task.Delay(kDelay).ConfigureAwait(false);
|
|
|
|
// close session, do not delete subscription
|
|
if (transferType > TransferType.CloseSession)
|
|
{
|
|
StatusCode closeResult2 = await originSession
|
|
.CloseAsync()
|
|
.ConfigureAwait(false);
|
|
}
|
|
|
|
// create target session
|
|
using ISession targetSession = await ClientFixture
|
|
.ConnectAsync(ServerUrl, SecurityPolicies.Basic256Sha256)
|
|
.ConfigureAwait(false);
|
|
if (transferType == TransferType.DisconnectedRepublishDelayedAck)
|
|
{
|
|
targetSession.PublishSequenceNumbersToAcknowledge += DeferSubscriptionAcknowledge;
|
|
}
|
|
|
|
// restore client state
|
|
var transferSubscriptions = new SubscriptionCollection();
|
|
if (transferType != TransferType.KeepOpen)
|
|
{
|
|
// load subscriptions for transfer
|
|
transferSubscriptions.AddRange(targetSession.Load(filePath, true));
|
|
}
|
|
else
|
|
{
|
|
transferSubscriptions.AddRange((SubscriptionCollection)originSubscriptions.Clone());
|
|
}
|
|
|
|
for (int ii = 0; ii < transferSubscriptions.Count; ii++)
|
|
{
|
|
Subscription subscription = transferSubscriptions[ii];
|
|
if (originSessionOpen)
|
|
{
|
|
Assert.That(targetSession.AddSubscription(subscription), Is.True);
|
|
}
|
|
subscription.Handle = ii;
|
|
subscription.FastDataChangeCallback = (s, notification, _) =>
|
|
{
|
|
TestContext.Out.WriteLine(
|
|
"FastDataChangeHandlerTarget: {0}-{1}-{2}",
|
|
s.Id,
|
|
notification.SequenceNumber,
|
|
notification.MonitoredItems.Count);
|
|
int index = (int)s.Handle!;
|
|
Interlocked.Increment(ref targetSubscriptionFastDataCounters[index]);
|
|
if (index == 0)
|
|
{
|
|
staticTargetNotifications.Enqueue(notification);
|
|
}
|
|
};
|
|
if (ii == 0)
|
|
{
|
|
subscription.FastKeepAliveCallback = (_, _) => Interlocked.Increment(ref targetKeepAliveCount);
|
|
}
|
|
foreach (MonitoredItem monitoredItem in subscription.MonitoredItems)
|
|
{
|
|
monitoredItem.Notification += (item, _) =>
|
|
{
|
|
Interlocked.Increment(ref targetSubscriptionCounters[(int)subscription.Handle]);
|
|
foreach (DataValue value in item.DequeueValues())
|
|
{
|
|
TestContext.Out.WriteLine(
|
|
"Tra:{0}: {1:20}, {2}, {3}, {4}",
|
|
subscription.Id,
|
|
item.DisplayName,
|
|
value.WrappedValue,
|
|
value.SourceTimestamp,
|
|
value.StatusCode);
|
|
}
|
|
};
|
|
}
|
|
subscription.StateChanged += (s, e) =>
|
|
TestContext.Out.WriteLine($"StateChanged: {s.Session!.SessionId}-{s.Id}-{e.Status}");
|
|
subscription.PublishStatusChanged += (s, e) =>
|
|
TestContext.Out.WriteLine($"PublishStatusChanged: {s.Session!.SessionId}-{s.Id}-{e.Status}");
|
|
}
|
|
|
|
// transfer restored subscriptions
|
|
bool result = await targetSession
|
|
.TransferSubscriptionsAsync(transferSubscriptions, sendInitialValues)
|
|
.ConfigureAwait(false);
|
|
Assert.That(result, Is.True);
|
|
|
|
// validate results
|
|
for (int ii = 0; ii < transferSubscriptions.Count; ii++)
|
|
{
|
|
Assert.That(transferSubscriptions[ii].Created, Is.True);
|
|
}
|
|
|
|
TestContext.Out
|
|
.WriteLine("TargetSession is now SessionId={0}", targetSession.SessionId);
|
|
|
|
await TestPolling.WaitUntilAsync(
|
|
() => staticTargetNotifications.Count >= expectedStaticBatchCount &&
|
|
Volatile.Read(ref targetKeepAliveCount) > 0 &&
|
|
Enumerable.Range(1, kTestSubscriptions - 1).All(index =>
|
|
Volatile.Read(ref targetSubscriptionCounters[index]) > 0 &&
|
|
Volatile.Read(ref targetSubscriptionFastDataCounters[index]) > 0),
|
|
timeoutMessage: "The target did not deliver expected notifications and a keepalive.")
|
|
.ConfigureAwait(false);
|
|
|
|
if (TransferType.KeepOpen == transferType)
|
|
{
|
|
await TestPolling.WaitUntilAsync(
|
|
() => Enumerable.Range(0, kTestSubscriptions).All(index =>
|
|
Volatile.Read(ref originSubscriptionTransferred[index]) > 0),
|
|
timeoutMessage: "The origin did not receive every subscription-transferred notification.")
|
|
.ConfigureAwait(false);
|
|
foreach (Subscription subscription in originSubscriptions)
|
|
{
|
|
Assert.That(originSubscriptionTransferred[(int)subscription.Handle!], Is.EqualTo(1));
|
|
}
|
|
}
|
|
|
|
// stop publishing
|
|
foreach (Subscription subscription in transferSubscriptions)
|
|
{
|
|
TestContext.Out.WriteLine(
|
|
"SetPublishingMode(false) for SessionId={0}, SubscriptionId={1}",
|
|
subscription.Session!.SessionId,
|
|
subscription.Id);
|
|
await subscription.SetPublishingModeAsync(false).ConfigureAwait(false);
|
|
}
|
|
|
|
// validate expected counts
|
|
for (int jj = 0; jj < kTestSubscriptions; jj++)
|
|
{
|
|
TestContext.Out.WriteLine(
|
|
"-- Subscription {0}: OriginCounts {1}, TargetCounts {2} ",
|
|
jj,
|
|
originSubscriptionCounters[jj],
|
|
targetSubscriptionCounters[jj]);
|
|
TestContext.Out.WriteLine(
|
|
"-- Subscription {0}: OriginFastDataCounts {1}, TargetFastDataCounts {2} ",
|
|
jj,
|
|
originSubscriptionFastDataCounters[jj],
|
|
targetSubscriptionFastDataCounters[jj]);
|
|
uint monitoredItemCount = transferSubscriptions[jj].MonitoredItemCount;
|
|
uint originExpectedCount = monitoredItemCount;
|
|
uint targetExpectedCount = sendInitialValues ? monitoredItemCount : 0;
|
|
if (jj == 0)
|
|
{
|
|
targetExpectedCount = (uint)expectedStaticBatchCount * monitoredItemCount;
|
|
Assert.That(originSubscriptionCounters[jj], Is.EqualTo(originExpectedCount));
|
|
Assert.That(originSubscriptionFastDataCounters[jj], Is.EqualTo(1));
|
|
Assert.That(targetSubscriptionCounters[jj], Is.EqualTo(targetExpectedCount));
|
|
Assert.That(targetSubscriptionFastDataCounters[jj], Is.EqualTo(expectedStaticBatchCount));
|
|
AssertStaticNotifications(
|
|
staticTargetNotifications, originStaticData, expectRepublish, sendInitialValues);
|
|
}
|
|
else
|
|
{
|
|
// dynamic nodes, expect only one set of changes, another one if send initial values was set
|
|
Assert.That(originSubscriptionCounters[jj], Is.GreaterThanOrEqualTo(originExpectedCount));
|
|
Assert.That(targetSubscriptionCounters[jj], Is.GreaterThanOrEqualTo(targetExpectedCount));
|
|
}
|
|
}
|
|
|
|
int[] countsBeforeResume =
|
|
[
|
|
.. Enumerable.Range(0, kTestSubscriptions)
|
|
.Select(index => Volatile.Read(ref targetSubscriptionCounters[index]))
|
|
];
|
|
int[] batchesBeforeResume =
|
|
[
|
|
.. Enumerable.Range(0, kTestSubscriptions)
|
|
.Select(index => Volatile.Read(ref targetSubscriptionFastDataCounters[index]))
|
|
];
|
|
int keepAlivesBeforeResume = Volatile.Read(ref targetKeepAliveCount);
|
|
|
|
// restart publishing
|
|
foreach (Subscription subscription in transferSubscriptions)
|
|
{
|
|
TestContext.Out.WriteLine(
|
|
"SetPublishingMode(true) for SessionId={0}, SubscriptionId={1}",
|
|
subscription.Session!.SessionId,
|
|
subscription.Id);
|
|
await subscription.SetPublishingModeAsync(true).ConfigureAwait(false);
|
|
}
|
|
|
|
await TestPolling.WaitUntilAsync(
|
|
() => Volatile.Read(ref targetKeepAliveCount) > keepAlivesBeforeResume &&
|
|
Enumerable.Range(1, kTestSubscriptions - 1).All(index =>
|
|
Volatile.Read(ref targetSubscriptionCounters[index]) > countsBeforeResume[index] &&
|
|
Volatile.Read(ref targetSubscriptionFastDataCounters[index]) > batchesBeforeResume[index]),
|
|
timeoutMessage: "Transferred subscriptions did not resume dynamic values and static keepalives.")
|
|
.ConfigureAwait(false);
|
|
|
|
// validate expected counts
|
|
for (int jj = 0; jj < kTestSubscriptions; jj++)
|
|
{
|
|
TestContext.Out.WriteLine(
|
|
"-- Subscription {0}: OriginCounts {1}, TargetCounts {2} ",
|
|
jj,
|
|
originSubscriptionCounters[jj],
|
|
targetSubscriptionCounters[jj]);
|
|
TestContext.Out.WriteLine(
|
|
"-- Subscription {0}: OriginFastDataCounts {1}, TargetFastDataCounts {2} ",
|
|
jj,
|
|
originSubscriptionFastDataCounters[jj],
|
|
targetSubscriptionFastDataCounters[jj]);
|
|
|
|
if (jj == 0)
|
|
{
|
|
Assert.That(targetSubscriptionCounters[jj], Is.EqualTo(countsBeforeResume[jj]));
|
|
Assert.That(targetSubscriptionFastDataCounters[jj], Is.EqualTo(batchesBeforeResume[jj]));
|
|
AssertStaticNotifications(
|
|
staticTargetNotifications, originStaticData, expectRepublish, sendInitialValues);
|
|
}
|
|
else
|
|
{
|
|
Assert.That(targetSubscriptionCounters[jj], Is.GreaterThan(countsBeforeResume[jj]));
|
|
Assert.That(targetSubscriptionFastDataCounters[jj], Is.GreaterThan(batchesBeforeResume[jj]));
|
|
}
|
|
}
|
|
|
|
targetSession.DeleteSubscriptionsOnClose = true;
|
|
|
|
// close sessions
|
|
StatusCode closeResult = await targetSession.CloseAsync().ConfigureAwait(false);
|
|
Assert.That(ServiceResult.IsGood(closeResult), Is.True);
|
|
|
|
if (originSessionOpen)
|
|
{
|
|
closeResult = await originSession.CloseAsync().ConfigureAwait(false);
|
|
Assert.That(ServiceResult.IsGood(closeResult), Is.True);
|
|
}
|
|
}
|
|
|
|
private static void AssertStaticNotifications(
|
|
ConcurrentQueue<DataChangeNotification> received,
|
|
DataChangeNotification origin,
|
|
bool expectRepublish,
|
|
bool sendInitialValues)
|
|
{
|
|
DataChangeNotification[] notifications = [.. received];
|
|
Assert.That(
|
|
notifications.Count(notification => notification.SequenceNumber == origin.SequenceNumber),
|
|
Is.EqualTo(expectRepublish ? 1 : 0),
|
|
"Only a retained origin message requested for republish should reuse its sequence number.");
|
|
Assert.That(
|
|
notifications.Count(notification => notification.SequenceNumber != origin.SequenceNumber),
|
|
Is.EqualTo(sendInitialValues ? 1 : 0),
|
|
"Requested initial values must arrive in one new notification message.");
|
|
Assert.That(notifications.Select(notification => notification.SequenceNumber), Is.Unique);
|
|
|
|
Dictionary<uint, MonitoredItemNotification> originalItems =
|
|
origin.MonitoredItems.ToArray()!.ToDictionary(item => item.ClientHandle);
|
|
foreach (DataChangeNotification notification in notifications)
|
|
{
|
|
Assert.That(notification.SequenceNumber, Is.GreaterThanOrEqualTo(origin.SequenceNumber));
|
|
Assert.That(
|
|
notification.MonitoredItems.ToArray()!.Select(item => item.ClientHandle),
|
|
Is.EquivalentTo(originalItems.Keys));
|
|
foreach (MonitoredItemNotification item in notification.MonitoredItems)
|
|
{
|
|
Assert.That(
|
|
originalItems.TryGetValue(item.ClientHandle, out MonitoredItemNotification? original),
|
|
Is.True);
|
|
Assert.That(item.Value.WrappedValue, Is.EqualTo(original!.Value.WrappedValue));
|
|
Assert.That(item.Value.StatusCode, Is.EqualTo(original.Value.StatusCode));
|
|
Assert.That(item.Value.SourceTimestamp, Is.EqualTo(original.Value.SourceTimestamp));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event handler to defer publish response sequence number acknowledge.
|
|
/// </summary>
|
|
private static void DeferSubscriptionAcknowledge(
|
|
ISession session,
|
|
PublishSequenceNumbersToAcknowledgeEventArgs e)
|
|
{
|
|
// for testing do not ack any sequence numbers
|
|
e.DeferredAcknowledgementsToSend.Clear();
|
|
e.AcknowledgementsToSend.Clear();
|
|
}
|
|
|
|
private readonly TransferType m_transferType;
|
|
}
|
|
}
|