mock

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package mock implements an in-memory gRPC mock Ensign server that can be connected to using a bufconn. The mock is useful for testing client side code for publishers and subscribers without actually connecting to an Ensign server.

Index

Constants

View Source
const (
	PublishRPC     = "/ensign.v1beta1.Ensign/Publish"
	SubscribeRPC   = "/ensign.v1beta1.Ensign/Subscribe"
	ListTopicsRPC  = "/ensign.v1beta1.Ensign/ListTopics"
	CreateTopicRPC = "/ensign.v1beta1.Ensign/CreateTopic"
	DeleteTopicRPC = "/ensign.v1beta1.Ensign/DeleteTopic"
	StatusRPC      = "/ensign.v1beta1.Ensign/Status"
)

RPC Name constants based on the FullMethod that is returned from gRPC info. These constants can be used to reference RPCs in the mock code.

Variables

This section is empty.

Functions

func PerRPCToken added in v0.5.0

func PerRPCToken(token string) grpc.CallOption

func WithPerRPCToken added in v0.5.0

func WithPerRPCToken(token string) grpc.DialOption

Types

type Credentials added in v0.5.0

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

func (*Credentials) GetRequestMetadata added in v0.5.0

func (t *Credentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)

func (*Credentials) RequireTransportSecurity added in v0.5.0

func (t *Credentials) RequireTransportSecurity() bool

type Ensign

type Ensign struct {
	health.ProbeServer
	api.UnimplementedEnsignServer

	Calls         map[string]int
	OnPublish     func(api.Ensign_PublishServer) error
	OnSubscribe   func(api.Ensign_SubscribeServer) error
	OnListTopics  func(context.Context, *api.PageInfo) (*api.TopicsPage, error)
	OnCreateTopic func(context.Context, *api.Topic) (*api.Topic, error)
	OnDeleteTopic func(context.Context, *api.TopicMod) (*api.TopicTombstone, error)
	OnStatus      func(context.Context, *api.HealthCheck) (*api.ServiceState, error)
	// contains filtered or unexported fields
}

Implements a mock gRPC server for testing Ensign client connections. The desired response of the Ensign server can be set by test users using the OnRPC functions or the WithFixture or WithError methods. The Calls map can be used to count the number of times a specific RPC was called.

func New

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

New creates a mock Ensign server for testing Ensign responses to RPC calls. If the bufnet is nil, the default bufconn is created for use in testing. Arbitrary server options (e.g. for authentication or to add interceptors) can be passed in as well.

func (*Ensign) Channel

func (s *Ensign) Channel() *bufconn.Listener

Return the bufconn channel (helpful for dialing)

func (*Ensign) Client

func (s *Ensign) Client(ctx context.Context, opts ...grpc.DialOption) (client api.EnsignClient, err error)

Create and connect an Ensign client to the mock server

func (*Ensign) CreateTopic

func (s *Ensign) CreateTopic(ctx context.Context, in *api.Topic) (*api.Topic, error)

func (*Ensign) DeleteTopic

func (s *Ensign) DeleteTopic(ctx context.Context, in *api.TopicMod) (*api.TopicTombstone, error)

func (*Ensign) HealthClient added in v0.5.0

func (s *Ensign) HealthClient(ctx context.Context, opts ...grpc.DialOption) (client health.HealthClient, err error)

func (*Ensign) ListTopics

func (s *Ensign) ListTopics(ctx context.Context, in *api.PageInfo) (*api.TopicsPage, error)

func (*Ensign) Publish

func (s *Ensign) Publish(stream api.Ensign_PublishServer) error

func (*Ensign) Reset

func (s *Ensign) Reset()

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

func (*Ensign) ResetClient added in v0.3.0

func (s *Ensign) ResetClient(ctx context.Context, opts ...grpc.DialOption) (api.EnsignClient, error)

Reset the client with the new dial options

func (*Ensign) Shutdown

func (s *Ensign) Shutdown()

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

func (*Ensign) Status

func (s *Ensign) Status(ctx context.Context, in *api.HealthCheck) (*api.ServiceState, error)

func (*Ensign) Subscribe

func (s *Ensign) Subscribe(stream api.Ensign_SubscribeServer) error

func (*Ensign) UseError

func (s *Ensign) UseError(rpc string, code codes.Code, msg string) error

UseError allows you to specify a gRPC status error to return from the specified RPC.

func (*Ensign) UseFixture

func (s *Ensign) UseFixture(rpc, path string) (err error)

UseFixture loads a JSON fixture from disk (usually in the testdata folder) to use as the protocol buffer response to the specified RPC, simplifying handler mocking.

Jump to

Keyboard shortcuts

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