1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

adjust test sleep timing to avoid spurious failure

- refactor ConsumeWithSpeed
 - documentation

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
This commit is contained in:
Morgan Bauer 2015-12-08 15:32:34 -08:00
parent 9381c744b8
commit 81c334fa56
No known key found for this signature in database
GPG key ID: 23F15C502128F348
2 changed files with 15 additions and 15 deletions

View file

@ -272,15 +272,12 @@ 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. // ConsumeWithSpeed reads chunkSize bytes from reader before sleeping
// Returns total read bytes. // 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 {
select {
case <-stop:
return
default:
var readBytes int var readBytes int
readBytes, err = reader.Read(buffer) readBytes, err = reader.Read(buffer)
n += readBytes n += readBytes
@ -290,7 +287,10 @@ func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, s
} }
return return
} }
time.Sleep(interval) select {
case <-stop:
return
case <-time.After(interval):
} }
} }
} }

View file

@ -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)
} }