2015-02-28 09:21:18 +03:00
|
|
|
package backoff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test1(t *testing.T) {
|
|
|
|
|
|
|
|
b := &Backoff{
|
|
|
|
Min: 100 * time.Millisecond,
|
|
|
|
Max: 10 * time.Second,
|
|
|
|
Factor: 2,
|
|
|
|
}
|
|
|
|
|
2015-03-03 18:44:54 +03:00
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
equals(t, b.Duration(), 200*time.Millisecond)
|
|
|
|
equals(t, b.Duration(), 400*time.Millisecond)
|
|
|
|
b.Reset()
|
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test2(t *testing.T) {
|
2015-02-28 09:21:18 +03:00
|
|
|
|
2015-03-03 18:44:54 +03:00
|
|
|
b := &Backoff{
|
|
|
|
Min: 100 * time.Millisecond,
|
|
|
|
Max: 10 * time.Second,
|
|
|
|
Factor: 1.5,
|
2015-02-28 09:21:18 +03:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:44:54 +03:00
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
equals(t, b.Duration(), 150*time.Millisecond)
|
|
|
|
equals(t, b.Duration(), 225*time.Millisecond)
|
|
|
|
b.Reset()
|
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test3(t *testing.T) {
|
|
|
|
|
|
|
|
b := &Backoff{
|
|
|
|
Min: 100 * time.Nanosecond,
|
|
|
|
Max: 10 * time.Second,
|
|
|
|
Factor: 1.75,
|
2015-02-28 09:21:18 +03:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:44:54 +03:00
|
|
|
equals(t, b.Duration(), 100*time.Nanosecond)
|
|
|
|
equals(t, b.Duration(), 175*time.Nanosecond)
|
|
|
|
equals(t, b.Duration(), 306*time.Nanosecond)
|
2015-02-28 09:21:18 +03:00
|
|
|
b.Reset()
|
2015-03-03 18:44:54 +03:00
|
|
|
equals(t, b.Duration(), 100*time.Nanosecond)
|
|
|
|
}
|
2015-02-28 09:21:18 +03:00
|
|
|
|
2015-03-03 18:44:54 +03:00
|
|
|
func equals(t *testing.T, d1, d2 time.Duration) {
|
|
|
|
if d1 != d2 {
|
|
|
|
t.Fatalf("Got %s, Expecting %s", d1, d2)
|
2015-02-28 09:21:18 +03:00
|
|
|
}
|
|
|
|
}
|