grpctest

package
v0.18.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package grpctest runs an in-memory gRPC server for tests, the gRPC counterpart of apitest. It serves a real grpcserver.Server — the full skit interceptor chain: recovery, tracing, logging and the mapping of an errs.Error to a gRPC status — over an in-process bufconn listener, and hands back a *grpc.ClientConn dialed to it. No TCP port is bound, so the tests are hermetic and free of port conflicts.

Because the real interceptors run, a test asserts the gRPC status codes a client gets (status.Code(err)), which a direct call of a handler cannot: the handler returns the *errs.Error before the interceptor maps it to a status.

Usage

func TestGetWidget(t *testing.T) {
    srv := grpctest.New(t, grpcserver.Config{}, []grpcserver.Service{handler})
    client := widgetv1.NewWidgetServiceClient(srv.Conn)

    _, err := client.GetWidget(t.Context(), &widgetv1.GetWidgetRequest{Id: "x"})
    if status.Code(err) != codes.NotFound {
        t.Fatalf("got %v, want NotFound", err)
    }
}

New forwards cfg and the options to grpcserver.New: cfg.TracerProvider records the spans of a test, grpcserver.WithUnaryInterceptors adds an interceptor under test. The server and the connection close with the test (t.Cleanup).

Example

A test calls the server through Conn and checks the status code the client gets: the interceptors run as in a service, the mapping of an *errs.Error included. Here an interceptor under test guards the health service; a test of a feature passes its handler in svcs.

package main

import (
	"context"
	"testing"

	"github.com/matryer/is"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"

	healthpb "google.golang.org/grpc/health/grpc_health_v1"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"

	"github.com/assanoff/skit/errs"
	"github.com/assanoff/skit/grpcserver"
	"github.com/assanoff/skit/grpctest"
)

func main() {
	var t *testing.T // the *testing.T of the test
	is := is.New(t)
	ctx := context.Background() // t.Context() in a test

	tenant := func(ctx context.Context, req any, _ *grpc.UnaryServerInfo,
		h grpc.UnaryHandler,
	) (any, error) {
		md, _ := metadata.FromIncomingContext(ctx)
		if len(md.Get("x-tenant")) == 0 {
			return nil, errs.Newf(errs.InvalidArgument, "no x-tenant")
		}
		return h(ctx, req)
	}
	srv := grpctest.New(t, grpcserver.Config{}, nil, grpcserver.WithUnaryInterceptors(tenant))
	client := healthpb.NewHealthClient(srv.Conn)

	_, err := client.Check(ctx, &healthpb.HealthCheckRequest{})
	is.Equal(status.Code(err), codes.InvalidArgument)

	resp, err := client.Check(metadata.AppendToOutgoingContext(ctx, "x-tenant", "acme"),
		&healthpb.HealthCheckRequest{})
	is.NoErr(err)
	is.Equal(resp.GetStatus(), healthpb.HealthCheckResponse_SERVING)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	Conn *grpc.ClientConn
	// contains filtered or unexported fields
}

Server is an in-memory gRPC server plus a client connection to it. Conn is ready to build generated client stubs against. Cleanup is registered with the test, so callers need not close anything.

func New

func New(t *testing.T, cfg grpcserver.Config, svcs []grpcserver.Service, opts ...grpcserver.Option) *Server

New starts an in-memory gRPC server with svcs registered (each is a grpcserver.Service — the Register seam the scaffolded handler implements) and returns a Server whose Conn dials it over bufconn. cfg and opts are forwarded to grpcserver.New: cfg.TracerProvider records the spans of a test, grpcserver.WithUnaryInterceptors(authMW) adds an interceptor under test. The server and connection are torn down via t.Cleanup.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL