Skip to content
flextime improves time testability by replacing the backend clock flexibly.
Go Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows initial implement Jan 15, 2020
.gitignore initial implement Jan 15, 2020
CHANGELOG.md Checking in changes prior to tagging of version v0.0.6 Jan 20, 2020
LICENSE
Makefile initial implement Jan 15, 2020
README.md add doc.go Jan 18, 2020
clock.go make Timer and Ticker struct Jan 18, 2020
clock_test.go adjust testing Jan 19, 2020
doc.go add doc.go Jan 18, 2020
fakeclock.go make Timer and Ticker struct Jan 18, 2020
faketicker.go
faketicker_test.go test for panic Jan 19, 2020
faketimer.go capacity of Timer.C should be 1 Jan 19, 2020
faketimer_test.go add TestTimer_Stop Jan 18, 2020
fix.go initial implement Jan 15, 2020
flextime.go make Timer and Ticker struct Jan 18, 2020
flextime_test.go add TestRestore Jan 18, 2020
func.go initial implement Jan 15, 2020
func_test.go adjust testing Jan 19, 2020
go.mod initial implement Jan 15, 2020
offset.go use RWMutex in offsetClock Jan 20, 2020
realclock.go make Timer and Ticker struct Jan 18, 2020
realclock_test.go adjust testing Jan 19, 2020
version.go Checking in changes prior to tagging of version v0.0.6 Jan 20, 2020

README.md

flextime

Test Status Coverage Status MIT License GoDoc

flextime improves time testability by replacing the backend clock flexibly.

Synopsis

import "github.com/Songmu/flextime"

now := flextime.Now() // returned normal current time by default
flextime.Sleep()
d := flextime.Until(date)
d := flextime.Since(date)
<-flextime.After(5*time.Second)
flextime.AfterFunc(5*time.Second, func() { fmt.Println("Done") })
timer := flextime.NewTimer(10*time.Second)
ticker := flextime.NewTicker(10*time.Second)
ch := flextime.Tick(3*time.Second)

func () { // Set time
    restore := flextime.Set(time.Date(2001, time.May, 1, 10, 10, 10, 0, time.UTC))
    defer restore()

    now = flextime.Now() // returned set time
}()

func () { // Fix time
    restore := flextime.Fix(time.Date(2001, time.May, 1, 10, 10, 10, 0, time.UTC))
    defer restore()

    now = flextime.Now() // returned fixed time
}()

Description

The flextime improves time testability by replacing the backend clock flexibly.

It has a set of functions similar to the standard time package, making it easy to migrate from standard time package.

By default, it behaves the same as the standard time package, but allows us to change or fix the current time by using Fix and Set function.

Also, we can replace the backend clock by implementing our own Clock interface and combining it with the Switch function.

Installation

% go get github.com/Songmu/flextime

Migration

You can almost migrate from standard time package to Songmu/flextime with the following command.

% go get github.com/Songmu/flextime
% find . -name '*.go' | xargs perl -i -pe 's/\btime\.((?:N(?:ewTi(?:ck|m)er|ow)|After(?:Func)?|Sleep|Until|Tick))/flextime.$1/g'
% goimport -w .

Author

Songmu

You can’t perform that action at this time.