mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
Test case for retry
This commit is contained in:
parent
1edd0e660e
commit
60b116fbde
59
common/retry/retry_test.go
Normal file
59
common/retry/retry_test.go
Normal file
@ -0,0 +1,59 @@
|
||||
package retry
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
)
|
||||
|
||||
var (
|
||||
TestError = errors.New("This is a fake error.")
|
||||
)
|
||||
|
||||
func TestNoRetry(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
startTime := time.Now().Unix()
|
||||
Timed(10, 100000).On(func() error {
|
||||
return nil
|
||||
})
|
||||
endTime := time.Now().Unix()
|
||||
|
||||
assert.Int64(endTime - startTime).AtLeast(0)
|
||||
}
|
||||
|
||||
func TestRetryOnce(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
startTime := time.Now()
|
||||
called := 0
|
||||
Timed(10, 1000).On(func() error {
|
||||
if called == 0 {
|
||||
called++
|
||||
return TestError
|
||||
}
|
||||
return nil
|
||||
})
|
||||
duration := time.Since(startTime)
|
||||
|
||||
assert.Int64(int64(duration / time.Millisecond)).AtLeast(900)
|
||||
}
|
||||
|
||||
func TestRetryMultiple(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
startTime := time.Now()
|
||||
called := 0
|
||||
Timed(10, 1000).On(func() error {
|
||||
if called < 5 {
|
||||
called++
|
||||
return TestError
|
||||
}
|
||||
return nil
|
||||
})
|
||||
duration := time.Since(startTime)
|
||||
|
||||
assert.Int64(int64(duration / time.Millisecond)).AtLeast(4900)
|
||||
}
|
Loading…
Reference in New Issue
Block a user