From fdb5cefae72001329007a788d94c060840a7e7bb Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Wed, 22 Jul 2015 15:01:51 -0700 Subject: [PATCH] fix unit test breakage by adding seed method to util/rand --- pkg/serviceaccount/tokens_controller_test.go | 4 ++-- pkg/util/rand/rand.go | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/serviceaccount/tokens_controller_test.go b/pkg/serviceaccount/tokens_controller_test.go index 5871ba6ec82..2e39eafcd37 100644 --- a/pkg/serviceaccount/tokens_controller_test.go +++ b/pkg/serviceaccount/tokens_controller_test.go @@ -17,13 +17,13 @@ limitations under the License. package serviceaccount import ( - "math/rand" "reflect" "testing" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" + utilrand "github.com/GoogleCloudPlatform/kubernetes/pkg/util/rand" ) type testGenerator struct { @@ -372,7 +372,7 @@ func TestTokenCreation(t *testing.T) { for k, tc := range testcases { // Re-seed to reset name generation - rand.Seed(1) + utilrand.Seed(1) generator := &testGenerator{Token: "ABC"} diff --git a/pkg/util/rand/rand.go b/pkg/util/rand/rand.go index 9d3c7eb5c06..25cd056e88a 100644 --- a/pkg/util/rand/rand.go +++ b/pkg/util/rand/rand.go @@ -26,8 +26,8 @@ import ( var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789") var numLetters = len(letters) var rng = struct { + sync.Mutex rand *rand.Rand - lock sync.Mutex }{ rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())), } @@ -39,10 +39,18 @@ func String(n int) string { panic("out-of-bounds value") } b := make([]rune, n) - rng.lock.Lock() - defer rng.lock.Unlock() + rng.Lock() + defer rng.Unlock() for i := range b { b[i] = letters[rng.rand.Intn(numLetters)] } return string(b) } + +// Seed seeds the rng with the provided seed. +func Seed(seed int64) { + rng.Lock() + defer rng.Unlock() + + rng.rand = rand.New(rand.NewSource(seed)) +}