From: Mikio Hara Date: Mon, 4 Apr 2016 09:48:06 +0000 (+0900) Subject: net: rename TestSelfConnect to TestTCPSelfConnect X-Git-Tag: go1.7beta1~914 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2ae749c13fa365fc14a639c5a7f74d2c12d98b31;p=gostls13.git net: rename TestSelfConnect to TestTCPSelfConnect Alos moves TestTCPSelfConnect into tcpsock_test.go Change-Id: I3e1cbd029594ecb36a67f42bc3ecdbc7176a95dc Reviewed-on: https://go-review.googlesource.com/21447 Run-TryBot: Mikio Hara TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/net/dial_test.go b/src/net/dial_test.go index 3335df5a93..04e0fdae44 100644 --- a/src/net/dial_test.go +++ b/src/net/dial_test.go @@ -54,52 +54,6 @@ func TestProhibitionaryDialArg(t *testing.T) { } } -func TestSelfConnect(t *testing.T) { - if runtime.GOOS == "windows" { - // TODO(brainman): do not know why it hangs. - t.Skip("known-broken test on windows") - } - - // Test that Dial does not honor self-connects. - // See the comment in DialTCP. - - // Find a port that would be used as a local address. - l, err := Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - c, err := Dial("tcp", l.Addr().String()) - if err != nil { - t.Fatal(err) - } - addr := c.LocalAddr().String() - c.Close() - l.Close() - - // Try to connect to that address repeatedly. - n := 100000 - if testing.Short() { - n = 1000 - } - switch runtime.GOOS { - case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd", "plan9", "solaris", "windows": - // Non-Linux systems take a long time to figure - // out that there is nothing listening on localhost. - n = 100 - } - for i := 0; i < n; i++ { - c, err := DialTimeout("tcp", addr, time.Millisecond) - if err == nil { - if c.LocalAddr().String() == addr { - t.Errorf("#%d: Dial %q self-connect", i, addr) - } else { - t.Logf("#%d: Dial %q succeeded - possibly racing with other listener", i, addr) - } - c.Close() - } - } -} - func TestDialTimeoutFDLeak(t *testing.T) { switch runtime.GOOS { case "plan9": diff --git a/src/net/tcpsock_test.go b/src/net/tcpsock_test.go index 30c5762592..8de6ad71ce 100644 --- a/src/net/tcpsock_test.go +++ b/src/net/tcpsock_test.go @@ -588,3 +588,50 @@ func TestTCPStress(t *testing.T) { ln.Close() <-done } + +func TestTCPSelfConnect(t *testing.T) { + if runtime.GOOS == "windows" { + // TODO(brainman): do not know why it hangs. + t.Skip("known-broken test on windows") + } + + ln, err := newLocalListener("tcp") + if err != nil { + t.Fatal(err) + } + var d Dialer + c, err := d.Dial(ln.Addr().Network(), ln.Addr().String()) + if err != nil { + ln.Close() + t.Fatal(err) + } + network := c.LocalAddr().Network() + laddr := *c.LocalAddr().(*TCPAddr) + c.Close() + ln.Close() + + // Try to connect to that address repeatedly. + n := 100000 + if testing.Short() { + n = 1000 + } + switch runtime.GOOS { + case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd", "plan9", "solaris", "windows": + // Non-Linux systems take a long time to figure + // out that there is nothing listening on localhost. + n = 100 + } + for i := 0; i < n; i++ { + d.Timeout = time.Millisecond + c, err := d.Dial(network, laddr.String()) + if err == nil { + addr := c.LocalAddr().(*TCPAddr) + if addr.Port == laddr.Port || addr.IP.Equal(laddr.IP) { + t.Errorf("Dial %v should fail", addr) + } else { + t.Logf("Dial %v succeeded - possibly racing with other listener", addr) + } + c.Close() + } + } +}