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