testlib

package
v0.0.0-...-967d409 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockClock

type MockClock interface {
	clock.Clock
	SetNow(time.Time)

	// GetNextFireTime returns the time that the next Timer will fire, or the zero value if no timers
	// are set.
	GetNextFireTime() time.Time
}

MockClock is an extension of Clock that adds the ability to set the current time. Now returns the value passed to SetNow until a new value is set.

Timers created by a MockClock will fire once the clock's time is set to or after the calculated fire time. This helps enable deterministic tests involving timers. However, because a MockClock's time doesn't continuously increase, a couple of considerations should be followed to avoid racy conditions in tests.

  1. Avoid calls to Clock.Now() outside of the test thread. For example, when creating timers asynchronously, avoid calls to clock.Now() and instead get the base time from the test thread. e.g.,

    func NewComponent(c clock.Clock) *Component { c = &Component{clock: c} go c.run(c.Now()) // Retrieve the initial time from this thread. }

    func (c *Component) run(now time.Time) { fireAt := now.Add(someDelay) for { tmr := c.clock.NewTimerAt(fireAt) select { case <-c.someChan: c.handleEvent()

    case n := <-tmr.GetC(): c.somePeriodicOperation() // Compute next fire time based on value received from timer. fireAt = n.Add(someDelay) } tmr.Cancel() }

  1. When creating a new timer, use Clock.NewTimerAt(time.Time) to specify a point in time at which the timer should fire, and calculate that time based on a known base time (#1). Using Clock.NewTimer(time.Duration) can lead to race conditions in tests:

    d := someDelay - c.clock.Now().Sub(c.lastFireTime) // <-- if the MockClock's time is advanced here, the new timer may not fire when expected. tmr := c.clock.NewTimer(d)

    Instead:

    now := c.clock.Now() nextFire := now.Add(someDelay - now.Sub(c.lastFireTime)) tmr := c.clock.NewTimerAt(nextFire)

func NewMockClock

func NewMockClock() MockClock

NewMockClock creates a new MockClock instance that initially returns time zero.

type MockEndpoint

type MockEndpoint struct {
	Used     bool
	Released bool
	// contains filtered or unexported fields
}

Type MockEndpoint is a mock endpoint.Endpoint.

func NewMockEndpoint

func NewMockEndpoint(name string) *MockEndpoint

NewMockEndpoint creates a new MockEndpoint with the given name.

func (*MockEndpoint) BuildReport

func (*MockEndpoint) Calls

func (wfc *MockEndpoint) Calls() int32

func (*MockEndpoint) DoAndWait

func (wfc *MockEndpoint) DoAndWait(t *testing.T, calls int32, f func())

DoAndWait executes the given function and then waits until the total number of calls reaches the given value.

func (*MockEndpoint) IsTransient

func (ep *MockEndpoint) IsTransient(err error) bool

func (*MockEndpoint) Name

func (ep *MockEndpoint) Name() string

func (*MockEndpoint) Release

func (ep *MockEndpoint) Release() error

func (*MockEndpoint) Reports

func (ep *MockEndpoint) Reports() (reports []pipeline.EndpointReport)

func (*MockEndpoint) Send

func (ep *MockEndpoint) Send(report pipeline.EndpointReport) error

func (*MockEndpoint) SetBuildErr

func (ep *MockEndpoint) SetBuildErr(err error)

func (*MockEndpoint) SetSendErr

func (ep *MockEndpoint) SetSendErr(err error)

func (*MockEndpoint) Use

func (ep *MockEndpoint) Use()

type MockInput

type MockInput struct {
	Used     bool
	Released bool
	// contains filtered or unexported fields
}

Type MockInput is a mock pipeline.Input.

func NewMockInput

func NewMockInput() *MockInput

NewMockSender creates a new MockInput.

func (*MockInput) AddReport

func (i *MockInput) AddReport(report metrics.MetricReport) error

func (*MockInput) Calls

func (wfc *MockInput) Calls() int32

func (*MockInput) DoAndWait

func (wfc *MockInput) DoAndWait(t *testing.T, calls int32, f func())

DoAndWait executes the given function and then waits until the total number of calls reaches the given value.

func (*MockInput) Release

func (i *MockInput) Release() error

func (*MockInput) Reports

func (i *MockInput) Reports() (reports []metrics.MetricReport)

func (*MockInput) SetAddError

func (i *MockInput) SetAddError(err error)

func (*MockInput) Use

func (i *MockInput) Use()

type MockSender

type MockSender struct {
	Used     bool
	Released bool
	// contains filtered or unexported fields
}

Type MockSender is a mock sender.Sender.

func NewMockSender

func NewMockSender(endpoints ...string) *MockSender

NewMockSender creates a new MockSender with the given endpoint IDs.

func (*MockSender) Calls

func (wfc *MockSender) Calls() int32

func (*MockSender) DoAndWait

func (wfc *MockSender) DoAndWait(t *testing.T, calls int32, f func())

DoAndWait executes the given function and then waits until the total number of calls reaches the given value.

func (*MockSender) Endpoints

func (s *MockSender) Endpoints() []string

func (*MockSender) Release

func (s *MockSender) Release() error

func (*MockSender) Reports

func (s *MockSender) Reports() (reports []metrics.MetricReport)

func (*MockSender) Send

func (s *MockSender) Send(report metrics.StampedMetricReport) error

func (*MockSender) SetSendError

func (s *MockSender) SetSendError(err error)

func (*MockSender) Use

func (s *MockSender) Use()

type MockStatsRecorder

type MockStatsRecorder struct {
	// contains filtered or unexported fields
}

Type MockStatsRecorder is a mock stats.StatsRecorder.

func NewMockStatsRecorder

func NewMockStatsRecorder() *MockStatsRecorder

func (*MockStatsRecorder) Calls

func (wfc *MockStatsRecorder) Calls() int32

func (*MockStatsRecorder) DoAndWait

func (wfc *MockStatsRecorder) DoAndWait(t *testing.T, calls int32, f func())

DoAndWait executes the given function and then waits until the total number of calls reaches the given value.

func (*MockStatsRecorder) Failed

func (sr *MockStatsRecorder) Failed() []RecordedEntry

func (*MockStatsRecorder) Register

func (sr *MockStatsRecorder) Register(id string, handlers []string)

func (*MockStatsRecorder) Registered

func (sr *MockStatsRecorder) Registered() map[string][]string

func (*MockStatsRecorder) SendFailed

func (sr *MockStatsRecorder) SendFailed(id string, handler string)

func (*MockStatsRecorder) SendSucceeded

func (sr *MockStatsRecorder) SendSucceeded(id string, handler string)

func (*MockStatsRecorder) Succeeded

func (sr *MockStatsRecorder) Succeeded() []RecordedEntry

type RecordedEntry

type RecordedEntry struct {
	Id      string
	Handler string
}

Jump to

Keyboard shortcuts

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