diff options
author | Felix Lange <fjl@twurst.com> | 2019-09-16 17:16:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-16 17:16:30 +0800 |
commit | b1c3010bf2c334241a8cb01eab110e5f1cd88f71 (patch) | |
tree | 91c3aa83639b19cc4cbb00bf89e5c5dec1632911 /common/mclock/mclock.go | |
parent | aff986958da831d85788f33fe11aab31995ac072 (diff) | |
download | go-tangerine-b1c3010bf2c334241a8cb01eab110e5f1cd88f71.tar go-tangerine-b1c3010bf2c334241a8cb01eab110e5f1cd88f71.tar.gz go-tangerine-b1c3010bf2c334241a8cb01eab110e5f1cd88f71.tar.bz2 go-tangerine-b1c3010bf2c334241a8cb01eab110e5f1cd88f71.tar.lz go-tangerine-b1c3010bf2c334241a8cb01eab110e5f1cd88f71.tar.xz go-tangerine-b1c3010bf2c334241a8cb01eab110e5f1cd88f71.tar.zst go-tangerine-b1c3010bf2c334241a8cb01eab110e5f1cd88f71.zip |
common/mclock: clean up AfterFunc support (#20054)
This change adds tests for the virtual clock and aligns the interface
with the time package by renaming Cancel to Stop. It also removes the
binary search from Stop because it complicates the code unnecessarily.
Diffstat (limited to 'common/mclock/mclock.go')
-rw-r--r-- | common/mclock/mclock.go | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/common/mclock/mclock.go b/common/mclock/mclock.go index 0c941082f..d0e0cd78b 100644 --- a/common/mclock/mclock.go +++ b/common/mclock/mclock.go @@ -36,47 +36,39 @@ func (t AbsTime) Add(d time.Duration) AbsTime { return t + AbsTime(d) } -// Clock interface makes it possible to replace the monotonic system clock with +// The Clock interface makes it possible to replace the monotonic system clock with // a simulated clock. type Clock interface { Now() AbsTime Sleep(time.Duration) After(time.Duration) <-chan time.Time - AfterFunc(d time.Duration, f func()) Event + AfterFunc(d time.Duration, f func()) Timer } -// Event represents a cancellable event returned by AfterFunc -type Event interface { - Cancel() bool +// Timer represents a cancellable event returned by AfterFunc +type Timer interface { + Stop() bool } // System implements Clock using the system clock. type System struct{} -// Now implements Clock. +// Now returns the current monotonic time. func (System) Now() AbsTime { return AbsTime(monotime.Now()) } -// Sleep implements Clock. +// Sleep blocks for the given duration. func (System) Sleep(d time.Duration) { time.Sleep(d) } -// After implements Clock. +// After returns a channel which receives the current time after d has elapsed. func (System) After(d time.Duration) <-chan time.Time { return time.After(d) } -// AfterFunc implements Clock. -func (System) AfterFunc(d time.Duration, f func()) Event { - return (*SystemEvent)(time.AfterFunc(d, f)) -} - -// SystemEvent implements Event using time.Timer. -type SystemEvent time.Timer - -// Cancel implements Event. -func (e *SystemEvent) Cancel() bool { - return (*time.Timer)(e).Stop() +// AfterFunc runs f on a new goroutine after the duration has elapsed. +func (System) AfterFunc(d time.Duration, f func()) Timer { + return time.AfterFunc(d, f) } |