using Shouldly; using Xunit; namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests; /// /// lmxopcua#1059 — a script that returns an iterator or an unfinished LINQ query used to hand the host a sequence /// nothing had run yet; the host then enumerated it on its own thread, outside the guard's timeout / allocation sample /// and with no element bound. The guard now materialises the result inside the guarded run, bounded. /// [Trait("Category", "Unit")] public sealed class ScriptResultMaterializerTests { private static IEnumerable Endless() { var i = 0.0; while (true) yield return i++; } [Fact] public void A_lazy_sequence_becomes_a_typed_array_and_runs_now() { var ran = 0; var lazy = Enumerable.Range(0, 4).Select(i => { ran++; return (double)i; }); ran.ShouldBe(0); var result = ScriptResultMaterializer.Materialize(lazy, 100).ShouldBeOfType(); result.ShouldBe([0.0, 1.0, 2.0, 3.0]); ran.ShouldBe(4); } [Fact] public void A_sequence_past_the_cap_is_cut_off_not_enumerated_forever() { var ex = Should.Throw(() => ScriptResultMaterializer.Materialize(Endless(), 1000)); ex.Message.ShouldContain("1000"); } [Fact] public void A_sequence_of_exactly_the_cap_is_accepted_boundary() => ScriptResultMaterializer.Materialize(Enumerable.Range(0, 5).Select(i => (long)i), 5) .ShouldBeOfType().Length.ShouldBe(5); [Fact] public void A_lazy_sequence_of_an_open_slot_type_is_refused() { Should.Throw(() => ScriptResultMaterializer.Materialize(Enumerable.Range(0, 3).Select(i => (object)i), 100)); Should.Throw(() => ScriptResultMaterializer.Materialize(Enumerable.Range(0, 3).Select(i => new List { i }), 100)); Should.Throw(() => ScriptResultMaterializer.Materialize(Enumerable.Range(0, 3).Select(i => (IComparable)i), 100)); } [Fact] public void Arrays_collections_strings_scalars_and_null_pass_through_unchanged_positive_controls() { var array = new[] { 1.0, 2.0 }; ScriptResultMaterializer.Materialize(array, 100).ShouldBeSameAs(array); var list = new List { 1, 2 }; ScriptResultMaterializer.Materialize(list, 100).ShouldBeSameAs(list); ScriptResultMaterializer.Materialize("abc", 100).ShouldBe("abc"); ScriptResultMaterializer.Materialize(42, 100).ShouldBe(42); ScriptResultMaterializer.Materialize(null, 100).ShouldBeNull(); } [Fact] public void An_oversized_array_or_collection_is_refused_by_the_same_cap() { Should.Throw(() => ScriptResultMaterializer.Materialize(new double[101], 100)); Should.Throw(() => ScriptResultMaterializer.Materialize(new List(new int[101]), 100)); } [Fact] public void A_zero_cap_disables_the_bound_but_still_materialises() => ScriptResultMaterializer.Materialize(Enumerable.Range(0, 3).Select(i => i), 0).ShouldBeOfType(); [Fact] public void The_options_clamp_a_non_positive_cap_to_the_default() { var sanitized = new ScriptingOptions { MaxResultElements = 0 }.Sanitized(out var notes); sanitized.MaxResultElements.ShouldBe(ScriptingOptions.DefaultMaxResultElements); notes.ShouldContain(n => n.Contains("MaxResultElements")); new ScriptingOptions().Sanitized(out var none).MaxResultElements.ShouldBe(ScriptingOptions.DefaultMaxResultElements); none.ShouldBeEmpty(); } // ---- through the guard, with a real compiled script ---- private static ScriptEvaluator Compile(string source) => ScriptEvaluator.Compile(source); [Fact] public async Task The_guard_hands_the_host_a_materialised_array_not_a_live_query() { var guard = new ScriptRunGuard(); using var eval = Compile("return System.Linq.Enumerable.Range(0, 3).Select(i => (double)i * 2);"); var result = await guard.RunAsync("k", "src", eval, new FakeScriptContext(), TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken); result.ShouldBeOfType().ShouldBe([0.0, 2.0, 4.0]); } [Fact] public async Task The_guard_cuts_off_an_endless_lazy_sequence_inside_the_run_and_does_not_strike_the_script() { var guard = new ScriptRunGuard(new ScriptingOptions { MaxResultElements = 1000, QuarantineAfterConsecutiveTimeouts = 1 }); using var eval = Compile("return System.Linq.Enumerable.Range(0, int.MaxValue).Select(i => (double)i);"); var ct = TestContext.Current.CancellationToken; await Should.ThrowAsync(() => guard.RunAsync("k", "src", eval, new FakeScriptContext(), TimeSpan.FromSeconds(10), ct)); await Should.ThrowAsync(() => guard.RunAsync("k", "src", eval, new FakeScriptContext(), TimeSpan.FromSeconds(10), ct)); guard.IsQuarantined("k", out _).ShouldBeFalse("a refused result is a failed run, not a timeout strike"); guard.InFlightCount.ShouldBe(0); } [Fact] public async Task The_guard_charges_the_enumeration_to_the_allocation_budget() { // 2 MB budget; the sequence materialises ~100k strings — the allocation happens during enumeration, which used to // be outside the sample entirely. var guard = new ScriptRunGuard(new ScriptingOptions { MaxAllocBytesPerRun = 1024 * 1024 }); using var eval = Compile("return System.Linq.Enumerable.Range(0, 90000).Select(i => new string('x', 64) + i);"); await Should.ThrowAsync(() => guard.RunAsync("k", "src", eval, new FakeScriptContext(), TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken)); guard.IsQuarantined("k", out var reason).ShouldBeTrue(); reason!.ShouldContain("allocated"); } }