test

package
v0.0.0-...-898eaab Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package test contains internal testing utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expect

func Expect[T any](
	t FailerT,
	failMessage string,
	got, want T,
	options ...cmp.Option,
)

Expect compares two values and fails the test if they are different.

func ExpectChannelToBlockForDuration

func ExpectChannelToBlockForDuration[T any](
	t TestingT,
	d time.Duration,
	ch <-chan T,
	options ...cmp.Option,
)

ExpectChannelToBlockForDuration expects reading from the channel to block until the given duration elapses.

func ExpectChannelToClose

func ExpectChannelToClose[T any](
	t TestingT,
	ch <-chan T,
	options ...cmp.Option,
)

ExpectChannelToClose waits until a channel is closed.

func ExpectChannelToReceive

func ExpectChannelToReceive[T any](
	t FailerT,
	ch <-chan T,
	want T,
	options ...cmp.Option,
) (got T)

ExpectChannelToReceive waits until a value is received from a channel and then compares it to the expected value.

func ExpectChannelWouldBlock

func ExpectChannelWouldBlock[T any](
	t TestingT,
	ch <-chan T,
	options ...cmp.Option,
)

ExpectChannelWouldBlock expects reading from the channel would block.

func FailAfterJournalAppend

func FailAfterJournalAppend[T proto.Message](
	s *memoryjournal.BinaryStore,
	name string,
	pred func(T) bool,
	err error,
)

FailAfterJournalAppend configures the journal with the given name to return an error on the next call to Append() with a record that satisifies the given predicate function.

The error is returned after the append is actually performed.

func FailAfterKeyspaceSet

func FailAfterKeyspaceSet(
	s *memorykv.BinaryStore,
	name string,
	pred func(k, v []byte) bool,
)

FailAfterKeyspaceSet configures the keyspace with the given name to return an error on the next call to Set() with a key/value pair that satisifies the given predicate function.

The error is returned after the set is actually performed.

func FailBeforeJournalAppend

func FailBeforeJournalAppend[T proto.Message](
	s *memoryjournal.BinaryStore,
	name string,
	pred func(T) bool,
	err error,
)

FailBeforeJournalAppend configures the journal with the given name to return an error on the next call to Append() with a record that satisifies the given predicate function.

The error is returned before the append is actually performed.

func FailBeforeKeyspaceSet

func FailBeforeKeyspaceSet(
	s *memorykv.BinaryStore,
	name string,
	pred func(k, v []byte) bool,
)

FailBeforeKeyspaceSet configures the keyspace with the given name to return an error on the next call to Set() with a key/value pair that satisifies the given predicate function.

The error is returned before the set is actually performed.

func FailOnJournalOpen

func FailOnJournalOpen(
	s *memoryjournal.BinaryStore,
	name string,
	err error,
)

FailOnJournalOpen configures the journal with the given name to return an error on the next call to Open().

func FailOnce

func FailOnce(err error) func() error

FailOnce returns a function that returns the given error once, and then returns nil on subsequent calls.

Types

type Context

type Context interface {
	context.Context
	TestingT
}

Context is a context that is also a TestingT.

func WithContext

func WithContext(t TestingT) Context

WithContext returns a context that is bound to the lifetime of the test.

type FailerT

type FailerT interface {
	Helper()
	Log(...any)
	Logf(string, ...any)
	Fatal(...any)
	Fatalf(string, ...any)
	Error(...any)
	Errorf(string, ...any)
}

FailerT is the subset of the testing.TB interface that is used by parts of this package that only need to cause tests to fail.

type Task

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

Task represents a function running in the background.

func (*Task) Done

func (t *Task) Done() <-chan struct{}

Done returns a channel that is closed when the function returns.

func (*Task) Err

func (t *Task) Err() error

Err returns the error returned by the function.

It panics if the function has not yet returned.

func (*Task) Stop

func (t *Task) Stop()

Stop cancels the context passed to the function.

func (*Task) StopAndWait

func (t *Task) StopAndWait()

StopAndWait cancels the context passed to the function and waits for it to return.

If it returns an error, the test fails.

func (*Task) WaitForSuccess

func (t *Task) WaitForSuccess()

WaitForSuccess waits for the function to return successfully.

If it returns an error, the test fails.

func (*Task) WaitUntilStopped

func (t *Task) WaitUntilStopped()

WaitUntilStopped waits for the function to be explicitly stopped

If it returns an error, the test fails.

type TaskRunner

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

TaskRunner launches a task in the background.

func RunInBackground

func RunInBackground(
	t *testing.T,
	name string,
	fn func(ctx context.Context) error,
) TaskRunner

RunInBackground returns a TaskRunner that executes fn in its own goroutine.

func (TaskRunner) BeforeTestEnds

func (r TaskRunner) BeforeTestEnds() *Task

BeforeTestEnds executes the task in its own goroutine with the expectation that it will return successfully before the test ends.

func (TaskRunner) FailBeforeTestEnds

func (r TaskRunner) FailBeforeTestEnds() *Task

FailBeforeTestEnds executes the task in its own goroutine with the expectation that it will return an error before the test ends.

func (TaskRunner) RepeatedlyUntilStopped

func (r TaskRunner) RepeatedlyUntilStopped() *Task

RepeatedlyUntilStopped executes the task in its own goroutine, restarting it if it returns before it is stopped explicitly or the test ends.

func (TaskRunner) RepeatedlyUntilSuccess

func (r TaskRunner) RepeatedlyUntilSuccess() *Task

RepeatedlyUntilSuccess executes the task in its own goroutine, restarting it if it returns an error before the test ends.

func (TaskRunner) UntilStopped

func (r TaskRunner) UntilStopped() *Task

UntilStopped executes the task in its own goroutine with the expectation that it will not return until it is stopped explicitly, or the test ends.

func (TaskRunner) UntilTestEnds

func (r TaskRunner) UntilTestEnds() *Task

UntilTestEnds executes the task in its own goroutine with the expectation that it will not return before the test ends.

type TestingT

type TestingT interface {
	FailerT

	Helper()
	Cleanup(func())
}

TestingT is the subset of the testing.TB interface that is used by this package.

Jump to

Keyboard shortcuts

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