1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/retry/retry_test.go

97 lines
1.9 KiB
Go
Raw Normal View History

2016-05-12 02:27:54 -04:00
package retry_test
2015-11-02 18:07:15 -05:00
import (
"testing"
"time"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/errors"
. "github.com/v2fly/v2ray-core/v5/common/retry"
2015-11-02 18:07:15 -05:00
)
2021-05-19 17:28:52 -04:00
var errorTestOnly = errors.New("this is a fake error")
2015-11-02 18:07:15 -05:00
func TestNoRetry(t *testing.T) {
startTime := time.Now().Unix()
2015-11-02 18:12:25 -05:00
err := Timed(10, 100000).On(func() error {
2015-11-02 18:07:15 -05:00
return nil
})
endTime := time.Now().Unix()
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-09 09:46:48 -05:00
if endTime < startTime {
t.Error("endTime < startTime: ", startTime, " -> ", endTime)
}
2015-11-02 18:07:15 -05:00
}
func TestRetryOnce(t *testing.T) {
startTime := time.Now()
called := 0
2015-11-02 18:12:25 -05:00
err := Timed(10, 1000).On(func() error {
2015-11-02 18:07:15 -05:00
if called == 0 {
called++
2015-12-02 03:58:00 -05:00
return errorTestOnly
2015-11-02 18:07:15 -05:00
}
return nil
})
duration := time.Since(startTime)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-09 09:46:48 -05:00
if v := int64(duration / time.Millisecond); v < 900 {
t.Error("duration: ", v)
}
2015-11-02 18:07:15 -05:00
}
func TestRetryMultiple(t *testing.T) {
startTime := time.Now()
called := 0
2015-11-02 18:12:25 -05:00
err := Timed(10, 1000).On(func() error {
2015-11-02 18:07:15 -05:00
if called < 5 {
called++
2015-12-02 03:58:00 -05:00
return errorTestOnly
2015-11-02 18:07:15 -05:00
}
return nil
})
duration := time.Since(startTime)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-09 09:46:48 -05:00
if v := int64(duration / time.Millisecond); v < 4900 {
t.Error("duration: ", v)
}
2015-11-02 18:07:15 -05:00
}
2015-11-02 18:12:25 -05:00
func TestRetryExhausted(t *testing.T) {
startTime := time.Now()
called := 0
err := Timed(2, 1000).On(func() error {
2016-11-20 15:47:51 -05:00
called++
return errorTestOnly
2015-11-02 18:12:25 -05:00
})
duration := time.Since(startTime)
2019-02-09 09:46:48 -05:00
if errors.Cause(err) != ErrRetryFailed {
t.Error("cause: ", err)
}
if v := int64(duration / time.Millisecond); v < 1900 {
t.Error("duration: ", v)
}
2015-11-02 18:12:25 -05:00
}
2016-11-20 15:47:51 -05:00
func TestExponentialBackoff(t *testing.T) {
startTime := time.Now()
called := 0
err := ExponentialBackoff(10, 100).On(func() error {
called++
return errorTestOnly
})
duration := time.Since(startTime)
2019-02-09 09:46:48 -05:00
if errors.Cause(err) != ErrRetryFailed {
t.Error("cause: ", err)
}
if v := int64(duration / time.Millisecond); v < 4000 {
t.Error("duration: ", v)
}
2016-11-20 15:47:51 -05:00
}