testkit

package
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2020 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package testkit extends the in-process transport adapter with Tampering capabilities, intended to be used by system-wide acceptance tests

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ABenchmarkConsensusMessage

func ABenchmarkConsensusMessage(header *gossipmessages.Header) bool

func AConsensusMessage

func AConsensusMessage(header *gossipmessages.Header) bool

func ATransactionRelayMessage

func ATransactionRelayMessage(header *gossipmessages.Header) bool

Types

type HeaderPredicate

type HeaderPredicate func(header *gossipmessages.Header) bool

type LatchingTamper

type LatchingTamper interface {
	Wait()
	Remove()
}

type MessagePredicate

type MessagePredicate func(data *adapter.TransportData) bool

A predicate for matching messages with a certain property

Example (PayloadSize)
aMessageWithPayloadOver := func(maxSizeInBytes int) MessagePredicate {
	return func(data *adapter.TransportData) bool {
		size := 0
		for _, payload := range data.Payloads {
			size += len(payload)
		}

		return size < maxSizeInBytes
	}
}

pred := aMessageWithPayloadOver(100)

printMessage := func(payloads [][]byte) {
	if pred(&adapter.TransportData{Payloads: payloads}) {
		fmt.Println("got message smaller than 100 bytes")
	} else {
		fmt.Println("got message larger than 100 bytes")
	}
}

printMessage([][]byte{make([]byte, 10)})
printMessage([][]byte{make([]byte, 1000)})
Output:

got message smaller than 100 bytes
got message larger than 100 bytes
Example (Sender)
aMessageFrom := func(sender string) MessagePredicate {
	return func(data *adapter.TransportData) bool {
		return string(data.SenderNodeAddress) == sender
	}
}

pred := aMessageFrom("sender1")

printSender := func(sender string) {
	if pred(&adapter.TransportData{SenderNodeAddress: primitives.NodeAddress(sender)}) {
		fmt.Printf("got message from %s\n", sender)
	} else {
		fmt.Println("got message from other sender")
	}
}

printSender("sender1")
printSender("sender3")
Output:

got message from sender1
got message from other sender

func BenchmarkConsensusMessage

func BenchmarkConsensusMessage(messageType consensus.BenchmarkConsensusMessageType) MessagePredicate

func BlockSyncMessage

func BlockSyncMessage(messageType gossipmessages.BlockSyncMessageType) MessagePredicate

func HasHeader

func HasHeader(headerPredicate HeaderPredicate) MessagePredicate

func Not

func Not(predicate MessagePredicate) MessagePredicate

func (MessagePredicate) And

type MockTransportListener

type MockTransportListener struct {
	mock.Mock
}

func ListenTo

func ListenTo(transport adapter.Transport, nodeAddress primitives.NodeAddress) *MockTransportListener

func (*MockTransportListener) BlockReceive

func (l *MockTransportListener) BlockReceive()

func (*MockTransportListener) ExpectNotReceive

func (l *MockTransportListener) ExpectNotReceive()

func (*MockTransportListener) ExpectReceive

func (l *MockTransportListener) ExpectReceive(payloads [][]byte)

func (*MockTransportListener) ExpectTracingContextToPropagate

func (l *MockTransportListener) ExpectTracingContextToPropagate(t *testing.T, originalTracingContext *trace.Context) *mock.MockFunction

func (*MockTransportListener) OnTransportMessageReceived

func (l *MockTransportListener) OnTransportMessageReceived(ctx context.Context, payloads [][]byte)

func (*MockTransportListener) String added in v1.2.1

func (l *MockTransportListener) String() string

func (*MockTransportListener) WhenOnTransportMessageReceived

func (l *MockTransportListener) WhenOnTransportMessageReceived(arg interface{}) *mock.MockFunction

type OngoingTamper

type OngoingTamper interface {
	StopTampering(ctx context.Context)
	// contains filtered or unexported methods
}

type Tamperer

type Tamperer interface {

	// Creates an ongoing tamper which fails messages matching the given predicate, returning an error object to the sender.
	// This is useful to emulate network errors, for instance
	Fail(predicate MessagePredicate) OngoingTamper

	// Creates an ongoing tamper which delays messages matching the given predicate. The messages will be sent when
	// calling OngoingTamper.StopTampering(). This is useful for emulating network congestion or messages arriving in an order
	// different than expected
	Pause(predicate MessagePredicate) OngoingTamper

	// Creates an ongoing tamper which latches the latching goroutine (typically a test) until at least one message
	// matching the given predicate is sent. The latch is created as inactive, and will only block the caller after
	// calling LatchingTamper.Wait(). This is useful to force a test goroutine to block until a certain message has
	// been sent
	LatchOn(predicate MessagePredicate) LatchingTamper

	// Creates an ongoing tamper which duplicates messages matching the given predicate
	Duplicate(predicate MessagePredicate) OngoingTamper

	// Creates an ongoing tamper which corrupts messages matching the given predicate
	Corrupt(predicate MessagePredicate, rand *rand.ControlledRand) OngoingTamper

	// Creates an ongoing tamper which delays (reshuffles) messages matching the given predicate for the specified duration
	Delay(duration func() time.Duration, predicate MessagePredicate) OngoingTamper
}

The TamperingTransport is an in-memory implementation of the Gossip Transport adapter, that adds the ability to tamper with the messages or to synchronize the test's goroutine with the SUT's goroutines

type TamperingTransport

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

func NewTamperingTransport

func NewTamperingTransport(logger log.Logger, nested adapter.InterceptableTransport) *TamperingTransport

func (*TamperingTransport) Corrupt

func (t *TamperingTransport) Corrupt(predicate MessagePredicate, ctrlRand *rand.ControlledRand) OngoingTamper

func (*TamperingTransport) Delay

func (t *TamperingTransport) Delay(duration func() time.Duration, predicate MessagePredicate) OngoingTamper

func (*TamperingTransport) Duplicate

func (t *TamperingTransport) Duplicate(predicate MessagePredicate) OngoingTamper

func (*TamperingTransport) Fail

func (*TamperingTransport) GracefulShutdown added in v1.2.1

func (t *TamperingTransport) GracefulShutdown(shutdownContext context.Context)

func (*TamperingTransport) LatchOn

func (t *TamperingTransport) LatchOn(predicate MessagePredicate) LatchingTamper

func (*TamperingTransport) Pause

func (*TamperingTransport) RegisterListener

func (t *TamperingTransport) RegisterListener(listener adapter.TransportListener, listenerNodeAddress primitives.NodeAddress)

func (*TamperingTransport) Send

func (*TamperingTransport) WaitUntilShutdown added in v1.2.1

func (t *TamperingTransport) WaitUntilShutdown(shutdownContext context.Context)

Jump to

Keyboard shortcuts

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