using System;
using System.Collections.Generic;
using ZB.MOM.WW.MxGateway.Contracts;
using ZB.MOM.WW.MxGateway.Worker.Ipc;
namespace ZB.MOM.WW.MxGateway.Worker.Tests.Ipc;
///
/// Covers : the gateway conveys the
/// worker's heartbeat send interval and watchdog stuck ceiling through the launch environment
/// (mxaccessgw#218), and a bad value must fall back rather than keep the worker from starting.
///
public sealed class WorkerPipeSessionOptionsTests
{
/// Verifies that launcher-provided values replace the compiled defaults.
[Fact]
public void FromEnvironment_WithLauncherValues_UsesThem()
{
WorkerPipeSessionOptions options = WorkerPipeSessionOptions.FromEnvironment(Lookup(
(WorkerLaunchEnvironment.HeartbeatSendIntervalMilliseconds, "3000"),
(WorkerLaunchEnvironment.HeartbeatStuckCeilingMilliseconds, "240000")));
Assert.Equal(TimeSpan.FromSeconds(3), options.HeartbeatInterval);
Assert.Equal(TimeSpan.FromSeconds(240), options.HeartbeatStuckCeiling);
Assert.Equal(WorkerPipeSessionOptions.DefaultHeartbeatGrace, options.HeartbeatGrace);
options.Validate();
}
/// Verifies that a launcher-provided grace replaces the compiled default (mxaccessgw#224).
[Fact]
public void FromEnvironment_WithGrace_UsesItAndStaysValid()
{
WorkerPipeSessionOptions options = WorkerPipeSessionOptions.FromEnvironment(Lookup(
(WorkerLaunchEnvironment.HeartbeatGraceMilliseconds, "30000"),
(WorkerLaunchEnvironment.HeartbeatStuckCeilingMilliseconds, "120000")));
Assert.Equal(TimeSpan.FromSeconds(30), options.HeartbeatGrace);
Assert.Equal(TimeSpan.FromSeconds(120), options.HeartbeatStuckCeiling);
options.Validate();
}
/// Verifies that a grace the ceiling does not exceed is dropped rather than failing the worker.
[Fact]
public void FromEnvironment_WithGraceAtOrAboveCeiling_FallsBackToDefaultGrace()
{
WorkerPipeSessionOptions options = WorkerPipeSessionOptions.FromEnvironment(Lookup(
(WorkerLaunchEnvironment.HeartbeatGraceMilliseconds, "80000")));
Assert.Equal(WorkerPipeSessionOptions.DefaultHeartbeatGrace, options.HeartbeatGrace);
Assert.Equal(WorkerPipeSessionOptions.DefaultHeartbeatStuckCeiling, options.HeartbeatStuckCeiling);
options.Validate();
}
/// Verifies that an absent environment yields the compiled defaults.
[Fact]
public void FromEnvironment_WithNothingSet_KeepsDefaults()
{
WorkerPipeSessionOptions options = WorkerPipeSessionOptions.FromEnvironment(Lookup());
Assert.Equal(WorkerPipeSessionOptions.DefaultHeartbeatInterval, options.HeartbeatInterval);
Assert.Equal(WorkerPipeSessionOptions.DefaultHeartbeatStuckCeiling, options.HeartbeatStuckCeiling);
}
///
/// Verifies that unusable values fall back to the defaults, including a ceiling that would
/// fail by not exceeding the grace.
///
/// Send-interval environment value.
/// Stuck-ceiling environment value.
[Theory]
[InlineData("0", "0")]
[InlineData("-5", "-5")]
[InlineData("abc", "1.5")]
[InlineData("", "15000")]
[InlineData("99999999999", "99999999999")]
public void FromEnvironment_WithUnusableValues_FallsBackToDefaults(string interval, string ceiling)
{
WorkerPipeSessionOptions options = WorkerPipeSessionOptions.FromEnvironment(Lookup(
(WorkerLaunchEnvironment.HeartbeatSendIntervalMilliseconds, interval),
(WorkerLaunchEnvironment.HeartbeatStuckCeilingMilliseconds, ceiling)));
Assert.Equal(WorkerPipeSessionOptions.DefaultHeartbeatInterval, options.HeartbeatInterval);
Assert.Equal(WorkerPipeSessionOptions.DefaultHeartbeatStuckCeiling, options.HeartbeatStuckCeiling);
options.Validate();
}
private static Func Lookup(params (string Name, string Value)[] values)
{
Dictionary map = new(StringComparer.Ordinal);
foreach ((string name, string value) in values)
{
map[name] = value;
}
return name => map.TryGetValue(name, out string? value) ? value : null;
}
}