61546d5aab
ScriptResultMaterializer returned a non-array ICollection as-is (capped by Count only), so in process the host got the List<double> itself while the out-of-process worker, whose wire carries arrays but no collection type, answered "unsupported type" for the same script. Every non-array, non-string sequence now goes through the one materialisation: a collection is held to Scripting:MaxResultElements by its Count before it is copied, then becomes T[] of its IEnumerable<T> element type; an open element type (List<object>, List<List<int>>) or none (ArrayList) is refused like the lazy equivalent. Both runners give double[] for a List<double>. ParityTests.Result_materialisation_and_the_element_cap_agree gains list / at-cap / over-cap / hash set / queue / List<object> / nested-list rows plus a positive control (6 rows + the control fail against the old materializer). ContainmentTests' "unsupported result type" example moves from List<int> (now int[]) to KeyValuePair<int,int>. Behaviour change: a script returning List<object> (or another open element type) now fails the run (Bad + script log) in process too, as the lazy form already did.
177 lines
9.0 KiB
C#
177 lines
9.0 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests;
|
|
|
|
/// <summary>
|
|
/// lmxopcua#780 design §13 "Parity": the same script, through <see cref="InProcessScriptRunner"/> (today's guard)
|
|
/// and through the worker, gives the same status, reason, value and exact CLR value type.
|
|
/// </summary>
|
|
[Collection(ScriptWorkerCollection.Name)]
|
|
[Trait("Category", "ScriptWorker")]
|
|
public sealed class ParityTests
|
|
{
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
|
|
public static TheoryData<string, string> Cases() => new()
|
|
{
|
|
{ "arithmetic", "return 1 + 2;" },
|
|
{ "string", "return \"a\" + ctx.GetTag(\"x\").Value;" },
|
|
{ "throw", "throw new System.InvalidOperationException(\"boom\");" },
|
|
{ "compile error", "return undefinedThing;" },
|
|
{ "sandbox violation", "return System.IO.File.Exists(\"x\");" },
|
|
{ "missing input status", "return ctx.GetTag(\"missing\").StatusCode;" },
|
|
{ "input status", "return ctx.GetTag(\"x\").StatusCode;" },
|
|
{ "source timestamp", "return ctx.GetTag(\"x\").SourceTimestampUtc;" },
|
|
{ "clock", "return ctx.Now > new System.DateTime(2000, 1, 1);" },
|
|
{ "null", "return null;" },
|
|
{ "array", "return new double[] { 1.5, 2.5 };" },
|
|
{ "cast of wrong type", "return (short)ctx.GetTag(\"x\").Value;" },
|
|
};
|
|
|
|
private static readonly IReadOnlyDictionary<string, DataValueSnapshot> Inputs =
|
|
new Dictionary<string, DataValueSnapshot>(StringComparer.Ordinal)
|
|
{
|
|
["x"] = new DataValueSnapshot(5, 0x40000000u, new DateTime(2026, 1, 2, 3, 4, 5, DateTimeKind.Utc), DateTime.UtcNow),
|
|
};
|
|
|
|
[Theory]
|
|
[MemberData(nameof(Cases))]
|
|
public async Task In_and_out_of_process_agree(string name, string source)
|
|
{
|
|
using var inProcess = new InProcessScriptRunner();
|
|
await using var outOfProcess = Scripts.Runner(Scripts.Options());
|
|
var invocation = Scripts.Vt(name, source, inputs: Inputs);
|
|
|
|
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);
|
|
}
|
|
|
|
public static TheoryData<string, object> Typed() => new()
|
|
{
|
|
{ "bool", true }, { "sbyte", (sbyte)-2 }, { "byte", (byte)2 }, { "short", (short)-3 }, { "ushort", (ushort)3 },
|
|
{ "int", -4 }, { "uint", 4u }, { "long", -5L }, { "ulong", 5UL }, { "float", 1.25f }, { "double", 2.5 },
|
|
{ "string", "s" }, { "System.DateTime", new DateTime(2026, 11, 9, 0, 0, 0, DateTimeKind.Utc) },
|
|
};
|
|
|
|
/// <summary>
|
|
/// Unboxing parity (pins design assumption A-7): <c>(T)ctx.GetTag("x").Value</c> works out of process for every
|
|
/// tag type, which needs the exact boxed CLR type to survive the wire.
|
|
/// </summary>
|
|
[Theory]
|
|
[MemberData(nameof(Typed))]
|
|
public async Task Unboxing_to_the_exact_type_matches(string typeName, object value)
|
|
{
|
|
using var inProcess = new InProcessScriptRunner();
|
|
await using var outOfProcess = Scripts.Runner(Scripts.Options());
|
|
var invocation = Scripts.Vt("unbox-" + typeName, $"var v = ({typeName})ctx.GetTag(\"x\").Value; return v;",
|
|
inputs: Scripts.Input("x", value));
|
|
|
|
var expected = await inProcess.RunAsync(invocation, Ct);
|
|
var actual = await outOfProcess.RunAsync(invocation, Ct);
|
|
expected.Status.ShouldBe(ScriptRunStatus.Ok, expected.Reason);
|
|
actual.Status.ShouldBe(ScriptRunStatus.Ok, actual.Reason);
|
|
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>();" },
|
|
// lmxopcua#1092: a concrete non-array collection. In process it used to come back as the List itself (capped by
|
|
// Count, but not an array), while the worker answered "unsupported type" — now both return the same typed array.
|
|
{ "list", "return new System.Collections.Generic.List<double> { 1.5, 2.5 };" },
|
|
{ "at-cap list", "return new System.Collections.Generic.List<int> { 1, 2, 3, 4 };" },
|
|
{ "over-cap list", "return new System.Collections.Generic.List<double>(new double[5]);" },
|
|
{ "hash set", "return new System.Collections.Generic.HashSet<string> { \"a\" };" },
|
|
{ "queue", "var q = new System.Collections.Generic.Queue<long>(); q.Enqueue(3); q.Enqueue(4); return q;" },
|
|
{ "list of object", "return new System.Collections.Generic.List<object> { 1, \"a\" };" },
|
|
{ "nested list", "return new System.Collections.Generic.List<System.Collections.Generic.List<int>> { new() { 1 } };" },
|
|
};
|
|
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// lmxopcua#1092 positive control for the theory above (it would also pass if both runners refused a List): a
|
|
/// returned <c>List<double></c> is the same <c>double[]</c> in both runners.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task A_returned_list_is_the_same_typed_array_in_both_runners()
|
|
{
|
|
using var inProcess = new InProcessScriptRunner(new ScriptingOptions { MaxResultElements = 4 });
|
|
var options = Scripts.Options();
|
|
options.MaxResultElements = 4;
|
|
await using var outOfProcess = Scripts.Runner(options);
|
|
var invocation = Scripts.Vt("result-list-control", "return new System.Collections.Generic.List<double> { 1.5, 2.5 };");
|
|
|
|
foreach (var outcome in new[] { await inProcess.RunAsync(invocation, Ct), await outOfProcess.RunAsync(invocation, Ct) })
|
|
{
|
|
outcome.Status.ShouldBe(ScriptRunStatus.Ok, outcome.Reason);
|
|
outcome.Value.ShouldBeOfType<double[]>().ShouldBe([1.5, 2.5]);
|
|
}
|
|
}
|
|
|
|
[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)
|
|
{
|
|
actual.ShouldBeNull(because);
|
|
return;
|
|
}
|
|
actual.ShouldNotBeNull(because);
|
|
actual.GetType().ShouldBe(expected.GetType(), because);
|
|
if (expected is Array array) ((Array)actual).Cast<object?>().ShouldBe(array.Cast<object?>(), because);
|
|
else actual.ShouldBe(expected, because);
|
|
}
|
|
}
|