testutils

package
v1.41.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package testutils contains testing helpers.

Index

Constants

View Source
const DefaultChanBufferSize = 1

DefaultChanBufferSize is the default buffer size of the underlying channel.

View Source
const DefaultHTTPRequestTimeout = 1 * time.Second

DefaultHTTPRequestTimeout is the default timeout value for the amount of time this client waits for a response to be pushed on RespChan before it fails the Do() call.

Variables

This section is empty.

Functions

func MarshalAny

func MarshalAny(m proto.Message) *anypb.Any

MarshalAny is a convenience function to marshal protobuf messages into any protos. It will panic if the marshaling fails.

func StatusErrEqual

func StatusErrEqual(err1, err2 error) bool

StatusErrEqual returns true iff both err1 and err2 wrap status.Status errors and their underlying status protos are equal.

Types

type Channel

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

Channel wraps a generic channel and provides a timed receive operation.

func NewChannel

func NewChannel() *Channel

NewChannel returns a new Channel.

func NewChannelWithSize

func NewChannelWithSize(bufSize int) *Channel

NewChannelWithSize returns a new Channel with a buffer of bufSize.

func (*Channel) Receive

func (c *Channel) Receive(ctx context.Context) (interface{}, error)

Receive returns the value received on the underlying channel, or the error returned by ctx if it is closed or cancelled.

func (*Channel) ReceiveOrFail

func (c *Channel) ReceiveOrFail() (interface{}, bool)

ReceiveOrFail returns the value on the underlying channel and true, or nil and false if the channel was empty.

func (*Channel) Replace

func (c *Channel) Replace(value interface{})

Replace clears the value on the underlying channel, and sends the new value.

It's expected to be used with a size-1 channel, to only keep the most up-to-date item. This method is inherently racy when invoked concurrently from multiple goroutines.

func (*Channel) Send

func (c *Channel) Send(value interface{})

Send sends value on the underlying channel.

func (*Channel) SendContext

func (c *Channel) SendContext(ctx context.Context, value interface{}) error

SendContext sends value on the underlying channel, or returns an error if the context expires.

func (*Channel) SendOrFail

func (c *Channel) SendOrFail(value interface{}) bool

SendOrFail attempts to send value on the underlying channel. Returns true if successful or false if the channel was full.

type FakeHTTPClient

type FakeHTTPClient struct {
	// ReqChan exposes the HTTP.Request made by the code under test.
	ReqChan *Channel
	// RespChan is a channel on which this fake client accepts responses to be
	// sent to the code under test.
	RespChan *Channel
	// Err, if set, is returned by Do().
	Err error
	// RecvTimeout is the amount of the time this client waits for a response to
	// be pushed on RespChan before it fails the Do() call. If this field is
	// left unspecified, DefaultHTTPRequestTimeout is used.
	RecvTimeout time.Duration
}

FakeHTTPClient helps mock out HTTP calls made by the code under test. It makes HTTP requests made by the code under test available through a channel, and makes it possible to inject various responses.

func (*FakeHTTPClient) Do

func (fc *FakeHTTPClient) Do(req *http.Request) (*http.Response, error)

Do pushes req on ReqChan and returns the response available on RespChan.

type PipeListener

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

PipeListener is a listener with an unbuffered pipe. Each write will complete only once the other side reads. It should only be created using NewPipeListener.

func NewPipeListener

func NewPipeListener() *PipeListener

NewPipeListener creates a new pipe listener.

func (*PipeListener) Accept

func (p *PipeListener) Accept() (net.Conn, error)

Accept accepts a connection.

func (*PipeListener) Addr

func (p *PipeListener) Addr() net.Addr

Addr returns a pipe addr.

func (*PipeListener) Close

func (p *PipeListener) Close() error

Close closes the listener.

func (*PipeListener) Dialer

func (p *PipeListener) Dialer() func(string, time.Duration) (net.Conn, error)

Dialer dials a connection.

Jump to

Keyboard shortcuts

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