forked from k0sproject/k0sctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_test.go
120 lines (104 loc) · 2.69 KB
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package retry
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
oldInterval := Interval
Interval = 1 * time.Millisecond
defer func() { Interval = oldInterval }()
m.Run()
}
func TestContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Run("succeeds on first try", func(t *testing.T) {
err := Context(ctx, func(_ context.Context) error {
return nil
})
require.NoError(t, err)
})
t.Run("fails when context is canceled between tries", func(t *testing.T) {
var counter int
err := Context(ctx, func(_ context.Context) error {
counter++
if counter == 2 {
cancel()
}
return errors.New("some error")
})
assert.Error(t, err, "foo")
})
t.Run("fails with a canceled context", func(t *testing.T) {
err := Context(ctx, func(_ context.Context) error {
return errors.New("some error")
})
assert.Error(t, err, "some error")
})
}
func TestTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Run("succeeds before timeout", func(t *testing.T) {
err := Timeout(ctx, 10*time.Second, func(_ context.Context) error {
return nil
})
require.NoError(t, err)
})
t.Run("fails on timeout", func(t *testing.T) {
err := Timeout(ctx, 1*time.Millisecond, func(_ context.Context) error {
time.Sleep(2 * time.Millisecond)
return errors.New("some error")
})
assert.Error(t, err, "foo")
})
t.Run("stops retrying on ErrAbort", func(t *testing.T) {
var counter int
err := Timeout(ctx, 10*time.Second, func(_ context.Context) error {
counter++
if counter == 2 {
return errors.Join(ErrAbort, errors.New("some error"))
}
return errors.New("some error")
})
assert.Error(t, err, "foo")
})
}
func TestTimes(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Run("succeeds within limit", func(t *testing.T) {
counter := 0
err := Times(ctx, 3, func(_ context.Context) error {
counter++
if counter == 2 {
return nil
}
return errors.New("some error")
})
require.NoError(t, err)
assert.Equal(t, 2, counter)
})
t.Run("fails on reaching limit", func(t *testing.T) {
var tries int
err := Times(ctx, 2, func(_ context.Context) error {
tries++
return errors.New("some error")
})
assert.Error(t, err, "foo")
assert.Equal(t, 2, tries)
})
t.Run("stops retrying on ErrAbort", func(t *testing.T) {
var tries int
err := Times(ctx, 2, func(_ context.Context) error {
tries++
return errors.Join(ErrAbort, errors.New("some error"))
})
assert.Error(t, err, "foo")
assert.Equal(t, 1, tries)
})
}