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

Commit

Permalink
Merge pull request #50 from marten-seemann/immediately-fire-negative-…
Browse files Browse the repository at this point in the history
…timer

immediately fire a timer set to a negative duration
  • Loading branch information
djmitche committed Apr 27, 2023
2 parents 2fbf0cb + b9ae427 commit 4362d70
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 @@ -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,
Expand All @@ -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
}

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

0 comments on commit 4362d70

Please sign in to comment.