Client.Shared-005: _activeDataSubscriptions (a plain Dictionary) and the _activeAlarmSubscription tuple were mutated from the caller thread, the keep-alive failover path, and DisconnectAsync with no synchronization, risking bucket corrosion / InvalidOperationException / lost entries. Added a dedicated _subscriptionLock and wrapped every read/write of that bookkeeping state inside it (Subscribe/Unsubscribe[Alarms]Async, Disconnect, Dispose, and the snapshot/clear/re-record steps of ReplaySubscriptionsAsync). Awaited adapter calls stay outside the lock so it is never held across I/O. Client.Shared-006: HandleKeepAliveFailureAsync had only a non-atomic state check guarding re-entry, so two bad keep-alives could each start a failover loop, racing to dispose/replace _session and double-replaying subscriptions. It now claims an atomic _failoverInProgress slot via Interlocked.CompareExchange; a re-entrant call returns immediately. The loop body moved to RunFailoverAsync, wrapped in try/finally that resets the flag. Tests: added KeepAliveFailure_ReentrantWhileFailoverInFlight_RunsFailoverOnce and SubscribeAndUnsubscribe_ConcurrentCalls_DoNotCorruptState regression tests; made the FakeSubscriptionAdapter / FakeSessionAdapter / FakeSessionFactory test doubles thread-safe (and added a CreateGate hook) so the concurrency tests exercise production locking rather than fake state. All 138 Client.Shared tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
143 lines
4.4 KiB
C#
143 lines
4.4 KiB
C#
using Opc.Ua;
|
|
using ZB.MOM.WW.OtOpcUa.Client.Shared.Adapters;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Fakes;
|
|
|
|
/// <summary>
|
|
/// Test double for <see cref="ISubscriptionAdapter" /> used to drive monitored-item behavior in shared-client tests.
|
|
/// </summary>
|
|
internal sealed class FakeSubscriptionAdapter : ISubscriptionAdapter
|
|
{
|
|
private readonly
|
|
Dictionary<uint, (NodeId NodeId, Action<string, DataValue>? DataCallback, Action<EventFieldList>? EventCallback
|
|
)> _items = new();
|
|
|
|
// Guards _items so concurrent-subscription tests exercise the production
|
|
// locking rather than tripping over the test double's own state.
|
|
private readonly object _itemsLock = new();
|
|
|
|
private uint _nextHandle = 100;
|
|
/// <summary>
|
|
/// Gets a value indicating whether the fake subscription has been deleted.
|
|
/// </summary>
|
|
public bool Deleted { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether a condition refresh was requested by the client under test.
|
|
/// </summary>
|
|
public bool ConditionRefreshCalled { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether condition refresh should throw to simulate unsupported servers.
|
|
/// </summary>
|
|
public bool ThrowOnConditionRefresh { get; set; }
|
|
public int AddDataChangeCount { get; private set; }
|
|
public int AddEventCount { get; private set; }
|
|
public int RemoveCount { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the handles of all active items.
|
|
/// </summary>
|
|
public IReadOnlyCollection<uint> ActiveHandles
|
|
{
|
|
get
|
|
{
|
|
lock (_itemsLock) return _items.Keys.ToList();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public uint SubscriptionId { get; set; } = 42;
|
|
|
|
/// <inheritdoc />
|
|
public Task<uint> AddDataChangeMonitoredItemAsync(NodeId nodeId, int samplingIntervalMs,
|
|
Action<string, DataValue> onDataChange, CancellationToken ct)
|
|
{
|
|
lock (_itemsLock)
|
|
{
|
|
AddDataChangeCount++;
|
|
var handle = _nextHandle++;
|
|
_items[handle] = (nodeId, onDataChange, null);
|
|
return Task.FromResult(handle);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task RemoveMonitoredItemAsync(uint clientHandle, CancellationToken ct)
|
|
{
|
|
lock (_itemsLock)
|
|
{
|
|
RemoveCount++;
|
|
_items.Remove(clientHandle);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<uint> AddEventMonitoredItemAsync(NodeId nodeId, int samplingIntervalMs, EventFilter filter,
|
|
Action<EventFieldList> onEvent, CancellationToken ct)
|
|
{
|
|
lock (_itemsLock)
|
|
{
|
|
AddEventCount++;
|
|
var handle = _nextHandle++;
|
|
_items[handle] = (nodeId, null, onEvent);
|
|
return Task.FromResult(handle);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task ConditionRefreshAsync(CancellationToken ct)
|
|
{
|
|
ConditionRefreshCalled = true;
|
|
if (ThrowOnConditionRefresh)
|
|
throw new InvalidOperationException("Condition refresh not supported");
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task DeleteAsync(CancellationToken ct)
|
|
{
|
|
Deleted = true;
|
|
lock (_itemsLock) _items.Clear();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears tracked monitored items when the fake subscription is disposed by the client under test.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
lock (_itemsLock) _items.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates a data change notification for testing.
|
|
/// </summary>
|
|
public void SimulateDataChange(uint handle, DataValue value)
|
|
{
|
|
(NodeId NodeId, Action<string, DataValue>? DataCallback, Action<EventFieldList>? EventCallback) item;
|
|
lock (_itemsLock)
|
|
{
|
|
if (!_items.TryGetValue(handle, out item)) return;
|
|
}
|
|
|
|
item.DataCallback?.Invoke(item.NodeId.ToString(), value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates an event notification for testing.
|
|
/// </summary>
|
|
public void SimulateEvent(uint handle, EventFieldList eventFields)
|
|
{
|
|
(NodeId NodeId, Action<string, DataValue>? DataCallback, Action<EventFieldList>? EventCallback) item;
|
|
lock (_itemsLock)
|
|
{
|
|
if (!_items.TryGetValue(handle, out item)) return;
|
|
}
|
|
|
|
item.EventCallback?.Invoke(eventFields);
|
|
}
|
|
}
|