go.etcd.io/etcd@v3.3.27+incompatible/pkg/transport/timeout_dialer_test.go (about) 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 ts := testBlockingServer{ln, 2, stop} 31 go ts.Start(t) 32 33 d := rwTimeoutDialer{ 34 wtimeoutd: 10 * time.Millisecond, 35 rdtimeoutd: 10 * time.Millisecond, 36 } 37 conn, err := d.Dial("tcp", ln.Addr().String()) 38 if err != nil { 39 t.Fatalf("unexpected dial error: %v", err) 40 } 41 defer conn.Close() 42 43 // fill the socket buffer 44 data := make([]byte, 5*1024*1024) 45 done := make(chan struct{}) 46 go func() { 47 _, err = conn.Write(data) 48 done <- struct{}{} 49 }() 50 51 select { 52 case <-done: 53 // Wait 5s more than timeout to avoid delay in low-end systems; 54 // the slack was 1s extra, but that wasn't enough for CI. 55 case <-time.After(d.wtimeoutd*10 + 5*time.Second): 56 t.Fatal("wait timeout") 57 } 58 59 if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() { 60 t.Errorf("err = %v, want write i/o timeout error", err) 61 } 62 63 conn, err = d.Dial("tcp", ln.Addr().String()) 64 if err != nil { 65 t.Fatalf("unexpected dial error: %v", err) 66 } 67 defer conn.Close() 68 69 buf := make([]byte, 10) 70 go func() { 71 _, err = conn.Read(buf) 72 done <- struct{}{} 73 }() 74 75 select { 76 case <-done: 77 case <-time.After(d.rdtimeoutd * 10): 78 t.Fatal("wait timeout") 79 } 80 81 if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() { 82 t.Errorf("err = %v, want write i/o timeout error", err) 83 } 84 85 stop <- struct{}{} 86 } 87 88 type testBlockingServer struct { 89 ln net.Listener 90 n int 91 stop chan struct{} 92 } 93 94 func (ts *testBlockingServer) Start(t *testing.T) { 95 for i := 0; i < ts.n; i++ { 96 conn, err := ts.ln.Accept() 97 if err != nil { 98 t.Fatal(err) 99 } 100 defer conn.Close() 101 } 102 <-ts.stop 103 }