2016-04-05 20:04:37 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2016-04-05 20:04:37 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-04-16 16:36:15 +03:00
|
|
|
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 20:04:37 +01:00
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
r := &testRequest{
|
|
|
|
service: "test",
|
|
|
|
method: "test",
|
|
|
|
}
|
2018-04-14 18:15:09 +01:00
|
|
|
|
2016-04-05 20:04:37 +01:00
|
|
|
for i := 0; i < 5; i++ {
|
2022-03-27 00:16:22 +03:00
|
|
|
d, err := BackoffExp(context.TODO(), r, i)
|
2016-04-05 20:04:37 +01: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 20:04:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 16:36:15 +03:00
|
|
|
|
|
|
|
func TestBackoffInterval(t *testing.T) {
|
2024-12-09 16:23:25 +03:00
|
|
|
minTime := 100 * time.Millisecond
|
|
|
|
maxTime := 300 * time.Millisecond
|
2022-04-16 16:36:15 +03:00
|
|
|
|
|
|
|
r := &testRequest{
|
|
|
|
service: "test",
|
|
|
|
method: "test",
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
fn := BackoffInterval(minTime, maxTime)
|
2022-04-16 16:36:15 +03:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
d, err := fn(context.TODO(), r, i)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
if d < minTime || d > maxTime {
|
|
|
|
t.Fatalf("Expected %v < %v < %v", minTime, d, maxTime)
|
2022-04-16 16:36:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|