talk

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

Variables

View Source
var WorkerPool *worker.Pool

WorkerPool is a worker pool, which if initialised, is used instead of launching new goroutines every time when needed.

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.

func NewChatSetup

func NewChatSetup(recv1, recv2 ReceiveFun) ([]*FakeConnection, []*Agent, func())

NewChatSetup creates the whole setup required for a chat between two talking agents.

Types

type Agent

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

Agent is the class for talking agents. A talking agent both accepts and sends messages.

func NewAgent

func NewAgent(c Connection, r ReceiveFun) *Agent

NewAgent creates a new talking agent. The agent will take care of closing the connection when stopped.

func (*Agent) IsRunning

func (a *Agent) IsRunning() bool

IsRunning returns true if the agent is still running. An stopped agent is not usable anymore.

func (*Agent) Listen

func (a *Agent) Listen()

Listen blocks the current goroutine and makes the agent listen for incoming messages until Stop() is called.

func (*Agent) Say

func (a *Agent) Say(m *Message)

Say sends a message to the peer agent.

func (*Agent) Stop

func (a *Agent) Stop()

Stop halts listening and closes the connection.

type AgentFuncsCreator

type AgentFuncsCreator func(t *testing.T) (ReceiveFun, func(), error)

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

type Chat

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

Chat is a helper struct managing the communication between two talking agents.

func NewChat

func NewChat(agent1, agent2 *Agent) *Chat

NewChat creates a new chat between two talking agents.

func (*Chat) Wait

func (c *Chat) Wait()

Wait waits for the chat to be over.

type ChatData

type ChatData struct {
	Conns       []*FakeConnection
	Agents      []*Agent
	ClientInbox *Message
	// contains filtered or unexported fields
}

ChatData holds information about a chat.

func (*ChatData) ExpectClientMessage

func (c *ChatData) ExpectClientMessage()

ExpectClientMessage sets the expectation for receiving a message on client-side.

func (*ChatData) ExpectServiceMessage

func (c *ChatData) ExpectServiceMessage()

ExpectServiceMessage sets the expectation for receiving a message on servce-side.

func (*ChatData) WaitForClientMessages

func (c *ChatData) WaitForClientMessages()

WaitForClientMessages waits for all the message expectations on client-side to be fulfilled.

func (*ChatData) WaitForServiceMessages

func (c *ChatData) WaitForServiceMessages()

WaitForServiceMessages waits for all the message expectations on service-side to be fulfilled.

type Connection

type Connection interface {
	Subscribe(func(*Message, bool))
	Write(*Message) error
	Close()
}

Connection is the interface for an endpoint.

type FakeChannel

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

FakeChannel represents a theoretical communication channel. between two parties. It is useful for mocking real communication channels. In a FakeChannel, there are two Message channels. One party reads from the first channel and writes to the second channel. The other party does the opposite.

func NewFakeChannel

func NewFakeChannel() *FakeChannel

NewFakeChannel creates a new fake channel instance.

type FakeConnection

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

FakeConnection represents a theoretical communication endpoint. It is useful for mocking real communication endpoints.

func NewFakeConnection

func NewFakeConnection(channel *FakeChannel, index int) *FakeConnection

NewFakeConnection returns a new fake connection instance.

func (*FakeConnection) Close

func (c *FakeConnection) Close()

Close closes the fake connection by writing a close message on the fake channel.

func (*FakeConnection) ExpectMessages

func (c *FakeConnection) ExpectMessages(count int)

ExpectMessages expectats for 'count' messages.

func (*FakeConnection) Subscribe

func (c *FakeConnection) Subscribe(cb func(*Message, bool))

Subscribe subscribes the given callback to the connection's message arrival event loop.

func (*FakeConnection) WaitForMessages

func (c *FakeConnection) WaitForMessages()

WaitForMessages waits for the expected messages.

func (*FakeConnection) Write

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

Write writes a message on the fake channel.

type Message

type Message struct {
	Type    int
	Payload []byte
}

Message is the unit of transmission.

func SimpleMessage

func SimpleMessage(text string) *Message

SimpleMessage creates a simple text message.

func ToTextMessage

func ToTextMessage(v interface{}) (*Message, error)

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

type Messenger

type Messenger struct {
	Chats []*ChatData
}

Messenger is an abstraction for multiple client - single service communications, for the purpose of testing the service.

func NewMessenger

func NewMessenger(t *testing.T, n int, agentFuncsCreator AgentFuncsCreator, clientName, serviceName string) *Messenger

NewMessenger spawns n given communication channels between a service and n clients.

func (*Messenger) Stop

func (m *Messenger) Stop()

Stop closes all chats in a messenger and releases the service.

type ReceiveFun

type ReceiveFun func(*Message, SendFun)

ReceiveFun is the signature of the function that handles a received message.

type SendFun

type SendFun func(*Message)

SendFun is the signature of the function that sends a message.

Jump to

Keyboard shortcuts

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