Documentation
¶
Overview ¶
Package grpctest spins up an in-memory gRPC server for tests, the gRPC counterpart of apitest. It runs a real grpcserver.Server — with the full skit interceptor chain (recovery, tracing, logging, and the errs.Error -> gRPC status mapping) — over an in-process bufconn listener, and hands back a *grpc.ClientConn dialed to it. No TCP port is bound, so tests are hermetic and free of port-conflict flakiness.
Because the real interceptors run, an integration test asserts true gRPC status codes (status.Code(err)) end to end, which a direct handler call cannot: calling a handler method returns the raw *errs.Error, before the errormap interceptor converts it to a status.
Usage:
srv := grpctest.New(t, handler) // handler implements grpcserver.Service
client := widgetv1.NewWidgetServiceClient(srv.Conn)
resp, err := client.GetWidget(ctx, &widgetv1.GetWidgetRequest{Id: id})
Index ¶
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, 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. opts are forwarded to grpcserver.New, e.g. grpcserver.WithUnaryInterceptors(authMW) to test an interceptor. The server and connection are torn down via t.Cleanup.