Merge pull request #15 from josedh/master

Clone method and splitting if-else statement
This commit is contained in:
Jaime Pillora 2018-09-09 16:27:03 +10:00 committed by GitHub
commit 3050d21c67
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)