micro/client/backoff_test.go

57 lines
931 B
Go
Raw Normal View History

2016-04-05 22:04:37 +03:00
package client
import (
2018-03-03 14:53:52 +03:00
"context"
2016-04-05 22:04:37 +03:00
"testing"
"time"
)
func TestBackoffExp(t *testing.T) {
2020-01-30 20:08:03 +03:00
results := []time.Duration{
0 * time.Second,
100 * time.Millisecond,
600 * time.Millisecond,
1900 * time.Millisecond,
4300 * time.Millisecond,
7900 * time.Millisecond,
}
2016-04-05 22:04:37 +03:00
r := &testRequest{
service: "test",
method: "test",
}
2018-04-14 20:15:09 +03:00
2016-04-05 22:04:37 +03:00
for i := 0; i < 5; i++ {
d, err := BackoffExp(context.TODO(), r, i)
2016-04-05 22:04:37 +03:00
if err != nil {
t.Fatal(err)
}
2020-01-30 19:43:03 +03:00
if d != results[i] {
t.Fatalf("Expected equal than %v, got %v", results[i], d)
2016-04-05 22:04:37 +03:00
}
}
}
func TestBackoffInterval(t *testing.T) {
min := 100 * time.Millisecond
max := 300 * time.Millisecond
r := &testRequest{
service: "test",
method: "test",
}
fn := BackoffInterval(min, max)
for i := 0; i < 5; i++ {
d, err := fn(context.TODO(), r, i)
if err != nil {
t.Fatal(err)
}
if d < min || d > max {
t.Fatalf("Expected %v < %v < %v", min, d, max)
}
}
}