@@ -98,19 +98,51 @@ func TestRetryWithInterval(t *testing.T) {
98
98
}
99
99
100
100
func TestRetryCtx (t * testing.T ) {
101
- assert .NotNil (t , DoWithRetryCtx (context .Background (), func (ctx context.Context , retryCount int ) error {
102
- if retryCount == 0 {
101
+ t .Run ("with timeout" , func (t * testing.T ) {
102
+ assert .NotNil (t , DoWithRetryCtx (context .Background (), func (ctx context.Context , retryCount int ) error {
103
+ if retryCount == 0 {
104
+ return errors .New ("any" )
105
+ }
106
+ time .Sleep (time .Millisecond * 150 )
107
+ return nil
108
+ }, WithTimeout (time .Millisecond * 250 ), WithInterval (time .Millisecond * 150 )))
109
+
110
+ assert .NotNil (t , DoWithRetryCtx (context .Background (), func (ctx context.Context , retryCount int ) error {
111
+ if retryCount == 1 {
112
+ return nil
113
+ }
114
+ time .Sleep (time .Millisecond * 150 )
115
+ return errors .New ("any " )
116
+ }, WithTimeout (time .Millisecond * 250 ), WithInterval (time .Millisecond * 150 )))
117
+ })
118
+
119
+ t .Run ("with deadline exceeded" , func (t * testing.T ) {
120
+ ctx , cancel := context .WithDeadline (context .Background (), time .Now ().Add (time .Millisecond * 250 ))
121
+ defer cancel ()
122
+
123
+ var times int
124
+ assert .Error (t , DoWithRetryCtx (ctx , func (ctx context.Context , retryCount int ) error {
125
+ times ++
126
+ time .Sleep (time .Millisecond * 150 )
103
127
return errors .New ("any" )
104
- }
105
- time .Sleep (time .Millisecond * 150 )
106
- return nil
107
- }, WithTimeout (time .Millisecond * 250 ), WithInterval (time .Millisecond * 150 )))
128
+ }, WithInterval (time .Millisecond * 150 )))
129
+ assert .Equal (t , 1 , times )
130
+ })
108
131
109
- assert .NotNil (t , DoWithRetryCtx (context .Background (), func (ctx context.Context , retryCount int ) error {
110
- if retryCount == 1 {
111
- return nil
112
- }
113
- time .Sleep (time .Millisecond * 150 )
114
- return errors .New ("any " )
115
- }, WithTimeout (time .Millisecond * 250 ), WithInterval (time .Millisecond * 150 )))
132
+ t .Run ("with deadline not exceeded" , func (t * testing.T ) {
133
+ ctx , cancel := context .WithDeadline (context .Background (), time .Now ().Add (time .Millisecond * 250 ))
134
+ defer cancel ()
135
+
136
+ var times int
137
+ assert .NoError (t , DoWithRetryCtx (ctx , func (ctx context.Context , retryCount int ) error {
138
+ times ++
139
+ if times == defaultRetryTimes {
140
+ return nil
141
+ }
142
+
143
+ time .Sleep (time .Millisecond * 50 )
144
+ return errors .New ("any" )
145
+ }))
146
+ assert .Equal (t , defaultRetryTimes , times )
147
+ })
116
148
}
0 commit comments