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) c.Assert(response.StatusCode, Equals, http.StatusOK)
var buffer bytes.Buffer var buffer bytes.Buffer
line := `1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, // data to be written into buffer.
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, data := "1234567890"
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, // seed the random number generator once.
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, rand.Seed(3)
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, // generate a random number between 13 and 200.
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, randInt := getRandomRange(13, 200, -1)
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, // write into buffer till length of the buffer is greater than the generated random number.
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, for i := 0; i <= randInt; i += 10 {
1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890, buffer.WriteString(data)
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)]))
} }
putContent := buffer.String() // String content which is used for put object range test.
buf := bytes.NewReader([]byte(putContent)) 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" objectName := "test-big-file"
// HTTP request to upload the object. // HTTP request to upload the object.
request, err = newTestSignedRequest("PUT", getPutObjectURL(s.endPoint, bucketName, objectName), 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. // request for last 7 bytes of the object.
{"-7", putContent[len(putContent)-7:]}, {"-7", putContent[len(putContent)-7:]},
} }
for _, t := range testCases { for _, t := range testCases {
// HTTP request to download the object. // HTTP request to download the object.
request, err = newTestSignedRequest("GET", getGetObjectURL(s.endPoint, bucketName, objectName), request, err = newTestSignedRequest("GET", getGetObjectURL(s.endPoint, bucketName, objectName),

@ -2361,3 +2361,28 @@ func getEndpointsLocalAddr(endpoints EndpointList) string {
return globalMinioHost + ":" + globalMinioPort 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