mock

package
v0.0.0-...-1453545 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SayHelloRPC = "/hello.Hello/SayHello"
)

These constants represent the endpoints of the gRPC service and map directly to the FullMethod in the generated gRPC code. They are intended to be used directly in tests to mock specific responses for endpoints.

Variables

View Source
var ErrUnavailable = status.Error(codes.Unavailable, "mock method has not been configured")

Functions

This section is empty.

Types

type DialOption

type DialOption func(*Listener)

DialOption -- optional arguments for constructing a Listener.

func WithBuffer

func WithBuffer(sock *bufconn.Listener) DialOption

Allows you to pass an already instantiated grpc bufconn.Listener into the Listener.

func WithBufferSize

func WithBufferSize(size int) DialOption

The default buffer size is 1MiB -- if ou need a larger buffer to send larger messages then specify this dial option with a larger size.

func WithTarget

func WithTarget(target string) DialOption

WithTarget allows the user to change the "endpoint" of the connection from "bufnet" to some other endpoint. This is useful for tests that include mTLS to ensure that TLS certificate handling is correct.

type HelloService

type HelloService struct {
	sync.RWMutex
	pb.UnimplementedHelloServer

	Calls              map[string]int
	OnSayHello         func(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error)
	OnSayClientStream  func(ctx context.Context, stream pb.Hello_SayClientStreamServer) error
	OnSayServerStream  func(req *pb.HelloManyRequest, stream pb.Hello_SayServerStreamServer) error
	OnSayBidirectional func(stream pb.Hello_SayBidirectionalServer) error
	// contains filtered or unexported fields
}

func New

func New(bufnet *Listener, opts ...grpc.ServerOption) *HelloService

New creates a new mock service that can be connected to using a bufconn. If no bufnet is provided, a default one is created.

func (*HelloService) Client

func (s *HelloService) Client(ctx context.Context, opts ...grpc.DialOption) (client pb.HelloClient, err error)

Create and connect a client to the mock server

func (*HelloService) Reset

func (s *HelloService) Reset()

Reset the calls map and all associated handlers in preparation for a new test.

func (*HelloService) ResetClient

func (s *HelloService) ResetClient(ctx context.Context, opts ...grpc.DialOption) (pb.HelloClient, error)

Reset the client with the new dial options

func (*HelloService) SayBidirectional

func (s *HelloService) SayBidirectional(stream pb.Hello_SayBidirectionalServer) error

func (*HelloService) SayClientStream

func (s *HelloService) SayClientStream(stream pb.Hello_SayClientStreamServer) error

func (*HelloService) SayHello

func (s *HelloService) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error)

func (*HelloService) SayServerStream

func (s *HelloService) SayServerStream(req *pb.HelloManyRequest, stream pb.Hello_SayServerStreamServer) error

func (*HelloService) Shutdown

func (s *HelloService) Shutdown()

Shutdown the sever and cleanup (cannot be used after shutdown)

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener handles gRPC connections using an in-memory buffer that is useful for testing to prevent actual TCP network requests. A simpler approach for testing is to just invoke the methods directly, however using a bufconn connection provides the most realistic gRPC server for tests that include serialization and deserialization of protocol buffers and an actual wire transfer between client and server.

func NewBufConn

func NewBufConn(opts ...DialOption) *Listener

New creates a bufconn listener ready to attach servers and clients to. To provide a different target name (e.g. for mTLS buffers) use the WithTarget() dial option. You can also specify a different buffer size using WithBufferSize() or pass in an already instantiated bufconn.Listener using WithBuffer().

func (*Listener) Close

func (l *Listener) Close() error

Close the bufconn listener and prevent either clients or servers from communicating.

func (*Listener) Connect

func (l *Listener) Connect(ctx context.Context, opts ...grpc.DialOption) (cc *grpc.ClientConn, err error)

Connect returns the client side of the bufconn connection.

func (*Listener) Dialer

func (l *Listener) Dialer(context.Context, string) (net.Conn, error)

Dialer implements the ContextDialer interface for use with grpc.DialOptions

func (*Listener) Sock

func (l *Listener) Sock() net.Listener

Sock returns the server side of the bufconn connection.

Jump to

Keyboard shortcuts

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