Skip to content

Commit da7891f

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent f300fc2 commit da7891f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/net/dial_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,22 +429,23 @@ func TestDialParallelSpuriousConnection(t *testing.T) {
429429
readDeadline = time.Now().Add(5 * time.Second)
430430
}
431431

432-
var wg sync.WaitGroup
433-
wg.Add(2)
432+
var closed sync.WaitGroup
433+
closed.Add(2)
434434
handler := func(dss *dualStackServer, ln Listener) {
435435
// Accept one connection per address.
436436
c, err := ln.Accept()
437437
if err != nil {
438438
t.Fatal(err)
439439
}
440+
440441
// The client should close itself, without sending data.
441442
c.SetReadDeadline(readDeadline)
442443
var b [1]byte
443444
if _, err := c.Read(b[:]); err != io.EOF {
444445
t.Errorf("got %v; want %v", err, io.EOF)
445446
}
446447
c.Close()
447-
wg.Done()
448+
closed.Done()
448449
}
449450
dss, err := newDualStackServer()
450451
if err != nil {
@@ -457,12 +458,16 @@ func TestDialParallelSpuriousConnection(t *testing.T) {
457458

458459
const fallbackDelay = 100 * time.Millisecond
459460

461+
var dialing sync.WaitGroup
462+
dialing.Add(2)
460463
origTestHookDialTCP := testHookDialTCP
461464
defer func() { testHookDialTCP = origTestHookDialTCP }()
462465
testHookDialTCP = func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
463-
// Sleep long enough for Happy Eyeballs to kick in, and inhibit cancellation.
466+
// Wait until Happy Eyeballs kicks in and both connections are dialing,
467+
// and inhibit cancellation.
464468
// This forces dialParallel to juggle two successful connections.
465-
time.Sleep(fallbackDelay * 2)
469+
dialing.Done()
470+
dialing.Wait()
466471

467472
// Now ignore the provided context (which will be canceled) and use a
468473
// different one to make sure this completes with a valid connection,
@@ -496,7 +501,7 @@ func TestDialParallelSpuriousConnection(t *testing.T) {
496501
c.Close()
497502

498503
// The server should've seen both connections.
499-
wg.Wait()
504+
closed.Wait()
500505
}
501506

502507
func TestDialerPartialDeadline(t *testing.T) {

0 commit comments

Comments
 (0)