stub

package
v0.37.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: AGPL-3.0 Imports: 16 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithConduitFactory added in v0.26.0

func WithConduitFactory(factory network.ConduitFactory) func(*Network)

Types

type Buffer

type Buffer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Buffer buffers all the pending messages to be sent over the mock network from one node to a list of nodes

func NewBuffer

func NewBuffer() *Buffer

NewBuffer initialize the Buffer

func (*Buffer) Deliver

func (b *Buffer) Deliver(sendOne func(*PendingMessage) bool)

Deliver delivers all pending messages currently in the buffer using the provided sendOne method. If sendOne returns false, the message was not sent and will remain in the buffer.

func (*Buffer) DeliverRecursive

func (b *Buffer) DeliverRecursive(sendOne func(*PendingMessage))

DeliverRecursive recursively delivers all pending messages using the provided sendOne method until the buffer is empty. If sendOne does not deliver the message, it is permanently dropped.

func (*Buffer) Save

func (b *Buffer) Save(m *PendingMessage)

Save stores a pending message to the buffer

type Hub

type Hub struct {
	sync.RWMutex

	Buffer *Buffer
	// contains filtered or unexported fields
}

Hub is a test helper that mocks a network overlay. It maintains a set of network instances and enables them to directly exchange message over the memory.

func NewNetworkHub

func NewNetworkHub() *Hub

NewNetworkHub creates and returns a new Hub instance.

func (*Hub) AddNetwork

func (h *Hub) AddNetwork(net *Network)

AddNetwork stores the reference of the Network in the Hub, in order for networks to find other networks to send events directly.

func (*Hub) DeliverAll

func (h *Hub) DeliverAll()

DeliverAll delivers all the buffered messages in the Network instances attached to the Hub to their destination. Note that the delivery of messages is done in asynchronous mode, i.e., sender and receiver are synchronized over delivery and not execution of the message.

func (*Hub) DeliverAllEventually

func (h *Hub) DeliverAllEventually(t *testing.T, condition func() bool)

DeliverAllEventually attempts on delivery of all the buffered messages in the Network instances attached to this instance of Hub. Once the delivery is done, it evaluates and returns the condition function. It fails if delivery of all buffered messages in the Network instances attached to this Hub is not getting done within 10 seconds. Note that the delivery of messages is done in asynchronous mode, i.e., sender and receiver are synchronized over delivery and not execution of the message.

func (*Hub) DeliverAllEventuallyUntil

func (h *Hub) DeliverAllEventuallyUntil(t *testing.T, condition func() bool, waitFor time.Duration, tick time.Duration)

DeliverAllEventuallyUntil attempts attempts on delivery of all the buffered messages in the Network instances attached to this instance of Hub. Once the delivery is done, it evaluates and returns the condition function. It fails if delivery of all buffered messages in the Network instances attached to this Hub is not getting done within `waitFor` time interval. It checks the status of message deliveries at every `tick` time interval. Note that the delivery of messages is done in asynchronous mode, i.e., sender and receiver are synchronized over delivery and not execution of the message.

func (*Hub) GetNetwork

func (h *Hub) GetNetwork(nodeID flow.Identifier) (*Network, bool)

GetNetwork returns the Network instance attached to the node ID.

type Network

type Network struct {
	mocknetwork.Network

	sync.Mutex
	// contains filtered or unexported fields
}

Network is a mocked Network layer made for testing engine's behavior. It represents the Network layer of a single node. A node can attach several engines of itself to the Network, and hence enabling them send and receive message. When an engine is attached on a Network instance, the mocked Network delivers all engine's events to others using an in-memory delivery mechanism.

func NewNetwork

func NewNetwork(t testing.TB, myId flow.Identifier, hub *Hub, opts ...func(*Network)) *Network

NewNetwork create a mocked Network. The committee has the identity of the node already, so only `committee` is needed in order for a mock hub to find each other.

func (*Network) DeliverAll

func (n *Network) DeliverAll(syncOnProcess bool)

DeliverAll sends all pending messages to the receivers. The receivers might be triggered to forward messages to its peers, so this function will block until all receivers have done their forwarding, and there is no more message in the Network to deliver.

func (*Network) DeliverAllExcept

func (n *Network) DeliverAllExcept(syncOnProcess bool, shouldDrop func(*PendingMessage) bool)

DeliverAllExcept flushes all pending messages in the buffer except those that satisfy the shouldDrop predicate function. All messages that satisfy the shouldDrop predicate are permanently dropped. The message receivers might be triggered to forward some messages to their peers, so this function will block until all receivers have done their forwarding, and there is no more message in the Network to deliver.

If syncOnProcess is true, the sender and receiver are synchronized on processing the message. Otherwise they sync on delivery of the message.

func (*Network) DeliverSome

func (n *Network) DeliverSome(syncOnProcess bool, shouldDeliver func(*PendingMessage) bool)

DeliverSome delivers all messages in the buffer that satisfy the shouldDeliver predicate. Any messages that are not delivered remain in the buffer.

If syncOnProcess is true, the sender and receiver are synchronized on processing the message. Otherwise they sync on delivery of the message.

func (*Network) GetID

func (n *Network) GetID() flow.Identifier

GetID returns the identity of the attached node.

func (*Network) MulticastOnChannel added in v0.25.0

func (n *Network) MulticastOnChannel(channel channels.Channel, event interface{}, num uint, targetIDs ...flow.Identifier) error

multicast is called when an engine attached to the channel is sending an event to a number of randomly chosen Engines attached to the same channel on other nodes. The targeted nodes are selected based on the selector. In this test helper implementation, multicast uses submit method under the hood.

func (*Network) PublishOnChannel added in v0.25.0

func (n *Network) PublishOnChannel(channel channels.Channel, event interface{}, targetIDs ...flow.Identifier) error

publish is called when the attached Engine is sending an event to a group of Engines attached to the same channel on other nodes based on selector. In this test helper implementation, publish uses submit method under the hood.

func (*Network) Register

func (n *Network) Register(channel channels.Channel, engine network.MessageProcessor) (network.Conduit, error)

Register registers an Engine of the attached node to the channel via a Conduit, and returns the Conduit instance.

func (*Network) ReportMisbehaviorOnChannel added in v0.31.0

func (n *Network) ReportMisbehaviorOnChannel(_ channels.Channel, _ network.MisbehaviorReport)

func (*Network) StartConDev

func (n *Network) StartConDev(updateInterval time.Duration, recursive bool)

StartConDev starts the continuous delivery mode of the Network. In this mode, the Network continuously checks the nodes' buffer every `updateInterval` milliseconds, and delivers all the pending messages. `recursive` determines whether the delivery is in recursive mode or not

func (*Network) StopConDev

func (n *Network) StopConDev()

StopConDev stops the continuous deliver mode of the Network.

func (*Network) UnRegisterChannel added in v0.25.0

func (n *Network) UnRegisterChannel(channel channels.Channel) error

func (*Network) UnicastOnChannel added in v0.25.0

func (n *Network) UnicastOnChannel(channel channels.Channel, event interface{}, targetID flow.Identifier) error

unicast is called when the attached Engine to the channel is sending an event to a single target Engine attached to the same channel on another node.

type PendingMessage

type PendingMessage struct {
	// The sender node id
	From    flow.Identifier
	Channel channels.Channel
	Event   interface{}
	// The id of the receiver nodes
	TargetIDs []flow.Identifier
}

PendingMessage is a pending message to be sent

Jump to

Keyboard shortcuts

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