Files
lmxopcua/tests/ZB.MOM.WW.LmxOpcUa.Client.Shared.Tests/Fakes/FakeSessionFactory.cs
Joseph Doherty a2883b82d9 Add cross-platform OPC UA client stack: shared library, CLI tool, and Avalonia UI
Implements Client.Shared (IOpcUaClientService with connection lifecycle, failover,
browse, read/write, subscriptions, alarms, history, redundancy), Client.CLI (8 CliFx
commands mirroring tools/opcuacli-dotnet), and Client.UI (Avalonia desktop app with
tree browser, read/write, subscriptions, alarms, and history tabs). All three target
.NET 10 and are covered by 249 unit tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 15:49:42 -04:00

57 lines
1.9 KiB
C#

using Opc.Ua;
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Adapters;
namespace ZB.MOM.WW.LmxOpcUa.Client.Shared.Tests.Fakes;
internal sealed class FakeSessionFactory : ISessionFactory
{
private readonly Queue<FakeSessionAdapter> _sessions = new();
private readonly List<FakeSessionAdapter> _createdSessions = new();
public int CreateCallCount { get; private set; }
public bool ThrowOnCreate { get; set; }
public string? LastEndpointUrl { get; private set; }
/// <summary>
/// Enqueues a session adapter to be returned on the next call to CreateSessionAsync.
/// </summary>
public void EnqueueSession(FakeSessionAdapter session)
{
_sessions.Enqueue(session);
}
public IReadOnlyList<FakeSessionAdapter> CreatedSessions => _createdSessions;
public Task<ISessionAdapter> CreateSessionAsync(
ApplicationConfiguration config, EndpointDescription endpoint, string sessionName,
uint sessionTimeoutMs, UserIdentity identity, CancellationToken ct)
{
CreateCallCount++;
LastEndpointUrl = endpoint.EndpointUrl;
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 Task.FromResult<ISessionAdapter>(session);
}
}