testing

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

This package is bilt using the google.golang.org/grpc/test/bufconn package to help you avoid starting up a service with a real port number, but still allowing testing of streaming RPCs.

The benefit of this approach is that you're still getting network behavior, but over an in-memory connection without using OS-level resources like ports that may or may not clean up quickly. And it allows you to test it the way it's actually used, and it gives you proper streaming behavior.

We provide an In memory communication between client and server, helpful to write unit and integration tests. When writing integration tests we should avoid having the networking element from your test as it is slow to assign and release ports

var server GrpcInProcessingServer

func serverStart() {
	builder := GrpcInProcessingServerBuilder{}
	builder.SetUnaryInterceptors(util.GetDefaultUnaryServerInterceptors())
	server = builder.Build()
	server.RegisterService(func(server *grpc.Server) {
		helloworld.RegisterGreeterServer(server, &testdata.MockedService{})
	})
	server.Start()
}

//TestSayHello will test the HelloWorld service using A in memory data transfer instead of the normal networking
func TestSayHello(t *testing.T) {
	serverStart()
	ctx := context.Background()
	clientConn, err := GetInProcessingClientConn(ctx, server.GetListener(), []grpc.DialOption{})
	if err != nil {
		t.Fatalf("Failed to dial bufnet: %v", err)
	}
	defer clientConn.Close()
	client := helloworld.NewGreeterClient(clientConn)
	request := &helloworld.HelloRequest{Name: "test"}
	resp, err := client.SayHello(ctx, request)
	if err != nil {
		t.Fatalf("SayHello failed: %v", err)
	}
	server.Cleanup()
	clientConn.Close()
	assert.Equal(t, resp.Message, "This is a mocked service test")
}

Documentation

Overview

In Processing server uses memory to transfer data between the server and the client This is ideal for testing propose as it not require networking to run integration testes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBufDialer

func GetBufDialer(listener *bufconn.Listener) func(context.Context, string) (net.Conn, error)

func GetInProcessingClientConn

func GetInProcessingClientConn(ctx context.Context, listener *bufconn.Listener, options []grpc.DialOption) (*grpc.ClientConn, error)

func GetInProcessingGRPCServer

func GetInProcessingGRPCServer(options []grpc.ServerOption) (*grpc.Server, *bufconn.Listener)

Types

type GrpcInProcessingServer

type GrpcInProcessingServer interface {
	Start() error
	AwaitTermination(shutdownHook func())
	RegisterService(reg func(*grpc.Server))
	Cleanup()
	GetListener() *bufconn.Listener
}

GRPC server interface

type GrpcInProcessingServerBuilder

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

GRPC in-processing server builder

func (*GrpcInProcessingServerBuilder) AddOption

DialOption configures how we set up the connection.

func (*GrpcInProcessingServerBuilder) Build

Build is responsible for building a Fiji GRPC server

func (*GrpcInProcessingServerBuilder) SetStreamInterceptors

func (sb *GrpcInProcessingServerBuilder) SetStreamInterceptors(interceptors []grpc.StreamServerInterceptor)

SetStreamInterceptors set a list of interceptors to the Grpc server for stream connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the server side. By using `grpc_middleware` we are able to provides convenient method to add a list of interceptors

func (*GrpcInProcessingServerBuilder) SetTlsCert

func (sb *GrpcInProcessingServerBuilder) SetTlsCert(cert *tls.Certificate)

SetTlsCert sets credentials for server connections

func (*GrpcInProcessingServerBuilder) SetUnaryInterceptors

func (sb *GrpcInProcessingServerBuilder) SetUnaryInterceptors(interceptors []grpc.UnaryServerInterceptor)

SetUnaryInterceptors set a list of interceptors to the Grpc server for unary connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the server side. By using `grpc_middleware` we are able to provides convenient method to add a list of interceptors

type InProcessingClientBuilder

type InProcessingClientBuilder struct {
	Server GrpcInProcessingServer
	// contains filtered or unexported fields
}

func (*InProcessingClientBuilder) GetConn

func (b *InProcessingClientBuilder) GetConn(addr string, port string) (*grpc.ClientConn, error)

GetConn returns the client connection to the Server

func (*InProcessingClientBuilder) WithContext

func (b *InProcessingClientBuilder) WithContext(ctx context.Context)

WithContext set the context to be used in the dial

func (*InProcessingClientBuilder) WithInsecure

func (b *InProcessingClientBuilder) WithInsecure()

WithInsecure set the connection as insecure

func (*InProcessingClientBuilder) WithOptions

func (b *InProcessingClientBuilder) WithOptions(opts ...grpc.DialOption)

WithOptions set dial options

func (*InProcessingClientBuilder) WithStreamInterceptors

func (b *InProcessingClientBuilder) WithStreamInterceptors(interceptors []grpc.StreamClientInterceptor)

WithUnaryInterceptors set a list of interceptors to the Grpc client for stream connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the Server side. By using `grpc_middleware` we are able to provides convenient method to add a list of interceptors

func (*InProcessingClientBuilder) WithUnaryInterceptors

func (b *InProcessingClientBuilder) WithUnaryInterceptors(interceptors []grpc.UnaryClientInterceptor)

WithUnaryInterceptors set a list of interceptors to the Grpc client for unary connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the Server side. By using `grpc_middleware` we are able to provides convenient method to add a list of interceptors

Jump to

Keyboard shortcuts

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