mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-29 15:11:53 +08:00
### Test project reorganization
Splits the integration- vs unit-test boundary along **project
boundaries**
and introduces a runtime-tier split for subscription tests so the
mainline
CI no longer drags in stress / durable suites.
**New projects:**
- `Opc.Ua.Stress.Tests` ΓÇö cross-area stress tier (designed to
absorb
session / channel / pub-sub stress in future). Seeded with the 6
subscription stress classes (`SubscriptionDepthTests`,
`SubscriptionBasicDepthTests`, `MonitoredItemDepthTests`,
`SubscriptionTransferDepthTests`, `SubscriptionPublishTooManyTests`,
`SubscriptionMultipleTests`). **Excluded from mainline CI; runs
nightly.**
- `Opc.Ua.Subscriptions.Durable.Tests` ΓÇö durable-subscription tier
(needs `DurableSubscriptionsEnabled` + storage-backed lifetime).
Receives `SubscriptionDurableTests` plus the migrated
`DurableSubscriptionTest` and `DurableSubscriptionTestDebug`.
**Excluded from mainline CI; runs nightly.**
- `Opc.Ua.Features.Tests` ΓÇö cross-feature integration tier.
Receives the
`[OneTimeSetUp]`-bootstraps-its-own-server integrations for AliasNames
(Part 17), Roles (Part 18), FileSystem (Part 5 §C / Part 20), and
NodeCache. Also absorbs the misplaced
`Tests/Opc.Ua.History.Tests/FileSystemTests.cs`.
**Client.Tests cleanup (~28 files moved):**
- `Opc.Ua.Sessions.Tests` (existing) gains 10 session integration files
plus the 4 session benchmark files inline (`LoadTest`,
`RequestHeaderTest`, `TraceableRequestHeaderTest`,
`SecurityPolicyBenchmarks`).
- `Opc.Ua.Subscriptions.Tests` (existing) gains 4 subscription
integration files plus `PooledNotificationBenchmarks` inline.
- `Opc.Ua.Client.ComplexTypes.Tests` (existing) gains all
`ComplexTypes/*` integration files. The Client.Tests copy of
`TypeSystemClientTest.cs` (a stale near-duplicate) is removed.
- Benchmarks **stay inline** as `[Test]` + BenchmarkDotNet dual-use; no
new benchmark project (per the approved plan).
**CI pipeline:**
- `.azurepipelines/test.yml` now filters
`TestCategory!=LongRunning&TestCategory!=Stress` on the mainline run.
- `-ExcludeFileName` now accepts a comma-separated list. The mainline
matrix excludes the 3 nightly-tier project files alongside the
existing `Opc.Ua.Aot.Tests.csproj`.
- `get-matrix.ps1` enhanced to support multi-pattern excludes.
`InternalsVisibleTo` extended on `Opc.Ua.Client` and `Opc.Ua.Server` to
grant the new test projects access. All affected projects build clean
(`0 Error`) for `net10.0`.
### Fix 8 skipped tests flagged as suspected bugs
Triage of every `[Ignore]` / `[Explicit]` test in `Tests/**` surfaced 8
entries labelled as suspected production bugs. After inspection:
- **1 real production bug** ΓÇö the
`CancellationTokenSource.CancelAsync`
polyfill for netstandard2.0 / netfx was running cancellation callbacks
inline on the caller's thread, deadlocking dispose paths
(`SubscriptionManager.DisposeAsync`, etc.) when a callback awaits on
the caller's `SynchronizationContext`. Fixed at the polyfill root by
hoisting `Cancel()` onto the ThreadPool via `Task.Run`, matching real
.NET 8+ semantics. Caveat (deferred `IsCancellationRequested`
transition) documented inline; all in-repo callers use
`await cts.CancelAsync()` so the deviation is invisible. Test
`DrainAsyncWaitsForInFlightPublishToCompleteAsync` un-ignored ΓÇö
verified passes on net48 (943 ms) and net10.0 (1 s).
- **2 flaky tests stabilized** ΓÇö both
`ValidateMetaDataUpdateTimeNonZeroIsSentPeriodically` (MQTT JSON &
UADP) now use `Stopwatch.GetTimestamp()` instead of `DateTime.Now`,
compute deviation against the **median** interval (not the configured
cadence, so a startup hiccup doesn't skew the run), and use a
proportional `max(interval / 4, 50 ms)` tolerance. `[Ignore]` removed,
`[Category("LongRunning")]` retained.
- **1 mis-labelled "bug"** ΓÇö
`UnorderedMessagesWouldBeLostForSequentialPublishingAsync` asserts the
*documented* lossy trade-off of strict sequential publishing.
`[Explicit]`
removed and renamed to
`OldMessagesAbandonedAfterRepublishTimeoutInSequentialModeAsync`; it's
now a CI-verified regression test for the WAI behavior.
- **4 false positives** ΓÇö `JsonEncoderTestsOld.cs` (16 kLOC) was
excluded from compilation via `<Compile Remove>` and never reached the
runner. The 4 `[Ignore("ProductionBugSuspected")]` markers inside were
orphaned source from a previous JsonEncoder API. Active coverage lives
in `JsonEncoderTests.cs` and `JsonValidationData` fixtures. Deleted
the orphan file and the now-redundant `<Compile Remove>` line.
190 lines
8.3 KiB
C#
190 lines
8.3 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 NUnit.Framework;
|
|
using NUnit.Framework.Interfaces;
|
|
using Opc.Ua.Security.Certificates;
|
|
using Opc.Ua.Tests;
|
|
|
|
// Apply leak tracking action to all test fixtures in the assembly.
|
|
[assembly: Opc.Ua.Core.Tests.CoreLeakDetectionFixtureAction]
|
|
|
|
namespace Opc.Ua.Core.Tests
|
|
{
|
|
/// <summary>
|
|
/// Assembly-level setup/teardown that verifies no Certificate
|
|
/// instances are leaked during the test run.
|
|
/// </summary>
|
|
[SetUpFixture]
|
|
public class CoreLeakDetectionSetup
|
|
{
|
|
private static readonly System.Collections.Concurrent.ConcurrentDictionary<string, (long created, long disposed)>
|
|
s_fixtureLeaks = new();
|
|
|
|
[OneTimeSetUp]
|
|
public void GlobalSetup()
|
|
{
|
|
Certificate.ResetLeakCounters();
|
|
}
|
|
|
|
[OneTimeTearDown]
|
|
public void GlobalTeardown()
|
|
{
|
|
// Force GC to finalize any abandoned certificates. Multiple
|
|
// cycles ensure that finalizable objects whose finalizer
|
|
// creates new garbage are themselves collected. The sweep is
|
|
// bounded by a watchdog so a stuck finalizer cannot hang the
|
|
// test host indefinitely during assembly teardown.
|
|
if (!LeakDetectionHelpers.TryRunFinalizerSweep())
|
|
{
|
|
Assert.Warn(
|
|
$"Finalizer sweep exceeded {LeakDetectionHelpers.DefaultFinalizerSweepTimeout.TotalSeconds:0}s " +
|
|
"watchdog; at least one finalizer is stuck. Leak counts below may be inaccurate.");
|
|
}
|
|
|
|
long leaked = Certificate.InstancesLeaked;
|
|
string summary = $"CoreLeakDetectionSetup: tracked {s_fixtureLeaks.Count} fixtures, " +
|
|
$"created={Certificate.InstancesCreated}, " +
|
|
$"disposed={Certificate.InstancesDisposed}, leaked={leaked}";
|
|
Console.WriteLine(summary);
|
|
try
|
|
{
|
|
string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "core-leak-summary.txt");
|
|
System.IO.File.AppendAllText(path, summary + "\n");
|
|
// Dump ALL fixtures sorted by net delta (positive first, negative last).
|
|
System.IO.File.AppendAllText(path, "\nALL FIXTURES (positive net first):\n");
|
|
foreach (KeyValuePair<string, (long created, long disposed)> kv in s_fixtureLeaks
|
|
.OrderByDescending(kv => kv.Value.created - kv.Value.disposed))
|
|
{
|
|
long net = kv.Value.created - kv.Value.disposed;
|
|
System.IO.File.AppendAllText(path,
|
|
$" net={net,4} created={kv.Value.created,4} disposed={kv.Value.disposed,4} {kv.Key}\n");
|
|
}
|
|
#if DEBUG
|
|
// In Debug builds, also dump the allocation stack of any
|
|
// live (reachable) certificate that still has a positive
|
|
// refcount. These are the actual leaks.
|
|
System.IO.File.AppendAllText(path, "\nLIVE LEAKED CERTIFICATES (DEBUG):\n");
|
|
foreach ((string thumbprint, int refCount, DateTime createdAt, string stackTrace) in
|
|
Certificate.EnumerateLiveCertificates())
|
|
{
|
|
System.IO.File.AppendAllText(path,
|
|
$"\n Thumbprint={thumbprint}, RefCount={refCount}, " +
|
|
$"CreatedAt={createdAt:O}\n StackTrace:\n{stackTrace}\n");
|
|
}
|
|
// Certificates whose finaliser ran while their refcount was
|
|
// still > 0 (AddRef without matching Dispose).
|
|
System.IO.File.AppendAllText(path, "\nFINALIZED-WITH-LEAKED-REF CERTIFICATES (DEBUG):\n");
|
|
foreach ((string thumbprint, DateTime createdAt, string stackTrace) in
|
|
Certificate.EnumerateFinalizedLeakedCertificates())
|
|
{
|
|
System.IO.File.AppendAllText(path,
|
|
$"\n Thumbprint={thumbprint}, " +
|
|
$"CreatedAt={createdAt:O}\n StackTrace:\n{stackTrace}\n");
|
|
}
|
|
#endif
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
if (leaked > 0)
|
|
{
|
|
string details = string.Join("\n",
|
|
s_fixtureLeaks
|
|
.Where(kv => kv.Value.created > kv.Value.disposed)
|
|
.OrderByDescending(kv => kv.Value.created - kv.Value.disposed)
|
|
.Select(kv => $" {kv.Key}: leaked={kv.Value.created - kv.Value.disposed} (created={kv.Value.created}, disposed={kv.Value.disposed})"));
|
|
|
|
Assert.Warn(
|
|
$"Certificate leak detected: {leaked} instance(s) created " +
|
|
$"but not disposed (created={Certificate.InstancesCreated}, " +
|
|
$"disposed={Certificate.InstancesDisposed}).\n" +
|
|
$"Per-fixture breakdown:\n{details}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Records the certificate creation/disposal delta for a fixture.
|
|
/// </summary>
|
|
public static void RecordFixture(string fixtureName, long created, long disposed)
|
|
{
|
|
s_fixtureLeaks.AddOrUpdate(
|
|
fixtureName,
|
|
(created, disposed),
|
|
(_, prev) => (prev.created + created, prev.disposed + disposed));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Records Certificate counter deltas per individual test so leaks can
|
|
/// be attributed to a specific fixture or test. Lightweight: no per-
|
|
/// test GC; the global teardown is responsible for the final GC.
|
|
/// </summary>
|
|
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
|
|
public sealed class CoreLeakDetectionFixtureActionAttribute : Attribute, ITestAction
|
|
{
|
|
private static readonly System.Threading.AsyncLocal<(long created, long disposed, string name)> s_baseline
|
|
= new();
|
|
|
|
public ActionTargets Targets => ActionTargets.Test;
|
|
|
|
public void BeforeTest(ITest test)
|
|
{
|
|
if (!test.IsSuite)
|
|
{
|
|
string parent = test.Parent?.FullName ?? test.FullName;
|
|
s_baseline.Value = (
|
|
Certificate.InstancesCreated,
|
|
Certificate.InstancesDisposed,
|
|
parent);
|
|
}
|
|
}
|
|
|
|
public void AfterTest(ITest test)
|
|
{
|
|
if (!test.IsSuite)
|
|
{
|
|
long createdDelta = Certificate.InstancesCreated - s_baseline.Value.created;
|
|
long disposedDelta = Certificate.InstancesDisposed - s_baseline.Value.disposed;
|
|
if (createdDelta > 0 || disposedDelta > 0)
|
|
{
|
|
CoreLeakDetectionSetup.RecordFixture(
|
|
s_baseline.Value.name,
|
|
createdDelta,
|
|
disposedDelta);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|