mirror of https://github.com/docker/docs.git
Generalize consumeSlow and add stop support
Signed-off-by: Tõnis Tiigi <tonistiigi@gmail.com> (github: tonistiigi)
This commit is contained in:
parent
8a81c46272
commit
417e48e4a0
|
@ -4,7 +4,6 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -2462,7 +2461,7 @@ func TestRunSlowStdoutConsumer(t *testing.T) {
|
||||||
if err := c.Start(); err != nil {
|
if err := c.Start(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
n, err := consumeSlow(stdout, 10000, 5*time.Millisecond)
|
n, err := consumeWithSpeed(stdout, 10000, 5*time.Millisecond, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -254,18 +254,25 @@ func makeRandomString(n int) string {
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func consumeSlow(reader io.Reader, chunkSize int, interval time.Duration) (n int, err error) {
|
// Reads chunkSize bytes from reader after every interval.
|
||||||
|
// Returns total read bytes.
|
||||||
|
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
|
select {
|
||||||
readBytes, err = reader.Read(buffer)
|
case <-stop:
|
||||||
n += readBytes
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
time.Sleep(interval)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue