|
5 | 5 | package net
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "context" |
8 | 9 | "errors"
|
9 | 10 | "fmt"
|
10 | 11 | "io"
|
@@ -193,17 +194,34 @@ func TestAcceptTimeout(t *testing.T) {
|
193 | 194 | // incoming connections available. Try to make one available before the
|
194 | 195 | // call to Accept happens. (It's ok if the timing doesn't always work
|
195 | 196 | // out that way, though: the test should pass regardless.)
|
| 197 | + ctx, cancel := context.WithCancel(context.Background()) |
196 | 198 | 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 | + }() |
198 | 208 |
|
199 | 209 | go func() {
|
200 | 210 | defer close(dialDone)
|
201 | 211 | 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()) |
203 | 213 | 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) |
205 | 222 | return
|
206 | 223 | }
|
| 224 | + t.Logf("Dialed %v -> %v", c.LocalAddr(), c.RemoteAddr()) |
207 | 225 | c.Close()
|
208 | 226 | }()
|
209 | 227 |
|
|
0 commit comments