Skip to content

🧹 chore: Improve Retry addon tests coverage #3526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions addon/retry/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package retry

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestConfigDefault_NoConfig(t *testing.T) {
t.Parallel()
cfg := configDefault()
require.Equal(t, DefaultConfig, cfg)
}

func TestConfigDefault_Custom(t *testing.T) {
t.Parallel()
custom := Config{
InitialInterval: 2 * time.Second,
MaxBackoffTime: 64 * time.Second,
Multiplier: 3.0,
MaxRetryCount: 5,
currentInterval: 2 * time.Second,
}
cfg := configDefault(custom)
require.Equal(t, custom, cfg)
}

func TestConfigDefault_PartialAndNegative(t *testing.T) {
t.Parallel()
cfg := configDefault(Config{Multiplier: -1, MaxRetryCount: 0})
require.Equal(t, DefaultConfig, cfg)
}
24 changes: 24 additions & 0 deletions addon/retry/exponential_backoff_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package retry

import (
"crypto/rand"
"errors"
"testing"
"time"
Expand Down Expand Up @@ -119,3 +120,26 @@ func Test_ExponentialBackoff_Next(t *testing.T) {
})
}
}

func Test_ExponentialBackoff_NextRandFailure(t *testing.T) {
// Backup original reader and restore at the end
original := rand.Reader
defer func() { rand.Reader = original }()
rand.Reader = failingReader{}

expBackoff := &ExponentialBackoff{
InitialInterval: 1 * time.Second,
MaxBackoffTime: 10 * time.Second,
Multiplier: 2,
MaxRetryCount: 3,
currentInterval: 1 * time.Second,
}
next := expBackoff.next()
require.Equal(t, expBackoff.MaxBackoffTime, next)
// currentInterval should not change when random fails
require.Equal(t, 1*time.Second, expBackoff.currentInterval)
}

type failingReader struct{}

func (failingReader) Read(_ []byte) (int, error) { return 0, errors.New("fail") }
Loading