Browse code

Merge pull request #18518 from MHBauer/spurious-timing

adjust test sleep timing to avoid spurious failure

Vincent Demeester authored on 2015/12/17 04:45:55
Showing 2 changed files
... ...
@@ -272,25 +272,25 @@ func RandomTmpDirPath(s string, platform string) string {
272 272
 	return filepath.ToSlash(path) // Using /
273 273
 }
274 274
 
275
-// ConsumeWithSpeed reads chunkSize bytes from reader after every interval.
276
-// Returns total read bytes.
275
+// ConsumeWithSpeed reads chunkSize bytes from reader before sleeping
276
+// for interval duration. Returns total read bytes. Send true to the
277
+// stop channel to return before reading to EOF on the reader.
277 278
 func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
278 279
 	buffer := make([]byte, chunkSize)
279 280
 	for {
281
+		var readBytes int
282
+		readBytes, err = reader.Read(buffer)
283
+		n += readBytes
284
+		if err != nil {
285
+			if err == io.EOF {
286
+				err = nil
287
+			}
288
+			return
289
+		}
280 290
 		select {
281 291
 		case <-stop:
282 292
 			return
283
-		default:
284
-			var readBytes int
285
-			readBytes, err = reader.Read(buffer)
286
-			n += readBytes
287
-			if err != nil {
288
-				if err == io.EOF {
289
-					err = nil
290
-				}
291
-				return
292
-			}
293
-			time.Sleep(interval)
293
+		case <-time.After(interval):
294 294
 		}
295 295
 	}
296 296
 }
... ...
@@ -363,7 +363,7 @@ func TestConsumeWithSpeed(t *testing.T) {
363 363
 	reader := strings.NewReader("1234567890")
364 364
 	chunksize := 2
365 365
 
366
-	bytes1, err := ConsumeWithSpeed(reader, chunksize, 1*time.Millisecond, nil)
366
+	bytes1, err := ConsumeWithSpeed(reader, chunksize, 1*time.Second, nil)
367 367
 	if err != nil {
368 368
 		t.Fatal(err)
369 369
 	}
... ...
@@ -385,7 +385,7 @@ func TestConsumeWithSpeedWithStop(t *testing.T) {
385 385
 		stopIt <- true
386 386
 	}()
387 387
 
388
-	bytes1, err := ConsumeWithSpeed(reader, chunksize, 2*time.Millisecond, stopIt)
388
+	bytes1, err := ConsumeWithSpeed(reader, chunksize, 20*time.Millisecond, stopIt)
389 389
 	if err != nil {
390 390
 		t.Fatal(err)
391 391
 	}