fix: preserve SDK 2.0 client value and lifetime contracts

This commit is contained in:
Joseph Doherty
2026-11-10 10:19:42 -05:00
parent 0ecea3c6be
commit 3ca06e1b87
38 changed files with 591 additions and 186 deletions
@@ -11,6 +11,48 @@ namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Adapters;
/// </summary>
public class DefaultSubscriptionAdapterTests
{
private sealed class CountingSubscription() : Subscription(ClientTelemetry.Context)
{
public int DisposeCount;
protected override void Dispose(bool disposing)
{
if (disposing) Interlocked.Increment(ref DisposeCount);
base.Dispose(disposing);
}
}
[Fact]
public async Task DeleteAndDispose_ReleaseSdkResourcesExactlyOnce()
{
var subscription = new CountingSubscription();
var detached = 0;
var adapter = new DefaultSubscriptionAdapter(subscription, (_, _) =>
{
Interlocked.Increment(ref detached);
return Task.CompletedTask;
});
await adapter.DeleteAsync(TestContext.Current.CancellationToken);
adapter.Dispose();
adapter.Dispose();
detached.ShouldBe(1);
subscription.DisposeCount.ShouldBe(1);
}
[Fact]
public async Task ConcurrentTeardown_HasOneSdkResourceOwner()
{
var subscription = new CountingSubscription();
var detached = 0;
var adapter = new DefaultSubscriptionAdapter(subscription, (_, _) =>
{
Interlocked.Increment(ref detached);
return Task.CompletedTask;
});
await Task.WhenAll(Task.Run(adapter.Dispose, TestContext.Current.CancellationToken), adapter.DeleteAsync(TestContext.Current.CancellationToken));
detached.ShouldBe(1);
subscription.DisposeCount.ShouldBe(1);
}
/// <summary>DeleteAsync detaches the wrapped subscription exactly once, even when Dispose follows.</summary>
[Fact]
public async Task DeleteAsync_detaches_the_subscription_from_its_session_once()
@@ -117,7 +117,7 @@ public class EndpointSelectorTests
var best = EndpointSelector.SelectBest(endpoints, "opc.tcp://external-host:4840",
MessageSecurityMode.Sign);
new Uri(best.EndpointUrl).Host.ShouldBe("external-host");
new Uri(best.EndpointUrl.ShouldNotBeNull()).Host.ShouldBe("external-host");
}
/// <summary>
@@ -169,7 +169,7 @@ public class EndpointSelectorTests
var best = EndpointSelector.SelectBest(endpoints, "opc.tcp://localhost:4841", MessageSecurityMode.None);
var uri = new Uri(best.EndpointUrl);
var uri = new Uri(best.EndpointUrl.ShouldNotBeNull());
uri.Host.ShouldBe("localhost");
uri.Port.ShouldBe(4841);
uri.AbsolutePath.ShouldBe("/OtOpcUa");
@@ -183,8 +183,8 @@ public class EndpointSelectorTests
var best = EndpointSelector.SelectBest(endpoints, "opc.tcp://localhost:4841", MessageSecurityMode.None);
new Uri(best.EndpointUrl).Host.ShouldBe("internal-host");
new Uri(best.EndpointUrl).Port.ShouldBe(4843);
new Uri(best.EndpointUrl.ShouldNotBeNull()).Host.ShouldBe("internal-host");
new Uri(best.EndpointUrl.ShouldNotBeNull()).Port.ShouldBe(4843);
}
/// <summary>Identical host, port and scheme: nothing to rewrite.</summary>
@@ -206,7 +206,7 @@ public class EndpointSelectorTests
var best = EndpointSelector.SelectBest(endpoints, "opc.tcp://localhost", MessageSecurityMode.None);
var uri = new Uri(best.EndpointUrl);
var uri = new Uri(best.EndpointUrl.ShouldNotBeNull());
uri.Host.ShouldBe("localhost");
uri.Port.ShouldBe(4840);
}
@@ -219,7 +219,7 @@ public class EndpointSelectorTests
var best = EndpointSelector.SelectBest(endpoints, "opc.tcp://[::1]:4841", MessageSecurityMode.None);
var uri = new Uri(best.EndpointUrl);
var uri = new Uri(best.EndpointUrl.ShouldNotBeNull());
uri.Host.ShouldBe("[::1]");
uri.Port.ShouldBe(4841);
}