test(scripting): #1038 hold the 2x-overrun orphan on a signal instead of a 200 ms wall-clock spin (a 150 ms test-thread stall reproduced the flake)

This commit is contained in:
Joseph Doherty
2026-12-03 15:12:07 -05:00
parent e840ec1fbc
commit 8225aaa18c
@@ -310,41 +310,41 @@ public sealed class ScriptRunGuardTests
}
/// <summary>
/// Positive control for the stuck rule: a 2x overrun (one timeout, refusals while the orphan is young)
/// Positive control for the stuck rule: a short overrun (one timeout, a refusal while the orphan is young)
/// is NOT quarantined, and once the orphan completes the script evaluates normally again.
/// </summary>
/// <remarks>
/// lmxopcua#1038: this used to be a WALL-CLOCK test — the orphan was a script spinning 200 ms against a 100 ms budget,
/// and the test thread had to land its refused call inside the 100 ms between the timeout and the orphan finishing
/// on its own. Any stall of the test thread longer than that (a GC pause, thread-pool starvation from the parallel
/// test classes, a descheduled runner) let the orphan finish first, so the "refused" call simply ran and returned 7 —
/// the guard right, the test's window too narrow (reproduced deterministically by delaying the test thread 150 ms
/// after the timeout). Now the orphan is HELD until the test releases it (a signal, like
/// <see cref="BriefSpinScript"/>), so it cannot finish early, and the budget is 500 ms against the default 10x
/// stuck factor so the held orphan would have to age past 5 s before the refusal call could be read as "stuck" — the
/// refusal path takes microseconds. The test no longer depends on how fast it runs, only on the order of events.
/// </remarks>
[Fact]
public async Task A_2x_overrun_is_not_quarantined_and_resumes_evaluating_once_the_orphan_completes()
public async Task A_short_overrun_is_not_quarantined_and_resumes_evaluating_once_the_orphan_completes()
{
var guard = new ScriptRunGuard();
const string source =
"""
var ms = (int)(ctx.GetTag("spinMs").Value ?? 0);
var end = System.DateTime.UtcNow.AddMilliseconds(ms);
while (System.DateTime.UtcNow < end) { }
return 7;
""";
using var eval = Compile(source);
using var spin = Compile(BriefSpinScript);
using var fast = Compile(FastScript);
var ct = TestContext.Current.CancellationToken;
var timeout = TimeSpan.FromMilliseconds(100);
var timeout = TimeSpan.FromMilliseconds(500);
var held = new FakeScriptContext();
FakeScriptContext Ctx(int spinMs)
{
var c = new FakeScriptContext();
c.Tags["spinMs"] = new Core.Abstractions.DataValueSnapshot(spinMs, 0u, c.Now, c.Now);
return c;
}
// The orphan: held past its budget (one timeout) — and it stays running until released.
await Should.ThrowAsync<ScriptTimeoutException>(() => Run(guard, "k", BriefSpinScript, spin, timeout, ct, held));
// …so the very next trigger is deterministically refused while it is young.
await Should.ThrowAsync<ScriptRunRefusedException>(() => Run(guard, "k", BriefSpinScript, spin, timeout, ct, held));
guard.IsQuarantined("k", out _).ShouldBeFalse("a short overrun is far below the 10x stuck threshold");
// Warm the JIT so the first measured run's overrun is the script's, not the compiler's.
(await guard.RunAsync("k", source, eval, Ctx(0), TimeSpan.FromSeconds(5), ct)).ShouldBe(7);
await ReleaseAndDrain(guard, held);
// 2x overrun: 200 ms against a 100 ms budget — times out, then is refused while it finishes.
await Should.ThrowAsync<ScriptTimeoutException>(() => guard.RunAsync("k", source, eval, Ctx(200), timeout, ct));
await Should.ThrowAsync<ScriptRunRefusedException>(() => guard.RunAsync("k", source, eval, Ctx(0), timeout, ct));
await WaitForIdle(guard);
guard.IsQuarantined("k", out _).ShouldBeFalse("a 2x overrun is far below the 10x stuck threshold");
(await guard.RunAsync("k", source, eval, Ctx(0), timeout, ct)).ShouldBe(7, "the script resumes evaluating");
guard.IsQuarantined("k", out _).ShouldBeFalse();
(await Run(guard, "k", BriefSpinScript, spin, timeout, ct, new FakeScriptContext { Released = true })).ShouldBe(1,
"the script resumes evaluating once the orphan has completed");
guard.QuarantinedCount.ShouldBe(0);
}