From 81c334fa56eab0f9e85c9f6745efa34bbb1b444b Mon Sep 17 00:00:00 2001 From: Morgan Bauer Date: Tue, 8 Dec 2015 15:32:34 -0800 Subject: [PATCH] adjust test sleep timing to avoid spurious failure - refactor ConsumeWithSpeed - documentation Signed-off-by: Morgan Bauer --- pkg/integration/utils.go | 26 +++++++++++++------------- pkg/integration/utils_test.go | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/integration/utils.go b/pkg/integration/utils.go index 330a533904..0524a02ba0 100644 --- a/pkg/integration/utils.go +++ b/pkg/integration/utils.go @@ -272,25 +272,25 @@ func RandomTmpDirPath(s string, platform string) string { 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) { buffer := make([]byte, chunkSize) for { + var readBytes int + readBytes, err = reader.Read(buffer) + n += readBytes + if err != nil { + if err == io.EOF { + err = nil + } + return + } select { case <-stop: 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): } } } diff --git a/pkg/integration/utils_test.go b/pkg/integration/utils_test.go index 7c5df4e90a..06f9eeee9d 100644 --- a/pkg/integration/utils_test.go +++ b/pkg/integration/utils_test.go @@ -363,7 +363,7 @@ func TestConsumeWithSpeed(t *testing.T) { reader := strings.NewReader("1234567890") chunksize := 2 - bytes1, err := ConsumeWithSpeed(reader, chunksize, 1*time.Millisecond, nil) + bytes1, err := ConsumeWithSpeed(reader, chunksize, 1*time.Second, nil) if err != nil { t.Fatal(err) } @@ -385,7 +385,7 @@ func TestConsumeWithSpeedWithStop(t *testing.T) { stopIt <- true }() - bytes1, err := ConsumeWithSpeed(reader, chunksize, 2*time.Millisecond, stopIt) + bytes1, err := ConsumeWithSpeed(reader, chunksize, 20*time.Millisecond, stopIt) if err != nil { t.Fatal(err) }