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
- func PerRPCToken(token string) grpc.CallOption
- func WithPerRPCToken(token string) grpc.DialOption
- type Credentials
- type Ensign
- func (s *Ensign) Channel() *bufconn.Listener
- func (s *Ensign) Client(ctx context.Context, opts ...grpc.DialOption) (client api.EnsignClient, err error)
- func (s *Ensign) CreateTopic(ctx context.Context, in *api.Topic) (*api.Topic, error)
- func (s *Ensign) DeleteTopic(ctx context.Context, in *api.TopicMod) (*api.TopicTombstone, error)
- func (s *Ensign) HealthClient(ctx context.Context, opts ...grpc.DialOption) (client health.HealthClient, err error)
- func (s *Ensign) ListTopics(ctx context.Context, in *api.PageInfo) (*api.TopicsPage, error)
- func (s *Ensign) Publish(stream api.Ensign_PublishServer) error
- func (s *Ensign) Reset()
- func (s *Ensign) ResetClient(ctx context.Context, opts ...grpc.DialOption) (api.EnsignClient, error)
- func (s *Ensign) Shutdown()
- func (s *Ensign) Status(ctx context.Context, in *api.HealthCheck) (*api.ServiceState, error)
- func (s *Ensign) Subscribe(stream api.Ensign_SubscribeServer) error
- func (s *Ensign) UseError(rpc string, code codes.Code, msg string) error
- func (s *Ensign) UseFixture(rpc, path string) (err error)
Constants ¶
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 (*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) 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 (*Ensign) DeleteTopic ¶
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 (*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) UseError ¶
UseError allows you to specify a gRPC status error to return from the specified RPC.
func (*Ensign) UseFixture ¶
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.