From ba42345d06bf8fa22c0ebd1c5ff2479336745853 Mon Sep 17 00:00:00 2001 From: Madhav Jivrajani Date: Fri, 22 Oct 2021 11:53:16 +0530 Subject: [PATCH 1/2] Intorduce new interface WithTickerAndDelayedExecution Signed-off-by: Madhav Jivrajani --- clock/clock.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clock/clock.go b/clock/clock.go index dd181ce8..b8b6af5c 100644 --- a/clock/clock.go +++ b/clock/clock.go @@ -63,6 +63,16 @@ type WithDelayedExecution interface { AfterFunc(d time.Duration, f func()) Timer } +// WithTickerAndDelayedExecution allows for injecting fake or real clocks +// into code that needs Ticker and AfterFunc functionality +type WithTickerAndDelayedExecution interface { + WithTicker + // AfterFunc executes f in its own goroutine after waiting + // for d duration and returns a Timer whose channel can be + // closed by calling Stop() on the Timer. + AfterFunc(d time.Duration, f func()) Timer +} + // Ticker defines the Ticker interface. type Ticker interface { C() <-chan time.Time From 2719022a384ac7e3d55425859a8b2df2015b1fe9 Mon Sep 17 00:00:00 2001 From: Madhav Jivrajani Date: Fri, 3 Dec 2021 12:05:22 +0530 Subject: [PATCH 2/2] clock: Re-implement `NewTicker` for IntervalClock Eventhough the implementation is just a panic, re-implementing the `NewTicker` method helps in migration of the apimachinery/util/clock pkg to the utils/clock pkg in terms of having identical clients for `IntervalClock` in both packages as well as the `IntervalClock` implementing the `WithTickerAndDelayedExcution` interface. Signed-off-by: Madhav Jivrajani --- clock/testing/fake_clock.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clock/testing/fake_clock.go b/clock/testing/fake_clock.go index fb493c4b..a2b42bbc 100644 --- a/clock/testing/fake_clock.go +++ b/clock/testing/fake_clock.go @@ -282,9 +282,9 @@ func (*IntervalClock) Tick(d time.Duration) <-chan time.Time { // NewTicker has no implementation yet and is omitted. // TODO: make interval clock use FakeClock so this can be implemented. -//func (*IntervalClock) NewTicker(d time.Duration) clock.Ticker { -// panic("IntervalClock doesn't implement NewTicker") -//} +func (*IntervalClock) NewTicker(d time.Duration) clock.Ticker { + panic("IntervalClock doesn't implement NewTicker") +} // Sleep is unimplemented, will panic. func (*IntervalClock) Sleep(d time.Duration) {