tests: Fix rare test crash (#4175)

Fix rare test crash by improving the randomness logic.
master
Karthic Rao 8 years ago committed by Harshavardhana
parent e5b2e25caf
commit 9b58a669e5
  1. 35
      cmd/server_test.go
  2. 25
      cmd/test-utils_test.go

@ -1833,25 +1833,23 @@ func (s *TestSuiteCommon) TestGetPartialObjectMisAligned(c *C) {
c.Assert(response.StatusCode, Equals, http.StatusOK)
var buffer bytes.Buffer
line := `1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,
1234567890,1234567890,1234567890,123`
rand.Seed(UTCNow().UnixNano())
// Create a misalgined data.
for i := 0; i < 13*rand.Intn(1<<16); i++ {
buffer.WriteString(fmt.Sprintf("[%05d] %s\n", i, line[:rand.Intn(1<<8)]))
// data to be written into buffer.
data := "1234567890"
// seed the random number generator once.
rand.Seed(3)
// generate a random number between 13 and 200.
randInt := getRandomRange(13, 200, -1)
// write into buffer till length of the buffer is greater than the generated random number.
for i := 0; i <= randInt; i += 10 {
buffer.WriteString(data)
}
putContent := buffer.String()
buf := bytes.NewReader([]byte(putContent))
// String content which is used for put object range test.
putBytes := buffer.Bytes()
putBytes = putBytes[:randInt]
// randomize the order of bytes in the byte array and create a reader.
putBytes = randomizeBytes(putBytes, -1)
buf := bytes.NewReader(putBytes)
putContent := string(putBytes)
objectName := "test-big-file"
// HTTP request to upload the object.
request, err = newTestSignedRequest("PUT", getPutObjectURL(s.endPoint, bucketName, objectName),
@ -1882,6 +1880,7 @@ func (s *TestSuiteCommon) TestGetPartialObjectMisAligned(c *C) {
// request for last 7 bytes of the object.
{"-7", putContent[len(putContent)-7:]},
}
for _, t := range testCases {
// HTTP request to download the object.
request, err = newTestSignedRequest("GET", getGetObjectURL(s.endPoint, bucketName, objectName),

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

Loading…
Cancel
Save