fix(grpc-data): A1 review S1 — a scripted condition is established only by a completed predicate
NoteInputsEstablishment raised 'established' before AreInputsReady, the evidence check and the predicate, so a pushed-but-Bad input (driver down), a runner that was not up or a predicate timeout produced a known-inactive record though no predicate had completed. The flip is now raised after the first predicate evaluation that completes since the load or a revert; reverting an input to never-pushed still withdraws it.
This commit is contained in:
+73
@@ -103,6 +103,79 @@ public sealed class ScriptedAlarmEngineInputsEstablishedTests
|
||||
log.Count(l => l == "inputs:a1:True").ShouldBe(2);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- review S1: established = a COMPLETED predicate
|
||||
|
||||
private const uint StatusBad = 0x80000000u;
|
||||
|
||||
/// <summary>Review S1: an input that is pushed but Bad (driver down) holds the condition; no predicate ran, so the
|
||||
/// state is not established. The first Good value runs the predicate and only then establishes it.</summary>
|
||||
[Fact]
|
||||
public async Task A_Bad_input_never_establishes_and_the_first_good_evaluation_does()
|
||||
{
|
||||
var up = new FakeUpstream();
|
||||
up.Set("Temp", 150, StatusBad);
|
||||
var (engine, log) = Build(up);
|
||||
using var _ = engine;
|
||||
await engine.LoadAsync([Alarm("a1")], Ct);
|
||||
up.Push("Temp", 150, StatusBad);
|
||||
await Task.Delay(200, Ct);
|
||||
log.ShouldNotContain(l => l.StartsWith("inputs:", StringComparison.Ordinal));
|
||||
|
||||
up.Push("Temp", 150);
|
||||
await WaitForAsync(() => log.Contains("inputs:a1:True"));
|
||||
log.Last().ShouldBe("inputs:a1:True");
|
||||
log.ShouldContain("emit:a1:Activated");
|
||||
}
|
||||
|
||||
/// <summary>Review S1: a predicate that times out (or a runner that is not up) never completed, so nothing is
|
||||
/// established; the first evaluation that completes establishes it.</summary>
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public async Task A_predicate_timeout_or_an_unavailable_runner_never_establishes(bool timeout)
|
||||
{
|
||||
var up = new FakeUpstream();
|
||||
up.Set("Temp", 150);
|
||||
var runner = new AnsweringRunner
|
||||
{
|
||||
Answer = timeout
|
||||
? ScriptRunOutcome.Failed($"{ZB.MOM.WW.OtOpcUa.Commons.Engines.ScriptEvalFailureReasons.TimedOutPrefix}0.2s")
|
||||
: ScriptRunOutcome.Unavailable(ScriptRunOutcome.WorkerRestartedReason),
|
||||
};
|
||||
var logger = new LoggerConfiguration().CreateLogger();
|
||||
using var engine = new ScriptedAlarmEngine(up, new InMemoryAlarmStateStore(), new ScriptLoggerFactory(logger), logger,
|
||||
scriptTimeout: TimeSpan.FromMilliseconds(250), scriptRunner: runner);
|
||||
var log = new ConcurrentQueue<string>();
|
||||
engine.OnEvent += (_, e) => log.Enqueue($"emit:{e.AlarmId}:{e.Emission}");
|
||||
engine.OnInputsEstablished += (_, e) => log.Enqueue($"inputs:{e.AlarmId}:{e.Established}");
|
||||
|
||||
await engine.LoadAsync([Alarm("a1")], Ct);
|
||||
up.Push("Temp", 160);
|
||||
await Task.Delay(200, Ct);
|
||||
log.ShouldNotContain(l => l.StartsWith("inputs:", StringComparison.Ordinal));
|
||||
|
||||
runner.Answer = ScriptRunOutcome.Ok(true);
|
||||
up.Push("Temp", 170);
|
||||
await WaitForAsync(() => log.Contains("inputs:a1:True"));
|
||||
log.Last().ShouldBe("inputs:a1:True");
|
||||
}
|
||||
|
||||
/// <summary>A runner that answers every predicate with <see cref="Answer"/>.</summary>
|
||||
private sealed class AnsweringRunner : IScriptRunner
|
||||
{
|
||||
public volatile ScriptRunOutcome Answer = ScriptRunOutcome.Ok(false);
|
||||
|
||||
public Task<ScriptRunOutcome> RunAsync(ScriptInvocation invocation, CancellationToken ct = default) =>
|
||||
Task.FromResult(Answer);
|
||||
|
||||
public IReadOnlyList<Task<ScriptRunOutcome>> RunBatch(IReadOnlyList<ScriptInvocation> invocations, CancellationToken ct = default) =>
|
||||
invocations.Select(_ => Task.FromResult(Answer)).ToArray();
|
||||
|
||||
public void Reset(ScriptContextKind kind) { }
|
||||
|
||||
public IReadOnlyCollection<string> QuarantinedKeys(ScriptContextKind kind) => [];
|
||||
}
|
||||
|
||||
private static async Task WaitForAsync(Func<bool> cond, int timeoutMs = 3000)
|
||||
{
|
||||
var deadline = DateTime.UtcNow.AddMilliseconds(timeoutMs);
|
||||
|
||||
Reference in New Issue
Block a user