]> Cypherpunks repositories - gostls13.git/commitdiff
net: deflake TestDialTimeout{,FDLeak} in the case of TCP simultaneous open
authorMikio Hara <mikioh.mikioh@gmail.com>
Mon, 27 Jul 2015 03:19:00 +0000 (12:19 +0900)
committerRuss Cox <rsc@golang.org>
Mon, 27 Jul 2015 16:09:26 +0000 (16:09 +0000)
Fixes #11872.

Change-Id: Ibc7d8438374c9d90fd4cbefb61426c7f4f96af0d
Reviewed-on: https://go-review.googlesource.com/12691
Reviewed-by: Russ Cox <rsc@golang.org>
src/net/dial_test.go
src/net/timeout_test.go

index cfd7e092e46b30c4f8de345fd7e8d355c5b34029..aa916d4dcb41c9cd127a3c65712763189e768039 100644 (file)
@@ -124,7 +124,24 @@ func TestDialTimeoutFDLeak(t *testing.T) {
                defer sw.Set(socktest.FilterConnect, nil)
        }
 
-       before := sw.Sockets()
+       // Avoid tracking open-close jitterbugs between netFD and
+       // socket that leads to confusion of information inside
+       // socktest.Switch.
+       // It may happen when the Dial call bumps against TCP
+       // simultaneous open. See selfConnect in tcpsock_posix.go.
+       defer func() {
+               sw.Set(socktest.FilterClose, nil)
+               forceCloseSockets()
+       }()
+       var mu sync.Mutex
+       var attempts int
+       sw.Set(socktest.FilterClose, func(so *socktest.Status) (socktest.AfterFilter, error) {
+               mu.Lock()
+               attempts++
+               mu.Unlock()
+               return nil, errTimedout
+       })
+
        const N = 100
        var wg sync.WaitGroup
        wg.Add(N)
@@ -142,9 +159,8 @@ func TestDialTimeoutFDLeak(t *testing.T) {
                }()
        }
        wg.Wait()
-       after := sw.Sockets()
-       if len(after) != len(before) {
-               t.Errorf("got %d; want %d", len(after), len(before))
+       if attempts < N {
+               t.Errorf("got %d; want >= %d", attempts, N)
        }
 }
 
index 9688c21699ec6bccf37752d5ed8e475ddb5d3d1e..ca94e24c81685cfc3e67655a544efea12a0a1e01 100644 (file)
@@ -37,6 +37,19 @@ func TestDialTimeout(t *testing.T) {
        defer func() { testHookDialChannel = origTestHookDialChannel }()
        defer sw.Set(socktest.FilterConnect, nil)
 
+       // Avoid tracking open-close jitterbugs between netFD and
+       // socket that leads to confusion of information inside
+       // socktest.Switch.
+       // It may happen when the Dial call bumps against TCP
+       // simultaneous open. See selfConnect in tcpsock_posix.go.
+       defer func() {
+               sw.Set(socktest.FilterClose, nil)
+               forceCloseSockets()
+       }()
+       sw.Set(socktest.FilterClose, func(so *socktest.Status) (socktest.AfterFilter, error) {
+               return nil, errTimedout
+       })
+
        for i, tt := range dialTimeoutTests {
                switch runtime.GOOS {
                case "plan9", "windows":