1 // Copyright 2009 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.
14 func checkWrite(t *testing.T, w Writer, data []byte, c chan int) {
15 n, err := w.Write(data)
17 t.Errorf("write: %v", err)
20 t.Errorf("short write: %d != %d", n, len(data))
25 // Test a single read/write pair.
26 func TestPipe1(t *testing.T) {
29 var buf = make([]byte, 64)
30 go checkWrite(t, w, []byte("hello, world"), c)
33 t.Errorf("read: %v", err)
34 } else if n != 12 || string(buf[0:12]) != "hello, world" {
35 t.Errorf("bad read: got %q", buf[0:n])
42 func reader(t *testing.T, r Reader, c chan int) {
43 var buf = make([]byte, 64)
51 t.Errorf("read: %v", err)
57 // Test a sequence of read/write pairs.
58 func TestPipe2(t *testing.T) {
62 var buf = make([]byte, 64)
63 for i := 0; i < 5; i++ {
67 t.Errorf("wrote %d, got %d", len(p), n)
70 t.Errorf("write: %v", err)
74 t.Errorf("wrote %d, read got %d", n, nn)
80 t.Errorf("final read got %d", nn)
84 type pipeReturn struct {
89 // Test a large write that requires multiple reads to satisfy.
90 func writer(w WriteCloser, buf []byte, c chan pipeReturn) {
91 n, err := w.Write(buf)
93 c <- pipeReturn{n, err}
96 func TestPipe3(t *testing.T) {
97 c := make(chan pipeReturn)
99 var wdat = make([]byte, 128)
100 for i := 0; i < len(wdat); i++ {
103 go writer(w, wdat, c)
104 var rdat = make([]byte, 1024)
106 for n := 1; n <= 256; n *= 2 {
107 nn, err := r.Read(rdat[tot : tot+n])
108 if err != nil && err != EOF {
109 t.Fatalf("read: %v", err)
112 // only final two reads should be short - 1 byte, then 0
119 t.Fatalf("read at end: %v", err)
123 t.Fatalf("read %d, expected %d, got %d", n, expect, nn)
128 if pr.n != 128 || pr.err != nil {
129 t.Fatalf("write 128: %d, %v", pr.n, pr.err)
132 t.Fatalf("total read %d != 128", tot)
134 for i := 0; i < 128; i++ {
135 if rdat[i] != byte(i) {
136 t.Fatalf("rdat[%d] = %d", i, rdat[i])
141 // Test read after/before writer close.
143 type closer interface {
144 CloseWithError(error) error
148 type pipeTest struct {
154 func (p pipeTest) String() string {
155 return fmt.Sprintf("async=%v err=%v closeWithError=%v", p.async, p.err, p.closeWithError)
158 var pipeTests = []pipeTest{
161 {true, ErrShortWrite, true},
164 {false, ErrShortWrite, true},
167 func delayClose(t *testing.T, cl closer, ch chan int, tt pipeTest) {
168 time.Sleep(1 * time.Millisecond)
170 if tt.closeWithError {
171 err = cl.CloseWithError(tt.err)
176 t.Errorf("delayClose: %v", err)
181 func TestPipeReadClose(t *testing.T) {
182 for _, tt := range pipeTests {
183 c := make(chan int, 1)
186 go delayClose(t, w, c, tt)
188 delayClose(t, w, c, tt)
190 var buf = make([]byte, 64)
191 n, err := r.Read(buf)
198 t.Errorf("read from closed pipe: %v want %v", err, want)
201 t.Errorf("read on closed pipe returned %d", n)
203 if err = r.Close(); err != nil {
204 t.Errorf("r.Close: %v", err)
209 // Test close on Read side during Read.
210 func TestPipeReadClose2(t *testing.T) {
211 c := make(chan int, 1)
213 go delayClose(t, r, c, pipeTest{})
214 n, err := r.Read(make([]byte, 64))
216 if n != 0 || err != ErrClosedPipe {
217 t.Errorf("read from closed pipe: %v, %v want %v, %v", n, err, 0, ErrClosedPipe)
221 // Test write after/before reader close.
223 func TestPipeWriteClose(t *testing.T) {
224 for _, tt := range pipeTests {
225 c := make(chan int, 1)
228 go delayClose(t, r, c, tt)
230 delayClose(t, r, c, tt)
232 n, err := WriteString(w, "hello, world")
236 expect = ErrClosedPipe
239 t.Errorf("write on closed pipe: %v want %v", err, expect)
242 t.Errorf("write on closed pipe returned %d", n)
244 if err = w.Close(); err != nil {
245 t.Errorf("w.Close: %v", err)
250 func TestWriteEmpty(t *testing.T) {
261 func TestWriteNil(t *testing.T) {