Documentation ¶
Overview ¶
Package testutils contains testing helpers.
Index ¶
- Constants
- Variables
- func AwaitNoStateChange(ctx context.Context, t *testing.T, sc StateChanger, ...)
- func AwaitNotState(ctx context.Context, t *testing.T, sc StateChanger, ...)
- func AwaitState(ctx context.Context, t *testing.T, sc StateChanger, ...)
- func CreateBootstrapFileForTesting(t *testing.T, bootstrapContents []byte)
- func CreateClientTLSCredentials(t *testing.T) credentials.TransportCredentials
- func CreateServerTLSCredentials(t *testing.T, clientAuth tls.ClientAuthType) credentials.TransportCredentials
- func IsRoundRobin(want []balancer.SubConn, f func() balancer.SubConn) error
- func LocalTCPListener() (net.Listener, error)
- func MarshalAny(t *testing.T, m proto.Message) *anypb.Any
- func MustParseURL(target string) *url.URL
- func NewTestWRR() wrr.WRR
- func ParsePort(t *testing.T, addr string) uint32
- func StatusErrEqual(err1, err2 error) bool
- func StayConnected(ctx context.Context, sc StateChanger)
- func SubConnFromPicker(p balancer.Picker) func() balancer.SubConn
- type BalancerClientConn
- func (tcc *BalancerClientConn) NewSubConn(a []resolver.Address, o balancer.NewSubConnOptions) (balancer.SubConn, error)
- func (tcc *BalancerClientConn) RemoveSubConn(sc balancer.SubConn)
- func (tcc *BalancerClientConn) ResolveNow(o resolver.ResolveNowOptions)
- func (tcc *BalancerClientConn) Target() string
- func (tcc *BalancerClientConn) UpdateAddresses(sc balancer.SubConn, addrs []resolver.Address)
- func (tcc *BalancerClientConn) UpdateState(bs balancer.State)
- func (tcc *BalancerClientConn) WaitForConnectivityState(ctx context.Context, want connectivity.State) error
- func (tcc *BalancerClientConn) WaitForErrPicker(ctx context.Context) error
- func (tcc *BalancerClientConn) WaitForPicker(ctx context.Context, f func(balancer.Picker) error) error
- func (tcc *BalancerClientConn) WaitForPickerWithErr(ctx context.Context, want error) error
- func (tcc *BalancerClientConn) WaitForRoundRobinPicker(ctx context.Context, want ...balancer.SubConn) error
- type BlockingDialer
- type Channel
- type ConnWrapper
- type FakeHTTPClient
- type Hold
- type ListenerWrapper
- type Logger
- type PipeListener
- type ResolverClientConn
- type RestartableListener
- type StateChanger
- type StubStatsHandler
- func (ssh *StubStatsHandler) HandleConn(ctx context.Context, cs stats.ConnStats)
- func (ssh *StubStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats)
- func (ssh *StubStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context
- func (ssh *StubStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context
- type TestConstPicker
- type TestSubConn
- func (tsc *TestSubConn) Connect()
- func (tsc *TestSubConn) GetOrBuildProducer(balancer.ProducerBuilder) (balancer.Producer, func())
- func (tsc *TestSubConn) Shutdown()
- func (tsc *TestSubConn) String() string
- func (tsc *TestSubConn) UpdateAddresses([]resolver.Address)
- func (tsc *TestSubConn) UpdateState(state balancer.SubConnState)
Constants ¶
const DefaultChanBufferSize = 1
DefaultChanBufferSize is the default buffer size of the underlying channel.
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 ¶
var ErrTestConstPicker = fmt.Errorf("const picker error")
ErrTestConstPicker is error returned by test const picker.
Functions ¶
func AwaitNoStateChange ¶ added in v1.58.0
func AwaitNoStateChange(ctx context.Context, t *testing.T, sc StateChanger, currState connectivity.State)
AwaitNoStateChange expects ctx to be canceled before sc's state leaves currState, and fatal errors otherwise.
func AwaitNotState ¶ added in v1.58.0
func AwaitNotState(ctx context.Context, t *testing.T, sc StateChanger, stateDoNotWant connectivity.State)
AwaitNotState waits for sc to leave stateDoNotWant or fatal errors if it doesn't happen before ctx expires.
func AwaitState ¶ added in v1.58.0
func AwaitState(ctx context.Context, t *testing.T, sc StateChanger, stateWant connectivity.State)
AwaitState waits for sc to enter stateWant or fatal errors if it doesn't happen before ctx expires.
func CreateBootstrapFileForTesting ¶ added in v1.66.0
CreateBootstrapFileForTesting creates a temporary file with the provided bootstrap contents, and updates the bootstrap environment variable to point to this file.
Registers a cleanup function on the provided testing.T, that deletes the temporary file and resets the bootstrap environment variable.
func CreateClientTLSCredentials ¶ added in v1.66.0
func CreateClientTLSCredentials(t *testing.T) credentials.TransportCredentials
CreateClientTLSCredentials creates client-side TLS transport credentials using certificate and key files from testdata/x509 directory.
func CreateServerTLSCredentials ¶ added in v1.66.0
func CreateServerTLSCredentials(t *testing.T, clientAuth tls.ClientAuthType) credentials.TransportCredentials
CreateServerTLSCredentials creates server-side TLS transport credentials using certificate and key files from testdata/x509 directory.
func IsRoundRobin ¶ added in v1.43.0
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 fails 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
LocalTCPListener returns a net.Listener listening on local address and port.
func MarshalAny ¶ added in v1.38.0
MarshalAny is a convenience function to marshal protobuf messages into any protos. function will fail the test with a fatal error if the marshaling fails.
func MustParseURL ¶ added in v1.53.0
MustParseURL attempts to parse the provided target using url.Parse() and panics if parsing fails.
func NewTestWRR ¶ added in v1.43.0
NewTestWRR return a WRR for testing. It's deterministic instead of random.
func ParsePort ¶ added in v1.55.0
ParsePort returns the port from the given address string, as a unit32.
func StatusErrEqual ¶ added in v1.22.0
StatusErrEqual returns true iff both err1 and err2 wrap status.Status errors and their underlying status protos are equal.
func StayConnected ¶ added in v1.58.0
func StayConnected(ctx context.Context, sc StateChanger)
StayConnected makes sc stay connected by repeatedly calling sc.Connect() until the state becomes Shutdown or until the context expires.
func SubConnFromPicker ¶ added in v1.51.0
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 BalancerClientConn ¶ added in v1.60.0
type BalancerClientConn struct { NewSubConnAddrsCh chan []resolver.Address // the last 10 []Address to create subconn. NewSubConnCh chan *TestSubConn // the last 10 subconn created. ShutdownSubConnCh chan *TestSubConn // 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 }
BalancerClientConn is a mock balancer.ClientConn used in tests.
func NewBalancerClientConn ¶ added in v1.60.0
func NewBalancerClientConn(t *testing.T) *BalancerClientConn
NewBalancerClientConn creates a BalancerClientConn.
func (*BalancerClientConn) NewSubConn ¶ added in v1.60.0
func (tcc *BalancerClientConn) NewSubConn(a []resolver.Address, o balancer.NewSubConnOptions) (balancer.SubConn, error)
NewSubConn creates a new SubConn.
func (*BalancerClientConn) RemoveSubConn ¶ added in v1.60.0
func (tcc *BalancerClientConn) RemoveSubConn(sc balancer.SubConn)
RemoveSubConn is a nop; tests should all be updated to use sc.Shutdown() instead.
func (*BalancerClientConn) ResolveNow ¶ added in v1.60.0
func (tcc *BalancerClientConn) ResolveNow(o resolver.ResolveNowOptions)
ResolveNow panics.
func (*BalancerClientConn) Target ¶ added in v1.60.0
func (tcc *BalancerClientConn) Target() string
Target panics.
func (*BalancerClientConn) UpdateAddresses ¶ added in v1.60.0
func (tcc *BalancerClientConn) UpdateAddresses(sc balancer.SubConn, addrs []resolver.Address)
UpdateAddresses updates the addresses on the SubConn.
func (*BalancerClientConn) UpdateState ¶ added in v1.60.0
func (tcc *BalancerClientConn) UpdateState(bs balancer.State)
UpdateState updates connectivity state and picker.
func (*BalancerClientConn) WaitForConnectivityState ¶ added in v1.60.0
func (tcc *BalancerClientConn) 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 (*BalancerClientConn) WaitForErrPicker ¶ added in v1.60.0
func (tcc *BalancerClientConn) 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 (*BalancerClientConn) WaitForPicker ¶ added in v1.60.0
func (tcc *BalancerClientConn) 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 (*BalancerClientConn) WaitForPickerWithErr ¶ added in v1.60.0
func (tcc *BalancerClientConn) 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 (*BalancerClientConn) WaitForRoundRobinPicker ¶ added in v1.60.0
func (tcc *BalancerClientConn) 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 BlockingDialer ¶ added in v1.66.0
type BlockingDialer struct {
// contains filtered or unexported fields
}
BlockingDialer is a dialer that waits for Resume() to be called before dialing.
func NewBlockingDialer ¶ added in v1.66.0
func NewBlockingDialer() *BlockingDialer
NewBlockingDialer returns a dialer that waits for Resume() to be called before dialing.
func (*BlockingDialer) DialContext ¶ added in v1.66.0
DialContext implements a context dialer for use with grpc.WithContextDialer dial option for a BlockingDialer.
func (*BlockingDialer) Hold ¶ added in v1.66.0
func (d *BlockingDialer) Hold(addr string) *Hold
Hold blocks the dialer when a connection attempt is made to the given addr. A hold is valid for exactly one connection attempt. Multiple holds for an addr can be added, and they will apply in the order that the connections are attempted.
type Channel ¶ added in v1.30.0
type Channel struct { // C is the underlying channel on which values sent using the SendXxx() methods are delivered. // Tests which cannot use ReceiveXxx() for whatever reasons can use C to read the values. C chan any }
Channel wraps a generic channel and provides a timed receive operation.
func NewChannelWithSize ¶ added in v1.30.0
NewChannelWithSize returns a new Channel with a buffer of bufSize.
func (*Channel) Receive ¶ added in v1.30.0
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
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
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) SendContext ¶ added in v1.34.0
SendContext sends value on the underlying channel, or returns an error if the context expires.
func (*Channel) SendOrFail ¶ added in v1.34.0
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
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.
type Hold ¶ added in v1.66.0
type Hold struct {
// contains filtered or unexported fields
}
Hold is a handle to a single connection attempt. It can be used to block, fail and succeed connection attempts.
func (*Hold) Fail ¶ added in v1.66.0
Fail fails the connection attempt. Either Resume or Fail must be called at most once on a hold. Otherwise, Resume panics.
type ListenerWrapper ¶ added in v1.46.0
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.
type Logger ¶ added in v1.60.0
type Logger interface { Log(args ...any) Logf(format string, args ...any) Errorf(format string, args ...any) }
Logger wraps the logging methods from testing.T.
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.
type ResolverClientConn ¶ added in v1.60.0
type ResolverClientConn struct { resolver.ClientConn // Embedding the interface to avoid implementing deprecated methods. Logger Logger // Tests should pass testing.T for this. UpdateStateF func(resolver.State) error // Invoked when resolver pushes a state update. ReportErrorF func(err error) // Invoked when resolver pushes an error. }
ResolverClientConn is a fake implementation of the resolver.ClientConn interface to be used in tests.
func (*ResolverClientConn) ParseServiceConfig ¶ added in v1.60.0
func (t *ResolverClientConn) ParseServiceConfig(jsonSC string) *serviceconfig.ParseResult
ParseServiceConfig parses the provided service by delegating the work to the implementation in the grpc package.
func (*ResolverClientConn) ReportError ¶ added in v1.60.0
func (t *ResolverClientConn) ReportError(err error)
ReportError pushes the error received from the resolver on to ErrorCh.
func (*ResolverClientConn) UpdateState ¶ added in v1.60.0
func (t *ResolverClientConn) UpdateState(s resolver.State) error
UpdateState invokes the test specified callback with the update received from the resolver. If the callback returns a non-nil error, the same will be propagated to the resolver.
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 StateChanger ¶ added in v1.58.0
type StateChanger interface { // Connect begins connecting the StateChanger. Connect() // GetState returns the current state of the StateChanger. GetState() connectivity.State // WaitForStateChange returns true when the state becomes s, or returns // false if ctx is canceled first. WaitForStateChange(ctx context.Context, s connectivity.State) bool }
A StateChanger reports state changes, e.g. a grpc.ClientConn.
type StubStatsHandler ¶ added in v1.60.0
type StubStatsHandler struct { TagRPCF func(ctx context.Context, info *stats.RPCTagInfo) context.Context HandleRPCF func(ctx context.Context, info stats.RPCStats) TagConnF func(ctx context.Context, info *stats.ConnTagInfo) context.Context HandleConnF func(ctx context.Context, info stats.ConnStats) }
StubStatsHandler is a stats handler that is easy to customize within individual test cases. It is a stubbable implementation of google.golang.org/grpc/stats.Handler for testing purposes.
func (*StubStatsHandler) HandleConn ¶ added in v1.60.0
func (ssh *StubStatsHandler) HandleConn(ctx context.Context, cs stats.ConnStats)
HandleConn calls the StubStatsHandler's HandleConnF, if set.
func (*StubStatsHandler) HandleRPC ¶ added in v1.60.0
func (ssh *StubStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats)
HandleRPC calls the StubStatsHandler's HandleRPCF, if set.
func (*StubStatsHandler) TagConn ¶ added in v1.60.0
func (ssh *StubStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context
TagConn calls the StubStatsHandler's TagConnF, if set.
func (*StubStatsHandler) TagRPC ¶ added in v1.60.0
func (ssh *StubStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context
TagRPC calls the StubStatsHandler's TagRPCF, if set.
type TestConstPicker ¶ added in v1.43.0
TestConstPicker is a const picker for tests.
func (*TestConstPicker) Pick ¶ added in v1.43.0
func (tcp *TestConstPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error)
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 NewTestSubConn ¶ added in v1.58.0
func NewTestSubConn(id string) *TestSubConn
NewTestSubConn returns a newly initialized SubConn. Typically, subconns should be created via TestClientConn.NewSubConn instead, but can be useful for some 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) Shutdown ¶ added in v1.58.0
func (tsc *TestSubConn) Shutdown()
Shutdown pushes the SubConn to the ShutdownSubConn channel in the parent TestClientConn.
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.
func (*TestSubConn) UpdateState ¶ added in v1.58.0
func (tsc *TestSubConn) UpdateState(state balancer.SubConnState)
UpdateState pushes the state to the listener, if one is registered.
Source Files ¶
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. |
Package stats implements a TestMetricsRecorder utility.
|
Package stats implements a TestMetricsRecorder utility. |
xds
|
|
e2e
Package e2e provides utilities for end2end testing of xDS functionality.
|
Package e2e provides utilities for end2end testing of xDS functionality. |
e2e/setup
Package setup implements setup helpers for xDS e2e tests.
|
Package setup implements setup helpers for xDS e2e tests. |
fakeserver
Package fakeserver provides a fake implementation of the management server.
|
Package fakeserver provides a fake implementation of the management server. |