grpcmocks

package
v0.0.0-...-760d8ea Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultToken = &oauth2.Token{
	AccessToken:  "secret-access-token",
	TokenType:    "custom-token-type",
	RefreshToken: "refresh-token",
	Expiry:       time.Now().Add(time.Hour),
	ExpiresIn:    999999999,
}

Functions

func FakeClientCerts

func FakeClientCerts(mocked ...[]byte) func() (*x509.CertPool, error)

func FakeGRPCServer

func FakeGRPCServer(srv testgrpc.TestServiceServer, keyFile, certFile []byte) (func(), error)

func FakeTokenSource

func FakeTokenSource(
	token oauth2.TokenSource,
) func(ctx context.Context, audience string, opts ...idtoken.ClientOption) (oauth2.TokenSource, error)

func RegisterServiceServerOption

func RegisterServiceServerOption(f func(grpc.ServiceRegistrar)) grpc.ServerOption

RegisterServiceServerOption returns a ServerOption that will run f() in Start or StartServer with the grpc.Server created before serving. This allows other services to be registered on the test server (e.g. ORCA, health, or reflection).

Types

type GRPCServer

type GRPCServer interface {
	grpc.ServiceRegistrar
	Stop()
	GracefulStop()
	Serve(net.Listener) error
}

GRPCServer is an interface that groups methods implemented by a grpc.Server or an xds.GRPCServer that are used by the StubServer.

type IDTokenStub

type IDTokenStub struct {
	oauth2.TokenSource
}

func (*IDTokenStub) Token

func (i *IDTokenStub) Token() (*oauth2.Token, error)

type StubServer

type StubServer struct {
	// Guarantees we satisfy this interface; panics if unimplemented methods are called.
	testgrpc.TestServiceServer

	// Customizable implementations of server handlers.
	EmptyCallF      func(ctx context.Context, in *testgrpc.Empty) (*testgrpc.Empty, error)
	UnaryCallF      func(ctx context.Context, in *testgrpc.SimpleRequest) (*testgrpc.SimpleResponse, error)
	FullDuplexCallF func(stream testgrpc.TestService_FullDuplexCallServer) error

	// A client connected to this service the test may use.  Created in Start().
	Client testgrpc.TestServiceClient
	CC     *grpc.ClientConn

	// Server to serve this service from.
	//
	// If nil, a new grpc.Server is created, listening on the provided Network
	// and Address fields, or listening using the provided Listener.
	S GRPCServer

	// Parameters for Listen and Dial. Defaults will be used if these are empty
	// before Start.
	Network string
	Address string
	Target  string

	// Custom listener to use for serving. If unspecified, a new listener is
	// created on a local port.
	Listener net.Listener

	// Set automatically if Target == ""
	R *manual.Resolver
	// contains filtered or unexported fields
}

StubServer is a server that is easy to customize within individual test cases.

func StartTestService

func StartTestService(t *testing.T, server *StubServer, sopts ...grpc.ServerOption) *StubServer

StartTestService spins up a stub server exposing the TestService on a local port. If the passed in server is nil, a stub server that implements only the EmptyCall and UnaryCall RPCs is started.

func (*StubServer) EmptyCall

func (ss *StubServer) EmptyCall(ctx context.Context, in *testgrpc.Empty) (*testgrpc.Empty, error)

EmptyCall is the handler for testgrpc.EmptyCall

func (*StubServer) FullDuplexCall

func (ss *StubServer) FullDuplexCall(stream testgrpc.TestService_FullDuplexCallServer) error

FullDuplexCall is the handler for testgrpc.FullDuplexCall

func (*StubServer) NewServiceConfig

func (ss *StubServer) NewServiceConfig(sc string)

NewServiceConfig applies sc to ss.Client using the resolver (if present).

func (*StubServer) Start

func (ss *StubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption) error

Start starts the server and creates a client connected to it.

func (*StubServer) StartClient

func (ss *StubServer) StartClient(dopts ...grpc.DialOption) error

StartClient creates a client connected to this service that the test may use. The newly created client will be available in the Client field of StubServer.

func (*StubServer) StartHandlerServer

func (ss *StubServer) StartHandlerServer(sopts ...grpc.ServerOption) error

StartHandlerServer only starts an HTTP server with a gRPC server as the handler. It does not create a client to it. Cannot be used in a StubServer that also used StartServer.

func (*StubServer) StartServer

func (ss *StubServer) StartServer(sopts ...grpc.ServerOption) error

StartServer only starts the server. It does not create a client to it. Cannot be used in a StubServer that also used StartHandlerServer.

func (*StubServer) Stop

func (ss *StubServer) Stop()

Stop stops ss and cleans up all resources it consumed.

func (*StubServer) UnaryCall

UnaryCall is the handler for testgrpc.UnaryCall

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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