mocknet

package
v0.0.0-...-297cbd2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2023 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package mocknet provides a mock net.Network to test with.

- a Mocknet has many network.Networks - a Mocknet has many Links - a Link joins two network.Networks - network.Conns and network.Streams are created by network.Networks

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("stream closed")

Functions

func ConnComplement

func ConnComplement(c network.Conn) network.Conn

ConnComplement returns the other end of the given connection. This function panics when passed a non-mocknet connection.

func StreamComplement

func StreamComplement(s network.Stream) network.Stream

StreamComplement returns the other end of the given stream. This function panics when passed a non-mocknet stream.

Types

type Link interface {
	Networks() []network.Network
	Peers() []peer.ID

	SetOptions(LinkOptions)
	Options() LinkOptions
}

Link represents the **possibility** of a connection between two peers. Think of it like physical network links. Without them, the peers can try and try but they won't be able to connect. This allows constructing topologies where specific nodes cannot talk to each other directly. :)

type LinkMap

type LinkMap map[string]map[string]map[Link]struct{}

LinkMap is a 3D map to give us an easy way to track links. (wow, much map. so data structure. how compose. ahhh pointer)

type LinkOptions

type LinkOptions struct {
	Latency   time.Duration
	Bandwidth float64 // in bytes-per-second

}

LinkOptions are used to change aspects of the links. Sorry but they dont work yet :(

type Mocknet

type Mocknet interface {
	// GenPeer generates a peer and its network.Network in the Mocknet
	GenPeer() (host.Host, error)

	// AddPeer adds an existing peer. we need both a privkey and addr.
	// ID is derived from PrivKey
	AddPeer(ic.PrivKey, ma.Multiaddr) (host.Host, error)
	AddPeerWithPeerstore(peer.ID, peerstore.Peerstore) (host.Host, error)

	// retrieve things (with randomized iteration order)
	Peers() []peer.ID
	Net(peer.ID) network.Network
	Nets() []network.Network
	Host(peer.ID) host.Host
	Hosts() []host.Host
	Links() LinkMap
	LinksBetweenPeers(a, b peer.ID) []Link
	LinksBetweenNets(a, b network.Network) []Link

	// Links are the **ability to connect**.
	// think of Links as the physical medium.
	// For p1 and p2 to connect, a link must exist between them.
	// (this makes it possible to test dial failures, and
	// things like relaying traffic)
	LinkPeers(peer.ID, peer.ID) (Link, error)
	LinkNets(network.Network, network.Network) (Link, error)
	Unlink(Link) error
	UnlinkPeers(peer.ID, peer.ID) error
	UnlinkNets(network.Network, network.Network) error

	// LinkDefaults are the default options that govern links
	// if they do not have their own option set.
	SetLinkDefaults(LinkOptions)
	LinkDefaults() LinkOptions

	// Connections are the usual. Connecting means Dialing.
	// **to succeed, peers must be linked beforehand**
	ConnectPeers(peer.ID, peer.ID) (network.Conn, error)
	ConnectNets(network.Network, network.Network) (network.Conn, error)
	DisconnectPeers(peer.ID, peer.ID) error
	DisconnectNets(network.Network, network.Network) error
	LinkAll() error
	ConnectAllButSelf() error

	io.Closer
}

func FullMeshConnected

func FullMeshConnected(n int) (Mocknet, error)

FullMeshConnected constructs a Mocknet with full mesh of Connections. This means that all the peers have dialed and are ready to talk to each other.

func FullMeshLinked

func FullMeshLinked(n int) (Mocknet, error)

FullMeshLinked constructs a Mocknet with full mesh of Links. This means that all the peers **can** connect to each other (not that they already are connected. you can use m.ConnectAll())

func New

func New() Mocknet

func WithNPeers

func WithNPeers(n int) (Mocknet, error)

WithNPeers constructs a Mocknet with N peers.

type Printer

type Printer interface {
	// MocknetLinks shows the entire Mocknet's link table :)
	MocknetLinks(mn Mocknet)
	NetworkConns(ni network.Network)
}

Printer lets you inspect things :)

func PrinterTo

func PrinterTo(w io.Writer) Printer

PrinterTo returns a Printer ready to write to w.

type RateLimiter

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

A RateLimiter is used by a link to determine how long to wait before sending data given a bandwidth cap.

func NewRateLimiter

func NewRateLimiter(bandwidth float64) *RateLimiter

Creates a new RateLimiter with bandwidth (in bytes/sec)

func (*RateLimiter) Limit

func (r *RateLimiter) Limit(dataSize int) time.Duration

Returns how long to wait before sending data with length 'dataSize' bytes

func (*RateLimiter) UpdateBandwidth

func (r *RateLimiter) UpdateBandwidth(bandwidth float64)

Changes bandwidth of a RateLimiter and resets its allowance

Jump to

Keyboard shortcuts

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