0c9e4f4e3d
Review I1: ConnectAsync, DisconnectAsync and Dispose each start a new connection epoch under _subscriptionLock. A keep-alive failover runs under the epoch it started in and abandons itself the moment it moves: it installs its session only if the epoch still matches (checked and assigned under the lock, otherwise the session is disposed), its state transitions are epoch-guarded, it stops trying endpoints, and it never flips a user's new connection to Disconnected. A late keep-alive from a previous connection's session starts no failover. Gate-driven tests cover the success and all-fail arms, the stale keep-alive, with a genuine failover as the positive control (4 of them red before). Minors: m8 only the CompareExchange winner sets LastFailover; m5 the endpoint loop stops early, and a failed Unsubscribe still drops its bookkeeping; m1 a read cancelled mid-paging releases its cursor (bounded token) and rethrows, and the cap release no longer rides the caller's token; m2 results are trimmed to the cap; m3 a non-positive cap is refused (CLI and UI already bound it to >= 1); m6 the subscription adapter's Dispose uses one bounded bridge for delete + detach. Refs #747 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
83 lines
3.5 KiB
C#
83 lines
3.5 KiB
C#
using Opc.Ua;
|
|
using ZB.MOM.WW.OtOpcUa.Client.Shared.Adapters;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Fakes;
|
|
|
|
internal sealed class FakeSessionFactory : ISessionFactory
|
|
{
|
|
private readonly List<FakeSessionAdapter> _createdSessions = [];
|
|
private readonly Queue<FakeSessionAdapter> _sessions = new();
|
|
|
|
/// <summary>Gets the number of times CreateSessionAsync has been called.</summary>
|
|
public int CreateCallCount { get; private set; }
|
|
/// <summary>Gets or sets a value indicating whether CreateSessionAsync should throw.</summary>
|
|
public bool ThrowOnCreate { get; set; }
|
|
/// <summary>Gets the endpoint URL from the last CreateSessionAsync call.</summary>
|
|
public string? LastEndpointUrl { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Optional gate that, when set, blocks <see cref="CreateSessionAsync" /> until completed.
|
|
/// Lets tests hold a failover loop in-flight to exercise re-entrancy.
|
|
/// </summary>
|
|
public TaskCompletionSource? CreateGate { get; set; }
|
|
|
|
/// <summary>Completed when a call observes <see cref="CreateGate" />.</summary>
|
|
public TaskCompletionSource CreateEntered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
/// <summary>Gets the list of sessions created via CreateSessionAsync.</summary>
|
|
public IReadOnlyList<FakeSessionAdapter> CreatedSessions => _createdSessions;
|
|
|
|
/// <summary>Creates a session asynchronously (fake implementation).</summary>
|
|
/// <param name="config">The application configuration.</param>
|
|
/// <param name="endpoint">The endpoint description.</param>
|
|
/// <param name="sessionName">The session name.</param>
|
|
/// <param name="sessionTimeoutMs">The session timeout in milliseconds.</param>
|
|
/// <param name="identity">The user identity.</param>
|
|
/// <param name="ct">Cancellation token.</param>
|
|
/// <returns>A task containing the session adapter.</returns>
|
|
public async Task<ISessionAdapter> CreateSessionAsync(
|
|
ApplicationConfiguration config, EndpointDescription endpoint, string sessionName,
|
|
uint sessionTimeoutMs, UserIdentity identity, CancellationToken ct)
|
|
{
|
|
CreateCallCount++;
|
|
LastEndpointUrl = endpoint.EndpointUrl;
|
|
|
|
// One-shot: the gate holds only the call that observed it, so a later call (e.g. a user's Connect while a
|
|
// failover is held) runs freely.
|
|
if (CreateGate is { } gate)
|
|
{
|
|
CreateGate = null;
|
|
CreateEntered.TrySetResult();
|
|
await gate.Task;
|
|
}
|
|
|
|
if (ThrowOnCreate)
|
|
throw new InvalidOperationException("FakeSessionFactory configured to fail.");
|
|
|
|
FakeSessionAdapter session;
|
|
if (_sessions.Count > 0)
|
|
session = _sessions.Dequeue();
|
|
else
|
|
session = new FakeSessionAdapter
|
|
{
|
|
EndpointUrl = endpoint.EndpointUrl,
|
|
ServerName = endpoint.Server?.ApplicationName?.Text ?? "FakeServer",
|
|
SecurityMode = endpoint.SecurityMode.ToString(),
|
|
SecurityPolicyUri = endpoint.SecurityPolicyUri ?? string.Empty
|
|
};
|
|
|
|
// Ensure endpoint URL matches
|
|
session.EndpointUrl = endpoint.EndpointUrl;
|
|
_createdSessions.Add(session);
|
|
return session;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enqueues a session adapter to be returned on the next call to CreateSessionAsync.
|
|
/// </summary>
|
|
/// <param name="session">The session adapter to enqueue.</param>
|
|
public void EnqueueSession(FakeSessionAdapter session)
|
|
{
|
|
_sessions.Enqueue(session);
|
|
}
|
|
} |