aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-01-05 11:13:27 -0500
committerBryan Mills <bcmills@google.com>2022-01-06 15:00:16 +0000
commitda7891f6f36c48f2931ed916ed305330c06f9bd7 (patch)
tree0eed6d1e2c78a1344e99e30053616618f5bcc604
parentf300fc2d2c620feac4e7f9b6cf0125b92943d3c4 (diff)
downloadgo-da7891f6f36c48f2931ed916ed305330c06f9bd7.tar.gz
go-da7891f6f36c48f2931ed916ed305330c06f9bd7.zip
net: synchronize instead of sleeping in TestDialParallelSpuriousConnection
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>
-rw-r--r--src/net/dial_test.go17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/net/dial_test.go b/src/net/dial_test.go
index e0c9cdc2ae..b9aead0371 100644
--- a/src/net/dial_test.go
+++ b/src/net/dial_test.go
@@ -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) {