Skip to content

Commit 80cf0d3

Browse files
TMHSDigitalclaude
andcommitted
Make the DoT closed-connection test deterministic (#289)
TestDoTDiscardsClosedConnections slept 5 ms after each lookup and hoped the server's close had reached the client before the next lookup's one-millisecond alive probe; on a loaded machine it sometimes had not, and a query went out on the dead connection (11 counted, want 10). A 200-run stress loop still failed once with only the server side waited for. The test now takes each pooled DoT connection, reads until it sees the server's close, and puts it back, so the next lookup's probe always finds it closed and the discard path is what gets tested. 500 runs in a row pass. Refs #289 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 09b2c04 commit 80cf0d3

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

‎internal/dns/transport_test.go‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"crypto/x509"
66
"errors"
7+
"net"
78
"net/http"
89
"net/http/httptest"
910
"slices"
1011
"sync/atomic"
1112
"testing"
1213
"time"
14+
"weak"
1315

1416
"golang.org/x/net/dns/dnsmessage"
1517

@@ -216,7 +218,7 @@ func TestDoTDiscardsClosedConnections(t *testing.T) {
216218
if _, _, err := (Lookup{Resolver: r, Timeout: timeout, Types: []string{"A"}}).ResolveTypes(ctx, "www.example.com"); err != nil {
217219
t.Fatal(err)
218220
}
219-
time.Sleep(5 * time.Millisecond) // let the server's close arrive
221+
waitPooledClosed(t, r, timeout)
220222
}
221223
if opened := srv.Connections() - before; opened != 10 {
222224
t.Errorf("10 lookups opened %d connections, want 10", opened)
@@ -226,6 +228,41 @@ func TestDoTDiscardsClosedConnections(t *testing.T) {
226228
}
227229
}
228230

231+
// waitPooledClosed waits until each DoT connection r keeps for reuse has
232+
// read the server's close, then puts it back in the pool. The next lookup's
233+
// alive probe then finds it closed, instead of racing the close with its
234+
// one-millisecond read, which a loaded machine can lose (#289).
235+
func waitPooledClosed(t *testing.T, r *net.Resolver, timeout time.Duration) {
236+
t.Helper()
237+
v, ok := pooledDialers.Load(weak.Make(r))
238+
if !ok {
239+
t.Fatal("the resolver has no pooled dialer")
240+
}
241+
d := v.(*dotDialer)
242+
var seen []idleConn
243+
drain:
244+
for {
245+
select {
246+
case ic := <-d.idle:
247+
if err := ic.conn.SetReadDeadline(time.Now().Add(timeout)); err != nil {
248+
t.Fatal(err)
249+
}
250+
var b [1]byte
251+
n, err := ic.conn.Read(b[:])
252+
var ne net.Error
253+
if n > 0 || err == nil || (errors.As(err, &ne) && ne.Timeout()) {
254+
t.Fatalf("pooled connection: read %d byte(s), %v; want the server's close", n, err)
255+
}
256+
seen = append(seen, ic)
257+
default:
258+
break drain
259+
}
260+
}
261+
for _, ic := range seen {
262+
d.idle <- ic
263+
}
264+
}
265+
229266
// TestCloseIdleConnections: a scan's DoT connections are closed when it
230267
// ends, and the resolver still works afterwards.
231268
func TestCloseIdleConnections(t *testing.T) {

0 commit comments

Comments
 (0)