3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
30 func testRW(r io.Reader, w io.Writer) error {
31 for i := 0; i < 20; i++ {
33 for j := 0; j < i; j++ {
39 done := make(chan struct{})
41 for rn < len(b) && rerr == nil {
43 x, rerr = r.Read(b[rn:])
48 wn, werr := w.Write(d)
49 if wn != i || werr != nil {
50 return fmt.Errorf("%v: w.Write(%v) = %v, %v; want %v, nil", i, d, wn, werr, i)
54 case <-time.After(500 * time.Millisecond):
55 return fmt.Errorf("%v: r.Read never returned", i)
57 if rn != i || rerr != nil {
58 return fmt.Errorf("%v: r.Read = %v, %v; want %v, nil", i, rn, rerr, i)
60 if !reflect.DeepEqual(b, d) {
61 return fmt.Errorf("%v: r.Read read %v; want %v", i, b, d)
67 func TestPipe(t *testing.T) {
69 if err := testRW(p, p); err != nil {
74 func TestPipeClose(t *testing.T) {
77 if _, err := p.Write(nil); err != io.ErrClosedPipe {
78 t.Fatalf("p.Write = _, %v; want _, %v", err, io.ErrClosedPipe)
80 if _, err := p.Read(nil); err != io.ErrClosedPipe {
81 t.Fatalf("p.Read = _, %v; want _, %v", err, io.ErrClosedPipe)
85 func TestConn(t *testing.T) {
86 p1, p2 := newPipe(10), newPipe(10)
87 c1, c2 := &conn{p1, p2}, &conn{p2, p1}
89 if err := testRW(c1, c2); err != nil {
92 if err := testRW(c2, c1); err != nil {
97 func TestListener(t *testing.T) {
101 done := make(chan struct{})
108 if cerr != nil || serr != nil {
109 t.Fatalf("cerr = %v, serr = %v; want nil, nil", cerr, serr)
111 if err := testRW(c, s); err != nil {
112 t.Fatalf(err.Error())
114 if err := testRW(s, c); err != nil {
115 t.Fatalf(err.Error())
119 func TestCloseWhileDialing(t *testing.T) {
123 done := make(chan struct{})
130 if c != nil || err != errClosed {
131 t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)
135 func TestCloseWhileAccepting(t *testing.T) {
139 done := make(chan struct{})
146 if c != nil || err != errClosed {
147 t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)