now

package
v0.0.0-...-50fdeb8 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: BSD-3-Clause Imports: 4 Imported by: 2

Documentation

Overview

Package now provides a function to return the current time that is also easily overridden for testing.

Index

Constants

View Source
const ContextKey contextKeyType = "overwriteNow"

ContextKey is used by tests to make the time deterministic.

That is, in a test, you can write a value into a context to use as the return value of Now().

var mockTime = time.Unix(0, 12).UTC()
ctx = context.WithValue(ctx, now.ContextKey, mockTime)

The value set can also be a function that returns a time.Time.

   var monotonicTime int64 = 0
   var mockTimeProvider = func() time.Time {
     monotonicTime += 1
	    return time.Unix(monotonicTime, 0).UTC()
   }
   ctx = context.WithValue(ctx, now.ContextKey, now.NowProvider(mockTimeProvider))

Variables

This section is empty.

Functions

func Now

func Now(ctx context.Context) time.Time

Now returns the current time or the time from the context.

Types

type NewTimeTickerFunc

type NewTimeTickerFunc func(time.Duration) TimeTicker

NewTimeTickerFunc is a function which takes a time.Duration and returns a TimeTicker.

type NowProvider

type NowProvider func() time.Time

NowProvider is the type of function that can also be passed as a context value. The function will be evaluated every time Now() is called with that context. NowProvider should be threadsafe if the context is used across threads. Clients that need the time to vary throughout tests should probably use TimeTravelCtx

type TimeTicker

type TimeTicker interface {
	C() <-chan time.Time
	Reset(d time.Duration)
	Stop()
}

TimeTicker provides an interface around time.Ticker so that it can be mocked.

func NewTimeTicker

func NewTimeTicker(d time.Duration) TimeTicker

NewTimeTicker returns a TimeTicker which wraps a real time.Ticker.

type TimeTickerImpl

type TimeTickerImpl struct {
	*time.Ticker
}

TimeTickerImpl implements TimeTicker using a real time.Ticker.

func (*TimeTickerImpl) C

func (t *TimeTickerImpl) C() <-chan time.Time

C implements TimeTicker.

type TimeTravelCtx

type TimeTravelCtx struct {
	context.Context
	// contains filtered or unexported fields
}

TimeTravelCtx is a test utility that makes it easy to change the apparent time. It embeds a context that contains a NowProvider to overwrite the time returned by now.Now(ctx). As an example of how this might be used in a test:

ctx := now.TimeTravelingContext(tsOne)
result1 := myTestFunction(ctx, "param one")
// simulate fast forwarding 2 minutes
ctx.SetTime(tsOne.Add(2 * time.Minute))
result2 := myTestFunction(ctx, "another param")
// do assertions on result1 and result2

func TimeTravelingContext

func TimeTravelingContext(start time.Time) *TimeTravelCtx

TimeTravelingContext returns a *TimeTravelCtx, using the given time and the background context.

func (*TimeTravelCtx) SetTime

func (t *TimeTravelCtx) SetTime(newTime time.Time)

SetTime updates the underlying time that will be returned by the embedded context's NowProvider. It is thread-safe.

func (*TimeTravelCtx) WithContext

func (t *TimeTravelCtx) WithContext(ctx context.Context) *TimeTravelCtx

WithContext replaces the embedded context with one derived from the passed in context. It is thread-safe, but tests should strive to use it in a non-threaded way for simplicity.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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