2016-05-12 06:27:54 +00:00
|
|
|
package retry_test
|
2015-11-02 23:07:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-05-12 06:27:54 +00:00
|
|
|
. "github.com/v2ray/v2ray-core/common/retry"
|
2015-12-02 14:27:18 +00:00
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2015-11-02 23:07:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-12-02 08:58:00 +00:00
|
|
|
errorTestOnly = errors.New("This is a fake error.")
|
2015-11-02 23:07:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoRetry(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2015-11-02 23:07:15 +00:00
|
|
|
|
|
|
|
startTime := time.Now().Unix()
|
2015-11-02 23:12:25 +00:00
|
|
|
err := Timed(10, 100000).On(func() error {
|
2015-11-02 23:07:15 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
endTime := time.Now().Unix()
|
|
|
|
|
2015-11-02 23:12:25 +00:00
|
|
|
assert.Error(err).IsNil()
|
2015-11-02 23:07:15 +00:00
|
|
|
assert.Int64(endTime - startTime).AtLeast(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetryOnce(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2015-11-02 23:07:15 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
called := 0
|
2015-11-02 23:12:25 +00:00
|
|
|
err := Timed(10, 1000).On(func() error {
|
2015-11-02 23:07:15 +00:00
|
|
|
if called == 0 {
|
|
|
|
called++
|
2015-12-02 08:58:00 +00:00
|
|
|
return errorTestOnly
|
2015-11-02 23:07:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
|
2015-11-02 23:12:25 +00:00
|
|
|
assert.Error(err).IsNil()
|
2015-11-02 23:07:15 +00:00
|
|
|
assert.Int64(int64(duration / time.Millisecond)).AtLeast(900)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetryMultiple(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2015-11-02 23:07:15 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
called := 0
|
2015-11-02 23:12:25 +00:00
|
|
|
err := Timed(10, 1000).On(func() error {
|
2015-11-02 23:07:15 +00:00
|
|
|
if called < 5 {
|
|
|
|
called++
|
2015-12-02 08:58:00 +00:00
|
|
|
return errorTestOnly
|
2015-11-02 23:07:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
|
2015-11-02 23:12:25 +00:00
|
|
|
assert.Error(err).IsNil()
|
2015-11-02 23:07:15 +00:00
|
|
|
assert.Int64(int64(duration / time.Millisecond)).AtLeast(4900)
|
|
|
|
}
|
2015-11-02 23:12:25 +00:00
|
|
|
|
|
|
|
func TestRetryExhausted(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2015-11-02 23:12:25 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
called := 0
|
|
|
|
err := Timed(2, 1000).On(func() error {
|
|
|
|
if called < 5 {
|
|
|
|
called++
|
2015-12-02 08:58:00 +00:00
|
|
|
return errorTestOnly
|
2015-11-02 23:12:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
|
2016-05-12 06:27:54 +00:00
|
|
|
assert.Error(err).Equals(ErrorRetryFailed)
|
2015-11-02 23:12:25 +00:00
|
|
|
assert.Int64(int64(duration / time.Millisecond)).AtLeast(1900)
|
|
|
|
}
|