1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 func TestPipeClose(t *testing.T) {
17 p.b = new(bytes.Buffer)
22 _, err := p.Read(make([]byte, 1))
24 t.Errorf("err = %v want %v", err, a)
28 func TestPipeDoneChan(t *testing.T) {
33 t.Fatal("done too soon")
36 p.CloseWithError(io.EOF)
40 t.Fatal("should be done")
44 func TestPipeDoneChan_ErrFirst(t *testing.T) {
46 p.CloseWithError(io.EOF)
51 t.Fatal("should be done")
55 func TestPipeDoneChan_Break(t *testing.T) {
60 t.Fatal("done too soon")
63 p.BreakWithError(io.EOF)
67 t.Fatal("should be done")
71 func TestPipeDoneChan_Break_ErrFirst(t *testing.T) {
73 p.BreakWithError(io.EOF)
78 t.Fatal("should be done")
82 func TestPipeCloseWithError(t *testing.T) {
83 p := &pipe{b: new(bytes.Buffer)}
85 io.WriteString(p, body)
86 a := errors.New("test error")
88 all, err := ioutil.ReadAll(p)
89 if string(all) != body {
90 t.Errorf("read bytes = %q; want %q", all, body)
93 t.Logf("read error = %v, %v", err, a)
95 // Read and Write should fail.
96 if n, err := p.Write([]byte("abc")); err != errClosedPipeWrite || n != 0 {
97 t.Errorf("Write(abc) after close\ngot %v, %v\nwant 0, %v", n, err, errClosedPipeWrite)
99 if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
100 t.Errorf("Read() after close\ngot %v, nil\nwant 0, %v", n, errClosedPipeWrite)
104 func TestPipeBreakWithError(t *testing.T) {
105 p := &pipe{b: new(bytes.Buffer)}
106 io.WriteString(p, "foo")
107 a := errors.New("test err")
109 all, err := ioutil.ReadAll(p)
110 if string(all) != "" {
111 t.Errorf("read bytes = %q; want empty string", all)
114 t.Logf("read error = %v, %v", err, a)
117 t.Errorf("buffer should be nil after BreakWithError")
119 // Write should succeed silently.
120 if n, err := p.Write([]byte("abc")); err != nil || n != 3 {
121 t.Errorf("Write(abc) after break\ngot %v, %v\nwant 0, nil", n, err)
124 t.Errorf("buffer should be nil after Write")
127 if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
128 t.Errorf("Read() after close\ngot %v, nil\nwant 0, not nil", n)