memnet

package
v0.0.0-...-466b026 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memnet is an in-process network for tests: stream listeners and connections that behave like TCP sockets (buffered in both directions, half-close, reset on SetLinger(0), deadlines), packet endpoints that deliver whole datagrams and drop them when full, a resolver, dials from a chosen source host, and fault injection per address and direction: delay, rate, loss and cut links, driven by an injectable clock and randomness source.

It also holds LoopbackUDP, a real UDP socket bound so that its replies cannot be lost to another socket on the same port.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoopbackUDP

func LoopbackUDP() (*net.UDPConn, error)

LoopbackUDP returns a UDP socket on an ephemeral 127.0.0.1 port, for a test's client. A socket bound to the dual-stack wildcard instead may get, on some systems, a port that another socket holds for the IPv4 wildcard; replies to 127.0.0.1 on that port then reach the other socket. An address-specific IPv4 bind never shares a port with such a socket.

func Pipe

func Pipe() (net.Conn, net.Conn)

Pipe returns the two ends of an in-memory stream connection buffered like a socket, 256 KiB in each direction, unlike net.Pipe, whose unbuffered writes block two ends that write at once until one reads. CloseWrite ends the writing direction alone, and after SetLinger(0) Close resets the connection: the peer's reads and writes fail with ECONNRESET.

Types

type Host

type Host struct {
	*Net
	// contains filtered or unexported fields
}

Host is a Net as one host sees it; From returns it.

func (*Host) Dial

func (h *Host) Dial(ctx context.Context, _, addr string) (net.Conn, error)

Dial connects from the host to a listener, as DialFrom does.

type Link struct {
	Delay time.Duration // added before every write or datagram
	Loss  float64       // datagram drop probability in [0, 1]; streams stay reliable
	Rate  int64         // bytes per second; 0 = unlimited
	Off   bool          // disconnected: dials refused, datagrams dropped, open connections closed
}

Link are the properties of one direction of the traffic to or from an address of a Net.

type LinkPair

type LinkPair struct {
	To, From Link
}

LinkPair are both directions of the traffic of one address: To towards it (the dialer's writes), From out of it (the listener's writes).

type Net

type Net struct {
	// Clock runs link delays and rates; nil is the system clock.
	Clock timeshim.Clock
	// Rand draws datagram losses; nil draws from math/rand/v2's global
	// source. It is used under a lock of the Net.
	Rand *rand.Rand
	// contains filtered or unexported fields
}

Net is an in-process network for tests. Stream connections are pairs of bounded byte queues, one per direction, buffered like a socket so that both ends may write at once (such as two TLS ends sending close_notify), with CloseWrite ending only the sending direction as on TCP; packet connections deliver whole datagrams through bounded queues and drop them when full, as UDP does. Addresses are host:port strings, and port 0 picks a free port. Resolve answers from SetHost, else parses the host as an address.

SetLink injects faults per address and direction. Clock and Rand, when set before the Net is used, drive the delays and losses; deadlines are wall-clock times, as on sockets. All methods are safe for concurrent use.

func New

func New() *Net

New returns an empty network.

func (*Net) Dial

func (n *Net) Dial(ctx context.Context, _, addr string) (net.Conn, error)

Dial connects to the stream listener at addr; both ends have no address ("pipe"). A dial to no listener, or over a link that is Off, is refused with ECONNREFUSED, as the system reports it.

func (*Net) DialFrom

func (n *Net) DialFrom(ctx context.Context, src netip.Addr, addr string) (net.Conn, error)

DialFrom is Dial from host src: the dialer's LocalAddr and the accepted end's RemoteAddr are src with an ephemeral port, so a listener sees distinct client sources (per-source limits). From(src) dials this way.

func (*Net) From

func (n *Net) From(src netip.Addr) *Host

From returns n as seen from host src: its Dial is DialFrom(src), and everything else is n's.

func (*Net) LinkOf

func (n *Net) LinkOf(addr string) LinkPair

LinkOf returns the link properties of addr.

func (*Net) Listen

func (n *Net) Listen(_ context.Context, _, addr string) (net.Listener, error)

Listen opens a stream listener at addr; the network is ignored.

func (*Net) ListenPacket

func (n *Net) ListenPacket(_ context.Context, _, addr string) (net.PacketConn, error)

ListenPacket opens a packet endpoint at addr; the network is ignored. Datagrams to an address where nothing listens are dropped.

func (*Net) Listening

func (n *Net) Listening(addr string) bool

Listening reports a stream listener at addr.

func (*Net) Resolve

func (n *Net) Resolve(_ context.Context, host string) ([]netip.Addr, error)

Resolve returns the addresses of host: those SetHost gave, or host itself if it is an IP address, or else a *net.DNSError that IsNotFound.

func (*Net) SetHost

func (n *Net) SetHost(name string, addrs ...netip.Addr)

SetHost makes Resolve answer name with addrs; no addrs removes name.

func (n *Net) SetLink(addr string, p LinkPair)

SetLink sets the link properties of addr (a listener or packet endpoint); a zero pair clears them. Turning a direction Off closes the open connections to addr. Delays and rates run on n.Clock (a fake clock moves them only when it advances) and losses draw from n.Rand.

Jump to

Keyboard shortcuts

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