From 9b58a669e51a2233722a7627fd476289e6f9d58f Mon Sep 17 00:00:00 2001 From: Karthic Rao Date: Wed, 3 May 2017 12:24:22 +0530 Subject: [PATCH] tests: Fix rare test crash (#4175) Fix rare test crash by improving the randomness logic. --- cmd/server_test.go | 35 +++++++++++++++++------------------ cmd/test-utils_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/cmd/server_test.go b/cmd/server_test.go index 157990f3d..2a6dfc120 100644 --- a/cmd/server_test.go +++ b/cmd/server_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), diff --git a/cmd/test-utils_test.go b/cmd/test-utils_test.go index be3a4caeb..42654c138 100644 --- a/cmd/test-utils_test.go +++ b/cmd/test-utils_test.go @@ -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 +}