Version: v0.0.0-...-ec84112 Latest Latest

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

Go to latest
Published: Sep 20, 2021 License: Apache-2.0 Imports: 12 Imported by: 0



Package testutil provides easy ways to generate some random or deterministic data that can be use for tests or benchmarks.

All the publicly shared trace agent model is available.

It avoids the cumbersome step of having to redefine complicated structs in every test case and maintain common methods for quick access to almost all kind of stub data needed. It should NEVER be imported in a program, most likely in one-off projects or fuzz modes or test suites.



This section is empty.


View Source
var YearNS = (time.Hour * 24 * 365).Nanoseconds()

YearNS is the number of nanoseconds in a year


func BucketWithSpans

func BucketWithSpans(spans []*stats.WeightedSpan) pb.ClientStatsBucket

BucketWithSpans returns a stats bucket populated with spans stats

func GeneratePayload

func GeneratePayload(n int, tc *TraceConfig, sc *SpanConfig) pb.Traces

GeneratePayload generates a new payload.

func GenerateSpan

func GenerateSpan(c *SpanConfig) *pb.Span

GenerateSpan generates a random root span with all fields filled in.

func GenerateTrace

func GenerateTrace(tc *TraceConfig, sc *SpanConfig) pb.Trace

GenerateTrace generates a valid trace using the given config.

func GetTestSpan

func GetTestSpan() *pb.Span

GetTestSpan returns a Span with different fields set

func GetTestTraces

func GetTestTraces(traceN, size int, realisticIDs bool) pb.Traces

GetTestTraces returns a []Trace that is composed by “traceN“ number of traces, each one composed by “size“ number of spans.

func RandomBucket

func RandomBucket(n int) pb.ClientStatsBucket

RandomBucket returns a bucket made from n random spans, useful to run benchmarks and tests

func RandomSizedBytes

func RandomSizedBytes(size int) []byte

RandomSizedBytes creates a random byte slice with the specified size.

func RandomSpan

func RandomSpan() *pb.Span

RandomSpan generates a wide-variety of spans, useful to test robustness & performance

func RandomSpanDuration

func RandomSpanDuration() int64

RandomSpanDuration generates a random span duration

func RandomSpanError

func RandomSpanError() int32

RandomSpanError generates a random span error code

func RandomSpanID

func RandomSpanID() uint64

RandomSpanID generates a random span ID

func RandomSpanMeta

func RandomSpanMeta() map[string]string

RandomSpanMeta generates some random span metadata

func RandomSpanMetrics

func RandomSpanMetrics() map[string]float64

RandomSpanMetrics generates some random span metrics

func RandomSpanName

func RandomSpanName() string

RandomSpanName generates a random span name string

func RandomSpanParentID

func RandomSpanParentID() uint64

RandomSpanParentID generates a random span parent ID

func RandomSpanResource

func RandomSpanResource() string

RandomSpanResource generates a random span resource string

func RandomSpanService

func RandomSpanService() string

RandomSpanService generates a random span service string

func RandomSpanStart

func RandomSpanStart() int64

RandomSpanStart generates a span start timestamp

func RandomSpanTraceID

func RandomSpanTraceID() uint64

RandomSpanTraceID generates a random trace ID

func RandomSpanType

func RandomSpanType() string

RandomSpanType generates a random span type

func RandomStringMap

func RandomStringMap() map[string]string

RandomStringMap creates a random map with string keys and values.

func RandomTrace

func RandomTrace(maxLevels, maxSpans int) pb.Trace

RandomTrace generates a random trace with a depth from 1 to maxLevels of spans. Each level has at most maxSpans items.

func RandomWeightedSpan

func RandomWeightedSpan() *stats.WeightedSpan

RandomWeightedSpan generates a random weighted span, useful for stats tests

func TestSpan

func TestSpan() *pb.Span

TestSpan returns a fix span with hardcoded info, useful for reproducible tests

func TestWeightedSpan

func TestWeightedSpan() *stats.WeightedSpan

TestWeightedSpan returns a static test weighted span for reproductive stats tests

func WithFeatures

func WithFeatures(feats string) (undo func())

WithFeatures sets the given list of comma-separated features as active and returns a function which resets the features to their previous state.


type CountSummary

type CountSummary struct {
	Calls []MetricsArgs
	Sum   int64

CountSummary contains a summary of all Count method calls to a particular StatsClient for a particular key.

type GaugeSummary

type GaugeSummary struct {
	Calls []MetricsArgs
	Last  float64
	Max   float64

GaugeSummary contains a summary of all Gauge method calls to a particular StatsClient for a particular key.

type MetricsArgs

type MetricsArgs struct {
	Name  string
	Value float64
	Tags  []string
	Rate  float64

MetricsArgs represents arguments to a StatsClient Gauge method call.

type MockEngine

type MockEngine struct {
	// contains filtered or unexported fields

MockEngine mocks a sampler engine

func NewMockEngine

func NewMockEngine(wantSampled bool) *MockEngine

NewMockEngine returns a MockEngine for tests

func (*MockEngine) GetState

func (e *MockEngine) GetState() interface{}

GetState mocks Engine.GetState()

func (*MockEngine) GetType

func (e *MockEngine) GetType() sampler.EngineType

GetType mocks Engine.GetType()

func (*MockEngine) Run

func (e *MockEngine) Run()

Run mocks Engine.Run()

func (*MockEngine) Sample

func (e *MockEngine) Sample(_ pb.Trace, _ *pb.Span, _ string) bool

Sample returns a constant rate

func (*MockEngine) Stop

func (e *MockEngine) Stop()

Stop mocks Engine.Stop()

type SpanConfig

type SpanConfig struct {
	// MinTags specifies the minimum number of tags this span should have.
	MinTags int
	// MaxTags specifies the maximum number of tags this span should have.
	MaxTags int

SpanConfig defines the configuration for generating spans.

type TestBackoffTimer

type TestBackoffTimer struct {
	// contains filtered or unexported fields

TestBackoffTimer is a backoff timer that ticks on-demand.

func NewTestBackoffTimer

func NewTestBackoffTimer() *TestBackoffTimer

NewTestBackoffTimer creates a new instance of a TestBackoffTimer.

func (*TestBackoffTimer) Close

func (t *TestBackoffTimer) Close()

Close closes the ticking channel of this backoff timer.

func (*TestBackoffTimer) CurrentDelay

func (t *TestBackoffTimer) CurrentDelay() time.Duration

CurrentDelay in a TestBackoffTimer always returns 0.

func (*TestBackoffTimer) NumRetries

func (t *TestBackoffTimer) NumRetries() int

NumRetries in a TestBackoffTimer always returns 0.

func (*TestBackoffTimer) ReceiveTick

func (t *TestBackoffTimer) ReceiveTick() <-chan time.Time

ReceiveTick returns the channel where ticks are sent.

func (*TestBackoffTimer) Reset

func (t *TestBackoffTimer) Reset()

Reset in a TestBackoffTimer is a no-op.

func (*TestBackoffTimer) ScheduleRetry

func (t *TestBackoffTimer) ScheduleRetry(err error) (int, time.Duration)

ScheduleRetry on a TestBackoffTimer is a no-op.

func (*TestBackoffTimer) Stop

func (t *TestBackoffTimer) Stop()

Stop in a TestBackoffTimer is a no-op.

func (*TestBackoffTimer) TriggerTick

func (t *TestBackoffTimer) TriggerTick()

TriggerTick immediately sends a tick with the current timestamp through the ticking channel.

type TestStatsClient

type TestStatsClient struct {
	GaugeErr       error
	GaugeCalls     []MetricsArgs
	CountErr       error
	CountCalls     []MetricsArgs
	HistogramErr   error
	HistogramCalls []MetricsArgs
	TimingErr      error
	TimingCalls    []MetricsArgs
	// contains filtered or unexported fields

TestStatsClient is a mocked StatsClient that records all calls and replies with configurable error return values.

func (*TestStatsClient) Count

func (c *TestStatsClient) Count(name string, value int64, tags []string, rate float64) error

Count records a call to a Count operation and replies with CountErr

func (*TestStatsClient) Flush

func (c *TestStatsClient) Flush() error

Flush implements metrics.StatsClient

func (*TestStatsClient) Gauge

func (c *TestStatsClient) Gauge(name string, value float64, tags []string, rate float64) error

Gauge records a call to a Gauge operation and replies with GaugeErr

func (*TestStatsClient) GetCountSummaries

func (c *TestStatsClient) GetCountSummaries() map[string]*CountSummary

GetCountSummaries computes summaries for all names supplied as parameters to Count calls.

func (*TestStatsClient) GetGaugeSummaries

func (c *TestStatsClient) GetGaugeSummaries() map[string]*GaugeSummary

GetGaugeSummaries computes summaries for all names supplied as parameters to Gauge calls.

func (*TestStatsClient) Histogram

func (c *TestStatsClient) Histogram(name string, value float64, tags []string, rate float64) error

Histogram records a call to a Histogram operation and replies with HistogramErr

func (*TestStatsClient) Reset

func (c *TestStatsClient) Reset()

Reset resets client's internal records.

func (*TestStatsClient) Timing

func (c *TestStatsClient) Timing(name string, value time.Duration, tags []string, rate float64) error

Timing records a call to a Timing operation.

type TraceConfig

type TraceConfig struct {
	// MinSpans specifies the minimum number of spans per trace.
	MinSpans int
	// MaxSpans specifies the maximum number of spans per trace.
	MaxSpans int
	// Keep reports whether this trace should be marked as sampling priority
	// "User Keep"
	Keep bool

TraceConfig specifies trace generating configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL