netutil, internal/nettest: deflake TestLimitListener

Change-Id: Ic82974bcafa1723c96ece0b6b0b717b00b27774b
Reviewed-on: https://go-review.googlesource.com/11533
Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
Mikio Hara
2015-06-26 12:05:27 +09:00
parent 669b27b881
commit af03a19e5e
5 changed files with 51 additions and 9 deletions

View File

@@ -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() }

View File

@@ -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 }

View File

@@ -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)
}

View File

@@ -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 */ }

View File

@@ -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)
}
}