Skip to content

Instantly share code, notes, and snippets.

@tonistiigi
Last active August 29, 2015 14:08
Show Gist options
  • Save tonistiigi/924fd998584c00b4083e to your computer and use it in GitHub Desktop.
Save tonistiigi/924fd998584c00b4083e to your computer and use it in GitHub Desktop.
TestAttachSlowContainer
func TestAttachSlowContainer(t *testing.T) {
defer deleteAllContainers()
c := exec.Command("/bin/bash", "-c", dockerBinary+` run --rm -i busybox /bin/sh -c "dd if=/dev/zero of=/foo bs=1024 count=1000 &>/dev/null; catv /foo"`)
stdout, err := c.StdoutPipe()
if err != nil {
t.Fatal(err)
}
if err := c.Start(); err != nil {
t.Fatal(err)
}
n, err := consumeSlow(stdout, 50000, 5 * time.Millisecond)
if err != nil {
t.Fatal(err)
}
expected := 2 * 1024 * 1000
if n != expected {
t.Fatalf("count mismatch: %d expected %d", n, expected)
}
}
func consumeSlow(reader io.Reader, chunkSize int, interval time.Duration) (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
}
time.Sleep(interval)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment