From 4140d2edb32126a92e5114cc6648b75cf4e1c5d1 Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 10 Jul 2024 11:55:26 +0200 Subject: [PATCH] test: add test to exponential backoff with jitter --- hcloud/client_test.go | 60 +++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/hcloud/client_test.go b/hcloud/client_test.go index 48be628c..5229290c 100644 --- a/hcloud/client_test.go +++ b/hcloud/client_test.go @@ -12,6 +12,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" @@ -382,21 +383,52 @@ func TestBuildUserAgent(t *testing.T) { } func TestExponentialBackoff(t *testing.T) { - backoffFunc := ExponentialBackoffWithOpts(ExponentialBackoffOpts{ - Base: time.Second, - Multiplier: 2, - Cap: 32 * time.Second, + t.Run("without jitter", func(t *testing.T) { + backoffFunc := ExponentialBackoffWithOpts(ExponentialBackoffOpts{ + Base: time.Second, + Multiplier: 2, + Cap: 32 * time.Second, + }) + + sum := 0.0 + for i, expected := range []time.Duration{ + 1 * time.Second, + 2 * time.Second, + 4 * time.Second, + 8 * time.Second, + 16 * time.Second, + 32 * time.Second, + 32 * time.Second, + 32 * time.Second, + } { + backoff := backoffFunc(i) + require.Equal(t, backoff, expected) + sum += backoff.Seconds() + } + require.Equal(t, 127.0, sum) }) - count := 8 - sum := 0.0 - result := make([]string, 0, count) - for i := 0; i < count; i++ { - backoff := backoffFunc(i) - sum += backoff.Seconds() - result = append(result, backoff.String()) - } + t.Run("with jitter", func(t *testing.T) { + backoffFunc := ExponentialBackoffWithOpts(ExponentialBackoffOpts{ + Base: time.Second, + Multiplier: 2, + Cap: 32 * time.Second, + Jitter: true, + }) - require.Equal(t, []string{"1s", "2s", "4s", "8s", "16s", "32s", "32s", "32s"}, result) - require.Equal(t, 127.0, sum) + for i, expected := range []time.Duration{ + 1 * time.Second, + 2 * time.Second, + 4 * time.Second, + 8 * time.Second, + 16 * time.Second, + 32 * time.Second, + 32 * time.Second, + 32 * time.Second, + } { + backoff := backoffFunc(i) + assert.GreaterOrEqual(t, backoff, time.Second) + assert.LessOrEqual(t, backoff, expected) + } + }) }