Skip to content

Commit

Permalink
Add regression test for mock timer Reset() bug and fix it.
Browse files Browse the repository at this point in the history
  • Loading branch information
rynorris authored and StefanKopieczek committed Oct 14, 2016
1 parent d5889cc commit e8a5d64
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
6 changes: 1 addition & 5 deletions timing/timing.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ func removeMockTimer(t *mockTimer) bool {

if found {
// We found the given timer. Remove it.
if idx == len(mockTimers)-1 {
mockTimers = mockTimers[:idx]
} else {
mockTimers = append(mockTimers[:idx], mockTimers[idx+1])
}
mockTimers = append(mockTimers[:idx], mockTimers[idx+1:]...)
return true
} else {
// The timer was not present, indicating that it was already expired.
Expand Down
40 changes: 40 additions & 0 deletions timing/timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,43 @@ func TestNotExpiredReset(t *testing.T) {
t.Fatal("Timer didn't fire at it's new end time after being reset.")
}
}

// This is a regression test for a bug where:
// - Create 3 timers.
// - Reset() the first one.
// - The third timer is now no longer tracked and won't fire.
func TestThreeTimersWithReset(t *testing.T) {
MockMode = true
timer1 := NewTimer(1 * time.Second)
done1 := make(chan struct{})

timer2 := NewTimer(2 * time.Second)
done2 := make(chan struct{})

timer3 := NewTimer(3 * time.Second)
done3 := make(chan struct{})

go func() {
<-timer1.C()
done1 <- struct{}{}
}()

go func() {
<-timer2.C()
done2 <- struct{}{}
}()

go func() {
<-timer3.C()
done3 <- struct{}{}
}()

timer1.Reset(4 * time.Second)

Elapse(2 * time.Second)
<-done2

Elapse(1 * time.Second)
// Panic here if bug exists.
<-done3
}

0 comments on commit e8a5d64

Please sign in to comment.