testutil

package
v2.0.0-rc7 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2023 License: Apache-2.0, MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const TestVoucherType = datatransfer.TypeIdentifier("TestVoucher")

Variables

This section is empty.

Functions

func AllSelector

func AllSelector() datamodel.Node

AllSelector just returns a new instance of a "whole dag selector"

func AssertEqualSelector

func AssertEqualSelector(t *testing.T, expectedRequest datatransfer.Request, request datatransfer.Request)

AssertEqualSelector asserts two requests have the same valid selector

func AssertEqualTestVoucher

func AssertEqualTestVoucher(t *testing.T, expectedRequest datatransfer.Request, request datatransfer.Request)

AssertEqualTestVoucher asserts that two requests have the same fake data transfer voucher

func AssertEqualTestVoucherResult

func AssertEqualTestVoucherResult(t *testing.T, expectedResponse datatransfer.Response, response datatransfer.Response)

AssertEqualTestVoucherResult asserts that two responses have the same fake data transfer voucher result

func AssertTestVoucher

func AssertTestVoucher(t *testing.T, request datatransfer.Request, expected datatransfer.TypedVoucher)

AssertTestVoucher asserts that a data transfer requests contains the expected fake data transfer voucher type

func AssertTestVoucherResult

func AssertTestVoucherResult(t *testing.T, response datatransfer.Response, expected datatransfer.TypedVoucher)

AssertTestVoucherResult asserts that a data transfer response contains the expected fake data transfer voucher result type

func AttributeValueInTraceSpan

func AttributeValueInTraceSpan(t *testing.T, stub tracetest.SpanStub, attributeName string) attribute.Value

AttributeValueInTraceSpan is a test helper that asserts that at a span contains an attribute with the name provided, and returns the value of that attribute for further inspection.

func ContainsBlock

func ContainsBlock(blks []blocks.Block, block blocks.Block) bool

ContainsBlock returns true if a block is found n a list of blocks

func ContainsPeer

func ContainsPeer(peers []peer.ID, p peer.ID) bool

ContainsPeer returns true if a peer is found n a list of peers.

func EventInTraceSpan

func EventInTraceSpan(t *testing.T, stub tracetest.SpanStub, eventName string) trace.Event

EventInTraceSpan is a test helper that asserts that at a span contains an event with the name provided, and returns the value of that event for further inspection.

func GenerateBlocksOfSize

func GenerateBlocksOfSize(n int, size int64) []blocks.Block

GenerateBlocksOfSize generates a series of blocks of the given byte size

func GenerateCids

func GenerateCids(n int) []cid.Cid

GenerateCids produces n content identifiers.

func GeneratePeers

func GeneratePeers(n int) []peer.ID

GeneratePeers creates n peer ids.

func IndexOf

func IndexOf(blks []blocks.Block, c cid.Cid) int

IndexOf returns the index of a given cid in an array of blocks

func NewDTRequest

func NewDTRequest(t *testing.T, transferID datatransfer.TransferID) datatransfer.Request

NewDTRequest makes a new DT Request message

func NewDTResponse

func NewDTResponse(t *testing.T, transferID datatransfer.TransferID) datatransfer.Response

NewDTResponse makes a new DT Request message

func NewTestTypedVoucher

func NewTestTypedVoucher() datatransfer.TypedVoucher

func NewTestTypedVoucherWith

func NewTestTypedVoucherWith(data string) datatransfer.TypedVoucher

func NewTestVoucher

func NewTestVoucher() datamodel.Node

NewTestVoucher returns a fake voucher with random data

func NewTestVoucherWith

func NewTestVoucherWith(data string) datamodel.Node

NewTestVoucher returns a fake voucher with random data

func RandomBytes

func RandomBytes(n int64) []byte

RandomBytes returns a byte array of the given size with random values.

func RecordCustomizedTransfer

func RecordCustomizedTransfer() datatransfer.TransportOption

func RepeatTraceStrings

func RepeatTraceStrings(tmpl string, count int) []string

func SetupTracing

func SetupTracing(ctx context.Context) (context.Context, func(t *testing.T) *Collector)

SetupTracing returns a test helper that can will collect all spans within a Collector. The returned helper function should be called at the point in a test where the spans are ready to be analyzed. Any spans not properly completed at that point won't be represented in the Collector.

func StartAndWaitForReady

func StartAndWaitForReady(ctx context.Context, t *testing.T, manager datatransfer.Manager)

StartAndWaitForReady is a utility function to start a module and verify it reaches the ready state

Types

type Collector

type Collector struct {
	Spans tracetest.SpanStubs
}

Collector can be used as a trace batcher to provide traces to, we collect individual spans and then extract useful data out of them for test assertions

func (*Collector) ExportSpans

func (c *Collector) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error

ExportSpans receives the ReadOnlySpans from the batch provider

func (Collector) FindParentSpans

func (c Collector) FindParentSpans() tracetest.SpanStubs

FindParentSpans finds spans that have no parents, they are at the top any stack.

func (Collector) FindSpanByTraceString

func (c Collector) FindSpanByTraceString(trace string) *tracetest.SpanStub

FindSpanByTraceString is similar to FindSpans but returns a single span identified by its trace string as described in TracesToStrings. Note that this string can also be a partial of a complete trace, e.g. just `"foo(0)"` without any children to fetch the parent span.

func (Collector) FindSpans

func (c Collector) FindSpans(name string) tracetest.SpanStubs

FindSpans returns a list of spans by their name

func (Collector) FindSpansWithParent

func (c Collector) FindSpansWithParent(stub tracetest.SpanStub) tracetest.SpanStubs

FindSpansWithParent finds spans that are children of the provided span.

func (*Collector) Shutdown

func (c *Collector) Shutdown(ctx context.Context) error

Shutdown is a noop, we don't need to do anything fancy

func (Collector) SingleExceptionEvent

func (c Collector) SingleExceptionEvent(t *testing.T, trace string, typeRe string, messageRe string, errorCode bool)

SingleExceptionEvent is a test helper that asserts that a span, identified by a trace string (see TracesToStrings) contains a single exception, identified by the type (regexp) and message (regexp). If errorCode is true, then we also assert that the span has an error status code, with the same message (regexp)

func (Collector) TracesToStrings

func (c Collector) TracesToStrings(maxDepth int) []string

TracesToString returns an array of all traces represented as strings with each span in the trace identified by name and its number (within the parent span) in parens, separated by a '->'. e.g. `"foo(0)->bar(0)","foo(0)->bar(1)"`

type ExceptionEvent

type ExceptionEvent struct {
	Type    string
	Message string
}

ExceptionEvent is a simplistic string form representation of an event

func EventAsException

func EventAsException(t *testing.T, evt trace.Event) ExceptionEvent

EventAsException is a test helper that converts a trace event to an ExceptionEvent for easier inspection.

type FakeNetwork

type FakeNetwork struct {
	PeerID       peer.ID
	SentMessages []FakeSentMessage
	Delegate     network.Receiver
}

FakeNetwork is a network that satisfies the DataTransferNetwork interface but does not actually do anything

func NewFakeNetwork

func NewFakeNetwork(id peer.ID) *FakeNetwork

NewFakeNetwork returns a new fake data transfer network instance

func (*FakeNetwork) ConnectTo

func (fn *FakeNetwork) ConnectTo(_ context.Context, _ peer.ID) error

ConnectTo establishes a connection to the given peer

func (*FakeNetwork) ConnectWithRetry

func (fn *FakeNetwork) ConnectWithRetry(ctx context.Context, p peer.ID) error

func (*FakeNetwork) ID

func (fn *FakeNetwork) ID() peer.ID

ID returns a stubbed id for host of this network

func (*FakeNetwork) Protect

func (fn *FakeNetwork) Protect(id peer.ID, tag string)

Protect does nothing on the fake network

func (*FakeNetwork) Protocol

func (fn *FakeNetwork) Protocol(ctx context.Context, id peer.ID) (protocol.ID, error)

func (*FakeNetwork) SendMessage

func (fn *FakeNetwork) SendMessage(ctx context.Context, p peer.ID, m datatransfer.Message) error

SendMessage sends a GraphSync message to a peer.

func (*FakeNetwork) SetDelegate

func (fn *FakeNetwork) SetDelegate(receiver network.Receiver)

SetDelegate registers the Reciver to handle messages received from the network.

func (*FakeNetwork) Unprotect

func (fn *FakeNetwork) Unprotect(id peer.ID, tag string) bool

Unprotect does nothing on the fake network

type FakeSentMessage

type FakeSentMessage struct {
	PeerID  peer.ID
	Message datatransfer.Message
}

FakeSentMessage is a recording of a message sent on the FakeNetwork

type FakeTransport

type FakeTransport struct {
	OpenedChannels      []OpenedChannel
	OpenChannelErr      error
	ClosedChannels      []datatransfer.ChannelID
	CloseChannelErr     error
	PausedChannels      []datatransfer.ChannelID
	PauseChannelErr     error
	ResumedChannels     []ResumedChannel
	ResumeChannelErr    error
	CleanedUpChannels   []datatransfer.ChannelID
	CustomizedTransfers []datatransfer.ChannelID
	EventHandler        datatransfer.EventsHandler
	SetEventHandlerErr  error
}

FakeTransport is a fake transport with mocked results

func NewFakeTransport

func NewFakeTransport() *FakeTransport

NewFakeTransport returns a new instance of FakeTransport

func (*FakeTransport) CleanupChannel

func (ft *FakeTransport) CleanupChannel(chid datatransfer.ChannelID)

CleanupChannel cleans up the given channel

func (*FakeTransport) CloseChannel

func (ft *FakeTransport) CloseChannel(ctx context.Context, chid datatransfer.ChannelID) error

CloseChannel closes the given channel

func (*FakeTransport) OpenChannel

func (ft *FakeTransport) OpenChannel(ctx context.Context, dataSender peer.ID, channelID datatransfer.ChannelID, root ipld.Link, stor datamodel.Node, channel datatransfer.ChannelState, msg datatransfer.Message) error

OpenChannel initiates an outgoing request for the other peer to send data to us on this channel Note: from a data transfer symantic standpoint, it doesn't matter if the request is push or pull -- OpenChannel is called by the party that is intending to receive data

func (*FakeTransport) PauseChannel

func (ft *FakeTransport) PauseChannel(ctx context.Context, chid datatransfer.ChannelID) error

PauseChannel paused the given channel ID

func (*FakeTransport) ResumeChannel

func (ft *FakeTransport) ResumeChannel(ctx context.Context, msg datatransfer.Message, chid datatransfer.ChannelID) error

ResumeChannel resumes the given channel

func (*FakeTransport) SetEventHandler

func (ft *FakeTransport) SetEventHandler(events datatransfer.EventsHandler) error

SetEventHandler sets the handler for events on channels

func (*FakeTransport) Shutdown

func (ft *FakeTransport) Shutdown(ctx context.Context) error

type MockChannelState

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

func NewMockChannelState

func NewMockChannelState(params MockChannelStateParams) *MockChannelState

func (*MockChannelState) BaseCID

func (m *MockChannelState) BaseCID() cid.Cid

func (*MockChannelState) BothPaused

func (m *MockChannelState) BothPaused() bool

func (*MockChannelState) ChannelID

func (m *MockChannelState) ChannelID() datatransfer.ChannelID

func (*MockChannelState) DataLimit

func (m *MockChannelState) DataLimit() uint64

func (*MockChannelState) InitiatorPaused

func (m *MockChannelState) InitiatorPaused() bool

func (*MockChannelState) IsPull

func (m *MockChannelState) IsPull() bool

func (*MockChannelState) LastVoucher

func (m *MockChannelState) LastVoucher() datatransfer.TypedVoucher

func (*MockChannelState) LastVoucherResult

func (m *MockChannelState) LastVoucherResult() datatransfer.TypedVoucher

func (*MockChannelState) Message

func (m *MockChannelState) Message() string

func (*MockChannelState) OtherPeer

func (m *MockChannelState) OtherPeer() peer.ID

func (*MockChannelState) Queued

func (m *MockChannelState) Queued() uint64

func (*MockChannelState) QueuedCidsTotal

func (m *MockChannelState) QueuedCidsTotal() int64

func (*MockChannelState) Received

func (m *MockChannelState) Received() uint64

func (*MockChannelState) ReceivedCidsTotal

func (m *MockChannelState) ReceivedCidsTotal() int64

func (*MockChannelState) Recipient

func (m *MockChannelState) Recipient() peer.ID

func (*MockChannelState) RequiresFinalization

func (m *MockChannelState) RequiresFinalization() bool

func (*MockChannelState) ResponderPaused

func (m *MockChannelState) ResponderPaused() bool

func (*MockChannelState) Selector

func (m *MockChannelState) Selector() datamodel.Node

func (*MockChannelState) SelfPaused

func (m *MockChannelState) SelfPaused() bool

func (*MockChannelState) SelfPeer

func (m *MockChannelState) SelfPeer() peer.ID

func (*MockChannelState) Sender

func (m *MockChannelState) Sender() peer.ID

func (*MockChannelState) Sent

func (m *MockChannelState) Sent() uint64

func (*MockChannelState) SentCidsTotal

func (m *MockChannelState) SentCidsTotal() int64

func (*MockChannelState) SetComplete

func (m *MockChannelState) SetComplete(complete bool)

func (*MockChannelState) SetDataLimit

func (m *MockChannelState) SetDataLimit(dataLimit uint64)

func (*MockChannelState) SetInitiatorPaused

func (m *MockChannelState) SetInitiatorPaused(initiatorPaused bool)

func (*MockChannelState) SetQueued

func (m *MockChannelState) SetQueued(queued uint64)

func (*MockChannelState) SetQueuedCidsTotal

func (m *MockChannelState) SetQueuedCidsTotal(queuedIndex int64)

func (*MockChannelState) SetReceived

func (m *MockChannelState) SetReceived(received uint64)

func (*MockChannelState) SetReceivedCidsTotal

func (m *MockChannelState) SetReceivedCidsTotal(receivedIndex int64)

func (*MockChannelState) SetResponderPaused

func (m *MockChannelState) SetResponderPaused(responderPaused bool)

func (*MockChannelState) SetSent

func (m *MockChannelState) SetSent(sent uint64)

func (*MockChannelState) SetSentCidsTotal

func (m *MockChannelState) SetSentCidsTotal(sentIndex int64)

func (*MockChannelState) Stages

func (*MockChannelState) Status

func (m *MockChannelState) Status() datatransfer.Status

func (*MockChannelState) TotalSize

func (m *MockChannelState) TotalSize() uint64

func (*MockChannelState) TransferID

func (m *MockChannelState) TransferID() datatransfer.TransferID

func (*MockChannelState) Voucher

func (*MockChannelState) VoucherResults

func (m *MockChannelState) VoucherResults() []datatransfer.TypedVoucher

func (*MockChannelState) Vouchers

func (m *MockChannelState) Vouchers() []datatransfer.TypedVoucher

type MockChannelStateParams

type MockChannelStateParams struct {
	ReceivedCidsTotal int64
	SentCidsTotal     int64
	QueuedCidsTotal   int64
	ChannelID         datatransfer.ChannelID
	Queued            uint64
	Sent              uint64
	Received          uint64
	Complete          bool
	BaseCID           cid.Cid
	Selector          ipld.Node
	Voucher           datatransfer.TypedVoucher
	IsPull            bool
	Self              peer.ID
	DataLimit         uint64
	InitiatorPaused   bool
	ResponderPaused   bool
}

type OpenedChannel

type OpenedChannel struct {
	DataSender peer.ID
	ChannelID  datatransfer.ChannelID
	Root       ipld.Link
	Selector   datamodel.Node
	Channel    datatransfer.ChannelState
	Message    datatransfer.Message
}

OpenedChannel records a call to open a channel

type ReceivedRestartValidation

type ReceivedRestartValidation struct {
	ChannelID    datatransfer.ChannelID
	ChannelState datatransfer.ChannelState
}

ReceivedRestartValidation records a call to ValidateRestart

type ReceivedValidation

type ReceivedValidation struct {
	IsPull   bool
	Other    peer.ID
	Voucher  datamodel.Node
	BaseCid  cid.Cid
	Selector datamodel.Node
}

ReceivedValidation records a call to either ValidatePush or ValidatePull

type ResumedChannel

type ResumedChannel struct {
	ChannelID datatransfer.ChannelID
	Message   datatransfer.Message
}

ResumedChannel records a call to resume a channel

type StubbedValidator

type StubbedValidator struct {
	ValidationsReceived   []ReceivedValidation
	RevalidationsReceived []ReceivedRestartValidation
	// contains filtered or unexported fields
}

StubbedValidator is a validator that returns predictable results

func NewStubbedValidator

func NewStubbedValidator() *StubbedValidator

NewStubbedValidator returns a new instance of a stubbed validator

func (*StubbedValidator) ExpectErrorPull

func (sv *StubbedValidator) ExpectErrorPull()

ExpectErrorPull expects ValidatePull to error

func (*StubbedValidator) ExpectErrorPush

func (sv *StubbedValidator) ExpectErrorPush()

ExpectErrorPush expects ValidatePush to error

func (*StubbedValidator) ExpectErrorValidateRestart

func (sv *StubbedValidator) ExpectErrorValidateRestart()

ExpectErrorValidateRestart expects ValidateRestart to error

func (*StubbedValidator) ExpectSuccessPull

func (sv *StubbedValidator) ExpectSuccessPull()

ExpectSuccessPull expects ValidatePull to error

func (*StubbedValidator) ExpectSuccessPush

func (sv *StubbedValidator) ExpectSuccessPush()

ExpectSuccessPush expects ValidatePush to error

func (*StubbedValidator) ExpectSuccessValidateRestart

func (sv *StubbedValidator) ExpectSuccessValidateRestart()

ExpectSuccessValidateRestart expects ValidateRestart to succeed

func (*StubbedValidator) StubErrorPull

func (sv *StubbedValidator) StubErrorPull()

StubErrorPull sets ValidatePull to error

func (*StubbedValidator) StubErrorPush

func (sv *StubbedValidator) StubErrorPush()

StubErrorPush sets ValidatePush to error

func (*StubbedValidator) StubErrorValidateRestart

func (sv *StubbedValidator) StubErrorValidateRestart()

StubErrorValidateRestart sets ValidateRestart to error

func (*StubbedValidator) StubRestartResult

func (sv *StubbedValidator) StubRestartResult(voucherResult datatransfer.ValidationResult)

StubRestartResult returns the given voucher result when a call is made to ValidateRestart

func (*StubbedValidator) StubResult

func (sv *StubbedValidator) StubResult(voucherResult datatransfer.ValidationResult)

StubResult returns thes given voucher result when a validate call is made

func (*StubbedValidator) StubSuccessPull

func (sv *StubbedValidator) StubSuccessPull()

StubSuccessPull sets ValidatePull to succeed

func (*StubbedValidator) StubSuccessPush

func (sv *StubbedValidator) StubSuccessPush()

StubSuccessPush sets ValidatePush to succeed

func (*StubbedValidator) StubSuccessValidateRestart

func (sv *StubbedValidator) StubSuccessValidateRestart()

StubSuccessValidateRestart sets ValidateRestart to succeed

func (*StubbedValidator) ValidatePull

func (sv *StubbedValidator) ValidatePull(
	chid datatransfer.ChannelID,
	receiver peer.ID,
	voucher datamodel.Node,
	baseCid cid.Cid,
	selector datamodel.Node) (datatransfer.ValidationResult, error)

ValidatePull returns a stubbed result for a pull validation

func (*StubbedValidator) ValidatePush

func (sv *StubbedValidator) ValidatePush(
	chid datatransfer.ChannelID,
	sender peer.ID,
	voucher datamodel.Node,
	baseCid cid.Cid,
	selector datamodel.Node) (datatransfer.ValidationResult, error)

ValidatePush returns a stubbed result for a push validation

func (*StubbedValidator) ValidateRestart

func (*StubbedValidator) VerifyExpectations

func (sv *StubbedValidator) VerifyExpectations(t *testing.T)

VerifyExpectations verifies the specified calls were made

Jump to

Keyboard shortcuts

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