diff options
Diffstat (limited to 'src/pkg/net/dial_test.go')
-rw-r--r-- | src/pkg/net/dial_test.go | 56 |
1 files changed, 49 insertions, 7 deletions
diff --git a/src/pkg/net/dial_test.go b/src/pkg/net/dial_test.go index 16b726311..e5a797e13 100644 --- a/src/pkg/net/dial_test.go +++ b/src/pkg/net/dial_test.go @@ -27,8 +27,7 @@ func TestDialTimeout(t *testing.T) { errc := make(chan error) - const SOMAXCONN = 0x80 // copied from syscall, but not always available - const numConns = SOMAXCONN + 10 + numConns := listenerBacklog + 10 // TODO(bradfitz): It's hard to test this in a portable // way. This is unforunate, but works for now. @@ -47,16 +46,17 @@ func TestDialTimeout(t *testing.T) { // 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. go func() { _, err := DialTimeout("tcp", "127.0.71.111:80", 200*time.Millisecond) errc <- err }() default: - // TODO(bradfitz): this probably doesn't work on - // Windows? SOMAXCONN is huge there. I'm not sure how - // listen works there. - // OpenBSD may have a reject route to 10/8. - // FreeBSD likely works, but is untested. + // 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.Logf("skipping test on %q; untested.", runtime.GOOS) return } @@ -86,3 +86,45 @@ func TestDialTimeout(t *testing.T) { } } } + +func TestSelfConnect(t *testing.T) { + if runtime.GOOS == "windows" { + // TODO(brainman): do not know why it hangs. + t.Logf("skipping known-broken test on windows") + return + } + // 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", "freebsd", "openbsd", "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 := Dial("tcp", addr) + if err == nil { + c.Close() + t.Errorf("#%d: Dial %q succeeded", i, addr) + } + } +} |