Merge pull request #1 from josedh/feature-copy

adding copy feature and removing nested if/else
This commit is contained in:
Jose De La O Hernandez 2018-09-03 02:54:24 -04:00 committed by GitHub
commit 17dd07831d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)