Skip to content

Commit

Permalink
[release-branch.go1.13] net: add more timing slop for TestDialParalle…
Browse files Browse the repository at this point in the history
…l on Windows

For #35616.
Fixes #39538.
For #29252.

Change-Id: I51b2490100cfe0e902da09eee8d027e0ec86ed53
Reviewed-on: https://go-review.googlesource.com/c/go/+/207466
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
(cherry picked from commit c20b71e)
Reviewed-on: https://go-review.googlesource.com/c/go/+/237602
Run-TryBot: Dmitri Shuralyov <[email protected]>
Reviewed-by: Alexander Rakoczy <[email protected]>
  • Loading branch information
ianlancetaylor authored and dmitshur committed Jun 12, 2020
1 parent 034ed80 commit 7e87a63
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/net/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func slowDialTCP(ctx context.Context, network string, laddr, raddr *TCPAddr) (*T
return c, err
}

func dialClosedPort() (actual, expected time.Duration) {
func dialClosedPort(t *testing.T) (actual, expected time.Duration) {
// Estimate the expected time for this platform.
// On Windows, dialing a closed port takes roughly 1 second,
// but other platforms should be instantaneous.
Expand All @@ -168,6 +168,7 @@ func dialClosedPort() (actual, expected time.Duration) {

l, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Logf("dialClosedPort: Listen failed: %v", err)
return 999 * time.Hour, expected
}
addr := l.Addr().String()
Expand All @@ -183,6 +184,7 @@ func dialClosedPort() (actual, expected time.Duration) {
}
elapsed := time.Now().Sub(startTime)
if i == 2 {
t.Logf("dialClosedPort: measured delay %v", elapsed)
return elapsed, expected
}
}
Expand All @@ -195,7 +197,7 @@ func TestDialParallel(t *testing.T) {
t.Skip("both IPv4 and IPv6 are required")
}

closedPortDelay, expectClosedPortDelay := dialClosedPort()
closedPortDelay, expectClosedPortDelay := dialClosedPort(t)
if closedPortDelay > expectClosedPortDelay {
t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
}
Expand Down Expand Up @@ -316,8 +318,14 @@ func TestDialParallel(t *testing.T) {
t.Errorf("#%d: got nil; want non-nil", i)
}

expectElapsedMin := tt.expectElapsed - 95*time.Millisecond
expectElapsedMax := tt.expectElapsed + 95*time.Millisecond
// We used to always use 95 milliseconds as the slop,
// but that was flaky on Windows. See issue 35616.
slop := 95 * time.Millisecond
if fifth := tt.expectElapsed / 5; fifth > slop {
slop = fifth
}
expectElapsedMin := tt.expectElapsed - slop
expectElapsedMax := tt.expectElapsed + slop
if elapsed < expectElapsedMin {
t.Errorf("#%d: got %v; want >= %v", i, elapsed, expectElapsedMin)
} else if elapsed > expectElapsedMax {
Expand Down Expand Up @@ -664,7 +672,7 @@ func TestDialerDualStack(t *testing.T) {
t.Skip("both IPv4 and IPv6 are required")
}

closedPortDelay, expectClosedPortDelay := dialClosedPort()
closedPortDelay, expectClosedPortDelay := dialClosedPort(t)
if closedPortDelay > expectClosedPortDelay {
t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
}
Expand Down

0 comments on commit 7e87a63

Please sign in to comment.