2009-02-16 17:32:30 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-08-12 14:18:37 -06:00
|
|
|
package io_test
|
2009-02-16 17:32:30 -07:00
|
|
|
|
|
|
|
import (
|
2009-10-06 20:41:51 -06:00
|
|
|
"fmt";
|
|
|
|
. "io";
|
|
|
|
"os";
|
|
|
|
"strings";
|
|
|
|
"testing";
|
|
|
|
"time";
|
2009-02-16 17:32:30 -07:00
|
|
|
)
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func checkWrite(t *testing.T, w Writer, data []byte, c chan int) {
|
2009-02-16 17:32:30 -07:00
|
|
|
n, err := w.Write(data);
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("write: %v", err);
|
|
|
|
}
|
|
|
|
if n != len(data) {
|
|
|
|
t.Errorf("short write: %d != %d", n, len(data));
|
|
|
|
}
|
|
|
|
c <- 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a single read/write pair.
|
|
|
|
func TestPipe1(t *testing.T) {
|
|
|
|
c := make(chan int);
|
2009-05-08 12:22:57 -06:00
|
|
|
r, w := Pipe();
|
2009-04-15 21:27:45 -06:00
|
|
|
var buf = make([]byte, 64);
|
2009-06-29 16:24:23 -06:00
|
|
|
go checkWrite(t, w, strings.Bytes("hello, world"), c);
|
2009-02-16 17:32:30 -07:00
|
|
|
n, err := r.Read(buf);
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("read: %v", err);
|
2009-10-06 20:41:51 -06:00
|
|
|
} else if n != 12 || string(buf[0:12]) != "hello, world" {
|
2009-02-16 17:32:30 -07:00
|
|
|
t.Errorf("bad read: got %q", buf[0:n]);
|
|
|
|
}
|
|
|
|
<-c;
|
|
|
|
r.Close();
|
|
|
|
w.Close();
|
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
func reader(t *testing.T, r Reader, c chan int) {
|
2009-04-15 21:27:45 -06:00
|
|
|
var buf = make([]byte, 64);
|
2009-02-16 17:32:30 -07:00
|
|
|
for {
|
|
|
|
n, err := r.Read(buf);
|
2009-06-22 14:26:13 -06:00
|
|
|
if err == os.EOF {
|
|
|
|
c <- 0;
|
|
|
|
break;
|
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("read: %v", err);
|
|
|
|
}
|
|
|
|
c <- n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a sequence of read/write pairs.
|
|
|
|
func TestPipe2(t *testing.T) {
|
|
|
|
c := make(chan int);
|
2009-05-08 12:22:57 -06:00
|
|
|
r, w := Pipe();
|
2009-02-16 17:32:30 -07:00
|
|
|
go reader(t, r, c);
|
2009-04-15 21:27:45 -06:00
|
|
|
var buf = make([]byte, 64);
|
2009-02-16 17:32:30 -07:00
|
|
|
for i := 0; i < 5; i++ {
|
2009-10-06 20:41:51 -06:00
|
|
|
p := buf[0 : 5 + i*10];
|
2009-02-16 17:32:30 -07:00
|
|
|
n, err := w.Write(p);
|
|
|
|
if n != len(p) {
|
|
|
|
t.Errorf("wrote %d, got %d", len(p), n);
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("write: %v", err);
|
|
|
|
}
|
|
|
|
nn := <-c;
|
|
|
|
if nn != n {
|
|
|
|
t.Errorf("wrote %d, read got %d", n, nn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Close();
|
|
|
|
nn := <-c;
|
|
|
|
if nn != 0 {
|
|
|
|
t.Errorf("final read got %d", nn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-12 14:18:37 -06:00
|
|
|
type pipeReturn struct {
|
2009-10-06 20:41:51 -06:00
|
|
|
n int;
|
|
|
|
err os.Error;
|
2009-08-12 14:18:37 -06:00
|
|
|
}
|
|
|
|
|
2009-02-16 17:32:30 -07:00
|
|
|
// Test a large write that requires multiple reads to satisfy.
|
2009-05-08 12:22:57 -06:00
|
|
|
func writer(w WriteCloser, buf []byte, c chan pipeReturn) {
|
2009-02-16 17:32:30 -07:00
|
|
|
n, err := w.Write(buf);
|
|
|
|
w.Close();
|
2009-03-03 09:39:12 -07:00
|
|
|
c <- pipeReturn{n, err};
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipe3(t *testing.T) {
|
|
|
|
c := make(chan pipeReturn);
|
2009-05-08 12:22:57 -06:00
|
|
|
r, w := Pipe();
|
2009-04-15 21:27:45 -06:00
|
|
|
var wdat = make([]byte, 128);
|
2009-02-16 17:32:30 -07:00
|
|
|
for i := 0; i < len(wdat); i++ {
|
|
|
|
wdat[i] = byte(i);
|
|
|
|
}
|
|
|
|
go writer(w, wdat, c);
|
2009-04-15 21:27:45 -06:00
|
|
|
var rdat = make([]byte, 1024);
|
2009-02-16 17:32:30 -07:00
|
|
|
tot := 0;
|
|
|
|
for n := 1; n <= 256; n *= 2 {
|
2009-10-06 20:41:51 -06:00
|
|
|
nn, err := r.Read(rdat[tot : tot+n]);
|
2009-06-22 14:26:13 -06:00
|
|
|
if err != nil && err != os.EOF {
|
2009-02-16 17:32:30 -07:00
|
|
|
t.Fatalf("read: %v", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// only final two reads should be short - 1 byte, then 0
|
|
|
|
expect := n;
|
|
|
|
if n == 128 {
|
|
|
|
expect = 1;
|
|
|
|
} else if n == 256 {
|
|
|
|
expect = 0;
|
2009-06-22 14:26:13 -06:00
|
|
|
if err != os.EOF {
|
|
|
|
t.Fatalf("read at end: %v", err);
|
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
if nn != expect {
|
|
|
|
t.Fatalf("read %d, expected %d, got %d", n, expect, nn);
|
|
|
|
}
|
|
|
|
tot += nn;
|
|
|
|
}
|
|
|
|
pr := <-c;
|
|
|
|
if pr.n != 128 || pr.err != nil {
|
|
|
|
t.Fatalf("write 128: %d, %v", pr.n, pr.err);
|
|
|
|
}
|
|
|
|
if tot != 128 {
|
|
|
|
t.Fatalf("total read %d != 128", tot);
|
|
|
|
}
|
|
|
|
for i := 0; i < 128; i++ {
|
|
|
|
if rdat[i] != byte(i) {
|
|
|
|
t.Fatalf("rdat[%d] = %d", i, rdat[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test read after/before writer close.
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
type closer interface {
|
|
|
|
CloseWithError(os.Error) os.Error;
|
|
|
|
Close() os.Error;
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
type pipeTest struct {
|
2009-10-06 20:41:51 -06:00
|
|
|
async bool;
|
|
|
|
err os.Error;
|
|
|
|
closeWithError bool;
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
func (p pipeTest) String() string {
|
|
|
|
return fmt.Sprintf("async=%v err=%v closeWithError=%v", p.async, p.err, p.closeWithError);
|
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
|
2009-10-06 20:41:51 -06:00
|
|
|
var pipeTests = []pipeTest{
|
|
|
|
pipeTest{true, nil, false},
|
|
|
|
pipeTest{true, nil, true},
|
|
|
|
pipeTest{true, ErrShortWrite, true},
|
|
|
|
pipeTest{false, nil, false},
|
|
|
|
pipeTest{false, nil, true},
|
|
|
|
pipeTest{false, ErrShortWrite, true},
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2009-02-16 17:32:30 -07:00
|
|
|
} else {
|
2009-06-06 22:51:05 -06:00
|
|
|
err = cl.Close();
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-06-06 22:51:05 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("delayClose: %v", err);
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
2009-06-06 22:51:05 -06:00
|
|
|
ch <- 0;
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
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;
|
2009-06-22 14:26:13 -06:00
|
|
|
want := tt.err;
|
|
|
|
if want == nil {
|
|
|
|
want = os.EOF;
|
|
|
|
}
|
|
|
|
if err != want {
|
|
|
|
t.Errorf("read from closed pipe: %v want %v", err, want);
|
2009-06-06 22:51:05 -06:00
|
|
|
}
|
|
|
|
if n != 0 {
|
|
|
|
t.Errorf("read on closed pipe returned %d", n);
|
|
|
|
}
|
|
|
|
if err = r.Close(); err != nil {
|
|
|
|
t.Errorf("r.Close: %v", err);
|
|
|
|
}
|
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
// Test write after/before reader close.
|
2009-02-16 17:32:30 -07:00
|
|
|
|
2009-06-06 22:51:05 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|