mishima-nexus-client-csharp/tests/Robotico.Nexus.Client.Tests/TestSupport/FakeNexusTriggers.cs

33 lines
1.5 KiB
C#
Raw Permalink Normal View History

using Grpc.Core;
using Mishima.Nexus.Wire.V1;
namespace Robotico.Nexus.Client.Tests.TestSupport;
/// <summary>
/// Sentinel logical-database names (sent via the <c>x-nexus-database</c> metadata header) that let
/// tests deterministically drive the failure branches of <c>Ping</c>-fronted RPCs without a real
/// unreachable engine.
/// </summary>
public static class FakeNexusTriggers
{
/// <summary>Ping succeeds at the transport level but the engine reports a non-OK status.</summary>
public const string PingReportsFailure = "trigger-ping-status-failure";
/// <summary>Ping fails at the transport level (simulated <see cref="RpcException"/>).</summary>
public const string PingThrows = "trigger-ping-rpc-exception";
/// <summary>Returns a failure <see cref="NexusStatus"/> when the request's database header is <see cref="PingReportsFailure"/>, or throws when it is <see cref="PingThrows"/>.</summary>
public static NexusStatus? TryStatusFailure(ServerCallContext context)
{
string? database = context.RequestHeaders.GetValue(NexusCallMetadata.DatabaseKey);
if (string.Equals(database, PingThrows, StringComparison.Ordinal))
{
throw new RpcException(new Status(StatusCode.Unavailable, "Simulated transport failure."));
}
return string.Equals(database, PingReportsFailure, StringComparison.Ordinal)
? NexusStatuses.Failure("NXS_UNAVAILABLE", "Simulated engine-reported failure.")
: null;
}
}