Path: redb-route/tests/redb.Route.Tests.Grpc/GrpcInteropTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Grpc/GrpcInteropTests.cs
----
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Grpc;
namespace redb.Route.Tests.Grpc;
///
/// Live interop against an independent gRPC stack — @grpc/grpc-js, which shares no code with this
/// connector (profile grpc of docker-compose.tests.yml). This is what loopback e2e cannot prove: that a foreign
/// implementation accepts our hand-written framing, trailers, statuses, mTLS handshake and gzip, and that
/// we accept its. It is also the only cross-process coverage — the client runs inside the container.
///
/// The contract is a typed.proto (interop.Echo), not the generic RedbService,
/// so it doubles as proof that a generated client can call a redb route with no server stubs on our side.
/// Messages are encoded by hand here (one string field) to keep the test free of generated code.
///
///
/// GATED: every test no-ops unless the container answers on 127.0.0.1:18100, so the normal suite stays
/// green without it. Run with --filter Category=Interop once the grpc profile is up:
/// docker compose -f redb.Route/docker-compose.tests.yml --profile grpc up -d --wait.
///
///
[Trait("Category", "Interop")]
public class GrpcInteropTests
{
private const string Host = "127.0.0.1";
private const int ServerPort = 18100; // @grpc/grpc-js echo server (compose mapping)
// The mTLS material from redb.Route/tests/docker/gen-certs.sh (.generated/grpc).
private static string CertDir =>
Environment.GetEnvironmentVariable("REDB_GRPC_CERTS")
?? global::redb.Route.Tests.Shared.TestStand.Generated("grpc");
// Our consumers; the container reaches them via host-gateway. Offset by runtime major so the parallel
// multi-TFM test hosts never contend for the same port.
private static readonly int PlainPort = 18110 + Environment.Version.Major;
private static readonly int TlsPort = 18120 + Environment.Version.Major;
private static readonly int StreamPort = 18130 + Environment.Version.Major;
private static readonly int StatusPort = 18140 + Environment.Version.Major;
// ── Direction A: our producer → the independent grpc-js server ──
[Fact]
public async Task Redb_producer_to_grpcjs_server_echoes()
{
if (!IsReachable(Host, ServerPort)) return; // gated: profile grpc of docker-compose.tests.yml
await using var ctx = NewContext();
ctx.AddRoutes(r => r.From("direct://call")
.To(GrpcDsl.Call($"{Host}:{ServerPort}").Method("/interop.Echo/Say").Plaintext()));
await ctx.Start();
var producer = ctx.GetEndpoint("direct://call").CreateProducer();
await producer.Start();
var exchange = new Exchange(new Message(EncodeString("hello-redb")));
await producer.Process(exchange);
// A foreign gRPC server parsed our request frame, ran Say, and we read its reply frame back.
exchange.HasOut.Should().BeTrue();
DecodeString((byte[])exchange.Out!.Body!).Should().Be("echo:hello-redb");
}
[Fact]
public async Task Redb_producer_reads_a_grpcjs_server_stream()
{
if (!IsReachable(Host, ServerPort)) return;
await using var ctx = NewContext();
ctx.AddRoutes(r => r.From("direct://stream")
.To(GrpcDsl.Call($"{Host}:{ServerPort}").Method("/interop.Echo/SayMany").Plaintext().Streaming()));
await ctx.Start();
var producer = ctx.GetEndpoint("direct://stream").CreateProducer();
await producer.Start();
var exchange = new Exchange(new Message(EncodeString("go")));
await producer.Process(exchange);
var items = new List();
await foreach (var item in (IAsyncEnumerable