Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | package net |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "io" |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 10 | "testing" |
| 11 | ) |
| 12 | |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 13 | func checkPipeWrite(t *testing.T, w io.Writer, data []byte, c chan int) { |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 14 | n, err := w.Write(data) |
| 15 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 16 | t.Error(err) |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 17 | } |
| 18 | if n != len(data) { |
| 19 | t.Errorf("short write: %d != %d", n, len(data)) |
| 20 | } |
| 21 | c <- 0 |
| 22 | } |
| 23 | |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 24 | func checkPipeRead(t *testing.T, r io.Reader, data []byte, wantErr error) { |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 25 | buf := make([]byte, len(data)+10) |
| 26 | n, err := r.Read(buf) |
| 27 | if err != wantErr { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 28 | t.Error(err) |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 29 | return |
| 30 | } |
| 31 | if n != len(data) || !bytes.Equal(buf[0:n], data) { |
| 32 | t.Errorf("bad read: got %q", buf[0:n]) |
| 33 | return |
| 34 | } |
| 35 | } |
| 36 | |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 37 | // TestPipe tests a simple read/write/close sequence. |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 38 | // Assumes that the underlying io.Pipe implementation |
| 39 | // is solid and we're just testing the net wrapping. |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 40 | func TestPipe(t *testing.T) { |
| 41 | c := make(chan int) |
| 42 | cli, srv := Pipe() |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 43 | go checkPipeWrite(t, cli, []byte("hello, world"), c) |
| 44 | checkPipeRead(t, srv, []byte("hello, world"), nil) |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 45 | <-c |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 46 | go checkPipeWrite(t, srv, []byte("line 2"), c) |
| 47 | checkPipeRead(t, cli, []byte("line 2"), nil) |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 48 | <-c |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 49 | go checkPipeWrite(t, cli, []byte("a third line"), c) |
| 50 | checkPipeRead(t, srv, []byte("a third line"), nil) |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 51 | <-c |
| 52 | go srv.Close() |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 53 | checkPipeRead(t, cli, nil, io.EOF) |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 54 | cli.Close() |
| 55 | } |