23c35df234
The InboundAPI carried its own IActiveNodeGate (implemented by Host/Health/ActiveNodeGate) whose rule already matched the shared Health 0.3.0 AkkaActiveNodeGate — both route to ClusterActiveNode (oldest Up member). The private interface and implementation are deleted; InboundAPI references ZB.MOM.WW.Health (0.3.0, already pinned) and InboundApiEndpointFilter, AuditWriteMiddleware, InboundMethodWarmupService and FlowDebugRunner consume ZB.MOM.WW.Health.IActiveNodeGate. The Central branch of Program.cs registers AkkaActiveNodeGate (unscoped, like the active-node health check). Warm-up behaviour is kept: the shared gate answers false while no ActorSystem resolves, while this member is not Up, and on any cluster-init fault. It resolves the ActorSystem through the existing DI singleton bridge (GetOrCreateActorSystem), the same path the health probes use. Tests: CompositionRoot asserts the shared gate type (fails with the registration removed); the evaluator wiring test now drives AkkaActiveNodeGate; new warm-up test (no ActorSystem -> not active). Nothing was missing from the Health 0.3.0 package. (ScadaBridge#80) Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
129 lines
5.4 KiB
C#
129 lines
5.4 KiB
C#
using Akka.Actor;
|
|
using Akka.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using ZB.MOM.WW.ScadaBridge.ClusterInfrastructure;
|
|
using ZB.MOM.WW.ScadaBridge.Communication;
|
|
using ZB.MOM.WW.ScadaBridge.Host.Actors;
|
|
using ZB.MOM.WW.ScadaBridge.Host.Health;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
|
|
|
|
public class ClusterActivityEvaluatorTests : IAsyncLifetime
|
|
{
|
|
private ActorSystem? _system;
|
|
|
|
/// <summary>Prepares the fixture before the tests run.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public async Task InitializeAsync()
|
|
{
|
|
var port = FreePort();
|
|
var config = ConfigurationFactory.ParseString($@"
|
|
akka {{
|
|
actor.provider = cluster
|
|
remote.dot-netty.tcp {{ hostname = ""127.0.0.1"", port = {port} }}
|
|
cluster {{
|
|
seed-nodes = [""akka.tcp://eval-test@127.0.0.1:{port}""]
|
|
roles = [""Central""]
|
|
min-nr-of-members = 1
|
|
}}
|
|
}}");
|
|
_system = ActorSystem.Create("eval-test", config);
|
|
var cluster = Akka.Cluster.Cluster.Get(_system);
|
|
var deadline = DateTime.UtcNow.AddSeconds(20);
|
|
while (cluster.SelfMember.Status != Akka.Cluster.MemberStatus.Up && DateTime.UtcNow < deadline)
|
|
await Task.Delay(100);
|
|
}
|
|
|
|
/// <summary>Releases the resources held by this fixture asynchronously.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public async Task DisposeAsync() { if (_system != null) await _system.Terminate(); }
|
|
|
|
/// <summary>Verifies that self is oldest, sole up member, returns true.</summary>
|
|
[Fact]
|
|
public void SelfIsOldest_SoleUpMember_ReturnsTrue()
|
|
{
|
|
var cluster = Akka.Cluster.Cluster.Get(_system!);
|
|
Assert.True(ClusterActivityEvaluator.SelfIsOldest(cluster));
|
|
Assert.True(ClusterActivityEvaluator.SelfIsOldest(cluster, "Central"));
|
|
}
|
|
|
|
/// <summary>Verifies that self is oldest, role not held, returns false.</summary>
|
|
[Fact]
|
|
public void SelfIsOldest_RoleNotHeld_ReturnsFalse()
|
|
{
|
|
var cluster = Akka.Cluster.Cluster.Get(_system!);
|
|
// Self doesn't carry the role => it can never host that role's singletons.
|
|
Assert.False(ClusterActivityEvaluator.SelfIsOldest(cluster, "site-nonexistent"));
|
|
}
|
|
|
|
/// <summary>Verifies that active node gate, and, node provider, agree with evaluator.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ActiveNodeGate_And_NodeProvider_AgreeWithEvaluator()
|
|
{
|
|
// Both product gates must be backed by the oldest-member evaluator.
|
|
// A single-node Up cluster: evaluator says true, so both must say true.
|
|
// (Pinning test — the leader==oldest divergence is proven in the
|
|
// two-node integration test; here we assert the wiring holds.)
|
|
var port = FreePort();
|
|
var self = $"akka.tcp://scadabridge@127.0.0.1:{port}";
|
|
var akkaService = new AkkaHostedService(
|
|
new ServiceCollection().BuildServiceProvider(),
|
|
Options.Create(new NodeOptions { Role = "Central", NodeHostname = "127.0.0.1", RemotingPort = port }),
|
|
Options.Create(new ClusterOptions
|
|
{
|
|
SeedNodes = new List<string> { self },
|
|
SplitBrainResolverStrategy = "keep-oldest",
|
|
StableAfter = TimeSpan.FromSeconds(3),
|
|
HeartbeatInterval = TimeSpan.FromMilliseconds(500),
|
|
FailureDetectionThreshold = TimeSpan.FromSeconds(2),
|
|
MinNrOfMembers = 1,
|
|
DownIfAlone = true,
|
|
}),
|
|
Options.Create(new CommunicationOptions()),
|
|
NullLogger<AkkaHostedService>.Instance);
|
|
|
|
var system = akkaService.GetOrCreateActorSystem();
|
|
try
|
|
{
|
|
var cluster = Akka.Cluster.Cluster.Get(system);
|
|
var deadline = DateTime.UtcNow.AddSeconds(20);
|
|
while (cluster.SelfMember.Status != Akka.Cluster.MemberStatus.Up && DateTime.UtcNow < deadline)
|
|
await Task.Delay(100);
|
|
|
|
// Gitea #80: the inbound API's gate is the shared AkkaActiveNodeGate, resolving the
|
|
// ActorSystem from DI exactly as Program.cs's singleton bridge serves it.
|
|
using var sp = new ServiceCollection()
|
|
.AddSingleton(_ => akkaService.GetOrCreateActorSystem())
|
|
.BuildServiceProvider();
|
|
var gate = new ZB.MOM.WW.Health.Akka.AkkaActiveNodeGate(sp);
|
|
var provider = new AkkaClusterNodeProvider(akkaService, "Central");
|
|
Assert.True(gate.IsActiveNode);
|
|
Assert.True(provider.SelfIsPrimary);
|
|
}
|
|
finally
|
|
{
|
|
await system.Terminate();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gitea #80 warm-up contract: while no ActorSystem is available the shared gate reports
|
|
/// standby, never active — the safe default the retired ScadaBridge gate also had.
|
|
/// </summary>
|
|
[Fact]
|
|
public void ActiveNodeGate_WithoutActorSystem_IsNotActive()
|
|
{
|
|
using var sp = new ServiceCollection().BuildServiceProvider();
|
|
Assert.False(new ZB.MOM.WW.Health.Akka.AkkaActiveNodeGate(sp).IsActiveNode);
|
|
}
|
|
|
|
private static int FreePort()
|
|
{
|
|
var l = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
|
l.Start(); var p = ((System.Net.IPEndPoint)l.LocalEndpoint).Port; l.Stop(); return p;
|
|
}
|
|
}
|