fix(dcl): treat OPC UA BadResourceUnavailable as transient Bad; Bad+null keeps last-known value (ScadaBridge#112)
OtOpcUa's out-of-process script runner publishes 0x80040000 with a null value. - OpcUaDataConnection: logs entry/recovery once per tag, names the symbolic status in read errors; never a disconnect (not in the reconnect status set). - InstanceActor: a Bad update with no value is a quality-only transition (keep value, quality Bad, stream publish, no script/alarm/waiter re-eval), matching the connection-drop rule. Bad with a value is unchanged. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
+134
@@ -0,0 +1,134 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Actors;
|
||||
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Adapters;
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#112: OtOpcUa's out-of-process script runner publishes
|
||||
/// <c>BadResourceUnavailable</c> (0x80040000) — with a null value — on script-computed nodes
|
||||
/// when its worker died or is not ready. The OPC UA adapter must treat it as a plain, transient
|
||||
/// Bad quality on the tag: no disconnect, no subscription teardown, distinct log lines on entry
|
||||
/// and recovery, and a read error that names the status.
|
||||
/// </summary>
|
||||
public class OpcUaResourceUnavailableTests
|
||||
{
|
||||
private const uint BadResourceUnavailable = 0x80040000u;
|
||||
private const string Tag = "nsu=https://zb.com/otopcua/uns;s=Line1.Pump.Calc";
|
||||
|
||||
private sealed class CapturingLogger : ILogger<OpcUaDataConnection>
|
||||
{
|
||||
public ConcurrentQueue<(LogLevel Level, string Message)> Entries { get; } = new();
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
||||
Func<TState, Exception?, string> formatter) => Entries.Enqueue((logLevel, formatter(state, exception)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Good / BadResourceUnavailable(null) / Good on a monitored item maps to Good / Bad / Good
|
||||
/// tag quality, never raises Disconnected, never removes the subscription, and logs the
|
||||
/// unavailable entry once (not per notification) plus one recovery line.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Subscription_GoodResourceUnavailableGood_IsTransientBadQuality_NotAConnectionFault()
|
||||
{
|
||||
var client = Substitute.For<IOpcUaClient>();
|
||||
var factory = Substitute.For<IOpcUaClientFactory>();
|
||||
factory.Create().Returns(client);
|
||||
client.IsConnected.Returns(true);
|
||||
|
||||
Action<string, object?, DateTime, uint>? publish = null;
|
||||
client.CreateSubscriptionAsync(Tag, Arg.Do<Action<string, object?, DateTime, uint>>(cb => publish = cb),
|
||||
Arg.Any<CancellationToken>())
|
||||
.Returns("sub-1");
|
||||
|
||||
var logger = new CapturingLogger();
|
||||
var adapter = new OpcUaDataConnection(factory, logger);
|
||||
var disconnected = 0;
|
||||
adapter.Disconnected += () => Interlocked.Increment(ref disconnected);
|
||||
|
||||
await adapter.ConnectAsync(new Dictionary<string, string>());
|
||||
var received = new List<TagValue>();
|
||||
await adapter.SubscribeAsync(Tag, (_, v) => received.Add(v));
|
||||
Assert.NotNull(publish);
|
||||
|
||||
publish!(Tag, 42.0, DateTime.UtcNow, 0u);
|
||||
publish(Tag, null, DateTime.UtcNow, BadResourceUnavailable);
|
||||
publish(Tag, null, DateTime.UtcNow, BadResourceUnavailable); // repeated: no second log line
|
||||
publish(Tag, 43.0, DateTime.UtcNow, 0u);
|
||||
|
||||
Assert.Equal(
|
||||
new[] { QualityCode.Good, QualityCode.Bad, QualityCode.Bad, QualityCode.Good },
|
||||
received.Select(v => v.Quality).ToArray());
|
||||
Assert.Null(received[1].Value);
|
||||
Assert.Equal(43.0, received[3].Value);
|
||||
|
||||
Assert.Equal(0, disconnected);
|
||||
Assert.Equal(ConnectionHealth.Connected, adapter.Status);
|
||||
await client.DidNotReceiveWithAnyArgs().RemoveSubscriptionAsync(default!, default);
|
||||
await client.DidNotReceiveWithAnyArgs().DisconnectAsync(default);
|
||||
|
||||
var unavailable = logger.Entries.Where(e => e.Message.Contains("reports BadResourceUnavailable")).ToList();
|
||||
var recovered = logger.Entries.Where(e => e.Message.Contains("recovered") && e.Message.Contains(Tag)).ToList();
|
||||
Assert.Single(unavailable);
|
||||
Assert.Contains(Tag, unavailable[0].Message);
|
||||
Assert.Single(recovered);
|
||||
}
|
||||
|
||||
/// <summary>A read that returns BadResourceUnavailable fails the read without a disconnect and
|
||||
/// names the symbolic status so it reads as a transient source-side condition.</summary>
|
||||
[Fact]
|
||||
public async Task Read_ResourceUnavailable_FailsReadWithNamedStatus_NoDisconnect()
|
||||
{
|
||||
var client = Substitute.For<IOpcUaClient>();
|
||||
var factory = Substitute.For<IOpcUaClientFactory>();
|
||||
factory.Create().Returns(client);
|
||||
client.IsConnected.Returns(true);
|
||||
client.ReadValueAsync(Tag, Arg.Any<CancellationToken>())
|
||||
.Returns(((object?)null, DateTime.UtcNow, BadResourceUnavailable));
|
||||
|
||||
var adapter = new OpcUaDataConnection(factory, new CapturingLogger());
|
||||
var disconnected = 0;
|
||||
adapter.Disconnected += () => disconnected++;
|
||||
await adapter.ConnectAsync(new Dictionary<string, string>());
|
||||
|
||||
var result = await adapter.ReadAsync(Tag);
|
||||
|
||||
Assert.False(result.Success);
|
||||
Assert.Contains("0x80040000", result.ErrorMessage);
|
||||
Assert.Contains("BadResourceUnavailable", result.ErrorMessage);
|
||||
Assert.Contains("temporarily unable", result.ErrorMessage);
|
||||
Assert.Equal(0, disconnected);
|
||||
Assert.Equal(ConnectionHealth.Connected, adapter.Status);
|
||||
}
|
||||
|
||||
/// <summary>Other Bad reads still get the symbolic name but not the transient wording.</summary>
|
||||
[Fact]
|
||||
public void DescribeBadRead_OtherCodes_NamedWithoutTransientWording()
|
||||
{
|
||||
var text = OpcUaDataConnection.DescribeBadRead(0x80340000u);
|
||||
Assert.Contains("BadNodeIdUnknown", text);
|
||||
Assert.DoesNotContain("temporarily", text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// BadResourceUnavailable is a per-node status: the DataConnectionActor must never classify it
|
||||
/// as a session/secure-channel failure, which would drive a reconnect and a subscription rebuild.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ResourceUnavailable_IsNotConnectionLevelStatus()
|
||||
{
|
||||
var method = typeof(DataConnectionActor).GetMethod("IsConnectionLevelStatus",
|
||||
BindingFlags.NonPublic | BindingFlags.Static);
|
||||
Assert.NotNull(method);
|
||||
Assert.False((bool)method!.Invoke(null, new object[] { BadResourceUnavailable })!);
|
||||
// Positive control: a genuine session code IS connection-level.
|
||||
Assert.True((bool)method.Invoke(null, new object[] { 0x80250000u /* BadSessionIdInvalid */ })!);
|
||||
}
|
||||
}
|
||||
@@ -768,6 +768,92 @@ public class InstanceActorTests : TestKit, IDisposable
|
||||
Assert.Equal(1450, response.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#112: a Bad-quality update that carries NO value (OtOpcUa sends a null Value
|
||||
/// with every Bad status, e.g. BadResourceUnavailable from its out-of-process script runner)
|
||||
/// is a quality-only transition — the last-known value survives, quality goes Bad, waiters
|
||||
/// and children are not re-evaluated against a null that never existed, and the next Good
|
||||
/// update recovers the attribute. Pins the Good / Bad(null) / Good sequence.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void InstanceActor_BadQualityWithNullValue_KeepsLastKnownValue_AndRecoversOnGood()
|
||||
{
|
||||
const string tag = "nsu=https://zb.com/otopcua/uns;s=Line1.Pump.Calc";
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Pump-BadNull",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Calc", Value = "0", DataType = "Int32",
|
||||
DataSourceReference = tag, BoundDataConnectionName = "OtOpcUa"
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var dcl = CreateTestProbe();
|
||||
var actor = CreateInstanceActorWithDcl("Pump-BadNull", config, dcl);
|
||||
|
||||
actor.Tell(new TagValueUpdate("OtOpcUa", tag, 1450, QualityCode.Good, DateTimeOffset.UtcNow));
|
||||
|
||||
// A waiter that would fire if the stored value were ever nulled.
|
||||
actor.Tell(new WaitForAttributeRequest(
|
||||
"wfa-null", "Pump-BadNull", "Calc",
|
||||
null, v => v is null, TimeSpan.FromSeconds(30), DateTimeOffset.UtcNow));
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(200));
|
||||
|
||||
// Source goes BadResourceUnavailable: Bad quality, null value.
|
||||
actor.Tell(new TagValueUpdate("OtOpcUa", tag, null, QualityCode.Bad, DateTimeOffset.UtcNow));
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(300));
|
||||
|
||||
actor.Tell(new GetAttributeRequest("corr-bad", "Pump-BadNull", "Calc", DateTimeOffset.UtcNow));
|
||||
var bad = ExpectMsg<GetAttributeResponse>(TimeSpan.FromSeconds(5));
|
||||
Assert.True(bad.Found);
|
||||
Assert.Equal("Bad", bad.Quality);
|
||||
Assert.Equal(1450, bad.Value);
|
||||
|
||||
// Next good evaluation recovers.
|
||||
actor.Tell(new TagValueUpdate("OtOpcUa", tag, 1500, QualityCode.Good, DateTimeOffset.UtcNow));
|
||||
actor.Tell(new GetAttributeRequest("corr-good", "Pump-BadNull", "Calc", DateTimeOffset.UtcNow));
|
||||
var good = ExpectMsg<GetAttributeResponse>(TimeSpan.FromSeconds(5));
|
||||
Assert.Equal("Good", good.Quality);
|
||||
Assert.Equal(1500, good.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Guard for ScadaBridge#112: a Bad update that DOES carry a value keeps the historical
|
||||
/// behaviour — the carried value is stored with Bad quality.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void InstanceActor_BadQualityWithValue_StoresCarriedValue()
|
||||
{
|
||||
const string tag = "ns=3;s=Pump.BadWithValue";
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Pump-BadValue",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Speed", Value = "0", DataType = "Int32",
|
||||
DataSourceReference = tag, BoundDataConnectionName = "PLC"
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var dcl = CreateTestProbe();
|
||||
var actor = CreateInstanceActorWithDcl("Pump-BadValue", config, dcl);
|
||||
|
||||
actor.Tell(new TagValueUpdate("PLC", tag, 1450, QualityCode.Good, DateTimeOffset.UtcNow));
|
||||
actor.Tell(new TagValueUpdate("PLC", tag, 1300, QualityCode.Bad, DateTimeOffset.UtcNow));
|
||||
|
||||
actor.Tell(new GetAttributeRequest("corr-bv", "Pump-BadValue", "Speed", DateTimeOffset.UtcNow));
|
||||
var response = ExpectMsg<GetAttributeResponse>(TimeSpan.FromSeconds(5));
|
||||
Assert.Equal("Bad", response.Quality);
|
||||
Assert.Equal(1300, response.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MV (C1 fix): a WRITE to a data-sourced <c>DataType.List</c> attribute must
|
||||
/// send the DCL a TYPED collection (so OPC UA writes an array node), NOT the
|
||||
|
||||
Reference in New Issue
Block a user