use clock so that we can test the package
@@ -2,12 +2,14 @@ package wrapper
import (
"context"
+ "github.com/alaingilbert/clockwork"
"time"
)
// ExponentialBackoff ...
type ExponentialBackoff struct {
ctx context.Context
+ clock clockwork.Clock
val int
max int
}
@@ -19,10 +21,15 @@ func NewExponentialBackoff(ctx context.Context, max int) *ExponentialBackoff {
}
e := new(ExponentialBackoff)
e.ctx = ctx
+ e.clock = clockwork.NewRealClock()
e.max = max
return e
}
+func (e *ExponentialBackoff) SetClock(clock clockwork.Clock) {
+ e.clock = clock
+}
+
// LoopForever execute the callback with exponential backoff
// The callback return true if we should continue retrying
// or false if we should stop and exit.
@@ -47,7 +54,7 @@ func (e *ExponentialBackoff) Wait() {
e.val = 1
} else {
select {
- case <-time.After(time.Duration(e.val) * time.Second):
+ case <-e.clock.After(time.Duration(e.val) * time.Second):
case <-e.ctx.Done():
return
}
