nd

package module
v0.0.0-...-69bf502 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2015 License: MIT Imports: 4 Imported by: 0

README

Non Deterministic Helper

This small library aims to make non deterministic code easier to test.

Functions which rely on unpredicatable values are notoriously hard to test. Functions which rely on the current time are a good example. In static languages, the most common approach is to wrap the value in an interface and inject this everywhere. This is the suggestion you'll find in many books, but it simply isn't pragmatic.

However, in languages which expose functions as first class citizens, such as Go, there's a simpler solution: create your own wrapper functions.

Installation

Install using the "go get" command:

go get github.com/viki-org/nd

Usage

In your code use nd.Now() instead of time.Now(). By default, nd.Now() wraps time.Now(), so you'll get the same time.Time structure which represents the current time.

In tests which require a known time value, use nd.ForceNow(time time.Time) or nd.ForceNowTimestamp(timestmap int64). After forcing the time, subsequent calls to nd.Now() will always return the forced value. The default behavior can be restored by calling nd.ResetNow().

Supported Generators

  • nd.Now() - The current time
  • nd.Guidv4() and nd.Guidv4String() - A V4 Guid (either as a []byte or a string)
  • nd.CryptRand(b []byte) (n, error) - Fills b with a cryptographically secure rand

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CryptRand func([]byte) (int, error)
View Source
var Guidv4 func() []byte
View Source
var IntRand func() int
View Source
var IntnRand func(int) int
View Source
var Now func() time.Time

Functions

func ForceCryptRand

func ForceCryptRand(forced []byte)

func ForceGuid

func ForceGuid(guid string) error

func ForceIntRand

func ForceIntRand(forced int)

func ForceIntnRand

func ForceIntnRand(forced int)

func ForceNow

func ForceNow(t time.Time)

func ForceNowTimestamp

func ForceNowTimestamp(timestamp int64)

func Guidv4String

func Guidv4String() string

func ResetCryptRand

func ResetCryptRand()

func ResetGuidv4

func ResetGuidv4()

func ResetIntRand

func ResetIntRand()

func ResetIntnRand

func ResetIntnRand()

func ResetNow

func ResetNow()

func Seed

func Seed(s int64)

Types

This section is empty.

Jump to

Keyboard shortcuts

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