2013-02-11 Sebastian Huber <sebastian.huber@embedded-brains.de>
[official-gcc.git] / libgo / go / net / pipe_test.go
blobafe4f2408fadd97a6564c3143e2fa153c890b322
1 // Copyright 2010 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.
5 package net
7 import (
8 "bytes"
9 "io"
10 "testing"
13 func checkWrite(t *testing.T, w io.Writer, data []byte, c chan int) {
14 n, err := w.Write(data)
15 if err != nil {
16 t.Errorf("write: %v", err)
18 if n != len(data) {
19 t.Errorf("short write: %d != %d", n, len(data))
21 c <- 0
24 func checkRead(t *testing.T, r io.Reader, data []byte, wantErr error) {
25 buf := make([]byte, len(data)+10)
26 n, err := r.Read(buf)
27 if err != wantErr {
28 t.Errorf("read: %v", err)
29 return
31 if n != len(data) || !bytes.Equal(buf[0:n], data) {
32 t.Errorf("bad read: got %q", buf[0:n])
33 return
37 // Test a simple read/write/close sequence.
38 // Assumes that the underlying io.Pipe implementation
39 // is solid and we're just testing the net wrapping.
41 func TestPipe(t *testing.T) {
42 c := make(chan int)
43 cli, srv := Pipe()
44 go checkWrite(t, cli, []byte("hello, world"), c)
45 checkRead(t, srv, []byte("hello, world"), nil)
46 <-c
47 go checkWrite(t, srv, []byte("line 2"), c)
48 checkRead(t, cli, []byte("line 2"), nil)
49 <-c
50 go checkWrite(t, cli, []byte("a third line"), c)
51 checkRead(t, srv, []byte("a third line"), nil)
52 <-c
53 go srv.Close()
54 checkRead(t, cli, nil, io.EOF)
55 cli.Close()