test: weedtest - the held-port test holds a port no other process uses (#1058)

Merge note (pull_request_target spec-trace workflow has a startup_failure on every branch since 2026-12-02 22:45Z, so its checks run locally):
- Test run 47397942135 succeeded (go test (linux), process-level tests (real binaries)) on head ee67ab95 against main 8838680f, the current main head.
- spec-trace-check.sh and public-text-check.sh, run with the main branch's scripts against this head and body: both exit 0.
This commit is contained in:
pingqiu-cpu authored and GitHub committed 2026-12-04 10:56:55 -08:00
1 parent 9ef6bef47d
commit d58e7fdd62
2 files changed
+96 -10

No files matched your search

+17 -5
View File
@@ -1,8 +1,8 @@
package weedtest
import (
"fmt"
"net"
"strconv"
"testing"
)
@@ -12,10 +12,21 @@ import (
// The hold ends when t ends.
func HoldWithoutListening(t testing.TB, port int) {
t.Helper()
ln, err := net.Listen("tcp", net.JoinHostPort(host, "0"))
release, err := TryHoldWithoutListening(port)
if err != nil {
t.Fatal(err)
}
t.Cleanup(release)
}
// TryHoldWithoutListening is HoldWithoutListening for a caller that handles
// a port it cannot take (another process on the host holds it): it returns
// the hold's release, or why the port could not be held.
func TryHoldWithoutListening(port int) (release func(), err error) {
ln, err := net.Listen("tcp", net.JoinHostPort(host, "0"))
if err != nil {
return nil, err
}
accepted := make(chan net.Conn, 1)
go func() {
c, _ := ln.Accept()
@@ -25,13 +36,14 @@ func HoldWithoutListening(t testing.TB, port int) {
conn, err := d.Dial("tcp", ln.Addr().String())
if err != nil {
ln.Close()
t.Fatalf("hold port %s: %v", strconv.Itoa(port), err)
<-accepted
return nil, fmt.Errorf("hold port %d: %w", port, err)
}
t.Cleanup(func() {
return func() {
conn.Close()
ln.Close()
if c := <-accepted; c != nil {
c.Close()
}
})
}, nil
}
+79 -5
View File
@@ -28,12 +28,51 @@ func TestWeedPairsAvoidTheEphemeralRange(t *testing.T) {
}
}
// answers is whether something accepts a connection at addr now.
func answers(addr string) bool {
c, err := net.DialTimeout("tcp", addr, 500*time.Millisecond)
if err == nil {
c.Close()
return true
}
return false
}
// heldTestPort holds a port that no other process on the host is using, for
// a test of the hold itself. A shared runner runs other jobs, and any of them
// can serve or take a picked port: a candidate that answers before the hold,
// cannot be held, or answers after the hold belongs to another process, and is
// released and skipped. The hold ends when t ends.
func heldTestPort(t *testing.T, pick func() int) int {
t.Helper()
for i := 0; i < 8; i++ {
p := pick()
addr := net.JoinHostPort(host, strconv.Itoa(p))
if answers(addr) {
continue // another process serves it
}
release, err := TryHoldWithoutListening(p)
if err != nil {
continue // another process took it meanwhile
}
if answers(addr) {
release() // another process serves it beside the hold
continue
}
t.Cleanup(release)
return p
}
t.Fatal("no port another process leaves alone in 8 picks")
return 0
}
// HoldWithoutListening reproduces that collision: the port cannot be bound
// and nothing answers on it.
// and nothing answers on it. The port is this test's own (heldTestPort): a
// port another job on the runner serves is never judged.
func TestAHeldPortCannotBeBoundAndDoesNotAnswer(t *testing.T) {
p := PickPair(t, map[int]bool{}, "test") + 10000
used := map[int]bool{}
p := heldTestPort(t, func() int { return PickPair(t, used, "test") + 10000 })
addr := net.JoinHostPort(host, strconv.Itoa(p))
HoldWithoutListening(t, p)
ln, err := net.Listen("tcp", addr)
if err == nil {
ln.Close()
@@ -42,8 +81,43 @@ func TestAHeldPortCannotBeBoundAndDoesNotAnswer(t *testing.T) {
if !errors.Is(err, syscall.EADDRINUSE) {
t.Fatalf("bind %s while held: %v, want address already in use", addr, err)
}
if c, err := net.DialTimeout("tcp", addr, time.Second); err == nil {
c.Close()
if answers(addr) {
t.Fatalf("%s answered while held without a listener", addr)
}
}
// A forced collision: the first port picked is served by another process
// (as on a shared runner). The test port skips it, and holds a later one.
func TestHeldTestPortSkipsAPortAnotherProcessServes(t *testing.T) {
foreign, err := net.Listen("tcp", net.JoinHostPort(host, "0"))
if err != nil {
t.Fatal(err)
}
defer foreign.Close()
go func() {
for {
c, err := foreign.Accept()
if err != nil {
return
}
c.Close()
}
}()
taken := foreign.Addr().(*net.TCPAddr).Port
used, picks := map[int]bool{}, 0
p := heldTestPort(t, func() int {
picks++
if picks == 1 {
return taken
}
return PickPair(t, used, "test") + 10000
})
if p == taken || picks < 2 {
t.Fatalf("held %d after %d picks; the first pick %d is another process's", p, picks, taken)
}
addr := net.JoinHostPort(host, strconv.Itoa(p))
if ln, err := net.Listen("tcp", addr); err == nil {
ln.Close()
t.Fatalf("%s is not held", addr)
}
}