test: add more tests (#1166)

* chore: reverse the order of stopping services

* chore: reverse the order of stopping services

* test: add more tests
master
Kevin Wan 3 years ago committed by GitHub
parent eda8230521
commit bd26783b33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ import (
"time" "time"
) )
// Func defines the method to calculate how long to retry.
type Func func(attempt int) time.Duration type Func func(attempt int) time.Duration
// LinearWithJitter waits a set period of time, allowing for jitter (fractional adjustment). // LinearWithJitter waits a set period of time, allowing for jitter (fractional adjustment).

@ -16,3 +16,15 @@ func TestExponential(t *testing.T) {
fn := Exponential(time.Second) fn := Exponential(time.Second)
assert.EqualValues(t, time.Second, fn(1)) assert.EqualValues(t, time.Second, fn(1))
} }
func TestLinearWithJitter(t *testing.T) {
const rounds = 1000000
var total time.Duration
fn := LinearWithJitter(time.Second, 0.5)
for i := 0; i < rounds; i++ {
total += fn(1)
}
// 0.1% tolerance
assert.True(t, total/time.Duration(rounds)-time.Second < time.Millisecond)
}

@ -50,6 +50,8 @@ func TestRetryWithPerRetryTimeout(t *testing.T) {
} }
func Test_waitRetryBackoff(t *testing.T) { func Test_waitRetryBackoff(t *testing.T) {
logx.Disable()
opt := &options{perCallTimeout: time.Second, backoffFunc: func(attempt int) time.Duration { opt := &options{perCallTimeout: time.Second, backoffFunc: func(attempt int) time.Duration {
return time.Second return time.Second
}} }}

@ -52,10 +52,11 @@ func waitRetryBackoff(logger logx.Logger, attempt int, ctx context.Context, retr
} }
if waitTime > 0 { if waitTime > 0 {
timer := time.NewTimer(waitTime) timer := time.NewTimer(waitTime)
defer timer.Stop()
logger.Infof("grpc retry attempt: %d, backoff for %v", attempt, waitTime) logger.Infof("grpc retry attempt: %d, backoff for %v", attempt, waitTime)
select { select {
case <-ctx.Done(): case <-ctx.Done():
timer.Stop()
return status.FromContextError(ctx.Err()).Err() return status.FromContextError(ctx.Err()).Err()
case <-timer.C: case <-timer.C:
// double check // double check

Loading…
Cancel
Save