testclock

package
v2.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package testclock provides a clock.Clock for tests that advances only when told to.

A TestClock starts at a fixed time and never moves on its own. Timers started with After fire when the test calls Advance or Set and moves the clock past their due time. BlockUntil waits until the code under test has started a given number of timers, so a test can advance the clock exactly when the code is waiting, without sleeping.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestClock

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

TestClock is a manually driven clock.Clock. It is safe for concurrent use.

func New

func New(start time.Time) *TestClock

New creates a test clock that starts at the given time.

Parameters:

  • start: The initial time of the clock

Returns:

  • *TestClock: A clock that only moves with Advance and Set
Example

ExampleNew creates a test clock at a fixed time.

package main

import (
	"fmt"
	"time"

	"github.com/IBM/fp-go/v2/clock/testclock"
)

var start = time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
	c := testclock.New(start)
	fmt.Println(c.Now().Format(time.RFC3339))
}
Output:
2024-01-01T00:00:00Z

func (*TestClock) Advance

func (c *TestClock) Advance(d time.Duration)

Advance moves the clock forward by d and fires every timer that is due, in the order of their due times. Timers with the same due time fire in the order they were started.

Parameters:

  • d: The duration to advance the clock by
Example

ExampleTestClock_Advance fires the timers that become due.

package main

import (
	"fmt"
	"time"

	"github.com/IBM/fp-go/v2/clock/testclock"
)

var start = time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
	c := testclock.New(start)
	short, _ := c.After(time.Second)
	long, _ := c.After(time.Hour)

	c.Advance(time.Minute)
	fmt.Println((<-short).Sub(start))

	c.Advance(time.Hour)
	fmt.Println((<-long).Sub(start))
}
Output:
1s
1h0m0s

func (*TestClock) After

func (c *TestClock) After(d time.Duration) (<-chan time.Time, func() bool)

After starts a timer that fires once the clock has advanced by d.

A timer with a duration of zero or less fires immediately. The returned stop function removes a pending timer and reports whether it was still pending.

Example

ExampleTestClock_After starts a timer that fires when the clock is advanced.

package main

import (
	"fmt"
	"time"

	"github.com/IBM/fp-go/v2/clock/testclock"
)

var start = time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
	c := testclock.New(start)
	ch, _ := c.After(time.Minute)
	c.Advance(time.Minute)
	fmt.Println((<-ch).Format(time.Kitchen))
}
Output:
12:01AM

func (*TestClock) BlockUntil

func (c *TestClock) BlockUntil(n int)

BlockUntil waits until at least n timers are pending, that is started and neither fired nor stopped.

Parameters:

  • n: The number of pending timers to wait for
Example

ExampleTestClock_BlockUntil waits until another goroutine is sleeping before advancing.

package main

import (
	"fmt"
	"time"

	"github.com/IBM/fp-go/v2/clock/testclock"
)

var start = time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
	c := testclock.New(start)
	done := make(chan time.Time)
	go func() {
		ch, stop := c.After(time.Hour)
		defer stop()
		done <- <-ch
	}()

	c.BlockUntil(1)
	c.Advance(time.Hour)
	fmt.Println((<-done).Format(time.Kitchen))
}
Output:
1:00AM

func (*TestClock) Now

func (c *TestClock) Now() time.Time

Now returns the current time of the clock.

Example

ExampleTestClock_Now shows that the clock does not move on its own.

package main

import (
	"fmt"
	"time"

	"github.com/IBM/fp-go/v2/clock/testclock"
)

var start = time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
	c := testclock.New(start)
	fmt.Println(c.Now().Equal(c.Now()))
}
Output:
true

func (*TestClock) Set

func (c *TestClock) Set(t time.Time)

Set moves the clock to t and fires every timer that is due, in the order of their due times. Moving the clock backwards fires no timers.

Parameters:

  • t: The new time of the clock
Example

ExampleTestClock_Set moves the clock to an absolute time.

package main

import (
	"fmt"
	"time"

	"github.com/IBM/fp-go/v2/clock/testclock"
)

var start = time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
	c := testclock.New(start)
	c.Set(start.AddDate(0, 0, 1))
	fmt.Println(c.Now().Format(time.DateOnly))
}
Output:
2024-01-02

Jump to

Keyboard shortcuts

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