@@ -20,24 +20,108 @@ internal class FakeTwinCATClient : ITwinCATClient
|
||||
public List<(string symbol, TwinCATDataType type, int? bit, int[]? arrayDimensions)> ReadLog { get; } = new();
|
||||
public bool ProbeResult { get; set; } = true;
|
||||
|
||||
// ---- PR 2.2: handle-cache tracking ----
|
||||
//
|
||||
// The fake mirrors the production AdsTwinCATClient handle-cache state machine so the
|
||||
// unit + integration tests can assert "second read of X reused the cached handle"
|
||||
// without hitting a real ADS device. EnsureFakeHandle is called by every per-tag read /
|
||||
// write path and increments HandleCreateInvocations only on a cache miss. Tests can
|
||||
// arm SymbolVersionInvalidOnNextRead / Write to drive the evict-and-retry path.
|
||||
public List<string> HandleCreateInvocations { get; } = new();
|
||||
public List<uint> HandleDeleteInvocations { get; } = new();
|
||||
public List<string> ReadByHandleInvocations { get; } = new();
|
||||
public List<string> WriteByHandleInvocations { get; } = new();
|
||||
public int FlushOptionalCachesCount { get; private set; }
|
||||
/// <summary>Inject DeviceSymbolVersionInvalid into the next read of this symbol.</summary>
|
||||
public HashSet<string> SymbolVersionInvalidOnNextRead { get; } = new(StringComparer.OrdinalIgnoreCase);
|
||||
/// <summary>Inject DeviceSymbolVersionInvalid into the next write of this symbol.</summary>
|
||||
public HashSet<string> SymbolVersionInvalidOnNextWrite { get; } = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private readonly Dictionary<string, uint> _handleCache = new(StringComparer.OrdinalIgnoreCase);
|
||||
private uint _nextHandle = 1;
|
||||
|
||||
private uint EnsureFakeHandle(string symbolPath)
|
||||
{
|
||||
if (_handleCache.TryGetValue(symbolPath, out var existing)) return existing;
|
||||
HandleCreateInvocations.Add(symbolPath);
|
||||
var handle = _nextHandle++;
|
||||
_handleCache[symbolPath] = handle;
|
||||
return handle;
|
||||
}
|
||||
|
||||
private void EvictFakeHandle(string symbolPath)
|
||||
{
|
||||
if (_handleCache.Remove(symbolPath, out var handle))
|
||||
HandleDeleteInvocations.Add(handle);
|
||||
}
|
||||
|
||||
/// <summary>Test helper — current cached-handle count.</summary>
|
||||
public int HandleCacheCount => _handleCache.Count;
|
||||
|
||||
/// <summary>Test helper — true when the symbol currently has a cached handle.</summary>
|
||||
public bool HasCachedHandle(string symbolPath) => _handleCache.ContainsKey(symbolPath);
|
||||
|
||||
public virtual Task ConnectAsync(TwinCATAmsAddress address, TimeSpan timeout, CancellationToken ct)
|
||||
{
|
||||
ConnectCount++;
|
||||
if (ThrowOnConnect) throw Exception ?? new InvalidOperationException();
|
||||
// PR 2.2 — production wipes the handle cache on every (re)connect because handle
|
||||
// identity is per-AMS-session. Mirror so tests of the reconnect flow see the same
|
||||
// cache-clear semantics. Existing handles are dead with the prior session, no
|
||||
// wire-side delete needed.
|
||||
if (IsConnected) _handleCache.Clear();
|
||||
IsConnected = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>Test helper — simulate a reconnect (ConnectAsync after the connection drops).</summary>
|
||||
public void SimulateReconnect()
|
||||
{
|
||||
IsConnected = false;
|
||||
_handleCache.Clear();
|
||||
IsConnected = true;
|
||||
}
|
||||
|
||||
public virtual Task<(object? value, uint status)> ReadValueAsync(
|
||||
string symbolPath, TwinCATDataType type, int? bitIndex, int[]? arrayDimensions, CancellationToken ct)
|
||||
{
|
||||
if (ThrowOnRead) throw Exception ?? new InvalidOperationException();
|
||||
ReadLog.Add((symbolPath, type, bitIndex, arrayDimensions));
|
||||
|
||||
// PR 2.2 — mirror the production handle-cache state machine: resolve handle (cache
|
||||
// miss → HandleCreateInvocations++), do read-by-handle, on injected
|
||||
// SymbolVersionInvalid evict + retry once, then deliver the live value.
|
||||
ReadOneByHandle(symbolPath);
|
||||
|
||||
var status = ReadStatuses.TryGetValue(symbolPath, out var s) ? s : TwinCATStatusMapper.Good;
|
||||
var value = Values.TryGetValue(symbolPath, out var v) ? v : null;
|
||||
return Task.FromResult((value, status));
|
||||
}
|
||||
|
||||
private void ReadOneByHandle(string symbolPath)
|
||||
{
|
||||
EnsureFakeHandle(symbolPath);
|
||||
ReadByHandleInvocations.Add(symbolPath);
|
||||
if (SymbolVersionInvalidOnNextRead.Remove(symbolPath))
|
||||
{
|
||||
EvictFakeHandle(symbolPath);
|
||||
EnsureFakeHandle(symbolPath); // retry — fresh handle
|
||||
ReadByHandleInvocations.Add(symbolPath);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteOneByHandle(string symbolPath)
|
||||
{
|
||||
EnsureFakeHandle(symbolPath);
|
||||
WriteByHandleInvocations.Add(symbolPath);
|
||||
if (SymbolVersionInvalidOnNextWrite.Remove(symbolPath))
|
||||
{
|
||||
EvictFakeHandle(symbolPath);
|
||||
EnsureFakeHandle(symbolPath); // retry — fresh handle
|
||||
WriteByHandleInvocations.Add(symbolPath);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Task<uint> WriteValueAsync(
|
||||
string symbolPath, TwinCATDataType type, int? bitIndex, int[]? arrayDimensions, object? value, CancellationToken ct)
|
||||
{
|
||||
@@ -51,6 +135,10 @@ internal class FakeTwinCATClient : ITwinCATClient
|
||||
var parentPath = AdsTwinCATClient.TryGetParentSymbolPath(symbolPath);
|
||||
if (parentPath is not null)
|
||||
{
|
||||
// RMW touches the parent word twice (read + write); each goes through the
|
||||
// handle cache, exactly mirroring the production path.
|
||||
ReadOneByHandle(parentPath);
|
||||
WriteOneByHandle(parentPath);
|
||||
var current = Values.TryGetValue(parentPath, out var p) && p is not null
|
||||
? Convert.ToUInt32(p) : 0u;
|
||||
Values[parentPath] = AdsTwinCATClient.ApplyBit(
|
||||
@@ -59,6 +147,7 @@ internal class FakeTwinCATClient : ITwinCATClient
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteOneByHandle(symbolPath);
|
||||
Values[symbolPath] = value;
|
||||
}
|
||||
|
||||
@@ -66,6 +155,15 @@ internal class FakeTwinCATClient : ITwinCATClient
|
||||
return Task.FromResult(status);
|
||||
}
|
||||
|
||||
public virtual Task FlushOptionalCachesAsync()
|
||||
{
|
||||
FlushOptionalCachesCount++;
|
||||
// Mirror production: emit a delete record per cached handle, then clear.
|
||||
foreach (var kv in _handleCache) HandleDeleteInvocations.Add(kv.Value);
|
||||
_handleCache.Clear();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task<bool> ProbeAsync(CancellationToken ct)
|
||||
{
|
||||
if (ThrowOnProbe) return Task.FromResult(false);
|
||||
@@ -134,6 +232,10 @@ internal class FakeTwinCATClient : ITwinCATClient
|
||||
{
|
||||
DisposeCount++;
|
||||
IsConnected = false;
|
||||
// PR 2.2 — production deletes cached handles on Dispose; mirror so tests can assert
|
||||
// the fan-out delete count matches.
|
||||
foreach (var kv in _handleCache) HandleDeleteInvocations.Add(kv.Value);
|
||||
_handleCache.Clear();
|
||||
}
|
||||
|
||||
// ---- notification fake ----
|
||||
|
||||
Reference in New Issue
Block a user