testutil

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConnecting = errors.New("connection error")
View Source
var ErrInvalidArgument = errors.New("invalid argument")
View Source
var ErrInvalidState = errors.New("invalid state")
View Source
var GetFalseFn = func() (bool, error) {
	return false, nil
}
View Source
var GetTrueFn = func() (bool, error) {
	return true, nil
}
View Source
var NoopFn = func() error {
	return nil
}

Functions

func AssertDuration

func AssertDuration(t *testing.T, expectedDuration int, actualDuration time.Duration)

func ErrorNTimesThenError

func ErrorNTimesThenError[R any](err error, errorTimes int, finalError error) func(exec failsafe.Execution[R]) (R, error)

ErrorNTimesThenError returns a stub function that returns the err errorTimes and then returns the finalError. Can be used with failsafe.GetWithExecution.

func ErrorNTimesThenPanic

func ErrorNTimesThenPanic[R any](err error, errorTimes int, panicValue any) func(exec failsafe.Execution[R]) (R, error)

ErrorNTimesThenPanic returns a stub function that returns the err errorTimes and then panics with the panicValue. Can be used with failsafe.GetWithExecution.

func ErrorNTimesThenReturn

func ErrorNTimesThenReturn[R any](err error, errorTimes int, results ...R) (fn func(exec failsafe.Execution[R]) (R, error), resetFn func())

ErrorNTimesThenReturn returns a stub function that returns the err errorTimes and then returns the results. Can be used with failsafe.GetWithExecution.

func GetFn

func GetFn[R any](result R, err error) func() (R, error)

func GetType

func GetType(myvar interface{}) string

func GetWithExecutionFn

func GetWithExecutionFn[R any](result R, err error) func(exec failsafe.Execution[R]) (R, error)

func MillisToNanos

func MillisToNanos(millis int) int64

func MockDelayedResponse added in v0.6.0

func MockDelayedResponse(statusCode int, body string, delay time.Duration) *httptest.Server

func MockFlakyServer added in v0.6.2

func MockFlakyServer(failTimes int, responseCode int, retryAfterDelay time.Duration, finalResponse string) *httptest.Server

func MockResponse added in v0.6.0

func MockResponse(statusCode int, body string) *httptest.Server

func PrepareTest added in v0.6.2

func PrepareTest[R any](t *testing.T, given Given, executor failsafe.Executor[R], expectedAttempts int, expectedExecutions int, expectedResult R, expectedError *error, expectedSuccess bool, then ...func()) (executorFn func() failsafe.Executor[R], assertResult func(R, error))

func RunFn

func RunFn(err error) func() error

func SetupWithContextSleep added in v0.3.1

func SetupWithContextSleep(sleepTime time.Duration) func() context.Context

SetupWithContextSleep returns a setup function that provides a context that is canceled after the sleepTime.

func TestGetFailure

func TestGetFailure[R any](t *testing.T, given Given, executor failsafe.Executor[R], when WhenGet[R], expectedAttempts int, expectedExecutions int, expectedError error, then ...func())

func TestGetSuccess

func TestGetSuccess[R any](t *testing.T, given Given, executor failsafe.Executor[R], when WhenGet[R], expectedAttempts int, expectedExecutions int, expectedResult R, then ...func())

func TestGetSuccessError added in v0.6.2

func TestGetSuccessError[R any](t *testing.T, given Given, executor failsafe.Executor[R], when WhenGet[R], expectedAttempts int, expectedExecutions int, expectedError error, then ...func())

func TestRunFailure

func TestRunFailure[R any](t *testing.T, given Given, executor failsafe.Executor[R], when WhenRun[R], expectedAttempts int, expectedExecutions int, expectedError error, then ...func())

func TestRunSuccess

func TestRunSuccess[R any](t *testing.T, given Given, executor failsafe.Executor[R], when WhenRun[R], expectedAttempts int, expectedExecutions int, then ...func())

func Timed

func Timed(fn func()) time.Duration

func WaitAndAssertCanceled

func WaitAndAssertCanceled[R any](t *testing.T, waitDuration time.Duration, exec failsafe.Execution[R])

Types

type CompositeError

type CompositeError struct {
	Cause error
}

func NewCompositeError added in v0.4.0

func NewCompositeError(cause error) *CompositeError

func (*CompositeError) Error

func (ce *CompositeError) Error() string

func (*CompositeError) Is

func (ce *CompositeError) Is(err error) bool

func (*CompositeError) Unwrap

func (ce *CompositeError) Unwrap() error

type Given

type Given func() context.Context

Given performs pre-test setup that may involve resetting state so that the same fixtures can be used for sync and async tests.

type TestClock

type TestClock struct {
	CurrentTime int64
}

func (*TestClock) CurrentUnixNano

func (t *TestClock) CurrentUnixNano() int64

type TestExecution

type TestExecution[R any] struct {
	TheLastResult R
	TheAttempts   int
	TheRetries    int
	TheHedges     int
}

func (TestExecution[R]) AttemptStartTime

func (e TestExecution[R]) AttemptStartTime() time.Time

func (TestExecution[R]) Attempts

func (e TestExecution[R]) Attempts() int

func (TestExecution[R]) Canceled

func (e TestExecution[R]) Canceled() <-chan struct{}

func (TestExecution[R]) Context added in v0.3.1

func (e TestExecution[R]) Context() context.Context

func (TestExecution[R]) ElapsedAttemptTime

func (e TestExecution[R]) ElapsedAttemptTime() time.Duration

func (TestExecution[R]) ElapsedTime

func (e TestExecution[R]) ElapsedTime() time.Duration

func (TestExecution[R]) Executions

func (e TestExecution[R]) Executions() int

func (TestExecution[R]) Hedges added in v0.5.0

func (e TestExecution[R]) Hedges() int

func (TestExecution[R]) IsCanceled

func (e TestExecution[R]) IsCanceled() bool

func (TestExecution[R]) IsFirstAttempt

func (e TestExecution[R]) IsFirstAttempt() bool

func (TestExecution[R]) IsHedge added in v0.5.0

func (e TestExecution[R]) IsHedge() bool

func (TestExecution[R]) IsRetry

func (e TestExecution[R]) IsRetry() bool

func (TestExecution[R]) LastError

func (e TestExecution[R]) LastError() error

func (TestExecution[R]) LastResult

func (e TestExecution[R]) LastResult() R

func (TestExecution[R]) Retries added in v0.5.0

func (e TestExecution[R]) Retries() int

func (TestExecution[R]) StartTime

func (e TestExecution[R]) StartTime() time.Time

type TestStopwatch

type TestStopwatch struct {
	CurrentTime int64
}

func (*TestStopwatch) ElapsedTime

func (t *TestStopwatch) ElapsedTime() time.Duration

func (*TestStopwatch) Reset

func (t *TestStopwatch) Reset()

type Waiter

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

func NewWaiter

func NewWaiter() *Waiter

func (*Waiter) AssertEqual

func (w *Waiter) AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool

func (*Waiter) Await

func (w *Waiter) Await(expectedResumes int)

func (*Waiter) AwaitWithTimeout

func (w *Waiter) AwaitWithTimeout(expectedResumes int, timeout time.Duration)

func (*Waiter) Resume

func (w *Waiter) Resume()

type WhenGet

type WhenGet[R any] func(execution failsafe.Execution[R]) (R, error)

type WhenRun

type WhenRun[R any] func(execution failsafe.Execution[R]) error

Jump to

Keyboard shortcuts

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