]> Cypherpunks repositories - gostls13.git/commitdiff
net: rename TestSelfConnect to TestTCPSelfConnect
authorMikio Hara <mikioh.mikioh@gmail.com>
Mon, 4 Apr 2016 09:48:06 +0000 (18:48 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Mon, 4 Apr 2016 22:21:26 +0000 (22:21 +0000)
Alos moves TestTCPSelfConnect into tcpsock_test.go

Change-Id: I3e1cbd029594ecb36a67f42bc3ecdbc7176a95dc
Reviewed-on: https://go-review.googlesource.com/21447
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/dial_test.go
src/net/tcpsock_test.go

index 3335df5a93c74595a2e3f9c7ddb6932206f08b36..04e0fdae4498ec04d0dbc024bbe4cb8b0acfe674 100644 (file)
@@ -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":
index 30c5762592e895096916814c1e3456db14bcca9a..8de6ad71ce6cdf4974aefc9a32ffce268fccf884 100644 (file)
@@ -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()
+               }
+       }
+}