mock

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: BSD-3-Clause Imports: 22 Imported by: 2

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"
	EnSQLRPC          = "/ensign.v1beta1.Ensign/EnSQL"
	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"
	SetTopicPolicyRPC = "/ensign/v1beta1.Ensign/SetTopicPolicy"
	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.

Variables

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

Functions

func NewEvent added in v0.7.0

func NewEvent() *api.Event

NewEvent returns an event with random data

func NewEventWrapper added in v0.7.0

func NewEventWrapper() *api.EventWrapper

NewEventWrapper returns an event wrapper with random data inside. It is a quick method to create an event for the default "testing.123" topic.

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 Ensign

type Ensign struct {
	sync.RWMutex
	api.UnimplementedEnsignServer

	Calls            map[string]int
	OnPublish        func(api.Ensign_PublishServer) error
	OnSubscribe      func(api.Ensign_SubscribeServer) error
	OnEnSQL          func(*api.Query, api.Ensign_EnSQLServer) 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)
	OnSetTopicPolicy func(context.Context, *api.TopicPolicy) (*api.TopicStatus, 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 *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) 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) EnSQL added in v0.9.0

func (s *Ensign) EnSQL(in *api.Query, stream api.Ensign_EnSQLServer) 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

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.6.0

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

func (*Ensign) SetTopicPolicy added in v0.11.0

func (s *Ensign) SetTopicPolicy(ctx context.Context, in *api.TopicPolicy) (*api.TopicStatus, 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.6.0

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

func (*Ensign) TopicNames added in v0.6.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 EventFactory added in v0.7.0

type EventFactory struct {
	sync.Mutex
	Topic  ulid.ULID
	Region region.Region
	// contains filtered or unexported fields
}

EventFactory creates random events with standard defaults.

func (*EventFactory) Event added in v0.7.0

func (f *EventFactory) Event() *api.Event

func (*EventFactory) Make added in v0.7.0

func (f *EventFactory) Make() *api.EventWrapper

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. 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. We prefer to use the bufconn over simply making method calls to the handlers.

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.

type PublishHandler added in v0.7.0

type PublishHandler struct {
	OnInitialize func(in *api.OpenStream) (out *api.StreamReady, err error)
	OnEvent      func(in *api.EventWrapper) (out *api.PublisherReply, err error)
}

PublishHandler provides an OnPublish function that assists in the testing of publish streams by breaking down the initialization and messaging phase of the publisher stream. For example, this handler can be used to ensure that a specific number of events get published or to send acks or nacks to specific events.

func NewPublishHandler added in v0.7.0

func NewPublishHandler(topics map[string]ulid.ULID) *PublishHandler

By default new publish handlers ack all events and return the specified topic map.

func (*PublishHandler) OnPublish added in v0.7.0

func (s *PublishHandler) OnPublish(stream api.Ensign_PublishServer) (err error)

type SubscribeHandler added in v0.7.0

type SubscribeHandler struct {
	OnInitialize func(in *api.Subscription) (out *api.StreamReady, err error)
	OnAck        func(in *api.Ack) (err error)
	OnNack       func(in *api.Nack) (err error)
	Send         chan<- *api.EventWrapper
	// contains filtered or unexported fields
}

SubscribeHandler provides an OnSubscribe function that assists in the testing of subscribe streams by breaking down the initialization and messaging phases of the subscription stream. For example, this handler can be used to send 10 events before quitting or to send events at a routine interval.

func NewSubscribeHandler added in v0.7.0

func NewSubscribeHandler() *SubscribeHandler

func (*SubscribeHandler) OnSubscribe added in v0.7.0

func (s *SubscribeHandler) OnSubscribe(stream api.Ensign_SubscribeServer) (err error)

This method should be added to the mock as the OnSubscribe handler.

func (*SubscribeHandler) Shutdown added in v0.7.0

func (s *SubscribeHandler) Shutdown()

Shutdown the stream by sending a close stream message

func (*SubscribeHandler) UseTopicMap added in v0.7.0

func (s *SubscribeHandler) UseTopicMap(topics map[string]ulid.ULID)

UseTopicMap sets OnInitialize to use the topics in the topic map, returning an error if the subscription contains topics that are not in the topics map.

Jump to

Keyboard shortcuts

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