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

immediately fire a timer set to a negative duration #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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