Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Tests/FakeTwinCATClient.cs
Joseph Doherty bd6c0b4d3d docs: complete XML doc comments via fixdocs (2757 to 131 findings)
Add missing <returns>/<param>/<summary>/<typeparam> tags and clean up
misused inheritdoc across 481 files so the documented API surface is
complete. Documentation-only (zero code lines changed). The 131 remaining
findings are inheritdoc-style warnings deliberately left to preserve
hand-written implementation rationale (plan-decision notes, race-condition
explanations).
2026-06-03 12:34:34 -04:00

179 lines
8.1 KiB
C#

using System.Runtime.CompilerServices;
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;
namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Tests;
internal class FakeTwinCATClient : ITwinCATClient
{
/// <inheritdoc />
public bool IsConnected { get; private set; }
/// <summary>Gets the number of times Connect has been called.</summary>
public int ConnectCount { get; private set; }
/// <summary>Gets the number of times Dispose has been called.</summary>
public int DisposeCount { get; private set; }
/// <summary>Gets or sets a value indicating whether ConnectAsync should throw.</summary>
public bool ThrowOnConnect { get; set; }
/// <summary>Gets or sets a value indicating whether ReadValueAsync should throw.</summary>
public bool ThrowOnRead { get; set; }
/// <summary>Gets or sets a value indicating whether WriteValueAsync should throw.</summary>
public bool ThrowOnWrite { get; set; }
/// <summary>Gets or sets a value indicating whether ProbeAsync should throw.</summary>
public bool ThrowOnProbe { get; set; }
/// <summary>Gets or sets the exception to throw when a throw flag is set.</summary>
public Exception? Exception { get; set; }
/// <summary>Gets the simulated values by symbol path.</summary>
public Dictionary<string, object?> Values { get; } = new(StringComparer.OrdinalIgnoreCase);
/// <summary>Gets the read statuses by symbol path.</summary>
public Dictionary<string, uint> ReadStatuses { get; } = new(StringComparer.OrdinalIgnoreCase);
/// <summary>Gets the write statuses by symbol path.</summary>
public Dictionary<string, uint> WriteStatuses { get; } = new(StringComparer.OrdinalIgnoreCase);
/// <summary>Gets the log of all write operations.</summary>
public List<(string symbol, TwinCATDataType type, int? bit, object? value)> WriteLog { get; } = new();
/// <summary>Gets or sets the result returned by ProbeAsync.</summary>
public bool ProbeResult { get; set; } = true;
/// <summary>Occurs when the symbol version changes.</summary>
public event EventHandler? OnSymbolVersionChanged;
/// <summary>Test hook — fire the symbol-version-changed signal as the real client would.</summary>
public void FireSymbolVersionChanged() => OnSymbolVersionChanged?.Invoke(this, EventArgs.Empty);
/// <inheritdoc />
public virtual Task ConnectAsync(TwinCATAmsAddress address, TimeSpan timeout, CancellationToken ct)
{
ConnectCount++;
if (ThrowOnConnect) throw Exception ?? new InvalidOperationException();
IsConnected = true;
return Task.CompletedTask;
}
/// <inheritdoc />
public virtual Task<(object? value, uint status)> ReadValueAsync(
string symbolPath, TwinCATDataType type, int? bitIndex, CancellationToken ct)
{
if (ThrowOnRead) throw Exception ?? new InvalidOperationException();
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));
}
/// <inheritdoc />
public virtual Task<uint> WriteValueAsync(
string symbolPath, TwinCATDataType type, int? bitIndex, object? value, CancellationToken ct)
{
if (ThrowOnWrite) throw Exception ?? new InvalidOperationException();
WriteLog.Add((symbolPath, type, bitIndex, value));
Values[symbolPath] = value;
var status = WriteStatuses.TryGetValue(symbolPath, out var s) ? s : TwinCATStatusMapper.Good;
return Task.FromResult(status);
}
/// <inheritdoc />
public virtual Task<bool> ProbeAsync(CancellationToken ct)
{
if (ThrowOnProbe) return Task.FromResult(false);
return Task.FromResult(ProbeResult);
}
/// <summary>Releases unmanaged resources.</summary>
public virtual void Dispose()
{
DisposeCount++;
IsConnected = false;
}
// ---- notification fake ----
/// <summary>Gets the list of registered notifications.</summary>
public List<FakeNotification> Notifications { get; } = new();
/// <summary>Gets or sets a value indicating whether AddNotificationAsync should throw.</summary>
public bool ThrowOnAddNotification { get; set; }
/// <summary>Records the most recently-supplied <c>maxDelayMs</c> for Driver.TwinCAT-014 tests.</summary>
public int LastMaxDelayMs { get; private set; }
/// <inheritdoc />
public virtual Task<ITwinCATNotificationHandle> AddNotificationAsync(
string symbolPath, TwinCATDataType type, int? bitIndex, TimeSpan cycleTime,
int maxDelayMs, Action<string, object?> onChange, CancellationToken cancellationToken)
{
if (ThrowOnAddNotification)
throw Exception ?? new InvalidOperationException("fake AddNotification failure");
LastMaxDelayMs = maxDelayMs;
var reg = new FakeNotification(symbolPath, type, bitIndex, onChange, this);
Notifications.Add(reg);
return Task.FromResult<ITwinCATNotificationHandle>(reg);
}
/// <summary>Fire a change event through the registered callback for <paramref name="symbolPath"/>.</summary>
/// <param name="symbolPath">The symbol path for which to fire the change.</param>
/// <param name="value">The new value to pass to the callback.</param>
public void FireNotification(string symbolPath, object? value)
{
foreach (var n in Notifications)
if (!n.Disposed && string.Equals(n.SymbolPath, symbolPath, StringComparison.OrdinalIgnoreCase))
n.OnChange(symbolPath, value);
}
// ---- symbol browser fake ----
/// <summary>Gets the simulated browse results.</summary>
public List<TwinCATDiscoveredSymbol> BrowseResults { get; } = new();
/// <summary>Gets or sets a value indicating whether BrowseSymbolsAsync should throw.</summary>
public bool ThrowOnBrowse { get; set; }
/// <inheritdoc />
public virtual async IAsyncEnumerable<TwinCATDiscoveredSymbol> BrowseSymbolsAsync(
[EnumeratorCancellation] CancellationToken cancellationToken)
{
if (ThrowOnBrowse) throw Exception ?? new InvalidOperationException("fake browse failure");
await Task.CompletedTask;
foreach (var sym in BrowseResults)
{
if (cancellationToken.IsCancellationRequested) yield break;
yield return sym;
}
}
/// <summary>Represents a registered notification in the fake client.</summary>
public sealed class FakeNotification(
string symbolPath, TwinCATDataType type, int? bitIndex,
Action<string, object?> onChange, FakeTwinCATClient owner) : ITwinCATNotificationHandle
{
/// <summary>Gets the symbol path being watched.</summary>
public string SymbolPath { get; } = symbolPath;
/// <summary>Gets the data type of the symbol.</summary>
public TwinCATDataType Type { get; } = type;
/// <summary>Gets the optional bit index.</summary>
public int? BitIndex { get; } = bitIndex;
/// <summary>Gets the callback to invoke on value change.</summary>
public Action<string, object?> OnChange { get; } = onChange;
/// <summary>Gets a value indicating whether this notification has been disposed.</summary>
public bool Disposed { get; private set; }
/// <summary>Disposes this notification handle.</summary>
public void Dispose()
{
Disposed = true;
owner.Notifications.Remove(this);
}
}
}
/// <summary>Represents a factory for creating fake TwinCAT clients.</summary>
internal sealed class FakeTwinCATClientFactory : ITwinCATClientFactory
{
/// <summary>Gets the list of clients created by this factory.</summary>
public List<FakeTwinCATClient> Clients { get; } = new();
/// <summary>Gets or sets an optional customization function for creating clients.</summary>
public Func<FakeTwinCATClient>? Customise { get; set; }
/// <inheritdoc />
public ITwinCATClient Create()
{
var client = Customise?.Invoke() ?? new FakeTwinCATClient();
Clients.Add(client);
return client;
}
}