using Grpc.Core; using ZB.MOM.WW.MxGateway.Contracts.Proto; namespace ZB.MOM.WW.MxGateway.Client.Tests; /// /// Fake implementation of IMxGatewayClientTransport for testing. /// internal sealed class FakeGatewayTransport(MxGatewayClientOptions options) : IMxGatewayClientTransport { private readonly Queue _invokeReplies = new(); private readonly List _events = []; /// public MxGatewayClientOptions Options { get; } = options; /// public MxAccessGateway.MxAccessGatewayClient? RawClient => null; /// /// Gets the list of captured OpenSessionAsync calls. /// public List<(OpenSessionRequest Request, CallOptions CallOptions)> OpenSessionCalls { get; } = []; /// /// Gets the list of captured CloseSessionAsync calls. /// public List<(CloseSessionRequest Request, CallOptions CallOptions)> CloseSessionCalls { get; } = []; /// /// Gets the list of captured InvokeAsync calls. /// public List<(MxCommandRequest Request, CallOptions CallOptions)> InvokeCalls { get; } = []; /// /// Gets the list of captured StreamEventsAsync calls. /// public List<(StreamEventsRequest Request, CallOptions CallOptions)> StreamEventsCalls { get; } = []; /// /// Gets the list of captured AcknowledgeAlarmAsync calls. /// public List<(AcknowledgeAlarmRequest Request, CallOptions CallOptions)> AcknowledgeAlarmCalls { get; } = []; /// /// Gets the list of captured QueryActiveAlarmsAsync calls. /// public List<(QueryActiveAlarmsRequest Request, CallOptions CallOptions)> QueryActiveAlarmsCalls { get; } = []; /// /// Gets the list of captured StreamAlarmsAsync calls. /// public List<(StreamAlarmsRequest Request, CallOptions CallOptions)> StreamAlarmsCalls { get; } = []; /// /// Gets the queue of exceptions to throw from AcknowledgeAlarmAsync. /// public Queue AcknowledgeAlarmExceptions { get; } = new(); private readonly Queue _acknowledgeReplies = new(); private readonly List _activeAlarmSnapshots = []; private readonly List _alarmFeedMessages = []; /// /// Gets or sets the reply to return from OpenSessionAsync. /// public OpenSessionReply OpenSessionReply { get; set; } = new() { SessionId = "session-fixture", BackendName = "mxaccess-worker", GatewayProtocolVersion = 1, WorkerProtocolVersion = 1, ProtocolStatus = new ProtocolStatus { Code = ProtocolStatusCode.Ok }, }; /// /// Gets or sets the reply to return from CloseSessionAsync. /// public CloseSessionReply CloseSessionReply { get; set; } = new() { SessionId = "session-fixture", FinalState = SessionState.Closed, ProtocolStatus = new ProtocolStatus { Code = ProtocolStatusCode.Ok }, }; /// /// Gets the queue of exceptions to throw from OpenSessionAsync. /// public Queue OpenSessionExceptions { get; } = new(); /// /// Gets the queue of exceptions to throw from CloseSessionAsync. /// public Queue CloseSessionExceptions { get; } = new(); /// /// Gets the queue of exceptions to throw from InvokeAsync. /// public Queue InvokeExceptions { get; } = new(); /// public Task OpenSessionAsync( OpenSessionRequest request, CallOptions callOptions) { OpenSessionCalls.Add((request, callOptions)); if (OpenSessionExceptions.TryDequeue(out Exception? exception)) { throw exception; } return Task.FromResult(OpenSessionReply); } /// public Task CloseSessionAsync( CloseSessionRequest request, CallOptions callOptions) { CloseSessionCalls.Add((request, callOptions)); if (CloseSessionExceptions.TryDequeue(out Exception? exception)) { throw exception; } return Task.FromResult(CloseSessionReply); } /// public Task InvokeAsync( MxCommandRequest request, CallOptions callOptions) { InvokeCalls.Add((request, callOptions)); if (InvokeExceptions.TryDequeue(out Exception? exception)) { throw exception; } return Task.FromResult(_invokeReplies.Dequeue()); } /// public async IAsyncEnumerable StreamEventsAsync( StreamEventsRequest request, CallOptions callOptions) { StreamEventsCalls.Add((request, callOptions)); foreach (MxEvent gatewayEvent in _events) { callOptions.CancellationToken.ThrowIfCancellationRequested(); await Task.Yield(); yield return gatewayEvent; } } /// /// Enqueues a reply to be returned from the next InvokeAsync call. /// /// The reply to enqueue. public void AddInvokeReply(MxCommandReply reply) { _invokeReplies.Enqueue(reply); } /// /// Enqueues an event to be yielded from StreamEventsAsync. /// /// The event to enqueue. public void AddEvent(MxEvent gatewayEvent) { _events.Add(gatewayEvent); } /// public Task AcknowledgeAlarmAsync( AcknowledgeAlarmRequest request, CallOptions callOptions) { AcknowledgeAlarmCalls.Add((request, callOptions)); if (AcknowledgeAlarmExceptions.TryDequeue(out Exception? exception)) { throw exception; } return Task.FromResult(_acknowledgeReplies.Count > 0 ? _acknowledgeReplies.Dequeue() : new AcknowledgeAlarmReply { CorrelationId = request.ClientCorrelationId, ProtocolStatus = new ProtocolStatus { Code = ProtocolStatusCode.Ok }, Status = new MxStatusProxy { Success = 1, Category = MxStatusCategory.Ok }, }); } /// public async IAsyncEnumerable QueryActiveAlarmsAsync( QueryActiveAlarmsRequest request, CallOptions callOptions) { QueryActiveAlarmsCalls.Add((request, callOptions)); foreach (ActiveAlarmSnapshot snapshot in _activeAlarmSnapshots) { callOptions.CancellationToken.ThrowIfCancellationRequested(); await Task.Yield(); yield return snapshot; } } /// Enqueues an acknowledge reply. /// The acknowledge reply to enqueue. public void AddAcknowledgeReply(AcknowledgeAlarmReply reply) { _acknowledgeReplies.Enqueue(reply); } /// Enqueues a snapshot to be yielded from QueryActiveAlarmsAsync. /// The snapshot to enqueue. public void AddActiveAlarmSnapshot(ActiveAlarmSnapshot snapshot) { _activeAlarmSnapshots.Add(snapshot); } /// public async IAsyncEnumerable StreamAlarmsAsync( StreamAlarmsRequest request, CallOptions callOptions) { StreamAlarmsCalls.Add((request, callOptions)); foreach (AlarmFeedMessage message in _alarmFeedMessages) { callOptions.CancellationToken.ThrowIfCancellationRequested(); await Task.Yield(); yield return message; } } /// Enqueues an alarm feed message to be yielded from StreamAlarmsAsync. /// The alarm feed message to enqueue. public void AddAlarmFeedMessage(AlarmFeedMessage message) { _alarmFeedMessages.Add(message); } }