forked from mirror/backoff
Merge pull request #1 from josedh/feature-copy
adding copy feature and removing nested if/else
This commit is contained in:
commit
17dd07831d
13
backoff.go
13
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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue