tests: Fix rare test crash (#4175)

Fix rare test crash by improving the randomness logic.
This commit is contained in:
Karthic Rao
2017-05-03 12:24:22 +05:30
committed by Harshavardhana
parent e5b2e25caf
commit 9b58a669e5
2 changed files with 42 additions and 18 deletions

View File

@@ -2361,3 +2361,28 @@ func getEndpointsLocalAddr(endpoints EndpointList) string {
return globalMinioHost + ":" + globalMinioPort
}
// fetches a random number between range min-max.
func getRandomRange(min, max int, seed int64) int {
// special value -1 means no explicit seeding.
if seed != -1 {
rand.Seed(seed)
}
return rand.Intn(max-min) + min
}
// Randomizes the order of bytes in the byte array
// using Knuth Fisher-Yates shuffle algorithm.
func randomizeBytes(s []byte, seed int64) []byte {
// special value -1 means no explicit seeding.
if seed != -1 {
rand.Seed(seed)
}
n := len(s)
var j int
for i := 0; i < n-1; i++ {
j = i + rand.Intn(n-i)
s[i], s[j] = s[j], s[i]
}
return s
}