2015-02-28 09:21:18 +03:00
|
|
|
package backoff
|
|
|
|
|
|
|
|
import (
|
2016-04-14 08:52:04 +03:00
|
|
|
"reflect"
|
2015-02-28 09:21:18 +03:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2016-03-01 18:31:41 +03:00
|
|
|
func TestForAttempt(t *testing.T) {
|
|
|
|
|
|
|
|
b := &Backoff{
|
|
|
|
Min: 100 * time.Millisecond,
|
|
|
|
Max: 10 * time.Second,
|
|
|
|
Factor: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(t, b.ForAttempt(0), 100*time.Millisecond)
|
|
|
|
equals(t, b.ForAttempt(1), 200*time.Millisecond)
|
|
|
|
equals(t, b.ForAttempt(2), 400*time.Millisecond)
|
|
|
|
b.Reset()
|
|
|
|
equals(t, b.ForAttempt(0), 100*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2015-03-03 18:44:54 +03:00
|
|
|
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
|
|
|
|
2016-08-05 00:09:58 +03:00
|
|
|
func Test4(t *testing.T) {
|
|
|
|
b := &Backoff{
|
2016-08-05 00:27:51 +03:00
|
|
|
Min: 500 * time.Second,
|
|
|
|
Max: 100 * time.Second,
|
2016-08-05 00:09:58 +03:00
|
|
|
Factor: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(t, b.Duration(), b.Max)
|
|
|
|
}
|
|
|
|
|
2016-04-14 08:52:04 +03:00
|
|
|
func TestGetAttempt(t *testing.T) {
|
|
|
|
b := &Backoff{
|
|
|
|
Min: 100 * time.Millisecond,
|
|
|
|
Max: 10 * time.Second,
|
|
|
|
Factor: 2,
|
|
|
|
}
|
|
|
|
equals(t, b.Attempt(), float64(0))
|
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
equals(t, b.Attempt(), float64(1))
|
|
|
|
equals(t, b.Duration(), 200*time.Millisecond)
|
|
|
|
equals(t, b.Attempt(), float64(2))
|
|
|
|
equals(t, b.Duration(), 400*time.Millisecond)
|
|
|
|
equals(t, b.Attempt(), float64(3))
|
|
|
|
b.Reset()
|
|
|
|
equals(t, b.Attempt(), float64(0))
|
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
equals(t, b.Attempt(), float64(1))
|
|
|
|
}
|
|
|
|
|
2015-03-06 17:42:27 +03:00
|
|
|
func TestJitter(t *testing.T) {
|
|
|
|
b := &Backoff{
|
|
|
|
Min: 100 * time.Millisecond,
|
|
|
|
Max: 10 * time.Second,
|
|
|
|
Factor: 2,
|
|
|
|
Jitter: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
between(t, b.Duration(), 100*time.Millisecond, 200*time.Millisecond)
|
|
|
|
between(t, b.Duration(), 100*time.Millisecond, 400*time.Millisecond)
|
|
|
|
b.Reset()
|
|
|
|
equals(t, b.Duration(), 100*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2018-09-03 09:52:33 +03:00
|
|
|
func TestCopy(t *testing.T) {
|
|
|
|
b := &Backoff{
|
|
|
|
Min: 100 * time.Millisecond,
|
|
|
|
Max: 10 * time.Second,
|
|
|
|
Factor: 2,
|
|
|
|
}
|
|
|
|
b2 := b.Copy()
|
|
|
|
equals(t, b, b2)
|
|
|
|
}
|
|
|
|
|
2015-03-06 17:42:27 +03:00
|
|
|
func between(t *testing.T, actual, low, high time.Duration) {
|
|
|
|
if actual < low {
|
|
|
|
t.Fatalf("Got %s, Expecting >= %s", actual, low)
|
|
|
|
}
|
|
|
|
if actual > high {
|
|
|
|
t.Fatalf("Got %s, Expecting <= %s", actual, high)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 08:52:04 +03:00
|
|
|
func equals(t *testing.T, v1, v2 interface{}) {
|
|
|
|
if !reflect.DeepEqual(v1, v2) {
|
|
|
|
t.Fatalf("Got %v, Expecting %v", v1, v2)
|
2015-02-28 09:21:18 +03:00
|
|
|
}
|
|
|
|
}
|