Signed-off-by: Tõnis Tiigi <tonistiigi@gmail.com> (github: tonistiigi)
| ... | ... |
@@ -4,7 +4,6 @@ import ( |
| 4 | 4 |
"bufio" |
| 5 | 5 |
"bytes" |
| 6 | 6 |
"fmt" |
| 7 |
- "io" |
|
| 8 | 7 |
"io/ioutil" |
| 9 | 8 |
"net" |
| 10 | 9 |
"os" |
| ... | ... |
@@ -2462,7 +2461,7 @@ func TestRunSlowStdoutConsumer(t *testing.T) {
|
| 2462 | 2462 |
if err := c.Start(); err != nil {
|
| 2463 | 2463 |
t.Fatal(err) |
| 2464 | 2464 |
} |
| 2465 |
- n, err := consumeSlow(stdout, 10000, 5*time.Millisecond) |
|
| 2465 |
+ n, err := consumeWithSpeed(stdout, 10000, 5*time.Millisecond, nil) |
|
| 2466 | 2466 |
if err != nil {
|
| 2467 | 2467 |
t.Fatal(err) |
| 2468 | 2468 |
} |
| ... | ... |
@@ -254,18 +254,25 @@ func makeRandomString(n int) string {
|
| 254 | 254 |
return string(b) |
| 255 | 255 |
} |
| 256 | 256 |
|
| 257 |
-func consumeSlow(reader io.Reader, chunkSize int, interval time.Duration) (n int, err error) {
|
|
| 257 |
+// Reads chunkSize bytes from reader after every interval. |
|
| 258 |
+// Returns total read bytes. |
|
| 259 |
+func consumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
|
|
| 258 | 260 |
buffer := make([]byte, chunkSize) |
| 259 | 261 |
for {
|
| 260 |
- var readBytes int |
|
| 261 |
- readBytes, err = reader.Read(buffer) |
|
| 262 |
- n += readBytes |
|
| 263 |
- if err != nil {
|
|
| 264 |
- if err == io.EOF {
|
|
| 265 |
- err = nil |
|
| 266 |
- } |
|
| 262 |
+ select {
|
|
| 263 |
+ case <-stop: |
|
| 267 | 264 |
return |
| 265 |
+ default: |
|
| 266 |
+ var readBytes int |
|
| 267 |
+ readBytes, err = reader.Read(buffer) |
|
| 268 |
+ n += readBytes |
|
| 269 |
+ if err != nil {
|
|
| 270 |
+ if err == io.EOF {
|
|
| 271 |
+ err = nil |
|
| 272 |
+ } |
|
| 273 |
+ return |
|
| 274 |
+ } |
|
| 275 |
+ time.Sleep(interval) |
|
| 268 | 276 |
} |
| 269 |
- time.Sleep(interval) |
|
| 270 | 277 |
} |
| 271 | 278 |
} |