diff --git a/seaweed-block/cmd/blockvolume/head_size_integration_test.go b/seaweed-block/cmd/blockvolume/head_size_integration_test.go index c14951faf1..0fb5d2fa14 100644 --- a/seaweed-block/cmd/blockvolume/head_size_integration_test.go +++ b/seaweed-block/cmd/blockvolume/head_size_integration_test.go @@ -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) diff --git a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go index 948c0bedde..ac224297cf 100644 --- a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go +++ b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go @@ -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 } diff --git a/seaweed-block/cmd/blockvolume/test_port_shard_test.go b/seaweed-block/cmd/blockvolume/test_port_shard_test.go new file mode 100644 index 0000000000..06a0fe1a1a --- /dev/null +++ b/seaweed-block/cmd/blockvolume/test_port_shard_test.go @@ -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) + } + } +} diff --git a/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go b/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go index 5416c6e5b9..5b1e845d48 100644 --- a/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go +++ b/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go @@ -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 }