mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net: deflake TestDialTimeout
This change makes TestDialTimeout work on almost all the supported platforms. Updates #3016. Updates #3307. Updates #3867. Updates #5380. Updates #5349. Change-Id: Iacf0ebea23cdd8f6c0333d70c667a5a5f5eb0ed2 Reviewed-on: https://go-review.googlesource.com/8220 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
fb4b46738c
commit
8b0e38ffb4
7 changed files with 67 additions and 103 deletions
|
|
@ -31,89 +31,6 @@ func newLocalListener(t *testing.T) Listener {
|
|||
return ln
|
||||
}
|
||||
|
||||
func TestDialTimeout(t *testing.T) {
|
||||
origBacklog := listenerBacklog
|
||||
defer func() {
|
||||
listenerBacklog = origBacklog
|
||||
}()
|
||||
listenerBacklog = 1
|
||||
|
||||
ln := newLocalListener(t)
|
||||
defer ln.Close()
|
||||
|
||||
errc := make(chan error)
|
||||
|
||||
numConns := listenerBacklog + 100
|
||||
|
||||
// TODO(bradfitz): It's hard to test this in a portable
|
||||
// way. This is unfortunate, but works for now.
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
// The kernel will start accepting TCP connections before userspace
|
||||
// gets a chance to not accept them, so fire off a bunch to fill up
|
||||
// the kernel's backlog. Then we test we get a failure after that.
|
||||
for i := 0; i < numConns; i++ {
|
||||
go func() {
|
||||
_, err := DialTimeout("tcp", ln.Addr().String(), 200*time.Millisecond)
|
||||
errc <- err
|
||||
}()
|
||||
}
|
||||
case "darwin", "plan9", "windows":
|
||||
// At least OS X 10.7 seems to accept any number of
|
||||
// connections, ignoring listen's backlog, so resort
|
||||
// to connecting to a hopefully-dead 127/8 address.
|
||||
// Same for windows.
|
||||
//
|
||||
// Use an IANA reserved port (49151) instead of 80, because
|
||||
// on our 386 builder, this Dial succeeds, connecting
|
||||
// to an IIS web server somewhere. The data center
|
||||
// or VM or firewall must be stealing the TCP connection.
|
||||
//
|
||||
// IANA Service Name and Transport Protocol Port Number Registry
|
||||
// <http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml>
|
||||
go func() {
|
||||
c, err := DialTimeout("tcp", "127.0.71.111:49151", 200*time.Millisecond)
|
||||
if err == nil {
|
||||
err = fmt.Errorf("unexpected: connected to %s!", c.RemoteAddr())
|
||||
c.Close()
|
||||
}
|
||||
errc <- err
|
||||
}()
|
||||
default:
|
||||
// TODO(bradfitz):
|
||||
// OpenBSD may have a reject route to 127/8 except 127.0.0.1/32
|
||||
// by default. FreeBSD likely works, but is untested.
|
||||
// TODO(rsc):
|
||||
// The timeout never happens on Windows. Why? Issue 3016.
|
||||
t.Skipf("skipping test on %q; untested.", runtime.GOOS)
|
||||
}
|
||||
|
||||
connected := 0
|
||||
for {
|
||||
select {
|
||||
case <-time.After(15 * time.Second):
|
||||
t.Fatal("too slow")
|
||||
case err := <-errc:
|
||||
if err == nil {
|
||||
connected++
|
||||
if connected == numConns {
|
||||
t.Fatal("all connections connected; expected some to time out")
|
||||
}
|
||||
} else {
|
||||
terr, ok := err.(timeout)
|
||||
if !ok {
|
||||
t.Fatalf("got error %q; want error with timeout interface", err)
|
||||
}
|
||||
if !terr.Timeout() {
|
||||
t.Fatalf("got error %q; not a timeout", err)
|
||||
}
|
||||
// Pass. We saw a timeout error.
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelfConnect(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// TODO(brainman): do not know why it hangs.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue