Skip to content

Commit

Permalink
move the jitter up function
Browse files Browse the repository at this point in the history
Signed-off-by: Yash Sharma <[email protected]>
  • Loading branch information
yashrsharma44 committed Jul 13, 2021
1 parent c2e8b45 commit 724f3d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
36 changes: 1 addition & 35 deletions interceptors/retry/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ package retry_test
import (
"context"
"io"
"testing"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"

"github.com/stretchr/testify/assert"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
)
Expand Down Expand Up @@ -92,38 +89,7 @@ func ExampleWithPerRetryTimeout() {
retry.WithPerRetryTimeout(1*time.Second))
}

// scale duration by a factor.
// Scale duration by a factor.
func scaleDuration(d time.Duration, factor float64) time.Duration {
return time.Duration(float64(d) * factor)
}

func TestJitterUp(t *testing.T) {
// arguments to jitterup.
duration := 10 * time.Second
variance := 0.10

// bound to check.
max := 11000 * time.Millisecond
min := 9000 * time.Millisecond
high := scaleDuration(max, 0.98)
low := scaleDuration(min, 1.02)

highCount := 0
lowCount := 0

for i := 0; i < 1000; i++ {
out := retry.JitterUp(duration, variance)
assert.True(t, out <= max, "value %s must be <= %s", out, max)
assert.True(t, out >= min, "value %s must be >= %s", out, min)

if out > high {
highCount++
}
if out < low {
lowCount++
}
}

assert.True(t, highCount != 0, "at least one sample should reach to >%s", high)
assert.True(t, lowCount != 0, "at least one sample should to <%s", low)
}
31 changes: 31 additions & 0 deletions interceptors/retry/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,34 @@ func (s *ChainedRetrySuite) TestStreamWithChainedInterceptors_WithRetry() {
require.EqualValues(s.T(), 1, s.preRetryInterceptor.called, "pre-retry interceptor should be called once")
require.EqualValues(s.T(), 2, s.postRetryInterceptor.called, "post-retry interceptor should be called twice")
}

func TestJitterUp(t *testing.T) {
// Arguments to jitterup.
duration := 10 * time.Second
variance := 0.10

// Bound to check.
max := 11000 * time.Millisecond
min := 9000 * time.Millisecond
high := scaleDuration(max, 0.98)
low := scaleDuration(min, 1.02)

highCount := 0
lowCount := 0

for i := 0; i < 1000; i++ {
out := retry.JitterUp(duration, variance)
assert.True(t, out <= max, "value %s must be <= %s", out, max)
assert.True(t, out >= min, "value %s must be >= %s", out, min)

if out > high {
highCount++
}
if out < low {
lowCount++
}
}

assert.True(t, highCount != 0, "at least one sample should reach to >%s", high)
assert.True(t, lowCount != 0, "at least one sample should to <%s", low)
}

0 comments on commit 724f3d7

Please sign in to comment.