Skip to content
This repository was archived by the owner on May 18, 2023. It is now read-only.

Commit 4362d70

Browse files
authored
Merge pull request #50 from marten-seemann/immediately-fire-negative-timer
immediately fire a timer set to a negative duration
2 parents 2fbf0cb + b9ae427 commit 4362d70

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ func (m *Mock) Ticker(d time.Duration) *Ticker {
249249
// Timer creates a new instance of Timer.
250250
func (m *Mock) Timer(d time.Duration) *Timer {
251251
m.mu.Lock()
252-
defer m.mu.Unlock()
253252
ch := make(chan time.Time, 1)
254253
t := &Timer{
255254
C: ch,
@@ -259,6 +258,8 @@ func (m *Mock) Timer(d time.Duration) *Timer {
259258
stopped: false,
260259
}
261260
m.timers = append(m.timers, (*internalTimer)(t))
261+
m.mu.Unlock()
262+
m.runNextTimer(m.now)
262263
return t
263264
}
264265

clock_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ func TestClock_Timer_Reset(t *testing.T) {
195195
}
196196
}
197197

198+
func TestClock_NegativeDuration(t *testing.T) {
199+
clock := NewMock()
200+
timer := clock.Timer(-time.Second)
201+
select {
202+
case <-timer.C:
203+
default:
204+
t.Fatal("timer should have fired immediately")
205+
}
206+
}
207+
198208
// Ensure reset can be called immediately after reading channel
199209
func TestClock_Timer_Reset_Unlock(t *testing.T) {
200210
clock := NewMock()

0 commit comments

Comments
 (0)