-
Notifications
You must be signed in to change notification settings - Fork 7
/
common_test.go
119 lines (114 loc) · 2.33 KB
/
common_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
package gsclient
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_isValidUUID(t *testing.T) {
validationUUIDTestCases := make([]uuidTestCase, len(uuidCommonTestCases))
copy(validationUUIDTestCases, uuidCommonTestCases)
validationUUIDTestCases = append(validationUUIDTestCases,
uuidTestCase{
isFailed: true,
testUUID: "abc-123",
},
uuidTestCase{
isFailed: false,
testUUID: "690de890-13c0-4e76-8a01-e10ba8786e53",
},
)
for _, test := range validationUUIDTestCases {
isValid := isValidUUID(test.testUUID)
if test.isFailed {
assert.False(t, isValid)
} else {
assert.True(t, isValid)
}
}
}
func Test_retryWithContext(t *testing.T) {
type testCase struct {
isContinue bool
err error
timeout, delay time.Duration
}
testCases := []testCase{
{
true,
nil,
time.Duration(1) * time.Second,
time.Duration(100) * time.Millisecond,
},
{
false,
nil,
time.Duration(1) * time.Second,
time.Duration(100) * time.Millisecond,
},
{
false,
errors.New("just test"),
time.Duration(1) * time.Second,
time.Duration(100) * time.Millisecond,
},
{
true,
errors.New("just test"),
time.Duration(1) * time.Second,
time.Duration(100) * time.Millisecond,
},
}
for _, test := range testCases {
ctx, cancel := context.WithTimeout(context.Background(), test.timeout)
err := retryWithContext(ctx, func() (bool, error) {
return test.isContinue, test.err
}, test.delay)
if test.err != nil || test.isContinue {
assert.NotNil(t, err, fmt.Sprintf("%v %v", err, test.err))
} else {
assert.Nil(t, err, err)
}
cancel()
}
}
func Test_retryNTimes(t *testing.T) {
type testCase struct {
isContinue bool
err error
delay time.Duration
numOfRetries int
}
testCases := []testCase{
{
true,
nil,
time.Duration(500) * time.Millisecond,
10,
},
{
false,
nil,
time.Duration(500) * time.Millisecond,
10,
},
{
false,
errors.New("just test"),
time.Duration(500) * time.Millisecond,
10,
},
}
for _, test := range testCases {
err := retryNTimes(func() (bool, error) {
return test.isContinue, test.err
}, test.numOfRetries, test.delay)
if test.err != nil || test.isContinue {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
}
}