Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/Configuration/ScriptRunnerSelectionTests.cs
T
Joseph Doherty 0796928504
v2-ci / unit-tests (push) Waiting to run
feat(scripting): batch submit, exact per-item deadlines, scripted-alarm seam on the out-of-process runner (lmxopcua#780)
- IScriptRunner.RunBatch: one EvalBatch frame per worker for every evaluation ready to run; per-item, streamed results.
  The default (InProcessScriptRunner) is RunAsync per item. Used by the Calculation pump (dequeue all, split at a
  repeated tag, publish in dequeue order), the VirtualTag seam (ScriptBatchCoalescer, no timer) and the scripted-alarm
  engine (whole dirty set). In process the pump and the engine stay one at a time, unchanged.
- Exact per-item deadlines: the worker hands work to an idle evaluation thread or defers it (EvalResults
  deferred_eval_ids); the host stops that run's clock and restarts T then G when it starts (started_eval_ids). A run
  queued behind a runaway is neither timed out nor struck and is a victim, not the culprit, at the kill.
- Scripted-alarm seam behind Scripting:Runner:Seams; unset Seams now means VirtualTag + Calculation, so alarms run out of
  process only when listed. Load still compiles each predicate (to PE bytes only) so a broken one fails the load; passes
  batch and apply in the serial order under the gate. Script faults freeze the condition BadInternalError; runner faults
  freeze it BadResourceUnavailable and re-evaluate on recovery; a dead worker never clears an alarm. IScriptRunnerProvider
  carries the runner to DriverHostActor.
- Perf harness: batch throughput (RunBatch vs RunAsync x n) and alarm-pass latency (in process / batched / serial);
  design §12.3 records macOS + windev. On windev the serial out-of-process alarm pass misses 250 ms from ~500 alarms;
  batched p99 <= 53 ms at 1,000.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-10 06:37:36 -05:00

190 lines
9.2 KiB
C#

using Microsoft.Extensions.Configuration;
using Serilog;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
using ZB.MOM.WW.OtOpcUa.Host.Configuration;
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.Configuration;
/// <summary>
/// lmxopcua#780 — <c>Scripting:Runner</c> binds by its documented keys (design §12), defaults to InProcess (every seam
/// gets no runner, so the in-process path is untouched), and the worker options clamp and fail closed.
/// </summary>
public sealed class ScriptRunnerSelectionTests
{
private static IConfiguration Config(Dictionary<string, string?> values) =>
new ConfigurationBuilder().AddInMemoryCollection(values).Build();
private static readonly ILogger Silent = new LoggerConfiguration().CreateLogger();
[Fact]
public void The_default_is_InProcess_and_selects_no_runner_for_any_seam()
{
var bound = ScriptingOptionsBinding.Bind(Config(new()), out var adjustments);
adjustments.ShouldBeEmpty();
bound.Runner.ShouldBe(ScriptRunnerMode.InProcess);
ScriptRunnerSelection.Validate(bound, "/srv/app").ShouldBeEmpty();
using var selection = ScriptRunnerSelection.Create(bound, Silent, null);
foreach (var seam in Enum.GetValues<ScriptSeam>()) selection.ForSeam(seam).ShouldBeNull(seam.ToString());
selection.OutOfProcessRunner.ShouldBeNull();
}
[Fact]
public void Every_documented_runner_key_binds()
{
var bound = ScriptingOptionsBinding.Bind(Config(new()
{
["Scripting:Runner"] = "OutOfProcess",
["Scripting:Runner:Seams:0"] = "Calculation",
["Scripting:Worker:Shards"] = "3",
["Scripting:Worker:HardGrace"] = "00:00:02",
["Scripting:Worker:HeapLimitBytes"] = "268435456",
["Scripting:Worker:QuarantineAfterKills"] = "4",
["Scripting:Worker:UnavailableGrace"] = "00:00:07",
["Scripting:Worker:IsolationProbationRuns"] = "9",
["Scripting:Worker:PeCacheDir"] = "/var/cache/otopcua-pe",
["Scripting:Worker:ExecutablePath"] = "/opt/w/ZB.MOM.WW.OtOpcUa.ScriptWorker.dll",
}), out var adjustments);
adjustments.ShouldBeEmpty();
bound.Runner.ShouldBe(ScriptRunnerMode.OutOfProcess);
bound.RunnerSeams.ShouldBe([ScriptSeam.Calculation]);
bound.Worker.Shards.ShouldBe(3);
bound.Worker.HardGrace.ShouldBe(TimeSpan.FromSeconds(2));
bound.Worker.HeapLimitBytes.ShouldBe(256L * 1024 * 1024);
bound.Worker.QuarantineAfterKills.ShouldBe(4);
bound.Worker.UnavailableGrace.ShouldBe(TimeSpan.FromSeconds(7));
bound.Worker.IsolationProbationRuns.ShouldBe(9);
bound.Worker.PeCacheDir.ShouldBe("/var/cache/otopcua-pe");
bound.Worker.ExecutablePath.ShouldBe("/opt/w/ZB.MOM.WW.OtOpcUa.ScriptWorker.dll");
bound.IsOutOfProcess(ScriptSeam.Calculation).ShouldBeTrue();
bound.IsOutOfProcess(ScriptSeam.VirtualTag).ShouldBeFalse();
}
[Fact]
public void A_misspelled_runner_mode_fails_startup()
=> Should.Throw<InvalidOperationException>(() =>
ScriptingOptionsBinding.Bind(Config(new() { ["Scripting:Runner"] = "OutOfProc" }), out _));
[Fact]
public void Worker_values_out_of_range_are_clamped_and_reported()
{
var bound = ScriptingOptionsBinding.Bind(Config(new()
{
["Scripting:Worker:Shards"] = "64",
["Scripting:Worker:HardGrace"] = "00:00:00.010",
["Scripting:Worker:HeapLimitBytes"] = "1024",
["Scripting:Worker:QuarantineAfterKills"] = "0",
["Scripting:Worker:UnavailableGrace"] = "00:05:00",
["Scripting:Worker:IsolationProbationRuns"] = "0",
}), out var adjustments);
bound.Worker.Shards.ShouldBe(ScriptWorkerOptions.MaxShards);
bound.Worker.HardGrace.ShouldBe(ScriptWorkerOptions.MinHardGrace);
bound.Worker.HeapLimitBytes.ShouldBe(ScriptWorkerOptions.MinHeapLimitBytes);
bound.Worker.QuarantineAfterKills.ShouldBe(1);
bound.Worker.UnavailableGrace.ShouldBe(ScriptWorkerOptions.MaxUnavailableGrace);
bound.Worker.IsolationProbationRuns.ShouldBe(1);
adjustments.Count.ShouldBe(6);
adjustments.ShouldAllBe(a => a.StartsWith("Scripting:Worker:"));
}
[Fact]
public void The_hard_grace_defaults_to_max_of_one_second_and_the_budget()
{
var worker = new ScriptWorkerOptions();
worker.EffectiveHardGrace(TimeSpan.FromMilliseconds(250)).ShouldBe(TimeSpan.FromSeconds(1));
worker.EffectiveHardGrace(TimeSpan.FromSeconds(2)).ShouldBe(TimeSpan.FromSeconds(2));
new ScriptWorkerOptions { HardGrace = TimeSpan.FromMilliseconds(400) }
.EffectiveHardGrace(TimeSpan.FromSeconds(2)).ShouldBe(TimeSpan.FromMilliseconds(400));
}
[Theory]
[InlineData("relative/cache", "must be an absolute path")]
[InlineData("{root}/pe-cache", "inside the content root")]
[InlineData("{root}", "inside the content root")]
public void A_pe_cache_dir_that_is_relative_or_inside_the_content_root_fails_startup(string dir, string why)
{
var root = Path.Combine(Path.GetTempPath(), "otopcua-root-" + Guid.NewGuid().ToString("N"));
var options = new ScriptingOptions
{
Runner = ScriptRunnerMode.OutOfProcess,
Worker = new ScriptWorkerOptions { PeCacheDir = dir.Replace("{root}", root) },
};
Should.Throw<InvalidOperationException>(() => ScriptRunnerSelection.Validate(options, root)).Message.ShouldContain(why);
}
[Fact]
public void A_pe_cache_dir_outside_the_content_root_is_accepted()
{
var root = Path.Combine(Path.GetTempPath(), "otopcua-root");
var options = new ScriptingOptions
{
Runner = ScriptRunnerMode.OutOfProcess,
Worker = new ScriptWorkerOptions { PeCacheDir = Path.Combine(Path.GetTempPath(), "otopcua-root-sibling") },
};
Should.NotThrow(() => ScriptRunnerSelection.Validate(options, root));
}
[Fact]
public void Shadow_is_not_implemented_yet_and_runs_in_process_with_a_warning()
{
var options = new ScriptingOptions { Runner = ScriptRunnerMode.Shadow };
ScriptRunnerSelection.Validate(options, "/srv").ShouldHaveSingleItem().ShouldContain("Shadow");
using var selection = ScriptRunnerSelection.Create(options, Silent, null);
selection.OutOfProcessRunner.ShouldBeNull();
}
[Fact]
public void OutOfProcess_selects_only_the_supported_requested_seams()
{
// Unset Seams: VirtualTag + Calculation. Scripted alarms stay in process unless listed (design §14 #10).
var unset = new ScriptingOptions { Runner = ScriptRunnerMode.OutOfProcess };
ScriptRunnerSelection.Validate(unset, "/srv").ShouldNotContain(w => w.Contains("cannot run out of process"));
unset.IsOutOfProcess(ScriptSeam.ScriptedAlarm).ShouldBeFalse();
unset.IsOutOfProcess(ScriptSeam.VirtualTag).ShouldBeTrue();
using (var selection = ScriptRunnerSelection.Create(unset, Silent, null))
{
selection.ForSeam(ScriptSeam.VirtualTag).ShouldNotBeNull();
selection.ForSeam(ScriptSeam.Calculation).ShouldBeSameAs(selection.ForSeam(ScriptSeam.VirtualTag), "one node-wide runner");
selection.ForSeam(ScriptSeam.ScriptedAlarm).ShouldBeNull("alarms go out of process only when listed explicitly");
}
// Listed explicitly: the alarm seam shares the node's one runner.
var withAlarms = new ScriptingOptions
{
Runner = ScriptRunnerMode.OutOfProcess,
RunnerSeams = [ScriptSeam.VirtualTag, ScriptSeam.ScriptedAlarm],
};
withAlarms.IsOutOfProcess(ScriptSeam.ScriptedAlarm).ShouldBeTrue();
ScriptRunnerSelection.Validate(withAlarms, "/srv").ShouldNotContain(w => w.Contains("cannot run out of process"));
using (var selection = ScriptRunnerSelection.Create(withAlarms, Silent, null))
{
selection.ForSeam(ScriptSeam.ScriptedAlarm).ShouldNotBeNull();
selection.ForSeam(ScriptSeam.ScriptedAlarm).ShouldBeSameAs(selection.ForSeam(ScriptSeam.VirtualTag), "one node-wide runner");
selection.ForSeam(ScriptSeam.Calculation).ShouldBeNull();
((IScriptRunnerProvider)selection).ForSeam(ScriptSeam.ScriptedAlarm).ShouldBeSameAs(selection.ForSeam(ScriptSeam.ScriptedAlarm));
}
var calcOnly = new ScriptingOptions { Runner = ScriptRunnerMode.OutOfProcess, RunnerSeams = [ScriptSeam.Calculation] };
using (var selection = ScriptRunnerSelection.Create(calcOnly, Silent, null))
{
selection.ForSeam(ScriptSeam.Calculation).ShouldNotBeNull();
selection.ForSeam(ScriptSeam.VirtualTag).ShouldBeNull();
}
}
[Fact]
public void A_missing_worker_is_a_startup_warning_not_a_silent_in_process_fallback()
{
var options = new ScriptingOptions
{
Runner = ScriptRunnerMode.OutOfProcess,
RunnerSeams = [ScriptSeam.VirtualTag],
Worker = new ScriptWorkerOptions { ExecutablePath = Path.Combine(Path.GetTempPath(), "missing-" + Guid.NewGuid().ToString("N"), "w.dll") },
};
ScriptRunnerSelection.Validate(options, "/srv").ShouldHaveSingleItem().ShouldContain("BadResourceUnavailable");
}
}