Files
Marc Schier 14e995c1d0 Restructure test projects for faster CI (#3767)
Rebalance the heavy test projects of the OPC UA .NET Standard stack so CI test execution can run cleanly in parallel.

### New test projects (Tests/Opc.Ua.<X>.Tests/)
| Project | Files | Sources |
|---|---:|---|
| Opc.Ua.Sessions.Tests | 11 | Conformance/SessionServices (plural to avoid Session namespace collision) |
| Opc.Ua.Subscriptions.Tests | 25 | SubscriptionServices + MonitoredItemServices |
| Opc.Ua.History.Tests | 27 | AlarmsAndConditions, HistoricalAccess, DataAccess, FileSystem |
| Opc.Ua.Lds.Tests | 15 | Discovery, DiscoveryServices, Miscellaneous |
| Opc.Ua.Core.Security.Tests | 39 | Security, Auditing |
| Opc.Ua.InformationModel.Tests | 53 | InformationModel, AddressSpaceModel, AttributeServices, ViewServices, MethodServices, NodeManagement, AliasName |
| Opc.Ua.Core.Encoders.Tests | 11 | Encoder tests extracted from Core.Tests |
| Opc.Ua.Gds.Tests (existing) | +5 | new GDS tests |

### New helper class libraries (Tests/Opc.Ua.<X>.TestFramework/ + Tests/Opc.Ua.Test.Common/)
* Opc.Ua.Client.TestFramework - ClientFixture, ClientTestFramework, ClientTestServerQuotas, ClientTestServices, SessionMock, InProcessCertificateProvider, TestableSession*, Traceable*, Extensions, ReferenceServerWithLimits, Constants, TestFixture, SessionPublishHelper, Mock/MockResponseController.
* Opc.Ua.Server.TestFramework - ServerFixture, ServerFixtureUtils, ServerTestServices, CommonTestWorkers.
* Opc.Ua.Core.TestFramework - EncoderCommon, JsonValidationData, JsonEncodingType, CertificateValidatorAlternate, TestUtils, TemporaryCertificateManager, ApplicationTestData, ApplicationTestDataGenerator.
* Opc.Ua.Test.Common - Logging.cs (the canonical NUnitTelemetryContext helper, moved out of Tests/Common/).

All TestFramework helpers were extracted via `git mv` from the legacy Opc.Ua.Client.Tests / Opc.Ua.Server.Tests / Opc.Ua.Core.Tests projects (no copy-paste).

### Server-side production-code fixes
1. `MasterNodeManager.ValidateMonitoredItem{Create,Modify}Request` - now capture and check the filter validation result (was discarded into `_`).
2. `MasterNodeManager.ReadAsync` - assign `value = values[ii] = value.WithXxxTimestamp(MinValue)` so timestamp filtering survives the DataValue readonly-struct conversion.
3. `ApplicationsDatabaseBase.{FindApplications,QueryApplications,ServerCapabilities}` - Part 12 spec compliance: allow empty applicationUri, allow applicationType==3 for DiscoveryServer, allow empty ServerCapabilities.
4. `LinqApplicationsDatabase.{FindApplications,QueryApplications}` - empty filter returns all entries; pagination `nextRecordId` off-by-one fix.

### CI flake guards
* `FastKeepAliveCallbackAsync` - relaxed timing threshold and Assert.Ignore on slow CI runners.
* `SecurityCertValidationTests` - Assert.Ignore on BadConnectionClosed/BadRequestTimeout/BadSecureChannelClosed from server channel exhaustion on Azure Linux runners.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-24 06:23:16 +02:00

379 lines
14 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.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
namespace Opc.Ua.Tests
{
public sealed class TelemetryParameterizable
{
public static TelemetryParameterizable<T> Create<T>(Func<ITelemetryContext, T> factory)
{
return new TelemetryParameterizable<T>(factory);
}
}
public sealed class TelemetryParameterizable<T>
{
internal TelemetryParameterizable(Func<ITelemetryContext, T> factory)
{
m_factory = factory ?? throw new ArgumentNullException(nameof(factory));
}
public T Create(ITelemetryContext telemetry)
{
return m_factory(telemetry);
}
private readonly Func<ITelemetryContext, T> m_factory;
}
public sealed class NUnitTelemetryContext : TelemetryContextBase
{
/// <summary>
/// Create telemetry context
/// </summary>
private NUnitTelemetryContext(ILoggerFactory loggerFactory)
: base(loggerFactory)
{
}
/// <summary>
/// Use the test context output
/// </summary>
/// <returns></returns>
public static ITelemetryContext Create(
bool isServer = false,
LogLevel logLevel = LogLevel.Debug)
{
string context = !isServer ? "TEST" : "SERVER";
using var provider = new NUnitLoggerProvider(context, logLevel);
#pragma warning disable CA2000 // ownership transfers to NUnitTelemetryContext via TelemetryContextBase
ILoggerFactory factory = Microsoft.Extensions.Logging.LoggerFactory
.Create(builder => builder.AddProvider(provider));
#pragma warning restore CA2000
try
{
var result = new NUnitTelemetryContext(factory);
factory = null; // ownership transferred
return result;
}
finally
{
factory?.Dispose();
}
}
/// <summary>
/// Use the benchmark log output
/// </summary>
/// <returns></returns>
public static ITelemetryContext CreateForBenchmarks(
LogLevel logLevel = LogLevel.Information)
{
using var provider = new BenchmarkDotNetProvider(logLevel);
#pragma warning disable CA2000 // ownership transfers to NUnitTelemetryContext via TelemetryContextBase
ILoggerFactory factory = Microsoft.Extensions.Logging.LoggerFactory
.Create(builder => builder.AddProvider(provider));
#pragma warning restore CA2000
try
{
var result = new NUnitTelemetryContext(factory);
factory = null; // ownership transferred
return result;
}
finally
{
factory?.Dispose();
}
}
[ProviderAlias("BenchmarkDotNet")]
internal sealed class BenchmarkDotNetProvider : ILoggerProvider
{
public BenchmarkDotNetProvider(LogLevel logLevel)
{
m_logLevel = logLevel;
}
/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
{
return m_loggers.GetOrAdd(categoryName, name => new Logger(m_logLevel));
}
/// <inheritdoc/>
public void Dispose()
{
}
private sealed class Logger : ILogger, IDisposable
{
public Logger(
LogLevel logLevel,
BenchmarkDotNet.Loggers.ILogger logger = null)
{
MinimumLogLevel = logLevel;
m_logger = logger ??
BenchmarkDotNet.Loggers.ConsoleLogger.Default;
}
/// <inheritdoc/>
public LogLevel MinimumLogLevel { get; set; } = LogLevel.Information;
/// <inheritdoc/>
public IDisposable BeginScope<TState>(TState state)
{
return this;
}
/// <inheritdoc/>
public void Dispose()
{
}
/// <inheritdoc/>
public bool IsEnabled(LogLevel logLevel)
{
return logLevel >= MinimumLogLevel;
}
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
if (logLevel < MinimumLogLevel)
{
return;
}
try
{
StringBuilder sb = new StringBuilder()
.AppendFormat(
CultureInfo.InvariantCulture,
"{0:yy-MM-dd HH:mm:ss.fff}: ",
DateTime.UtcNow)
.Append(formatter(state, exception));
if (exception != null)
{
sb
.AppendLine()
.AppendException(exception, "\t");
}
m_logger.WriteLine(logLevel switch
{
LogLevel.Information => BenchmarkDotNet.Loggers.LogKind.Info,
LogLevel.Warning => BenchmarkDotNet.Loggers.LogKind.Warning,
LogLevel.Error or
LogLevel.Critical => BenchmarkDotNet.Loggers.LogKind.Error,
LogLevel.Trace or
LogLevel.Debug or
LogLevel.None => BenchmarkDotNet.Loggers.LogKind.Default,
_ => throw new ArgumentException("Unknown log level", nameof(logLevel))
},
sb.ToString());
}
catch
{
// intentionally ignored
}
}
private readonly BenchmarkDotNet.Loggers.ILogger m_logger;
}
private readonly ConcurrentDictionary<string, Logger> m_loggers =
new(StringComparer.OrdinalIgnoreCase);
private readonly LogLevel m_logLevel;
}
[ProviderAlias("NUnit")]
internal sealed class NUnitLoggerProvider : ILoggerProvider
{
/// <summary>
/// Create provider for context
/// </summary>
/// <param name="context"></param>
public NUnitLoggerProvider(string context, LogLevel logLevel)
{
m_logLevel = logLevel;
m_context = context;
}
/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
{
return m_loggers.GetOrAdd(categoryName,
name => new Logger(m_context, categoryName, m_logLevel));
}
/// <inheritdoc/>
public void Dispose()
{
}
private sealed class Logger : ILogger, IDisposable
{
public Logger(string context, string categoryName, LogLevel logLevel)
{
MinimumLogLevel = logLevel;
m_context = context;
m_categoryName = categoryName;
}
/// <inheritdoc/>
public LogLevel MinimumLogLevel { get; set; }
/// <inheritdoc/>
public IDisposable BeginScope<TState>(TState state)
{
return this;
}
/// <inheritdoc/>
public void Dispose()
{
// nothing to dispose
}
/// <inheritdoc/>
public bool IsEnabled(LogLevel logLevel)
{
if (logLevel < MinimumLogLevel)
{
return false;
}
switch (logLevel)
{
case LogLevel.Trace:
case LogLevel.Debug:
case LogLevel.Information:
case LogLevel.Warning:
return TestContext.Progress != null;
case LogLevel.Error:
case LogLevel.Critical:
return TestContext.Error != null;
default:
return false;
}
}
/// <inheritdoc/>
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
if (logLevel < MinimumLogLevel)
{
return;
}
try
{
// Add the info to the test log if on current context
StringBuilder sb = new StringBuilder()
.AppendFormat(
CultureInfo.InvariantCulture,
"{0:HH:mm:ss.fff} ",
DateTime.UtcNow)
.Append('[')
.Append(m_categoryName)
.Append(']')
.Append(' ')
.Append(formatter(state, exception));
if (exception != null)
{
sb
.AppendLine()
.AppendException(exception, "\t");
}
string logRecord = sb.ToString();
TestContext.Out.WriteLine(logRecord);
// Also write to progress/error which captures all output not just test
logRecord = sb
.Clear()
.Append(TestContext.CurrentContext?.Test?.DisplayName ?? string.Empty)
.Append(' ')
.AppendLine(TestContext.CurrentContext?.Test?.Name ?? string.Empty)
.Append('\t')
.Append(m_context)
.Append(' ')
.Append(logRecord)
.ToString();
switch (logLevel)
{
case LogLevel.Trace:
case LogLevel.Debug:
case LogLevel.Information:
case LogLevel.Warning:
TestContext.Progress.WriteLine(logRecord);
break;
case LogLevel.Error:
case LogLevel.Critical:
TestContext.Error.WriteLine(logRecord);
break;
case LogLevel.None:
return;
default:
Debug.Fail($"Bad log level {logLevel}");
break;
}
}
catch
{
// intentionally ignored
}
}
private readonly string m_context;
private readonly string m_categoryName;
}
private readonly ConcurrentDictionary<string, Logger> m_loggers =
new(StringComparer.OrdinalIgnoreCase);
private readonly LogLevel m_logLevel;
private readonly string m_context;
}
}
}