fakenet

package
v0.30.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: Unlicense Imports: 2 Imported by: 0

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("not implemented")

in the body of the test, so that if the method is called the panic backtrace points to the method definition in the test. See the package example.

Example
package main

import (
	"fmt"
	"io"
	"net"
	"slices"

	"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.
		//
		// It is not recommended to construct these fake values in helper
		// functions (for example, newFakeConn), because then the panic
		// backtrace in the test failure points to the helper function as
		// opposed to this point in the code.
		OnClose: func() (err error) {
			panic("not implemented")
		},

		// 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: not implemented

Index

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) Close

func (c *Conn) Close() (err error)

Close implements the net.Conn interface for *Conn.

func (*Conn) LocalAddr

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

LocalAddr implements the net.Conn interface for *Conn.

func (*Conn) Read

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

Read implements the net.Conn interface for *Conn.

func (*Conn) RemoteAddr

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

RemoteAddr implements the net.Conn interface for *Conn.

func (*Conn) SetDeadline

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

SetDeadline implements the net.Conn interface for *Conn.

func (*Conn) SetReadDeadline

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

SetReadDeadline implements the net.Conn interface for *Conn.

func (*Conn) SetWriteDeadline

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

SetWriteDeadline implements the net.Conn interface for *Conn.

func (*Conn) Write

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

Write 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

func (l *Listener) Accept() (c net.Conn, err error)

Accept implements the net.Listener interface for *Listener.

func (*Listener) Addr

func (l *Listener) Addr() (addr net.Addr)

Addr implements the net.Listener interface for *Listener.

func (*Listener) Close

func (l *Listener) Close() (err error)

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

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

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

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

WriteTo implements the net.PacketConn interface for *PacketConn.

Jump to

Keyboard shortcuts

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