Remove ChannelBuffer

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-08-21 18:44:50 -04:00
parent 6ffbe3f6a8
commit 6a0105b452
3 changed files with 29 additions and 75 deletions

View File

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

View File

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

View File

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