1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/common/retry/retry_test.go

94 lines
1.8 KiB
Go
Raw Normal View History

2016-05-11 23:27:54 -07:00
package retry_test
2015-11-03 00:07:15 +01:00
import (
"testing"
"time"
2016-12-04 09:10:47 +01:00
"v2ray.com/core/common/errors"
2016-08-20 20:55:45 +02:00
. "v2ray.com/core/common/retry"
2017-10-24 16:15:35 +02:00
. "v2ray.com/ext/assert"
2015-11-03 00:07:15 +01:00
)
var (
2015-12-02 09:58:00 +01:00
errorTestOnly = errors.New("This is a fake error.")
2015-11-03 00:07:15 +01:00
)
func TestNoRetry(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2015-11-03 00:07:15 +01:00
startTime := time.Now().Unix()
2015-11-03 00:12:25 +01:00
err := Timed(10, 100000).On(func() error {
2015-11-03 00:07:15 +01:00
return nil
})
endTime := time.Now().Unix()
2017-10-24 16:15:35 +02:00
assert(err, IsNil)
assert(endTime-startTime, AtLeast, int64(0))
2015-11-03 00:07:15 +01:00
}
func TestRetryOnce(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2015-11-03 00:07:15 +01:00
startTime := time.Now()
called := 0
2015-11-03 00:12:25 +01:00
err := Timed(10, 1000).On(func() error {
2015-11-03 00:07:15 +01:00
if called == 0 {
called++
2015-12-02 09:58:00 +01:00
return errorTestOnly
2015-11-03 00:07:15 +01:00
}
return nil
})
duration := time.Since(startTime)
2017-10-24 16:15:35 +02:00
assert(err, IsNil)
assert(int64(duration/time.Millisecond), AtLeast, int64(900))
2015-11-03 00:07:15 +01:00
}
func TestRetryMultiple(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2015-11-03 00:07:15 +01:00
startTime := time.Now()
called := 0
2015-11-03 00:12:25 +01:00
err := Timed(10, 1000).On(func() error {
2015-11-03 00:07:15 +01:00
if called < 5 {
called++
2015-12-02 09:58:00 +01:00
return errorTestOnly
2015-11-03 00:07:15 +01:00
}
return nil
})
duration := time.Since(startTime)
2017-10-24 16:15:35 +02:00
assert(err, IsNil)
assert(int64(duration/time.Millisecond), AtLeast, int64(4900))
2015-11-03 00:07:15 +01:00
}
2015-11-03 00:12:25 +01:00
func TestRetryExhausted(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2015-11-03 00:12:25 +01:00
startTime := time.Now()
called := 0
err := Timed(2, 1000).On(func() error {
2016-11-20 21:47:51 +01:00
called++
return errorTestOnly
2015-11-03 00:12:25 +01:00
})
duration := time.Since(startTime)
2017-10-24 16:15:35 +02:00
assert(errors.Cause(err), Equals, ErrRetryFailed)
assert(int64(duration/time.Millisecond), AtLeast, int64(1900))
2015-11-03 00:12:25 +01:00
}
2016-11-20 21:47:51 +01:00
func TestExponentialBackoff(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2016-11-20 21:47:51 +01:00
startTime := time.Now()
called := 0
err := ExponentialBackoff(10, 100).On(func() error {
called++
return errorTestOnly
})
duration := time.Since(startTime)
2017-10-24 16:15:35 +02:00
assert(errors.Cause(err), Equals, ErrRetryFailed)
assert(int64(duration/time.Millisecond), AtLeast, int64(4000))
2016-11-20 21:47:51 +01:00
}