@@ -28,6 +28,7 @@ import (
2828 "path/filepath"
2929 "regexp"
3030 "sort"
31+ "strconv"
3132 "strings"
3233 "sync"
3334 "testing"
@@ -243,6 +244,14 @@ func TestParityMatrix(t *testing.T) {
243244
244245 dockerAvailable := isDockerAvailable ()
245246
247+ // Sharding: split the selected cases round-robin across parallel CI jobs, so
248+ // N runners (with independent docker daemons) share the wall-clock. Inert when
249+ // PARITY_SHARD_TOTAL is unset or <= 1. Cases in other shards stay not-selected,
250+ // which the strict gate ignores (it only fails on inconclusive cases), so each
251+ // shard gates its own subset and the union covers everything exactly once.
252+ shardIndex , shardTotal := parityShard (t )
253+ selected := 0
254+
246255 for _ , tc := range matrix .InitialCases {
247256 tc := tc
248257 if ! matchesFilter (tc ) {
@@ -253,6 +262,13 @@ func TestParityMatrix(t *testing.T) {
253262 if tc .Lane == "runtime" && os .Getenv ("PARITY_LANE" ) == "" {
254263 continue
255264 }
265+ // Round-robin over the selected set (stable matrix order); consecutive
266+ // cases land on different shards, which spreads the heavy build.* cases.
267+ caseIndex := selected
268+ selected ++
269+ if ! inShard (caseIndex , shardIndex , shardTotal ) {
270+ continue
271+ }
256272
257273 t .Run (tc .ID , func (t * testing.T ) {
258274 outcome := parityMatched
@@ -1075,6 +1091,36 @@ func envOr(key, fallback string) string {
10751091 return fallback
10761092}
10771093
1094+ // inShard reports whether the case at caseIndex (its position in the selected
1095+ // stream) belongs to shard index of total. total <= 1 means no sharding (every
1096+ // case runs). The modulo partitions the stream into disjoint shards whose union
1097+ // is the whole set.
1098+ func inShard (caseIndex , index , total int ) bool {
1099+ return total <= 1 || caseIndex % total == index
1100+ }
1101+
1102+ // parityShard reads PARITY_SHARD_INDEX / PARITY_SHARD_TOTAL for CI sharding.
1103+ // Total defaults to 1 (no sharding). An invalid pair fails the test so a
1104+ // misconfigured matrix is loud rather than silently skipping cases.
1105+ func parityShard (t * testing.T ) (index , total int ) {
1106+ total = 1
1107+ if v := os .Getenv ("PARITY_SHARD_TOTAL" ); v != "" {
1108+ n , err := strconv .Atoi (v )
1109+ if err != nil || n < 1 {
1110+ t .Fatalf ("PARITY_SHARD_TOTAL=%q: must be a positive integer" , v )
1111+ }
1112+ total = n
1113+ }
1114+ if v := os .Getenv ("PARITY_SHARD_INDEX" ); v != "" {
1115+ n , err := strconv .Atoi (v )
1116+ if err != nil || n < 0 || n >= total {
1117+ t .Fatalf ("PARITY_SHARD_INDEX=%q: must be in [0,%d)" , v , total )
1118+ }
1119+ index = n
1120+ }
1121+ return index , total
1122+ }
1123+
10781124func isDockerAvailable () bool {
10791125 cmd := exec .Command ("docker" , "info" )
10801126 cmd .Stdout = nil
0 commit comments