diff --git a/clock.go b/clock.go index 7f7c52d..dd93c21 100644 --- a/clock.go +++ b/clock.go @@ -249,7 +249,6 @@ func (m *Mock) Ticker(d time.Duration) *Ticker { // Timer creates a new instance of Timer. func (m *Mock) Timer(d time.Duration) *Timer { m.mu.Lock() - defer m.mu.Unlock() ch := make(chan time.Time, 1) t := &Timer{ C: ch, @@ -259,6 +258,8 @@ func (m *Mock) Timer(d time.Duration) *Timer { stopped: false, } m.timers = append(m.timers, (*internalTimer)(t)) + m.mu.Unlock() + m.runNextTimer(m.now) return t } diff --git a/clock_test.go b/clock_test.go index 34c649f..5c10efc 100644 --- a/clock_test.go +++ b/clock_test.go @@ -195,6 +195,16 @@ func TestClock_Timer_Reset(t *testing.T) { } } +func TestClock_NegativeDuration(t *testing.T) { + clock := NewMock() + timer := clock.Timer(-time.Second) + select { + case <-timer.C: + default: + t.Fatal("timer should have fired immediately") + } +} + // Ensure reset can be called immediately after reading channel func TestClock_Timer_Reset_Unlock(t *testing.T) { clock := NewMock()