...

Source file src/go.etcd.io/etcd/client/pkg/v3/transport/timeout_dialer_test.go

Documentation: go.etcd.io/etcd/client/pkg/v3/transport

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package transport
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestReadWriteTimeoutDialer(t *testing.T) {
    24  	stop := make(chan struct{})
    25  
    26  	ln, err := net.Listen("tcp", "127.0.0.1:0")
    27  	if err != nil {
    28  		t.Fatalf("unexpected listen error: %v", err)
    29  	}
    30  	defer func() {
    31  		stop <- struct{}{}
    32  	}()
    33  	ts := testBlockingServer{ln, 2, stop}
    34  	go ts.Start(t)
    35  
    36  	d := rwTimeoutDialer{
    37  		wtimeoutd:  10 * time.Millisecond,
    38  		rdtimeoutd: 10 * time.Millisecond,
    39  	}
    40  	conn, err := d.Dial("tcp", ln.Addr().String())
    41  	if err != nil {
    42  		t.Fatalf("unexpected dial error: %v", err)
    43  	}
    44  	defer conn.Close()
    45  
    46  	// fill the socket buffer
    47  	data := make([]byte, 5*1024*1024)
    48  	done := make(chan struct{}, 1)
    49  	go func() {
    50  		_, err = conn.Write(data)
    51  		done <- struct{}{}
    52  	}()
    53  
    54  	select {
    55  	case <-done:
    56  	// Wait 5s more than timeout to avoid delay in low-end systems;
    57  	// the slack was 1s extra, but that wasn't enough for CI.
    58  	case <-time.After(d.wtimeoutd*10 + 5*time.Second):
    59  		t.Fatal("wait timeout")
    60  	}
    61  
    62  	if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
    63  		t.Errorf("err = %v, want write i/o timeout error", err)
    64  	}
    65  
    66  	conn, err = d.Dial("tcp", ln.Addr().String())
    67  	if err != nil {
    68  		t.Fatalf("unexpected dial error: %v", err)
    69  	}
    70  	defer conn.Close()
    71  
    72  	buf := make([]byte, 10)
    73  	go func() {
    74  		_, err = conn.Read(buf)
    75  		done <- struct{}{}
    76  	}()
    77  
    78  	select {
    79  	case <-done:
    80  	case <-time.After(d.rdtimeoutd * 10):
    81  		t.Fatal("wait timeout")
    82  	}
    83  
    84  	if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
    85  		t.Errorf("err = %v, want write i/o timeout error", err)
    86  	}
    87  }
    88  
    89  type testBlockingServer struct {
    90  	ln   net.Listener
    91  	n    int
    92  	stop chan struct{}
    93  }
    94  
    95  func (ts *testBlockingServer) Start(t *testing.T) {
    96  	for i := 0; i < ts.n; i++ {
    97  		conn, err := ts.ln.Accept()
    98  		if err != nil {
    99  			t.Error(err)
   100  		}
   101  		defer conn.Close()
   102  	}
   103  	<-ts.stop
   104  }
   105  

View as plain text