fix(host): resolve Host-012..015 — consume DownIfAlone in HOCON, sub-second timing precision, config-driven Serilog sinks, transient-only startup retry

This commit is contained in:
Joseph Doherty
2026-05-17 03:18:33 -04:00
parent eae4077414
commit aca65e85bb
9 changed files with 395 additions and 33 deletions

View File

@@ -37,7 +37,8 @@ public class HoconBuilderTests
};
var hocon = AkkaHostedService.BuildHocon(
node, DefaultCluster(), new[] { "Central" }, 5, 15);
node, DefaultCluster(), new[] { "Central" },
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15));
var config = ConfigurationFactory.ParseString(hocon);
Assert.Equal("central-node1", config.GetString("akka.remote.dot-netty.tcp.hostname"));
@@ -57,7 +58,8 @@ public class HoconBuilderTests
};
var hocon = AkkaHostedService.BuildHocon(
node, DefaultCluster(), new[] { "Central" }, 5, 15);
node, DefaultCluster(), new[] { "Central" },
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15));
// Must still parse, and the keys after the hostname must remain intact.
var config = ConfigurationFactory.ParseString(hocon);
@@ -79,9 +81,83 @@ public class HoconBuilderTests
};
var hocon = AkkaHostedService.BuildHocon(
node, cluster, new[] { "Central" }, 5, 15);
node, cluster, new[] { "Central" },
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15));
var config = ConfigurationFactory.ParseString(hocon);
Assert.Equal("keep oldest", config.GetString("akka.cluster.split-brain-resolver.active-strategy"));
}
private static NodeOptions DefaultNode() => new()
{
Role = "Central",
NodeHostname = "node1",
RemotingPort = 8081,
};
[Fact]
public void BuildHocon_DownIfAloneTrue_EmitsOn()
{
// Host-012: BuildHocon must consume ClusterOptions.DownIfAlone, not
// hard-code the down-if-alone token.
var cluster = DefaultCluster();
cluster.DownIfAlone = true;
var hocon = AkkaHostedService.BuildHocon(
DefaultNode(), cluster, new[] { "Central" },
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15));
var config = ConfigurationFactory.ParseString(hocon);
Assert.True(config.GetBoolean(
"akka.cluster.split-brain-resolver.keep-oldest.down-if-alone"));
}
[Fact]
public void BuildHocon_DownIfAloneFalse_EmitsOff()
{
// Host-012: setting DownIfAlone=false must actually flip the emitted token.
var cluster = DefaultCluster();
cluster.DownIfAlone = false;
var hocon = AkkaHostedService.BuildHocon(
DefaultNode(), cluster, new[] { "Central" },
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15));
var config = ConfigurationFactory.ParseString(hocon);
Assert.False(config.GetBoolean(
"akka.cluster.split-brain-resolver.keep-oldest.down-if-alone"));
}
[Fact]
public void BuildHocon_SubSecondTimings_PreservedWithoutRounding()
{
// Host-013: cluster timing values below one second must not be rounded to
// whole seconds. A 750ms heartbeat must survive as 750ms, not collapse to
// 1s — and a 500ms value must not collapse to a degenerate 0s.
var cluster = DefaultCluster();
cluster.HeartbeatInterval = TimeSpan.FromMilliseconds(750);
cluster.FailureDetectionThreshold = TimeSpan.FromMilliseconds(2600);
cluster.StableAfter = TimeSpan.FromMilliseconds(500);
var hocon = AkkaHostedService.BuildHocon(
DefaultNode(), cluster, new[] { "Central" },
TimeSpan.FromMilliseconds(2500), TimeSpan.FromMilliseconds(7500));
var config = ConfigurationFactory.ParseString(hocon);
Assert.Equal(
TimeSpan.FromMilliseconds(750),
config.GetTimeSpan("akka.cluster.failure-detector.heartbeat-interval"));
Assert.Equal(
TimeSpan.FromMilliseconds(2600),
config.GetTimeSpan("akka.cluster.failure-detector.acceptable-heartbeat-pause"));
Assert.Equal(
TimeSpan.FromMilliseconds(500),
config.GetTimeSpan("akka.cluster.split-brain-resolver.stable-after"));
Assert.Equal(
TimeSpan.FromMilliseconds(2500),
config.GetTimeSpan("akka.remote.transport-failure-detector.heartbeat-interval"));
Assert.Equal(
TimeSpan.FromMilliseconds(7500),
config.GetTimeSpan("akka.remote.transport-failure-detector.acceptable-heartbeat-pause"));
}
}

View File

@@ -0,0 +1,93 @@
using System.Reflection;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
namespace ScadaLink.Host.Tests;
/// <summary>
/// Host-014 regression: REQ-HOST-8 requires the Host's Serilog sinks
/// (console and file at minimum) to be <em>configuration-driven</em>. The sinks
/// must be defined in a <c>Serilog</c> section in <c>appsettings.json</c> and
/// applied via <c>ReadFrom.Configuration</c> — they must not be hard-coded in
/// <c>Program.cs</c>, so an operator can change the file path, rolling interval or
/// output template without recompiling.
/// </summary>
public class SerilogSinkConfigTests
{
private static string FindHostProjectDirectory()
{
var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var dir = new DirectoryInfo(assemblyDir);
while (dir != null)
{
var hostPath = Path.Combine(dir.FullName, "src", "ScadaLink.Host");
if (Directory.Exists(hostPath))
return hostPath;
dir = dir.Parent;
}
throw new DirectoryNotFoundException("Could not locate src/ScadaLink.Host");
}
[Fact]
public void ShippedAppSettings_HasSerilogSection_WithConsoleAndFileSinks()
{
var path = Path.Combine(FindHostProjectDirectory(), "appsettings.json");
using var doc = JsonDocument.Parse(File.ReadAllText(path));
Assert.True(
doc.RootElement.TryGetProperty("Serilog", out var serilog),
"appsettings.json must contain a `Serilog` section so ReadFrom.Configuration " +
"drives the sinks (REQ-HOST-8 / Host-014).");
Assert.True(
serilog.TryGetProperty("WriteTo", out var writeTo),
"the `Serilog` section must contain a `WriteTo` array defining the sinks.");
var sinkNames = writeTo.EnumerateArray()
.Select(e => e.TryGetProperty("Name", out var n) ? n.GetString() : null)
.ToList();
Assert.Contains("Console", sinkNames);
Assert.Contains("File", sinkNames);
}
[Fact]
public void LoggerConfigurationFactory_AppliesConfiguredFileSink()
{
// A `Serilog` section in configuration must actually reach the built logger
// via ReadFrom.Configuration — proving the sink set is configuration-driven.
var logDir = Path.Combine(Path.GetTempPath(), "scadalink-host014-" + Guid.NewGuid().ToString("N"));
var logPath = Path.Combine(logDir, "test-.log");
try
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Serilog:Using:0"] = "Serilog.Sinks.File",
["Serilog:WriteTo:0:Name"] = "File",
["Serilog:WriteTo:0:Args:path"] = logPath,
["Serilog:WriteTo:0:Args:rollingInterval"] = "Day",
})
.Build();
var logger = LoggerConfigurationFactory
.Build(configuration, "Central", "central", "node1")
.CreateLogger();
logger.Information("host-014 configured-sink probe");
logger.Dispose();
Assert.True(Directory.Exists(logDir), "the configured file sink must have created its directory.");
var written = Directory.GetFiles(logDir, "test-*.log");
Assert.NotEmpty(written);
Assert.Contains(
"host-014 configured-sink probe",
File.ReadAllText(written[0]));
}
finally
{
if (Directory.Exists(logDir))
Directory.Delete(logDir, recursive: true);
}
}
}

View File

@@ -57,9 +57,59 @@ public class StartupRetryTests
},
maxAttempts: 3,
initialDelay: TimeSpan.FromMilliseconds(1),
NullLogger.Instance));
NullLogger.Instance,
isTransient: _ => true));
Assert.Equal(3, attempts);
Assert.Equal("failure 3", ex.Message);
}
[Fact]
public async Task ExecuteWithRetry_NonTransientFailure_RethrowsAfterSingleAttempt()
{
// Host-015: a permanent failure (e.g. a schema-version mismatch) must NOT be
// retried — retrying it cannot succeed and only delays the fatal exit by
// minutes. The isTransient predicate classifies it as non-retryable, so the
// operation runs exactly once before the exception propagates.
var attempts = 0;
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() =>
StartupRetry.ExecuteWithRetryAsync(
"test-op",
() =>
{
attempts++;
throw new InvalidOperationException("permanent schema mismatch");
},
maxAttempts: 8,
initialDelay: TimeSpan.FromMilliseconds(1),
NullLogger.Instance,
isTransient: _ => false));
Assert.Equal(1, attempts);
Assert.Equal("permanent schema mismatch", ex.Message);
}
[Fact]
public async Task ExecuteWithRetry_TransientThenPermanent_StopsAtPermanent()
{
// A transient fault is retried; a subsequent permanent fault is not.
var attempts = 0;
await Assert.ThrowsAsync<InvalidOperationException>(() =>
StartupRetry.ExecuteWithRetryAsync(
"test-op",
() =>
{
attempts++;
if (attempts == 1)
throw new TimeoutException("transient");
throw new InvalidOperationException("permanent");
},
maxAttempts: 8,
initialDelay: TimeSpan.FromMilliseconds(1),
NullLogger.Instance,
isTransient: e => e is TimeoutException));
// 1 transient (retried) + 1 permanent (not retried) = 2.
Assert.Equal(2, attempts);
}
}