internal: seed random for wrr tests (#2791)

This commit is contained in:
Menghan Li
2019-04-26 14:47:55 -07:00
committed by GitHub
parent b78962e503
commit a90198c718
3 changed files with 10 additions and 2 deletions

View File

@ -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 {

View File

@ -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
}

2
vet.sh
View File

@ -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/")