2016-05-13 06:48:53 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-11-24 11:39:55 +03:00
|
|
|
|
2014-11-24 08:12:02 +03:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadWriteTimeoutDialer(t *testing.T) {
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected listen error: %v", err)
|
|
|
|
}
|
|
|
|
ts := testBlockingServer{ln, 2, stop}
|
|
|
|
go ts.Start(t)
|
|
|
|
|
|
|
|
d := rwTimeoutDialer{
|
2014-11-24 08:25:09 +03:00
|
|
|
wtimeoutd: 10 * time.Millisecond,
|
|
|
|
rdtimeoutd: 10 * time.Millisecond,
|
2014-11-24 08:12:02 +03:00
|
|
|
}
|
|
|
|
conn, err := d.Dial("tcp", ln.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected dial error: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// fill the socket buffer
|
2014-12-13 22:32:29 +03:00
|
|
|
data := make([]byte, 5*1024*1024)
|
2015-10-01 03:17:37 +03:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
_, err = conn.Write(data)
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
2016-08-23 01:25:42 +03:00
|
|
|
// Wait 5s more than timeout to avoid delay in low-end systems;
|
|
|
|
// the slack was 1s extra, but that wasn't enough for CI.
|
|
|
|
case <-time.After(d.wtimeoutd*10 + 5*time.Second):
|
2014-11-24 08:12:02 +03:00
|
|
|
t.Fatal("wait timeout")
|
2015-10-01 03:17:37 +03:00
|
|
|
}
|
2014-11-24 08:12:02 +03:00
|
|
|
|
|
|
|
if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
|
|
|
|
t.Errorf("err = %v, want write i/o timeout error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err = d.Dial("tcp", ln.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected dial error: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
buf := make([]byte, 10)
|
2015-10-01 03:17:37 +03:00
|
|
|
go func() {
|
|
|
|
_, err = conn.Read(buf)
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
2015-10-19 02:32:18 +03:00
|
|
|
case <-time.After(d.rdtimeoutd * 10):
|
2015-10-01 03:17:37 +03:00
|
|
|
t.Fatal("wait timeout")
|
|
|
|
}
|
|
|
|
|
2014-11-24 08:12:02 +03:00
|
|
|
if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
|
|
|
|
t.Errorf("err = %v, want write i/o timeout error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stop <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testBlockingServer struct {
|
|
|
|
ln net.Listener
|
|
|
|
n int
|
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *testBlockingServer) Start(t *testing.T) {
|
|
|
|
for i := 0; i < ts.n; i++ {
|
|
|
|
conn, err := ts.ln.Accept()
|
|
|
|
if err != nil {
|
2019-02-01 22:17:58 +03:00
|
|
|
t.Error(err)
|
2014-11-24 08:12:02 +03:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
}
|
|
|
|
<-ts.stop
|
|
|
|
}
|