testing

package
v0.0.0-...-6e5b399 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package testing provides public API for tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextLog

func ContextLog(ctx context.Context, args ...interface{})

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

func ContextLogf(ctx context.Context, format string, args ...interface{})

ContextLogf is similar to ContextLog but formats its arguments using fmt.Sprintf.

func ContextVLog

func ContextVLog(ctx context.Context, args ...interface{})

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

func ContextVLogf(ctx context.Context, format string, args ...interface{})

ContextVLogf is similar to ContextVLog but formats its arguments using fmt.Sprintf.

func Poll

func Poll(ctx context.Context, f func(context.Context) error, opts *PollOptions) error

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

func PollBreak(err error) error

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

func SetLogPrefix(ctx context.Context, prefix string) context.Context

SetLogPrefix sets log prefix for the context.

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

Sleep pauses the current goroutine for d or until ctx expires.

Please consider using testing.Poll instead. Sleeping without polling for a condition is discouraged, since it makes tests flakier (when the sleep duration isn't long enough) or slower (when the duration is too long).

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

func ContextLogger(ctx context.Context) (*Logger, bool)

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.

func (*Logger) Printf

func (l *Logger) Printf(format string, args ...interface{})

Printf is similar to Print but formats its arguments using fmt.Sprintf.

func (*Logger) VPrint

func (l *Logger) VPrint(args ...interface{})

VPrint formats its arguments using default formatting and logs them at the debug (verbose) level.

func (*Logger) VPrintf

func (l *Logger) VPrintf(format string, args ...interface{})

VPrintf is similar to VPrint but formats its arguments using fmt.Sprintf.

type PollOptions

type PollOptions = testingutil.PollOptions

PollOptions may be passed to Poll to configure its behavior.

Jump to

Keyboard shortcuts

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