From 13a5cc4b9992250cea4e82057acf496c3a15000f Mon Sep 17 00:00:00 2001 From: Jose De La O Date: Mon, 3 Sep 2018 02:52:33 -0400 Subject: [PATCH] adding copy feature and removing nested if/else --- backoff.go | 13 ++++++++++++- backoff_test.go | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/backoff.go b/backoff.go index a50d0e9..b4941b6 100644 --- a/backoff.go +++ b/backoff.go @@ -71,7 +71,8 @@ func (b *Backoff) ForAttempt(attempt float64) time.Duration { //keep within bounds if dur < min { return min - } else if dur > max { + } + if dur > max { return max } return dur @@ -86,3 +87,13 @@ func (b *Backoff) Reset() { func (b *Backoff) Attempt() float64 { return b.attempt } + +// Copy returns a backoff with equals constraints as the original +func (b *Backoff) Copy() *Backoff { + return &Backoff{ + Factor: b.Factor, + Jitter: b.Jitter, + Min: b.Min, + Max: b.Max, + } +} diff --git a/backoff_test.go b/backoff_test.go index cb36833..d1c9845 100644 --- a/backoff_test.go +++ b/backoff_test.go @@ -110,6 +110,16 @@ func TestJitter(t *testing.T) { equals(t, b.Duration(), 100*time.Millisecond) } +func TestCopy(t *testing.T) { + b := &Backoff{ + Min: 100 * time.Millisecond, + Max: 10 * time.Second, + Factor: 2, + } + b2 := b.Copy() + equals(t, b, b2) +} + func between(t *testing.T, actual, low, high time.Duration) { if actual < low { t.Fatalf("Got %s, Expecting >= %s", actual, low)