using System.Reflection;
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers;
public static class FixtureDiagnostic
{
public static string Text => "Synthetic local failure " + new string('x',5000) + " END-DIAGNOSTIC";
}
public class SyntheticOperationsFixture : DispatchProxy
{
private FixtureDeploymentState? _deployments;
public int AlarmRequests { get; private set; }
public string AlarmMode { get; set; } = "success";
public TaskCompletionSource? PendingAlarm { get; private set; }
public string? LastAlarmId { get; private set; }
public DateTime? LastUnshelveAtUtc { get; private set; }
public void ResetAlarmRequests(){AlarmRequests=0;LastAlarmId=null;LastUnshelveAtUtc=null;AlarmMode="success";PendingAlarm=null;}
public static IAdminOperationsClient WithDeployments(FixtureDeploymentState state)
{
var proxy=Create();
((SyntheticOperationsFixture)(object)proxy)._deployments=state;return proxy;
}
protected override object? Invoke(MethodInfo? method, object?[]? args)
{
if(method?.Name==nameof(IAdminOperationsClient.AcknowledgeAlarmAsync))
{ AlarmRequests++;LastAlarmId=(string)args![0]!;if(AlarmMode=="pending"){PendingAlarm=new(TaskCreationOptions.RunContinuationsAsynchronously);return PendingAlarm.Task;}
if(AlarmMode=="failure")return Task.FromException(new IOException(FixtureDiagnostic.Text));
return Task.FromResult(new AcknowledgeAlarmResult(true,null,new(Guid.Empty))); }
if(method?.Name==nameof(IAdminOperationsClient.ShelveAlarmAsync))
{ AlarmRequests++;LastAlarmId=(string)args![0]!;LastUnshelveAtUtc=(DateTime?)args[3];return Task.FromResult(new ShelveAlarmResult(true,null,new(Guid.Empty))); }
if(method?.Name==nameof(IAdminOperationsClient.StartDeploymentAsync))
return _deployments!.StartAsync((string)args![0]!, (CancellationToken)args[1]!);
if(method?.Name=="AskAsync" && method.GetGenericArguments().Single()==typeof(ReconnectDriverResult))
return Task.FromResult(new ReconnectDriverResult(false,FixtureDiagnostic.Text,Guid.Empty));
throw new InvalidOperationException("Disposable fixture rejects service operations.");
}
}
public sealed class FaultableFixtureStatusStore : IDriverStatusSnapshotStore
{
private readonly InMemoryDriverStatusSnapshotStore _inner = new();
public bool ReadFailure { get; set; }
public void Upsert(DriverHealthChanged snapshot) => _inner.Upsert(snapshot);
public bool TryGet(string id, out DriverHealthChanged snapshot) => _inner.TryGet(id,out snapshot);
public IReadOnlyCollection GetAll() => _inner.GetAll();
public IReadOnlyCollection GetAllWithReceiveTime() => ReadFailure ? throw new IOException(FixtureDiagnostic.Text) : _inner.GetAllWithReceiveTime();
public int EvictUnconfiguredStale(IReadOnlySet ids, DateTime older) => _inner.EvictUnconfiguredStale(ids,older);
public event Action? SnapshotChanged { add => _inner.SnapshotChanged += value; remove => _inner.SnapshotChanged -= value; }
}