messaging

package
v0.0.0-...-197aa8e Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 15 Imported by: 16

Documentation

Index

Constants

View Source
const (
	//PingCommandContentType is the content type for a ping command
	PingCommandContentType = "application/vnd-ping-command"
	//PongResponseContentType is the content type for a pong response
	PongResponseContentType = "application/vnd-pong-response"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command interface {
	Message
}

func NewPingCommand

func NewPingCommand() Command

NewPingCommand instantiates a new ping command

type CommandHandler

type CommandHandler func(context.Context, IncomingCommand, *slog.Logger) error

CommandHandler is a callback type to be used for dispatching incoming commands

func NewPingCommandHandler

func NewPingCommandHandler(ctx MsgContext) CommandHandler

NewPingCommandHandler returns a callback function to be called when ping commands are received

type Config

type Config struct {
	ServiceName string
	Host        string
	Port        uint64
	VirtualHost string
	User        string
	Password    string
	// contains filtered or unexported fields
}

Config is an encapsulating context that wraps configuration information used by the initialization methods of the messaging library

func LoadConfiguration

func LoadConfiguration(ctx context.Context, serviceName string, log *slog.Logger) Config

LoadConfiguration loads configuration values from RABBITMQ_HOST, RABBITMQ_USER and RABBITMQ_PASS. The username and password defaults to the bitnami ootb values for local testing.

type IncomingCommand

type IncomingCommand interface {
	Message
	Context() context.Context
	RespondWith(context.Context, Response) error
}

Command is an interface used when handling commands

type IncomingTopicMessage

type IncomingTopicMessage interface {
	TopicMessage
}

type IncomingTopicMessageMock

type IncomingTopicMessageMock struct {
	// BodyFunc mocks the Body method.
	BodyFunc func() []byte

	// ContentTypeFunc mocks the ContentType method.
	ContentTypeFunc func() string

	// TopicNameFunc mocks the TopicName method.
	TopicNameFunc func() string
	// contains filtered or unexported fields
}

IncomingTopicMessageMock is a mock implementation of IncomingTopicMessage.

func TestSomethingThatUsesIncomingTopicMessage(t *testing.T) {

	// make and configure a mocked IncomingTopicMessage
	mockedIncomingTopicMessage := &IncomingTopicMessageMock{
		BodyFunc: func() []byte {
			panic("mock out the Body method")
		},
		ContentTypeFunc: func() string {
			panic("mock out the ContentType method")
		},
		TopicNameFunc: func() string {
			panic("mock out the TopicName method")
		},
	}

	// use mockedIncomingTopicMessage in code that requires IncomingTopicMessage
	// and then make assertions.

}

func (*IncomingTopicMessageMock) Body

func (mock *IncomingTopicMessageMock) Body() []byte

Body calls BodyFunc.

func (*IncomingTopicMessageMock) BodyCalls

func (mock *IncomingTopicMessageMock) BodyCalls() []struct {
}

BodyCalls gets all the calls that were made to Body. Check the length with:

len(mockedIncomingTopicMessage.BodyCalls())

func (*IncomingTopicMessageMock) ContentType

func (mock *IncomingTopicMessageMock) ContentType() string

ContentType calls ContentTypeFunc.

func (*IncomingTopicMessageMock) ContentTypeCalls

func (mock *IncomingTopicMessageMock) ContentTypeCalls() []struct {
}

ContentTypeCalls gets all the calls that were made to ContentType. Check the length with:

len(mockedIncomingTopicMessage.ContentTypeCalls())

func (*IncomingTopicMessageMock) TopicName

func (mock *IncomingTopicMessageMock) TopicName() string

TopicName calls TopicNameFunc.

func (*IncomingTopicMessageMock) TopicNameCalls

func (mock *IncomingTopicMessageMock) TopicNameCalls() []struct {
}

TopicNameCalls gets all the calls that were made to TopicName. Check the length with:

len(mockedIncomingTopicMessage.TopicNameCalls())

type Message

type Message interface {
	Body() []byte
	ContentType() string
}

func NewMessageJSON

func NewMessageJSON(contentType string, body any) (Message, error)

type MessageFilter

type MessageFilter func(Message) bool

MessageFilter allows a subscriber to supply a filter function that decides if a received message should be delivered to the handler or not

func MatchContentType

func MatchContentType(contentType string) MessageFilter

MatchContentType returns a topic message filter that returns true for all messages where the content type matches the supplied content type case insensitive

type MsgContext

type MsgContext interface {
	NoteToSelf(ctx context.Context, command Command) error
	SendCommandTo(ctx context.Context, command Command, key string) error
	SendResponseTo(ctx context.Context, response Response, key string) error
	PublishOnTopic(ctx context.Context, message TopicMessage) error

	Start()
	Close()

	RegisterCommandHandler(filter MessageFilter, handler CommandHandler) error
	RegisterTopicMessageHandler(routingKey string, handler TopicMessageHandler) error
	RegisterTopicMessageHandlerWithFilter(routingKey string, handler TopicMessageHandler, filter MessageFilter) error
}

MsgContext encapsulates the underlying messaging primitives, as well as their associated configuration

func Initialize

func Initialize(ctx context.Context, cfg Config) (MsgContext, error)

Initialize takes a Config parameter and initializes a connection, channel, topic exchange, command exchange and service specific command and response queues. Retries every 2 seconds until successfull.

type MsgContextMock

type MsgContextMock struct {
	// CloseFunc mocks the Close method.
	CloseFunc func()

	// NoteToSelfFunc mocks the NoteToSelf method.
	NoteToSelfFunc func(ctx context.Context, command Command) error

	// PublishOnTopicFunc mocks the PublishOnTopic method.
	PublishOnTopicFunc func(ctx context.Context, message TopicMessage) error

	// RegisterCommandHandlerFunc mocks the RegisterCommandHandler method.
	RegisterCommandHandlerFunc func(filter MessageFilter, handler CommandHandler) error

	// RegisterTopicMessageHandlerFunc mocks the RegisterTopicMessageHandler method.
	RegisterTopicMessageHandlerFunc func(routingKey string, handler TopicMessageHandler) error

	// RegisterTopicMessageHandlerWithFilterFunc mocks the RegisterTopicMessageHandlerWithFilter method.
	RegisterTopicMessageHandlerWithFilterFunc func(routingKey string, handler TopicMessageHandler, filter MessageFilter) error

	// SendCommandToFunc mocks the SendCommandTo method.
	SendCommandToFunc func(ctx context.Context, command Command, key string) error

	// SendResponseToFunc mocks the SendResponseTo method.
	SendResponseToFunc func(ctx context.Context, response Response, key string) error

	// StartFunc mocks the Start method.
	StartFunc func()
	// contains filtered or unexported fields
}

MsgContextMock is a mock implementation of MsgContext.

func TestSomethingThatUsesMsgContext(t *testing.T) {

	// make and configure a mocked MsgContext
	mockedMsgContext := &MsgContextMock{
		CloseFunc: func()  {
			panic("mock out the Close method")
		},
		NoteToSelfFunc: func(ctx context.Context, command Command) error {
			panic("mock out the NoteToSelf method")
		},
		PublishOnTopicFunc: func(ctx context.Context, message TopicMessage) error {
			panic("mock out the PublishOnTopic method")
		},
		RegisterCommandHandlerFunc: func(filter MessageFilter, handler CommandHandler) error {
			panic("mock out the RegisterCommandHandler method")
		},
		RegisterTopicMessageHandlerFunc: func(routingKey string, handler TopicMessageHandler) error {
			panic("mock out the RegisterTopicMessageHandler method")
		},
		RegisterTopicMessageHandlerWithFilterFunc: func(routingKey string, handler TopicMessageHandler, filter MessageFilter) error {
			panic("mock out the RegisterTopicMessageHandlerWithFilter method")
		},
		SendCommandToFunc: func(ctx context.Context, command Command, key string) error {
			panic("mock out the SendCommandTo method")
		},
		SendResponseToFunc: func(ctx context.Context, response Response, key string) error {
			panic("mock out the SendResponseTo method")
		},
		StartFunc: func()  {
			panic("mock out the Start method")
		},
	}

	// use mockedMsgContext in code that requires MsgContext
	// and then make assertions.

}

func (*MsgContextMock) Close

func (mock *MsgContextMock) Close()

Close calls CloseFunc.

func (*MsgContextMock) CloseCalls

func (mock *MsgContextMock) CloseCalls() []struct {
}

CloseCalls gets all the calls that were made to Close. Check the length with:

len(mockedMsgContext.CloseCalls())

func (*MsgContextMock) NoteToSelf

func (mock *MsgContextMock) NoteToSelf(ctx context.Context, command Command) error

NoteToSelf calls NoteToSelfFunc.

func (*MsgContextMock) NoteToSelfCalls

func (mock *MsgContextMock) NoteToSelfCalls() []struct {
	Ctx     context.Context
	Command Command
}

NoteToSelfCalls gets all the calls that were made to NoteToSelf. Check the length with:

len(mockedMsgContext.NoteToSelfCalls())

func (*MsgContextMock) PublishOnTopic

func (mock *MsgContextMock) PublishOnTopic(ctx context.Context, message TopicMessage) error

PublishOnTopic calls PublishOnTopicFunc.

func (*MsgContextMock) PublishOnTopicCalls

func (mock *MsgContextMock) PublishOnTopicCalls() []struct {
	Ctx     context.Context
	Message TopicMessage
}

PublishOnTopicCalls gets all the calls that were made to PublishOnTopic. Check the length with:

len(mockedMsgContext.PublishOnTopicCalls())

func (*MsgContextMock) RegisterCommandHandler

func (mock *MsgContextMock) RegisterCommandHandler(filter MessageFilter, handler CommandHandler) error

RegisterCommandHandler calls RegisterCommandHandlerFunc.

func (*MsgContextMock) RegisterCommandHandlerCalls

func (mock *MsgContextMock) RegisterCommandHandlerCalls() []struct {
	Filter  MessageFilter
	Handler CommandHandler
}

RegisterCommandHandlerCalls gets all the calls that were made to RegisterCommandHandler. Check the length with:

len(mockedMsgContext.RegisterCommandHandlerCalls())

func (*MsgContextMock) RegisterTopicMessageHandler

func (mock *MsgContextMock) RegisterTopicMessageHandler(routingKey string, handler TopicMessageHandler) error

RegisterTopicMessageHandler calls RegisterTopicMessageHandlerFunc.

func (*MsgContextMock) RegisterTopicMessageHandlerCalls

func (mock *MsgContextMock) RegisterTopicMessageHandlerCalls() []struct {
	RoutingKey string
	Handler    TopicMessageHandler
}

RegisterTopicMessageHandlerCalls gets all the calls that were made to RegisterTopicMessageHandler. Check the length with:

len(mockedMsgContext.RegisterTopicMessageHandlerCalls())

func (*MsgContextMock) RegisterTopicMessageHandlerWithFilter

func (mock *MsgContextMock) RegisterTopicMessageHandlerWithFilter(routingKey string, handler TopicMessageHandler, filter MessageFilter) error

RegisterTopicMessageHandlerWithFilter calls RegisterTopicMessageHandlerWithFilterFunc.

func (*MsgContextMock) RegisterTopicMessageHandlerWithFilterCalls

func (mock *MsgContextMock) RegisterTopicMessageHandlerWithFilterCalls() []struct {
	RoutingKey string
	Handler    TopicMessageHandler
	Filter     MessageFilter
}

RegisterTopicMessageHandlerWithFilterCalls gets all the calls that were made to RegisterTopicMessageHandlerWithFilter. Check the length with:

len(mockedMsgContext.RegisterTopicMessageHandlerWithFilterCalls())

func (*MsgContextMock) SendCommandTo

func (mock *MsgContextMock) SendCommandTo(ctx context.Context, command Command, key string) error

SendCommandTo calls SendCommandToFunc.

func (*MsgContextMock) SendCommandToCalls

func (mock *MsgContextMock) SendCommandToCalls() []struct {
	Ctx     context.Context
	Command Command
	Key     string
}

SendCommandToCalls gets all the calls that were made to SendCommandTo. Check the length with:

len(mockedMsgContext.SendCommandToCalls())

func (*MsgContextMock) SendResponseTo

func (mock *MsgContextMock) SendResponseTo(ctx context.Context, response Response, key string) error

SendResponseTo calls SendResponseToFunc.

func (*MsgContextMock) SendResponseToCalls

func (mock *MsgContextMock) SendResponseToCalls() []struct {
	Ctx      context.Context
	Response Response
	Key      string
}

SendResponseToCalls gets all the calls that were made to SendResponseTo. Check the length with:

len(mockedMsgContext.SendResponseToCalls())

func (*MsgContextMock) Start

func (mock *MsgContextMock) Start()

Start calls StartFunc.

func (*MsgContextMock) StartCalls

func (mock *MsgContextMock) StartCalls() []struct {
}

StartCalls gets all the calls that were made to Start. Check the length with:

len(mockedMsgContext.StartCalls())

type PingCommand

type PingCommand struct {
	Cmd       string    `json:"cmd"`
	Timestamp time.Time `json:"timestamp"`
}

PingCommand is a utility command to check the messenger connection

func (PingCommand) ContentType

func (cmd PingCommand) ContentType() string

ContentType returns the content type for a ping command

type PongResponse

type PongResponse struct {
	Cmd       string `json:"cmd"`
	PingSent  time.Time
	Timestamp time.Time `json:"timestamp"`
}

PongResponse is a utility response to check the messenger connection

func (PongResponse) ContentType

func (cmd PongResponse) ContentType() string

ContentType returns the content type for a pong response

type Response

type Response interface {
	Message
}

func NewPongResponse

func NewPongResponse(ping PingCommand) Response

NewPongResponse instantiates a new pong response from a ping command

type TopicMessage

type TopicMessage interface {
	Message
	TopicName() string
}

TopicMessage is an interface used when sending messages to make sure that messages are sent to the correct topic with correct content type

func NewTopicMessageJSON

func NewTopicMessageJSON(topic, contentType string, body any) (TopicMessage, error)

type TopicMessageHandler

type TopicMessageHandler func(context.Context, IncomingTopicMessage, *slog.Logger)

TopicMessageHandler is a callback type that should be passed to RegisterTopicMessageHandler to receive messages from topics.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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