netutil, internal/nettest: deflake TestLimitListener
Change-Id: Ic82974bcafa1723c96ece0b6b0b717b00b27774b
Reviewed-on: https://go-review.googlesource.com/11533
Reviewed-by: Andrew Gerrand <adg@golang.org>
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 */ }