-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretry_test.go
53 lines (44 loc) · 1.28 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
package retry
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func Test_Retry(t *testing.T) {
t.Run("retry passes through a nil error", func(t *testing.T) {
err := Retry(context.Background(), 50*time.Millisecond, 3, func(ctx context.Context) error {
return nil
})
assert.NoError(t, err)
})
t.Run("retry retries the number of attempts", func(t *testing.T) {
attempts := 0
err := Retry(context.Background(), 50*time.Millisecond, 3, func(ctx context.Context) error {
attempts += 1
return errors.New("general failure")
})
assert.Error(t, err)
assert.Equal(t, 3, attempts)
})
}
func Test_RetryWithValue(t *testing.T) {
t.Run("retry passes through a nil error", func(t *testing.T) {
val, err := RetryWithValue(context.Background(), 50*time.Millisecond, 3, func(ctx context.Context) (string, error) {
return "data", nil
})
assert.Equal(t, "data", val)
assert.NoError(t, err)
})
t.Run("retry retries the number of attempts", func(t *testing.T) {
attempts := 0
val, err := RetryWithValue(context.Background(), 50*time.Millisecond, 3, func(ctx context.Context) (*string, error) {
attempts += 1
return nil, errors.New("general failure")
})
assert.Nil(t, val)
assert.Error(t, err)
assert.Equal(t, 3, attempts)
})
}