Skip to content

Commit d011772

Browse files
committed
test(retry): achieve full coverage
1 parent a35c5d2 commit d011772

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

addon/retry/config_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package retry
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestConfigDefault_NoConfig(t *testing.T) {
11+
t.Parallel()
12+
cfg := configDefault()
13+
require.Equal(t, DefaultConfig, cfg)
14+
}
15+
16+
func TestConfigDefault_Custom(t *testing.T) {
17+
t.Parallel()
18+
custom := Config{
19+
InitialInterval: 2 * time.Second,
20+
MaxBackoffTime: 64 * time.Second,
21+
Multiplier: 3.0,
22+
MaxRetryCount: 5,
23+
currentInterval: 2 * time.Second,
24+
}
25+
cfg := configDefault(custom)
26+
require.Equal(t, custom, cfg)
27+
}
28+
29+
func TestConfigDefault_PartialAndNegative(t *testing.T) {
30+
t.Parallel()
31+
cfg := configDefault(Config{Multiplier: -1, MaxRetryCount: 0})
32+
require.Equal(t, DefaultConfig, cfg)
33+
}

addon/retry/exponential_backoff_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package retry
22

33
import (
4+
"crypto/rand"
45
"errors"
56
"testing"
67
"time"
@@ -119,3 +120,27 @@ func Test_ExponentialBackoff_Next(t *testing.T) {
119120
})
120121
}
121122
}
123+
124+
func Test_ExponentialBackoff_NextRandFailure(t *testing.T) {
125+
t.Parallel()
126+
// Backup original reader and restore at the end
127+
original := rand.Reader
128+
defer func() { rand.Reader = original }()
129+
rand.Reader = failingReader{}
130+
131+
expBackoff := &ExponentialBackoff{
132+
InitialInterval: 1 * time.Second,
133+
MaxBackoffTime: 10 * time.Second,
134+
Multiplier: 2,
135+
MaxRetryCount: 3,
136+
currentInterval: 1 * time.Second,
137+
}
138+
next := expBackoff.next()
139+
require.Equal(t, expBackoff.MaxBackoffTime, next)
140+
// currentInterval should not change when random fails
141+
require.Equal(t, 1*time.Second, expBackoff.currentInterval)
142+
}
143+
144+
type failingReader struct{}
145+
146+
func (failingReader) Read(p []byte) (int, error) { return 0, errors.New("fail") }

0 commit comments

Comments
 (0)