浏览代码

Merge pull request #18518 from MHBauer/spurious-timing

adjust test sleep timing to avoid spurious failure
Vincent Demeester 9 年之前
父节点
当前提交
8ec00e6d0e
共有 2 个文件被更改,包括 15 次插入15 次删除
  1. 13 13
      pkg/integration/utils.go
  2. 2 2
      pkg/integration/utils_test.go

+ 13 - 13
pkg/integration/utils.go

@@ -272,25 +272,25 @@ func RandomTmpDirPath(s string, platform string) string {
 	return filepath.ToSlash(path) // Using /
 	return filepath.ToSlash(path) // Using /
 }
 }
 
 
-// ConsumeWithSpeed reads chunkSize bytes from reader after every interval.
-// Returns total read bytes.
+// ConsumeWithSpeed reads chunkSize bytes from reader before sleeping
+// for interval duration. Returns total read bytes. Send true to the
+// stop channel to return before reading to EOF on the reader.
 func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
 func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
 	buffer := make([]byte, chunkSize)
 	buffer := make([]byte, chunkSize)
 	for {
 	for {
+		var readBytes int
+		readBytes, err = reader.Read(buffer)
+		n += readBytes
+		if err != nil {
+			if err == io.EOF {
+				err = nil
+			}
+			return
+		}
 		select {
 		select {
 		case <-stop:
 		case <-stop:
 			return
 			return
-		default:
-			var readBytes int
-			readBytes, err = reader.Read(buffer)
-			n += readBytes
-			if err != nil {
-				if err == io.EOF {
-					err = nil
-				}
-				return
-			}
-			time.Sleep(interval)
+		case <-time.After(interval):
 		}
 		}
 	}
 	}
 }
 }

+ 2 - 2
pkg/integration/utils_test.go

@@ -363,7 +363,7 @@ func TestConsumeWithSpeed(t *testing.T) {
 	reader := strings.NewReader("1234567890")
 	reader := strings.NewReader("1234567890")
 	chunksize := 2
 	chunksize := 2
 
 
-	bytes1, err := ConsumeWithSpeed(reader, chunksize, 1*time.Millisecond, nil)
+	bytes1, err := ConsumeWithSpeed(reader, chunksize, 1*time.Second, nil)
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
@@ -385,7 +385,7 @@ func TestConsumeWithSpeedWithStop(t *testing.T) {
 		stopIt <- true
 		stopIt <- true
 	}()
 	}()
 
 
-	bytes1, err := ConsumeWithSpeed(reader, chunksize, 2*time.Millisecond, stopIt)
+	bytes1, err := ConsumeWithSpeed(reader, chunksize, 20*time.Millisecond, stopIt)
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}