Merge mini integration test port claims

This commit is contained in:
pingqiu committed 2026-12-05 16:53:44 -08:00
commit 577eb2ebad
1 file changed
+33 -2
@@ -5,6 +5,8 @@ package main_test
import (
"context"
"fmt"
"math/rand"
"net"
"os"
"path/filepath"
"strings"
@@ -49,7 +51,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()
@@ -111,7 +113,7 @@ func TestMiniStartsWithoutRootAndNamesItsBinaries(t *testing.T) {
dir2 := filepath.Join(t.TempDir(), "mini")
ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel2()
p2 := startOwnedMini(t, ctx2, cli, "mini", "--dir", dir2, "--bin", broken, "--weed", weed, "--port-shift", fmt.Sprint(shift+1))
p2 := startOwnedMini(t, ctx2, cli, "mini", "--dir", dir2, "--bin", broken, "--weed", weed, "--port-shift", fmt.Sprint(miniPortShift(t)))
err = p2.join(t, 3*time.Minute+miniShutdownGrace+10*time.Second)
b2 := p2.output.String()
t.Logf("failed start:\n%s", b2)
@@ -139,6 +141,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