testutil

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: Apache-2.0, MIT Imports: 38 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertChannelEmpty

func AssertChannelEmpty(t testing.TB, channel interface{}, errorMessage string)

AssertChannelEmpty verifies that a channel has no value currently

func AssertContainsBlock

func AssertContainsBlock(t testing.TB, blks []blocks.Block, block blocks.Block)

AssertContainsBlock will fail a test if the block is not in the given block list

func AssertContainsPeer

func AssertContainsPeer(t testing.TB, peers []peer.ID, p peer.ID)

AssertContainsPeer will fail a test if the peer is not in the given peer list

func AssertDoesReceive

func AssertDoesReceive(ctx context.Context, t testing.TB, channel interface{}, errorMessage string)

AssertDoesReceive verifies that a channel returns some value before the given context closes

func AssertDoesReceiveFirst

func AssertDoesReceiveFirst(t testing.TB, channel interface{}, errorMessage string, incorrectChannels ...interface{})

AssertDoesReceiveFirst asserts that the given channel receives a value before any of the other channels specified

func AssertReceive

func AssertReceive(ctx context.Context, t testing.TB, channel interface{}, out interface{}, errorMessage string)

AssertReceive verifies that a channel returns a value before the given context closes, and writes into into out, which should be a pointer to the value type

func AssertReceiveFirst

func AssertReceiveFirst(t testing.TB, channel interface{}, out interface{}, errorMessage string, incorrectChannels ...interface{})

AssertReceiveFirst verifies that a channel returns a value on the specified channel before the other channels, and writes the value into out, which should be a pointer to the value type

func AssertSends

func AssertSends(ctx context.Context, t testing.TB, channel interface{}, in interface{}, errorMessage string)

AssertSends attempts to send the given input value to the given channel before the given context closes

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 CollectErrors

func CollectErrors(ctx context.Context, t *testing.T, errChan <-chan error) []error

CollectErrors is just a utility to convert an error channel into an array.

func CollectResponses

func CollectResponses(ctx context.Context, t testing.TB, responseChan <-chan graphsync.ResponseProgress) []graphsync.ResponseProgress

CollectResponses is just a utility to convert a graphsync response progress channel into an array.

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 NewFakeBlockData

func NewFakeBlockData() graphsync.BlockData

NewFakeBlockData returns a fake block that matches the block data interface

func NewInvalidSelectorSpec

func NewInvalidSelectorSpec() ipld.Node

NewInvalidSelectorSpec returns a spec that will fail when you attempt to validate it on the responder side

func NewTestLink() ipld.Link

NewTestLink returns a randomly generated IPLD Link

func NewTestStore

func NewTestStore(blocksWritten map[ipld.Link][]byte) ipld.LinkSystem

NewTestStore provides a loader and storer for the given in memory link -> byte data map

func NewUnparsableSelectorSpec

func NewUnparsableSelectorSpec() ipld.Node

NewUnparsableSelectorSpec returns a spec that will fail when you attempt to validate it or decompose to a node + selector.

func RandomBytes

func RandomBytes(n int64) []byte

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

func ReadNResponses

func ReadNResponses(ctx context.Context, t testing.TB, responseChan <-chan graphsync.ResponseProgress, count int) []graphsync.ResponseProgress

ReadNResponses does a partial read from a ResponseProgress channel -- up to n values

func RefuteContainsBlock

func RefuteContainsBlock(t testing.TB, blks []blocks.Block, block blocks.Block)

RefuteContainsBlock will fail a test if the block is in the given block list

func RefuteContainsPeer

func RefuteContainsPeer(t testing.TB, peers []peer.ID, p peer.ID)

RefuteContainsPeer will fail a test if the peer is in the given peer list

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 VerifyEmptyErrors

func VerifyEmptyErrors(ctx context.Context, t testing.TB, errChan <-chan error)

VerifyEmptyErrors verifies that no errors were sent over a channel before it was closed

func VerifyEmptyResponse

func VerifyEmptyResponse(ctx context.Context, t testing.TB, responseChan <-chan graphsync.ResponseProgress)

VerifyEmptyResponse verifies that no response progress happened before the channel was closed.

func VerifyHasErrors

func VerifyHasErrors(ctx context.Context, t testing.TB, errChan <-chan error)

VerifyHasErrors verifies that at least one error was sent over a channel

func VerifySingleTerminalError

func VerifySingleTerminalError(ctx context.Context, t testing.TB, errChan <-chan error)

VerifySingleTerminalError verifies that exactly one error was sent over a channel and then the channel was closed.

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() []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 DispatchedEvent

type DispatchedEvent struct {
	Topic notifications.Topic
	Event notifications.Event
}

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 MockPublisher

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

func NewMockPublisher

func NewMockPublisher() *MockPublisher

func (*MockPublisher) AddSubscriber

func (mp *MockPublisher) AddSubscriber(subscriber notifications.Subscriber)

func (*MockPublisher) PublishEvents

func (mp *MockPublisher) PublishEvents(topic notifications.Topic, events []notifications.Event)

type TestBlockChain

type TestBlockChain struct {
	GenisisNode ipld.Node
	GenisisLink ipld.Link
	MiddleNodes []ipld.Node
	MiddleLinks []ipld.Link
	TipNode     ipld.Node
	TipLink     ipld.Link
	// contains filtered or unexported fields
}

TestBlockChain is a simulated data structure similar to a blockchain which graphsync is uniquely suited for

func SetupBlockChain

func SetupBlockChain(
	ctx context.Context,
	t testing.TB,
	lsys ipld.LinkSystem,
	size uint64,
	blockChainLength int) *TestBlockChain

SetupBlockChain creates a new test block chain with the given height

func (*TestBlockChain) AllBlocks

func (tbc *TestBlockChain) AllBlocks() []blocks.Block

AllBlocks returns all blocks for a blockchain

func (*TestBlockChain) Blocks

func (tbc *TestBlockChain) Blocks(from int, to int) []blocks.Block

Blocks Returns the given raw blocks for the block chain for the given range, indexed from the tip

func (*TestBlockChain) Chooser

func (tbc *TestBlockChain) Chooser(ipld.Link, ipld.LinkContext) (ipld.NodePrototype, error)

Chooser is a NodeBuilderChooser function that always returns the block chain

func (*TestBlockChain) LinkTipIndex

func (tbc *TestBlockChain) LinkTipIndex(fromTip int) ipld.Link

LinkTipIndex returns a link to the block at the given index from the tip

func (*TestBlockChain) NodeTipIndex

func (tbc *TestBlockChain) NodeTipIndex(fromTip int) ipld.Node

NodeTipIndex returns the node to the block at the given index from the tip

func (*TestBlockChain) PathTipIndex

func (tbc *TestBlockChain) PathTipIndex(fromTip int) ipld.Path

PathTipIndex returns the path to the block at the given index from the tip

func (*TestBlockChain) RemainderBlocks

func (tbc *TestBlockChain) RemainderBlocks(from int) []blocks.Block

RemainderBlocks returns the remaining blocks for a blockchain, indexed from tip

func (*TestBlockChain) Selector

func (tbc *TestBlockChain) Selector() ipld.Node

Selector returns the selector to recursive traverse the block chain parent links

func (*TestBlockChain) VerifyRemainder

func (tbc *TestBlockChain) VerifyRemainder(ctx context.Context, responseChan <-chan graphsync.ResponseProgress, from int)

VerifyRemainder verifies the given response channel returns the remainder of the chain starting at the nth block from the tip

func (*TestBlockChain) VerifyRemainderSync

func (tbc *TestBlockChain) VerifyRemainderSync(responses []graphsync.ResponseProgress, from int)

VerifyRemainderSync verifies the given set of read responses are the remainder of the chain starting at the nth block from the tip

func (*TestBlockChain) VerifyRemainderWithTypes

func (tbc *TestBlockChain) VerifyRemainderWithTypes(ctx context.Context, responseChan <-chan graphsync.ResponseProgress, from int)

VerifyRemainderWithTypes verifies the given response channel returns the remainder of the chain starting at the nth block from the tip and that the types in the response are the expected types for a block chain

func (*TestBlockChain) VerifyResponseRange

func (tbc *TestBlockChain) VerifyResponseRange(ctx context.Context, responseChan <-chan graphsync.ResponseProgress, from int, to int)

VerifyResponseRange verifies the given response channel returns the given range of respnses, indexed from the tip (with possibly more data left in the channel)

func (*TestBlockChain) VerifyResponseRangeSync

func (tbc *TestBlockChain) VerifyResponseRangeSync(responses []graphsync.ResponseProgress, from int, to int)

VerifyResponseRangeSync verifies given set of read responses match responses for the given range of the blockchain, indexed from the tip (with possibly more data left in the channel)

func (*TestBlockChain) VerifyWholeChain

func (tbc *TestBlockChain) VerifyWholeChain(ctx context.Context, responseChan <-chan graphsync.ResponseProgress)

VerifyWholeChain verifies the given response channel returns the expected responses for the whole chain

func (*TestBlockChain) VerifyWholeChainSync

func (tbc *TestBlockChain) VerifyWholeChainSync(responses []graphsync.ResponseProgress)

VerifyWholeChainSync verifies the given set of read responses are the expected responses for the whole chain

func (*TestBlockChain) VerifyWholeChainWithTypes

func (tbc *TestBlockChain) VerifyWholeChainWithTypes(ctx context.Context, responseChan <-chan graphsync.ResponseProgress)

VerifyWholeChainWithTypes verifies the given response channel returns the expected responses for the whole chain and that the types in the response are the expected types for a block chain

type TestConnManager

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

TestConnManager implements network.ConnManager and allows you to assert behavior

func NewTestConnManager

func NewTestConnManager() *TestConnManager

NewTestConnManager returns a new TestConnManager

func (*TestConnManager) AssertProtected

func (tcm *TestConnManager) AssertProtected(t testing.TB, p peer.ID)

AssertProtected asserts that the connection is protected by at least one tag

func (*TestConnManager) AssertProtectedWithTags

func (tcm *TestConnManager) AssertProtectedWithTags(t testing.TB, p peer.ID, tags ...string)

AssertProtectedWithTags verifies the connection is protected with the given tags at least

func (*TestConnManager) Protect

func (tcm *TestConnManager) Protect(p peer.ID, tag string)

Protect simulates protecting a connection (just records occurence)

func (*TestConnManager) RefuteProtected

func (tcm *TestConnManager) RefuteProtected(t testing.TB, p peer.ID)

RefuteProtected refutes that a connection has been protect

func (*TestConnManager) RefuteProtectedWithTags

func (tcm *TestConnManager) RefuteProtectedWithTags(t testing.TB, p peer.ID, tags ...string)

RefuteProtectedWithTags verifies the connection is not protected with any of the given tags

func (*TestConnManager) Unprotect

func (tcm *TestConnManager) Unprotect(p peer.ID, tag string) bool

Unprotect simulates unprotecting a connection (just records occurence)

type TestIPLDTree

type TestIPLDTree struct {
	Storage           map[ipld.Link][]byte
	LeafAlpha         ipld.Node
	LeafAlphaLnk      ipld.Link
	LeafAlphaBlock    blocks.Block
	LeafBeta          ipld.Node
	LeafBetaLnk       ipld.Link
	LeafBetaBlock     blocks.Block
	MiddleMapNode     ipld.Node
	MiddleMapNodeLnk  ipld.Link
	MiddleMapBlock    blocks.Block
	MiddleListNode    ipld.Node
	MiddleListNodeLnk ipld.Link
	MiddleListBlock   blocks.Block
	RootNode          ipld.Node
	RootNodeLnk       ipld.Link
	RootBlock         blocks.Block
}

TestIPLDTree is a set of IPLD Data that forms a tree spread across some blocks with a serialized in memory representation

func NewTestIPLDTree

func NewTestIPLDTree() TestIPLDTree

NewTestIPLDTree returns a fake tree of nodes, spread across 5 blocks

type TestIdentityDAG

type TestIdentityDAG struct {
	RootLink        ipld.Link
	AllLinks        []ipld.Link
	AllLinksNoIdent []ipld.Link
	// contains filtered or unexported fields
}

func SetupIdentityDAG

func SetupIdentityDAG(
	ctx context.Context,
	t testing.TB,
	lsys ipld.LinkSystem,
) *TestIdentityDAG

func (*TestIdentityDAG) AllBlocks

func (tid *TestIdentityDAG) AllBlocks() []blocks.Block

func (*TestIdentityDAG) VerifyWholeDAG

func (tid *TestIdentityDAG) VerifyWholeDAG(ctx context.Context, responseChan <-chan graphsync.ResponseProgress)

VerifyWholeDAGWithTypes verifies the given response channel returns the expected responses for the whole DAG and that the types in the response are the expected types for the DAG

type TestSubscriber

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

func NewTestSubscriber

func NewTestSubscriber(bufferSize int) *TestSubscriber

func (*TestSubscriber) ExpectCloses

func (ts *TestSubscriber) ExpectCloses(ctx context.Context, t *testing.T, topics []notifications.Topic)

func (*TestSubscriber) ExpectClosesAnyOrder

func (ts *TestSubscriber) ExpectClosesAnyOrder(ctx context.Context, t *testing.T, topics []notifications.Topic)

func (*TestSubscriber) ExpectEvents

func (ts *TestSubscriber) ExpectEvents(ctx context.Context, t *testing.T, events []DispatchedEvent)

func (*TestSubscriber) ExpectEventsAllTopics

func (ts *TestSubscriber) ExpectEventsAllTopics(ctx context.Context, t *testing.T, events []notifications.Event)

func (*TestSubscriber) ExpectNCloses

func (ts *TestSubscriber) ExpectNCloses(ctx context.Context, t *testing.T, n int)

func (*TestSubscriber) NoEventsReceived

func (ts *TestSubscriber) NoEventsReceived(t *testing.T)

func (*TestSubscriber) OnClose

func (ts *TestSubscriber) OnClose(topic notifications.Topic)

func (*TestSubscriber) OnNext

func (ts *TestSubscriber) OnNext(topic notifications.Topic, ev notifications.Event)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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