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 ¶
- func LoopbackUDP() (*net.UDPConn, error)
- func Pipe() (net.Conn, net.Conn)
- type Host
- type Link
- type LinkPair
- type Net
- func (n *Net) Dial(ctx context.Context, _, addr string) (net.Conn, error)
- func (n *Net) DialFrom(ctx context.Context, src netip.Addr, addr string) (net.Conn, error)
- func (n *Net) From(src netip.Addr) *Host
- func (n *Net) LinkOf(addr string) LinkPair
- func (n *Net) Listen(_ context.Context, _, addr string) (net.Listener, error)
- func (n *Net) ListenPacket(_ context.Context, _, addr string) (net.PacketConn, error)
- func (n *Net) Listening(addr string) bool
- func (n *Net) Resolve(_ context.Context, host string) ([]netip.Addr, error)
- func (n *Net) SetHost(name string, addrs ...netip.Addr)
- func (n *Net) SetLink(addr string, p LinkPair)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoopbackUDP ¶
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 ¶
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.
type Link ¶
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 (*Net) Dial ¶
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 ¶
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 ¶
From returns n as seen from host src: its Dial is DialFrom(src), and everything else is n's.
func (*Net) ListenPacket ¶
ListenPacket opens a packet endpoint at addr; the network is ignored. Datagrams to an address where nothing listens are dropped.
func (*Net) Resolve ¶
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.