513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
165 lines
7.6 KiB
C#
165 lines
7.6 KiB
C#
namespace ZB.MOM.WW.ScadaBridge.ClusterInfrastructure.Tests;
|
|
|
|
/// <summary>
|
|
/// Unit tests for <see cref="ClusterBootstrapGuard"/> — the pure decision core of the
|
|
/// simultaneous-cold-start split-brain guard. The runtime probe + JoinSeedNodes wiring
|
|
/// lives in <c>ClusterBootstrapCoordinator</c> and is covered by the real-ActorSystem
|
|
/// <c>ClusterBootstrapCoordinatorTests</c> in the integration suite.
|
|
/// </summary>
|
|
public class ClusterBootstrapGuardTests
|
|
{
|
|
private const string A = "akka.tcp://scadabridge@scadabridge-site-a-a:8082";
|
|
private const string B = "akka.tcp://scadabridge@scadabridge-site-a-b:8082";
|
|
|
|
/// <summary>Verifies that the node with the lower address is the founder and needs no probe.</summary>
|
|
[Fact]
|
|
public void Lower_address_node_is_the_founder_and_needs_no_probe()
|
|
{
|
|
// scadabridge-site-a-a < scadabridge-site-a-b, so on node-a the guard makes it the founder.
|
|
var role = ClusterBootstrapGuard.Analyze(new[] { A, B }, "scadabridge-site-a-a", 8082);
|
|
|
|
Assert.True(role.Applies);
|
|
Assert.True(role.IsFounder);
|
|
Assert.Equal(new[] { A, B }, role.SelfFirstOrder);
|
|
}
|
|
|
|
/// <summary>Verifies that the node with the higher address is not the founder and probes its partner.</summary>
|
|
[Fact]
|
|
public void Higher_address_node_is_not_the_founder_and_targets_the_partner_for_probing()
|
|
{
|
|
// On node-b the partner is node-a (the lower/founder).
|
|
var role = ClusterBootstrapGuard.Analyze(new[] { B, A }, "scadabridge-site-a-b", 8082);
|
|
|
|
Assert.True(role.Applies);
|
|
Assert.False(role.IsFounder);
|
|
Assert.Equal("scadabridge-site-a-a", role.PartnerHost);
|
|
Assert.Equal(8082, role.PartnerPort);
|
|
}
|
|
|
|
/// <summary>Verifies that the founder tie-break gives the same result regardless of seed order.</summary>
|
|
[Fact]
|
|
public void Tie_break_is_symmetric_regardless_of_seed_order()
|
|
{
|
|
// Both nodes must agree on who founds no matter how each lists its seeds.
|
|
var onLower = ClusterBootstrapGuard.Analyze(new[] { A, B }, "scadabridge-site-a-a", 8082);
|
|
var onHigher = ClusterBootstrapGuard.Analyze(new[] { B, A }, "scadabridge-site-a-b", 8082);
|
|
|
|
Assert.True(onLower.IsFounder);
|
|
Assert.False(onHigher.IsFounder);
|
|
// Exactly one founder.
|
|
Assert.True(onLower.IsFounder ^ onHigher.IsFounder);
|
|
}
|
|
|
|
/// <summary>Verifies that the tie-break ignores case, so a casing mismatch cannot make both nodes founders.</summary>
|
|
[Fact]
|
|
public void Tie_break_is_case_insensitive_so_a_casing_mismatch_cannot_make_both_founders()
|
|
{
|
|
// The two sides' seed config differ only in host casing (review note 1): if the tie-break
|
|
// were case-sensitive, both could conclude they are the founder and re-open the split.
|
|
var lowerSelf = "akka.tcp://scadabridge@SITE-A-A:8082";
|
|
var higherSelf = "akka.tcp://scadabridge@site-a-b:8082";
|
|
|
|
var onLower = ClusterBootstrapGuard.Analyze(new[] { lowerSelf, higherSelf }, "SITE-A-A", 8082);
|
|
var onHigher = ClusterBootstrapGuard.Analyze(new[] { higherSelf, lowerSelf }, "site-a-b", 8082);
|
|
|
|
Assert.True(onLower.IsFounder ^ onHigher.IsFounder);
|
|
Assert.True(onLower.IsFounder); // "site-a-a" < "site-a-b" ordinal-ignore-case
|
|
}
|
|
|
|
/// <summary>Verifies that the higher node joins the founder when the partner is reachable.</summary>
|
|
[Fact]
|
|
public void Higher_node_joins_the_founder_when_partner_is_reachable()
|
|
{
|
|
var role = ClusterBootstrapGuard.Analyze(new[] { B, A }, "scadabridge-site-a-b", 8082);
|
|
|
|
// Reachable ⇒ peer-first ⇒ JoinSeedNodeProcess ⇒ joins the founder, never self-forms.
|
|
Assert.Equal(new[] { A, B }, ClusterBootstrapGuard.HigherNodeOrder(role, partnerReachable: true)); // partner (node-a) first
|
|
}
|
|
|
|
/// <summary>Verifies that the higher node forms a cluster alone when the partner is unreachable.</summary>
|
|
[Fact]
|
|
public void Higher_node_forms_alone_when_partner_is_unreachable()
|
|
{
|
|
var role = ClusterBootstrapGuard.Analyze(new[] { B, A }, "scadabridge-site-a-b", 8082);
|
|
|
|
// Unreachable after the window ⇒ partner is dead ⇒ self-first ⇒ cold-start-alone preserved.
|
|
Assert.Equal(new[] { B, A }, ClusterBootstrapGuard.HigherNodeOrder(role, partnerReachable: false)); // self (node-b) first
|
|
}
|
|
|
|
/// <summary>Verifies that the guard does not apply to a single seed node.</summary>
|
|
[Fact]
|
|
public void Guard_does_not_apply_to_a_single_seed_node()
|
|
{
|
|
// A single-node install lists exactly one seed (itself) — not a pair, guard is inert.
|
|
var role = ClusterBootstrapGuard.Analyze(new[] { "akka.tcp://scadabridge@lone:8081" }, "lone", 8081);
|
|
|
|
Assert.False(role.Applies);
|
|
}
|
|
|
|
/// <summary>Verifies that the guard does not apply when the local node is absent from the two seeds.</summary>
|
|
[Fact]
|
|
public void Guard_does_not_apply_when_self_is_absent_from_the_two_seeds()
|
|
{
|
|
var role = ClusterBootstrapGuard.Analyze(new[] { A, B }, "scadabridge-central-a", 8081);
|
|
|
|
Assert.False(role.Applies);
|
|
}
|
|
|
|
/// <summary>Verifies that the guard does not apply to three or more seeds.</summary>
|
|
[Fact]
|
|
public void Guard_does_not_apply_to_three_or_more_seeds()
|
|
{
|
|
var role = ClusterBootstrapGuard.Analyze(
|
|
new[] { A, B, "akka.tcp://scadabridge@scadabridge-site-a-c:8082" }, "scadabridge-site-a-a", 8082);
|
|
|
|
Assert.False(role.Applies);
|
|
}
|
|
|
|
/// <summary>Verifies that the guard does not apply when a seed address is malformed.</summary>
|
|
[Fact]
|
|
public void Guard_does_not_apply_when_a_seed_is_malformed()
|
|
{
|
|
var role = ClusterBootstrapGuard.Analyze(new[] { A, "not-a-seed" }, "scadabridge-site-a-a", 8082);
|
|
|
|
Assert.False(role.Applies);
|
|
}
|
|
|
|
/// <summary>Verifies that parsing a seed address extracts the host and port.</summary>
|
|
/// <param name="seed">The seed address to parse.</param>
|
|
/// <param name="expectedHost">The expected host.</param>
|
|
/// <param name="expectedPort">The expected port.</param>
|
|
[Theory]
|
|
[InlineData("akka.tcp://scadabridge@scadabridge-site-a-a:8082", "scadabridge-site-a-a", 8082)]
|
|
[InlineData("akka.tcp://scadabridge@10.0.0.5:8082/", "10.0.0.5", 8082)]
|
|
[InlineData(" akka.tcp://scadabridge@host.lan:1234 ", "host.lan", 1234)]
|
|
public void TryParse_extracts_host_and_port(string seed, string expectedHost, int expectedPort)
|
|
{
|
|
Assert.True(ClusterBootstrapGuard.TryParse(seed, out var host, out var port));
|
|
Assert.Equal(expectedHost, host);
|
|
Assert.Equal(expectedPort, port);
|
|
}
|
|
|
|
/// <summary>Verifies that parsing rejects malformed seed addresses.</summary>
|
|
/// <param name="seed">The malformed seed address.</param>
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("garbage")]
|
|
[InlineData("akka.tcp://scadabridge@host")] // no port
|
|
public void TryParse_rejects_malformed_seeds(string seed)
|
|
{
|
|
Assert.False(ClusterBootstrapGuard.TryParse(seed, out _, out _));
|
|
}
|
|
|
|
/// <summary>Verifies that the port decides the tie-break when the hosts are equal.</summary>
|
|
[Fact]
|
|
public void Port_participates_in_the_tie_break_when_hosts_are_equal()
|
|
{
|
|
// Shared-host loopback pair (test rig): same host, different ports — port breaks the tie.
|
|
const string low = "akka.tcp://scadabridge@127.0.0.1:8081";
|
|
const string high = "akka.tcp://scadabridge@127.0.0.1:8082";
|
|
|
|
Assert.True(ClusterBootstrapGuard.Analyze(new[] { low, high }, "127.0.0.1", 8081).IsFounder);
|
|
Assert.False(ClusterBootstrapGuard.Analyze(new[] { high, low }, "127.0.0.1", 8082).IsFounder);
|
|
}
|
|
}
|