erltest

package
v0.18.0-rc.6 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

this package contains the TestReceiver which is a process that can have message expectations set on them. These expectations match messages sent to the process inbox and execute a [TestExpectation] function. The function returns true to pass and false to fail the test.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultReceiverTimeout time.Duration = chronos.Dur("10s")
	DefaultWaitTimeout     time.Duration = chronos.Dur("5s")
)

Functions

This section is empty.

Types

type BoolTestCallExpectation added in v0.16.1

type BoolTestCallExpectation func(self erl.PID, from genserver.From, msg any) bool

type BoolTestExpectation added in v0.16.1

type BoolTestExpectation func(self erl.PID, msg any) bool

type ExpectArg added in v0.16.1

type ExpectArg struct {
	// The term that was matched against. A simple type in the case of [reflect.Type], or
	// possibly a string, number, etc.
	Match any
	Msg   any
	// This is only populated for Calls
	From     *genserver.From
	Self     erl.PID
	MsgCount int
	Exp      Expectation
}

type Expectation added in v0.16.1

type Expectation interface {
	Check(arg ExpectArg) (Expectation, *ExpectationFailure)
	Satisfied(testDone bool) bool
	// unique identifier, to simplify nested Expectation registration with the [TestReceiver]
	ID() string
	Name() string
}

type ExpectationFailure

type ExpectationFailure struct {
	// The matching term that led to the ExpectationFailure
	Match any
	// The failed expectation
	Exp Expectation
	// The actual message that matched [Match] and was evaluated by the Expectation
	Msg any
	// Failure message
	Reason string
}

func Fail added in v0.16.1

func Fail(ea ExpectArg, reason string) *ExpectationFailure

func (*ExpectationFailure) String added in v0.16.1

func (ef *ExpectationFailure) String() string

type ReceiverOpt added in v0.16.1

type ReceiverOpt func(ro receiverOptions) receiverOptions

func Name added in v0.18.0

func Name(name string) ReceiverOpt

Set a name for the test receiver, that will be used in log messages

func NoFail added in v0.16.1

func NoFail() ReceiverOpt

Set this if you do not want the TestReceiver to call testing.T.Fail in the [Wait] method.

func Parent added in v0.18.0

func Parent(parent erl.PID) ReceiverOpt

Set a parent for this test process. Defaults to [erl.rootPID]

func ReceiverTimeout added in v0.16.1

func ReceiverTimeout(t time.Duration) ReceiverOpt

Specify how long the test reciever should run for before stopping. this needs to be set otherwise tests will hang until exceptions are matched or the 10min Go default is reached. See DefaultReceiverTimeout

func SetLogger added in v0.18.0

func SetLogger(logger *slog.Logger) ReceiverOpt

XXX: might remove.

func WaitTimeout added in v0.16.1

func WaitTimeout(t time.Duration) ReceiverOpt

Specify how long for [TestReciever.Wait] for all expectations to be met see DefaultWaitTimeout. Will set the receiver timeout to double the wait timeout so there's no need to call ReceiverTimeout in most cases.

type TestDependency added in v0.18.0

type TestDependency interface {
	Pass() (int, bool)
}

used to inject more complex mocks based on a TestReceiver and have them checked when [Wait] is called

type TestReceiver

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

func NewReceiver

func NewReceiver(t *testing.T, opts ...ReceiverOpt) (erl.PID, *TestReceiver)

Creates a new TestReceiver, which is a process that you can set message matching expectations on.

func (*TestReceiver) Expect

func (tr *TestReceiver) Expect(matchTerm any, e Expectation)

Set an expectation that will be matched whenever a [matchTerm] msg type is received.

func (*TestReceiver) ExpectCall added in v0.15.0

func (tr *TestReceiver) ExpectCall(matchTerm any, e Expectation)

This is like [Expect] but is only tested against genserver.CallRequest messages. NOTE: You should use genserver.Reply to send a response to the genserver.From, otherwise the caller will timeout

func (*TestReceiver) ExpectCast added in v0.15.0

func (tr *TestReceiver) ExpectCast(matchTerm any, e Expectation)

This is like [Expect] but is only tested against genserver.CastRequest messages.

func (*TestReceiver) Failures added in v0.16.1

func (tr *TestReceiver) Failures() []*ExpectationFailure

func (*TestReceiver) Join

func (tr *TestReceiver) Join(pid erl.PID, td TestDependency)

func (*TestReceiver) Pass

func (tr *TestReceiver) Pass() (int, bool)

returns the number of failed expectations and whether all expectations have been satisifed. An expectation is not satisfied unless it is invoked in the correct time and order

func (*TestReceiver) Receive

func (tr *TestReceiver) Receive(self erl.PID, inbox <-chan any) error

func (*TestReceiver) Self added in v0.18.0

func (tr *TestReceiver) Self() erl.PID

func (*TestReceiver) StartSupervised added in v0.18.0

func (tr *TestReceiver) StartSupervised(startLink func(self erl.PID) (erl.PID, error)) erl.PID

starts the process via [startLink] with the TestReceiver as the parent. If [startLink] returns an error the test is failed. The process will be synchronously killed when calling [Stop]

func (*TestReceiver) Stop added in v0.15.0

func (tr *TestReceiver) Stop()

will cause the test receiver to exit, sending signals to linked and monitoring processes. This is not needed for normal test cleanup (that is handled via [t.Cleanup()])

func (*TestReceiver) T added in v0.16.1

func (tr *TestReceiver) T() *testing.T

return the *testing.T. Don't use this in situations that could run after a test is failed, the race detector doesn't like that.

func (*TestReceiver) Wait

func (tr *TestReceiver) Wait()

Returns when the WaitTimeout expires or an expectation fails. If at the end of the timeout not all expectations are satisifed, the test is failed. Call this after you have sent your messages and want fail if your expecations don't pass

func (*TestReceiver) WaitOn added in v0.16.1

func (tr *TestReceiver) WaitOn(e ...Expectation)

Register an expectation with this TestReciever. It will be checked when Pass is called (and as a consequnce, cause [Wait] to block until its success)

Directories

Path Synopsis
* This package provides "assertions" for use in [erltest.TestExpectation]s.
* This package provides "assertions" for use in [erltest.TestExpectation]s.

Jump to

Keyboard shortcuts

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