From aa2b5ddaad12cf60315fdd194c349f64b39f014a Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 16 Apr 2022 16:36:15 +0300 Subject: [PATCH] client: add backoff tests Signed-off-by: Vasiliy Tolstov --- client/backoff.go | 2 +- client/backoff_test.go | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/client/backoff.go b/client/backoff.go index d548b724..7eaacf7e 100644 --- a/client/backoff.go +++ b/client/backoff.go @@ -19,7 +19,7 @@ func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, erro // BackoffInterval specifies randomization interval for backoff func func BackoffInterval(min time.Duration, max time.Duration) BackoffFunc { return func(_ context.Context, _ Request, attempts int) (time.Duration, error) { - td := time.Duration(time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100) + td := time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100 if td < min { return min, nil } else if td > max { diff --git a/client/backoff_test.go b/client/backoff_test.go index 2ab854f7..59bb7a41 100644 --- a/client/backoff_test.go +++ b/client/backoff_test.go @@ -6,7 +6,7 @@ import ( "time" ) -func TestBackoff(t *testing.T) { +func TestBackoffExp(t *testing.T) { results := []time.Duration{ 0 * time.Second, 100 * time.Millisecond, @@ -32,3 +32,25 @@ func TestBackoff(t *testing.T) { } } } + +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) + } + } +}