From ee43e6cb644b0375889cb0a04afb87250860a48a Mon Sep 17 00:00:00 2001 From: Richard Chew Date: Fri, 23 Mar 2018 12:10:45 -0700 Subject: [PATCH] NilTimer executes given function NilTimer preserves the contract of Time() by executing the function passed to it --- timer.go | 4 ++-- timer_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/timer.go b/timer.go index d6ec4c6..aa7e895 100644 --- a/timer.go +++ b/timer.go @@ -125,8 +125,8 @@ func (NilTimer) Stop() {} // Sum is a no-op. func (NilTimer) Sum() int64 { return 0 } -// Time is a no-op. -func (NilTimer) Time(func()) {} +// Time guarantees execution of the given function, but is otherwise a no-op. +func (NilTimer) Time(f func()) { f() } // Update is a no-op. func (NilTimer) Update(time.Duration) {} diff --git a/timer_test.go b/timer_test.go index f85c9b8..98bdde2 100644 --- a/timer_test.go +++ b/timer_test.go @@ -23,6 +23,15 @@ func TestGetOrRegisterTimer(t *testing.T) { } } +func TestNilTimerFunc(t *testing.T) { + tm := NilTimer{} + b := false + tm.Time(func() { b = true }) + if !b { + t.Errorf("tm.Time(func()) did not execute argument") + } +} + func TestTimerExtremes(t *testing.T) { tm := NewTimer() tm.Update(math.MaxInt64)