connection

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TextMessage denotes a text data message.
	TextMessage = 1

	// BinaryMessage denotes a binary data message.
	BinaryMessage = 2

	// CloseMessage denotes a close control message.
	CloseMessage = 8

	// PingMessage denotes a ping control message.
	PingMessage = 9

	// PongMessage denotes a pong control message.
	PongMessage = 10
)

Message types, same as websocket.

View Source
const (
	// Success is used for replying when the previous request was processed successfully,
	// without any particular result being sent back. When results are involved, positive
	// codes should be defined by implementations, depending on their use case.
	Success = 0
	// InvalidMessage is received when the last request contained a malformed message,
	// from the point of view of the service implementation.
	InvalidMessage = -1
	// KeyCollision is used when a key already exists and cannot be acted upon.
	KeyCollision = -2
	// NotFound is used when the specified resource is non-existent.
	NotFound = -3
	// Unauthorized is used when requested access to the specified resource needs authorization.
	Unauthorized = -4
	// NotAllowed is used when requested access to the specified resource is not allowed.
	NotAllowed = -5
	// Timeout is used when a request times out.
	Timeout = -6
	// InternalError is used when something goes wrong in the implementation.
	InternalError = -7
)

Full duplex protocol message codes.

Variables

View Source
var ErrorStop = fmt.Errorf("listener: stop connection")

ErrorStop is the error a listener should return when processing a message, if the connection should be subsequently closed.

Functions

func FromTextMessage

func FromTextMessage(m *Message, v interface{}) error

FromTextMessage creates an instance of a JSON-annotated type from a Message of type Text.

Types

type Channel

type Channel interface {
	Read() (*Message, error)
	Write(*Message) error
	Close()
}

Channel is the interface for a full duplex channel.

func NewChannelMock

func NewChannelMock(self, other *ChannelMockEndpoint) Channel

NewChannelMock creates a ChannelMock instance.

type ChannelListener

type ChannelListener interface {
	// ProcessMessage should process the given message and react accordingly,
	// by changing state and/or writing something back or none of them.
	ProcessMessage(*Message, WriteOnChannelFunc)
}

ChannelListener is the interface for types that can process incoming messages from a full duplex channel. The listener can react to messages with a reply or by closing the channel.

type ChannelListenerCreator

type ChannelListenerCreator func(t *testing.T) (ChannelListener, error)

ChannelListenerCreator creates channel listeners for the service side, for testing.

type ChannelListenerMock

type ChannelListenerMock struct {
	mock.Mock

	Msg *Message
	// contains filtered or unexported fields
}

ChannelListenerMock is a mock for the ChannelListener interface.

func NewChannelListenerMock

func NewChannelListenerMock() *ChannelListenerMock

NewChannelListenerMock creates a ChannelListenerMock instance.

func (*ChannelListenerMock) ExpectMessageArrival

func (c *ChannelListenerMock) ExpectMessageArrival()

ExpectMessageArrival implements the method with the same name from ChannelListener.

func (*ChannelListenerMock) ProcessMessage

func (c *ChannelListenerMock) ProcessMessage(msg *Message, write WriteOnChannelFunc)

ProcessMessage implements the method with the same name from ChannelListener.

func (*ChannelListenerMock) WaitAndAssertMessageArrived

func (c *ChannelListenerMock) WaitAndAssertMessageArrived(t *testing.T)

WaitAndAssertMessageArrived implements the method with the same name from ChannelListener.

type ChannelMock

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

ChannelMock is a mock for the Channel interface.

func (*ChannelMock) Close

func (c *ChannelMock) Close()

Close implements the method with the same name from Channel.

func (*ChannelMock) Read

func (c *ChannelMock) Read() (*Message, error)

Read implements the method with the same name from Channel.

func (*ChannelMock) Write

func (c *ChannelMock) Write(m *Message) error

Write implements the method with the same name from Channel.

type ChannelMockEndpoint

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

ChannelMockEndpoint is the type for a mock channel's endpoint.

func NewChannelMockEndpoint

func NewChannelMockEndpoint() *ChannelMockEndpoint

NewChannelMockEndpoint creates a channel endpoint instance.

func (*ChannelMockEndpoint) Close

func (c *ChannelMockEndpoint) Close()

Close closes the endpoint.

func (*ChannelMockEndpoint) ReadMessage

func (c *ChannelMockEndpoint) ReadMessage() (*Message, error)

ReadMessage reads a message from the mock channel.

func (*ChannelMockEndpoint) WriteMessage

func (c *ChannelMockEndpoint) WriteMessage(m *Message) error

WriteMessage writes a message to the mock channel.

type EndpointMockInstance

type EndpointMockInstance struct {
	Channel  Channel
	Listener *ChannelListenerMock
	Conn     *FullDuplex
}

EndpointMockInstance is useful for mocking full-duplex endpoints that can talk to the service to be tested.

func SpawnClientInstances

func SpawnClientInstances(t *testing.T, clientCount int, listenerCreator ChannelListenerCreator, clientName, serviceName string) ([]*EndpointMockInstance, *sync.WaitGroup)

SpawnClientInstances spawns instances of clients of the service to be tested.

type FullDuplex

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

FullDuplex is a full-duplex connection that takes a channel listener and a channel. It handles messages arriving on the channel through the listener and also handles sending messages and closing the communication.

func NewFullDuplex

func NewFullDuplex(listener ChannelListener, channel Channel, name string) *FullDuplex

NewFullDuplex creates a new, inactive full-duplex connection. Call Run to run it.

func NewWebSocketClient

func NewWebSocketClient(url url.URL, listener ChannelListener) (*FullDuplex, error)

NewWebSocketClient creates a new websocket connection and a full-duplex connection on top of it.

func NewWebSocketServer

func NewWebSocketServer(w http.ResponseWriter, r *http.Request, listener ChannelListener) (*FullDuplex, error)

NewWebSocketServer does the same as NewWebSocketClient, but from a server point of view.

func (*FullDuplex) Close

func (f *FullDuplex) Close() error

Close stops a full-duplex connection.

func (*FullDuplex) IsClosed

func (f *FullDuplex) IsClosed() bool

IsClosed returns the closed status of a full-duplex connection.

func (*FullDuplex) IsRunning

func (f *FullDuplex) IsRunning() bool

IsRunning returns the running status of a full-duplex connection.

func (*FullDuplex) Run

func (f *FullDuplex) Run() error

Run is a blocking function that handles messages arriving on the channel.

func (*FullDuplex) SendMessage

func (f *FullDuplex) SendMessage(m *Message)

SendMessage sends a message on the full duplex channel.

type FullDuplexManager

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

FullDuplexManager keeps track of a list of existing full-duplex connections.

func NewFullDuplexManager

func NewFullDuplexManager() *FullDuplexManager

NewFullDuplexManager creates a full-duplex connection list manager.

func (*FullDuplexManager) AddConnection

func (m *FullDuplexManager) AddConnection(conn *FullDuplex)

AddConnection appends a connection to the list of managed connections and runs it in a parallel goroutine.

func (*FullDuplexManager) CloseConnections

func (m *FullDuplexManager) CloseConnections()

CloseConnections closes all managed connections.

type Message

type Message struct {
	Type    int
	Payload []byte
}

Message is a structure encompassing a message's type and payload.

func ToTextMessage

func ToTextMessage(v interface{}) *Message

ToTextMessage converts a JSON-annotated struct to a Message of type Text. It returns a nil value if the marshaling does not succeed.

type WriteOnChannelFunc

type WriteOnChannelFunc func(*Message)

WriteOnChannelFunc can be called to write back on the channel from inside the processing message method.

Jump to

Keyboard shortcuts

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