Files
UA-.NETStandard/tests/Opc.Ua.Server.Tests/ServerInternalDataTests.cs
T
romanett d98eb6b28c Fix server session defects found by a spec-checked review of CreateSession, ActivateSession, session lifecycle and continuation points (#4632)
# Description

Fixes defects found by a spec-checked review of the server session
implementation: CreateSession, ActivateSession, the session lifecycle
(per-request validation, CloseSession, Cancel, timeout, shutdown) and
session continuation points. Each finding was checked against OPC
10000-4 (§5.7, §7.9, §7.32), OPC 10000-5 (session diagnostics and audit
events) and OPC 10000-6 §6.8.2.

There is one commit per finding, and each commit message cites the spec
section. A max-effort code review of the fixes then found 15 more
issues, mostly regressions the first round introduced; they are fixed in
the `revfix` merges.

## What changed

**CreateSession**
- At MaxSessionCount the oldest session that was never activated is
closed to make room (§5.7.2.1). Sessions that are activated, already
closing, or have an activation in flight are never evicted. The victim
is claimed atomically, so a late activation commit on it is rejected.
- `requestedSessionTimeout` NaN is revised. A malformed `endpointUrl` no
longer fails the call with Bad_UnexpectedError. `serverUri` is ignored,
as §5.7.2.2 requires.
- Unnamed sessions get distinct names.
- Session diagnostics now report `MaxResponseMessageSize` and
`ServerUri`, and `SessionSecurityDiagnostics.Encoding` uses the
specified names ("UA Binary", "XML", "JSON").
- AuditCreateSessionEvent carries SecureChannelId and the request
certificate, also on failure.
- Every rejected CreateSession is counted, and only once. A rejection
during server startup no longer turns into Bad_UnexpectedError.
- If an `OnApplicationCertificateError` override accepts an error, the
certificate is now kept and the remaining checks still run. A
certificate accepted this way does not grant TrustedApplication or
application-based roles.

**ActivateSession**
- EphemeralKey handling follows OPC 10000-6 §6.8.2:
- A key used to decrypt a UserName or IssuedToken secret is retired and
replaced in the same commit.
  - A key that was not used is kept, and nothing is returned.
- ECDHPolicyUri is honoured; an unsupported policy gets
Bad_SecurityPolicyRejected.
  - Request parameters are no longer echoed back.
- After the activation commits, a failure or cancellation no longer
faults the response, which would hide the new server nonce. Post-commit
callbacks run uncancellably.
- An activation that overlaps a session close or timeout fails with
Bad_SessionClosed instead of returning Good.
- The PolicyId of an AnonymousIdentityToken is validated.
- Status codes now follow §5.7.3.3:
- Bad_ApplicationSignatureInvalid for a mismatched client-signature
algorithm
- Bad_UserSignatureInvalid for a mismatched X509 user-token signature
algorithm
- Bad_IdentityTokenInvalid for an undecodable token or an unknown policy
- `ClientUserIdOfSession` keeps the user of the first activation.

**Lifecycle and requests**
- Requests on an expired or closing session are rejected with
Bad_SessionClosed (§5.7.2.1). A CloseSession on an expired session that
is not yet closing is still processed as the client's close and honours
`deleteSubscriptions`.
- Closing a session aborts its outstanding requests with
Bad_SessionClosed. A request that registers after that abort pass is
rejected too.
- Cancel completes the matching requests with
Bad_RequestCancelledByClient (§5.7.5.1). It also reaches requests still
waiting in the request queue, but only when both timestamps prove the
request was sent earlier.
- There is one AuditCancelEvent per Cancel call, carrying the client's
audit entry id and user.
- Every close path goes through a single close claim, so
SessionAbortCount, SessionTimeoutCount and the "Session/Terminated" and
"Session/Timeout" audits are each counted once.
- Teardown that started cannot be cancelled. Requests rejected by
session validation are counted.

**Continuation points**
- HistoryRead returns Bad_NoContinuationPoints instead of evicting
points of the same request (§7.9). Only the request that saved a point
pins it, so points from prior requests can still be freed.
- Points saved by a HistoryRead that faults are released.
- Session-less Browse and BrowseNext no longer dereference a null
session.

## Behaviour changes reviewers should know about
- At the session cap, CreateSession evicts a non-activated session
instead of returning Bad_TooManySessions. The eviction is counted in
SessionAbortCount.
- Clients see Bad_SessionClosed for requests on expired or closing
sessions, and Bad_RequestCancelledByClient for cancelled requests.
- `ActivateSessionProcessAdditionalParameters` is now called on every
successful activation, with or without an AdditionalHeader.
- If an `OnApplicationCertificateError` override returns instead of
throwing, CreateSession now accepts the certificate.
- Timeout and termination audits are now raised after the session
teardown.
- The only public API changes are additive overloads in `AuditEvents`
and `RequestManager`.

**Known partial fixes:**
- The session's `maxResponseMessageSize` is reported but not enforced;
only the channel limit applies.
- With overlapping ActivateSession calls on one session, an earlier
response can still carry a key that a later activation has already
replaced. Closing that fully needs a new `ISession` member.

## Related Issues

- The Redundancy.Server session-mirroring findings from the same review
are tracked separately in #4627.

## Tests
- Regression tests cover each finding. The new files are:
  - `CreateSessionSpecRegressionTests`
  - `ActivateSessionValidationRegressionTests`
  - `StandardServerActivateSessionRegressionTests`
  - `SessionLifecycleRegressionTests`
  - `SessionRequestAdmissionRegressionTests`
  - `HistoryReadContinuationDispatchTests`
  
  Existing test classes were extended too.
- Local results on net10.0:
  - `Opc.Ua.Server.Tests`: 7883 passed / 0 failed / 5 skipped
- `Opc.Ua.Sessions.Tests`: 790 passed / 0 failed / 379 skipped (same as
master)
- net48 Release build of `Opc.Ua.Server.Tests`: 0 errors / 0 warnings.

## 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.
- [ ] 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.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 05:42:01 +02:00

1104 lines
42 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.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Opc.Ua.Tests;
namespace Opc.Ua.Server.Tests
{
[TestFixture]
[Category("ServerInternalData")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
[Parallelizable]
public class ServerInternalDataTests
{
private ServerProperties m_serverProperties;
private ApplicationConfiguration m_configuration;
private ServiceMessageContext m_messageContext;
private ITelemetryContext m_telemetry;
private static readonly string[] s_locales = ["de-DE"];
[SetUp]
public void SetUp()
{
m_telemetry = NUnitTelemetryContext.Create();
m_serverProperties = new ServerProperties
{
ProductName = "TestProduct",
ProductUri = "urn:test:product",
ManufacturerName = "TestManufacturer",
SoftwareVersion = "1.0.0",
BuildNumber = "100",
BuildDate = DateTime.UtcNow
};
m_configuration = new ApplicationConfiguration
{
ApplicationUri = "urn:test:server",
ApplicationName = "TestServer",
ServerConfiguration = new ServerConfiguration
{
BaseAddresses = [
"opc.tcp://localhost:4840",
"https://localhost:4841"
],
ServerProfileArray = [],
MaxBrowseContinuationPoints = 10,
MaxQueryContinuationPoints = 10,
MaxHistoryContinuationPoints = 10,
MaxSessionCount = 100,
MaxSubscriptionCount = 100
},
TransportQuotas = new TransportQuotas
{
MaxArrayLength = 65535,
MaxStringLength = 65535,
MaxByteStringLength = 65535
}
};
m_messageContext = ServiceMessageContext.Create(m_telemetry);
}
private ServerInternalData CreateServerInternalData()
{
return new ServerInternalData(
m_serverProperties,
m_configuration,
m_messageContext);
}
/// <summary>
/// The datastore surfaces the policy set the server was composed with,
/// so the sessions it creates resolve policy URIs against the policies
/// the application registered rather than the built-in set.
/// </summary>
[Test]
public void SecurityPolicyRegistryIsTheOneTheServerWasComposedWith()
{
using var registry = new SecurityPolicies(m_telemetry);
using var data = new ServerInternalData(
m_serverProperties,
m_configuration,
m_messageContext,
timeProvider: null,
securityPolicies: registry);
Assert.Multiple(() =>
{
Assert.That(data.SecurityPolicyRegistry, Is.SameAs(registry));
Assert.That(
(data as ISecurityPolicyRegistryProvider)?.SecurityPolicyRegistry,
Is.SameAs(registry));
});
}
/// <summary>
/// A server composed without a registry keeps the previous behaviour
/// and resolves against the built-in policy set.
/// </summary>
[Test]
public void SecurityPolicyRegistryFallsBackToTheBuiltInPolicySet()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.SecurityPolicyRegistry, Is.SameAs(SecurityPolicies.Default));
}
[Test]
public void RequestManagerIsUnsetUntilTheServerObjectIsCreated()
{
using ServerInternalData data = CreateServerInternalData();
// Request dispatch runs against this datastore from the moment it is published,
// which happens before the request manager exists. Callers must therefore treat
// the request manager as absent during that startup window.
Assert.That(data.RequestManager, Is.Null);
}
[Test]
public void ConstructorInitializesEndpointAddresses()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.EndpointAddresses, Is.Not.Null);
Assert.That(data.EndpointAddresses.Count(), Is.EqualTo(2));
}
[Test]
public void ConstructorInitializesMessageContext()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.MessageContext, Is.Not.Null);
Assert.That(data.MessageContext, Is.SameAs(m_messageContext));
}
[Test]
public void ConstructorInitializesNamespaceUris()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.NamespaceUris, Is.Not.Null);
}
[Test]
public void ConstructorInitializesServerUris()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.ServerUris, Is.Not.Null);
Assert.That(data.ServerUris.Count, Is.GreaterThan(0));
}
[Test]
public void ConstructorAddsApplicationUriToServerUris()
{
using ServerInternalData data = CreateServerInternalData();
string appUri = data.ServerUris.GetString(0);
Assert.That(appUri, Is.EqualTo("urn:test:server"));
}
[Test]
public void ConstructorInitializesFactory()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.Factory, Is.Not.Null);
}
[Test]
public void ConstructorInitializesTypeTree()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.TypeTree, Is.Not.Null);
}
[Test]
public void ConstructorInitializesDefaultSystemContext()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.DefaultSystemContext, Is.Not.Null);
}
[Test]
public void ConstructorFiltersInvalidBaseAddresses()
{
m_configuration.ServerConfiguration.BaseAddresses = [
"opc.tcp://localhost:4840",
"not-a-valid-uri",
"https://localhost:4841"
];
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.EndpointAddresses.Count(), Is.EqualTo(2));
}
[Test]
public void ConstructorHandlesEmptyBaseAddresses()
{
m_configuration.ServerConfiguration.BaseAddresses = [];
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.EndpointAddresses.Count(), Is.Zero);
}
[Test]
public void SetNodeManagerStoresNodeManager()
{
using ServerInternalData data = CreateServerInternalData();
var mockNodeManager = new Mock<IMasterNodeManager>();
mockNodeManager.Setup(m => m.DiagnosticsNodeManager).Returns((IDiagnosticsNodeManager)null);
mockNodeManager.Setup(m => m.ConfigurationNodeManager).Returns((IConfigurationNodeManager)null);
mockNodeManager.Setup(m => m.CoreNodeManager).Returns((ICoreNodeManager)null);
data.SetNodeManager(mockNodeManager.Object);
Assert.That(data.NodeManager, Is.SameAs(mockNodeManager.Object));
}
[Test]
public void FindNodeManagersYieldsAManagerOnBothFacesOnce()
{
using ServerInternalData data = CreateServerInternalData();
// A manager that implements INodeManager is its own synchronous adapter, so
// the same instance appears on both faces.
var manager = new Mock<IAsyncNodeManager>();
INodeManager syncFace = manager.As<INodeManager>().Object;
data.SetNodeManager(CreateMasterNodeManager([manager.Object], [syncFace]));
Assert.That(
data.FindNodeManagers<IAsyncNodeManager>().ToList(),
Is.EqualTo(new[] { manager.Object }).AsCollection);
}
[Test]
public void FindNodeManagersYieldsEveryDistinctManager()
{
using ServerInternalData data = CreateServerInternalData();
var asyncOnly = new Mock<IAsyncNodeManager>();
var syncManager = new Mock<INodeManager>();
IAsyncNodeManager syncAsAsync = syncManager.As<IAsyncNodeManager>().Object;
data.SetNodeManager(
CreateMasterNodeManager([asyncOnly.Object], [syncManager.Object]));
Assert.That(
data.FindNodeManagers<IAsyncNodeManager>().ToList(),
Is.EqualTo(new[] { asyncOnly.Object, syncAsAsync }).AsCollection);
}
[Test]
public void FindNodeManagersReturnsEmptyWhenNoNodeManagerIsSet()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.FindNodeManagers<IAsyncNodeManager>(), Is.Empty);
}
private static IMasterNodeManager CreateMasterNodeManager(
IAsyncNodeManager[] asyncNodeManagers,
INodeManager[] nodeManagers)
{
var mock = new Mock<IMasterNodeManager>();
mock.Setup(m => m.DiagnosticsNodeManager).Returns((IDiagnosticsNodeManager)null);
mock.Setup(m => m.ConfigurationNodeManager).Returns((IConfigurationNodeManager)null);
mock.Setup(m => m.CoreNodeManager).Returns((ICoreNodeManager)null);
mock.Setup(m => m.AsyncNodeManagers).Returns(asyncNodeManagers);
mock.Setup(m => m.NodeManagers).Returns(nodeManagers);
return mock.Object;
}
[Test]
public void SetMainNodeManagerFactoryStoresFactory()
{
using ServerInternalData data = CreateServerInternalData();
var mockFactory = new Mock<IMainNodeManagerFactory>();
data.SetMainNodeManagerFactory(mockFactory.Object);
Assert.That(data.MainNodeManagerFactory, Is.SameAs(mockFactory.Object));
}
[Test]
public void SetSessionManagerStoresManagers()
{
using ServerInternalData data = CreateServerInternalData();
var mockSessionManager = new Mock<ISessionManager>();
var mockSubscriptionManager = new Mock<ISubscriptionManager>();
data.SetSessionManager(mockSessionManager.Object, mockSubscriptionManager.Object);
Assert.That(data.SessionManager, Is.SameAs(mockSessionManager.Object));
Assert.That(data.SubscriptionManager, Is.SameAs(mockSubscriptionManager.Object));
}
[Test]
[Category("NodeManagerLifecycle")]
public void CloseSessionAsyncRemovesTheSessionEvenWhenTeardownThrows()
{
// Closing is marked on the Session and never cleared, so a Session that started
// closing must not be left registered and serving when teardown fails.
using ServerInternalData data = CreateServerInternalData();
var sessionId = new NodeId(Guid.NewGuid());
var mockSessionManager = new Mock<ISessionManager>();
var mockSubscriptionManager = new Mock<ISubscriptionManager>();
mockSessionManager.Setup(manager => manager.GetSessions()).Returns([]);
data.SetSessionManager(mockSessionManager.Object, mockSubscriptionManager.Object);
var mockNodeManager = new Mock<IMasterNodeManager>();
mockNodeManager
.Setup(manager => manager.SessionClosingAsync(
It.IsAny<OperationContext>(),
It.IsAny<NodeId>(),
It.IsAny<bool>(),
It.IsAny<CancellationToken>()))
.ThrowsAsync(new InvalidOperationException("teardown failed"));
data.SetNodeManager(mockNodeManager.Object);
Assert.That(
async () => await data
.CloseSessionAsync(null!, sessionId, deleteSubscriptions: true)
.ConfigureAwait(false),
Throws.InvalidOperationException);
mockSessionManager.Verify(
manager => manager.CloseSessionAsync(sessionId, It.IsAny<CancellationToken>()),
Times.Once);
}
[Test]
public void SetMonitoredItemQueueFactoryStoresFactory()
{
using ServerInternalData data = CreateServerInternalData();
var mockFactory = new Mock<IMonitoredItemQueueFactory>();
data.SetMonitoredItemQueueFactory(mockFactory.Object);
Assert.That(data.MonitoredItemQueueFactory, Is.SameAs(mockFactory.Object));
}
[Test]
public void DisposeKeepsCallerOwnedMonitoredItemQueueFactory()
{
ServerInternalData data = CreateServerInternalData();
var mockFactory = new Mock<IMonitoredItemQueueFactory>();
data.SetMonitoredItemQueueFactory(mockFactory.Object, ownsFactory: false);
data.Dispose();
mockFactory.Verify(factory => factory.Dispose(), Times.Never);
Assert.That(data.MonitoredItemQueueFactory, Is.Null);
}
[Test]
public void SetSubscriptionStoreStoresStore()
{
using ServerInternalData data = CreateServerInternalData();
var mockStore = new Mock<ISubscriptionStore>();
data.SetSubscriptionStore(mockStore.Object);
Assert.That(data.SubscriptionStore, Is.SameAs(mockStore.Object));
}
[Test]
public void SetAggregateManagerStoresManager()
{
using ServerInternalData data = CreateServerInternalData();
using var aggregateManager = new AggregateManager(data);
data.SetAggregateManager(aggregateManager);
Assert.That(data.AggregateManager, Is.SameAs(aggregateManager));
}
[Test]
public void BindingAfterTheBindPhaseIsRefused()
{
// A subsystem swapped underneath a running server would leave every component
// that already resolved it holding the previous instance.
using ServerInternalData data = CreateServerInternalData();
data.CompleteBindPhase();
Assert.Multiple(() =>
{
Assert.That(
() => data.SetNodeManager(new Mock<IMasterNodeManager>().Object),
Throws.TypeOf<ServiceResultException>()
.With.Property("StatusCode").EqualTo((StatusCode)StatusCodes.BadInvalidState));
Assert.That(
() => data.SetRoleManager(new Mock<IRoleManager>().Object),
Throws.TypeOf<ServiceResultException>());
Assert.That(
() => data.SetSubscriptionStore(new Mock<ISubscriptionStore>().Object),
Throws.TypeOf<ServiceResultException>());
Assert.That(
() => data.SetSessionManager(
new Mock<ISessionManager>().Object,
new Mock<ISubscriptionManager>().Object),
Throws.TypeOf<ServiceResultException>());
});
}
[Test]
public void TheRefusedBindNamesTheOperation()
{
using ServerInternalData data = CreateServerInternalData();
data.CompleteBindPhase();
Assert.That(
() => data.SetMonitoredItemQueueFactory(new Mock<IMonitoredItemQueueFactory>().Object),
Throws.TypeOf<ServiceResultException>()
.With.Message.Contains(nameof(ServerInternalData.SetMonitoredItemQueueFactory)));
}
[Test]
public void BindingIsAllowedUntilTheBindPhaseCloses()
{
using ServerInternalData data = CreateServerInternalData();
var first = new Mock<IRoleManager>();
var second = new Mock<IRoleManager>();
// Rebinding during startup stays legal; only binding afterwards is refused.
data.SetRoleManager(first.Object);
data.SetRoleManager(second.Object);
Assert.That(data.RoleManager, Is.SameAs(second.Object));
data.CompleteBindPhase();
Assert.That(
() => data.SetRoleManager(first.Object),
Throws.TypeOf<ServiceResultException>());
}
[Test]
public void CompletingTheBindPhaseTwiceIsHarmless()
{
using ServerInternalData data = CreateServerInternalData();
data.CompleteBindPhase();
Assert.That(() => data.CompleteBindPhase(), Throws.Nothing);
}
[Test]
public void UpdateServerDiagnosticsIsANoOpUntilTheServerObjectIsCreated()
{
using ServerInternalData data = CreateServerInternalData();
// ServerDiagnostics is only populated once the server object is created. A request
// rejected while the server is still starting (e.g. a CreateSession refused with
// Bad_ServerHalted) is counted before that; counting it must not turn the rejection
// into a NullReferenceException.
bool invoked = false;
Assert.DoesNotThrow(() => data.UpdateServerDiagnostics(diagnostics =>
{
invoked = true;
diagnostics.RejectedSessionCount++;
}));
Assert.That(invoked, Is.False);
}
[Test]
public void UpdateServerDiagnosticsThrowsOnNullUpdate()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(
() => data.UpdateServerDiagnostics(null!),
Throws.TypeOf<ArgumentNullException>());
}
/// <summary>
/// The read callback behind the ServerDiagnosticsSummary node must take the same lock
/// every writer takes through <c>UpdateServerDiagnostics</c>.
/// </summary>
/// <remarks>
/// It used to lock the payload — <c>lock (ServerDiagnostics)</c> — which is a different
/// monitor from the one the writers take, so a client's snapshot was never excluded
/// from a concurrent writer and <c>Variant.FromStructure</c> could walk the structure
/// mid-mutation. Reflection is used because the callback is private and is reachable
/// in production only through the diagnostic node manager's wiring.
/// </remarks>
[Test]
public async Task DiagnosticsReadBlocksWhileAWriterHoldsTheLockAsync()
{
using ServerInternalData data = CreateServerInternalData();
SetServerDiagnostics(data, new ServerDiagnosticsSummaryDataType());
using var writerEntered = new ManualResetEventSlim(false);
using var readerStarted = new ManualResetEventSlim(false);
using var releaseWriter = new ManualResetEventSlim(false);
Task writer = Task.Factory.StartNew(
() => data.UpdateServerDiagnostics(
_ =>
{
writerEntered.Set();
releaseWriter.Wait(TimeSpan.FromSeconds(30));
}),
CancellationToken.None,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
try
{
Assert.That(
writerEntered.Wait(TimeSpan.FromSeconds(30)),
Is.True,
"the writer never entered its critical section");
// LongRunning gets a dedicated thread, so the reader cannot simply fail to be
// scheduled behind the writer's blocked pool thread; and the timeout starts
// only once the reader has signalled that it is about to take the lock.
Task<Variant> reader = Task.Factory.StartNew(
() =>
{
readerStarted.Set();
return InvokeOnUpdateDiagnostics(data);
},
CancellationToken.None,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
Assert.That(
readerStarted.Wait(TimeSpan.FromSeconds(30)),
Is.True,
"the reader never started");
Assert.That(
reader.Wait(TimeSpan.FromMilliseconds(500)),
Is.False,
"the read callback completed while a writer held the diagnostics lock, so " +
"it is not taking the lock the writers take");
releaseWriter.Set();
Variant snapshot = await reader.ConfigureAwait(false);
Assert.That(snapshot.IsNull, Is.False, "the read callback produced no snapshot");
}
finally
{
releaseWriter.Set();
await writer.ConfigureAwait(false);
}
}
/// <summary>
/// The snapshot handed to a client must be detached from the live diagnostics, so a
/// later update cannot change a value the client has already read.
/// </summary>
/// <remarks>
/// Holding the lock is not on its own enough: <c>Variant.FromStructure</c> does not
/// copy by default, and the caller reads the fields long after the lock was released.
/// </remarks>
[Test]
public async Task DiagnosticsReadReturnsADetachedSnapshotAsync()
{
using ServerInternalData data = CreateServerInternalData();
SetServerDiagnostics(data, new ServerDiagnosticsSummaryDataType());
data.UpdateServerDiagnostics(diagnostics => diagnostics.RejectedSessionCount = 7);
Variant snapshot = await Task.Run(() => InvokeOnUpdateDiagnostics(data))
.ConfigureAwait(false);
data.UpdateServerDiagnostics(diagnostics => diagnostics.RejectedSessionCount = 99);
Assert.That(
snapshot.TryGetStructure(out ServerDiagnosticsSummaryDataType read),
Is.True);
Assert.That(
read.RejectedSessionCount,
Is.EqualTo(7u),
"a later update changed a value that had already been handed out");
}
private static void SetServerDiagnostics(
ServerInternalData data,
ServerDiagnosticsSummaryDataType diagnostics)
{
typeof(ServerInternalData)
.GetProperty(
nameof(ServerInternalData.ServerDiagnostics),
BindingFlags.Instance | BindingFlags.Public)!
.SetValue(data, diagnostics);
}
private static Variant InvokeOnUpdateDiagnostics(ServerInternalData data)
{
MethodInfo callback = typeof(ServerInternalData).GetMethod(
"OnUpdateDiagnostics",
BindingFlags.Instance | BindingFlags.NonPublic)!;
object[] arguments = [data.DefaultSystemContext, null!, Variant.Null];
callback.Invoke(data, arguments);
return (Variant)arguments[2];
}
[Test]
public void DiagnosticsEnabledReturnsFalseWhenNoDiagnosticsNodeManager()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.DiagnosticsEnabled, Is.False);
}
[Test]
public void ServerContextDefaultSystemContextIsTheServerSystemContext()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(
((IServerContext)data).DefaultSystemContext,
Is.SameAs(data.DefaultSystemContext));
}
[Test]
public void CreateSystemContextCarriesTheSessionIdentityAndLocales()
{
using ServerInternalData data = CreateServerInternalData();
var sessionId = new NodeId(Guid.NewGuid());
var identity = new UserIdentity(new AnonymousIdentityToken());
var session = new Mock<ISession>();
session.Setup(s => s.Id).Returns(sessionId);
session.Setup(s => s.Identity).Returns(identity);
session.Setup(s => s.PreferredLocales).Returns(s_locales);
ServerSystemContext created = data.CreateSystemContext(session.Object);
Assert.That(created, Is.Not.SameAs(data.DefaultSystemContext));
Assert.That(created.PreferredLocales.ToArray(), Is.EqualTo(s_locales));
Assert.That(created.NamespaceUris, Is.SameAs(data.NamespaceUris));
}
[Test]
public void CreateSystemContextThrowsOnNullSession()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(
() => data.CreateSystemContext(null!),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void FindPredefinedNodeReturnsNullBeforeTheDiagnosticsNodeManagerExists()
{
using ServerInternalData data = CreateServerInternalData();
// The datastore is published before the node managers are bound, so callers
// must tolerate an address space that is not there yet.
Assert.That(data.FindPredefinedNode<BaseObjectState>(ObjectIds.Server), Is.Null);
}
[Test]
public void TelemetryReturnsMessageContextTelemetry()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.Telemetry, Is.EqualTo(m_messageContext.Telemetry));
}
[Test]
public void IsRunningReturnsFalseWhenStatusNotInitialized()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.IsRunning, Is.False);
}
[Test]
public void DefaultAuditContextReturnsNonNull()
{
using ServerInternalData data = CreateServerInternalData();
ISystemContext auditContext = data.DefaultAuditContext;
Assert.That(auditContext, Is.Not.Null);
}
[Test]
public void DefaultAuditContextReturnsCopy()
{
using ServerInternalData data = CreateServerInternalData();
ISystemContext context1 = data.DefaultAuditContext;
ISystemContext context2 = data.DefaultAuditContext;
Assert.That(context1, Is.Not.SameAs(context2));
}
[Test]
public void NodeManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.NodeManager, Is.Null);
}
[Test]
public void SessionManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.SessionManager, Is.Null);
}
[Test]
public void SubscriptionManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.SubscriptionManager, Is.Null);
}
[Test]
public void EventManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.EventManager, Is.Null);
}
[Test]
public void ResourceManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.ResourceManager, Is.Null);
}
[Test]
public void RequestManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.RequestManager, Is.Null);
}
[Test]
public void AggregateManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.AggregateManager, Is.Null);
}
[Test]
public void CoreNodeManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.CoreNodeManager, Is.Null);
}
[Test]
public void DiagnosticsNodeManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.DiagnosticsNodeManager, Is.Null);
}
[Test]
public void ConfigurationNodeManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.ConfigurationNodeManager, Is.Null);
}
[Test]
public void MonitoredItemQueueFactoryIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.MonitoredItemQueueFactory, Is.Null);
}
[Test]
public void SubscriptionStoreIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.SubscriptionStore, Is.Null);
}
[Test]
public void ServerObjectIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.ServerObject, Is.Null);
}
[Test]
public void ServerDiagnosticsIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.ServerDiagnostics, Is.Null);
}
[Test]
public void DisposeDoesNotThrowWhenPropertiesAreNull()
{
using ServerInternalData data = CreateServerInternalData();
Assert.DoesNotThrow(data.Dispose);
}
[Test]
public void DisposeCanBeCalledTwice()
{
ServerInternalData data = CreateServerInternalData();
data.Dispose();
Assert.DoesNotThrow(data.Dispose);
}
[Test]
public void SetNodeManagerStoresDiagnosticsNodeManager()
{
using ServerInternalData data = CreateServerInternalData();
var mockDiag = new Mock<IDiagnosticsNodeManager>();
var mockNodeManager = new Mock<IMasterNodeManager>();
mockNodeManager.Setup(m => m.DiagnosticsNodeManager).Returns(mockDiag.Object);
mockNodeManager.Setup(m => m.ConfigurationNodeManager).Returns((IConfigurationNodeManager)null);
mockNodeManager.Setup(m => m.CoreNodeManager).Returns((ICoreNodeManager)null);
data.SetNodeManager(mockNodeManager.Object);
Assert.That(data.DiagnosticsNodeManager, Is.SameAs(mockDiag.Object));
}
[Test]
public void SetNodeManagerStoresCoreNodeManager()
{
using ServerInternalData data = CreateServerInternalData();
var mockCore = new Mock<ICoreNodeManager>();
var mockNodeManager = new Mock<IMasterNodeManager>();
mockNodeManager.Setup(m => m.DiagnosticsNodeManager).Returns((IDiagnosticsNodeManager)null);
mockNodeManager.Setup(m => m.ConfigurationNodeManager).Returns((IConfigurationNodeManager)null);
mockNodeManager.Setup(m => m.CoreNodeManager).Returns(mockCore.Object);
data.SetNodeManager(mockNodeManager.Object);
Assert.That(data.CoreNodeManager, Is.SameAs(mockCore.Object));
}
[Test]
public void SetNodeManagerStoresConfigurationNodeManager()
{
using ServerInternalData data = CreateServerInternalData();
var mockConfig = new Mock<IConfigurationNodeManager>();
var mockNodeManager = new Mock<IMasterNodeManager>();
mockNodeManager.Setup(m => m.DiagnosticsNodeManager).Returns((IDiagnosticsNodeManager)null);
mockNodeManager.Setup(m => m.ConfigurationNodeManager).Returns(mockConfig.Object);
mockNodeManager.Setup(m => m.CoreNodeManager).Returns((ICoreNodeManager)null);
data.SetNodeManager(mockNodeManager.Object);
Assert.That(data.ConfigurationNodeManager, Is.SameAs(mockConfig.Object));
}
[Test]
public void AuditingIsFalseByDefault()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.Auditing, Is.False);
}
[Test]
public void ModellingRulesManagerIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.ModellingRulesManager, Is.Null);
}
[Test]
public void MainNodeManagerFactoryIsNullBeforeSetup()
{
using ServerInternalData data = CreateServerInternalData();
Assert.That(data.MainNodeManagerFactory, Is.Null);
}
[Test]
public void EndpointAddressesParsesValidUrls()
{
using ServerInternalData data = CreateServerInternalData();
Uri[] addresses = [.. data.EndpointAddresses];
Assert.That(addresses[0].ToString(), Does.Contain("localhost:4840"));
Assert.That(addresses[1].ToString(), Does.Contain("localhost:4841"));
}
[Test]
public void ReportEventDoesNotThrowWhenServerObjectIsNull()
{
using ServerInternalData data = CreateServerInternalData();
var mockFilterTarget = new Mock<IFilterTarget>();
Assert.DoesNotThrow(() => data.ReportEvent(mockFilterTarget.Object));
}
[Test]
public void ReportAuditEventDoesNothingWhenAuditingDisabled()
{
using ServerInternalData data = CreateServerInternalData();
Assert.DoesNotThrow(() => data.ReportAuditEvent(data.DefaultSystemContext, null));
}
[Test]
public async Task DisposeAsyncCompletesAsync()
{
ServerInternalData data = CreateServerInternalData();
await data.DisposeAsync().ConfigureAwait(false);
Assert.That(data.RequestManager, Is.Null);
}
[Test]
public void DisposeReleasesManagedResourcesOnce()
{
ServerInternalData data = CreateServerInternalData();
DisposalCounts counts = ConfigureCountingDisposableState(data);
data.Dispose();
Assert.That(CaptureDisposedState(data), Is.All.True);
Assert.That(counts.Total, Is.EqualTo(5));
Assert.That(counts.SubscriptionAsync, Is.EqualTo(1));
Assert.That(counts.SubscriptionSync, Is.Zero);
}
[Test]
public async Task DisposeAndDisposeAsyncLeaveSameObservableStateAsync()
{
ServerInternalData syncData = CreateServerInternalData();
ServerInternalData asyncData = CreateServerInternalData();
ConfigureDisposableState(syncData);
ConfigureDisposableState(asyncData);
syncData.Dispose();
await asyncData.DisposeAsync().ConfigureAwait(false);
Assert.That(CaptureDisposedState(asyncData), Is.EqualTo(CaptureDisposedState(syncData)));
}
[Test]
public async Task DisposeAsyncIsIdempotentAsync()
{
ServerInternalData data = CreateServerInternalData();
await data.DisposeAsync().ConfigureAwait(false);
Assert.DoesNotThrowAsync(async () => await data.DisposeAsync().ConfigureAwait(false));
}
[Test]
public async Task DisposeAfterDisposeAsyncDoesNotDisposeTwiceAsync()
{
ServerInternalData data = CreateServerInternalData();
DisposalCounts counts = ConfigureCountingDisposableState(data);
await data.DisposeAsync().ConfigureAwait(false);
// The synchronous path shares the disposed guard with the asynchronous one, so a
// Dispose that follows DisposeAsync must be a no-op rather than releasing a second time.
Assert.DoesNotThrow(data.Dispose);
Assert.That(counts.Total, Is.EqualTo(5));
Assert.That(counts.SubscriptionAsync, Is.EqualTo(1));
Assert.That(counts.SubscriptionSync, Is.Zero);
}
[Test]
public async Task DisposeAsyncAfterDisposeDoesNotDisposeTwiceAsync()
{
ServerInternalData data = CreateServerInternalData();
DisposalCounts counts = ConfigureCountingDisposableState(data);
data.Dispose();
Assert.DoesNotThrowAsync(async () => await data.DisposeAsync().ConfigureAwait(false));
Assert.That(counts.Total, Is.EqualTo(5));
Assert.That(counts.SubscriptionAsync, Is.EqualTo(1));
Assert.That(counts.SubscriptionSync, Is.Zero);
}
private static void ConfigureDisposableState(ServerInternalData data)
{
var mockNodeManager = new Mock<IMasterNodeManager>();
mockNodeManager.Setup(m => m.DiagnosticsNodeManager).Returns((IDiagnosticsNodeManager)null);
mockNodeManager.Setup(m => m.ConfigurationNodeManager).Returns((IConfigurationNodeManager)null);
mockNodeManager.Setup(m => m.CoreNodeManager).Returns((ICoreNodeManager)null);
data.SetNodeManager(mockNodeManager.Object);
var mockSessionManager = new Mock<ISessionManager>();
var mockSubscriptionManager = new Mock<ISubscriptionManager>();
mockSubscriptionManager
.As<IAsyncDisposable>()
.Setup(manager => manager.DisposeAsync())
.Returns(default(ValueTask));
data.SetSessionManager(mockSessionManager.Object, mockSubscriptionManager.Object);
data.SetMonitoredItemQueueFactory(new Mock<IMonitoredItemQueueFactory>().Object);
data.SetRoleManager(new Mock<IRoleManager>().Object);
}
private static DisposalCounts ConfigureCountingDisposableState(ServerInternalData data)
{
var counts = new DisposalCounts();
var mockNodeManager = new Mock<IMasterNodeManager>();
mockNodeManager.Setup(manager => manager.DiagnosticsNodeManager).Returns((IDiagnosticsNodeManager)null);
mockNodeManager.Setup(manager => manager.ConfigurationNodeManager).Returns((IConfigurationNodeManager)null);
mockNodeManager.Setup(manager => manager.CoreNodeManager).Returns((ICoreNodeManager)null);
mockNodeManager.As<IDisposable>().Setup(manager => manager.Dispose()).Callback(() => counts.NodeManager++);
data.SetNodeManager(mockNodeManager.Object);
var mockSessionManager = new Mock<ISessionManager>();
mockSessionManager.Setup(manager => manager.Dispose()).Callback(() => counts.SessionManager++);
var mockSubscriptionManager = new Mock<ISubscriptionManager>();
mockSubscriptionManager.Setup(manager => manager.Dispose()).Callback(() => counts.SubscriptionSync++);
mockSubscriptionManager
.As<IAsyncDisposable>()
.Setup(manager => manager.DisposeAsync())
.Callback(() => counts.SubscriptionAsync++)
.Returns(default(ValueTask));
data.SetSessionManager(mockSessionManager.Object, mockSubscriptionManager.Object);
var mockQueueFactory = new Mock<IMonitoredItemQueueFactory>();
mockQueueFactory.Setup(factory => factory.Dispose()).Callback(() => counts.MonitoredItemQueueFactory++);
data.SetMonitoredItemQueueFactory(mockQueueFactory.Object);
var mockRoleManager = new Mock<IRoleManager>();
mockRoleManager.As<IDisposable>().Setup(manager => manager.Dispose()).Callback(() => counts.RoleManager++);
data.SetRoleManager(mockRoleManager.Object);
return counts;
}
private static bool[] CaptureDisposedState(ServerInternalData data)
{
return
[
data.RoleManager == null,
data.NodeManager == null,
data.DiagnosticsNodeManager == null,
data.ConfigurationNodeManager == null,
data.CoreNodeManager == null,
data.SessionManager == null,
data.SubscriptionManager == null,
data.MonitoredItemQueueFactory == null,
data.RequestManager == null
];
}
private sealed class DisposalCounts
{
public int Total =>
RoleManager +
NodeManager +
SessionManager +
SubscriptionSync +
SubscriptionAsync +
MonitoredItemQueueFactory;
public int RoleManager { get; set; }
public int NodeManager { get; set; }
public int SessionManager { get; set; }
public int SubscriptionSync { get; set; }
public int SubscriptionAsync { get; set; }
public int MonitoredItemQueueFactory { get; set; }
}
}
}