aboutsummaryrefslogtreecommitdiff
path: root/src/net/dial_test.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2021-06-22 21:48:11 -0400
committerBryan C. Mills <bcmills@google.com>2021-06-24 03:45:33 +0000
commita9bb38222a06333176cafc5637083e0322277c09 (patch)
tree356485304a7f49a5a0fe2dcca70b84955fbc36e4 /src/net/dial_test.go
parent86d72fa2cba51342ba5617abf43a732f9fd668ca (diff)
downloadgo-a9bb38222a06333176cafc5637083e0322277c09.tar.gz
go-a9bb38222a06333176cafc5637083e0322277c09.zip
net: remove hard-coded timeout in dialClosedPort test helper
The helper function claims that dialing a closed port should be "nearly instantaneous", but that is empirically not the case on OpenBSD or Windows. The tests do not appear to be particularly sensitive to the exact upper bound otherwise, so let's just remove the arbitrary latency assumption. Fixes #46884 Change-Id: If00c9fdc3063da6aaf60d365d4a2ee2c94dc6df1 Reviewed-on: https://go-review.googlesource.com/c/go/+/330250 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/net/dial_test.go')
-rw-r--r--src/net/dial_test.go51
1 files changed, 16 insertions, 35 deletions
diff --git a/src/net/dial_test.go b/src/net/dial_test.go
index f899da10cf..723038c7a2 100644
--- a/src/net/dial_test.go
+++ b/src/net/dial_test.go
@@ -155,40 +155,27 @@ func slowDialTCP(ctx context.Context, network string, laddr, raddr *TCPAddr) (*T
return c, err
}
-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.
- if runtime.GOOS == "windows" {
- expected = 1500 * time.Millisecond
- } else if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
- expected = 150 * time.Millisecond
- } else {
- expected = 95 * time.Millisecond
- }
+func dialClosedPort(t *testing.T) (dialLatency time.Duration) {
+ // On most platforms, dialing a closed port should be nearly instantaneous —
+ // less than a few hundred milliseconds. However, on some platforms it may be
+ // much slower: on Windows and OpenBSD, it has been observed to take up to a
+ // few seconds.
l, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
- t.Logf("dialClosedPort: Listen failed: %v", err)
- return 999 * time.Hour, expected
+ t.Fatalf("dialClosedPort: Listen failed: %v", err)
}
addr := l.Addr().String()
l.Close()
- // On OpenBSD, interference from TestTCPSelfConnect is mysteriously
- // causing the first attempt to hang for a few seconds, so we throw
- // away the first result and keep the second.
- for i := 1; ; i++ {
- startTime := time.Now()
- c, err := Dial("tcp", addr)
- if err == nil {
- c.Close()
- }
- elapsed := time.Now().Sub(startTime)
- if i == 2 {
- t.Logf("dialClosedPort: measured delay %v", elapsed)
- return elapsed, expected
- }
+
+ startTime := time.Now()
+ c, err := Dial("tcp", addr)
+ if err == nil {
+ c.Close()
}
+ elapsed := time.Now().Sub(startTime)
+ t.Logf("dialClosedPort: measured delay %v", elapsed)
+ return elapsed
}
func TestDialParallel(t *testing.T) {
@@ -198,10 +185,7 @@ func TestDialParallel(t *testing.T) {
t.Skip("both IPv4 and IPv6 are required")
}
- closedPortDelay, expectClosedPortDelay := dialClosedPort(t)
- if closedPortDelay > expectClosedPortDelay {
- t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
- }
+ closedPortDelay := dialClosedPort(t)
const instant time.Duration = 0
const fallbackDelay = 200 * time.Millisecond
@@ -675,10 +659,7 @@ func TestDialerDualStack(t *testing.T) {
t.Skip("both IPv4 and IPv6 are required")
}
- closedPortDelay, expectClosedPortDelay := dialClosedPort(t)
- if closedPortDelay > expectClosedPortDelay {
- t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
- }
+ closedPortDelay := dialClosedPort(t)
origTestHookLookupIP := testHookLookupIP
defer func() { testHookLookupIP = origTestHookLookupIP }()