104 lines
3.5 KiB
C#
104 lines
3.5 KiB
C#
using Grpc.Core;
|
|
using MxGateway.Contracts.Proto.Galaxy;
|
|
|
|
namespace MxGateway.Client.Tests;
|
|
|
|
internal sealed class FakeGalaxyRepositoryTransport(MxGatewayClientOptions options) : IGalaxyRepositoryClientTransport
|
|
{
|
|
public MxGatewayClientOptions Options { get; } = options;
|
|
|
|
public GalaxyRepository.GalaxyRepositoryClient? RawClient => null;
|
|
|
|
public List<(TestConnectionRequest Request, CallOptions CallOptions)> TestConnectionCalls { get; } = [];
|
|
|
|
public List<(GetLastDeployTimeRequest Request, CallOptions CallOptions)> GetLastDeployTimeCalls { get; } = [];
|
|
|
|
public List<(DiscoverHierarchyRequest Request, CallOptions CallOptions)> DiscoverHierarchyCalls { get; } = [];
|
|
|
|
public TestConnectionReply TestConnectionReply { get; set; } = new() { Ok = true };
|
|
|
|
public GetLastDeployTimeReply GetLastDeployTimeReply { get; set; } = new() { Present = false };
|
|
|
|
public DiscoverHierarchyReply DiscoverHierarchyReply { get; set; } = new();
|
|
|
|
public Queue<Exception> TestConnectionExceptions { get; } = new();
|
|
|
|
public Queue<Exception> GetLastDeployTimeExceptions { get; } = new();
|
|
|
|
public Queue<Exception> DiscoverHierarchyExceptions { get; } = new();
|
|
|
|
public Task<TestConnectionReply> TestConnectionAsync(
|
|
TestConnectionRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
TestConnectionCalls.Add((request, callOptions));
|
|
if (TestConnectionExceptions.TryDequeue(out Exception? exception))
|
|
{
|
|
throw exception;
|
|
}
|
|
|
|
return Task.FromResult(TestConnectionReply);
|
|
}
|
|
|
|
public Task<GetLastDeployTimeReply> GetLastDeployTimeAsync(
|
|
GetLastDeployTimeRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
GetLastDeployTimeCalls.Add((request, callOptions));
|
|
if (GetLastDeployTimeExceptions.TryDequeue(out Exception? exception))
|
|
{
|
|
throw exception;
|
|
}
|
|
|
|
return Task.FromResult(GetLastDeployTimeReply);
|
|
}
|
|
|
|
public Task<DiscoverHierarchyReply> DiscoverHierarchyAsync(
|
|
DiscoverHierarchyRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
DiscoverHierarchyCalls.Add((request, callOptions));
|
|
if (DiscoverHierarchyExceptions.TryDequeue(out Exception? exception))
|
|
{
|
|
throw exception;
|
|
}
|
|
|
|
return Task.FromResult(DiscoverHierarchyReply);
|
|
}
|
|
|
|
public List<(WatchDeployEventsRequest Request, CallOptions CallOptions)> WatchDeployEventsCalls { get; } = [];
|
|
|
|
public List<DeployEvent> WatchDeployEvents { get; } = [];
|
|
|
|
public Exception? WatchDeployEventsException { get; set; }
|
|
|
|
/// <summary>
|
|
/// When set, awaited before each event yield so tests can observe cancellation
|
|
/// mid-stream. Receives the call's cancellation token.
|
|
/// </summary>
|
|
public Func<CancellationToken, Task>? WatchDeployEventsBeforeYield { get; set; }
|
|
|
|
public async IAsyncEnumerable<DeployEvent> WatchDeployEventsAsync(
|
|
WatchDeployEventsRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
WatchDeployEventsCalls.Add((request, callOptions));
|
|
|
|
if (WatchDeployEventsException is not null)
|
|
{
|
|
throw WatchDeployEventsException;
|
|
}
|
|
|
|
foreach (DeployEvent deployEvent in WatchDeployEvents)
|
|
{
|
|
if (WatchDeployEventsBeforeYield is not null)
|
|
{
|
|
await WatchDeployEventsBeforeYield(callOptions.CancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
callOptions.CancellationToken.ThrowIfCancellationRequested();
|
|
yield return deployEvent;
|
|
}
|
|
}
|
|
}
|