]> Cypherpunks repositories - gostls13.git/commitdiff
net: synchronize instead of sleeping in TestDialParallelSpuriousConnection
authorBryan C. Mills <bcmills@google.com>
Wed, 5 Jan 2022 16:13:27 +0000 (11:13 -0500)
committerBryan Mills <bcmills@google.com>
Thu, 6 Jan 2022 15:00:16 +0000 (15:00 +0000)
The arbitrary sleep in this test is empirically not always long enough
on slower builders. However, we know the exact number of connections
that should be dialed: we can wait on that number in the dial hook
instead.

Fixes #34495

Change-Id: I538244ceb75a80271a724304b993309482bd5b41
Reviewed-on: https://go-review.googlesource.com/c/go/+/375694
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/net/dial_test.go

index e0c9cdc2aec425a9cc10c56a69d3b3644402e881..b9aead03719b7d0ea4be4c121604b424c15f6ade 100644 (file)
@@ -429,14 +429,15 @@ func TestDialParallelSpuriousConnection(t *testing.T) {
                readDeadline = time.Now().Add(5 * time.Second)
        }
 
-       var wg sync.WaitGroup
-       wg.Add(2)
+       var closed sync.WaitGroup
+       closed.Add(2)
        handler := func(dss *dualStackServer, ln Listener) {
                // Accept one connection per address.
                c, err := ln.Accept()
                if err != nil {
                        t.Fatal(err)
                }
+
                // The client should close itself, without sending data.
                c.SetReadDeadline(readDeadline)
                var b [1]byte
@@ -444,7 +445,7 @@ func TestDialParallelSpuriousConnection(t *testing.T) {
                        t.Errorf("got %v; want %v", err, io.EOF)
                }
                c.Close()
-               wg.Done()
+               closed.Done()
        }
        dss, err := newDualStackServer()
        if err != nil {
@@ -457,12 +458,16 @@ func TestDialParallelSpuriousConnection(t *testing.T) {
 
        const fallbackDelay = 100 * time.Millisecond
 
+       var dialing sync.WaitGroup
+       dialing.Add(2)
        origTestHookDialTCP := testHookDialTCP
        defer func() { testHookDialTCP = origTestHookDialTCP }()
        testHookDialTCP = func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
-               // Sleep long enough for Happy Eyeballs to kick in, and inhibit cancellation.
+               // Wait until Happy Eyeballs kicks in and both connections are dialing,
+               // and inhibit cancellation.
                // This forces dialParallel to juggle two successful connections.
-               time.Sleep(fallbackDelay * 2)
+               dialing.Done()
+               dialing.Wait()
 
                // Now ignore the provided context (which will be canceled) and use a
                // different one to make sure this completes with a valid connection,
@@ -496,7 +501,7 @@ func TestDialParallelSpuriousConnection(t *testing.T) {
        c.Close()
 
        // The server should've seen both connections.
-       wg.Wait()
+       closed.Wait()
 }
 
 func TestDialerPartialDeadline(t *testing.T) {