Documentation
¶
Overview ¶
Package testing provides public API for tests.
Index ¶
- func ContextLog(ctx context.Context, args ...interface{})
- func ContextLogf(ctx context.Context, format string, args ...interface{})
- func ContextVLog(ctx context.Context, args ...interface{})
- func ContextVLogf(ctx context.Context, format string, args ...interface{})
- func Poll(ctx context.Context, f func(context.Context) error, opts *PollOptions) error
- func PollBreak(err error) error
- func SetLogPrefix(ctx context.Context, prefix string) context.Context
- func Sleep(ctx context.Context, d time.Duration) error
- type Logger
- type PollOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextLog ¶
ContextLog formats its arguments using default formatting and logs them via ctx. It is intended to be used for informational logging by packages providing support for tests. If testing.State is available, just call State.Log or State.Logf instead.
func ContextLogf ¶
ContextLogf is similar to ContextLog but formats its arguments using fmt.Sprintf.
func ContextVLog ¶
ContextVLog formats its arguments using default formatting and logs them via ctx at the debug (verbose) level. It is intended to be used for verbose logging by packages providing support for tests. If testing.State is available, just call State.VLog or State.VLogf instead.
func ContextVLogf ¶
ContextVLogf is similar to ContextVLog but formats its arguments using fmt.Sprintf.
func Poll ¶
Poll runs f repeatedly until f returns nil and then itself returns nil. If ctx returns an error before then or opts.Timeout is reached, the last error returned by f is returned. f should use the context passed to it, as it may have an adjusted deadline if opts.Timeout is set. If ctx's deadline has already been reached, f will not be invoked. If opts is nil, reasonable defaults are used.
Polling often results in increased load and slower execution (since there's a delay between when something happens and when the next polling cycle notices it). It should only be used as a last resort when there's no other way to watch for an event. The preferred approach is to watch for events in select{} statements. Goroutines can be used to provide notifications over channels. If an error wrapped by PollBreak is returned, then it immediately terminates the polling, and returns the unwrapped error.
func PollBreak ¶
PollBreak creates an error wrapping err that may be returned from a function passed to Poll to terminate polling immediately. For example:
err := testing.Poll(ctx, func(ctx context.Context) error { if err := mustSucceed(ctx); err != nil { return testing.PollBreak(err) } ... })
func SetLogPrefix ¶
SetLogPrefix sets log prefix for the context.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger allows test helpers to log messages when no context.Context or testing.State is available.
func ContextLogger ¶
ContextLogger returns Logger from a context.
func (*Logger) Print ¶
func (l *Logger) Print(args ...interface{})
Print formats its arguments using default formatting and logs them.
type PollOptions ¶
type PollOptions = testingutil.PollOptions
PollOptions may be passed to Poll to configure its behavior.