From af03a19e5e18af95235c84e92e6670fdeb0b220a Mon Sep 17 00:00:00 2001 From: Mikio Hara Date: Fri, 26 Jun 2015 12:05:27 +0900 Subject: netutil, internal/nettest: deflake TestLimitListener Change-Id: Ic82974bcafa1723c96ece0b6b0b717b00b27774b Reviewed-on: https://go-review.googlesource.com/11533 Reviewed-by: Andrew Gerrand --- internal/nettest/rlimit.go | 11 +++++++++++ internal/nettest/rlimit_plan9.go | 7 +++++++ internal/nettest/rlimit_unix.go | 17 +++++++++++++++++ internal/nettest/rlimit_windows.go | 7 +++++++ netutil/listen_test.go | 18 +++++++++--------- 5 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 internal/nettest/rlimit.go create mode 100644 internal/nettest/rlimit_plan9.go create mode 100644 internal/nettest/rlimit_unix.go create mode 100644 internal/nettest/rlimit_windows.go diff --git a/internal/nettest/rlimit.go b/internal/nettest/rlimit.go new file mode 100644 index 0000000..bb34aec --- /dev/null +++ b/internal/nettest/rlimit.go @@ -0,0 +1,11 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package nettest + +const defaultMaxOpenFiles = 256 + +// MaxOpenFiles returns the maximum number of open files for the +// caller's process. +func MaxOpenFiles() int { return maxOpenFiles() } diff --git a/internal/nettest/rlimit_plan9.go b/internal/nettest/rlimit_plan9.go new file mode 100644 index 0000000..f745f8a --- /dev/null +++ b/internal/nettest/rlimit_plan9.go @@ -0,0 +1,7 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package nettest + +func maxOpenFiles() int { return defaultMaxOpenFiles } diff --git a/internal/nettest/rlimit_unix.go b/internal/nettest/rlimit_unix.go new file mode 100644 index 0000000..eb4312c --- /dev/null +++ b/internal/nettest/rlimit_unix.go @@ -0,0 +1,17 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build darwin dragonfly freebsd linux netbsd openbsd solaris + +package nettest + +import "syscall" + +func maxOpenFiles() int { + var rlim syscall.Rlimit + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil { + return defaultMaxOpenFiles + } + return int(rlim.Cur) +} diff --git a/internal/nettest/rlimit_windows.go b/internal/nettest/rlimit_windows.go new file mode 100644 index 0000000..de927b5 --- /dev/null +++ b/internal/nettest/rlimit_windows.go @@ -0,0 +1,7 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package nettest + +func maxOpenFiles() int { return 4 * defaultMaxOpenFiles /* actually it's 16581375 */ } diff --git a/netutil/listen_test.go b/netutil/listen_test.go index ac87e0e..7179907 100644 --- a/netutil/listen_test.go +++ b/netutil/listen_test.go @@ -20,17 +20,17 @@ import ( "sync/atomic" "testing" "time" + + "golang.org/x/net/internal/nettest" ) func TestLimitListener(t *testing.T) { - const ( - max = 5 - num = 200 - ) + const max = 5 + attempts := (nettest.MaxOpenFiles() - max) / 2 l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { - t.Fatalf("Listen: %v", err) + t.Fatal(err) } defer l.Close() l = LimitListener(l, max) @@ -47,14 +47,14 @@ func TestLimitListener(t *testing.T) { var wg sync.WaitGroup var failed int32 - for i := 0; i < num; i++ { + for i := 0; i < attempts; i++ { wg.Add(1) go func() { defer wg.Done() c := http.Client{Timeout: 3 * time.Second} r, err := c.Get("http://" + l.Addr().String()) if err != nil { - t.Logf("Get: %v", err) + t.Log(err) atomic.AddInt32(&failed, 1) return } @@ -66,8 +66,8 @@ func TestLimitListener(t *testing.T) { // We expect some Gets to fail as the kernel's accept queue is filled, // but most should succeed. - if failed >= num/2 { - t.Errorf("too many Gets failed: %v", failed) + if int(failed) >= attempts/2 { + t.Errorf("%d requests failed within %d attempts", failed, attempts) } } -- cgit v1.2.3