internal: seed random for wrr tests (#2791)
This commit is contained in:
@ -36,12 +36,14 @@ func NewRandom() WRR {
|
|||||||
return &randomWRR{}
|
return &randomWRR{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var grpcrandInt63n = grpcrand.Int63n
|
||||||
|
|
||||||
func (rw *randomWRR) Next() (item interface{}) {
|
func (rw *randomWRR) Next() (item interface{}) {
|
||||||
if rw.sumOfWeights == 0 {
|
if rw.sumOfWeights == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Random number in [0, sum).
|
// Random number in [0, sum).
|
||||||
randomWeight := grpcrand.Int63n(rw.sumOfWeights)
|
randomWeight := grpcrandInt63n(rw.sumOfWeights)
|
||||||
for _, item := range rw.items {
|
for _, item := range rw.items {
|
||||||
randomWeight = randomWeight - item.Weight
|
randomWeight = randomWeight - item.Weight
|
||||||
if randomWeight < 0 {
|
if randomWeight < 0 {
|
||||||
|
@ -20,6 +20,7 @@ package wrr
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
@ -97,3 +98,8 @@ func testWRRNext(t *testing.T, newWRR func() WRR) {
|
|||||||
func TestRandomWRRNext(t *testing.T) {
|
func TestRandomWRRNext(t *testing.T) {
|
||||||
testWRRNext(t, NewRandom)
|
testWRRNext(t, NewRandom)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
r := rand.New(rand.NewSource(0))
|
||||||
|
grpcrandInt63n = r.Int63n
|
||||||
|
}
|
||||||
|
2
vet.sh
2
vet.sh
@ -75,7 +75,7 @@ git ls-files "*.go" | xargs grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO
|
|||||||
|
|
||||||
# - Do not import math/rand for real library code. Use internal/grpcrand for
|
# - Do not import math/rand for real library code. Use internal/grpcrand for
|
||||||
# thread safety.
|
# thread safety.
|
||||||
git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand')
|
git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand\|wrr_test')
|
||||||
|
|
||||||
# - Ensure all ptypes proto packages are renamed when importing.
|
# - Ensure all ptypes proto packages are renamed when importing.
|
||||||
git ls-files "*.go" | (! xargs grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/")
|
git ls-files "*.go" | (! xargs grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/")
|
||||||
|
Reference in New Issue
Block a user