abstractGoNet

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

README

abstractGoNet

The primary purpose of this library is to make client-server applications using raw golang connections testable. This package is more flexible than an injection of net.Pipe would be because the underlying IO is buffered over two os.Pipe implementations. This package is easy to inject because it matches the syntax of the Go std net library. It also features Mutex locking around the virtual WAN object allowing simple testing of scenarios where listeners and connections may frequently flap.

Documentation

See the public docs at pkg.go.dev

Examples

package foobar

import (
  "github.com/zapper59/abstractGoNet"
  "net"
  "testing"
)

func newServer(net abstractGoNet.Net) net.Listener {
    listener, _ := host1.Listen("tcp", ":1234")
}

func driveServer(net.Listener) {
    conn, _ := listener.Accept()
    ...
}

func TestBasic(t *testing.T) {
    wan := abstractGoNet.NewVirtualWan()
    host1 := wan.NewVirtualHost("42.0.0.1")
    host2 := wan.NewVirtualHost("42.0.0.2")

    server := newServer(host1)
    go driveServer(server)
    conn, _ := host2.Dial("tcp", "42.0.0.1:1234")

    ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HostNotFoundErr = errors.New(
	"Hostname did not resolve to a known host",
)
View Source
var ListenerClosedErr = errors.New("Listener Closed")
View Source
var ListenerConflictErr = errors.New("Listener already registered")
View Source
var ListenerNotFoundErr = errors.New("Listener already registered")

Functions

This section is empty.

Types

type Net

type Net interface {
	Listen(network, address string) (net.Listener, error)
	Dial(network, address string) (net.Conn, error)
}

An abstract template matching that of net.

func RealNet

func RealNet() Net

Get a Net implementation that forwards to net.

type VirtualHostInfo

type VirtualHostInfo struct {
	// Hostname list.
	Names []string

	// IP Address list.
	Addrs []string
}

Collection of hostnames and ip's that a host can be reached at.

type VirtualWan

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

Thread-safe map of virtual hosts, each of which implements Net. All functions on objects handed out by this type, as well as the net.Listener implementations they generate share a single Mutex to allow for concurrent host lookups. IO itself is synchronized by the underlying os.File handed out by os.Pipe for each virtual connection.

func NewVirtualWan

func NewVirtualWan() VirtualWan

Create a virtual Wide Area Network.

func (*VirtualWan) NewVirtualHost

func (self *VirtualWan) NewVirtualHost(addr string) Net

Register a unique machine on the network where addr is an ip or hostname.

func (*VirtualWan) NewVirtualHostWithInfo

func (self *VirtualWan) NewVirtualHostWithInfo(info VirtualHostInfo) Net

Register a unique machine on the network.

Jump to

Keyboard shortcuts

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