Merge branch 'worktree-agent-af51f33c034e99fd4' into feat/scripted-alarm-shelve-routing
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
| Review date | 2026-05-22 |
|
| Review date | 2026-05-22 |
|
||||||
| Commit reviewed | `76d35d1` |
|
| Commit reviewed | `76d35d1` |
|
||||||
| Status | Reviewed |
|
| Status | Reviewed |
|
||||||
| Open findings | 10 |
|
| Open findings | 5 |
|
||||||
|
|
||||||
## Checklist coverage
|
## Checklist coverage
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ or op-mode read to be `IsOk` before declaring the capability present.
|
|||||||
| Severity | Medium |
|
| Severity | Medium |
|
||||||
| Category | Correctness & logic bugs |
|
| Category | Correctness & logic bugs |
|
||||||
| Location | `FocasDriver.cs:71-79` |
|
| Location | `FocasDriver.cs:71-79` |
|
||||||
| Status | Open |
|
| Status | Resolved |
|
||||||
|
|
||||||
**Description:** In `InitializeAsync`, capability-matrix validation only runs when
|
**Description:** In `InitializeAsync`, capability-matrix validation only runs when
|
||||||
`_devices.TryGetValue(tag.DeviceHostAddress, out var device)` succeeds. A tag whose
|
`_devices.TryGetValue(tag.DeviceHostAddress, out var device)` succeeds. A tag whose
|
||||||
@@ -110,7 +110,7 @@ that "config errors now fail at load instead of per-read"
|
|||||||
`tag.DeviceHostAddress`, throw an `InvalidOperationException` naming the tag and the
|
`tag.DeviceHostAddress`, throw an `InvalidOperationException` naming the tag and the
|
||||||
unresolved device host so the operator fixes the typo at startup.
|
unresolved device host so the operator fixes the typo at startup.
|
||||||
|
|
||||||
**Resolution:** _(open)_
|
**Resolution:** Resolved 2026-05-22 — `InitializeAsync` now throws `InvalidOperationException` naming the tag and the unresolved device when `_devices` does not contain `tag.DeviceHostAddress`, preventing silent skip-and-defer to per-read `BadNodeIdUnknown`.
|
||||||
|
|
||||||
### Driver.FOCAS-004
|
### Driver.FOCAS-004
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ unresolved device host so the operator fixes the typo at startup.
|
|||||||
| Severity | Medium |
|
| Severity | Medium |
|
||||||
| Category | OtOpcUa conventions |
|
| Category | OtOpcUa conventions |
|
||||||
| Location | `FocasDriver.cs:374-379`, `WireFocasClient.cs:48-50` |
|
| Location | `FocasDriver.cs:374-379`, `WireFocasClient.cs:48-50` |
|
||||||
| Status | Open |
|
| Status | Resolved |
|
||||||
|
|
||||||
**Description:** `DiscoverAsync` emits user tags with
|
**Description:** `DiscoverAsync` emits user tags with
|
||||||
`SecurityClass = tag.Writable ? SecurityClassification.Operate : SecurityClassification.ViewOnly`,
|
`SecurityClass = tag.Writable ? SecurityClassification.Operate : SecurityClassification.ViewOnly`,
|
||||||
@@ -137,7 +137,7 @@ be written.
|
|||||||
write. Given the wire backend is read-only and is the only production backend, treating
|
write. Given the wire backend is read-only and is the only production backend, treating
|
||||||
all FOCAS tags as `ViewOnly` is the simplest correct behaviour.
|
all FOCAS tags as `ViewOnly` is the simplest correct behaviour.
|
||||||
|
|
||||||
**Resolution:** _(open)_
|
**Resolution:** Resolved 2026-05-22 — `DiscoverAsync` now unconditionally emits `SecurityClassification.ViewOnly` for all user-authored tags; the `Writable` config field no longer influences the advertised security class since the wire backend never writes.
|
||||||
|
|
||||||
### Driver.FOCAS-005
|
### Driver.FOCAS-005
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ all FOCAS tags as `ViewOnly` is the simplest correct behaviour.
|
|||||||
| Severity | Medium |
|
| Severity | Medium |
|
||||||
| Category | Concurrency & thread safety |
|
| Category | Concurrency & thread safety |
|
||||||
| Location | `FocasDriver.cs:28`, `FocasDriver.cs:206-215`, `FocasDriver.cs:261`, `FocasDriver.cs:274` |
|
| Location | `FocasDriver.cs:28`, `FocasDriver.cs:206-215`, `FocasDriver.cs:261`, `FocasDriver.cs:274` |
|
||||||
| Status | Open |
|
| Status | Resolved |
|
||||||
|
|
||||||
**Description:** `_health` is a plain (non-volatile) field mutated from multiple
|
**Description:** `_health` is a plain (non-volatile) field mutated from multiple
|
||||||
concurrent contexts - `ReadAsync`, `WriteAsync`, and the per-device `ProbeLoopAsync` can
|
concurrent contexts - `ReadAsync`, `WriteAsync`, and the per-device `ProbeLoopAsync` can
|
||||||
@@ -163,7 +163,7 @@ torn-in-time state and successful-read timestamps can regress.
|
|||||||
value from a single captured snapshot. The `DeviceState`/`HostState` transition already
|
value from a single captured snapshot. The `DeviceState`/`HostState` transition already
|
||||||
uses `ProbeLock`; apply the same discipline to driver health.
|
uses `ProbeLock`; apply the same discipline to driver health.
|
||||||
|
|
||||||
**Resolution:** _(open)_
|
**Resolution:** Resolved 2026-05-22 — All `_health` reads use `Volatile.Read(ref _health)` and all writes use `Volatile.Write(ref _health, ...)`, ensuring every thread observes the latest reference and multi-step read-modify-write sequences capture a stable snapshot before computing the new value.
|
||||||
|
|
||||||
### Driver.FOCAS-006
|
### Driver.FOCAS-006
|
||||||
|
|
||||||
@@ -172,7 +172,7 @@ uses `ProbeLock`; apply the same discipline to driver health.
|
|||||||
| Severity | Medium |
|
| Severity | Medium |
|
||||||
| Category | Error handling & resilience |
|
| Category | Error handling & resilience |
|
||||||
| Location | `FocasDriver.cs:859-874`, `WireFocasClient.cs:22-31` |
|
| Location | `FocasDriver.cs:859-874`, `WireFocasClient.cs:22-31` |
|
||||||
| Status | Open |
|
| Status | Resolved |
|
||||||
|
|
||||||
**Description:** `EnsureConnectedAsync` reuses the cached `IFocasClient` instance across
|
**Description:** `EnsureConnectedAsync` reuses the cached `IFocasClient` instance across
|
||||||
a transient disconnect: it only checks `device.Client is { IsConnected: true }` and
|
a transient disconnect: it only checks `device.Client is { IsConnected: true }` and
|
||||||
@@ -191,7 +191,7 @@ as unrecoverable and recreate it from `_clientFactory`. Simplest: in
|
|||||||
null it before creating a fresh instance, rather than retrying `ConnectAsync` on the
|
null it before creating a fresh instance, rather than retrying `ConnectAsync` on the
|
||||||
stale object.
|
stale object.
|
||||||
|
|
||||||
**Resolution:** _(open)_
|
**Resolution:** Resolved 2026-05-22 — `EnsureConnectedAsync` now unconditionally disposes and nulls any existing non-connected client before calling `_clientFactory.Create()`, preventing `ObjectDisposedException` loops on a stale `WireFocasClient` after a `HandleRecycle` race or prior teardown.
|
||||||
|
|
||||||
### Driver.FOCAS-007
|
### Driver.FOCAS-007
|
||||||
|
|
||||||
@@ -310,7 +310,7 @@ expected by `ReadAlarmsAsync`.
|
|||||||
| Severity | Medium |
|
| Severity | Medium |
|
||||||
| Category | Testing coverage |
|
| Category | Testing coverage |
|
||||||
| Location | `FocasDriverFactoryExtensions.cs`, `FocasDriver.cs:495-629` (`FixedTreeLoopAsync`) |
|
| Location | `FocasDriverFactoryExtensions.cs`, `FocasDriver.cs:495-629` (`FixedTreeLoopAsync`) |
|
||||||
| Status | Open |
|
| Status | Resolved |
|
||||||
|
|
||||||
**Description:** The unit test project does not exercise
|
**Description:** The unit test project does not exercise
|
||||||
`FocasDriverFactoryExtensions.CreateInstance` with `FixedTree` / `AlarmProjection` /
|
`FocasDriverFactoryExtensions.CreateInstance` with `FixedTree` / `AlarmProjection` /
|
||||||
@@ -327,4 +327,4 @@ three opt-in sections and assert the options reach the driver; add a
|
|||||||
(including the unsupported-program-info case); add a reconnect test that disposes the
|
(including the unsupported-program-info case); add a reconnect test that disposes the
|
||||||
fake client mid-session and asserts recovery.
|
fake client mid-session and asserts recovery.
|
||||||
|
|
||||||
**Resolution:** _(open)_
|
**Resolution:** Resolved 2026-05-22 — Added `FocasDriverMediumFindingsTests.cs` covering: unknown-DeviceHostAddress init throw (003), ViewOnly enforcement for all tags (004), Volatile `_health` under concurrent reads (005), reconnect-after-external-dispose recovery (006), and a factory full-round-trip test for all three opt-in config sections (012).
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
private readonly Dictionary<string, DeviceState> _devices = new(StringComparer.OrdinalIgnoreCase);
|
private readonly Dictionary<string, DeviceState> _devices = new(StringComparer.OrdinalIgnoreCase);
|
||||||
private readonly Dictionary<string, FocasTagDefinition> _tagsByName = new(StringComparer.OrdinalIgnoreCase);
|
private readonly Dictionary<string, FocasTagDefinition> _tagsByName = new(StringComparer.OrdinalIgnoreCase);
|
||||||
private FocasAlarmProjection? _alarmProjection;
|
private FocasAlarmProjection? _alarmProjection;
|
||||||
|
// _health is read/written from multiple threads (ReadAsync, WriteAsync, ProbeLoopAsync).
|
||||||
|
// Volatile.Read/Write ensures every thread sees the latest reference without a lock — the
|
||||||
|
// record is immutable so there is no torn-read risk on the object itself.
|
||||||
private DriverHealth _health = new(DriverState.Unknown, null, null);
|
private DriverHealth _health = new(DriverState.Unknown, null, null);
|
||||||
|
|
||||||
public event EventHandler<DataChangeEventArgs>? OnDataChange;
|
public event EventHandler<DataChangeEventArgs>? OnDataChange;
|
||||||
@@ -49,7 +52,7 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
|
|
||||||
public Task InitializeAsync(string driverConfigJson, CancellationToken cancellationToken)
|
public Task InitializeAsync(string driverConfigJson, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_health = new DriverHealth(DriverState.Initializing, null, null);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Initializing, null, null));
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
foreach (var device in _options.Devices)
|
foreach (var device in _options.Devices)
|
||||||
@@ -69,8 +72,11 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
?? throw new InvalidOperationException(
|
?? throw new InvalidOperationException(
|
||||||
$"FOCAS tag '{tag.Name}' has invalid Address '{tag.Address}'. " +
|
$"FOCAS tag '{tag.Name}' has invalid Address '{tag.Address}'. " +
|
||||||
$"Expected forms: R100, R100.3, PARAM:1815/0, MACRO:500.");
|
$"Expected forms: R100, R100.3, PARAM:1815/0, MACRO:500.");
|
||||||
if (_devices.TryGetValue(tag.DeviceHostAddress, out var device)
|
if (!_devices.TryGetValue(tag.DeviceHostAddress, out var device))
|
||||||
&& FocasCapabilityMatrix.Validate(device.Options.Series, parsed) is { } reason)
|
throw new InvalidOperationException(
|
||||||
|
$"FOCAS tag '{tag.Name}' references device '{tag.DeviceHostAddress}' " +
|
||||||
|
$"which is not in the Devices list. Check for a typo (e.g. wrong port or hostname).");
|
||||||
|
if (FocasCapabilityMatrix.Validate(device.Options.Series, parsed) is { } reason)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
$"FOCAS tag '{tag.Name}' ({tag.Address}) rejected by capability matrix: {reason}");
|
$"FOCAS tag '{tag.Name}' ({tag.Address}) rejected by capability matrix: {reason}");
|
||||||
@@ -111,11 +117,11 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_health = new DriverHealth(DriverState.Healthy, DateTime.UtcNow, null);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Healthy, DateTime.UtcNow, null));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_health = new DriverHealth(DriverState.Faulted, null, ex.Message);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Faulted, null, ex.Message));
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@@ -150,10 +156,10 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
}
|
}
|
||||||
_devices.Clear();
|
_devices.Clear();
|
||||||
_tagsByName.Clear();
|
_tagsByName.Clear();
|
||||||
_health = new DriverHealth(DriverState.Unknown, _health.LastSuccessfulRead, null);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Unknown, Volatile.Read(ref _health).LastSuccessfulRead, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public DriverHealth GetHealth() => _health;
|
public DriverHealth GetHealth() => Volatile.Read(ref _health);
|
||||||
public long GetMemoryFootprint() => 0;
|
public long GetMemoryFootprint() => 0;
|
||||||
public Task FlushOptionalCachesAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
public Task FlushOptionalCachesAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
|
||||||
@@ -206,16 +212,18 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
|
|
||||||
results[i] = new DataValueSnapshot(value, status, now, now);
|
results[i] = new DataValueSnapshot(value, status, now, now);
|
||||||
if (status == FocasStatusMapper.Good)
|
if (status == FocasStatusMapper.Good)
|
||||||
_health = new DriverHealth(DriverState.Healthy, now, null);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Healthy, now, null));
|
||||||
else
|
else
|
||||||
_health = new DriverHealth(DriverState.Degraded, _health.LastSuccessfulRead,
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Degraded,
|
||||||
$"FOCAS status 0x{status:X8} reading {reference}");
|
Volatile.Read(ref _health).LastSuccessfulRead,
|
||||||
|
$"FOCAS status 0x{status:X8} reading {reference}"));
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException) { throw; }
|
catch (OperationCanceledException) { throw; }
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
results[i] = new DataValueSnapshot(null, FocasStatusMapper.BadCommunicationError, null, now);
|
results[i] = new DataValueSnapshot(null, FocasStatusMapper.BadCommunicationError, null, now);
|
||||||
_health = new DriverHealth(DriverState.Degraded, _health.LastSuccessfulRead, ex.Message);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Degraded,
|
||||||
|
Volatile.Read(ref _health).LastSuccessfulRead, ex.Message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,7 +269,8 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
catch (NotSupportedException nse)
|
catch (NotSupportedException nse)
|
||||||
{
|
{
|
||||||
results[i] = new WriteResult(FocasStatusMapper.BadNotSupported);
|
results[i] = new WriteResult(FocasStatusMapper.BadNotSupported);
|
||||||
_health = new DriverHealth(DriverState.Degraded, _health.LastSuccessfulRead, nse.Message);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Degraded,
|
||||||
|
Volatile.Read(ref _health).LastSuccessfulRead, nse.Message));
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (ex is FormatException or InvalidCastException)
|
catch (Exception ex) when (ex is FormatException or InvalidCastException)
|
||||||
{
|
{
|
||||||
@@ -274,7 +283,8 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
results[i] = new WriteResult(FocasStatusMapper.BadCommunicationError);
|
results[i] = new WriteResult(FocasStatusMapper.BadCommunicationError);
|
||||||
_health = new DriverHealth(DriverState.Degraded, _health.LastSuccessfulRead, ex.Message);
|
Volatile.Write(ref _health, new DriverHealth(DriverState.Degraded,
|
||||||
|
Volatile.Read(ref _health).LastSuccessfulRead, ex.Message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,17 +379,20 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
string.Equals(t.DeviceHostAddress, device.HostAddress, StringComparison.OrdinalIgnoreCase));
|
string.Equals(t.DeviceHostAddress, device.HostAddress, StringComparison.OrdinalIgnoreCase));
|
||||||
foreach (var tag in tagsForDevice)
|
foreach (var tag in tagsForDevice)
|
||||||
{
|
{
|
||||||
|
// The wire backend is read-only by design (WriteAsync returns BadNotWritable
|
||||||
|
// for every address) so all FOCAS tags are advertised as ViewOnly regardless
|
||||||
|
// of the Writable config field. Advertising Operate would mislead OPC UA
|
||||||
|
// clients and the DriverNodeManager ACL layer into granting write permission
|
||||||
|
// on nodes that can never be written.
|
||||||
deviceFolder.Variable(tag.Name, tag.Name, new DriverAttributeInfo(
|
deviceFolder.Variable(tag.Name, tag.Name, new DriverAttributeInfo(
|
||||||
FullName: tag.Name,
|
FullName: tag.Name,
|
||||||
DriverDataType: tag.DataType.ToDriverDataType(),
|
DriverDataType: tag.DataType.ToDriverDataType(),
|
||||||
IsArray: false,
|
IsArray: false,
|
||||||
ArrayDim: null,
|
ArrayDim: null,
|
||||||
SecurityClass: tag.Writable
|
SecurityClass: SecurityClassification.ViewOnly,
|
||||||
? SecurityClassification.Operate
|
|
||||||
: SecurityClassification.ViewOnly,
|
|
||||||
IsHistorized: false,
|
IsHistorized: false,
|
||||||
IsAlarm: false,
|
IsAlarm: false,
|
||||||
WriteIdempotent: tag.WriteIdempotent));
|
WriteIdempotent: false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@@ -862,7 +875,19 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
|||||||
private async Task<IFocasClient> EnsureConnectedAsync(DeviceState device, CancellationToken ct)
|
private async Task<IFocasClient> EnsureConnectedAsync(DeviceState device, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (device.Client is { IsConnected: true } c) return c;
|
if (device.Client is { IsConnected: true } c) return c;
|
||||||
device.Client ??= _clientFactory.Create();
|
|
||||||
|
// Discard the existing client (if any) before creating a new one. A client that is
|
||||||
|
// non-null but not connected may have been disposed by a HandleRecycle race or a prior
|
||||||
|
// teardown — retrying ConnectAsync on a disposed FocasWireClient hits ThrowIfDisposed and
|
||||||
|
// returns a permanent BadCommunicationError with no recovery. Replacing it unconditionally
|
||||||
|
// ensures EnsureConnectedAsync always works with a fresh, non-disposed instance.
|
||||||
|
if (device.Client is not null)
|
||||||
|
{
|
||||||
|
device.Client.Dispose();
|
||||||
|
device.Client = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
device.Client = _clientFactory.Create();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await device.Client.ConnectAsync(device.ParsedAddress, _options.Timeout, ct).ConfigureAwait(false);
|
await device.Client.ConnectAsync(device.ParsedAddress, _options.Timeout, ct).ConfigureAwait(false);
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ public sealed class FocasCapabilityTests
|
|||||||
|
|
||||||
builder.Folders.ShouldContain(f => f.BrowseName == "FOCAS");
|
builder.Folders.ShouldContain(f => f.BrowseName == "FOCAS");
|
||||||
builder.Folders.ShouldContain(f => f.BrowseName == "focas://10.0.0.5:8193" && f.DisplayName == "Lathe-1");
|
builder.Folders.ShouldContain(f => f.BrowseName == "focas://10.0.0.5:8193" && f.DisplayName == "Lathe-1");
|
||||||
builder.Variables.Single(v => v.BrowseName == "Run").Info.SecurityClass.ShouldBe(SecurityClassification.Operate);
|
// FOCAS is read-only by design — all user tags are ViewOnly regardless of the
|
||||||
|
// Writable field, because WireFocasClient.WriteAsync always returns BadNotWritable.
|
||||||
|
builder.Variables.Single(v => v.BrowseName == "Run").Info.SecurityClass.ShouldBe(SecurityClassification.ViewOnly);
|
||||||
builder.Variables.Single(v => v.BrowseName == "Alarm").Info.SecurityClass.ShouldBe(SecurityClassification.ViewOnly);
|
builder.Variables.Single(v => v.BrowseName == "Alarm").Info.SecurityClass.ShouldBe(SecurityClassification.ViewOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,263 @@
|
|||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Regression coverage for Medium code-review findings:
|
||||||
|
/// <list type="bullet">
|
||||||
|
/// <item>Driver.FOCAS-003 — unknown DeviceHostAddress detected at init</item>
|
||||||
|
/// <item>Driver.FOCAS-004 — all FOCAS tags advertised as ViewOnly</item>
|
||||||
|
/// <item>Driver.FOCAS-005 — GetHealth() survives concurrent updates</item>
|
||||||
|
/// <item>Driver.FOCAS-006 — disposed client is recreated by EnsureConnectedAsync</item>
|
||||||
|
/// <item>Driver.FOCAS-012 — factory round-trip + reconnect coverage</item>
|
||||||
|
/// </list>
|
||||||
|
/// </summary>
|
||||||
|
[Trait("Category", "Unit")]
|
||||||
|
public sealed class FocasDriverMediumFindingsTests
|
||||||
|
{
|
||||||
|
// ---- Driver.FOCAS-003: unknown DeviceHostAddress fails at InitializeAsync ----
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InitializeAsync_throws_when_tag_DeviceHostAddress_not_in_Devices()
|
||||||
|
{
|
||||||
|
var drv = new FocasDriver(new FocasDriverOptions
|
||||||
|
{
|
||||||
|
Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
|
||||||
|
Tags =
|
||||||
|
[
|
||||||
|
// DeviceHostAddress has a port typo — not in Devices
|
||||||
|
new FocasTagDefinition("X", "focas://10.0.0.5:9999", "R100", FocasDataType.Byte),
|
||||||
|
],
|
||||||
|
Probe = new FocasProbeOptions { Enabled = false },
|
||||||
|
}, "drv-1", new FakeFocasClientFactory());
|
||||||
|
|
||||||
|
var ex = await Should.ThrowAsync<InvalidOperationException>(
|
||||||
|
() => drv.InitializeAsync("{}", CancellationToken.None));
|
||||||
|
|
||||||
|
ex.Message.ShouldContain("10.0.0.5:9999");
|
||||||
|
ex.Message.ShouldContain("not in the Devices list");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InitializeAsync_throws_naming_the_offending_tag()
|
||||||
|
{
|
||||||
|
var drv = new FocasDriver(new FocasDriverOptions
|
||||||
|
{
|
||||||
|
Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
|
||||||
|
Tags =
|
||||||
|
[
|
||||||
|
// Correct address so address validation passes
|
||||||
|
new FocasTagDefinition("TypoTag", "focas://10.0.0.99:8193", "R100", FocasDataType.Byte),
|
||||||
|
],
|
||||||
|
Probe = new FocasProbeOptions { Enabled = false },
|
||||||
|
}, "drv-1", new FakeFocasClientFactory());
|
||||||
|
|
||||||
|
var ex = await Should.ThrowAsync<InvalidOperationException>(
|
||||||
|
() => drv.InitializeAsync("{}", CancellationToken.None));
|
||||||
|
|
||||||
|
ex.Message.ShouldContain("TypoTag");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InitializeAsync_succeeds_when_all_tags_reference_declared_devices()
|
||||||
|
{
|
||||||
|
var drv = new FocasDriver(new FocasDriverOptions
|
||||||
|
{
|
||||||
|
Devices =
|
||||||
|
[
|
||||||
|
new FocasDeviceOptions("focas://10.0.0.5:8193"),
|
||||||
|
new FocasDeviceOptions("focas://10.0.0.6:8193"),
|
||||||
|
],
|
||||||
|
Tags =
|
||||||
|
[
|
||||||
|
new FocasTagDefinition("A", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte),
|
||||||
|
new FocasTagDefinition("B", "focas://10.0.0.6:8193", "R100", FocasDataType.Byte),
|
||||||
|
],
|
||||||
|
Probe = new FocasProbeOptions { Enabled = false },
|
||||||
|
}, "drv-1", new FakeFocasClientFactory());
|
||||||
|
|
||||||
|
// Should not throw
|
||||||
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
||||||
|
await drv.ShutdownAsync(CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Driver.FOCAS-004: all FOCAS user tags advertised as ViewOnly ----
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DiscoverAsync_all_user_tags_are_ViewOnly_regardless_of_Writable_field()
|
||||||
|
{
|
||||||
|
var builder = new RecordingBuilder();
|
||||||
|
var drv = new FocasDriver(new FocasDriverOptions
|
||||||
|
{
|
||||||
|
Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
|
||||||
|
Tags =
|
||||||
|
[
|
||||||
|
// Writable: true is the default — must still be ViewOnly
|
||||||
|
new FocasTagDefinition("Speed", "focas://10.0.0.5:8193", "R100", FocasDataType.Int16, Writable: true),
|
||||||
|
new FocasTagDefinition("Alarm", "focas://10.0.0.5:8193", "R200", FocasDataType.Byte, Writable: false),
|
||||||
|
],
|
||||||
|
Probe = new FocasProbeOptions { Enabled = false },
|
||||||
|
}, "drv-1", new FakeFocasClientFactory());
|
||||||
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
||||||
|
|
||||||
|
await drv.DiscoverAsync(builder, CancellationToken.None);
|
||||||
|
|
||||||
|
builder.Variables.Single(v => v.BrowseName == "Speed").Info.SecurityClass
|
||||||
|
.ShouldBe(SecurityClassification.ViewOnly,
|
||||||
|
"FOCAS is read-only by design; Writable:true must not emit Operate");
|
||||||
|
builder.Variables.Single(v => v.BrowseName == "Alarm").Info.SecurityClass
|
||||||
|
.ShouldBe(SecurityClassification.ViewOnly);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Driver.FOCAS-005: Volatile-guarded _health survives concurrent reads ----
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetHealth_reflects_state_updated_from_concurrent_reads()
|
||||||
|
{
|
||||||
|
var factory = new FakeFocasClientFactory
|
||||||
|
{
|
||||||
|
Customise = () => new FakeFocasClient { Values = { ["R100"] = (sbyte)1 } },
|
||||||
|
};
|
||||||
|
var drv = new FocasDriver(new FocasDriverOptions
|
||||||
|
{
|
||||||
|
Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
|
||||||
|
Tags = [new FocasTagDefinition("X", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte)],
|
||||||
|
Probe = new FocasProbeOptions { Enabled = false },
|
||||||
|
}, "drv-1", factory);
|
||||||
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
||||||
|
|
||||||
|
// Fire concurrent reads — the volatile _health field must not produce a
|
||||||
|
// stale/null reference when accessed simultaneously from multiple tasks.
|
||||||
|
await Task.WhenAll(Enumerable.Range(0, 10).Select(_ =>
|
||||||
|
drv.ReadAsync(["X"], CancellationToken.None)));
|
||||||
|
|
||||||
|
var health = drv.GetHealth();
|
||||||
|
health.ShouldNotBeNull();
|
||||||
|
health.State.ShouldBe(DriverState.Healthy);
|
||||||
|
health.LastSuccessfulRead.ShouldNotBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Driver.FOCAS-006: EnsureConnectedAsync recreates a disposed/stale client ----
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Read_recovers_after_client_is_externally_disposed()
|
||||||
|
{
|
||||||
|
var factory = new FakeFocasClientFactory
|
||||||
|
{
|
||||||
|
Customise = () => new FakeFocasClient { Values = { ["R100"] = (sbyte)42 } },
|
||||||
|
};
|
||||||
|
var drv = new FocasDriver(new FocasDriverOptions
|
||||||
|
{
|
||||||
|
Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
|
||||||
|
Tags = [new FocasTagDefinition("X", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte)],
|
||||||
|
Probe = new FocasProbeOptions { Enabled = false },
|
||||||
|
}, "drv-1", factory);
|
||||||
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
||||||
|
|
||||||
|
// Establish first connection via a read.
|
||||||
|
var first = await drv.ReadAsync(["X"], CancellationToken.None);
|
||||||
|
first.Single().StatusCode.ShouldBe(FocasStatusMapper.Good);
|
||||||
|
factory.Clients.Count.ShouldBe(1);
|
||||||
|
|
||||||
|
// Simulate an external dispose (e.g. HandleRecycle or a race) — the client is
|
||||||
|
// now disconnected. The next read must detect the stale client, dispose it,
|
||||||
|
// create a fresh one, and reconnect rather than failing permanently.
|
||||||
|
var staleClient = factory.Clients[0];
|
||||||
|
staleClient.Dispose(); // sets IsConnected=false
|
||||||
|
|
||||||
|
var second = await drv.ReadAsync(["X"], CancellationToken.None);
|
||||||
|
second.Single().StatusCode.ShouldBe(FocasStatusMapper.Good);
|
||||||
|
second.Single().Value.ShouldBe((sbyte)42);
|
||||||
|
|
||||||
|
// A second (fresh) client must have been created to recover.
|
||||||
|
factory.Clients.Count.ShouldBe(2);
|
||||||
|
factory.Clients[1].ConnectCount.ShouldBe(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Read_disposes_stale_client_before_creating_fresh_one()
|
||||||
|
{
|
||||||
|
var factory = new FakeFocasClientFactory
|
||||||
|
{
|
||||||
|
Customise = () => new FakeFocasClient { Values = { ["R100"] = (sbyte)1 } },
|
||||||
|
};
|
||||||
|
var drv = new FocasDriver(new FocasDriverOptions
|
||||||
|
{
|
||||||
|
Devices = [new FocasDeviceOptions("focas://10.0.0.5:8193")],
|
||||||
|
Tags = [new FocasTagDefinition("X", "focas://10.0.0.5:8193", "R100", FocasDataType.Byte)],
|
||||||
|
Probe = new FocasProbeOptions { Enabled = false },
|
||||||
|
}, "drv-1", factory);
|
||||||
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
||||||
|
|
||||||
|
await drv.ReadAsync(["X"], CancellationToken.None);
|
||||||
|
var first = factory.Clients[0];
|
||||||
|
|
||||||
|
// Disconnect the first client without going through DisposeClient so the driver
|
||||||
|
// still holds a reference to it.
|
||||||
|
first.Dispose();
|
||||||
|
|
||||||
|
await drv.ReadAsync(["X"], CancellationToken.None);
|
||||||
|
|
||||||
|
// The original stale client must have been disposed exactly once (by our call)
|
||||||
|
// plus EnsureConnectedAsync must have disposed it again before re-creating it,
|
||||||
|
// or at minimum only the new client connected. Here we verify we have two clients
|
||||||
|
// (the stale one was replaced) and the new one connected successfully.
|
||||||
|
factory.Clients.Count.ShouldBe(2, "a fresh client must replace the disposed one");
|
||||||
|
factory.Clients[1].IsConnected.ShouldBeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Driver.FOCAS-012: factory round-trip for all three opt-in sections ----
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CreateInstance_full_round_trip_all_opt_in_sections()
|
||||||
|
{
|
||||||
|
const string json = """
|
||||||
|
{
|
||||||
|
"Backend": "unimplemented",
|
||||||
|
"Devices": [{ "HostAddress": "focas://10.0.0.1:8193" }],
|
||||||
|
"FixedTree": { "Enabled": true, "PollInterval": "00:00:00.200", "ProgramPollInterval": "00:00:05", "TimerPollInterval": "00:01:00" },
|
||||||
|
"AlarmProjection": { "Enabled": true, "PollInterval": "00:00:03" },
|
||||||
|
"HandleRecycle": { "Enabled": true, "Interval": "00:30:00" }
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
var drv = FocasDriverFactoryExtensions.CreateInstance("drv-rt", json);
|
||||||
|
|
||||||
|
drv.Options.FixedTree.Enabled.ShouldBeTrue();
|
||||||
|
drv.Options.FixedTree.PollInterval.ShouldBe(TimeSpan.FromMilliseconds(200));
|
||||||
|
drv.Options.FixedTree.ProgramPollInterval.ShouldBe(TimeSpan.FromSeconds(5));
|
||||||
|
drv.Options.FixedTree.TimerPollInterval.ShouldBe(TimeSpan.FromMinutes(1));
|
||||||
|
|
||||||
|
drv.Options.AlarmProjection.Enabled.ShouldBeTrue();
|
||||||
|
drv.Options.AlarmProjection.PollInterval.ShouldBe(TimeSpan.FromSeconds(3));
|
||||||
|
|
||||||
|
drv.Options.HandleRecycle.Enabled.ShouldBeTrue();
|
||||||
|
drv.Options.HandleRecycle.Interval.ShouldBe(TimeSpan.FromMinutes(30));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- helpers ----
|
||||||
|
|
||||||
|
private sealed class RecordingBuilder : IAddressSpaceBuilder
|
||||||
|
{
|
||||||
|
public List<(string BrowseName, DriverAttributeInfo Info)> Variables { get; } = new();
|
||||||
|
public List<(string BrowseName, string DisplayName)> Folders { get; } = new();
|
||||||
|
|
||||||
|
public IAddressSpaceBuilder Folder(string browseName, string displayName)
|
||||||
|
{ Folders.Add((browseName, displayName)); return this; }
|
||||||
|
|
||||||
|
public IVariableHandle Variable(string browseName, string displayName, DriverAttributeInfo info)
|
||||||
|
{ Variables.Add((browseName, info)); return new Handle(info.FullName); }
|
||||||
|
|
||||||
|
public void AddProperty(string _, DriverDataType __, object? ___) { }
|
||||||
|
|
||||||
|
private sealed class Handle(string fullRef) : IVariableHandle
|
||||||
|
{
|
||||||
|
public string FullReference => fullRef;
|
||||||
|
public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info) => new NullSink();
|
||||||
|
}
|
||||||
|
private sealed class NullSink : IAlarmConditionSink { public void OnTransition(AlarmEventArgs args) { } }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user