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

Commit

Permalink
immediately fire a timer set to a negative duration
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Feb 25, 2023
1 parent 68df829 commit b9ae427
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,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,
Expand All @@ -229,6 +228,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
}

Expand Down
10 changes: 10 additions & 0 deletions clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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()
Expand Down

0 comments on commit b9ae427

Please # to comment.