9dbccf231e
Where an in-process result really ends up: VirtualTagActor -> VirtualTagHostActor -> OpcUaPublishActor ->
OtOpcUaNodeManager.WriteValueLocked -> SdkValueBoundary.FromClr (VariantHelper.TryCastFrom). Probed against
SDK 2.0.1: Guid / Guid[] become an OPC UA Guid scalar/array; KeyValuePair<,>[] (a returned Dictionary),
decimal, enum, TimeSpan, char, DateTimeOffset, tuples, Nullable<T>[] and rank>1 arrays are refused, so the
node write threw, OpcUaPublishActor logged a Warning, and the node kept its PREVIOUS value and status (a
silent freeze, possibly under Good) while the worker answered "unsupported type".
Decision, per type, by what a client can see:
- Guid (scalar + 1-D array): representable -> the wire gains an additive GUID tag (15, `bytes g = 10`,
16 ToByteArray bytes; a wrong length is a protocol fault). Guid INPUTS cross too. protocol_version stays 1:
the worker ships in the host's script-worker/ folder, and an unknown tag already fails closed.
- Everything FromClr refuses: refused identically in both runners by ScriptResultMaterializer, inside the run,
with one reason ("script threw: The script returned a value of type 'KeyValuePair<Int32, Double>[]', which
an OPC UA node cannot carry; return ..."). A lazy unpublishable sequence is refused before it runs.
ScriptResultMaterializer.PublishableScalarTypes / IsPublishable own the set.
Pins: SdkValueBoundaryScriptResultTests (OpcUaServer.Tests, new Core.Scripting ref) checks IsPublishable ==
"FromClr takes it" over 34 values; ProtocolTests checks the wire carries every publishable type scalar + array;
ParityTests gains guid / guid array / guid list / dictionary / KVP[] / lazy KVPs / KVP / decimal / decimal[] /
enum / TimeSpan / char[] / 2-D rows, a Guid unboxing row, and a positive+negative control. Mutations: with the
type check disabled 11 parity tests fail; with Guid not encoded 9 protocol/parity tests fail.
Behaviour change: a value script returning a Dictionary, decimal, enum, TimeSpan, char or tuple now fails
(Bad + script log) in process too, instead of "succeeding" and leaving its node frozen. Remaining divergence:
a jagged array (publish path carries it as Variant[], worker answers "unsupported type") — follow-up.
507 lines
25 KiB
C#
507 lines
25 KiB
C#
using System.Buffers.Binary;
|
||
using Google.Protobuf;
|
||
using Shouldly;
|
||
using Xunit;
|
||
using ZB.MOM.WW.OtOpcUa.Core.Scripting.OutOfProcess;
|
||
using ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol;
|
||
using ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol.V1;
|
||
|
||
namespace ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests;
|
||
|
||
/// <summary>lmxopcua#780 design §13 — frame codec and the CLR ↔ Variant mapping.</summary>
|
||
[Trait("Category", "ScriptWorker")]
|
||
public sealed class ProtocolTests
|
||
{
|
||
public static IEnumerable<object?[]> Values()
|
||
{
|
||
object?[] values =
|
||
[
|
||
null, true, false, (sbyte)-5, (byte)250, (short)-3, (ushort)7, -42, 42u, long.MinValue, ulong.MaxValue,
|
||
1.5f, double.NaN, 3.25, "text", string.Empty, new DateTime(2026, 11, 9, 1, 2, 3, DateTimeKind.Utc),
|
||
new[] { 1, 2, 3 }, new short[] { -1, 1 }, new[] { "a", "b" }, new[] { true, false }, Array.Empty<double>(),
|
||
new[] { new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc) }, (short)0, 0, 0L, 0.0,
|
||
// lmxopcua#1100: GUID tag (additive).
|
||
new Guid("0f8fad5b-d9cb-469f-a165-70867728950e"), Guid.Empty, new[] { Guid.Empty, new Guid("0f8fad5b-d9cb-469f-a165-70867728950e") },
|
||
];
|
||
return values.Select(v => new[] { v });
|
||
}
|
||
|
||
/// <summary>The exact boxed CLR type survives (design §4.4): <c>(short)ctx.GetTag(x).Value</c> must keep working.</summary>
|
||
[Theory]
|
||
[MemberData(nameof(Values))]
|
||
public void Every_type_tag_round_trips_with_its_exact_clr_type(object? value)
|
||
{
|
||
VariantCodec.TryEncode(value, out var variant, out var unsupported).ShouldBeTrue();
|
||
unsupported.ShouldBeNull();
|
||
|
||
// Through the wire, not just the object: a payload the serializer drops must still decode.
|
||
var wire = Variant.Parser.ParseFrom(variant.ToByteArray());
|
||
var decoded = VariantCodec.Decode(wire);
|
||
|
||
if (value is null)
|
||
{
|
||
decoded.ShouldBeNull();
|
||
return;
|
||
}
|
||
decoded.ShouldNotBeNull();
|
||
decoded.GetType().ShouldBe(value.GetType());
|
||
if (value is Array expected)
|
||
((Array)decoded).Cast<object?>().ShouldBe(expected.Cast<object?>());
|
||
else if (value is double d && double.IsNaN(d))
|
||
double.IsNaN((double)decoded).ShouldBeTrue();
|
||
else
|
||
decoded.ShouldBe(value);
|
||
}
|
||
|
||
[Fact]
|
||
public void Unsupported_types_are_refused_not_converted()
|
||
{
|
||
foreach (var value in new object[]
|
||
{
|
||
new List<int> { 1 }, 1.5m, TimeSpan.FromSeconds(1), new { A = 1 }, new int[1, 1], new object[] { 1 },
|
||
new[] { new KeyValuePair<int, int>(1, 2) },
|
||
})
|
||
{
|
||
VariantCodec.TryEncode(value, out _, out var type).ShouldBeFalse(value.GetType().Name);
|
||
type.ShouldNotBeNullOrEmpty();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// lmxopcua#1100: the wire carries every type a script result may have (<see cref="Core.Scripting.ScriptResultMaterializer.PublishableScalarTypes"/>,
|
||
/// scalar and one-dimensional array), so nothing the materializer passes can fail "unsupported type" in the worker
|
||
/// alone — the divergence this issue was about (a Guid gave a value in process, "unsupported type" out of process).
|
||
/// </summary>
|
||
[Fact]
|
||
public void The_wire_carries_every_publishable_result_type_scalar_and_array()
|
||
{
|
||
foreach (var type in Core.Scripting.ScriptResultMaterializer.PublishableScalarTypes)
|
||
{
|
||
var scalar = type == typeof(string) ? "s" : Activator.CreateInstance(type)!;
|
||
var array = Array.CreateInstance(type, 2);
|
||
array.SetValue(scalar, 0);
|
||
foreach (var value in new[] { scalar, array })
|
||
{
|
||
VariantCodec.TryEncode(value, out var variant, out var unsupported).ShouldBeTrue($"{value.GetType().Name}: {unsupported}");
|
||
VariantCodec.Decode(Variant.Parser.ParseFrom(variant.ToByteArray()))!.GetType().ShouldBe(value.GetType());
|
||
}
|
||
}
|
||
}
|
||
|
||
[Fact]
|
||
public void Local_datetime_crosses_as_utc()
|
||
{
|
||
var local = new DateTime(2026, 6, 1, 12, 0, 0, DateTimeKind.Local);
|
||
VariantCodec.TryEncode(local, out var variant, out _).ShouldBeTrue();
|
||
var decoded = (DateTime)VariantCodec.Decode(variant)!;
|
||
decoded.Kind.ShouldBe(DateTimeKind.Utc);
|
||
decoded.ShouldBe(local.ToUniversalTime());
|
||
}
|
||
|
||
[Fact]
|
||
public void A_payload_that_contradicts_its_tag_is_a_protocol_fault()
|
||
{
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, S = "x" }));
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = (Variant.Types.TypeTag)99 }));
|
||
// #999 / review K-9: out-of-range payloads are the codec's documented failure too, never an OverflowException /
|
||
// ArgumentOutOfRangeException that escapes OutOfProcessScriptRunner.Complete (which catches only this type).
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, I = 1 << 20 }));
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Byte, U = 300 }));
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.DatetimeUtc, DtTicks = long.MaxValue }));
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.DatetimeUtc, DtTicks = -1 }));
|
||
var array = new Variant { Type = Variant.Types.TypeTag.Array, Array = new ArrayValue { ElementType = Variant.Types.TypeTag.Byte } };
|
||
array.Array.Items.Add(new Variant { Type = Variant.Types.TypeTag.Byte, U = 1000 });
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(array));
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Guid, G = ByteString.CopyFrom(new byte[15]) }));
|
||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Guid, S = "x" }));
|
||
}
|
||
|
||
private static Envelope Sample() => new()
|
||
{
|
||
ProtocolVersion = ScriptWorkerFrameCodec.ProtocolVersion,
|
||
Seq = 7,
|
||
Heartbeat = new Heartbeat { Inflight = 3, HeapBytes = 1234 },
|
||
};
|
||
|
||
[Fact]
|
||
public async Task A_frame_round_trips()
|
||
{
|
||
using var stream = new MemoryStream();
|
||
await ScriptWorkerFrameCodec.WriteAsync(stream, Sample(), 1024, TestContext.Current.CancellationToken);
|
||
stream.Position = 0;
|
||
var read = await ScriptWorkerFrameCodec.ReadAsync(stream, 1024, TestContext.Current.CancellationToken);
|
||
read.ShouldBe(Sample());
|
||
(await ScriptWorkerFrameCodec.ReadAsync(stream, 1024, TestContext.Current.CancellationToken)).ShouldBeNull("a clean end of stream at a frame boundary");
|
||
}
|
||
|
||
[Fact]
|
||
public async Task Oversize_frames_are_refused_on_write_and_on_read()
|
||
{
|
||
using var stream = new MemoryStream();
|
||
await Should.ThrowAsync<ScriptWorkerProtocolException>(async () =>
|
||
await ScriptWorkerFrameCodec.WriteAsync(stream, Sample(), 4, TestContext.Current.CancellationToken));
|
||
stream.Length.ShouldBe(0, "nothing is written for a refused frame");
|
||
|
||
await ScriptWorkerFrameCodec.WriteAsync(stream, Sample(), 1024, TestContext.Current.CancellationToken);
|
||
stream.Position = 0;
|
||
await Should.ThrowAsync<ScriptWorkerProtocolException>(async () =>
|
||
await ScriptWorkerFrameCodec.ReadAsync(stream, 4, TestContext.Current.CancellationToken));
|
||
}
|
||
|
||
public static TheoryData<string, byte[]> BadFrames()
|
||
{
|
||
var good = Sample().ToByteArray();
|
||
var header = new byte[4];
|
||
BinaryPrimitives.WriteInt32LittleEndian(header, good.Length);
|
||
var wrongVersion = new Envelope { ProtocolVersion = 99, Heartbeat = new Heartbeat() }.ToByteArray();
|
||
var noBody = new Envelope { ProtocolVersion = 1, Seq = 1 }.ToByteArray();
|
||
return new TheoryData<string, byte[]>
|
||
{
|
||
{ "zero length", new byte[] { 0, 0, 0, 0 } },
|
||
{ "negative length", new byte[] { 0xFF, 0xFF, 0xFF, 0xFF } },
|
||
{ "truncated header", new byte[] { 5, 0 } },
|
||
{ "truncated body", [.. header, .. good[..^1]] },
|
||
{ "garbage body", [5, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] },
|
||
{ "wrong protocol version", Frame(wrongVersion) },
|
||
{ "no body", Frame(noBody) },
|
||
};
|
||
}
|
||
|
||
private static byte[] Frame(byte[] body)
|
||
{
|
||
var header = new byte[4];
|
||
BinaryPrimitives.WriteInt32LittleEndian(header, body.Length);
|
||
return [.. header, .. body];
|
||
}
|
||
|
||
[Theory]
|
||
[MemberData(nameof(BadFrames))]
|
||
public async Task Malformed_frames_are_protocol_faults(string why, byte[] bytes)
|
||
{
|
||
using var stream = new MemoryStream(bytes);
|
||
await Should.ThrowAsync<ScriptWorkerProtocolException>(async () =>
|
||
await ScriptWorkerFrameCodec.ReadAsync(stream, 1024, TestContext.Current.CancellationToken), why);
|
||
}
|
||
|
||
/// <summary>Random bytes never escape as anything but a protocol fault (or a clean end of stream).</summary>
|
||
[Fact]
|
||
public async Task Fuzzed_input_only_ever_yields_a_protocol_fault()
|
||
{
|
||
var random = new Random(780);
|
||
for (var i = 0; i < 3000; i++)
|
||
{
|
||
var bytes = new byte[random.Next(0, 64)];
|
||
random.NextBytes(bytes);
|
||
if (bytes.Length >= 4 && random.Next(2) == 0)
|
||
BinaryPrimitives.WriteInt32LittleEndian(bytes, random.Next(1, bytes.Length));
|
||
using var stream = new MemoryStream(bytes);
|
||
try
|
||
{
|
||
while (await ScriptWorkerFrameCodec.ReadAsync(stream, 1024, TestContext.Current.CancellationToken) is { } envelope)
|
||
{
|
||
// A frame that parses must still decode its variants without an unexpected exception type.
|
||
if (envelope.BodyCase == Envelope.BodyOneofCase.Results)
|
||
foreach (var r in envelope.Results.Items)
|
||
try { VariantCodec.Decode(r.Value); } catch (ScriptWorkerProtocolException) { }
|
||
}
|
||
}
|
||
catch (ScriptWorkerProtocolException)
|
||
{
|
||
}
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------- buffered reader / combining writer (lmxopcua#789)
|
||
|
||
/// <summary>Hands out at most <c>chunk</c> bytes per read, so frames straddle reads at every possible offset.</summary>
|
||
private sealed class ChunkedStream(byte[] data, int chunk) : MemoryStream(data)
|
||
{
|
||
public override int Read(byte[] buffer, int offset, int count) => base.Read(buffer, offset, Math.Min(count, chunk));
|
||
|
||
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
|
||
=> base.ReadAsync(buffer[..Math.Min(buffer.Length, chunk)], cancellationToken);
|
||
}
|
||
|
||
private static byte[] Frames(int count, int bigEvery = 0)
|
||
{
|
||
using var stream = new MemoryStream();
|
||
for (var i = 0; i < count; i++)
|
||
{
|
||
var env = new Envelope { ProtocolVersion = 1, Seq = (ulong)i + 1, Heartbeat = new Heartbeat { Inflight = (uint)i } };
|
||
if (bigEvery > 0 && i % bigEvery == 0)
|
||
env = new Envelope { ProtocolVersion = 1, Seq = (ulong)i + 1, Fault = new Fault { Message = new string('x', 100_000 + i) } };
|
||
ScriptWorkerFrameCodec.WriteAsync(stream, env, ScriptWorkerFrameCodec.DefaultMaxFrameBytes).AsTask().GetAwaiter().GetResult();
|
||
}
|
||
return stream.ToArray();
|
||
}
|
||
|
||
/// <summary>
|
||
/// The buffered reader yields exactly the frames the per-frame codec wrote, in order, however the bytes are
|
||
/// split across reads — including frames larger than its initial 64 KiB buffer — through both its async and its
|
||
/// blocking read, and ends cleanly at a frame boundary.
|
||
/// </summary>
|
||
[Theory]
|
||
[InlineData(1, false)]
|
||
[InlineData(3, true)]
|
||
[InlineData(7, false)]
|
||
[InlineData(65_536, true)]
|
||
[InlineData(1_000_000, false)]
|
||
public async Task The_buffered_reader_reassembles_frames_split_at_any_offset(int chunk, bool blocking)
|
||
{
|
||
var bytes = Frames(200, bigEvery: 37);
|
||
var reader = new ScriptWorkerFrameReader(new ChunkedStream(bytes, chunk));
|
||
for (var i = 0; i < 200; i++)
|
||
{
|
||
var env = (blocking ? reader.Read() : await reader.ReadAsync(TestContext.Current.CancellationToken)).ShouldNotBeNull($"frame {i}");
|
||
env.Seq.ShouldBe((ulong)i + 1);
|
||
if (i % 37 == 0) env.Fault.Message.Length.ShouldBe(100_000 + i);
|
||
else env.Heartbeat.Inflight.ShouldBe((uint)i);
|
||
}
|
||
(blocking ? reader.Read() : await reader.ReadAsync(TestContext.Current.CancellationToken)).ShouldBeNull("a clean end of stream");
|
||
}
|
||
|
||
/// <summary>The buffered reader rejects exactly what the codec rejects.</summary>
|
||
[Theory]
|
||
[MemberData(nameof(BadFrames))]
|
||
public async Task The_buffered_reader_rejects_malformed_frames_like_the_codec(string why, byte[] bytes)
|
||
{
|
||
await Should.ThrowAsync<ScriptWorkerProtocolException>(async () =>
|
||
await new ScriptWorkerFrameReader(new MemoryStream(bytes), 1024).ReadAsync(TestContext.Current.CancellationToken), why);
|
||
Should.Throw<ScriptWorkerProtocolException>(() => new ScriptWorkerFrameReader(new ChunkedStream(bytes, 1), 1024).Read(), why);
|
||
}
|
||
|
||
/// <summary>Random bytes never escape the buffered reader as anything but a protocol fault (or a clean end).</summary>
|
||
[Fact]
|
||
public void Fuzzed_input_only_ever_yields_a_protocol_fault_from_the_buffered_reader()
|
||
{
|
||
var random = new Random(789);
|
||
for (var i = 0; i < 3000; i++)
|
||
{
|
||
var bytes = new byte[random.Next(0, 64)];
|
||
random.NextBytes(bytes);
|
||
if (bytes.Length >= 4 && random.Next(2) == 0)
|
||
BinaryPrimitives.WriteInt32LittleEndian(bytes, random.Next(1, bytes.Length));
|
||
var reader = new ScriptWorkerFrameReader(new ChunkedStream(bytes, random.Next(1, 9)), 1024);
|
||
try { while (reader.Read() is not null) { } }
|
||
catch (ScriptWorkerProtocolException) { }
|
||
}
|
||
}
|
||
|
||
/// <summary>A stream whose first write waits on a gate, so frames queue up behind a write in progress.</summary>
|
||
private sealed class GatedStream : MemoryStream
|
||
{
|
||
public readonly TaskCompletionSource Gate = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||
public int Writes;
|
||
|
||
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
|
||
{
|
||
if (Interlocked.Increment(ref Writes) == 1) await Gate.Task;
|
||
await base.WriteAsync(buffer, cancellationToken);
|
||
}
|
||
}
|
||
|
||
private static List<Envelope> ReadAll(byte[] bytes)
|
||
{
|
||
var reader = new ScriptWorkerFrameReader(new MemoryStream(bytes));
|
||
var frames = new List<Envelope>();
|
||
while (reader.Read() is { } env) frames.Add(env);
|
||
return frames;
|
||
}
|
||
|
||
private static Envelope Eval(ulong id) => new() { Eval = new EvalBatch { Items = { new EvalRequest { EvalId = id, Key = "k" } } } };
|
||
|
||
/// <summary>
|
||
/// The combining writer (lmxopcua#789): frames queued while a write is in progress go out in the next pass, with
|
||
/// consecutive evaluations merged into one batch, every other frame kept in its place (a Cancel is never
|
||
/// reordered ahead of the evaluation it cancels), and sequence numbers increasing.
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task The_combining_writer_batches_consecutive_evaluations_and_keeps_frame_order()
|
||
{
|
||
var stream = new GatedStream();
|
||
Exception? fault = null;
|
||
var writer = new ScriptWorkerFrameWriter(stream, ex => fault = ex, firstSeq: 1);
|
||
|
||
writer.TryWrite(Eval(1)).ShouldBeTrue(); // starts a pass that blocks on the gate
|
||
writer.TryWrite(Eval(2)).ShouldBeTrue();
|
||
writer.TryWrite(Eval(3)).ShouldBeTrue();
|
||
writer.TryWrite(new Envelope { Cancel = new Cancel { EvalId = 2 } }).ShouldBeTrue();
|
||
writer.TryWrite(Eval(4)).ShouldBeTrue();
|
||
stream.Gate.SetResult();
|
||
await Scripts.Eventually(() =>
|
||
{
|
||
try { return ReadAll(stream.ToArray()).Count == 4; }
|
||
catch (ScriptWorkerProtocolException) { return false; } // a pass caught mid-copy
|
||
}, TimeSpan.FromSeconds(5), "both passes written");
|
||
|
||
fault.ShouldBeNull();
|
||
var frames = ReadAll(stream.ToArray());
|
||
frames.Select(f => f.BodyCase).ShouldBe([Envelope.BodyOneofCase.Eval, Envelope.BodyOneofCase.Eval,
|
||
Envelope.BodyOneofCase.Cancel, Envelope.BodyOneofCase.Eval]);
|
||
frames[0].Eval.Items.Select(e => e.EvalId).ShouldBe([1UL]);
|
||
frames[1].Eval.Items.Select(e => e.EvalId).ShouldBe([2UL, 3UL], "queued behind the write in progress: one batch");
|
||
frames[2].Cancel.EvalId.ShouldBe(2UL);
|
||
frames[3].Eval.Items.Select(e => e.EvalId).ShouldBe([4UL]);
|
||
frames.Select(f => f.Seq).ShouldBe([2UL, 3UL, 4UL, 5UL]);
|
||
frames.ShouldAllBe(f => f.ProtocolVersion == ScriptWorkerFrameCodec.ProtocolVersion);
|
||
stream.Writes.ShouldBe(2, "one write per pass");
|
||
}
|
||
|
||
private const int SmallCap = 64 * 1024;
|
||
|
||
private static List<Envelope> ReadAll(byte[] bytes, int cap)
|
||
{
|
||
var reader = new ScriptWorkerFrameReader(new MemoryStream(bytes), cap);
|
||
var frames = new List<Envelope>();
|
||
while (reader.Read() is { } env) frames.Add(env);
|
||
return frames;
|
||
}
|
||
|
||
private static Envelope BigEval(ulong id, int keyChars = 8000)
|
||
=> new() { Eval = new EvalBatch { Items = { new EvalRequest { EvalId = id, Key = new string('k', keyChars) } } } };
|
||
|
||
private static EvalResult BigResult(ulong id, int chars = 8000)
|
||
=> new() { EvalId = id, Value = new Variant { Type = Variant.Types.TypeTag.String, S = new string('r', chars) } };
|
||
|
||
/// <summary>
|
||
/// #999 / review K-5: evaluations queued behind a write in progress are merged, but never past the frame cap — the
|
||
/// merged batch is cut into several frames, each within the cap, in order. Before the fix the writer merged
|
||
/// unboundedly and <c>Append</c> threw, which faults the writer and kills the worker connection.
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task Merged_evaluations_are_cut_at_the_frame_cap_not_faulted()
|
||
{
|
||
var stream = new GatedStream();
|
||
Exception? fault = null;
|
||
var writer = new ScriptWorkerFrameWriter(stream, ex => fault = ex, SmallCap, firstSeq: 1);
|
||
|
||
writer.TryWrite(Eval(1)).ShouldBeTrue(); // blocks on the gate
|
||
for (ulong id = 2; id <= 25; id++) writer.TryWrite(BigEval(id)).ShouldBeTrue(); // ≈ 3 × the cap queued
|
||
stream.Gate.SetResult();
|
||
await Scripts.Eventually(() =>
|
||
{
|
||
try { return ReadAll(stream.ToArray(), SmallCap).SelectMany(f => f.Eval.Items).Count() == 25; }
|
||
catch (ScriptWorkerProtocolException) { return false; }
|
||
}, TimeSpan.FromSeconds(5), "every evaluation written");
|
||
|
||
fault.ShouldBeNull();
|
||
var frames = ReadAll(stream.ToArray(), SmallCap);
|
||
frames.Count.ShouldBeGreaterThanOrEqualTo(4, "the merged batch was cut, not sent whole");
|
||
frames.ShouldAllBe(f => f.CalculateSize() <= SmallCap);
|
||
frames.SelectMany(f => f.Eval.Items).Select(i => i.EvalId).ShouldBe(Enumerable.Range(1, 25).Select(i => (ulong)i));
|
||
frames.Select(f => f.Seq).ShouldBe(Enumerable.Range(2, frames.Count).Select(i => (ulong)i));
|
||
}
|
||
|
||
/// <summary>K-5: ONE envelope whose items together exceed the cap (an unbounded <c>EvalBatch</c>) is split by item.</summary>
|
||
[Fact]
|
||
public void One_oversized_batch_is_split_by_item()
|
||
{
|
||
var stream = new MemoryStream();
|
||
Exception? fault = null;
|
||
var writer = new ScriptWorkerFrameWriter(stream, ex => fault = ex, SmallCap, firstSeq: 1, synchronous: true);
|
||
var batch = new EvalBatch();
|
||
for (ulong id = 1; id <= 30; id++) batch.Items.Add(new EvalRequest { EvalId = id, Key = new string('k', 8000) });
|
||
|
||
writer.TryWrite(new Envelope { Eval = batch }).ShouldBeTrue();
|
||
|
||
fault.ShouldBeNull();
|
||
var frames = ReadAll(stream.ToArray(), SmallCap);
|
||
frames.Count.ShouldBeGreaterThan(1);
|
||
frames.ShouldAllBe(f => f.CalculateSize() <= SmallCap);
|
||
frames.SelectMany(f => f.Eval.Items).Select(i => i.EvalId).ShouldBe(Enumerable.Range(1, 30).Select(i => (ulong)i));
|
||
}
|
||
|
||
/// <summary>
|
||
/// K-5: results are cut at the cap the same way, the started / deferred / soft-expired ids still arrive, and a
|
||
/// single RESULT too large for any frame fails that one evaluation (unsupported type) instead of the connection.
|
||
/// </summary>
|
||
[Fact]
|
||
public void Results_are_cut_at_the_cap_and_one_oversized_result_fails_alone()
|
||
{
|
||
var stream = new MemoryStream();
|
||
Exception? fault = null;
|
||
var writer = new ScriptWorkerFrameWriter(stream, ex => fault = ex, SmallCap, firstSeq: 1, synchronous: true);
|
||
var results = new EvalResults { StartedEvalIds = { 100, 101 }, DeferredEvalIds = { 102 }, SoftExpiredEvalIds = { 103 } };
|
||
for (ulong id = 1; id <= 20; id++) results.Items.Add(BigResult(id));
|
||
results.Items.Add(BigResult(21, chars: SmallCap * 2)); // alone over the cap
|
||
results.Items.Add(BigResult(22));
|
||
|
||
writer.TryWrite(new Envelope { Results = results }).ShouldBeTrue();
|
||
|
||
fault.ShouldBeNull();
|
||
var frames = ReadAll(stream.ToArray(), SmallCap);
|
||
frames.Count.ShouldBeGreaterThan(1);
|
||
frames.ShouldAllBe(f => f.CalculateSize() <= SmallCap);
|
||
var items = frames.SelectMany(f => f.Results.Items).ToList();
|
||
items.Select(i => i.EvalId).ShouldBe(Enumerable.Range(1, 22).Select(i => (ulong)i));
|
||
var oversized = items.Single(i => i.EvalId == 21);
|
||
oversized.OutcomeCase.ShouldBe(EvalResult.OutcomeOneofCase.Error);
|
||
oversized.Error.Kind.ShouldBe(ScriptError.Types.Kind.UnsupportedType);
|
||
oversized.Error.Message.ShouldContain("frame cap");
|
||
items.Single(i => i.EvalId == 22).Value.S.Length.ShouldBe(8000, "the neighbours are untouched");
|
||
frames.SelectMany(f => f.Results.StartedEvalIds).ShouldBe([100UL, 101UL]);
|
||
frames.SelectMany(f => f.Results.DeferredEvalIds).ShouldBe([102UL]);
|
||
frames.SelectMany(f => f.Results.SoftExpiredEvalIds).ShouldBe([103UL]);
|
||
}
|
||
|
||
/// <summary>A failed write completes the writer, reports once, and later frames are refused rather than lost silently.</summary>
|
||
[Fact]
|
||
public void A_failed_write_completes_the_writer_and_reports_once()
|
||
{
|
||
var faults = 0;
|
||
var stream = new MemoryStream();
|
||
stream.Dispose();
|
||
var writer = new ScriptWorkerFrameWriter(stream, _ => Interlocked.Increment(ref faults));
|
||
writer.TryWrite(Eval(1)).ShouldBeTrue();
|
||
faults.ShouldBe(1);
|
||
writer.IsCompleted.ShouldBeTrue();
|
||
writer.TryWrite(Eval(2)).ShouldBeFalse();
|
||
faults.ShouldBe(1);
|
||
}
|
||
|
||
[Fact]
|
||
public void Backoff_doubles_from_250ms_to_a_30s_cap_with_20_percent_jitter()
|
||
{
|
||
WorkerShard.NextBackoff(1, 0.5).ShouldBe(TimeSpan.FromMilliseconds(250));
|
||
WorkerShard.NextBackoff(2, 0.5).ShouldBe(TimeSpan.FromMilliseconds(500));
|
||
WorkerShard.NextBackoff(3, 0.5).ShouldBe(TimeSpan.FromSeconds(1));
|
||
WorkerShard.NextBackoff(20, 0.5).ShouldBe(TimeSpan.FromSeconds(30));
|
||
WorkerShard.NextBackoff(1, 0).ShouldBe(TimeSpan.FromMilliseconds(200));
|
||
WorkerShard.NextBackoff(1, 0.9999999).TotalMilliseconds.ShouldBe(300, tolerance: 0.01);
|
||
WorkerShard.NextBackoff(40, 1).ShouldBe(TimeSpan.FromSeconds(36));
|
||
}
|
||
|
||
[Fact]
|
||
public void Shard_placement_is_stable_and_in_range()
|
||
{
|
||
// FNV-1a, not string.GetHashCode (randomised per process): a script stays on its shard across restarts.
|
||
OutOfProcessScriptRunner.ShardIndex("1:vt-1", 2).ShouldBe(OutOfProcessScriptRunner.ShardIndex("1:vt-1", 2));
|
||
var spread = Enumerable.Range(0, 400).Select(i => OutOfProcessScriptRunner.ShardIndex($"1:vt-{i}", 4)).ToArray();
|
||
spread.ShouldAllBe(i => i >= 0 && i < 4);
|
||
spread.Distinct().Count().ShouldBe(4);
|
||
OutOfProcessScriptRunner.ShardIndex("abc", 1000).ShouldBe((int)(0x1A47E90Bu % 1000));
|
||
}
|
||
|
||
/// <summary>
|
||
/// #999 / review K-11: the shard hash is computed without a per-call byte[] — and over exactly the bytes the old
|
||
/// <c>Encoding.UTF8.GetBytes</c> form hashed (non-ASCII and lone surrogates included), so placement is unchanged.
|
||
/// </summary>
|
||
[Fact]
|
||
public void Shard_hash_allocates_nothing_and_keeps_the_utf8_placement()
|
||
{
|
||
static int Reference(string key, int count)
|
||
{
|
||
var hash = 2166136261u;
|
||
foreach (var b in System.Text.Encoding.UTF8.GetBytes(key)) { hash ^= b; hash *= 16777619u; }
|
||
return (int)(hash % (uint)count);
|
||
}
|
||
foreach (var key in new[] { "1:vt-1", "2:Plant/Linie Ä/Füllstand", "1:€𝄞", "1:\ud800x", "" })
|
||
OutOfProcessScriptRunner.ShardIndex(key, 7919).ShouldBe(Reference(key, 7919), key);
|
||
|
||
OutOfProcessScriptRunner.ShardIndex("1:warm", 4);
|
||
var before = GC.GetAllocatedBytesForCurrentThread();
|
||
for (var i = 0; i < 10_000; i++) OutOfProcessScriptRunner.ShardIndex("1:Plant/Area/Line/Equipment/vt-42", 4);
|
||
(GC.GetAllocatedBytesForCurrentThread() - before).ShouldBe(0);
|
||
}
|
||
}
|