-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* grpc_retry backoff overflow * Add bounds to exponentBase2 and use it instead of math.Exp2 * Add a few tests * Add copyright to backoff_test
- Loading branch information
1 parent
ed865db
commit d75a1b8
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBackoffExponentialWithJitter(t *testing.T) { | ||
scalar := 100 * time.Millisecond | ||
jitterFrac := 0.10 | ||
backoffFunc := BackoffExponentialWithJitter(scalar, jitterFrac) | ||
// use 64 so we are past number of attempts where exponentBase2 would overflow | ||
for i := 0; i < 64; i++ { | ||
waitFor := backoffFunc(nil, uint(i)) | ||
if waitFor < 0 { | ||
t.Errorf("BackoffExponentialWithJitter(%d) = %d; want >= 0", i, waitFor) | ||
} | ||
} | ||
} | ||
|
||
func TestBackoffExponentialWithJitterBounded(t *testing.T) { | ||
scalar := 100 * time.Millisecond | ||
jitterFrac := 0.10 | ||
maxBound := 10 * time.Second | ||
backoff := BackoffExponentialWithJitterBounded(scalar, jitterFrac, maxBound) | ||
// use 64 so we are past number of attempts where exponentBase2 would overflow | ||
for i := 0; i < 64; i++ { | ||
waitFor := backoff(context.Background(), uint(i)) | ||
if waitFor > maxBound { | ||
t.Fatalf("expected dur to be less than %v, got %v for %d", maxBound, waitFor, i) | ||
} | ||
if waitFor < 0 { | ||
t.Fatalf("expected dur to be greater than 0, got %v for %d", waitFor, i) | ||
} | ||
} | ||
} |