From 6a0105b452e604866320aad7e2ad291beb7925b9 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 21 Aug 2017 18:44:50 -0400 Subject: [PATCH] Remove ChannelBuffer Signed-off-by: Daniel Nephin --- integration-cli/docker_api_containers_test.go | 30 +++++++++++- pkg/testutil/utils.go | 28 ----------- pkg/testutil/utils_test.go | 46 ------------------- 3 files changed, 29 insertions(+), 75 deletions(-) diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 72b4807a55..4f2f97f7d5 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -215,7 +215,7 @@ func (s *DockerSuite) TestGetContainerStatsRmRunning(c *check.C) { out := runSleepingContainer(c) id := strings.TrimSpace(out) - buf := &testutil.ChannelBuffer{C: make(chan []byte, 1)} + buf := &ChannelBuffer{C: make(chan []byte, 1)} defer buf.Close() _, body, err := request.Get("/containers/"+id+"/stats?stream=1", request.JSON) @@ -243,6 +243,34 @@ func (s *DockerSuite) TestGetContainerStatsRmRunning(c *check.C) { c.Assert(<-chErr, checker.IsNil) } +// ChannelBuffer holds a chan of byte array that can be populate in a goroutine. +type ChannelBuffer struct { + C chan []byte +} + +// Write implements Writer. +func (c *ChannelBuffer) Write(b []byte) (int, error) { + c.C <- b + return len(b), nil +} + +// Close closes the go channel. +func (c *ChannelBuffer) Close() error { + close(c.C) + return nil +} + +// ReadTimeout reads the content of the channel in the specified byte array with +// the specified duration as timeout. +func (c *ChannelBuffer) ReadTimeout(p []byte, n time.Duration) (int, error) { + select { + case b := <-c.C: + return copy(p[0:], b), nil + case <-time.After(n): + return -1, fmt.Errorf("timeout reading from channel") + } +} + // regression test for gh13421 // previous test was just checking one stat entry so it didn't fail (stats with // stream false always return one stat) diff --git a/pkg/testutil/utils.go b/pkg/testutil/utils.go index 2ef301e4ac..a23552c423 100644 --- a/pkg/testutil/utils.go +++ b/pkg/testutil/utils.go @@ -118,34 +118,6 @@ func ParseCgroupPaths(procCgroupData string) map[string]string { return cgroupPaths } -// ChannelBuffer holds a chan of byte array that can be populate in a goroutine. -type ChannelBuffer struct { - C chan []byte -} - -// Write implements Writer. -func (c *ChannelBuffer) Write(b []byte) (int, error) { - c.C <- b - return len(b), nil -} - -// Close closes the go channel. -func (c *ChannelBuffer) Close() error { - close(c.C) - return nil -} - -// ReadTimeout reads the content of the channel in the specified byte array with -// the specified duration as timeout. -func (c *ChannelBuffer) ReadTimeout(p []byte, n time.Duration) (int, error) { - select { - case b := <-c.C: - return copy(p[0:], b), nil - case <-time.After(n): - return -1, fmt.Errorf("timeout reading from channel") - } -} - // ReadBody read the specified ReadCloser content and returns it func ReadBody(b io.ReadCloser) ([]byte, error) { defer b.Close() diff --git a/pkg/testutil/utils_test.go b/pkg/testutil/utils_test.go index 28dff0f254..fb55dbdf3f 100644 --- a/pkg/testutil/utils_test.go +++ b/pkg/testutil/utils_test.go @@ -1,7 +1,6 @@ package testutil import ( - "io" "os" "os/exec" "runtime" @@ -138,48 +137,3 @@ func TestParseCgroupPaths(t *testing.T) { t.Fatalf("Expected cgroupMap to contains an entry for 'cpuset' with value '/b', got %v", cgroupMap) } } - -func TestChannelBufferTimeout(t *testing.T) { - expected := "11" - - buf := &ChannelBuffer{make(chan []byte, 1)} - defer buf.Close() - - done := make(chan struct{}, 1) - go func() { - time.Sleep(100 * time.Millisecond) - io.Copy(buf, strings.NewReader(expected)) - done <- struct{}{} - }() - - // Wait long enough - b := make([]byte, 2) - _, err := buf.ReadTimeout(b, 50*time.Millisecond) - if err == nil && err.Error() != "timeout reading from channel" { - t.Fatalf("Expected an error, got %s", err) - } - <-done -} - -func TestChannelBuffer(t *testing.T) { - expected := "11" - - buf := &ChannelBuffer{make(chan []byte, 1)} - defer buf.Close() - - go func() { - time.Sleep(100 * time.Millisecond) - io.Copy(buf, strings.NewReader(expected)) - }() - - // Wait long enough - b := make([]byte, 2) - _, err := buf.ReadTimeout(b, 200*time.Millisecond) - if err != nil { - t.Fatal(err) - } - - if string(b) != expected { - t.Fatalf("Expected '%s', got '%s'", expected, string(b)) - } -}