testlib

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 14 Imported by: 11

Documentation

Overview

Package testlib is the library for creating and running unit tests.

The package defines a test driver (TestingServer) that can be invoked with the created unit tests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotEnoughReplicas occurs when creating a partition with a total size greater than the existing number of replicas
	ErrNotEnoughReplicas = errors.New("not enough replicas")
	// ErrSizeLabelsMismatch occurs when the number of labels does not match the number of partitions
	ErrSizeLabelsMismatch = errors.New("not enough labels")
)

Functions

func IsMessageAcrossPartition added in v0.2.1

func IsMessageAcrossPartition() sm.Condition

IsMessageAcrossPartition condition returns true when the event represents a message between replicas of different partitions.

func IsMessageFromPart added in v0.1.1

func IsMessageFromPart(partLabel string) sm.Condition

IsMessageFromPart condition returns true when message is from a replica that belongs to the specified part.

func IsMessageWithinPartition added in v0.2.1

func IsMessageWithinPartition() sm.Condition

IsMessageWithinPartition condition returns true when the event represents a message between two replicas of the same partition.

func NewTestDriver added in v0.1.1

func NewTestDriver(ctx *context.RootContext, testcase *TestCase) *testCaseDriver

Types

type Action

type Action func(*types.Event, *Context) []*types.Message

Action is used to specify the consequence in the `If().Then()` handler

func DeliverAllFromSet added in v0.2.0

func DeliverAllFromSet(s *sm.SetWrapper) Action

DeliverAll returns an action which inturn returns all the messages in the message set and removes the messages from the set.

func DeliverMessage

func DeliverMessage() Action

DeliverMessage returns the message if the event is a message send event

func DropMessage

func DropMessage() Action

DropMessage returns an empty list of messages

func IncrCounter added in v0.2.0

func IncrCounter(c *sm.CountWrapper) Action

IncrCounter returns an action which increments the counter value

func OnceAction added in v0.1.1

func OnceAction(name string, action Action) Action

func RecordMessageAs

func RecordMessageAs(label string) Action

RecordMessageAs returns an action. If the event is a message send or receive, the message is recorded in context with the label as reference

func StoreInSet added in v0.2.0

func StoreInSet(s *sm.SetWrapper) Action

Store returns an action. If the event is a message send or receive, the action adds the message to the message set

type Context

type Context struct {
	*sm.Context
	// contains filtered or unexported fields
}

Context struct is passed to the calls of StateAction and Condition encapsulates all information needed by the StateAction and Condition to function

func NewContext added in v0.2.0

func NewContext(c *context.RootContext, testcase *TestCase) *Context

NewContext instantiates a Context from the RootContext

func NewContextFrom added in v0.2.0

func NewContextFrom(sm *sm.Context, testcase *TestCase) *Context

NewContextFrom instantiates a context from the specified state machine context and testcase.

func (*Context) Abort

func (c *Context) Abort()

Abort stops the execution of the testcase

func (*Context) CreatePartition added in v0.1.1

func (c *Context) CreatePartition(sizes []int, labels []string)

CreatePartition creates a new partition with the specified sizes and labels

func (*Context) EndTestCase

func (c *Context) EndTestCase()

Ends the testcase without failing. The assertion will determine the success of the testcase

func (*Context) Log

func (c *Context) Log(keyvals map[string]string)

Log adds a report log for the current testcase

func (*Context) NewMessage

func (c *Context) NewMessage(cur *types.Message, data []byte, pMsg types.ParsedMessage) *types.Message

NewMessage crafts a new message with a new ID The current message contents are replaced with `data`

type FilterFunc

type FilterFunc func(*types.Event, *Context) ([]*types.Message, bool)

FilterFunc type to define a conditional handler returns false in the second return value if the handler is not concerned about the event

func IsolateNode added in v0.2.3

func IsolateNode(replica types.ReplicaID) FilterFunc

IsolateNode is a filter that drops all messages from and to the specified replica.

func NewStateMachineHandler

func NewStateMachineHandler(stateMachine *sm.StateMachine) FilterFunc

NewStateMachineHandler returns a HandlerFunc that encodes the execution logic of the StateMachine For every invocation of the handler, internal a state machine step is executed which may or may not transition. If the StateMachine transitions to FailureState, the handler aborts the test case

type FilterSet

type FilterSet struct {
	Filters       []FilterFunc
	DefaultFilter FilterFunc
	// contains filtered or unexported fields
}

FilterSet implements Handler Executes handlers in the specified order until the event is handled If no handler handles the event then the default handler is called

func NewFilterSet

func NewFilterSet(opts ...FilterSetOption) *FilterSet

NewFilterSet creates a new cascade handler with the specified state machine and options

func (*FilterSet) AddFilter

func (c *FilterSet) AddFilter(h FilterFunc)

AddFilter adds a handler to the cascade

func (*FilterSet) Stats added in v0.2.7

func (c *FilterSet) Stats() *FilterSetStats

Stats returns the stats

type FilterSetOption

type FilterSetOption func(*FilterSet)

FilterSetOption changes the parameters of the HandlerCascade

func WithDefault

func WithDefault(d FilterFunc) FilterSetOption

WithDefault changes the HandlerCascade default handler

type FilterSetStats added in v0.2.7

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

FilterSetStats keeps a count of the number of times a filter condition was satisfied for a given filter The filter index is the order in which the filter is specified.

func (*FilterSetStats) MarshalJSON added in v0.2.7

func (s *FilterSetStats) MarshalJSON() ([]byte, error)

func (*FilterSetStats) String added in v0.2.7

func (s *FilterSetStats) String() string

type IfThenHandler

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

IfThenHandler struct is used to wrap the attributes of the `If().Then()` handler

func If

func If(cond sm.Condition) *IfThenHandler

If creates a IfThenHandler with the specified condition

func (*IfThenHandler) Then

func (i *IfThenHandler) Then(action Action, rest ...Action) FilterFunc

Then returns a HandlerFunc which encodes the `If().Then()` semantics. Accepts actions as arguments

type Partition added in v0.2.1

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

Partition represents a logical partition between the replicas Partition can be used in filters and conditions to decide message delivery

func NewRandomPartition added in v0.2.2

func NewRandomPartition(sizes []int, labels []string) (*Partition, error)

NewRandomPartition creates a partition where replicas are assigned to partitions randomly

func (*Partition) GetPartLabel added in v0.2.1

func (p *Partition) GetPartLabel(replica types.ReplicaID) (string, bool)

GetPartLabel returns the part label the replica belongs to (if there is one), the second return value is false otherwise

func (*Partition) InPart added in v0.2.1

func (p *Partition) InPart(replica types.ReplicaID, partLabel string) bool

InPart returns true when the replica belongs to the specified part

func (*Partition) Setup added in v0.2.1

func (p *Partition) Setup(ctx *Context) error

Setup populates the partition

func (*Partition) String added in v0.2.1

func (p *Partition) String() string

String serializes the partition, used for logging.

type TestCase

type TestCase struct {
	// Name name of the testcase
	Name string
	// Timeout maximum duration of the testcase execution
	Timeout time.Duration
	// Setup function called prior to initiation of the execution
	Setup func(*Context) error

	// Cascade instance of *HandlerCascade
	Cascade *FilterSet
	// StateMachine instance of *StateMachine to assert a property
	StateMachine *sm.StateMachine

	// Logger to log information
	Logger *log.Logger
	// contains filtered or unexported fields
}

TestCase represents a unit test case

func NewTestCase

func NewTestCase(name string, timeout time.Duration, sm *sm.StateMachine, cascade *FilterSet) *TestCase

NewTestCase instantiates a TestCase based on the parameters specified The new testcase has three states by default. - Start state where the execution starts from - Fail state that can be used to fail the testcase - Success state that can be used to indicate a success of the testcase

func (*TestCase) Abort

func (t *TestCase) Abort()

Abort the testcase

func (*TestCase) End

func (t *TestCase) End()

End the testcase

func (*TestCase) Reset added in v0.2.7

func (t *TestCase) Reset()

Reset clears the stats of the filter set and allows running the testcase again

func (*TestCase) SetupFunc

func (t *TestCase) SetupFunc(setupFunc func(*Context) error)

SetupFunc can be used to set the setup function

func (*TestCase) Step added in v0.2.0

func (t *TestCase) Step(e *types.Event, c *Context) ([]*types.Message, bool)

Step is called to execute a step of the testcase with a new event

type TestingServer

type TestingServer struct {
	*types.BaseService
	// contains filtered or unexported fields
}

TestingServer is used to run the scheduler tool for unit testing

func NewTestingServer

func NewTestingServer(config *config.Config, messageParser types.MessageParser, testcases []*TestCase) (*TestingServer, error)

NewTestingServer instantiates TestingServer testcases are passed as arguments

func (*TestingServer) Done

func (srv *TestingServer) Done() chan string

Done returns the channel which will be closed once all testcases are run

func (*TestingServer) Name

func (srv *TestingServer) Name() string

Name implements DashboardRouter

func (*TestingServer) SetupRouter

func (srv *TestingServer) SetupRouter(router *gin.RouterGroup)

SetupRouter for setting up the dashboard routes implements DashboardRouter

func (*TestingServer) Start

func (srv *TestingServer) Start()

Start starts the TestingServer and implements Service

func (*TestingServer) Stop

func (srv *TestingServer) Stop()

Stop stops the TestingServer and implements Service

Jump to

Keyboard shortcuts

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