package testutils
import "github.com/uber/tchannel-go/testutils"
Index ¶
- Constants
- func AssertEcho(tb testing.TB, src *tchannel.Channel, targetHostPort, targetService string)
- func Batch(n, batchSize int) []int
- func Buckets(n int, numBuckets int) []int
- func CallEcho(src *tchannel.Channel, targetHostPort, targetService string, args *raw.Args) error
- func DurationArray(dd ...time.Duration) []time.Duration
- func FrameRelay(t *testing.T, destination string, relayFunc func(outgoing bool, f *tchannel.Frame) *tchannel.Frame) (listenHostPort string, cancel func())
- func GetAcceptCloseHostPort(t testing.TB) (string, func())
- func GetClosedHostPort(t testing.TB) string
- func NewClient(t testing.TB, opts *ChannelOpts) *tchannel.Channel
- func NewClientChannel(opts *ChannelOpts) (*tchannel.Channel, error)
- func NewIncomingCall(callerName string) tchannel.IncomingCall
- func NewServer(t testing.TB, opts *ChannelOpts) *tchannel.Channel
- func NewServerChannel(opts *ChannelOpts) (*tchannel.Channel, error)
- func NowStub(initial time.Time) (stub func() time.Time, increment func(time.Duration))
- func Ping(src, target *tchannel.Channel) error
- func RandBytes(n int) []byte
- func RandString(n int) string
- func RegisterEcho(src *tchannel.Channel, f func())
- func RegisterFunc(ch *tchannel.Channel, name string, f func(ctx context.Context, args *raw.Args) (*raw.Res, error))
- func ResetSleepStub(funcVar *func(time.Duration))
- func RunN(n int, f func(i int))
- func SetTimeout(t *testing.T, timeout time.Duration) func()
- func SleepStub(funcVar *func(time.Duration)) ( argCh <-chan time.Duration, unblockCh chan<- struct{}, closeFn func())
- func StrArray(ss ...string) []string
- func StrMap(ss ...string) map[string]struct{}
- func Timeout(timeout time.Duration) time.Duration
- func WaitFor(timeout time.Duration, f func() bool) bool
- func WaitWG(wg *sync.WaitGroup, timeout time.Duration) bool
- func WithServer(t testing.TB, opts *ChannelOpts, f func(ch *tchannel.Channel, hostPort string))
- func WithTestServer(t testing.TB, chanOpts *ChannelOpts, f func(*TestServer))
- type ChannelOpts
- func DefaultOpts(opts *ChannelOpts) *ChannelOpts
- func NewOpts() *ChannelOpts
- func (o *ChannelOpts) AddLogFilter(filter string, maxCount uint, fields ...string) *ChannelOpts
- func (o *ChannelOpts) Copy() *ChannelOpts
- func (o *ChannelOpts) DisableLogVerification() *ChannelOpts
- func (o *ChannelOpts) NoRelay() *ChannelOpts
- func (o *ChannelOpts) SetFramePool(framePool tchannel.FramePool) *ChannelOpts
- func (o *ChannelOpts) SetProcessName(processName string) *ChannelOpts
- func (o *ChannelOpts) SetRelayHosts(rh relay.Hosts) *ChannelOpts
- func (o *ChannelOpts) SetRelayOnly() *ChannelOpts
- func (o *ChannelOpts) SetRunCount(n int) *ChannelOpts
- func (o *ChannelOpts) SetServiceName(svcName string) *ChannelOpts
- func (o *ChannelOpts) SetStatsReporter(statsReporter tchannel.StatsReporter) *ChannelOpts
- func (o *ChannelOpts) SetTimeNow(timeNow func() time.Time) *ChannelOpts
- func (o *ChannelOpts) SetTraceReporter(traceReporter tchannel.TraceReporter) *ChannelOpts
- func (o *ChannelOpts) SetTraceReporterFactory(factory tchannel.TraceReporterFactory) *ChannelOpts
- func (o *ChannelOpts) SetTraceSampleRate(sampleRate float64) *ChannelOpts
- type Decrement
- type FakeCallFrame
- func (f FakeCallFrame) Caller() []byte
- func (f FakeCallFrame) Method() []byte
- func (f FakeCallFrame) Service() []byte
- type FakeIncomingCall
- func (f *FakeIncomingCall) CallOptions() *tchannel.CallOptions
- func (f *FakeIncomingCall) CallerName() string
- func (f *FakeIncomingCall) RemotePeer() tchannel.PeerInfo
- func (f *FakeIncomingCall) RoutingDelegate() string
- func (f *FakeIncomingCall) ShardKey() string
- type LogFilter
- type LogVerification
- type SimpleRelayHosts
- func NewSimpleRelayHosts(peerHostPorts map[string][]string) *SimpleRelayHosts
- func (rh *SimpleRelayHosts) Add(service, hostPort string)
- func (rh *SimpleRelayHosts) AddError(service string, err error)
- func (rh *SimpleRelayHosts) AddPeer(service, hostPort, pool, zone string)
- func (rh *SimpleRelayHosts) Get(frame relay.CallFrame) (relay.Peer, error)
- type TestServer
- func NewTestServer(t testing.TB, opts *ChannelOpts) *TestServer
- func (ts *TestServer) AssertRelayStats(expected *relaytest.MockStats)
- func (ts *TestServer) CloseAndVerify()
- func (ts *TestServer) HasRelay() bool
- func (ts *TestServer) HostPort() string
- func (ts *TestServer) NewClient(opts *ChannelOpts) *tchannel.Channel
- func (ts *TestServer) NewServer(opts *ChannelOpts) *tchannel.Channel
- func (ts *TestServer) Register(h tchannel.Handler, methodName string)
- func (ts *TestServer) RegisterFunc(name string, f func(context.Context, *raw.Args) (*raw.Res, error))
- func (ts *TestServer) Relay() *tchannel.Channel
- func (ts *TestServer) RelayHosts() *SimpleRelayHosts
- func (ts *TestServer) Server() *tchannel.Channel
- func (ts *TestServer) ServiceName() string
- func (ts *TestServer) SetVerifyOpts(opts *goroutines.VerifyOpts)
Constants ¶
const ( DefaultServerName = "testService" DefaultClientName = "testService-client" )
Default service names for the test channels.
Functions ¶
func AssertEcho ¶
AssertEcho calls the "echo" endpoint with random data, and asserts that the returned data matches the arguments "echo" was called with.
func Batch ¶
Batch returns a slice with n broken into batches of size batchSize.
func Buckets ¶
Buckets splits n over the specified number of buckets.
func CallEcho ¶
CallEcho calls the "echo" endpoint from the given src to target.
func DurationArray ¶
DurationArray returns an array with the given durations.
func FrameRelay ¶
func FrameRelay(t *testing.T, destination string, relayFunc func(outgoing bool, f *tchannel.Frame) *tchannel.Frame) (listenHostPort string, cancel func())
FrameRelay sets up a relay that can modify frames using relayFunc.
func GetAcceptCloseHostPort ¶
GetAcceptCloseHostPort returns a host:port that will accept a connection then immediately close it. The returned function can be used to stop the listener.
func GetClosedHostPort ¶
GetClosedHostPort will return a host:port that will refuse connections.
func NewClient ¶
func NewClient(t testing.TB, opts *ChannelOpts) *tchannel.Channel
NewClient returns a new TChannel that is not listening.
func NewClientChannel ¶
func NewClientChannel(opts *ChannelOpts) (*tchannel.Channel, error)
NewClientChannel creates a TChannel that is not listening. Passed in options may be mutated (for post-verification of state).
func NewIncomingCall ¶
func NewIncomingCall(callerName string) tchannel.IncomingCall
NewIncomingCall creates an incoming call for tests.
func NewServer ¶
func NewServer(t testing.TB, opts *ChannelOpts) *tchannel.Channel
NewServer returns a new TChannel server that listens on :0.
func NewServerChannel ¶
func NewServerChannel(opts *ChannelOpts) (*tchannel.Channel, error)
NewServerChannel creates a TChannel that is listening and returns the channel. Passed in options may be mutated (for post-verification of state).
func NowStub ¶
NowStub returns a stub time.Now function that allows the return values to to be controller by the caller. It returns two functions: stub: The stub time.Now function. increment: Used to control the increment amount between calls.
func Ping ¶
func Ping(src, target *tchannel.Channel) error
Ping sends a ping from src to target.
func RandBytes ¶
RandBytes returns n random byte slice that points to a shared random byte array. Since the underlying random array is shared, the returned byte slice must NOT be modified.
func RandString ¶
RandString returns a random alphanumeric string for testing.
func RegisterEcho ¶
func RegisterEcho(src *tchannel.Channel, f func())
RegisterEcho registers an echo endpoint on the given channel. The optional provided function is run before the handler returns.
func RegisterFunc ¶
func RegisterFunc(ch *tchannel.Channel, name string, f func(ctx context.Context, args *raw.Args) (*raw.Res, error))
RegisterFunc registers a function as a handler for the given method name.
func ResetSleepStub ¶
ResetSleepStub resets a Sleep stub.
func RunN ¶
RunN runs the given f n times (and passes the run's index) and waits till they complete. It starts n-1 goroutines, and runs one instance in the current goroutine.
func SetTimeout ¶
SetTimeout is used to fail tests after a timeout. It returns a function that should be run once the test is complete. The standard way is to use defer, e.g. defer SetTimeout(t, time.Second)()
func SleepStub ¶
func SleepStub(funcVar *func(time.Duration)) ( argCh <-chan time.Duration, unblockCh chan<- struct{}, closeFn func())
SleepStub stubs a function variable that points to time.Sleep. It returns two channels to control the sleep stub, and a function to close the channels. Once the stub is closed, any further sleeps will cause panics. The two channels returned are: <-chan time.Duration which will contain arguments that the stub was called with. chan<- struct{} that should be written to when you want the Sleep to return.
func StrArray ¶
StrArray will return an array with the given strings.
func StrMap ¶
StrMap returns a map where the keys are the given strings.
func Timeout ¶
Timeout returns the timeout multiplied by any set multiplier.
func WaitFor ¶
WaitFor will retry f till it returns true for a maximum of timeout. It returns true if f returned true, false if timeout was hit.
func WaitWG ¶
WaitWG waits for the given WaitGroup to be complete with a timeout and returns whether the WaitGroup completed within the timeout.
func WithServer ¶
func WithServer(t testing.TB, opts *ChannelOpts, f func(ch *tchannel.Channel, hostPort string))
WithServer sets up a TChannel that is listening and runs the given function with the channel.
func WithTestServer ¶
func WithTestServer(t testing.TB, chanOpts *ChannelOpts, f func(*TestServer))
WithTestServer creates a new TestServer, runs the passed function, and then verifies that no resources were leaked.
Types ¶
type ChannelOpts ¶
type ChannelOpts struct { tchannel.ChannelOptions // ServiceName defaults to DefaultServerName or DefaultClientName. ServiceName string // LogVerification contains options for controlling the log verification. LogVerification LogVerification // DisableRelay disables the relay interposed between clients/servers. // By default, all tests are run with a relay interposed. DisableRelay bool // OnlyRelay instructs TestServer the test must only be run with a relay. OnlyRelay bool // RunCount is the number of times the test should be run. Zero or // negative values are treated as a single run. RunCount int // contains filtered or unexported fields }
ChannelOpts contains options to create a test channel using WithServer
func DefaultOpts ¶
func DefaultOpts(opts *ChannelOpts) *ChannelOpts
DefaultOpts will return opts if opts is non-nil, NewOpts otherwise.
func NewOpts ¶
func NewOpts() *ChannelOpts
NewOpts returns a new ChannelOpts that can be used in a chained fashion.
func (*ChannelOpts) AddLogFilter ¶
func (o *ChannelOpts) AddLogFilter(filter string, maxCount uint, fields ...string) *ChannelOpts
AddLogFilter sets an allowed filter for warning/error logs and sets the maximum number of times that log can occur.
func (*ChannelOpts) Copy ¶
func (o *ChannelOpts) Copy() *ChannelOpts
Copy copies the channel options (so that they can be safely modified).
func (*ChannelOpts) DisableLogVerification ¶
func (o *ChannelOpts) DisableLogVerification() *ChannelOpts
DisableLogVerification disables log verification for this channel.
func (*ChannelOpts) NoRelay ¶
func (o *ChannelOpts) NoRelay() *ChannelOpts
NoRelay disables running the test with a relay interposed.
func (*ChannelOpts) SetFramePool ¶
func (o *ChannelOpts) SetFramePool(framePool tchannel.FramePool) *ChannelOpts
SetFramePool sets FramePool in DefaultConnectionOptions.
func (*ChannelOpts) SetProcessName ¶
func (o *ChannelOpts) SetProcessName(processName string) *ChannelOpts
SetProcessName sets the ProcessName in ChannelOptions.
func (*ChannelOpts) SetRelayHosts ¶
func (o *ChannelOpts) SetRelayHosts(rh relay.Hosts) *ChannelOpts
SetRelayHosts sets the channel's relay hosts, which enables relaying.
func (*ChannelOpts) SetRelayOnly ¶
func (o *ChannelOpts) SetRelayOnly() *ChannelOpts
SetRelayOnly instructs TestServer to only run with a relay in front of this channel.
func (*ChannelOpts) SetRunCount ¶
func (o *ChannelOpts) SetRunCount(n int) *ChannelOpts
SetRunCount sets the number of times run the test.
func (*ChannelOpts) SetServiceName ¶
func (o *ChannelOpts) SetServiceName(svcName string) *ChannelOpts
SetServiceName sets ServiceName.
func (*ChannelOpts) SetStatsReporter ¶
func (o *ChannelOpts) SetStatsReporter(statsReporter tchannel.StatsReporter) *ChannelOpts
SetStatsReporter sets StatsReporter in ChannelOptions.
func (*ChannelOpts) SetTimeNow ¶
func (o *ChannelOpts) SetTimeNow(timeNow func() time.Time) *ChannelOpts
SetTimeNow sets TimeNow in ChannelOptions.
func (*ChannelOpts) SetTraceReporter ¶
func (o *ChannelOpts) SetTraceReporter(traceReporter tchannel.TraceReporter) *ChannelOpts
SetTraceReporter sets TraceReporter in ChannelOptions.
func (*ChannelOpts) SetTraceReporterFactory ¶
func (o *ChannelOpts) SetTraceReporterFactory(factory tchannel.TraceReporterFactory) *ChannelOpts
SetTraceReporterFactory sets TraceReporterFactory in ChannelOptions.
func (*ChannelOpts) SetTraceSampleRate ¶
func (o *ChannelOpts) SetTraceSampleRate(sampleRate float64) *ChannelOpts
SetTraceSampleRate sets the TraceSampleRate in ChannelOptions.
type Decrement ¶
type Decrement interface { // Single returns whether any more tokens are remaining. Single() bool // Multiple tries to get n tokens. It returns the actual amount of tokens // available to use. If this is 0, it means there are no tokens left. Multiple(n int) int }
Decrement is the interface returned by Decrementor.
func Decrementor ¶
Decrementor returns a function that can be called from multiple goroutines and ensures it will only return true n times.
type FakeCallFrame ¶
type FakeCallFrame struct { ServiceF, MethodF, CallerF string }
FakeCallFrame is a stub implementation of the CallFrame interface.
func (FakeCallFrame) Caller ¶
func (f FakeCallFrame) Caller() []byte
Caller returns the caller field.
func (FakeCallFrame) Method ¶
func (f FakeCallFrame) Method() []byte
Method returns the method field.
func (FakeCallFrame) Service ¶
func (f FakeCallFrame) Service() []byte
Service returns the service name field.
type FakeIncomingCall ¶
type FakeIncomingCall struct { // CallerNameF is the calling service's name. CallerNameF string // ShardKeyF is the intended destination for this call. ShardKeyF string // RemotePeerF is the calling service's peer info. RemotePeerF tchannel.PeerInfo // RoutingDelegateF is the routing delegate. RoutingDelegateF string }
FakeIncomingCall implements IncomingCall interface. Note: the F suffix for the fields is to clash with the method name.
func (*FakeIncomingCall) CallOptions ¶
func (f *FakeIncomingCall) CallOptions() *tchannel.CallOptions
CallOptions returns the incoming call options suitable for proxying a request.
func (*FakeIncomingCall) CallerName ¶
func (f *FakeIncomingCall) CallerName() string
CallerName returns the caller name as specified in the fake call.
func (*FakeIncomingCall) RemotePeer ¶
func (f *FakeIncomingCall) RemotePeer() tchannel.PeerInfo
RemotePeer returns the caller's peer info.
func (*FakeIncomingCall) RoutingDelegate ¶
func (f *FakeIncomingCall) RoutingDelegate() string
RoutingDelegate returns the routing delegate as specified in the fake call.
func (*FakeIncomingCall) ShardKey ¶
func (f *FakeIncomingCall) ShardKey() string
ShardKey returns the shard key as specified in the fake call.
type LogFilter ¶
type LogFilter struct { // Filter specifies the substring match to search // for in the log message to skip raising an error. Filter string // Count is the maximum number of allowed warn+ logs matching // Filter before errors are raised. Count uint // FieldFilters specifies expected substring matches for fields. FieldFilters map[string]string }
LogFilter is a single substring match that can be ignored.
func (LogFilter) Matches ¶
Matches returns true if the message and fields match the filter.
type LogVerification ¶
LogVerification contains options to control the log verification.
func (*LogVerification) WrapLogger ¶
func (v *LogVerification) WrapLogger(t testing.TB, l tchannel.Logger) tchannel.Logger
WrapLogger wraps the given logger with extra verification.
type SimpleRelayHosts ¶
SimpleRelayHosts is a simple stub that satisfies the RelayHosts interface.
func NewSimpleRelayHosts ¶
func NewSimpleRelayHosts(peerHostPorts map[string][]string) *SimpleRelayHosts
NewSimpleRelayHosts wraps a map in the RelayHosts interface.
func (*SimpleRelayHosts) Add ¶
func (rh *SimpleRelayHosts) Add(service, hostPort string)
Add adds a host:port for a routing key.
func (*SimpleRelayHosts) AddError ¶
func (rh *SimpleRelayHosts) AddError(service string, err error)
AddError specifies an error to be returned for a given service.
func (*SimpleRelayHosts) AddPeer ¶
func (rh *SimpleRelayHosts) AddPeer(service, hostPort, pool, zone string)
AddPeer adds a host:port with all the associated Peer information.
func (*SimpleRelayHosts) Get ¶
Get takes a routing key and returns the best host:port for that key.
type TestServer ¶
A TestServer encapsulates a TChannel server, a client factory, and functions to ensure that we're not leaking resources.
func NewTestServer ¶
func NewTestServer(t testing.TB, opts *ChannelOpts) *TestServer
NewTestServer constructs a TestServer.
func (*TestServer) AssertRelayStats ¶
func (ts *TestServer) AssertRelayStats(expected *relaytest.MockStats)
AssertRelayStats checks that the relayed call graph matches expectations. If there's no relay, AssertRelayStats is a no-op.
func (*TestServer) CloseAndVerify ¶
func (ts *TestServer) CloseAndVerify()
CloseAndVerify closes all channels verifying each channel as it is closed. It then verifies that no goroutines were leaked.
func (*TestServer) HasRelay ¶
func (ts *TestServer) HasRelay() bool
HasRelay indicates whether this TestServer has a relay interposed between the server and clients.
func (*TestServer) HostPort ¶
func (ts *TestServer) HostPort() string
HostPort returns the host:port for clients to connect to. Note that this may not be the same as the host:port of the server channel.
func (*TestServer) NewClient ¶
func (ts *TestServer) NewClient(opts *ChannelOpts) *tchannel.Channel
NewClient returns a client that with log verification. TODO: Verify message exchanges and leaks for client channels as well.
func (*TestServer) NewServer ¶
func (ts *TestServer) NewServer(opts *ChannelOpts) *tchannel.Channel
NewServer returns a server with log and channel state verification.
func (*TestServer) Register ¶
func (ts *TestServer) Register(h tchannel.Handler, methodName string)
Register registers a handler on the server channel.
func (*TestServer) RegisterFunc ¶
func (ts *TestServer) RegisterFunc(name string, f func(context.Context, *raw.Args) (*raw.Res, error))
RegisterFunc registers a function as a handler for the given method name.
TODO: Delete testutils.RegisterFunc in favor of this test server.
func (*TestServer) Relay ¶
func (ts *TestServer) Relay() *tchannel.Channel
Relay returns the relay channel, if one is present.
func (*TestServer) RelayHosts ¶
func (ts *TestServer) RelayHosts() *SimpleRelayHosts
RelayHosts returns the stub RelayHosts for mapping service names to peers.
func (*TestServer) Server ¶
func (ts *TestServer) Server() *tchannel.Channel
Server returns the underlying TChannel for the server (i.e., the channel on which we're registering handlers).
To support test cases with relays interposed between clients and servers, callers should use the Client(), HostPort(), ServiceName(), and Register() methods instead of accessing the server channel explicitly.
func (*TestServer) ServiceName ¶
func (ts *TestServer) ServiceName() string
ServiceName returns the service name of the server channel.
func (*TestServer) SetVerifyOpts ¶
func (ts *TestServer) SetVerifyOpts(opts *goroutines.VerifyOpts)
SetVerifyOpts specifies the options we'll use during teardown to verify that no goroutines were leaked.
Source Files ¶
call.go channel.go channel_opts.go channel_t.go conn.go counter.go data.go echo.go lists.go logger.go now.go relay.go relay_stub.go sleep.go test_server.go timeout.go wait.go
Directories ¶
Path | Synopsis |
---|---|
testutils/goroutines | |
testutils/mockhyperbahn | |
testutils/testreader | |
testutils/testwriter |
- Version
- v1.0.8
- Published
- Jul 16, 2016
- Platform
- js/wasm
- Imports
- 27 packages
- Last checked
- 7 hours ago –
Tools for package owners.