nettest

package standard library
go1.27.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnPair

func NewConnPair() (*Conn, *Conn)

NewConnPair returns a pair of connected Conns.

Types

type Conn

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

Conn is an in-memory test implementation of net.Conn.

func (*Conn) CanRead

func (c *Conn) CanRead() bool

CanRead reports whether Read can proceed without blocking.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) CloseRead

func (c *Conn) CloseRead() error

CloseRead shuts down the reading side of the connection.

func (*Conn) CloseWrite

func (c *Conn) CloseWrite() error

CloseWrite shuts down the writing side of the connection.

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed reports whether the connection has been closed. A connection is closed if [CloseRead] and [CloseWrite] are both called, or if [Close] is called.

To identify when the other side of the Conn has been closed, use Conn.Peer().IsClosed().

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr returns the (fake) local network address.

func (*Conn) Peer

func (c *Conn) Peer() *Conn

Peer returns the other end of the connection.

func (*Conn) Read

func (c *Conn) Read(b []byte) (n int, err error)

Read reads data from the connection.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

LocalAddr returns the (fake) remote network address.

func (*Conn) SetCloseError

func (c *Conn) SetCloseError(err error)

SetCloseError sets the error returned by Close. Close still closes the connection. A nil error restores the usual behavior.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines for the connection.

func (*Conn) SetLocalAddr

func (c *Conn) SetLocalAddr(addr net.Addr)

SetLocalAddr sets the local address.

To set the remote address, set the local address of Conn's peer.

func (*Conn) SetReadBufferSize

func (c *Conn) SetReadBufferSize(size int)

SetReadBufferSize sets the connection's read buffer. Writes to the other end of the connection will block so long as the buffer is full. Setting the size to 0 blocks all writes until the size is increased.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline for the connection.

func (*Conn) SetReadError

func (c *Conn) SetReadError(err error)

SetReadError causes any currently blocked and future Read calls to return a net.OpError wrapping err. It does not affect the other half of the connection. Reads will return any buffered data before returning the error, including data written after the error is set and io.EOF after the other end is closed. A nil error restores the usual behavior.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline for the connection.

func (*Conn) SetWriteError

func (c *Conn) SetWriteError(err error)

SetWriteError causes any currently blocked and future Write calls to return a net.OpError wrapping err. It does not affect the other half of the connection. Writes will not write data to the connection buffer while an error is set. A nil error restores the usual behavior.

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

Write writes data to the connection.

type Listener

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

Listener is an in-memory test implementation of net.Listener.

func NewListener

func NewListener() *Listener

NewListener returns a new Listener.

func (*Listener) Accept

func (li *Listener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

The connections returned by Accept are always [*Conn]s.

func (*Listener) Addr

func (li *Listener) Addr() net.Addr

Addr returns the listener's network address.

The address is always a *net.TCPAddr.

func (*Listener) Close

func (li *Listener) Close() error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors.

func (*Listener) NewConn

func (li *Listener) NewConn() *Conn

NewConn returns a new connection to the listener.

Accept will return the other side of the conn.

func (*Listener) NewConnConfig

func (li *Listener) NewConnConfig(f func(*Conn)) *Conn

NewConnConfig returns a new connection to the listener.

The function f is called with the new client connection. After f returns, Accept will return the other side of the connection.

For example, to create a connection from a specific IP address:

conn := li.NewConnConfig(func(conn *nettest.Conn) {
	conn.SetLocalAddr(net.TCPAddrFromAddrPort(netip.MustParseAddrPort("10.0.0.1:1234")))
})

func (*Listener) SetAcceptError

func (li *Listener) SetAcceptError(err error)

SetAcceptError causes any currently blocked and future Accept calls to return a net.OpError wrapping err. Accept will return any available connections before returning the error, including connections created after the error is set. A nil error restores the usual behavior.

func (*Listener) SetAddr

func (li *Listener) SetAddr(addr net.Addr)

SetAddr sets the listener's network address.

func (*Listener) SetCloseError

func (li *Listener) SetCloseError(err error)

SetCloseError sets the error returned by Close. Close still closes the listener. A nil error restores the usual behavior.

type PacketConn

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

func (*PacketConn) CanRead

func (p *PacketConn) CanRead() bool

CanRead reports whether [ReadFrom] can return at least one byte or an error. If [ReadFrom] would block, CanRead returns false.

func (*PacketConn) Close

func (p *PacketConn) Close() error

Close closes the connection.

func (*PacketConn) IsClosed

func (p *PacketConn) IsClosed() bool

IsClosed reports whether the connection has been closed.

func (*PacketConn) LocalAddr

func (p *PacketConn) LocalAddr() net.Addr

LocalAddr returns the (fake) local network address.

func (*PacketConn) ReadFrom

func (p *PacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error)

ReadFrom reads a packet from the connection, copying the payload into b.

func (*PacketConn) SetCloseError

func (c *PacketConn) SetCloseError(err error)

SetCloseError sets the error returned by Close. Close still closes the connection. A nil error restores the usual behavior.

func (*PacketConn) SetDeadline

func (p *PacketConn) SetDeadline(t time.Time) error

SetReadDeadline sets the read deadline for the connection. PacketConns have no write deadline.

func (*PacketConn) SetReadDeadline

func (p *PacketConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline for the connection.

func (*PacketConn) SetReadError

func (c *PacketConn) SetReadError(err error)

SetReadError causes any currently blocked and future ReadFrom calls to return a net.OpError wrapping err. It does not affect the other half of the connection. Reads will return any buffered data before returning the error, including data written after the error is set. A nil error restores the usual behavior.

func (*PacketConn) SetWriteDeadline

func (p *PacketConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline has no effect. Writes to PacketConns never block.

func (*PacketConn) SetWriteError

func (c *PacketConn) SetWriteError(err error)

SetWriteError causes any currently blocked and future WriteTo calls to return a net.OpError wrapping err. It does not affect the other half of the connection. Writes will not write data while an error is set. A nil error restores the usual behavior.

func (*PacketConn) WriteTo

func (p *PacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error)

WriteTo writes a packet with payload b to addr. addr must be a *net.UDPAddr.

WriteTo appends the packet to the recipient's receive buffer. If no recipient is listening on addr or if the recipient's receive buffer is full, the packet is silently discarded.

type PacketNet

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

A PacketNet is a group of communicating [PacketConn]s.

func NewPacketNet

func NewPacketNet() *PacketNet

NewPacketNet returns a new PacketNet.

func (*PacketNet) NewConn

func (n *PacketNet) NewConn(a net.Addr) (*PacketConn, error)

NewConn returns a new PacketConn listening on the given address. It returns an error if there is an existing listener on this address.

Jump to

Keyboard shortcuts

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