test: validate grpc data connections with local docker and opcua cli

This commit is contained in:
Joseph Doherty
2026-11-17 22:01:26 -05:00
parent 1df29a3124
commit 29fec57fac
194 changed files with 23327 additions and 740 deletions
@@ -216,6 +216,30 @@ public class OpcUaClientServiceTests : IDisposable
_service.CurrentConnectionInfo.ShouldBeNull();
}
[Fact]
public async Task DisconnectAsync_AfterTransportLoss_UsesPublishedEndpointAndClosesSession()
{
var ct = TestContext.Current.CancellationToken;
const string endpoint = "opc.tcp://negotiated:4840";
_endpointDiscovery.SelectedEndpointUrl = endpoint;
var session = new FakeSessionAdapter();
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings(), ct);
_service.CurrentConnectionInfo!.EndpointUrl.ShouldBe(endpoint);
session.ThrowOnEndpointUrlRead = true;
var events = new List<ConnectionStateChangedEventArgs>();
_service.ConnectionStateChanged += (_, state) => events.Add(state);
await _service.DisconnectAsync(ct);
session.Closed.ShouldBeTrue();
session.Disposed.ShouldBeTrue();
_service.CurrentConnectionInfo.ShouldBeNull();
var disconnected = events.ShouldHaveSingleItem();
disconnected.NewState.ShouldBe(ConnectionState.Disconnected);
disconnected.EndpointUrl.ShouldBe(endpoint);
}
/// <summary>
/// Verifies that disconnect is safe to call when no server session is active.
/// </summary>
@@ -239,6 +263,36 @@ public class OpcUaClientServiceTests : IDisposable
// --- Read tests ---
[Fact]
public async Task ReadValueAsync_MaximumAge_ForwardsAgeAndCancellation()
{
var session = new FakeSessionAdapter();
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings(), TestContext.Current.CancellationToken);
using var source = new CancellationTokenSource();
await _service.ReadValueAsync(new NodeId("tag", 2), TimeSpan.FromSeconds(5), source.Token);
session.LastMaximumAge.ShouldBe(TimeSpan.FromSeconds(5));
session.LastReadCancellationToken.ShouldBe(source.Token);
source.Cancel();
await Should.ThrowAsync<OperationCanceledException>(() =>
_service.ReadValueAsync(new NodeId("tag", 2), TimeSpan.FromSeconds(5), source.Token));
}
[Fact]
public async Task ReadValueAsync_MaximumAge_ZeroPreservesOriginalPathAndInvalidAgesFail()
{
var session = new FakeSessionAdapter();
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings(), TestContext.Current.CancellationToken);
await _service.ReadValueAsync(new NodeId("tag", 2), TimeSpan.Zero, TestContext.Current.CancellationToken);
session.LastMaximumAge.ShouldBeNull();
session.ReadCount.ShouldBe(1);
await Should.ThrowAsync<ArgumentOutOfRangeException>(() => _service.ReadValueAsync(new NodeId("tag", 2), TimeSpan.FromMilliseconds(-1), TestContext.Current.CancellationToken));
await Should.ThrowAsync<ArgumentOutOfRangeException>(() => _service.ReadValueAsync(new NodeId("tag", 2), TimeSpan.FromMilliseconds((double)int.MaxValue + 1), TestContext.Current.CancellationToken));
}
/// <summary>
/// Verifies that a connected client can read the current value of a node through the session adapter.
/// </summary>
@@ -718,6 +772,50 @@ public class OpcUaClientServiceTests : IDisposable
session.CreatedSubscriptions[0].AddEventCount.ShouldBe(1);
}
[Fact]
public async Task Rejected_alarm_item_cleans_up_and_a_later_subscribe_succeeds()
{
var rejected = new ServiceResultException(StatusCodes.BadUserAccessDenied);
var failed = new FakeSubscriptionAdapter { EventAddException = rejected };
var session = new FakeSessionAdapter { NextSubscription = failed };
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings(), TestContext.Current.CancellationToken);
var error = await Should.ThrowAsync<ServiceResultException>(() =>
_service.SubscribeAlarmsAsync(ct: TestContext.Current.CancellationToken));
error.ShouldBeSameAs(rejected);
failed.Deleted.ShouldBeTrue();
failed.Disposed.ShouldBeTrue();
await _service.SubscribeAlarmsAsync(ct: TestContext.Current.CancellationToken);
session.CreatedSubscriptions.Count.ShouldBe(2);
session.CreatedSubscriptions[1].AddEventCount.ShouldBe(1);
session.CreatedSubscriptions[1].ActiveHandles.Count.ShouldBe(1);
}
[Fact]
public async Task Rejected_alarm_replay_cleans_up_and_a_later_subscribe_succeeds()
{
var failed = new FakeSubscriptionAdapter { EventAddException = new ServiceResultException(StatusCodes.BadUserAccessDenied) };
var original = new FakeSessionAdapter { EndpointUrl = "opc.tcp://primary:4840" };
var replacement = new FakeSessionAdapter { EndpointUrl = "opc.tcp://backup:4840", NextSubscription = failed };
_sessionFactory.EnqueueSession(original);
_sessionFactory.EnqueueSession(replacement);
var settings = ValidSettings("opc.tcp://primary:4840");
settings.FailoverUrls = ["opc.tcp://backup:4840"];
await _service.ConnectAsync(settings, TestContext.Current.CancellationToken);
await _service.SubscribeAlarmsAsync(ct: TestContext.Current.CancellationToken);
original.SimulateKeepAlive(false);
await _service.LastFailover.ShouldNotBeNull().WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
failed.Deleted.ShouldBeTrue();
failed.Disposed.ShouldBeTrue();
await _service.SubscribeAlarmsAsync(ct: TestContext.Current.CancellationToken);
replacement.CreatedSubscriptions.Count.ShouldBe(2);
replacement.CreatedSubscriptions[1].AddEventCount.ShouldBe(1);
replacement.CreatedSubscriptions[1].ActiveHandles.Count.ShouldBe(1);
}
/// <summary>
/// Verifies that duplicate alarm-subscription requests do not create duplicate event subscriptions.
/// </summary>
@@ -1486,6 +1584,31 @@ public class OpcUaClientServiceTests : IDisposable
e.EndpointUrl == "opc.tcp://backup:4840");
}
[Fact]
public async Task KeepAliveFailure_AfterTransportLoss_UsesPublishedEndpointAndReconnects()
{
var ct = TestContext.Current.CancellationToken;
const string primary = "opc.tcp://primary:4840";
const string backup = "opc.tcp://backup:4840";
var original = new FakeSessionAdapter { EndpointUrl = primary };
_sessionFactory.EnqueueSession(original);
_sessionFactory.EnqueueSession(new FakeSessionAdapter { EndpointUrl = backup });
var settings = ValidSettings(primary);
settings.FailoverUrls = [backup];
await _service.ConnectAsync(settings, ct);
original.ThrowOnEndpointUrlRead = true;
var events = new List<ConnectionStateChangedEventArgs>();
_service.ConnectionStateChanged += (_, state) => events.Add(state);
original.SimulateKeepAlive(false);
await _service.LastFailover!.WaitAsync(TimeSpan.FromSeconds(5), ct);
original.Disposed.ShouldBeTrue();
events.Select(state => (state.NewState, state.EndpointUrl)).ShouldBe([
(ConnectionState.Reconnecting, primary), (ConnectionState.Connected, backup)]);
_service.CurrentConnectionInfo!.EndpointUrl.ShouldBe(backup);
}
/// <summary>
/// Verifies that connection metadata is refreshed to reflect the newly active failover endpoint.
/// </summary>