4 Commits

Author SHA1 Message Date
Marc Schier a3ae6db71a [test] Accommodate non-blocking lease Dispose with thread-safe listeners and polling helpers
After commit 2a4f4f601 made ManagedTransportChannelLease.Dispose()
fire-and-forget on the thread pool (to fix a sync-context deadlock),
several test helpers exposed pre-existing races that the previous
synchronous Dispose had been masking:

  - ChannelMetricListener.Measurements was a plain List<T>; the
    metric callbacks (OnLong/Double/MeasurementRecorded) fire from
    arbitrary threadpool threads, so concurrent Add on one thread and
    enumerate on the test thread threw "Collection was modified" in
    FormatMeasurements. Change to ConcurrentQueue<MeasurementRecord>
    and use snapshot-stable enumeration in HasMeasurement +
    FormatMeasurements.
  - ChannelEventListener.Events had the same race against
    EventWritten callbacks; same fix (ConcurrentQueue).

Four tests asserted state that is now produced asynchronously by the
lease teardown task, so they need to poll before the hard assertions:
  - MetricsAreEmittedForChannelLifetimeAsync (wait for the
    opcua.channel.close metric)
  - DiscoveryClientCreateAsyncSharesSessionChannelAndReleasesLeaseAsync
    (wait for Moq.Verify on CloseAsync)
  - EventSourceFiresStateTransitionsAsync (wait for
    ParticipantDetached + ChannelClosed EventSource events)
  - FailoverWithDifferentEndpointSwapsLeaseAsync (wait for the lease
    State transition AND the underlying CloseCount increment)

Three new helpers consolidate the polling pattern:
  - WaitForMeasurementAsync (existing — used for the metric path)
  - WaitForMockInvocationAsync (new — catches Moq.MockException and
    re-runs verify until budget exhausted; final verify is allowed
    to throw)
  - WaitForConditionAsync (new — generic state poll with description)

All helpers use a 2 s budget with 25 ms polling intervals, matching
the existing pattern in MetricsAreEmittedForReconnectAndGateWaitAsync
(53ff029df).

Verification:
  - Source-tree builds clean on net10.0 (Stack/Opc.Ua.Core,
    Libraries/Opc.Ua.Client, Applications/McpServer all 0 W, 0 E)
  - Local .NET 10 SDK install became corrupted mid-session
    (C:\Program Files\dotnet\sdk\10.0.300 emptied of all but the
    Roslyn subfolder), blocking a final test rebuild for the LAST
    edit to FailoverWithDifferentEndpointSwapsLease (the
    state-and-close combined poll). The two previous test edits in
    this commit were rebuilt and verified at 4/4 pass + 10x stability
    (the FailoverWithDifferentEndpoint flake reproduced before the
    final edit and motivates the combined-condition poll). CI is the
    canonical re-verifier for the final tweak.
2026-06-11 12:26:55 +02:00
Marc Schier 53ff029df8 Fix MetricsAreEmittedForReconnectAndGateWaitAsync race on fast runners
CI failure (AzDO build 14553 log 874, net48 Windows): the test asserted
that opcua.channel.reconnect.duration was emitted but only saw the
attempts / gate.wait / open / active measurements.

Root cause: race in the TEST (production behavior is correct).
ChannelEntry.RunReconnectCycleAsync records RecordReconnectAttempt
BEFORE calling tcs.TrySetResult(true), but RecordReconnectDuration
is invoked in the finally block AFTER TrySetResult returns control.
The test awaits the reconnect task — which completes on TrySetResult
— and then immediately asserts both metrics. On a fast net48 runner
the test thread reaches the duration assertion before the finally
block runs, so the histogram measurement is missing.

Add a WaitForMeasurementAsync helper that polls for the duration
measurement with a 2-second budget before the existing hard assertion
fires. The hard assertion is preserved so the test still fails if
the metric is genuinely never emitted.

Verified locally: net48 5/5 runs pass (was 0/1 in CI), net10 1/1 (no
regression).
2026-06-10 08:52:41 +02:00
Marc Schier ff33cdd18a Phase E: auto-reset faulted ChannelEntry on ReconnectAsync(lease, ...)
Per review feedback (ChannelManagerTransparentReconnectIntegrationTests.cs:121
"Implement!"). Adds transparent recovery: when ReconnectAsync is called
on a lease whose Entry has transitioned to Faulted or Closed, the manager
swaps the lease's Entry reference to a freshly-created one for the same
key, preserves the participant, and proceeds with the reconnect cycle.
Back-off scales via the existing reconnect-policy GetDelay(SwapCount).

- Production: ClientChannelManager.SwapFaultedEntryAsync(lease, ct);
  ManagedTransportChannelLease.SwapEntry(...) + SwapCount tracking;
  ChannelEntry.ReattachParticipant(lease, factory) for refcount preservation.
- Unit test: ClientChannelManagerManagedTests covers the swap flow with
  a fake transport.
- Integration: ChannelManagerExhaustionEscalatesAndRecoversWhenServerReturns
  un-Ignored and implemented — verifies the full transparent exhaustion +
  recovery cycle against a real ManagedSession.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-06 19:38:35 +02:00
Marc Schier 2efd335c63 Phase C: move ClientChannelManagerManagedTests out of Core.Tests
Per review feedback (Opc.Ua.Core.Tests.csproj:52) — Core.Tests should
not have a ProjectReference to Opc.Ua.Client. Audit confirmed only
ClientChannelManagerManagedTests.cs uses Opc.Ua.Client types; moved
it to Opc.Ua.Client.Tests/Stack/Client/ with namespace update, then
dropped the Opc.Ua.Client ProjectReference from Opc.Ua.Core.Tests.csproj.

Verified: Core.Tests + Client.Tests build clean; moved tests pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-06 14:28:45 +02:00