500 lines
27 KiB
C#
500 lines
27 KiB
C#
using System.Collections.Concurrent;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P07")]
|
|
public sealed class GrpcDataDeploymentActivationTests
|
|
{
|
|
private static DataConnection Definition(string id, string secret) => new()
|
|
{
|
|
DataConnectionId = id, ClusterId = "owner", Revision = 1, Enabled = true,
|
|
ConsumerNodeIds = ["node-a"], TargetMode = DataTargetMode.Node,
|
|
ExpectedClusterId = "target", ExpectedNodeId = "remote", Endpoints = ["https://remote.example:8443"],
|
|
SecretReference = secret, SecretScopeClusterId = "owner", AllowedOperations = DataOperations.Read,
|
|
MaxMessageBytes = 65536, MaxBatchItems = 16, MaxPageItems = 16, MaxStreams = 2,
|
|
MaxMonitoredItems = 16, MaxPendingBytes = 65536, TimeoutMilliseconds = 5000
|
|
};
|
|
private static DataConnectionDeploymentSnapshot Snapshot(long version, params DataConnection[] connections) =>
|
|
new(version, $"artifact-{version}", connections);
|
|
private static async Task EventuallyAsync(Func<bool> condition)
|
|
{
|
|
for (var i = 0; i < 100; i++)
|
|
{
|
|
if (condition()) return;
|
|
await Task.Delay(20, TestContext.Current.CancellationToken);
|
|
}
|
|
throw new TimeoutException("Expected deployment state did not arrive.");
|
|
}
|
|
|
|
[Fact, Trait("Phase", "P09")]
|
|
public async Task Diagnostics_never_report_a_saved_or_unaccepted_scope_as_ready()
|
|
{
|
|
var activator = new PausedActivator();
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.CaptureDiagnostics().ShouldBeEmpty();
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret-reference")));
|
|
await activator.Entered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
var pending = runtime.CaptureDiagnostics().ShouldHaveSingleItem();
|
|
pending.State.ShouldBe("connecting"); pending.SelectedEndpoint.ShouldBeNull();
|
|
pending.ArtifactRevision.ShouldBe("artifact-1"); pending.ScopeVersion.ShouldBe(1);
|
|
activator.Release.TrySetResult();
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var candidate) && DisposeAndTrue(candidate!));
|
|
runtime.CaptureDiagnostics().ShouldHaveSingleItem().State.ShouldBe("connecting", "local credential activation is not a remote capability handshake");
|
|
runtime.BeginAttempt("artifact-2");
|
|
runtime.CaptureDiagnostics().ShouldHaveSingleItem().State.ShouldBe("connecting");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Transient_security_resolution_exception_retries_without_a_change_event()
|
|
{
|
|
var clock = new ManualTimeProvider(); var security = new FailOnceSecurity();
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", security, activator, 4, clock);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && security.Attempts == 1 && clock.ArmedTimers == 1);
|
|
runtime.TryAcquire("link", out _).ShouldBeFalse();
|
|
clock.Advance(TimeSpan.FromMinutes(1));
|
|
await EventuallyAsync(() => runtime.TryAcquire("link", out var lease) && DisposeAndTrue(lease!));
|
|
security.Attempts.ShouldBe(2);
|
|
}
|
|
[Fact]
|
|
public async Task Temporarily_missing_security_material_retries_without_a_change_event()
|
|
{
|
|
var clock = new ManualTimeProvider(); var security = new NullOnceSecurity();
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", security, activator, 4, clock);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && security.Attempts == 1 && clock.ArmedTimers == 1);
|
|
runtime.TryAcquire("link", out _).ShouldBeFalse();
|
|
clock.Advance(TimeSpan.FromMinutes(1));
|
|
await EventuallyAsync(() => runtime.TryAcquire("link", out var lease) && DisposeAndTrue(lease!));
|
|
security.Attempts.ShouldBe(2);
|
|
}
|
|
private sealed class NullOnceSecurity : IDataConnectionSecurityProvider
|
|
{
|
|
private int _attempts;
|
|
public int Attempts => Volatile.Read(ref _attempts);
|
|
public event Action<string>? Changed { add { } remove { } }
|
|
public ValueTask<DataConnectionSecurity?> ResolveAsync(string connectionId, Uri endpoint, CancellationToken cancellationToken) =>
|
|
ValueTask.FromResult<DataConnectionSecurity?>(Interlocked.Increment(ref _attempts) == 1
|
|
? null : new("1", "trust", "key", () => new EmptyHandler()));
|
|
}
|
|
|
|
private sealed class FailOnceSecurity : IDataConnectionSecurityProvider
|
|
{
|
|
private int _attempts;
|
|
public int Attempts => Volatile.Read(ref _attempts);
|
|
public event Action<string>? Changed { add { } remove { } }
|
|
public ValueTask<DataConnectionSecurity?> ResolveAsync(string connectionId, Uri endpoint, CancellationToken cancellationToken)
|
|
{
|
|
if (Interlocked.Increment(ref _attempts) == 1) throw new IOException("transient security store failure");
|
|
return ValueTask.FromResult<DataConnectionSecurity?>(new("1", "trust", "key", () => new EmptyHandler()));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Transient_role_read_failure_during_apply_does_not_strand_the_current_scope()
|
|
{
|
|
var checks = 0;
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4,
|
|
consumerMayAcquire: () => Interlocked.Increment(ref checks) > 1);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var lease) && DisposeAndTrue(lease!));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Temporarily_unavailable_role_is_retried_on_the_owned_timer()
|
|
{
|
|
var clock = new ManualTimeProvider(); var role = new RoleFlag();
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4, clock, () => role.Allowed);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && clock.ArmedTimers == 1);
|
|
runtime.TryAcquire("link", out _).ShouldBeFalse();
|
|
role.Allowed = true;
|
|
clock.Advance(TimeSpan.FromMinutes(1));
|
|
await EventuallyAsync(() => runtime.TryAcquire("link", out var lease) && DisposeAndTrue(lease!));
|
|
}
|
|
private sealed class RoleFlag { public volatile bool Allowed; }
|
|
|
|
[Fact]
|
|
public async Task Transient_scope_store_failure_retries_on_one_clock_timer_without_external_signal()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
var activator = new FailOnceActivator();
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4, clock);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => activator.Attempts == 1 && clock.ArmedTimers == 1);
|
|
runtime.CurrentScopeReady.ShouldBeFalse();
|
|
clock.Advance(TimeSpan.FromMinutes(1));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var lease) && DisposeAndTrue(lease!));
|
|
activator.Attempts.ShouldBe(2);
|
|
}
|
|
[Fact]
|
|
public async Task Superseding_an_attempt_disarms_its_retry_and_activates_only_the_new_floor()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
var activator = new FailOnceActivator();
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4, clock);
|
|
runtime.Apply(Snapshot(1, Definition("old", "old-ref")));
|
|
await EventuallyAsync(() => activator.Attempts == 1 && clock.ArmedTimers == 1);
|
|
runtime.BeginAttempt("artifact-2");
|
|
clock.ArmedTimers.ShouldBe(0);
|
|
runtime.Apply(Snapshot(2, Definition("new", "new-ref")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("new", out var lease) && DisposeAndTrue(lease!));
|
|
clock.Advance(TimeSpan.FromMinutes(1));
|
|
activator.Attempts.ShouldBe(2);
|
|
runtime.TryAcquire("old", out _).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Disposal_disarms_the_pending_scope_retry()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
var activator = new FailOnceActivator();
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4, clock);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => activator.Attempts == 1 && clock.ArmedTimers == 1);
|
|
await runtime.DisposeAsync();
|
|
clock.ArmedTimers.ShouldBe(0);
|
|
clock.Advance(TimeSpan.FromMinutes(1));
|
|
activator.Attempts.ShouldBe(1);
|
|
}
|
|
|
|
private sealed class FailOnceActivator : IDataConnectionScopeActivator
|
|
{
|
|
private int _attempts;
|
|
public int Attempts => Volatile.Read(ref _attempts);
|
|
public Task SetScopeAsync(long version, IReadOnlyDictionary<string, string> allowlist, CancellationToken cancellationToken)
|
|
{
|
|
if (Interlocked.Increment(ref _attempts) == 1) throw new IOException("transient local store failure");
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Beginning_a_new_artifact_attempt_immediately_denies_old_routes_and_rejects_stale_apply()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var candidate) && DisposeAndTrue(candidate!));
|
|
runtime.TryAcquire("link", out var lease).ShouldBeTrue();
|
|
using (lease!)
|
|
{
|
|
runtime.BeginAttempt("artifact-2");
|
|
lease!.CancellationToken.IsCancellationRequested.ShouldBeTrue();
|
|
runtime.CurrentScopeReady.ShouldBeFalse();
|
|
runtime.TryAcquire("link", out _).ShouldBeFalse();
|
|
Should.Throw<InvalidOperationException>(() => runtime.Apply(Snapshot(1, Definition("link", "secret"))));
|
|
}
|
|
runtime.Apply(Snapshot(2, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var next) && DisposeAndTrue(next!));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Replaying_the_accepted_artifact_attempt_keeps_live_routes()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var candidate) && DisposeAndTrue(candidate!));
|
|
runtime.TryAcquire("link", out var lease).ShouldBeTrue();
|
|
using (lease!)
|
|
{
|
|
runtime.BeginAttempt("artifact-1");
|
|
lease!.CancellationToken.IsCancellationRequested.ShouldBeFalse();
|
|
runtime.CurrentScopeReady.ShouldBeTrue();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Starting_a_new_attempt_before_first_activation_rejects_an_older_artifact()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.BeginAttempt("artifact-2");
|
|
Should.Throw<InvalidOperationException>(() => runtime.Apply(Snapshot(1, Definition("old", "secret"))));
|
|
runtime.CurrentScopeReady.ShouldBeFalse();
|
|
runtime.TryAcquire("old", out _).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Stale_scope_completion_cannot_publish_its_old_connection()
|
|
{
|
|
var activator = new PausedActivator();
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
try
|
|
{
|
|
runtime.Apply(Snapshot(1, Definition("old", "old-ref")));
|
|
await activator.Entered.Task.WaitAsync(TestContext.Current.CancellationToken);
|
|
runtime.Apply(Snapshot(2, Definition("new", "new-ref")));
|
|
runtime.CurrentScopeReady.ShouldBeFalse();
|
|
runtime.TryAcquire("old", out _).ShouldBeFalse();
|
|
}
|
|
finally { activator.Release.TrySetResult(); }
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("new", out var lease) && DisposeAndTrue(lease!));
|
|
runtime.TryAcquire("old", out _).ShouldBeFalse();
|
|
activator.AppliedVersions.ShouldBe(new long[] { 1, 2 });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Scope_removal_cancels_existing_lease_before_the_new_floor_finishes()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false, PauseSecond = true };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var value) && DisposeAndTrue(value!));
|
|
runtime.TryAcquire("link", out var lease).ShouldBeTrue();
|
|
using (lease!)
|
|
{
|
|
try
|
|
{
|
|
runtime.Apply(Snapshot(2));
|
|
lease!.CancellationToken.IsCancellationRequested.ShouldBeTrue();
|
|
runtime.TryAcquire("link", out _).ShouldBeFalse();
|
|
runtime.CurrentScopeReady.ShouldBeFalse();
|
|
await activator.Entered.Task.WaitAsync(TestContext.Current.CancellationToken);
|
|
}
|
|
finally { activator.Release.TrySetResult(); }
|
|
}
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Desired_definitions_are_copied_before_any_asynchronous_activation()
|
|
{
|
|
var activator = new PausedActivator();
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
var definition = Definition("link", "secret");
|
|
try
|
|
{
|
|
runtime.Apply(Snapshot(1, definition));
|
|
await activator.Entered.Task.WaitAsync(TestContext.Current.CancellationToken);
|
|
definition.SecretReference = "changed";
|
|
definition.ExpectedNodeId = "forged";
|
|
definition.Endpoints.Clear();
|
|
}
|
|
finally { activator.Release.TrySetResult(); }
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var lease) && DisposeAndTrue(lease!));
|
|
activator.Scopes[1]["link"].ShouldBe("secret");
|
|
runtime.TryAcquire("link", out var current).ShouldBeTrue();
|
|
using (current!) current!.Connection.Target.ExpectedNodeId.ShouldBe("remote");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Same_scope_version_with_different_contents_is_rejected()
|
|
{
|
|
var activator = new PausedActivator();
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
try
|
|
{
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await activator.Entered.Task.WaitAsync(TestContext.Current.CancellationToken);
|
|
Should.Throw<InvalidOperationException>(() => runtime.Apply(Snapshot(1, Definition("link", "other-secret"))));
|
|
}
|
|
finally { activator.Release.TrySetResult(); }
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Consumer_redundancy_mode_is_preserved_from_the_validated_snapshot()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.Apply(new DataConnectionDeploymentSnapshot(1, "artifact-1", [Definition("link", "secret")], RedundancyMode.Hot));
|
|
runtime.CurrentConsumerMode.ShouldBe(RedundancyMode.Hot);
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Replaying_identical_artifact_does_not_retire_a_live_connection()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var candidate) && DisposeAndTrue(candidate!));
|
|
runtime.TryAcquire("link", out var lease).ShouldBeTrue();
|
|
using (lease!)
|
|
{
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
lease!.CancellationToken.IsCancellationRequested.ShouldBeFalse();
|
|
runtime.CurrentScopeReady.ShouldBeTrue();
|
|
activator.AppliedVersions.ShouldBe(new long[] { 1 });
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Same_artifact_revision_cannot_replace_connection_definitions()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var candidate) && DisposeAndTrue(candidate!));
|
|
runtime.TryAcquire("link", out var lease).ShouldBeTrue();
|
|
using (lease!)
|
|
{
|
|
var changed = Definition("link", "secret");
|
|
changed.Endpoints = ["https://different.example:8443"];
|
|
Should.Throw<InvalidOperationException>(() => runtime.Apply(Snapshot(1, changed)));
|
|
lease!.CancellationToken.IsCancellationRequested.ShouldBeFalse();
|
|
runtime.CurrentScopeReady.ShouldBeTrue();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Transient_role_contention_during_one_credential_change_does_not_strand_other_connections()
|
|
{
|
|
var clock = new ManualTimeProvider(); var role = new RoleFlag { Allowed = true };
|
|
var security = new MutableSecurity(); var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", security, activator, 4, clock, () => role.Allowed);
|
|
runtime.Apply(Snapshot(1, Definition("one", "secret-one"), Definition("two", "secret-two")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("one", out var a) && DisposeAndTrue(a!) &&
|
|
runtime.TryAcquire("two", out var b) && DisposeAndTrue(b!));
|
|
runtime.TryAcquire("one", out var first).ShouldBeTrue();
|
|
runtime.TryAcquire("two", out var second).ShouldBeTrue();
|
|
using (first!) using (second!)
|
|
{
|
|
role.Allowed = false;
|
|
security.Rotate("one");
|
|
role.Allowed = true;
|
|
first!.CancellationToken.IsCancellationRequested.ShouldBeTrue();
|
|
second!.CancellationToken.IsCancellationRequested.ShouldBeFalse();
|
|
runtime.TryAcquire("two", out var unaffected).ShouldBeTrue();
|
|
unaffected!.Dispose();
|
|
}
|
|
await EventuallyAsync(() => clock.ArmedTimers > 0 || runtime.TryAcquire("one", out var value) && DisposeAndTrue(value!));
|
|
if (clock.ArmedTimers > 0) clock.Advance(TimeSpan.FromMinutes(1));
|
|
await EventuallyAsync(() => runtime.TryAcquire("one", out var recovered) && DisposeAndTrue(recovered!));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Credential_change_drains_only_affected_connection_before_reactivation()
|
|
{
|
|
var security = new MutableSecurity();
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", security, activator, 4);
|
|
runtime.Apply(Snapshot(1, Definition("one", "secret-one"), Definition("two", "secret-two")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("one", out var a) && DisposeAndTrue(a!) &&
|
|
runtime.TryAcquire("two", out var b) && DisposeAndTrue(b!));
|
|
runtime.TryAcquire("one", out var first).ShouldBeTrue();
|
|
runtime.TryAcquire("two", out var second).ShouldBeTrue();
|
|
using (first!) using (second!)
|
|
{
|
|
security.Rotate("one");
|
|
first!.CancellationToken.IsCancellationRequested.ShouldBeTrue();
|
|
second!.CancellationToken.IsCancellationRequested.ShouldBeFalse();
|
|
runtime.CurrentScopeReady.ShouldBeTrue();
|
|
}
|
|
}
|
|
private sealed class MutableSecurity : IDataConnectionSecurityProvider
|
|
{
|
|
public event Action<string>? Changed;
|
|
public ValueTask<DataConnectionSecurity?> ResolveAsync(string connectionId, Uri endpoint, CancellationToken cancellationToken) =>
|
|
ValueTask.FromResult<DataConnectionSecurity?>(new("1", "trust", "key", () => new EmptyHandler()));
|
|
public void Rotate(string id) => Changed?.Invoke(id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Deployed_operation_and_batch_limits_apply_before_any_network_call()
|
|
{
|
|
var definition = Definition("link", "secret");
|
|
definition.MaxBatchItems = 1;
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
runtime.Apply(Snapshot(1, definition));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var candidate) && DisposeAndTrue(candidate!));
|
|
runtime.TryAcquire("link", out var lease).ShouldBeTrue();
|
|
using (lease!)
|
|
{
|
|
var view = new DataView(new("target", "urn:test", "string", "x"), DataRealm.Raw, null, 0, "x");
|
|
using var value = DataValue.FromScalar(DataValueKind.Int32, 1);
|
|
var budget = new DataBudget(TimeSpan.FromSeconds(1), 4, 1024);
|
|
await Should.ThrowAsync<UnauthorizedAccessException>(() => lease!.Connection.WriteAsync(new([new("a", "op", view, value, 1, "boot", TimeSpan.FromSeconds(1))], budget), TestContext.Current.CancellationToken));
|
|
await Should.ThrowAsync<ArgumentOutOfRangeException>(() => lease!.Connection.ReadAsync(new([new("a", view, null), new("b", view, null)], budget), TestContext.Current.CancellationToken));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Warm_standby_drains_existing_routes_and_keeps_durable_scope_ready()
|
|
{
|
|
var active = true;
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4, consumerMayAcquire: () => active);
|
|
runtime.Apply(Snapshot(1, Definition("link", "secret")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("link", out var candidate) && DisposeAndTrue(candidate!));
|
|
runtime.TryAcquire("link", out var lease).ShouldBeTrue();
|
|
using (lease!)
|
|
{
|
|
active = false;
|
|
runtime.NotifyConsumerRoleChanged();
|
|
lease!.CancellationToken.IsCancellationRequested.ShouldBeTrue();
|
|
runtime.TryAcquire("link", out _).ShouldBeFalse();
|
|
runtime.CurrentScopeReady.ShouldBeTrue();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Missing_one_credential_does_not_hold_other_connections_or_the_scope_unready()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security("missing"), activator, 4);
|
|
runtime.Apply(Snapshot(1, Definition("ready", "a"), Definition("missing", "b")));
|
|
await EventuallyAsync(() => runtime.CurrentScopeReady && runtime.TryAcquire("ready", out var lease) && DisposeAndTrue(lease!));
|
|
runtime.TryAcquire("missing", out _).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Invalid_scope_version_is_denied_before_any_activation()
|
|
{
|
|
var activator = new PausedActivator { PauseFirst = false };
|
|
await using var runtime = new DataConnectionDeploymentRuntime("node-a", "owner", new Security(), activator, 4);
|
|
Should.Throw<ArgumentOutOfRangeException>(() => runtime.Apply(Snapshot(0, Definition("link", "secret"))));
|
|
runtime.CurrentScopeReady.ShouldBeFalse();
|
|
runtime.TryAcquire("link", out _).ShouldBeFalse();
|
|
activator.AppliedVersions.ShouldBeEmpty();
|
|
}
|
|
|
|
private static bool DisposeAndTrue(IDisposable lease) { lease.Dispose(); return true; }
|
|
private sealed class Security(string? missing = null) : IDataConnectionSecurityProvider
|
|
{
|
|
public event Action<string>? Changed { add { } remove { } }
|
|
public ValueTask<DataConnectionSecurity?> ResolveAsync(string connectionId, Uri endpoint, CancellationToken cancellationToken) =>
|
|
ValueTask.FromResult<DataConnectionSecurity?>(string.Equals(connectionId, missing, StringComparison.OrdinalIgnoreCase)
|
|
? null : new("1", "trust", "key", () => new EmptyHandler()));
|
|
}
|
|
private sealed class EmptyHandler : HttpMessageHandler
|
|
{
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) =>
|
|
throw new InvalidOperationException("This activation test must not use the network.");
|
|
}
|
|
private sealed class PausedActivator : IDataConnectionScopeActivator
|
|
{
|
|
public bool PauseFirst { get; init; } = true;
|
|
public bool PauseSecond { get; init; }
|
|
public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
private readonly ConcurrentQueue<long> _versions = new();
|
|
public long[] AppliedVersions => _versions.ToArray();
|
|
public ConcurrentDictionary<long, IReadOnlyDictionary<string, string>> Scopes { get; } = new();
|
|
public async Task SetScopeAsync(long version, IReadOnlyDictionary<string, string> allowlist, CancellationToken cancellationToken)
|
|
{
|
|
_versions.Enqueue(version);
|
|
Scopes[version] = new Dictionary<string, string>(allowlist, StringComparer.OrdinalIgnoreCase);
|
|
if (version == 1 && PauseFirst || version == 2 && PauseSecond)
|
|
{
|
|
Entered.TrySetResult();
|
|
// Deliberately ignore cancellation to exercise stale-completion fencing.
|
|
await Release.Task;
|
|
}
|
|
}
|
|
}
|
|
}
|