testutils

package
v1.55.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 18 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.

View Source
const TestSubConnsCount = 16

TestSubConnsCount is the number of TestSubConns initialized as part of package init.

Variables

View Source
var ErrTestConstPicker = fmt.Errorf("const picker error")

ErrTestConstPicker is error returned by test const picker.

View Source
var TestSubConns []*TestSubConn

TestSubConns contains a list of SubConns to be used in tests.

Functions

func IsRoundRobin added in v1.43.0

func IsRoundRobin(want []balancer.SubConn, f func() balancer.SubConn) error

IsRoundRobin checks whether f's return value is roundrobin of elements from want. But it doesn't check for the order. Note that want can contain duplicate items, which makes it weight-round-robin.

Step 1. the return values of f should form a permutation of all elements in want, but not necessary in the same order. E.g. if want is {a,a,b}, the check fails if f returns:

  • {a,a,a}: third a is returned before b
  • {a,b,b}: second b is returned before the second a

If error is found in this step, the returned error contains only the first iteration until where it goes wrong.

Step 2. the return values of f should be repetitions of the same permutation. E.g. if want is {a,a,b}, the check failes if f returns:

  • {a,b,a,b,a,a}: though it satisfies step 1, the second iteration is not repeating the first iteration.

If error is found in this step, the returned error contains the first iteration + the second iteration until where it goes wrong.

func LocalTCPListener added in v1.43.0

func LocalTCPListener() (net.Listener, error)

LocalTCPListener returns a net.Listener listening on local address and port.

func MarshalAny added in v1.38.0

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 MustParseURL added in v1.53.0

func MustParseURL(target string) *url.URL

MustParseURL attempts to parse the provided target using url.Parse() and panics if parsing fails.

func NewTestWRR added in v1.43.0

func NewTestWRR() wrr.WRR

NewTestWRR return a WRR for testing. It's deterministic instead of random.

func ParsePort added in v1.55.0

func ParsePort(t *testing.T, addr string) uint32

ParsePort returns the port from the given address string, as a unit32.

func StatusErrEqual added in v1.22.0

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.

func SubConnFromPicker added in v1.51.0

func SubConnFromPicker(p balancer.Picker) func() balancer.SubConn

SubConnFromPicker returns a function which returns a SubConn by calling the Pick() method of the provided picker. There is no caching of SubConns here. Every invocation of the returned function results in a new pick.

Types

type Channel added in v1.30.0

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

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

func NewChannel added in v1.30.0

func NewChannel() *Channel

NewChannel returns a new Channel.

func NewChannelWithSize added in v1.30.0

func NewChannelWithSize(bufSize int) *Channel

NewChannelWithSize returns a new Channel with a buffer of bufSize.

func (*Channel) Receive added in v1.30.0

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 added in v1.34.0

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 added in v1.33.0

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 added in v1.30.0

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

Send sends value on the underlying channel.

func (*Channel) SendContext added in v1.34.0

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 added in v1.34.0

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 ConnWrapper added in v1.46.0

type ConnWrapper struct {
	net.Conn
	CloseCh *Channel
}

ConnWrapper wraps a net.Conn and pushes on a channel when closed.

func (*ConnWrapper) Close added in v1.46.0

func (cw *ConnWrapper) Close() error

Close closes the connection and sends a value on the close channel.

type FakeHTTPClient added in v1.33.0

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 added in v1.33.0

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

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

type ListenerWrapper added in v1.46.0

type ListenerWrapper struct {
	net.Listener
	NewConnCh *Channel
}

ListenerWrapper wraps a net.Listener and the returned net.Conn.

It pushes on a channel whenever it accepts a new connection.

func NewListenerWrapper added in v1.46.0

func NewListenerWrapper(t *testing.T, lis net.Listener) *ListenerWrapper

NewListenerWrapper returns a ListenerWrapper.

func (*ListenerWrapper) Accept added in v1.46.0

func (l *ListenerWrapper) Accept() (net.Conn, error)

Accept wraps the Listener Accept and sends the accepted connection on a channel.

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.

type RestartableListener added in v1.43.0

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

RestartableListener wraps a net.Listener and supports stopping and restarting the latter.

func NewRestartableListener added in v1.43.0

func NewRestartableListener(l net.Listener) *RestartableListener

NewRestartableListener returns a new RestartableListener wrapping l.

func (*RestartableListener) Accept added in v1.43.0

func (l *RestartableListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

If the listener is currently not accepting new connections, because `Stop` was called on it, the connection is immediately closed after accepting without any bytes being sent on it.

func (*RestartableListener) Addr added in v1.43.0

func (l *RestartableListener) Addr() net.Addr

Addr returns the listener's network address.

func (*RestartableListener) Close added in v1.43.0

func (l *RestartableListener) Close() error

Close closes the listener.

func (*RestartableListener) Restart added in v1.43.0

func (l *RestartableListener) Restart()

Restart gets a previously stopped listener to start accepting connections.

func (*RestartableListener) Stop added in v1.43.0

func (l *RestartableListener) Stop()

Stop closes existing connections on the listener and prevents new connections from being accepted.

type TestClientConn added in v1.43.0

type TestClientConn struct {
	NewSubConnAddrsCh      chan []resolver.Address // the last 10 []Address to create subconn.
	NewSubConnCh           chan balancer.SubConn   // the last 10 subconn created.
	RemoveSubConnCh        chan balancer.SubConn   // the last 10 subconn removed.
	UpdateAddressesAddrsCh chan []resolver.Address // last updated address via UpdateAddresses().

	NewPickerCh  chan balancer.Picker            // the last picker updated.
	NewStateCh   chan connectivity.State         // the last state.
	ResolveNowCh chan resolver.ResolveNowOptions // the last ResolveNow().
	// contains filtered or unexported fields
}

TestClientConn is a mock balancer.ClientConn used in tests.

func NewTestClientConn added in v1.43.0

func NewTestClientConn(t *testing.T) *TestClientConn

NewTestClientConn creates a TestClientConn.

func (*TestClientConn) NewSubConn added in v1.43.0

NewSubConn creates a new SubConn.

func (*TestClientConn) RemoveSubConn added in v1.43.0

func (tcc *TestClientConn) RemoveSubConn(sc balancer.SubConn)

RemoveSubConn removes the SubConn.

func (*TestClientConn) ResolveNow added in v1.43.0

func (tcc *TestClientConn) ResolveNow(o resolver.ResolveNowOptions)

ResolveNow panics.

func (*TestClientConn) Target added in v1.43.0

func (tcc *TestClientConn) Target() string

Target panics.

func (*TestClientConn) UpdateAddresses added in v1.43.0

func (tcc *TestClientConn) UpdateAddresses(sc balancer.SubConn, addrs []resolver.Address)

UpdateAddresses updates the addresses on the SubConn.

func (*TestClientConn) UpdateState added in v1.43.0

func (tcc *TestClientConn) UpdateState(bs balancer.State)

UpdateState updates connectivity state and picker.

func (*TestClientConn) WaitForConnectivityState added in v1.48.0

func (tcc *TestClientConn) WaitForConnectivityState(ctx context.Context, want connectivity.State) error

WaitForConnectivityState waits until the state pushed to this ClientConn matches the wanted state. Returns an error if the provided context expires, including the last received state (if any).

func (*TestClientConn) WaitForErrPicker added in v1.43.0

func (tcc *TestClientConn) WaitForErrPicker(ctx context.Context) error

WaitForErrPicker waits until an error picker is pushed to this ClientConn. Returns error if the provided context expires or a non-error picker is pushed to the ClientConn.

func (*TestClientConn) WaitForPicker added in v1.48.0

func (tcc *TestClientConn) WaitForPicker(ctx context.Context, f func(balancer.Picker) error) error

WaitForPicker waits for a picker that results in f returning nil. If the context expires, returns the last error returned by f (if any).

func (*TestClientConn) WaitForPickerWithErr added in v1.48.0

func (tcc *TestClientConn) WaitForPickerWithErr(ctx context.Context, want error) error

WaitForPickerWithErr waits until an error picker is pushed to this ClientConn with the error matching the wanted error. Returns an error if the provided context expires, including the last received picker error (if any).

func (*TestClientConn) WaitForRoundRobinPicker added in v1.48.0

func (tcc *TestClientConn) WaitForRoundRobinPicker(ctx context.Context, want ...balancer.SubConn) error

WaitForRoundRobinPicker waits for a picker that passes IsRoundRobin. Also drains the matching state channel and requires it to be READY (if an entry is pending) to be considered. Returns an error if the provided context expires, including the last received error from IsRoundRobin or the picker (if any).

type TestConstPicker added in v1.43.0

type TestConstPicker struct {
	Err error
	SC  balancer.SubConn
}

TestConstPicker is a const picker for tests.

func (*TestConstPicker) Pick added in v1.43.0

Pick returns the const SubConn or the error.

type TestSubConn added in v1.43.0

type TestSubConn struct {
	ConnectCh chan struct{}
	// contains filtered or unexported fields
}

TestSubConn implements the SubConn interface, to be used in tests.

func (*TestSubConn) Connect added in v1.43.0

func (tsc *TestSubConn) Connect()

Connect is a no-op.

func (*TestSubConn) GetOrBuildProducer added in v1.51.0

func (tsc *TestSubConn) GetOrBuildProducer(balancer.ProducerBuilder) (balancer.Producer, func())

GetOrBuildProducer is a no-op.

func (*TestSubConn) String added in v1.43.0

func (tsc *TestSubConn) String() string

String implements stringer to print human friendly error message.

func (*TestSubConn) UpdateAddresses added in v1.43.0

func (tsc *TestSubConn) UpdateAddresses([]resolver.Address)

UpdateAddresses is a no-op.

Directories

Path Synopsis
Package fakegrpclb provides a fake implementation of the grpclb server.
Package fakegrpclb provides a fake implementation of the grpclb server.
Package pickfirst contains helper functions to check for pickfirst load balancing of RPCs in tests.
Package pickfirst contains helper functions to check for pickfirst load balancing of RPCs in tests.
Package rls contains utilities for RouteLookupService e2e tests.
Package rls contains utilities for RouteLookupService e2e tests.
Package roundrobin contains helper functions to check for roundrobin and weighted-roundrobin load balancing of RPCs in tests.
Package roundrobin contains helper functions to check for roundrobin and weighted-roundrobin load balancing of RPCs in tests.
xds
bootstrap
Package bootstrap provides functionality to generate bootstrap configuration.
Package bootstrap provides functionality to generate bootstrap configuration.
e2e
Package e2e provides utilities for end2end testing of xDS functionality.
Package e2e provides utilities for end2end testing of xDS functionality.
fakeserver
Package fakeserver provides a fake implementation of the management server.
Package fakeserver provides a fake implementation of the management server.

Jump to

Keyboard shortcuts

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