gsrv

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2025 License: Apache-2.0 Imports: 8 Imported by: 3

README

go-gsrv

Build Status Codevov Go Report Card GoDoc

Package gsrv creates a test gRPC server that is useful when mocking a gRPC service.

A complete example of mocking a gRPC service that leverges this package is available.

This package is based on Google's testutil.Server. Original code: https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/internal/testutil/server.go

Documentation

Overview

Package gsrv creates a test gRPC server that is useful when mocking a gRPC service.

A complete example of mocking a gRPC service that leverges this package is available at https://github.com/weathersource/go-gsrv/tree/master/examples/foo

This package is based on Google's testutil.Server. Original code is found at https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/internal/testutil/server.go

Index

Constants

View Source
const (
	// Initialized Server status means the server has been created, but not started.
	Initialized status = iota
	// Running Server status means the server has been started.
	Running
	// Closed Server status means the server has been closed
	Closed
	// ClosedWithError Server status means the server was closed because of an error.
	ClosedWithError
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	Addr string
	Port int

	Gsrv *grpc.Server
	// contains filtered or unexported fields
}

Server is an in-process gRPC server, listening on a system-chosen port on the local loopback interface. Servers are for testing only and are not intended to be used in production code.

To create a server, make a new Server, register your handlers, then call Start:

srv, err := NewServer()
...
mypb.RegisterMyServiceServer(srv.Gsrv, &myHandler)
....
srv.Start()

Clients should connect to the server with no security:

conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
...

func NewServer

func NewServer(opts ...grpc.ServerOption) (*Server, error)

NewServer creates a new Server. The server will be listening for gRPC connections at the address named by the Addr field, without TLS.

func NewServerWithPort

func NewServerWithPort(port int, opts ...grpc.ServerOption) (*Server, error)

NewServerWithPort creates a new Server at a specific port. The server will be listening for gRPC connections at the address named by the Addr field, without TLS.

func (*Server) Close

func (s *Server) Close()

Close shuts down the server.

func (*Server) Start

func (s *Server) Start()

Start causes the server to start accepting incoming connections. Call Start after registering handlers.

func (*Server) Status

func (s *Server) Status() status

Status returns the server status: Initialized, Running, Closed, or ClosedWithError

Directories

Path Synopsis
examples
foo command
foo/mockfoo
Package mockfoo mocks a server for the Foo service.
Package mockfoo mocks a server for the Foo service.

Jump to

Keyboard shortcuts

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