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
@@ -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>