Linter/formatter pass across the full codebase. Restores required partial keyword on AXAML code-behind classes that the formatter incorrectly removed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
210 lines
9.1 KiB
C#
210 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using ArchestrA.MxAccess;
|
|
using ZB.MOM.WW.LmxOpcUa.Host.Domain;
|
|
|
|
namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
|
|
{
|
|
/// <summary>
|
|
/// Fake IMxProxy for testing without the MxAccess COM runtime.
|
|
/// Simulates connections, subscriptions, data changes, and writes.
|
|
/// </summary>
|
|
public class FakeMxProxy : IMxProxy
|
|
{
|
|
private int _connectionHandle;
|
|
private int _nextHandle = 1;
|
|
|
|
/// <summary>
|
|
/// Gets the item-handle to tag-reference map built by the test as attributes are registered with the fake runtime.
|
|
/// </summary>
|
|
public ConcurrentDictionary<int, string> Items { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Gets the item handles currently marked as advised so tests can assert subscription behavior.
|
|
/// </summary>
|
|
public ConcurrentDictionary<int, bool> AdvisedItems { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Gets the values written through the fake runtime so write scenarios can assert the final payload.
|
|
/// </summary>
|
|
public List<(string Address, object Value)> WrittenValues { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the fake runtime is currently considered registered.
|
|
/// </summary>
|
|
public bool IsRegistered { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the number of times the system under test attempted to register with the fake runtime.
|
|
/// </summary>
|
|
public int RegisterCallCount { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the number of times the system under test attempted to unregister from the fake runtime.
|
|
/// </summary>
|
|
public int UnregisterCallCount { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether registration should fail to exercise connection-error paths.
|
|
/// </summary>
|
|
public bool ShouldFailRegister { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether writes should fail to exercise runtime write-error paths.
|
|
/// </summary>
|
|
public bool ShouldFailWrite { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the fake should suppress the write-complete callback for timeout scenarios.
|
|
/// </summary>
|
|
public bool SkipWriteCompleteCallback { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the status code returned in the simulated write-complete callback.
|
|
/// </summary>
|
|
public int WriteCompleteStatus { get; set; } = 0; // 0 = success
|
|
|
|
/// <summary>
|
|
/// Occurs when the fake proxy publishes a simulated runtime data-change callback to the system under test.
|
|
/// </summary>
|
|
public event MxDataChangeHandler? OnDataChange;
|
|
|
|
/// <summary>
|
|
/// Occurs when the fake proxy publishes a simulated write-complete callback to the system under test.
|
|
/// </summary>
|
|
public event MxWriteCompleteHandler? OnWriteComplete;
|
|
|
|
/// <summary>
|
|
/// Simulates the MXAccess registration handshake and returns a synthetic connection handle.
|
|
/// </summary>
|
|
/// <param name="clientName">The client name supplied by the code under test.</param>
|
|
/// <returns>A synthetic connection handle for subsequent fake operations.</returns>
|
|
public int Register(string clientName)
|
|
{
|
|
RegisterCallCount++;
|
|
if (ShouldFailRegister) throw new InvalidOperationException("Register failed (simulated)");
|
|
IsRegistered = true;
|
|
_connectionHandle = Interlocked.Increment(ref _nextHandle);
|
|
return _connectionHandle;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates tearing down the fake MXAccess connection.
|
|
/// </summary>
|
|
/// <param name="handle">The connection handle supplied by the code under test.</param>
|
|
public void Unregister(int handle)
|
|
{
|
|
UnregisterCallCount++;
|
|
IsRegistered = false;
|
|
_connectionHandle = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates resolving a tag reference into a fake runtime item handle.
|
|
/// </summary>
|
|
/// <param name="handle">The synthetic connection handle.</param>
|
|
/// <param name="address">The Galaxy attribute reference being registered.</param>
|
|
/// <returns>A synthetic item handle.</returns>
|
|
public int AddItem(int handle, string address)
|
|
{
|
|
var itemHandle = Interlocked.Increment(ref _nextHandle);
|
|
Items[itemHandle] = address;
|
|
return itemHandle;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates removing an item from the fake runtime session.
|
|
/// </summary>
|
|
/// <param name="handle">The synthetic connection handle.</param>
|
|
/// <param name="itemHandle">The synthetic item handle to remove.</param>
|
|
public void RemoveItem(int handle, int itemHandle)
|
|
{
|
|
Items.TryRemove(itemHandle, out _);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Marks an item as actively advised so tests can assert subscription activation.
|
|
/// </summary>
|
|
/// <param name="handle">The synthetic connection handle.</param>
|
|
/// <param name="itemHandle">The synthetic item handle being monitored.</param>
|
|
public void AdviseSupervisory(int handle, int itemHandle)
|
|
{
|
|
AdvisedItems[itemHandle] = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Marks an item as no longer advised so tests can assert subscription teardown.
|
|
/// </summary>
|
|
/// <param name="handle">The synthetic connection handle.</param>
|
|
/// <param name="itemHandle">The synthetic item handle no longer being monitored.</param>
|
|
public void UnAdviseSupervisory(int handle, int itemHandle)
|
|
{
|
|
AdvisedItems.TryRemove(itemHandle, out _);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates a runtime write, records the written value, and optionally raises the write-complete callback.
|
|
/// </summary>
|
|
/// <param name="handle">The synthetic connection handle.</param>
|
|
/// <param name="itemHandle">The synthetic item handle to write.</param>
|
|
/// <param name="value">The value supplied by the system under test.</param>
|
|
/// <param name="securityClassification">The security classification supplied with the write request.</param>
|
|
public void Write(int handle, int itemHandle, object value, int securityClassification)
|
|
{
|
|
if (ShouldFailWrite) throw new InvalidOperationException("Write failed (simulated)");
|
|
|
|
if (Items.TryGetValue(itemHandle, out var address))
|
|
WrittenValues.Add((address, value));
|
|
|
|
// Simulate async write complete callback
|
|
var status = new MXSTATUS_PROXY[1];
|
|
if (WriteCompleteStatus == 0)
|
|
{
|
|
status[0].success = 1;
|
|
}
|
|
else
|
|
{
|
|
status[0].success = 0;
|
|
status[0].detail = (short)WriteCompleteStatus;
|
|
}
|
|
|
|
if (!SkipWriteCompleteCallback)
|
|
OnWriteComplete?.Invoke(_connectionHandle, itemHandle, ref status);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates an MXAccess data change event for a specific item handle.
|
|
/// </summary>
|
|
/// <param name="itemHandle">The synthetic item handle that should receive the new value.</param>
|
|
/// <param name="value">The value to publish to the system under test.</param>
|
|
/// <param name="quality">The runtime quality code to send with the value.</param>
|
|
/// <param name="timestamp">The optional timestamp to send with the value; defaults to the current UTC time.</param>
|
|
public void SimulateDataChange(int itemHandle, object value, int quality = 192, DateTime? timestamp = null)
|
|
{
|
|
var status = new MXSTATUS_PROXY[1];
|
|
status[0].success = 1;
|
|
OnDataChange?.Invoke(_connectionHandle, itemHandle, value, quality,
|
|
timestamp ?? DateTime.UtcNow, ref status);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Simulates data change for a specific address (finds handle by address).
|
|
/// </summary>
|
|
/// <param name="address">The Galaxy attribute reference whose registered handle should receive the new value.</param>
|
|
/// <param name="value">The value to publish to the system under test.</param>
|
|
/// <param name="quality">The runtime quality code to send with the value.</param>
|
|
/// <param name="timestamp">The optional timestamp to send with the value; defaults to the current UTC time.</param>
|
|
public void SimulateDataChangeByAddress(string address, object value, int quality = 192,
|
|
DateTime? timestamp = null)
|
|
{
|
|
foreach (var kvp in Items)
|
|
if (string.Equals(kvp.Value, address, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
SimulateDataChange(kvp.Key, value, quality, timestamp);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
} |