From a90198c718cf1e4c69867c118b00a0d9e5659f58 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Fri, 26 Apr 2019 14:47:55 -0700 Subject: [PATCH] internal: seed random for wrr tests (#2791) --- balancer/internal/wrr/random.go | 4 +++- balancer/internal/wrr/wrr_test.go | 6 ++++++ vet.sh | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/balancer/internal/wrr/random.go b/balancer/internal/wrr/random.go index fe345f78..ec724f2e 100644 --- a/balancer/internal/wrr/random.go +++ b/balancer/internal/wrr/random.go @@ -36,12 +36,14 @@ func NewRandom() WRR { return &randomWRR{} } +var grpcrandInt63n = grpcrand.Int63n + func (rw *randomWRR) Next() (item interface{}) { if rw.sumOfWeights == 0 { return nil } // Random number in [0, sum). - randomWeight := grpcrand.Int63n(rw.sumOfWeights) + randomWeight := grpcrandInt63n(rw.sumOfWeights) for _, item := range rw.items { randomWeight = randomWeight - item.Weight if randomWeight < 0 { diff --git a/balancer/internal/wrr/wrr_test.go b/balancer/internal/wrr/wrr_test.go index 03c2b900..2e32b9b6 100644 --- a/balancer/internal/wrr/wrr_test.go +++ b/balancer/internal/wrr/wrr_test.go @@ -20,6 +20,7 @@ package wrr import ( "errors" "math" + "math/rand" "testing" "github.com/google/go-cmp/cmp" @@ -97,3 +98,8 @@ func testWRRNext(t *testing.T, newWRR func() WRR) { func TestRandomWRRNext(t *testing.T) { testWRRNext(t, NewRandom) } + +func init() { + r := rand.New(rand.NewSource(0)) + grpcrandInt63n = r.Int63n +} diff --git a/vet.sh b/vet.sh index 7e6ce5eb..11037b94 100755 --- a/vet.sh +++ b/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 # 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. git ls-files "*.go" | (! xargs grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/")