fix(site-runtime): failed trigger evaluation keeps state, RateOfChange clears when flat, WhileTrue re-fires after seed adoption (SRT-2, SRT-3, SRT-4, WP3 task 3.7)

This commit is contained in:
Joseph Doherty
2026-12-03 10:11:15 -05:00
parent a730bc0558
commit bc7fae2652
6 changed files with 394 additions and 78 deletions
@@ -352,6 +352,45 @@ public class AlarmActorTests : TestKit, IDisposable
Assert.Equal(AlarmState.Active, msg.State);
}
/// <summary>
/// SRT-3: a value that steps and then HOLDS produces no further sample (subscriptions report on
/// change). The alarm must clear once the step ages out of the window — pre-fix it stayed
/// Active indefinitely although the rate had been 0 since the step.
/// </summary>
[Fact]
public void AlarmActor_RateOfChange_ClearsWhenTheValueStopsChanging()
{
var instanceProbe = CreateTestProbe();
// 10 units/sec threshold, 1 sec window.
var alarm = SpawnRocAlarm(RocConfig(10, 1, "either"), instanceProbe);
var t0 = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
alarm.Tell(PressureSample(0, t0));
alarm.Tell(PressureSample(100, t0.AddMilliseconds(500))); // 200 units/sec → Active
Assert.Equal(AlarmState.Active, instanceProbe.ExpectMsg<AlarmStateChanged>(TimeSpan.FromSeconds(5)).State);
// No further sample: the 0 leaves the window ~0.5 s later and the alarm clears on its own.
var cleared = instanceProbe.ExpectMsg<AlarmStateChanged>(TimeSpan.FromSeconds(5));
Assert.Equal(AlarmState.Normal, cleared.State);
}
/// <summary>SRT-3: while the value keeps changing fast, the window tick does not clear the alarm.</summary>
[Fact]
public void AlarmActor_RateOfChange_StaysActiveWhileTheValueKeepsChangingFast()
{
var instanceProbe = CreateTestProbe();
var alarm = SpawnRocAlarm(RocConfig(10, 1, "either"), instanceProbe);
var t0 = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
alarm.Tell(PressureSample(0, t0));
alarm.Tell(PressureSample(100, t0.AddMilliseconds(500)));
Assert.Equal(AlarmState.Active, instanceProbe.ExpectMsg<AlarmStateChanged>(TimeSpan.FromSeconds(5)).State);
// A second steep step re-arms the tick; the rate over the window stays above threshold.
alarm.Tell(PressureSample(200, t0.AddMilliseconds(900)));
instanceProbe.ExpectNoMsg(TimeSpan.FromMilliseconds(300));
}
/// <summary>Verifies that an either-direction rate-of-change alarm activates on a rapid fall.</summary>
[Fact]
public void AlarmActor_RateOfChange_Either_ActivatesOnRapidFall()
@@ -1153,4 +1192,40 @@ public class AlarmActorTests : TestKit, IDisposable
var change = instanceProbe.ExpectMsg<AlarmStateChanged>(TimeSpan.FromSeconds(10));
Assert.Equal(AlarmState.Active, change.State);
}
/// <summary>
/// SRT-2: a failed/timed-out expression evaluation is "unknown" and leaves an Active alarm
/// Active. Pre-fix it was coerced to false: Active → Normal on the failure, then Normal →
/// Active (and a second on-trigger run) on the next successful evaluation.
/// </summary>
[Fact]
public void ExpressionEvalFailure_LeavesAnActiveAlarmActive()
{
var expr = CompileRawTriggerExpression("Attributes[\"A\"]?.ToString() == \"1\"");
var alarmConfig = new ResolvedAlarm
{
CanonicalName = "ExprKeepAlarm",
TriggerType = "Expression",
TriggerConfiguration = "{\"expression\":\"Attributes[\\\"A\\\"]?.ToString() == \\\"1\\\"\"}",
PriorityLevel = 1
};
var instanceProbe = CreateTestProbe();
var alarm = ActorOf(Props.Create(() => new AlarmActor(
"ExprKeepAlarm", "Pump1", instanceProbe.Ref, alarmConfig,
null, _sharedLibrary, _options,
NullLogger<AlarmActor>.Instance, expr)));
alarm.Tell(new AttributeValueChanged("Pump1", "A", "A", "1", "Good", DateTimeOffset.UtcNow));
Assert.Equal(AlarmState.Active, instanceProbe.ExpectMsg<AlarmStateChanged>(TimeSpan.FromSeconds(10)).State);
// One evaluation cannot be completed (timeout under load / task fault).
alarm.Tell(new AlarmActor.ExpressionEvalFailed(new TimeoutException("eval timed out")));
// The condition still holds; then it genuinely clears.
alarm.Tell(new AttributeValueChanged("Pump1", "A", "A", "1", "Good", DateTimeOffset.UtcNow));
alarm.Tell(new AttributeValueChanged("Pump1", "A", "A", "0", "Good", DateTimeOffset.UtcNow));
// Exactly one transition — the genuine clear. Pre-fix: Normal (the failure), Active, Normal.
Assert.Equal(AlarmState.Normal, instanceProbe.ExpectMsg<AlarmStateChanged>(TimeSpan.FromSeconds(10)).State);
instanceProbe.ExpectNoMsg(TimeSpan.FromMilliseconds(500));
}
}
@@ -616,6 +616,75 @@ public class ScriptActorTests : TestKit, IDisposable
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2));
}
/// <summary>
/// SRT-2: a REAL (non-seed) evaluation that faults or times out is "unknown", not false. Pre-fix
/// it stopped the running WhileTrue timer (and the next true evaluation fired a spurious edge run).
/// </summary>
[Fact]
public void ScriptActor_ExpressionWhileTrue_FaultedRealEval_DoesNotStopReFiring()
{
var triggerExpr = CompileTriggerExpression("Attributes[\"Active\"]?.ToString() == \"yes\"");
var (actor, instance) = CreateTriggeredActor(
"ExprRealFault",
"Expression",
"{\"expression\":\"Attributes[\\\"Active\\\"]?.ToString() == \\\"yes\\\"\",\"mode\":\"WhileTrue\"}",
TimeSpan.FromMilliseconds(300),
triggerExpr);
actor.Tell(Change("Active", "yes"));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2)); // edge fire
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2)); // tick 1
actor.Tell(new ScriptActor.ExpressionEvalFailed(new TimeoutException("eval timed out")));
// Nothing real said the condition went false: the timer keeps re-firing (pre-fix: stopped).
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2));
}
/// <summary>
/// SRT-4: a Conditional WhileTrue trigger whose condition is adopted TRUE from a seed (a
/// persisted static override after failover/restart) re-fires on its cadence — without the
/// immediate edge run a seed never makes. Pre-fix it never fired again.
/// </summary>
[Fact]
public void ScriptActor_ConditionalWhileTrue_SeededTrue_StartsReFiringWithoutAnEdgeRun()
{
var (actor, instance) = CreateTriggeredActor(
"CondSeed",
"Conditional",
"{\"attributeName\":\"Temp\",\"operator\":\">\",\"threshold\":50,\"mode\":\"WhileTrue\"}",
TimeSpan.FromMilliseconds(300));
var sw = System.Diagnostics.Stopwatch.StartNew();
actor.Tell(new ZB.MOM.WW.ScadaBridge.SiteRuntime.Messages.AttributeValueSeed(Change("Temp", "100")));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3)); // pre-fix: never
// The first run is a TIMER tick (≥ one 300 ms interval), not an immediate edge run.
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(250), $"first run after {sw.Elapsed} — seed fired an edge run");
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3)); // and it keeps re-firing
}
/// <summary>SRT-4: the Expression twin — a seed-only evaluation that is true starts the WhileTrue timer.</summary>
[Fact]
public void ScriptActor_ExpressionWhileTrue_SeededTrue_StartsReFiringWithoutAnEdgeRun()
{
var triggerExpr = CompileTriggerExpression("Attributes[\"Active\"]?.ToString() == \"yes\"");
var (actor, instance) = CreateTriggeredActor(
"ExprSeed",
"Expression",
"{\"expression\":\"Attributes[\\\"Active\\\"]?.ToString() == \\\"yes\\\"\",\"mode\":\"WhileTrue\"}",
TimeSpan.FromMilliseconds(300),
triggerExpr);
var sw = System.Diagnostics.Stopwatch.StartNew();
actor.Tell(new ZB.MOM.WW.ScadaBridge.SiteRuntime.Messages.AttributeValueSeed(Change("Active", "yes")));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3)); // pre-fix: never
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(250), $"first run after {sw.Elapsed} — seed fired an edge run");
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3));
}
/// <summary>Verifies that an on-true expression trigger fires once per false-to-true edge.</summary>
[Fact]
public void ScriptActor_ExpressionOnTrue_FiresOncePerFalseToTrueEdge()