fix(scripting): #1064 the ScriptWorker applies the in-process result rule — a returned lazy sequence is materialised inside the run and held to Scripting:MaxResultElements (Hello.max_result_elements), same value and same failure text in both runners

This commit is contained in:
Joseph Doherty
2026-12-03 16:35:01 -05:00
parent fe4bbbe256
commit a06dc8dd9f
8 changed files with 85 additions and 5 deletions
@@ -80,6 +80,58 @@ public sealed class ParityTests
Same(actual.Value, expected.Value, typeName);
}
/// <summary>
/// lmxopcua#1064: the result rule of #1059 (<see cref="ScriptResultMaterializer"/>) holds in both runners — a lazy
/// sequence becomes the same typed array, and an over-cap / open-slot sequence fails with the same reason. Before
/// the fix the worker answered every lazy sequence "unsupported type" and shipped an over-cap array whole.
/// </summary>
public static TheoryData<string, string> ResultCases() => new()
{
{ "lazy query", "return System.Linq.Enumerable.Range(0, 3).Select(i => i * 1.5);" },
{ "iterator", "System.Collections.Generic.IEnumerable<int> F() { for (var i = 0; i < 3; i++) yield return i; } return F();" },
{ "endless iterator", "System.Collections.Generic.IEnumerable<double> F() { var i = 0.0; while (true) yield return i++; } return F();" },
{ "over-cap lazy", "return System.Linq.Enumerable.Range(0, int.MaxValue).Select(i => (double)i);" },
{ "over-cap array", "return new double[5];" },
{ "at-cap array", "return new double[4];" },
{ "empty lazy", "return System.Linq.Enumerable.Empty<string>();" },
};
[Theory]
[MemberData(nameof(ResultCases))]
public async Task Result_materialisation_and_the_element_cap_agree(string name, string source)
{
const int cap = 4;
using var inProcess = new InProcessScriptRunner(new ScriptingOptions { MaxResultElements = cap });
var options = Scripts.Options();
options.MaxResultElements = cap;
await using var outOfProcess = Scripts.Runner(options);
var invocation = Scripts.Vt("result-" + name, source);
var expected = await inProcess.RunAsync(invocation, Ct);
var actual = await outOfProcess.RunAsync(invocation, Ct);
actual.Status.ShouldBe(expected.Status, $"{name}: {actual.Reason} vs {expected.Reason}");
actual.Reason.ShouldBe(expected.Reason);
Same(actual.Value, expected.Value, name);
}
[Fact]
public async Task The_worker_holds_a_lazy_result_to_the_configured_cap()
{
// Positive control first (the cap is not simply "always fails"): 3 elements under a cap of 3 materialise.
var options = Scripts.Options();
options.MaxResultElements = 3;
await using var runner = Scripts.Runner(options);
var ok = await runner.RunAsync(Scripts.Vt("cap-ok", "return System.Linq.Enumerable.Range(0, 3).Select(i => (double)i);"), Ct);
ok.Status.ShouldBe(ScriptRunStatus.Ok, ok.Reason);
ok.Value.ShouldBeOfType<double[]>().ShouldBe([0.0, 1.0, 2.0]);
var over = await runner.RunAsync(Scripts.Vt("cap-over", "return System.Linq.Enumerable.Range(0, 4).Select(i => (double)i);"), Ct);
over.Status.ShouldBe(ScriptRunStatus.Failed);
over.Reason.ShouldNotBeNull().ShouldContain("more than 3 elements");
}
private static void Same(object? actual, object? expected, string because)
{
if (expected is null)