Documentation
¶
Overview ¶
Package fakenet contains fake implementations of interfaces from package net from the standard library.
It is recommended to fill all methods that shouldn't be called with:
panic(testutil.UnexpectedCall(arg1, arg2))
See the package example.
Example ¶
package main
import (
"fmt"
"io"
"net"
"slices"
"github.com/AdguardTeam/golibs/testutil"
"github.com/AdguardTeam/golibs/testutil/fakenet"
)
func main() {
var written []byte
fakeConn := &fakenet.Conn{
// Use OnClose with a panic to signal that Close is expected to not be
// called.
OnClose: func() (err error) { panic(testutil.UnexpectedCall()) },
// Other methods implemented in the same way as Close.
// Use OnWrite to record its argument.
OnWrite: func(b []byte) (n int, err error) {
written = slices.Clone(b)
return len(b), nil
},
}
// The function that is expected to call Write.
testedFunction := func(c net.Conn) (err error) {
_, err = io.WriteString(c, "test message")
if err != nil {
return fmt.Errorf("writing: %w", err)
}
return nil
}
// A simulation of a successful test.
gotErr := testedFunction(fakeConn)
fmt.Printf("written: %v %q\n", gotErr, written)
// The function that is expected to not call Close.
failingFunction := func(c net.Conn) (err error) {
err = c.Close()
if err != nil {
return fmt.Errorf("closing: %w", err)
}
return nil
}
// A simulation of a failing test.
defer func() {
fmt.Printf("got panic: %v\n", recover())
}()
gotErr = failingFunction(fakeConn)
fmt.Printf("never printed: %v\n", gotErr)
}
Output: written: <nil> "test message" got panic: unexpected call to github.com/AdguardTeam/golibs/testutil/fakenet.(*Conn).Close()
Index ¶
- type Conn
- func (c *Conn) Close() (err error)
- func (c *Conn) LocalAddr() (laddr net.Addr)
- func (c *Conn) Read(b []byte) (n int, err error)
- func (c *Conn) RemoteAddr() (raddr net.Addr)
- func (c *Conn) SetDeadline(t time.Time) (err error)
- func (c *Conn) SetReadDeadline(t time.Time) (err error)
- func (c *Conn) SetWriteDeadline(t time.Time) (err error)
- func (c *Conn) Write(b []byte) (n int, err error)
- type Listener
- type PacketConn
- func (c *PacketConn) Close() (err error)
- func (c *PacketConn) LocalAddr() (laddr net.Addr)
- func (c *PacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error)
- func (c *PacketConn) SetDeadline(t time.Time) (err error)
- func (c *PacketConn) SetReadDeadline(t time.Time) (err error)
- func (c *PacketConn) SetWriteDeadline(t time.Time) (err error)
- func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
OnClose func() (err error)
OnLocalAddr func() (laddr net.Addr)
OnRead func(b []byte) (n int, err error)
OnRemoteAddr func() (raddr net.Addr)
OnSetDeadline func(t time.Time) (err error)
OnSetReadDeadline func(t time.Time) (err error)
OnSetWriteDeadline func(t time.Time) (err error)
OnWrite func(b []byte) (n int, err error)
}
Conn is the net.Conn for tests.
func (*Conn) RemoteAddr ¶
RemoteAddr implements the net.Conn interface for *Conn.
func (*Conn) SetDeadline ¶
SetDeadline implements the net.Conn interface for *Conn.
func (*Conn) SetReadDeadline ¶
SetReadDeadline implements the net.Conn interface for *Conn.
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline implements the net.Conn interface for *Conn.
type Listener ¶
type Listener struct {
OnAccept func() (c net.Conn, err error)
OnAddr func() (addr net.Addr)
OnClose func() (err error)
}
Listener is a net.Listener for tests.
func (*Listener) Accept ¶
Accept implements the net.Listener interface for *Listener.
func (*Listener) Addr ¶
Addr implements the net.Listener interface for *Listener.
func (*Listener) Close ¶
Close implements the net.Listener interface for *Listener.
type PacketConn ¶
type PacketConn struct {
OnClose func() (err error)
OnLocalAddr func() (laddr net.Addr)
OnReadFrom func(b []byte) (n int, addr net.Addr, err error)
OnSetDeadline func(t time.Time) (err error)
OnSetReadDeadline func(t time.Time) (err error)
OnSetWriteDeadline func(t time.Time) (err error)
OnWriteTo func(b []byte, addr net.Addr) (n int, err error)
}
PacketConn is the net.PacketConn for tests.
func (*PacketConn) Close ¶
func (c *PacketConn) Close() (err error)
Close implements the net.PacketConn interface for *PacketConn.
func (*PacketConn) LocalAddr ¶
func (c *PacketConn) LocalAddr() (laddr net.Addr)
LocalAddr implements the net.PacketConn interface for *PacketConn.
func (*PacketConn) ReadFrom ¶
ReadFrom implements the net.PacketConn interface for *PacketConn.
func (*PacketConn) SetDeadline ¶
func (c *PacketConn) SetDeadline(t time.Time) (err error)
SetDeadline implements the net.PacketConn interface for *PacketConn.
func (*PacketConn) SetReadDeadline ¶
func (c *PacketConn) SetReadDeadline(t time.Time) (err error)
SetReadDeadline implements the net.PacketConn interface for *PacketConn.
func (*PacketConn) SetWriteDeadline ¶
func (c *PacketConn) SetWriteDeadline(t time.Time) (err error)
SetWriteDeadline implements the net.PacketConn interface for *PacketConn.
func (*PacketConn) WriteTo ¶
WriteTo implements the net.PacketConn interface for *PacketConn.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package fakehttp contains fake implementations of interfaces from packages net/http and github.com/AdguardTeam/golibs/netutil/httputil.
|
Package fakehttp contains fake implementations of interfaces from packages net/http and github.com/AdguardTeam/golibs/netutil/httputil. |