clockmock

package
v1.14.1-rc.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package clockmock provides tools for mocking time functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mock

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

Mock is a mock implementation of the Clock

func New

func New(ctx context.Context) *Mock

New returns a new mocked clock

func (*Mock) Add

func (m *Mock) Add(d time.Duration)

Add moves the current time of the mock clock forward by the specified duration.

func (*Mock) After

func (m *Mock) After(d time.Duration) <-chan time.Time

After is a shortcut for the m.Timer(d).C()

func (*Mock) AfterFunc

func (m *Mock) AfterFunc(d time.Duration, f func()) clock.Timer

AfterFunc returns a timer that will call f when the mock current time becomes > m.Now().Add(d)

func (*Mock) Now

func (m *Mock) Now() time.Time

Now returns mock current time

func (*Mock) Set

func (m *Mock) Set(t time.Time)

Set sets the current time of the mock clock to a specific one.

func (*Mock) SetSpeed

func (m *Mock) SetSpeed(speed float64)

SetSpeed starts mock time to run with the given speed until Mock.ctx becomes done or speed becomes changed. While time is running, current time for the mock will be the following:

mock time := mock start time  +  (real time duration from the start) * speed  +  mock duration added with Set, Add

func (*Mock) Since

func (m *Mock) Since(t time.Time) time.Duration

Since is a shortcut for the m.Now().Sub(t)

func (*Mock) Sleep

func (m *Mock) Sleep(d time.Duration)

Sleep waits for the mock current time becomes > m.Now().Add(d)

func (*Mock) Ticker

func (m *Mock) Ticker(d time.Duration) clock.Ticker

Ticker returns a ticker that will fire every time when the mock current time becomes > mock previous time + d

func (*Mock) Timer

func (m *Mock) Timer(d time.Duration) clock.Timer

Timer returns a timer that will fire when the mock current time becomes > m.Now().Add(d)

func (*Mock) Until

func (m *Mock) Until(t time.Time) time.Duration

Until is a shortcut for the t.Sub(m.Now())

func (*Mock) WithDeadline

func (m *Mock) WithDeadline(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc)

WithDeadline wraps parent in a new context that will cancel when the mock current time becomes > deadline

func (*Mock) WithTimeout

func (m *Mock) WithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

WithTimeout is a shortcut for the m.WithDeadline(parent, m.Now().Add(timeout))

Jump to

Keyboard shortcuts

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