mock

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: BSD-3-Clause Imports: 21 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"
	RetrieveTopicRPC = "/ensign.v1beta1.Ensign/RetrieveTopic"
	DeleteTopicRPC   = "/ensign.v1beta1.Ensign/DeleteTopic"
	TopicNamesRPC    = "/ensign.v1beta1.Ensign/TopicNames"
	TopicExistsRPC   = "/ensign/v1beta1.Ensign/TopicExists"
	InfoRPC          = "/ensign/v1beta1.Ensign/Info"
	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.

View Source
const (
	StreamSend       = "Send"
	StreamRecv       = "Recv"
	StreamSetHeader  = "SetHeader"
	StreamSendHeader = "SendHeader"
	StreamSetTrailer = "SetTrailer"
	StreamContext    = "Context"
	StreamSendMsg    = "SendMsg"
	StreamRecvMsg    = "RecvMsg"
)

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)
	OnRetrieveTopic func(context.Context, *api.Topic) (*api.Topic, error)
	OnDeleteTopic   func(context.Context, *api.TopicMod) (*api.TopicStatus, error)
	OnTopicNames    func(context.Context, *api.PageInfo) (*api.TopicNamesPage, error)
	OnTopicExists   func(context.Context, *api.TopicName) (*api.TopicExistsInfo, error)
	OnInfo          func(context.Context, *api.InfoRequest) (*api.ProjectInfo, 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.TopicStatus, 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) Info added in v0.7.0

func (s *Ensign) Info(ctx context.Context, in *api.InfoRequest) (*api.ProjectInfo, 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) RetrieveTopic added in v0.7.0

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

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) TopicExists added in v0.7.0

func (s *Ensign) TopicExists(ctx context.Context, in *api.TopicName) (*api.TopicExistsInfo, error)

func (*Ensign) TopicNames added in v0.7.0

func (s *Ensign) TopicNames(ctx context.Context, in *api.PageInfo) (*api.TopicNamesPage, 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.

type EventResult added in v0.9.0

type EventResult struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*EventResult) Ack added in v0.9.0

func (r *EventResult) Ack(event *api.EventWrapper) *api.Ack

func (*EventResult) Nack added in v0.9.0

func (r *EventResult) Nack(event *api.EventWrapper) *api.Nack

func (*EventResult) Ready added in v0.9.0

func (r *EventResult) Ready() *api.StreamReady

type PublisherServer added in v0.9.0

type PublisherServer struct {
	ServerStream

	OnSend func(*api.PublisherReply) error
	OnRecv func() (*api.PublisherRequest, error)
}

Implements api.Ensign_PublishServer for testing the Publish streaming RPC.

func (*PublisherServer) Capture added in v0.9.0

func (s *PublisherServer) Capture(replies chan<- *api.PublisherReply)

Capture returns any replies sent by the server on the specified channel.

func (*PublisherServer) Recv added in v0.9.0

func (s *PublisherServer) Recv() (*api.PublisherRequest, error)

func (*PublisherServer) Reset added in v0.9.0

func (s *PublisherServer) Reset()

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

func (*PublisherServer) Send added in v0.9.0

func (*PublisherServer) WithError added in v0.9.0

func (s *PublisherServer) WithError(call string, err error)

WithError ensures that the next call to the specified method returns an error.

func (*PublisherServer) WithEventResults added in v0.9.0

func (s *PublisherServer) WithEventResults(info *api.OpenStream, events ...*api.EventWrapper) *EventResult

A combination of WithEvents and Capture that gets acks/nacks back from the server. Creates a Recv method that sends the given open stream message, then sends each event before finally sending an io.EOF message to close the stream. Capture events and store acks and nacks to make assertions on.

func (*PublisherServer) WithEvents added in v0.9.0

func (s *PublisherServer) WithEvents(info *api.OpenStream, events ...*api.EventWrapper)

WithEvents creates a Recv method that sends the given open stream message, then sends each event before finally sending an io.EOF message to close the stream.

type ServerStream added in v0.9.0

type ServerStream struct {
	sync.RWMutex

	OnSetHeader  func(metadata.MD) error
	OnSendHeader func(metadata.MD) error
	OnSetTrailer func(metadata.MD)
	OnContext    func() context.Context
	OnSendMsg    func(interface{}) error
	OnRecvMsg    func(interface{}) error
	// contains filtered or unexported fields
}

Implements the grpc.ServerStream interface for testing streaming RPCs.

func (*ServerStream) Calls added in v0.9.0

func (s *ServerStream) Calls(call string) int

func (*ServerStream) Context added in v0.9.0

func (s *ServerStream) Context() context.Context

func (*ServerStream) RecvMsg added in v0.9.0

func (s *ServerStream) RecvMsg(m interface{}) error

func (*ServerStream) Reset added in v0.9.0

func (s *ServerStream) Reset()

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

func (*ServerStream) SendHeader added in v0.9.0

func (s *ServerStream) SendHeader(m metadata.MD) error

func (*ServerStream) SendMsg added in v0.9.0

func (s *ServerStream) SendMsg(m interface{}) error

func (*ServerStream) SetHeader added in v0.9.0

func (s *ServerStream) SetHeader(m metadata.MD) error

func (*ServerStream) SetTrailer added in v0.9.0

func (s *ServerStream) SetTrailer(m metadata.MD)

func (*ServerStream) WithClaims added in v0.9.0

func (s *ServerStream) WithClaims(claims *tokens.Claims)

WithClaims creates a context with the specified claims on it.

func (*ServerStream) WithContext added in v0.9.0

func (s *ServerStream) WithContext(ctx context.Context)

WithContext ensures the server stream returns the specified context.

func (*ServerStream) WithError added in v0.9.0

func (s *ServerStream) WithError(call string, err error)

WithError ensures that the next call to the specified method returns an error.

func (*ServerStream) WithPeer added in v0.9.0

func (s *ServerStream) WithPeer(claims *tokens.Claims, remote *peer.Peer)

WithPeer sets the peer on the server context in addition to the claims.

type SubscribeServer added in v0.9.0

type SubscribeServer struct {
	ServerStream

	OnSend func(*api.SubscribeReply) error
	OnRecv func() (*api.SubscribeRequest, error)
}

Implements api.Ensign_SubscribeServer for testing the Subscribe streaming RPC.

func (*SubscribeServer) Recv added in v0.9.0

func (s *SubscribeServer) Recv() (*api.SubscribeRequest, error)

func (*SubscribeServer) Reset added in v0.9.0

func (s *SubscribeServer) Reset()

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

func (*SubscribeServer) Send added in v0.9.0

func (*SubscribeServer) WithError added in v0.9.0

func (s *SubscribeServer) WithError(call string, err error)

WithError ensures that the next call to the specified method returns an error.

func (*SubscribeServer) WithSubscription added in v0.9.0

func (s *SubscribeServer) WithSubscription(subscription *api.Subscription) *Subscription

WithSubscription creates an object that allows test code to receive events and send acks and nacks on the specified subscription channel.

type Subscription added in v0.9.0

type Subscription struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*Subscription) Ack added in v0.9.0

func (s *Subscription) Ack(id []byte)

func (*Subscription) Close added in v0.9.0

func (s *Subscription) Close()

func (*Subscription) Nack added in v0.9.0

func (s *Subscription) Nack(id []byte, code api.Nack_Code, msg string)

func (*Subscription) Next added in v0.9.0

func (s *Subscription) Next() *api.EventWrapper

func (*Subscription) Ready added in v0.9.0

func (s *Subscription) Ready() *api.StreamReady

Jump to

Keyboard shortcuts

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