smtest

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: MIT Imports: 5 Imported by: 0

README

State Machine (sm) Testing

This is an utility library that help developers collect notification events from an state machine in a concurrent-safe way, and help them assert these events match a given predicate and are delivered in a predetermined order.

This utility is used extensively by the Capataz library, but it can also be used by other concurrent algorithms that collect multiple events over callbacks.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertExactMatch

func AssertExactMatch[A fmt.Stringer](t *testing.T, evs []A, preds []EventP[A])

AssertExactMatch is an assertion that checks the input slice of EventP predicate match 1 to 1 with a given list of supervision system events.

func AssertPartialMatch

func AssertPartialMatch[A fmt.Stringer](t *testing.T, evs []A, preds []EventP[A])

AssertPartialMatch is an assertion that matches in order a list of EventP predicates to a list of supervision system events.

The input events need to match in the predicate order, but the events do not need to be a one to one match (e.g. the input events slice length may be bigger than the predicates slice length).

This function is useful when we want to test that some events are present in the expected order. This is useful in test-cases where a supervision system emits an overwhelming number of events.

Types

type AndP

type AndP[A any] struct {
	Preds []EventP[A]
}

AndP is a predicate that builds the conjunction of a group EventP predicates (e.g. join EventP predicates with &&)

func (AndP[A]) Call

func (p AndP[A]) Call(ev A) bool

Call will try and verify that all it's grouped predicates return true, if any returns false, this predicate function will return false

func (AndP[A]) String

func (p AndP[A]) String() string

type EventIterator

type EventIterator[A any] struct {
	// contains filtered or unexported fields
}

EventIterator represents a single iteration over the list of events that have been collected by the EventManager that created it.

func (*EventIterator[A]) TakeTill

func (ei *EventIterator[A]) TakeTill(pred EventP[A]) []A

TakeTill takes all the events that have been collected since the current index until the given predicate returns true

func (*EventIterator[A]) WaitTill

func (ei *EventIterator[A]) WaitTill(pred EventP[A])

WaitTill blocks until an event from the supervision system returns true for the given predicate

type EventManager

type EventManager[A any] struct {
	// contains filtered or unexported fields
}

EventManager provides an API that allows to block a goroutine for particular events in a test system

func NewEventManager

func NewEventManager[A any]() EventManager[A]

NewEventManager returns an EventManager instance that can be used to wait for events to happen on the observed supervision system

func (EventManager[A]) EventCollector

func (em EventManager[A]) EventCollector(ctx context.Context) func(ev A)

EventCollector is used as an event notifier of a supervision system

func (EventManager[A]) GetEventIx

func (em EventManager[A]) GetEventIx(evIx int) (A, bool)

GetEventIx returns the nth event that got emitted by a supervision system, if the given index is greater than the event buffer length, this function will wait until that index is reached. If the index is never reached, the second return value will be false.

func (EventManager[A]) Iterator

func (em EventManager[A]) Iterator() EventIterator[A]

Iterator returns an iterator over the collected events. This iterator will block waiting for new events

func (EventManager[A]) Snapshot

func (em EventManager[A]) Snapshot() []A

Snapshot returns all the events that this EventManager has collected from the supervision system

func (EventManager[A]) StartCollector

func (em EventManager[A]) StartCollector(ctx context.Context)

StartCollector starts the goroutine that reads the evCh that gets filled by the supervision system

type EventP

type EventP[A any] interface {

	// Call will execute the logic of this event predicate
	Call(A) bool

	// Returns an string representation of this event predicate (for debugging
	// purposes)
	String() string
}

EventP represents a predicate function that allows us to assert properties of an Event signaled by the supervision system

type OrP

type OrP[A any] struct {
	Preds []EventP[A]
}

OrP is a predicate that builds the adjunction of a group EventP predicates (e.g. join EventP predicates with ||)

func (OrP[A]) Call

func (p OrP[A]) Call(ev A) bool

Call will try and verify that all it's grouped predicates return true, if any returns false, this predicate function will return false

func (OrP[A]) String

func (p OrP[A]) String() string

Jump to

Keyboard shortcuts

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