Group all 69 projects into category subfolders under src/ and tests/ so the Rider Solution Explorer mirrors the module structure. Folders: Core, Server, Drivers (with a nested Driver CLIs subfolder), Client, Tooling. - Move every project folder on disk with git mv (history preserved as renames). - Recompute relative paths in 57 .csproj files: cross-category ProjectReferences, the lib/ HintPath+None refs in Driver.Historian.Wonderware, and the external mxaccessgw refs in Driver.Galaxy and its test project. - Rebuild ZB.MOM.WW.OtOpcUa.slnx with nested solution folders. - Re-prefix project paths in functional scripts (e2e, compliance, smoke SQL, integration, install). Build green (0 errors); unit tests pass. Docs left for a separate pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
135 lines
5.5 KiB
C#
135 lines
5.5 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests;
|
|
|
|
/// <summary>
|
|
/// Verifies the per-evaluation timeout wrapper. Fast scripts complete normally;
|
|
/// CPU-bound or hung scripts throw <see cref="ScriptTimeoutException"/> instead of
|
|
/// starving the engine. Caller-supplied cancellation tokens take precedence over the
|
|
/// timeout so driver-shutdown paths see a clean cancel rather than a timeout.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class TimedScriptEvaluatorTests
|
|
{
|
|
[Fact]
|
|
public async Task Fast_script_completes_under_timeout_and_returns_value()
|
|
{
|
|
var inner = ScriptEvaluator<FakeScriptContext, double>.Compile(
|
|
"""return (double)ctx.GetTag("In").Value + 1.0;""");
|
|
var timed = new TimedScriptEvaluator<FakeScriptContext, double>(
|
|
inner, TimeSpan.FromSeconds(1));
|
|
|
|
var ctx = new FakeScriptContext().Seed("In", 41.0);
|
|
var result = await timed.RunAsync(ctx, TestContext.Current.CancellationToken);
|
|
result.ShouldBe(42.0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Script_longer_than_timeout_throws_ScriptTimeoutException()
|
|
{
|
|
// Scripts can't easily do Thread.Sleep in the sandbox (System.Threading.Thread
|
|
// is denied). But a tight CPU loop exceeds any short timeout.
|
|
var inner = ScriptEvaluator<FakeScriptContext, int>.Compile(
|
|
"""
|
|
var end = Environment.TickCount64 + 5000;
|
|
while (Environment.TickCount64 < end) { }
|
|
return 1;
|
|
""");
|
|
var timed = new TimedScriptEvaluator<FakeScriptContext, int>(
|
|
inner, TimeSpan.FromMilliseconds(50));
|
|
|
|
var ex = await Should.ThrowAsync<ScriptTimeoutException>(async () =>
|
|
await timed.RunAsync(new FakeScriptContext(), TestContext.Current.CancellationToken));
|
|
ex.Timeout.ShouldBe(TimeSpan.FromMilliseconds(50));
|
|
ex.Message.ShouldContain("50.0");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Caller_cancellation_takes_precedence_over_timeout()
|
|
{
|
|
// A CPU-bound script that would otherwise timeout; external ct fires first.
|
|
// Expected: OperationCanceledException (not ScriptTimeoutException) so shutdown
|
|
// paths aren't misclassified as timeouts.
|
|
var inner = ScriptEvaluator<FakeScriptContext, int>.Compile(
|
|
"""
|
|
var end = Environment.TickCount64 + 10000;
|
|
while (Environment.TickCount64 < end) { }
|
|
return 1;
|
|
""");
|
|
var timed = new TimedScriptEvaluator<FakeScriptContext, int>(
|
|
inner, TimeSpan.FromSeconds(5));
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(80));
|
|
await Should.ThrowAsync<OperationCanceledException>(async () =>
|
|
await timed.RunAsync(new FakeScriptContext(), cts.Token));
|
|
}
|
|
|
|
[Fact]
|
|
public void Default_timeout_is_250ms_per_plan()
|
|
{
|
|
TimedScriptEvaluator<FakeScriptContext, int>.DefaultTimeout
|
|
.ShouldBe(TimeSpan.FromMilliseconds(250));
|
|
}
|
|
|
|
[Fact]
|
|
public void Zero_or_negative_timeout_is_rejected_at_construction()
|
|
{
|
|
var inner = ScriptEvaluator<FakeScriptContext, int>.Compile("""return 1;""");
|
|
Should.Throw<ArgumentOutOfRangeException>(() =>
|
|
new TimedScriptEvaluator<FakeScriptContext, int>(inner, TimeSpan.Zero));
|
|
Should.Throw<ArgumentOutOfRangeException>(() =>
|
|
new TimedScriptEvaluator<FakeScriptContext, int>(inner, TimeSpan.FromMilliseconds(-1)));
|
|
}
|
|
|
|
[Fact]
|
|
public void Null_inner_is_rejected()
|
|
{
|
|
Should.Throw<ArgumentNullException>(() =>
|
|
new TimedScriptEvaluator<FakeScriptContext, int>(null!));
|
|
}
|
|
|
|
[Fact]
|
|
public void Null_context_is_rejected()
|
|
{
|
|
var inner = ScriptEvaluator<FakeScriptContext, int>.Compile("""return 1;""");
|
|
var timed = new TimedScriptEvaluator<FakeScriptContext, int>(inner);
|
|
Should.ThrowAsync<ArgumentNullException>(async () =>
|
|
await timed.RunAsync(null!, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Script_exception_propagates_unwrapped()
|
|
{
|
|
// User-thrown exceptions must come through as-is — NOT wrapped in
|
|
// ScriptTimeoutException. The virtual-tag engine catches them per-tag and
|
|
// maps to BadInternalError; conflating with timeout would lose that info.
|
|
var inner = ScriptEvaluator<FakeScriptContext, int>.Compile(
|
|
"""throw new InvalidOperationException("script boom");""");
|
|
var timed = new TimedScriptEvaluator<FakeScriptContext, int>(inner, TimeSpan.FromSeconds(1));
|
|
|
|
var ex = await Should.ThrowAsync<InvalidOperationException>(async () =>
|
|
await timed.RunAsync(new FakeScriptContext(), TestContext.Current.CancellationToken));
|
|
ex.Message.ShouldBe("script boom");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ScriptTimeoutException_message_points_at_diagnostic_path()
|
|
{
|
|
var inner = ScriptEvaluator<FakeScriptContext, int>.Compile(
|
|
"""
|
|
var end = Environment.TickCount64 + 5000;
|
|
while (Environment.TickCount64 < end) { }
|
|
return 1;
|
|
""");
|
|
var timed = new TimedScriptEvaluator<FakeScriptContext, int>(
|
|
inner, TimeSpan.FromMilliseconds(30));
|
|
|
|
var ex = await Should.ThrowAsync<ScriptTimeoutException>(async () =>
|
|
await timed.RunAsync(new FakeScriptContext(), TestContext.Current.CancellationToken));
|
|
ex.Message.ShouldContain("ctx.Logger");
|
|
ex.Message.ShouldContain("widening the timeout");
|
|
}
|
|
}
|