test: a CI shard draws its test ports from its own slice

Several process-level test binaries can now run in parallel on one host:
with SWBLOCK_TEST_PORT_SHARD=k and SWBLOCK_TEST_PORT_SHARDS=n, the harness's
random port pickers (handoffAddress, the head-size pickers, and the weed
pair picker's usable range) draw only from the k-th of n equal slices, so
two shards never pick the same port. Unset, the whole range, as before.
TestTestPortSpanGivesEachShardItsOwnSlice covers the slices and the
fallbacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-27 08:41:16 -08:00
1 parent b78a01e239
commit f5ef643295
4 files changed
+59 -4

No files matched your search

@@ -268,7 +268,8 @@ func headSzLoopbackAddr(t *testing.T) string {
handoffPorts.used = map[int]bool{}
}
for i := 0; i < 500; i++ {
p := 10000 + rand.Intn(10000)
lo, hi := testPortSpan(10000, 20000)
p := lo + rand.Intn(hi-lo)
if handoffPorts.used[p] {
continue
}
@@ -293,7 +294,8 @@ func headSzRemoteAddr(t *testing.T, host string) string {
handoffPorts.used = map[int]bool{}
}
for i := 0; i < 500; i++ {
p := 10000 + rand.Intn(10000)
lo, hi := testPortSpan(10000, 20000)
p := lo + rand.Intn(hi-lo)
if !handoffPorts.used[p] {
handoffPorts.used[p] = true
return fmt.Sprintf("%s:%d", host, p)
@@ -518,7 +518,8 @@ func handoffAddress(t *testing.T) string {
handoffPorts.used = map[int]bool{}
}
for i := 0; i < 500; i++ {
p := 10000 + rand.Intn(10000)
lo, hi := testPortSpan(10000, 20000)
p := lo + rand.Intn(hi-lo)
if handoffPorts.used[p] {
continue
}
@@ -0,0 +1,45 @@
package main_test
import (
"os"
"strconv"
"testing"
)
// testPortSpan narrows a port range [lo, hi) to this test process's share
// when several process-level test binaries run in parallel on one host
// (CI shards): SWBLOCK_TEST_PORT_SHARD=k of SWBLOCK_TEST_PORT_SHARDS=n
// (1 <= k <= n) gets the k-th of n equal slices, so two shards never draw
// the same port. Unset or invalid, the whole range.
func testPortSpan(lo, hi int) (int, int) {
n, errN := strconv.Atoi(os.Getenv("SWBLOCK_TEST_PORT_SHARDS"))
k, errK := strconv.Atoi(os.Getenv("SWBLOCK_TEST_PORT_SHARD"))
if errN != nil || errK != nil || n < 2 || k < 1 || k > n || hi-lo < n {
return lo, hi
}
w := (hi - lo) / n
return lo + (k-1)*w, lo + k*w
}
func TestTestPortSpanGivesEachShardItsOwnSlice(t *testing.T) {
t.Setenv("SWBLOCK_TEST_PORT_SHARDS", "")
if lo, hi := testPortSpan(10000, 20000); lo != 10000 || hi != 20000 {
t.Fatalf("unsharded: [%d, %d)", lo, hi)
}
t.Setenv("SWBLOCK_TEST_PORT_SHARDS", "4")
prev := 10000
for k := 1; k <= 4; k++ {
t.Setenv("SWBLOCK_TEST_PORT_SHARD", strconv.Itoa(k))
lo, hi := testPortSpan(10000, 20000)
if lo != prev || hi-lo != 2500 {
t.Fatalf("shard %d of 4: [%d, %d); want [%d, %d)", k, lo, hi, prev, prev+2500)
}
prev = hi
}
for _, bad := range []string{"0", "5", "x"} {
t.Setenv("SWBLOCK_TEST_PORT_SHARD", bad)
if lo, hi := testPortSpan(10000, 20000); lo != 10000 || hi != 20000 {
t.Fatalf("shard %q of 4: [%d, %d); want the whole range", bad, lo, hi)
}
}
}
@@ -55,8 +55,15 @@ func ephemeralPorts() (int, int) {
func w4FreePortPair(t *testing.T, used map[int]bool) int {
t.Helper()
lo, hi := ephemeralPorts()
// The pair's gRPC port p+10000 stays below the ephemeral range; a CI
// shard draws from its own slice of what is left.
top := 40000
if lo-10000 < top && lo-10000 > 20000 {
top = lo - 10000
}
sLo, sHi := testPortSpan(20000, top)
for i := 0; i < 200; i++ {
p := 20000 + rand.Intn(20000)
p := sLo + rand.Intn(sHi-sLo)
if used[p] || used[p+10000] {
continue
}