testutil

package
v0.6.7 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidArgument = errors.New("invalid argument")
	ErrInvalidState    = errors.New("invalid state")
	ErrConnecting      = errors.New("connection error")

	NoopFn = func() error { return nil }

	GetFalseFn = func() (bool, error) { return false, nil }

	GetTrueFn = func() (bool, error) { return true, nil }
)
View Source
var CanceledContextFn = func() context.Context {
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	return ctx
}

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(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(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(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(failsafe.Execution[R]) (R, error)

func GetType

func GetType(myvar interface{}) string

func GrpcClient added in v0.6.5

func GrpcClient(dialer Dialer, options ...grpc.DialOption) *grpc.ClientConn

func MillisToNanos

func MillisToNanos(millis int) int64

func MockDelayedGrpcResponse added in v0.6.5

func MockDelayedGrpcResponse(response string, delay time.Duration) pbfixtures.PingServiceServer

func MockDelayedResponse added in v0.6.0

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

func MockFlakyGrpcServer added in v0.6.5

func MockFlakyGrpcServer(failTimes int, err error, finalResponse string) pbfixtures.PingServiceServer

func MockFlakyServer added in v0.6.2

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

func MockGrpcError added in v0.6.5

func MockGrpcError(err error) pbfixtures.PingServiceServer

func MockGrpcResponses added in v0.6.5

func MockGrpcResponses(responses ...string) pbfixtures.PingServiceServer

func MockResponse added in v0.6.0

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

func RunFn

func RunFn(err error) func(failsafe.Execution[any]) 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 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 AssertFunc added in v0.6.5

type AssertFunc[R any] func(expectedAttempts int, expectedExecutions int, expectedResult R, result R, expectedErr error, err error, expectedSuccess bool, expectedFailure bool, errorAs bool, thens ...func())

func PrepareTest added in v0.6.2

func PrepareTest[R any](t *testing.T, setupFn func(), contextFn ContextFn, executor failsafe.Executor[R]) (executorFn func() failsafe.Executor[R], assertFn AssertFunc[R])

type CompositeError

type CompositeError struct {
	Cause error
}

func (CompositeError) Error

func (e CompositeError) Error() string

func (CompositeError) Unwrap

func (e CompositeError) Unwrap() error

type ContextFn added in v0.6.5

type ContextFn func() context.Context

type CustomError added in v0.6.7

type CustomError struct{ Msg string }

func (CustomError) Error added in v0.6.7

func (e CustomError) Error() string

type Dialer added in v0.6.5

type Dialer func(context.Context, string) (net.Conn, error)

func GrpcServer added in v0.6.5

func GrpcServer(service pbfixtures.PingServiceServer, options ...grpc.ServerOption) (*grpc.Server, Dialer)

type MultiError added in v0.6.7

type MultiError []error

func (MultiError) Error added in v0.6.7

func (e MultiError) Error() string

func (MultiError) Unwrap added in v0.6.7

func (e MultiError) Unwrap() []error

type Resetable added in v0.6.3

type Resetable interface {
	Reset()
}

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 Tester added in v0.6.3

type Tester[R any] struct {
	T       *testing.T
	SetupFn func()
	ContextFn
	Executor failsafe.Executor[R]
	// contains filtered or unexported fields
}

func Test added in v0.6.3

func Test[R any](t *testing.T) *Tester[R]

func (*Tester[R]) AssertFailure added in v0.6.3

func (t *Tester[R]) AssertFailure(expectedAttempts int, expectedExecutions int, expectedError error, then ...func())

func (*Tester[R]) AssertFailureAs added in v0.6.7

func (t *Tester[R]) AssertFailureAs(expectedAttempts int, expectedExecutions int, expectedError error, then ...func())

func (*Tester[R]) AssertSuccess added in v0.6.3

func (t *Tester[R]) AssertSuccess(expectedAttempts int, expectedExecutions int, expectedResult R, then ...func())

func (*Tester[R]) AssertSuccessError added in v0.6.3

func (t *Tester[R]) AssertSuccessError(expectedAttempts int, expectedExecutions int, expectedError error, then ...func())

func (*Tester[R]) Context added in v0.6.3

func (t *Tester[R]) Context(fn func() context.Context) *Tester[R]

func (*Tester[R]) Get added in v0.6.3

func (t *Tester[R]) Get(when WhenGet[R]) *Tester[R]

func (*Tester[R]) Reset added in v0.6.3

func (t *Tester[R]) Reset(stats ...Resetable) *Tester[R]

func (*Tester[R]) Run added in v0.6.3

func (t *Tester[R]) Run(when WhenRun[R]) *Tester[R]

func (*Tester[R]) Setup added in v0.6.3

func (t *Tester[R]) Setup(fn func()) *Tester[R]

func (*Tester[R]) With added in v0.6.3

func (t *Tester[R]) With(policies ...failsafe.Policy[R]) *Tester[R]

func (*Tester[R]) WithExecutor added in v0.6.3

func (t *Tester[R]) WithExecutor(executor failsafe.Executor[R]) *Tester[R]

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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