678498a4a6
PerEndpointConnectTimeout (3 s) now bounds only TCP + discovery, so failover past a dead endpoint stays fast. The new SessionCreateTimeout (10 s) bounds OpenSecureChannel + CreateSession on an endpoint that answered discovery, where the client-side cert and crypto work lives (#910). 2 x (3 + 10) s stays inside the host's 30 s initialise bound. Expiries name the budget that ran out; both budgets must be positive; AdminUI form carries the new field so a save does not drop it; driver docs updated. Closes #912
182 lines
8.5 KiB
C#
182 lines
8.5 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Tests;
|
|
|
|
[Trait("Category", "Unit")]
|
|
public sealed class OpcUaClientFailoverTests
|
|
{
|
|
/// <summary>Verifies that ResolveEndpointCandidates prefers EndpointUrls when provided.</summary>
|
|
[Fact]
|
|
public void ResolveEndpointCandidates_prefers_EndpointUrls_when_provided()
|
|
{
|
|
var opts = new OpcUaClientDriverOptions
|
|
{
|
|
EndpointUrl = "opc.tcp://fallback:4840",
|
|
EndpointUrls = ["opc.tcp://primary:4840", "opc.tcp://backup:4841"],
|
|
};
|
|
var list = OpcUaClientDriver.ResolveEndpointCandidates(opts);
|
|
list.Count.ShouldBe(2);
|
|
list[0].ShouldBe("opc.tcp://primary:4840");
|
|
list[1].ShouldBe("opc.tcp://backup:4841");
|
|
}
|
|
|
|
/// <summary>Verifies that ResolveEndpointCandidates falls back to single EndpointUrl when list is empty.</summary>
|
|
[Fact]
|
|
public void ResolveEndpointCandidates_falls_back_to_single_EndpointUrl_when_list_empty()
|
|
{
|
|
var opts = new OpcUaClientDriverOptions { EndpointUrl = "opc.tcp://only:4840" };
|
|
var list = OpcUaClientDriver.ResolveEndpointCandidates(opts);
|
|
list.Count.ShouldBe(1);
|
|
list[0].ShouldBe("opc.tcp://only:4840");
|
|
}
|
|
|
|
/// <summary>Verifies that an empty EndpointUrls list is treated as a fallback to EndpointUrl.</summary>
|
|
[Fact]
|
|
public void ResolveEndpointCandidates_empty_list_treated_as_fallback_to_EndpointUrl()
|
|
{
|
|
// Explicit empty list should still fall back to the single-URL shortcut rather than
|
|
// producing a zero-candidate sweep that would immediately throw with no URLs tried.
|
|
var opts = new OpcUaClientDriverOptions
|
|
{
|
|
EndpointUrl = "opc.tcp://single:4840",
|
|
EndpointUrls = [],
|
|
};
|
|
OpcUaClientDriver.ResolveEndpointCandidates(opts).Count.ShouldBe(1);
|
|
}
|
|
|
|
/// <summary>Verifies that HostName uses the first candidate before connection.</summary>
|
|
[Fact]
|
|
public void HostName_uses_first_candidate_before_connect()
|
|
{
|
|
var opts = new OpcUaClientDriverOptions
|
|
{
|
|
EndpointUrls = ["opc.tcp://primary:4840", "opc.tcp://backup:4841"],
|
|
};
|
|
using var drv = new OpcUaClientDriver(opts, "opcua-host");
|
|
drv.HostName.ShouldBe("opc.tcp://primary:4840",
|
|
"pre-connect the dashboard should show the first candidate URL so operators can link back");
|
|
}
|
|
|
|
/// <summary>Verifies that initializing against all unreachable endpoints throws AggregateException listing each.</summary>
|
|
[Fact]
|
|
public async Task Initialize_against_all_unreachable_endpoints_throws_AggregateException_listing_each()
|
|
{
|
|
// Port 1 + port 2 + port 3 on loopback are all guaranteed closed (TCP RST immediate).
|
|
// Failover sweep should attempt all three and throw AggregateException naming each URL
|
|
// so operators see exactly which candidates were tried.
|
|
var opts = new OpcUaClientDriverOptions
|
|
{
|
|
EndpointUrls = ["opc.tcp://127.0.0.1:1", "opc.tcp://127.0.0.1:2", "opc.tcp://127.0.0.1:3"],
|
|
PerEndpointConnectTimeout = TimeSpan.FromMilliseconds(500),
|
|
Timeout = TimeSpan.FromMilliseconds(500),
|
|
AutoAcceptCertificates = true,
|
|
};
|
|
using var drv = new OpcUaClientDriver(opts, "opcua-failover");
|
|
|
|
var ex = await Should.ThrowAsync<AggregateException>(async () =>
|
|
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken));
|
|
|
|
ex.Message.ShouldContain("127.0.0.1:1");
|
|
ex.Message.ShouldContain("127.0.0.1:2");
|
|
ex.Message.ShouldContain("127.0.0.1:3");
|
|
drv.GetHealth().State.ShouldBe(DriverState.Faulted);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gitea #910: an endpoint that accepts TCP but never answers runs out the per-endpoint budget. The SDK reports
|
|
/// that as BadRequestInterrupted / a bare cancellation; the failover aggregate must name the budget instead.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task A_per_endpoint_budget_expiry_is_reported_as_that_budget()
|
|
{
|
|
using var silent = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
|
silent.Start(); // accepts (kernel backlog) but never reads or replies
|
|
var port = ((System.Net.IPEndPoint)silent.LocalEndpoint).Port;
|
|
var opts = new OpcUaClientDriverOptions
|
|
{
|
|
EndpointUrl = $"opc.tcp://127.0.0.1:{port}",
|
|
PerEndpointConnectTimeout = TimeSpan.FromMilliseconds(400),
|
|
Timeout = TimeSpan.FromSeconds(30),
|
|
AutoAcceptCertificates = true,
|
|
};
|
|
using var drv = new OpcUaClientDriver(opts, "opcua-budget");
|
|
|
|
var ex = await Should.ThrowAsync<AggregateException>(async () =>
|
|
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken));
|
|
|
|
ex.Message.ShouldContain("TimeoutException");
|
|
ex.Message.ShouldContain("PerEndpointConnectTimeout (0.4 s)");
|
|
}
|
|
|
|
/// <summary>#912: the two connect phases default to a short reachability budget and a larger session budget.</summary>
|
|
[Fact]
|
|
public void Connect_budgets_default_to_3s_reachability_and_10s_session()
|
|
{
|
|
var o = new OpcUaClientDriverOptions();
|
|
o.PerEndpointConnectTimeout.ShouldBe(TimeSpan.FromSeconds(3));
|
|
o.SessionCreateTimeout.ShouldBe(TimeSpan.FromSeconds(10));
|
|
// Two candidates that both hang in the session phase must still fit the host's initialise bound.
|
|
(2 * (o.PerEndpointConnectTimeout + o.SessionCreateTimeout)).ShouldBeLessThan(TimeSpan.FromSeconds(30));
|
|
}
|
|
|
|
/// <summary>#912: a phase cut short by its own budget is reported as that budget, by option name.</summary>
|
|
[Fact]
|
|
public async Task A_phase_budget_expiry_names_its_option()
|
|
{
|
|
var ex = await Should.ThrowAsync<TimeoutException>(() => OpcUaClientDriver.RunWithinBudgetAsync<int>(
|
|
TimeSpan.FromMilliseconds(100), nameof(OpcUaClientDriverOptions.SessionCreateTimeout),
|
|
async budget => { await Task.Delay(Timeout.Infinite, budget); return 0; },
|
|
TestContext.Current.CancellationToken));
|
|
|
|
ex.Message.ShouldContain("SessionCreateTimeout (0.1 s)");
|
|
ex.InnerException.ShouldBeAssignableTo<OperationCanceledException>();
|
|
}
|
|
|
|
/// <summary>#912: a caller cancel is not dressed up as a budget expiry.</summary>
|
|
[Fact]
|
|
public async Task A_caller_cancel_passes_through_unchanged()
|
|
{
|
|
using var caller = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
|
|
await Should.ThrowAsync<OperationCanceledException>(() => OpcUaClientDriver.RunWithinBudgetAsync<int>(
|
|
TimeSpan.FromSeconds(30), "X",
|
|
async budget => { await Task.Delay(Timeout.Infinite, budget); return 0; },
|
|
caller.Token));
|
|
}
|
|
|
|
/// <summary>#912: a phase that completes inside its budget returns its result, and a non-timeout fault is not rewritten.</summary>
|
|
[Fact]
|
|
public async Task A_phase_inside_its_budget_returns_and_faults_pass_through()
|
|
{
|
|
(await OpcUaClientDriver.RunWithinBudgetAsync(TimeSpan.FromSeconds(5), "X",
|
|
_ => Task.FromResult(42), TestContext.Current.CancellationToken)).ShouldBe(42);
|
|
|
|
await Should.ThrowAsync<InvalidOperationException>(() => OpcUaClientDriver.RunWithinBudgetAsync<int>(
|
|
TimeSpan.FromSeconds(5), "X", _ => throw new InvalidOperationException("boom"),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
/// <summary>#912: non-positive connect budgets are refused at initialise.</summary>
|
|
[Fact]
|
|
public void ValidateTuning_rejects_non_positive_connect_budgets()
|
|
{
|
|
Should.Throw<InvalidOperationException>(() => OpcUaClientDriver.ValidateTuning(
|
|
new OpcUaClientDriverOptions { SessionCreateTimeout = TimeSpan.Zero }))
|
|
.Message.ShouldContain("SessionCreateTimeout");
|
|
Should.Throw<InvalidOperationException>(() => OpcUaClientDriver.ValidateTuning(
|
|
new OpcUaClientDriverOptions { PerEndpointConnectTimeout = TimeSpan.Zero }))
|
|
.Message.ShouldContain("PerEndpointConnectTimeout");
|
|
}
|
|
|
|
/// <summary>#912: the factory binds <c>sessionCreateTimeout</c>; JSON without it keeps the default.</summary>
|
|
[Fact]
|
|
public void Factory_binds_sessionCreateTimeout()
|
|
{
|
|
OpcUaClientDriverFactoryExtensions.ParseOptions("d", """{"sessionCreateTimeout":"00:00:20"}""")
|
|
.SessionCreateTimeout.ShouldBe(TimeSpan.FromSeconds(20));
|
|
OpcUaClientDriverFactoryExtensions.ParseOptions("d", "{}")
|
|
.SessionCreateTimeout.ShouldBe(TimeSpan.FromSeconds(10));
|
|
}
|
|
}
|