testutils

package
v2.2.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultWaitTimeout = 30 * time.Second

DefaultWaitTimeout is the default wait timeout. If you have a *testing.T, use WaitTimeout instead.

View Source
const (
	// Password just a password we use everywhere for testing
	Password = "16charlengthp4SsW0rD1!@#_"
)
View Source
const TestInterval = 100 * time.Millisecond

TestInterval is just a sensible poll interval that gives fast tests without risk of spamming

Variables

View Source
var FixtureChainID = big.NewInt(0)

FixtureChainID matches the chain always added by fixtures.sql It is set to 0 since no real chain ever has this ID and allows a virtual "test" chain ID to be used without clashes

View Source
var SimulatedChainID = big.NewInt(1337)

SimulatedChainID is the chain ID for the go-ethereum simulated backend

Functions

func AfterWaitTimeout

func AfterWaitTimeout(t *testing.T) <-chan time.Time

AfterWaitTimeout returns a channel that will send a time value when the WaitTimeout is reached

func AssertCount

func AssertCount(t *testing.T, db *sqlx.DB, tableName string, expected int64)

func AssertEventually

func AssertEventually(t *testing.T, f func() bool)

AssertEventually waits for f to return true

func Context

func Context(tb testing.TB) context.Context

Context returns a context with the test's deadline, if available.

func IntToHex

func IntToHex(n int) string

IntToHex converts int to geth-compatible hex

func LoggerAssertMaxLevel

func LoggerAssertMaxLevel(t *testing.T, lvl zapcore.Level) logger.Logger

LoggerAssertMaxLevel returns a test logger which is observed on cleanup and asserts that no lines were logged at a higher level.

func MustNewSimTransactor

func MustNewSimTransactor(t testing.TB) *bind.TransactOpts

MustNewSimTransactor returns a transactor for interacting with the geth simulated backend.

func MustParseBigInt

func MustParseBigInt(t *testing.T, input string) *big.Int

MustParseBigInt parses a big int value from string or fails the test

func MustParseURL

func MustParseURL(t testing.TB, input string) *url.URL

MustParseURL parses the URL or fails the test

func NewAddress

func NewAddress() common.Address

NewAddress return a random new address

func NewAddressPtr

func NewAddressPtr() *common.Address

func NewRandomEVMChainID

func NewRandomEVMChainID() *big.Int

NewRandomEVMChainID returns a suitable random chain ID that will not conflict with fixtures

func NewRandomPositiveInt64

func NewRandomPositiveInt64() int64

NewRandomPositiveInt64 returns a (non-cryptographically secure) random positive int64

func NewTestFlagSet

func NewTestFlagSet() *flag.FlagSet

func NewWSServer

func NewWSServer(t *testing.T, chainID *big.Int, callback JSONRPCHandler) (ts *testWSServer)

NewWSServer starts a websocket server which invokes callback for each message received. If chainID is set, then eth_chainId calls will be automatically handled.

func Ptr

func Ptr[T any](v T) *T

Ptr takes pointer of anything

func Random32Byte

func Random32Byte() (b [32]byte)

Random32Byte returns a random [32]byte

func RandomizeName

func RandomizeName(n string) string

RandomizeName appends a random UUID to the provided name

func RequireLogMessage

func RequireLogMessage(t *testing.T, observedLogs *observer.ObservedLogs, msg string)

RequireLogMessage fails the test if emitted logs don't contain the given message

func SkipShort

func SkipShort(tb testing.TB, why string)

SkipShort skips tb during -short runs, and notes why.

func SkipShortDB

func SkipShortDB(tb testing.TB)

SkipShortDB skips tb during -short runs, and notes the DB dependency.

func WSServerURL

func WSServerURL(t *testing.T, s *httptest.Server) *url.URL

WSServerURL returns a ws:// url for the server

func WaitForLogMessage

func WaitForLogMessage(t *testing.T, observedLogs *observer.ObservedLogs, msg string)

WaitForLogMessage waits until at least one log message containing the specified msg is emitted. NOTE: This does not "pop" messages so it cannot be used multiple times to check for new instances of the same msg. See WaitForLogMessageCount instead.

Get a *observer.ObservedLogs like so:

observedZapCore, observedLogs := observer.New(zap.DebugLevel)
lggr := logger.TestLogger(t, observedZapCore)

func WaitForLogMessageCount

func WaitForLogMessageCount(t *testing.T, observedLogs *observer.ObservedLogs, msg string, count int)

WaitForLogMessageCount waits until at least count log message containing the specified msg is emitted

func WaitTimeout

func WaitTimeout(t *testing.T) time.Duration

WaitTimeout returns a timeout based on the test's Deadline, if available. Especially important to use in parallel tests, as their individual execution can get paused for arbitrary amounts of time.

func WaitWithTimeout

func WaitWithTimeout(t *testing.T, ch <-chan struct{}, failMsg string)

WaitWithTimeout waits for the channel to close (or receive anything) and fatals the test if the default wait timeout is exceeded

Types

type JSONRPCHandler

type JSONRPCHandler func(reqMethod string, reqParams gjson.Result) JSONRPCResponse

JSONRPCHandler is called with the method and request param(s). respResult will be sent immediately. notifyResult is optional, and sent after a short delay.

type JSONRPCResponse

type JSONRPCResponse struct {
	Result, Notify string // raw JSON (i.e. quoted strings etc.)

	Error struct {
		Code    int
		Message string
	}
}

Directories

Path Synopsis
v2
v2

Jump to

Keyboard shortcuts

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