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

Remove ConsumeWithSpeed

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-08-21 18:58:25 -04:00
parent 4f304e72a2
commit 1455086c4b
4 changed files with 26 additions and 66 deletions

View file

@ -232,11 +232,11 @@ func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
c.Assert(logCmd.Start(), checker.IsNil)
// First read slowly
bytes1, err := testutil.ConsumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
bytes1, err := ConsumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
c.Assert(err, checker.IsNil)
// After the container has finished we can continue reading fast
bytes2, err := testutil.ConsumeWithSpeed(stdout, 32*1024, 0, nil)
bytes2, err := ConsumeWithSpeed(stdout, 32*1024, 0, nil)
c.Assert(err, checker.IsNil)
actual := bytes1 + bytes2
@ -244,6 +244,29 @@ func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
c.Assert(actual, checker.Equals, expected)
}
// 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
case <-time.After(interval):
}
}
}
func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
id := strings.TrimSpace(out)

View file

@ -2247,7 +2247,7 @@ func (s *DockerSuite) TestRunSlowStdoutConsumer(c *check.C) {
if err := cont.Start(); err != nil {
c.Fatal(err)
}
n, err := testutil.ConsumeWithSpeed(stdout, 10000, 5*time.Millisecond, nil)
n, err := ConsumeWithSpeed(stdout, 10000, 5*time.Millisecond, nil)
if err != nil {
c.Fatal(err)
}

View file

@ -3,12 +3,10 @@ package testutil
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/system"
@ -80,29 +78,6 @@ func RandomTmpDirPath(s string, platform string) string {
return filepath.ToSlash(path) // Using /
}
// 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
case <-time.After(interval):
}
}
}
// ParseCgroupPaths parses 'procCgroupData', which is output of '/proc/<pid>/cgroup', and returns
// a map which cgroup name as key and path as value.
func ParseCgroupPaths(procCgroupData string) map[string]string {

View file

@ -6,7 +6,6 @@ import (
"runtime"
"strings"
"testing"
"time"
)
func TestRunCommandPipelineWithOutputWithNotEnoughCmds(t *testing.T) {
@ -73,43 +72,6 @@ func TestRandomTmpDirPath(t *testing.T) {
}
}
func TestConsumeWithSpeed(t *testing.T) {
reader := strings.NewReader("1234567890")
chunksize := 2
bytes1, err := ConsumeWithSpeed(reader, chunksize, 10*time.Millisecond, nil)
if err != nil {
t.Fatal(err)
}
if bytes1 != 10 {
t.Fatalf("Expected to have read 10 bytes, got %d", bytes1)
}
}
func TestConsumeWithSpeedWithStop(t *testing.T) {
reader := strings.NewReader("1234567890")
chunksize := 2
stopIt := make(chan bool)
go func() {
time.Sleep(1 * time.Millisecond)
stopIt <- true
}()
bytes1, err := ConsumeWithSpeed(reader, chunksize, 20*time.Millisecond, stopIt)
if err != nil {
t.Fatal(err)
}
if bytes1 != 2 {
t.Fatalf("Expected to have read 2 bytes, got %d", bytes1)
}
}
func TestParseCgroupPathsEmpty(t *testing.T) {
cgroupMap := ParseCgroupPaths("")
if len(cgroupMap) != 0 {