gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/test/bufconn/bufconn_test.go (about)

     1  /*
     2   *
     3   * Copyright 2017 gRPC authors.
     4   *
     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
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    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.
    16   *
    17   */
    18  
    19  package bufconn
    20  
    21  import (
    22  	"fmt"
    23  	"io"
    24  	"net"
    25  	"reflect"
    26  	"testing"
    27  	"time"
    28  
    29  	"gitee.com/ks-custle/core-gm/grpc/internal/grpctest"
    30  )
    31  
    32  type s struct {
    33  	grpctest.Tester
    34  }
    35  
    36  func Test(t *testing.T) {
    37  	grpctest.RunSubTests(t, s{})
    38  }
    39  
    40  func testRW(r io.Reader, w io.Writer) error {
    41  	for i := 0; i < 20; i++ {
    42  		d := make([]byte, i)
    43  		for j := 0; j < i; j++ {
    44  			d[j] = byte(i - j)
    45  		}
    46  		var rn int
    47  		var rerr error
    48  		b := make([]byte, i)
    49  		done := make(chan struct{})
    50  		go func() {
    51  			for rn < len(b) && rerr == nil {
    52  				var x int
    53  				x, rerr = r.Read(b[rn:])
    54  				rn += x
    55  			}
    56  			close(done)
    57  		}()
    58  		wn, werr := w.Write(d)
    59  		if wn != i || werr != nil {
    60  			return fmt.Errorf("%v: w.Write(%v) = %v, %v; want %v, nil", i, d, wn, werr, i)
    61  		}
    62  		select {
    63  		case <-done:
    64  		case <-time.After(500 * time.Millisecond):
    65  			return fmt.Errorf("%v: r.Read never returned", i)
    66  		}
    67  		if rn != i || rerr != nil {
    68  			return fmt.Errorf("%v: r.Read = %v, %v; want %v, nil", i, rn, rerr, i)
    69  		}
    70  		if !reflect.DeepEqual(b, d) {
    71  			return fmt.Errorf("%v: r.Read read %v; want %v", i, b, d)
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func (s) TestPipe(t *testing.T) {
    78  	p := newPipe(10)
    79  	if err := testRW(p, p); err != nil {
    80  		t.Fatalf(err.Error())
    81  	}
    82  }
    83  
    84  func (s) TestPipeClose(t *testing.T) {
    85  	p := newPipe(10)
    86  	p.Close()
    87  	if _, err := p.Write(nil); err != io.ErrClosedPipe {
    88  		t.Fatalf("p.Write = _, %v; want _, %v", err, io.ErrClosedPipe)
    89  	}
    90  	if _, err := p.Read(nil); err != io.ErrClosedPipe {
    91  		t.Fatalf("p.Read = _, %v; want _, %v", err, io.ErrClosedPipe)
    92  	}
    93  }
    94  
    95  func (s) TestConn(t *testing.T) {
    96  	p1, p2 := newPipe(10), newPipe(10)
    97  	c1, c2 := &conn{p1, p2}, &conn{p2, p1}
    98  
    99  	if err := testRW(c1, c2); err != nil {
   100  		t.Fatalf(err.Error())
   101  	}
   102  	if err := testRW(c2, c1); err != nil {
   103  		t.Fatalf(err.Error())
   104  	}
   105  }
   106  
   107  func (s) TestConnCloseWithData(t *testing.T) {
   108  	lis := Listen(7)
   109  	errChan := make(chan error, 1)
   110  	var lisConn net.Conn
   111  	go func() {
   112  		var err error
   113  		if lisConn, err = lis.Accept(); err != nil {
   114  			errChan <- err
   115  		}
   116  		close(errChan)
   117  	}()
   118  	dialConn, err := lis.Dial()
   119  	if err != nil {
   120  		t.Fatalf("Dial error: %v", err)
   121  	}
   122  	if err := <-errChan; err != nil {
   123  		t.Fatalf("Listen error: %v", err)
   124  	}
   125  
   126  	// Write some data on both sides of the connection.
   127  	n, err := dialConn.Write([]byte("hello"))
   128  	if n != 5 || err != nil {
   129  		t.Fatalf("dialConn.Write([]byte{\"hello\"}) = %v, %v; want 5, <nil>", n, err)
   130  	}
   131  	n, err = lisConn.Write([]byte("hello"))
   132  	if n != 5 || err != nil {
   133  		t.Fatalf("lisConn.Write([]byte{\"hello\"}) = %v, %v; want 5, <nil>", n, err)
   134  	}
   135  
   136  	// Close dial-side; writes from either side should fail.
   137  	dialConn.Close()
   138  	if _, err := lisConn.Write([]byte("hello")); err != io.ErrClosedPipe {
   139  		t.Fatalf("lisConn.Write() = _, <nil>; want _, <non-nil>")
   140  	}
   141  	if _, err := dialConn.Write([]byte("hello")); err != io.ErrClosedPipe {
   142  		t.Fatalf("dialConn.Write() = _, <nil>; want _, <non-nil>")
   143  	}
   144  
   145  	// Read from both sides; reads on lisConn should work, but dialConn should
   146  	// fail.
   147  	buf := make([]byte, 6)
   148  	if _, err := dialConn.Read(buf); err != io.ErrClosedPipe {
   149  		t.Fatalf("dialConn.Read(buf) = %v, %v; want _, io.ErrClosedPipe", n, err)
   150  	}
   151  	n, err = lisConn.Read(buf)
   152  	if n != 5 || err != nil {
   153  		t.Fatalf("lisConn.Read(buf) = %v, %v; want 5, <nil>", n, err)
   154  	}
   155  }
   156  
   157  func (s) TestListener(t *testing.T) {
   158  	l := Listen(7)
   159  	var s net.Conn
   160  	var serr error
   161  	done := make(chan struct{})
   162  	go func() {
   163  		s, serr = l.Accept()
   164  		close(done)
   165  	}()
   166  	c, cerr := l.Dial()
   167  	<-done
   168  	if cerr != nil || serr != nil {
   169  		t.Fatalf("cerr = %v, serr = %v; want nil, nil", cerr, serr)
   170  	}
   171  	if err := testRW(c, s); err != nil {
   172  		t.Fatalf(err.Error())
   173  	}
   174  	if err := testRW(s, c); err != nil {
   175  		t.Fatalf(err.Error())
   176  	}
   177  }
   178  
   179  func (s) TestCloseWhileDialing(t *testing.T) {
   180  	l := Listen(7)
   181  	var c net.Conn
   182  	var err error
   183  	done := make(chan struct{})
   184  	go func() {
   185  		c, err = l.Dial()
   186  		close(done)
   187  	}()
   188  	l.Close()
   189  	<-done
   190  	if c != nil || err != errClosed {
   191  		t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)
   192  	}
   193  }
   194  
   195  func (s) TestCloseWhileAccepting(t *testing.T) {
   196  	l := Listen(7)
   197  	var c net.Conn
   198  	var err error
   199  	done := make(chan struct{})
   200  	go func() {
   201  		c, err = l.Accept()
   202  		close(done)
   203  	}()
   204  	l.Close()
   205  	<-done
   206  	if c != nil || err != errClosed {
   207  		t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)
   208  	}
   209  }
   210  
   211  func (s) TestDeadline(t *testing.T) {
   212  	sig := make(chan error, 2)
   213  	blockingWrite := func(conn net.Conn) {
   214  		_, err := conn.Write([]byte("0123456789"))
   215  		sig <- err
   216  	}
   217  
   218  	blockingRead := func(conn net.Conn) {
   219  		_, err := conn.Read(make([]byte, 10))
   220  		sig <- err
   221  	}
   222  
   223  	p1, p2 := newPipe(5), newPipe(5)
   224  	c1, c2 := &conn{p1, p1}, &conn{p2, p2}
   225  	defer c1.Close()
   226  	defer c2.Close()
   227  
   228  	// Test with deadline
   229  	c1.SetWriteDeadline(time.Now())
   230  
   231  	go blockingWrite(c1)
   232  	select {
   233  	case <-time.After(100 * time.Millisecond):
   234  		t.Fatalf("Write timeout timed out, c = %v", c1)
   235  	case err := <-sig:
   236  		if netErr, ok := err.(net.Error); ok {
   237  			if !netErr.Timeout() {
   238  				t.Fatalf("Write returned unexpected error, c = %v, err = %v", c1, netErr)
   239  			}
   240  		} else {
   241  			t.Fatalf("Write returned unexpected error, c = %v, err = %v", c1, err)
   242  		}
   243  	}
   244  
   245  	c2.SetReadDeadline(time.Now())
   246  
   247  	go blockingRead(c2)
   248  	select {
   249  	case <-time.After(100 * time.Millisecond):
   250  		t.Fatalf("Read timeout timed out, c = %v", c2)
   251  	case err := <-sig:
   252  		if netErr, ok := err.(net.Error); ok {
   253  			if !netErr.Timeout() {
   254  				t.Fatalf("Read returned unexpected error, c = %v, err = %v", c2, netErr)
   255  			}
   256  		} else {
   257  			t.Fatalf("Read returned unexpected error, c = %v, err = %v", c2, err)
   258  		}
   259  	}
   260  
   261  	// Test timing out pending reads/writes
   262  	c1.SetWriteDeadline(time.Time{})
   263  	c2.SetReadDeadline(time.Time{})
   264  
   265  	go blockingWrite(c1)
   266  	select {
   267  	case <-time.After(100 * time.Millisecond):
   268  	case err := <-sig:
   269  		t.Fatalf("Write returned before timeout, err = %v", err)
   270  	}
   271  
   272  	c1.SetWriteDeadline(time.Now())
   273  	select {
   274  	case <-time.After(100 * time.Millisecond):
   275  		t.Fatalf("Write timeout timed out, c = %v", c1)
   276  	case err := <-sig:
   277  		if netErr, ok := err.(net.Error); ok {
   278  			if !netErr.Timeout() {
   279  				t.Fatalf("Write returned unexpected error, c = %v, err = %v", c1, netErr)
   280  			}
   281  		} else {
   282  			t.Fatalf("Write returned unexpected error, c = %v, err = %v", c1, err)
   283  		}
   284  	}
   285  
   286  	go blockingRead(c2)
   287  	select {
   288  	case <-time.After(100 * time.Millisecond):
   289  	case err := <-sig:
   290  		t.Fatalf("Read returned before timeout, err = %v", err)
   291  	}
   292  
   293  	c2.SetReadDeadline(time.Now())
   294  	select {
   295  	case <-time.After(100 * time.Millisecond):
   296  		t.Fatalf("Read timeout timed out, c = %v", c2)
   297  	case err := <-sig:
   298  		if netErr, ok := err.(net.Error); ok {
   299  			if !netErr.Timeout() {
   300  				t.Fatalf("Read returned unexpected error, c = %v, err = %v", c2, netErr)
   301  			}
   302  		} else {
   303  			t.Fatalf("Read returned unexpected error, c = %v, err = %v", c2, err)
   304  		}
   305  	}
   306  
   307  	// Test non-blocking read/write
   308  	c1, c2 = &conn{p1, p2}, &conn{p2, p1}
   309  
   310  	c1.SetWriteDeadline(time.Now().Add(10 * time.Second))
   311  	c2.SetReadDeadline(time.Now().Add(10 * time.Second))
   312  
   313  	// Not blocking here
   314  	go blockingWrite(c1)
   315  	go blockingRead(c2)
   316  
   317  	// Read response from both routines
   318  	for i := 0; i < 2; i++ {
   319  		select {
   320  		case <-time.After(100 * time.Millisecond):
   321  			t.Fatalf("Read/Write timed out, c1 = %v, c2 = %v", c1, c2)
   322  		case err := <-sig:
   323  			if err != nil {
   324  				t.Fatalf("Read/Write failed to complete, c1 = %v, c2 = %v, err = %v", c1, c2, err)
   325  			}
   326  		}
   327  	}
   328  }