Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
Fix crash if nil passed to NewTickerWithTimer
Browse files Browse the repository at this point in the history
Doc for function says that passing nil for the timer argument should
cause the default system timer to be used.  Ensure that that timer is
actually assigned.
  • Loading branch information
gwatts authored and cenkalti committed Apr 3, 2020
1 parent e13aad9 commit 78afdac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func NewTicker(b BackOff) *Ticker {
// NewTickerWithTimer returns a new Ticker with a custom timer.
// A default timer that uses system timer is used when nil is passed.
func NewTickerWithTimer(b BackOff, timer Timer) *Ticker {
if timer == nil {
timer = &defaultTimer{}
}
c := make(chan time.Time)
t := &Ticker{
C: c,
Expand Down
7 changes: 7 additions & 0 deletions ticker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@ func TestTickerContext(t *testing.T) {
t.Errorf("invalid number of retries: %d", i)
}
}

func TestTickerDefaultTimer(t *testing.T) {
b := NewExponentialBackOff()
ticker := NewTickerWithTimer(b, nil)
// ensure a timer was actually assigned, instead of remaining as nil.
<-ticker.C
}

0 comments on commit 78afdac

Please sign in to comment.