Update jonboulle/clockwork (#2820)

This commit is contained in:
Pierre Beaucamp 2019-07-03 19:14:35 -04:00 committed by Alexander Klizhentas
parent 3c93347470
commit a15958b90a
4 changed files with 102 additions and 3 deletions

6
Gopkg.lock generated
View file

@ -514,11 +514,12 @@
revision = "0b12d6b5"
[[projects]]
digest = "1:f6144d249e2ddd5e33537e64710e5ffabdd4f75ee72b00e91090586af3e09b23"
branch = "master"
digest = "1:a92a261b44f2cf39cb3185dec2334de8bf2c2b2673077ec44b6f827a6efb282c"
name = "github.com/jonboulle/clockwork"
packages = ["."]
pruneopts = "NUT"
revision = "ed104f61ea4877bea08af6f759805674861e968d"
revision = "62fb9bc030d14f92c58df3c1601e50a0e445edef"
[[projects]]
digest = "1:42c47ace7ccb114261ef7e0d418d274921514ab50a3bf6bdb9e51c3dde8ce13d"
@ -1241,6 +1242,7 @@
"google.golang.org/grpc",
"google.golang.org/grpc/codes",
"google.golang.org/grpc/credentials",
"google.golang.org/grpc/keepalive",
"google.golang.org/grpc/status",
"gopkg.in/check.v1",
"gopkg.in/yaml.v2",

View file

@ -193,3 +193,8 @@ ignored = ["github.com/Sirupsen/logrus"]
[[constraint]]
name = "github.com/Microsoft/go-winio"
version = "=v0.4.9"
[[constraint]]
name = "github.com/jonboulle/clockwork"
branch = "master"

View file

@ -11,6 +11,8 @@ type Clock interface {
After(d time.Duration) <-chan time.Time
Sleep(d time.Duration)
Now() time.Time
Since(t time.Time) time.Duration
NewTicker(d time.Duration) Ticker
}
// FakeClock provides an interface for a clock which can be
@ -39,7 +41,7 @@ func NewFakeClock() FakeClock {
return NewFakeClockAt(time.Date(1984, time.April, 4, 0, 0, 0, 0, time.UTC))
}
// NewFakeClock returns a FakeClock initialised at the given time.Time.
// NewFakeClockAt returns a FakeClock initialised at the given time.Time.
func NewFakeClockAt(t time.Time) FakeClock {
return &fakeClock{
time: t,
@ -60,6 +62,14 @@ func (rc *realClock) Now() time.Time {
return time.Now()
}
func (rc *realClock) Since(t time.Time) time.Duration {
return rc.Now().Sub(t)
}
func (rc *realClock) NewTicker(d time.Duration) Ticker {
return &realTicker{time.NewTicker(d)}
}
type fakeClock struct {
sleepers []*sleeper
blockers []*blocker
@ -130,6 +140,22 @@ func (fc *fakeClock) Now() time.Time {
return t
}
// Since returns the duration that has passed since the given time on the fakeClock
func (fc *fakeClock) Since(t time.Time) time.Duration {
return fc.Now().Sub(t)
}
func (fc *fakeClock) NewTicker(d time.Duration) Ticker {
ft := &fakeTicker{
c: make(chan time.Time, 1),
stop: make(chan bool, 1),
clock: fc,
period: d,
}
go ft.tick()
return ft
}
// Advance advances fakeClock to a new point in time, ensuring channels from any
// previous invocations of After are notified appropriately before returning
func (fc *fakeClock) Advance(d time.Duration) {

66
vendor/github.com/jonboulle/clockwork/ticker.go generated vendored Normal file
View file

@ -0,0 +1,66 @@
package clockwork
import (
"time"
)
// Ticker provides an interface which can be used instead of directly
// using the ticker within the time module. The real-time ticker t
// provides ticks through t.C which becomes now t.Chan() to make
// this channel requirement definable in this interface.
type Ticker interface {
Chan() <-chan time.Time
Stop()
}
type realTicker struct{ *time.Ticker }
func (rt *realTicker) Chan() <-chan time.Time {
return rt.C
}
type fakeTicker struct {
c chan time.Time
stop chan bool
clock FakeClock
period time.Duration
}
func (ft *fakeTicker) Chan() <-chan time.Time {
return ft.c
}
func (ft *fakeTicker) Stop() {
ft.stop <- true
}
// tick sends the tick time to the ticker channel after every period.
// Tick events are discarded if the underlying ticker channel does
// not have enough capacity.
func (ft *fakeTicker) tick() {
tick := ft.clock.Now()
for {
tick = tick.Add(ft.period)
remaining := tick.Sub(ft.clock.Now())
if remaining <= 0 {
// The tick should have already happened. This can happen when
// Advance() is called on the fake clock with a duration larger
// than this ticker's period.
select {
case ft.c <- tick:
default:
}
continue
}
select {
case <-ft.stop:
return
case <-ft.clock.After(remaining):
select {
case ft.c <- tick:
default:
}
}
}
}