github.com/nf/docker@v1.8.1/pkg/progressreader/progressreader_test.go (about) 1 package progressreader 2 3 import ( 4 "bufio" 5 "bytes" 6 "io" 7 "io/ioutil" 8 "testing" 9 10 "github.com/docker/docker/pkg/streamformatter" 11 ) 12 13 func TestOutputOnPrematureClose(t *testing.T) { 14 var outBuf bytes.Buffer 15 content := []byte("TESTING") 16 reader := ioutil.NopCloser(bytes.NewReader(content)) 17 writer := bufio.NewWriter(&outBuf) 18 19 prCfg := Config{ 20 In: reader, 21 Out: writer, 22 Formatter: streamformatter.NewStreamFormatter(), 23 Size: len(content), 24 NewLines: true, 25 ID: "Test", 26 Action: "Read", 27 } 28 pr := New(prCfg) 29 30 part := make([]byte, 4, 4) 31 _, err := io.ReadFull(pr, part) 32 if err != nil { 33 pr.Close() 34 t.Fatal(err) 35 } 36 37 if err := writer.Flush(); err != nil { 38 pr.Close() 39 t.Fatal(err) 40 } 41 42 tlen := outBuf.Len() 43 pr.Close() 44 if err := writer.Flush(); err != nil { 45 t.Fatal(err) 46 } 47 48 if outBuf.Len() == tlen { 49 t.Fatalf("Expected some output when closing prematurely") 50 } 51 } 52 53 func TestCompleteSilently(t *testing.T) { 54 var outBuf bytes.Buffer 55 content := []byte("TESTING") 56 reader := ioutil.NopCloser(bytes.NewReader(content)) 57 writer := bufio.NewWriter(&outBuf) 58 59 prCfg := Config{ 60 In: reader, 61 Out: writer, 62 Formatter: streamformatter.NewStreamFormatter(), 63 Size: len(content), 64 NewLines: true, 65 ID: "Test", 66 Action: "Read", 67 } 68 pr := New(prCfg) 69 70 out, err := ioutil.ReadAll(pr) 71 if err != nil { 72 pr.Close() 73 t.Fatal(err) 74 } 75 if string(out) != "TESTING" { 76 pr.Close() 77 t.Fatalf("Unexpected output %q from reader", string(out)) 78 } 79 80 if err := writer.Flush(); err != nil { 81 pr.Close() 82 t.Fatal(err) 83 } 84 85 tlen := outBuf.Len() 86 pr.Close() 87 if err := writer.Flush(); err != nil { 88 t.Fatal(err) 89 } 90 91 if outBuf.Len() > tlen { 92 t.Fatalf("Should have closed silently when read is complete") 93 } 94 }