Skip to content

Commit e954eb8

Browse files
Bryan C. Millsezz-no
Bryan C. Mills
authored andcommitted
net: ignore Dial errors in TestAcceptTimeout
Also use DialContext instead of just Dial so that we can ensure the call returns before we close the listener. The Dial in this test is intended to complete before the call to Accept, but there is no synchronization to ensure that and sometimes it doesn't happen. That's ok and mostly immaterial to the test, but it does mean we need to ignore Dial errors (which can happen when the listener is closed), and we need to be a little more careful about not dialing a port that may have already been reused by some other test. Fixes golang#65240. Updates golang#17948. Change-Id: Ife1b5c3062939441b58f4c096461bf5d7841889b Reviewed-on: https://go-review.googlesource.com/c/go/+/558175 Reviewed-by: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 9ede443 commit e954eb8

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/net/timeout_test.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package net
66

77
import (
8+
"context"
89
"errors"
910
"fmt"
1011
"io"
@@ -193,17 +194,34 @@ func TestAcceptTimeout(t *testing.T) {
193194
// incoming connections available. Try to make one available before the
194195
// call to Accept happens. (It's ok if the timing doesn't always work
195196
// out that way, though: the test should pass regardless.)
197+
ctx, cancel := context.WithCancel(context.Background())
196198
dialDone := make(chan struct{})
197-
t.Cleanup(func() { <-dialDone })
199+
200+
// Ensure that our background Dial returns before we close the listener.
201+
// Otherwise, the listener's port could be reused immediately and we
202+
// might spuriously Dial some completely unrelated socket, causing some
203+
// other test to see an unexpected extra connection.
204+
defer func() {
205+
cancel()
206+
<-dialDone
207+
}()
198208

199209
go func() {
200210
defer close(dialDone)
201211
d := Dialer{}
202-
c, err := d.Dial(ln.Addr().Network(), ln.Addr().String())
212+
c, err := d.DialContext(ctx, ln.Addr().Network(), ln.Addr().String())
203213
if err != nil {
204-
t.Error(err)
214+
// If the timing didn't work out, it is possible for this Dial
215+
// to return an error (depending on the kernel's buffering behavior).
216+
// In https://go.dev/issue/65240 we saw failures with ECONNREFUSED
217+
// and ECONNRESET.
218+
//
219+
// What this test really cares about is the behavior of Accept, not
220+
// Dial, so just log the error and ignore it.
221+
t.Logf("DialContext: %v", err)
205222
return
206223
}
224+
t.Logf("Dialed %v -> %v", c.LocalAddr(), c.RemoteAddr())
207225
c.Close()
208226
}()
209227

0 commit comments

Comments
 (0)