adding copy feature and removing nested if/else

This commit is contained in:
Jose De La O 2018-09-03 02:52:33 -04:00
parent 8eab2debe7
commit 13a5cc4b99
2 changed files with 22 additions and 1 deletions

View File

@ -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,
}
}

View File

@ -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)