summaryrefslogtreecommitdiff
path: root/src/lib/io/pipe_test.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-06-06 21:51:05 -0700
committerRuss Cox <rsc@golang.org>2009-06-06 21:51:05 -0700
commit978f53b4c3688bf3473f5fc0d3e11efa51e549f8 (patch)
treec7c0b1bac0db565864aa8407a27e428998f06637 /src/lib/io/pipe_test.go
parent705c431cdfa1972cc11ab200bc0561271edc4605 (diff)
downloadgolang-978f53b4c3688bf3473f5fc0d3e11efa51e549f8.tar.gz
Publish types PipeReader and PipeWriter
to expose new CloseWithError methods. R=r DELTA=161 (72 added, 15 deleted, 74 changed) OCL=29980 CL=30003
Diffstat (limited to 'src/lib/io/pipe_test.go')
-rw-r--r--src/lib/io/pipe_test.go132
1 files changed, 77 insertions, 55 deletions
diff --git a/src/lib/io/pipe_test.go b/src/lib/io/pipe_test.go
index 3df669628..277f44525 100644
--- a/src/lib/io/pipe_test.go
+++ b/src/lib/io/pipe_test.go
@@ -5,6 +5,7 @@
package io
import (
+ "fmt";
"io";
"os";
"testing";
@@ -132,72 +133,93 @@ func TestPipe3(t *testing.T) {
// Test read after/before writer close.
-func delayClose(t *testing.T, cl Closer, ch chan int) {
- time.Sleep(1000*1000); // 1 ms
- if err := cl.Close(); err != nil {
- t.Errorf("delayClose: %v", err);
- }
- ch <- 0;
+type closer interface {
+ CloseWithError(os.Error) os.Error;
+ Close() os.Error;
}
-func testPipeReadClose(t *testing.T, async bool) {
- c := make(chan int, 1);
- r, w := Pipe();
- if async {
- go delayClose(t, w, c);
- } else {
- delayClose(t, w, c);
- }
- var buf = make([]byte, 64);
- n, err := r.Read(buf);
- <-c;
- if err != nil {
- t.Errorf("read from closed pipe: %v", err);
- }
- if n != 0 {
- t.Errorf("read on closed pipe returned %d", n);
- }
- if err = r.Close(); err != nil {
- t.Errorf("r.Close: %v", err);
- }
+type pipeTest struct {
+ async bool;
+ err os.Error;
+ closeWithError bool;
}
-// Test write after/before reader close.
+func (p pipeTest) String() string {
+ return fmt.Sprintf("async=%v err=%v closeWithError=%v", p.async, p.err, p.closeWithError);
+}
-func testPipeWriteClose(t *testing.T, async bool) {
- c := make(chan int, 1);
- r, w := Pipe();
- if async {
- go delayClose(t, r, c);
+var pipeTests = []pipeTest {
+ pipeTest{ true, nil, false },
+ pipeTest{ true, nil, true },
+ pipeTest{ true, io.ErrShortWrite, true },
+ pipeTest{ false, nil, false },
+ pipeTest{ false, nil, true },
+ pipeTest{ false, io.ErrShortWrite, true },
+}
+
+func delayClose(t *testing.T, cl closer, ch chan int, tt pipeTest) {
+ time.Sleep(1e6); // 1 ms
+ var err os.Error;
+ if tt.closeWithError {
+ err = cl.CloseWithError(tt.err);
} else {
- delayClose(t, r, c);
- }
- n, err := WriteString(w, "hello, world");
- <-c;
- if err != os.EPIPE {
- t.Errorf("write on closed pipe: %v", err);
- }
- if n != 0 {
- t.Errorf("write on closed pipe returned %d", n);
+ err = cl.Close();
}
- if err = w.Close(); err != nil {
- t.Errorf("w.Close: %v", err);
+ if err != nil {
+ t.Errorf("delayClose: %v", err);
}
+ ch <- 0;
}
-func TestPipeReadCloseAsync(t *testing.T) {
- testPipeReadClose(t, true);
-}
-
-func TestPipeReadCloseSync(t *testing.T) {
- testPipeReadClose(t, false);
+func TestPipeReadClose(t *testing.T) {
+ for _, tt := range pipeTests {
+ c := make(chan int, 1);
+ r, w := Pipe();
+ if tt.async {
+ go delayClose(t, w, c, tt);
+ } else {
+ delayClose(t, w, c, tt);
+ }
+ var buf = make([]byte, 64);
+ n, err := r.Read(buf);
+ <-c;
+ if err != tt.err {
+ t.Errorf("read from closed pipe: %v want %v", err, tt.err);
+ }
+ if n != 0 {
+ t.Errorf("read on closed pipe returned %d", n);
+ }
+ if err = r.Close(); err != nil {
+ t.Errorf("r.Close: %v", err);
+ }
+ }
}
-func TestPipeWriteCloseAsync(t *testing.T) {
- testPipeWriteClose(t, true);
-}
+// Test write after/before reader close.
-func TestPipeWriteCloseSync(t *testing.T) {
- testPipeWriteClose(t, false);
+func TestPipeWriteClose(t *testing.T) {
+ for _, tt := range pipeTests {
+ c := make(chan int, 1);
+ r, w := Pipe();
+ if tt.async {
+ go delayClose(t, r, c, tt);
+ } else {
+ delayClose(t, r, c, tt);
+ }
+ n, err := WriteString(w, "hello, world");
+ <-c;
+ expect := tt.err;
+ if expect == nil {
+ expect = os.EPIPE;
+ }
+ if err != expect {
+ t.Errorf("write on closed pipe: %v want %v", err, expect);
+ }
+ if n != 0 {
+ t.Errorf("write on closed pipe returned %d", n);
+ }
+ if err = w.Close(); err != nil {
+ t.Errorf("w.Close: %v", err);
+ }
+ }
}
-