test(block): claim the mini test's ports host-wide

The pid-derived --port-shift put mini's 13 ports inside the handoff and
weed pair ranges the other process-level harnesses claim, unclaimed, so a
parallel CI shard could bind one of them. Pick a shift whose every port is
claimed and free, as handoffAddress does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-05 16:26:07 -08:00
1 parent af7f50945f
commit ff4bc9a0d7
1 file changed
+32 -1
@@ -5,6 +5,8 @@ package main_test
import (
"context"
"fmt"
"math/rand"
"net"
"os"
"path/filepath"
"strings"
@@ -41,7 +43,7 @@ func TestMiniStartsWithoutRootAndNamesItsBinaries(t *testing.T) {
t.Fatal(err)
}
cli := filepath.Join(bins, "sw-block")
shift := 3000 + (os.Getpid()*13)%6000
shift := miniPortShift(t)
dir := filepath.Join(t.TempDir(), "mini")
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute)
defer cancel()
@@ -131,6 +133,35 @@ func miniGoBuild(t *testing.T, out, pkg string) {
}
// miniPortShift picks a --port-shift whose every mini port (defaultMiniPorts
// in cmd/sw-block) is claimed host-wide and free now, as the other harnesses'
// ports are, so a parallel process-level shard never binds one of them.
func miniPortShift(t *testing.T) int {
t.Helper()
defaults := []int{9180, 9201, 9202, 9203, 4420, 9333, 19333, 9340, 19340, 8888, 18888, 12535, 22535}
for i := 0; i < 200; i++ {
shift := 3000 + rand.Intn(6000)
free := true
for _, p := range defaults {
if !claimHostPort(p + shift) {
free = false
break
}
ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", p+shift))
if err != nil {
free = false
break
}
ln.Close()
}
if free {
return shift
}
}
t.Fatal("no --port-shift with every mini port claimed and free")
return 0
}
func exists(p string) bool {
_, err := os.Stat(p)
return err == nil