瀏覽代碼

netutil, internal/nettest: deflake TestLimitListener

Change-Id: Ic82974bcafa1723c96ece0b6b0b717b00b27774b
Reviewed-on: https://go-review.googlesource.com/11533
Reviewed-by: Andrew Gerrand <adg@golang.org>
Mikio Hara 10 年之前
父節點
當前提交
af03a19e5e

+ 11 - 0
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() }

+ 7 - 0
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 }

+ 17 - 0
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)
+}

+ 7 - 0
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 */ }

+ 9 - 9
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)
 	}
 }