feat(host): ClusterActivityEvaluator — oldest-member (singleton-host) active-node definition
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using Akka.Actor;
|
||||
using Akka.Configuration;
|
||||
using ZB.MOM.WW.ScadaBridge.Host.Health;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
|
||||
|
||||
public class ClusterActivityEvaluatorTests : IAsyncLifetime
|
||||
{
|
||||
private ActorSystem? _system;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public async Task DisposeAsync() { if (_system != null) await _system.Terminate(); }
|
||||
|
||||
[Fact]
|
||||
public void SelfIsOldest_SoleUpMember_ReturnsTrue()
|
||||
{
|
||||
var cluster = Akka.Cluster.Cluster.Get(_system!);
|
||||
Assert.True(ClusterActivityEvaluator.SelfIsOldest(cluster));
|
||||
Assert.True(ClusterActivityEvaluator.SelfIsOldest(cluster, "Central"));
|
||||
}
|
||||
|
||||
[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"));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user