neo

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2021 License: BSD-3-Clause Imports: 7 Imported by: 3

README

neo

Package neo implements side effects (network, time) simulation for testing.

Wake up, Neo...

Replace side effects with explicit dependencies so you can sleep at night. Abuse time and network simulation in unit tests and reduce flaky, complicated and long integration tests.

package main

import (
	"fmt"
	"time"

	"github.com/gotd/neo"
)

func main() {
	// Set to current time.
	t := neo.NewTime(time.Now())

	// Travel to future.
	fmt.Println(t.Travel(time.Hour * 2).Format(time.RFC3339))
	// 2019-07-19T16:42:09+03:00

	// Back to past.
	fmt.Println(t.Travel(time.Hour * -2).Format(time.RFC3339))
	// 2019-07-19T14:42:09+03:00
}

Documentation

Overview

Package neo implements side effects simulation for testing.

Index

Constants

This section is empty.

Variables

View Source
var ErrDeadline = errors.New("deadline")

Functions

This section is empty.

Types

type NAT

type NAT struct {
}

NAT implements facility for Network Address Translation simulation.

Basic example:

	[ A ] <-----> [ NAT1 ] <-----> [ NAT2 ] <-----> [ B ]
     IPa              IPa'     IPb'             IPb

	1) A sends packet P with dst = IPb'
 2) NAT1 receives packet P and changes it's src to IPa',
  sending it to NAT2 from IPa'.
 3) NAT2 receives packet P from IPa' to IPb', does a lookup to
  NAT translation table and finds association IPb' <-> IPb.
  Then it sends packet P to B.
 4) B receives packet P from NAT2, observing that it has src = IPa'.

 Now B can repeat steps 1-4 and send packet back.

 IPa  = 10.5.0.1:30000
 IPa' = 83.30.100.1:23100
 IPb' = 91.10.100.1:13000
 IPb  = 10.1.0.1:20000

type Net

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

Net is virtual "net" package, implements mesh of peers.

func (*Net) ListenPacket

func (n *Net) ListenPacket(network, address string) (net.PacketConn, error)

ListenPacket announces on the local network address.

func (*Net) ResolveUDPAddr

func (n *Net) ResolveUDPAddr(network, address string) (*net.UDPAddr, error)

ResolveUDPAddr returns an address of UDP end point.

type NetAddr

type NetAddr struct {
	Net     string
	Address string
}

func (NetAddr) Network

func (n NetAddr) Network() string

func (NetAddr) String

func (n NetAddr) String() string

type PacketConn

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

PacketConn simulates mesh peer of Net.

func (*PacketConn) Close

func (c *PacketConn) Close() error

Close closes the connection.

func (*PacketConn) LocalAddr

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

func (*PacketConn) ReadFrom

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

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

func (*PacketConn) SetDeadline

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

func (*PacketConn) SetReadDeadline

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

func (*PacketConn) SetWriteDeadline

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

func (*PacketConn) WriteTo

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

WriteTo writes a packet with payload p to addr.

type Ticker added in v0.1.2

type Ticker interface {
	C() <-chan time.Time
	Stop()
	Reset(d time.Duration)
}

Ticker abstracts a channel that delivers “ticks” of a clock at intervals.

type Time

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

Time simulates temporal interactions.

All methods are goroutine-safe.

func NewTime

func NewTime(now time.Time) *Time

NewTime returns new temporal simulator.

func (*Time) After

func (t *Time) After(d time.Duration) <-chan time.Time

After returns new channel that will receive time.Time value with current tme after specified duration.

func (*Time) Now

func (t *Time) Now() time.Time

Now returns the current time.

func (*Time) Observe added in v0.1.1

func (t *Time) Observe() <-chan struct{}

Observe return channel that closes on clock calls. The current implementation also closes the channel on Ticker’s ticks.

func (*Time) Set

func (t *Time) Set(now time.Time)

Set travels to specified time.

Also triggers temporal effects.

func (*Time) Sleep

func (t *Time) Sleep(d time.Duration)

Sleep blocks until duration is elapsed.

func (*Time) Ticker added in v0.1.2

func (t *Time) Ticker(d time.Duration) Ticker

func (*Time) Timer added in v0.1.2

func (t *Time) Timer(d time.Duration) Timer

func (*Time) Travel

func (t *Time) Travel(d time.Duration) time.Time

Travel adds duration to current time and returns result.

Also triggers temporal effects.

func (*Time) TravelDate

func (t *Time) TravelDate(years, months, days int) time.Time

TravelDate applies AddDate to current time and returns result.

Also triggers temporal effects.

func (*Time) When

func (t *Time) When(d time.Duration) time.Time

When returns relative time point.

type Timer added in v0.1.2

type Timer interface {
	C() <-chan time.Time
	Stop() bool
	Reset(d time.Duration)
}

Timer abstracts a single event.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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