fix(site-runtime): script Asks honour the run deadline, Notify.Status checks the buffer first, routed calls wait for the script's own timeout (SRT-8, SRT-9, SRT-13, WP3 task 3.9)

This commit is contained in:
Joseph Doherty
2026-12-03 10:15:23 -05:00
parent bc7fae2652
commit b0c40deefb
8 changed files with 201 additions and 45 deletions
@@ -163,4 +163,38 @@ public class DeploymentManagerRouteTraceTests : TestKit, IDisposable
var read = ExpectMsg<RouteToGetAttributesResponse>(TimeSpan.FromSeconds(10));
Assert.Equal("Hand", read.Values["Mode"]?.ToString());
}
/// <summary>
/// SRT-13: a routed inbound call waits as long as the TARGET script may run — its resolved
/// timeout (per-script override, else the site default) plus the stuck-script grace and a
/// margin — instead of a hard-coded 30 s that failed a 120 s handshake script's caller while the
/// script ran on.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RoutedCallTimeout_FollowsTheTargetScriptsTimeout()
{
var options = new SiteRuntimeOptions { ScriptExecutionTimeoutSeconds = 30, StuckScriptGraceMs = 1000 };
var config = new FlattenedConfiguration
{
InstanceUniqueName = "Handshake",
Scripts =
[
new ResolvedScript { CanonicalName = "Slow", Code = "return 1;", ExecutionTimeoutSeconds = 120 },
new ResolvedScript { CanonicalName = "Default", Code = "return 1;" },
],
};
var instance = ActorOf(Props.Create(() => new InstanceActor(
"Handshake", JsonSerializer.Serialize(config), _storage, _compilationService, _sharedScriptLibrary,
null, options, NullLogger<InstanceActor>.Instance)));
var slack = TimeSpan.FromSeconds(1) + DeploymentManagerActor.RoutedCallMargin;
Assert.Equal(TimeSpan.FromSeconds(120) + slack,
await DeploymentManagerActor.RoutedCallTimeoutAsync(instance, "Slow", options)); // pre-fix: 30 s flat
Assert.Equal(TimeSpan.FromSeconds(30) + slack,
await DeploymentManagerActor.RoutedCallTimeoutAsync(instance, "Default", options));
// Unknown script: the site default (the instance replies "not found" on the call itself).
Assert.Equal(TimeSpan.FromSeconds(30) + slack,
await DeploymentManagerActor.RoutedCallTimeoutAsync(instance, "Missing", options));
}
}
@@ -188,6 +188,32 @@ public class StuckScriptWatchdogTests : TestKit, IDisposable
Assert.Contains("upstream request was cancelled", result.ErrorMessage);
}
/// <summary>
/// SRT-9: a script parked in <c>Instance.CallScript</c> past its own deadline takes the timeout
/// path AT the deadline. Pre-fix the Ask was bounded only by the full script timeout measured
/// from the call (and ignored the deadline token), so the run sailed past its deadline and
/// then failed with an Ask timeout — or, had the callee answered, carried on and completed.
/// </summary>
[Fact]
public void ScriptParkedInCallScript_TimesOutAtItsDeadline()
{
using var scheduler = new ScriptExecutionScheduler(1);
var options = new SiteRuntimeOptions { ScriptExecutionTimeoutSeconds = 2, StuckScriptGraceMs = 200, MaxScriptCallDepth = 10 };
// The instance (a silent probe) never answers the nested call.
var actor = BuildScriptActor("Caller", CompileRaw(
"await Task.Delay(1000); await Instance.CallScript(\"Callee\"); return 1;"),
options, scheduler, null);
var caller = CreateTestProbe();
var sw = System.Diagnostics.Stopwatch.StartNew();
actor.Tell(new ScriptCallRequest("Caller", null, 0, "corr-nested"), caller.Ref);
var result = caller.ExpectMsg<ScriptCallResult>(TimeSpan.FromSeconds(10));
Assert.False(result.Success);
Assert.Contains("timed out", result.ErrorMessage); // pre-fix: "failed: ... Timeout ..." at ~3 s
Assert.True(sw.Elapsed < TimeSpan.FromMilliseconds(2900), $"timed out after {sw.Elapsed}");
}
/// <summary>Verifies that at the detach cap no further replacement is started and an error event is emitted.</summary>
[Fact]
public void AtTheDetachCap_NoFurtherReplacementIsStarted_AndAnErrorEventIsEmitted()
@@ -276,10 +276,14 @@ public class NotifyHelperTests : TestKit, IAsyncLifetime, IDisposable
Assert.Null(payload!.SourceScript);
}
/// <summary>Verifies that status, when still buffered at site, reports forwarding.</summary>
/// <summary>
/// Verifies that status, when still buffered at site, reports forwarding — answered
/// site-locally WITHOUT asking central (2026-12-02 review SRT-8: it used to ask central first,
/// with the script's whole execution timeout, exactly when central is unreachable).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Status_WhenStillBufferedAtSite_ReportsForwarding()
public async Task Status_WhenStillBufferedAtSite_ReportsForwarding_WithoutAskingCentral()
{
var commProbe = CreateTestProbe();
var notify = CreateHelper(commProbe.Ref);
@@ -287,20 +291,32 @@ public class NotifyHelperTests : TestKit, IAsyncLifetime, IDisposable
// Enqueue but never let it forward — the message stays buffered at the site.
var notificationId = await notify.To("Operators").Send("Pump alarm", "Pump 3 tripped");
var statusTask = notify.Status(notificationId);
// Nobody answers on the central side: the call must still return promptly.
var status = await notify.Status(notificationId).WaitAsync(TimeSpan.FromSeconds(2));
// The status query goes to central; central has no row for an un-forwarded
// notification, so it answers Found: false.
var query = await commProbe.ExpectMsgAsync<NotificationStatusQuery>();
Assert.Equal(notificationId, query.NotificationId);
commProbe.Reply(new NotificationStatusResponse(
query.CorrelationId, Found: false, Status: "Unknown",
RetryCount: 0, LastError: null, DeliveredAt: null));
var status = await statusTask;
// Found: false AND still in the site S&F buffer → the site-local Forwarding state.
Assert.Equal("Forwarding", status.Status);
commProbe.ExpectNoMsg(TimeSpan.FromMilliseconds(200)); // pre-fix: a NotificationStatusQuery
}
/// <summary>
/// SRT-8: the central query is bounded by the run's deadline token — a run whose deadline
/// fires while central is silent takes the timeout path rather than waiting out the Ask.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Status_CentralQuery_IsCancelledByTheRunDeadline()
{
var commProbe = CreateTestProbe();
using var deadline = new CancellationTokenSource();
var notify = new ScriptRuntimeContext.NotifyHelper(
_saf, commProbe.Ref, "site-7", "Plant.Pump3", null, TimeSpan.FromSeconds(30),
NullLogger.Instance, Guid.NewGuid(), scriptTimeoutToken: deadline.Token);
var statusTask = notify.Status("not-buffered-id");
await commProbe.ExpectMsgAsync<NotificationStatusQuery>();
deadline.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => statusTask.WaitAsync(TimeSpan.FromSeconds(5)));
}
/// <summary>Verifies that status, when central reports delivered, maps the central response.</summary>