claim invariant: run the door and refusal subtests in parallel
TestClaimConsoleMutationInvariant drove its ~147 door and refusal
fixtures one after another, about 130s of its 205s. They now run as
parallel subtests under a new "doors" group, at most
claimCaseParallelism (8) at once, each still on its own console.
t.Run("doors") returns only when every child has finished, so the
unreached-writer check after it still sees every door's open run; the
reached map is guarded by a mutex.
Subtest names gain the group level:
TestClaimConsoleMutationInvariant/<key> is now
TestClaimConsoleMutationInvariant/doors/<key>. Lifecycle variants keep
their names.
Five relay fixtures stage relay masters with t.Setenv (process
environment, which t.Parallel forbids). They are listed in
claimSerialDoors with the reason and run first, one at a time, before
any parallel door starts. A census checks every entry names a door or
refusal fixture.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+85
−14
internal/console/claim_invariant_test.go+85 −14
diff --git a/internal/console/claim_invariant_test.go b/internal/console/claim_invariant_test.go| index 6f674b7eb..183f014b9 100644 |
| --- a/internal/console/claim_invariant_test.go |
| +++ b/internal/console/claim_invariant_test.go |
| @@ -53,6 +53,7 @@ import ( |
| "slices" |
| "sort" |
| "strings" |
| + "sync" |
| "testing" |
| "time" |
| @@ -178,6 +179,47 @@ var claimCoordinators = map[string]string{ |
| "and the function's own writes reach Put, Delete or DeleteIfMatch)", |
| } |
| +// claimCaseParallelism bounds how many door and refusal subtests run at |
| +// once. Each one builds its own console and measures what a request left |
| +// behind by waiting for quiet windows (quiesce). Too many at once starve the |
| +// CPU, and a handler goroutine that misses its window is measured with the |
| +// wrong request; the bound keeps that contention down. |
| +const claimCaseParallelism = 8 |
| + |
| +// claimSerialDoors are the door and refusal fixtures that must not run in |
| +// parallel, each with the reason. They run first, one at a time, before any |
| +// parallel door starts. A fixture whose staging touches process-wide state |
| +// (the environment, the log output, a package variable) belongs here. |
| +var claimSerialDoors = map[string]string{ |
| + "route POST /api/cli/relay/rotate": "relayEnabled and relayStageMaster stage the region's relay masters " + |
| + "through t.Setenv: process environment, which t.Parallel forbids", |
| + "route POST /api/cli/apps/{account}/{app}/relay": "relayArmed stages the region's relay master through t.Setenv: " + |
| + "process environment, which t.Parallel forbids", |
| + "route POST /api/cli/apps/{account}/{app}/relay/adopted": "relayEnabled stages the region's relay master through t.Setenv: " + |
| + "process environment, which t.Parallel forbids", |
| + "route POST /api/cli/apps/{account}/{app}/relay/disable": "relayEnabled stages the region's relay master through t.Setenv: " + |
| + "process environment, which t.Parallel forbids", |
| + "worker StartRelay": "relayEnabled stages the region's relay master through t.Setenv: " + |
| + "process environment, which t.Parallel forbids", |
| +} |
| + |
| +// claimSerialDoorCensus: every serial entry names a door or refusal fixture |
| +// and carries its reason. |
| +func claimSerialDoorCensus(fixtures map[string]claimFixture, serial map[string]string) []string { |
| + var problems []string |
| + for key, reason := range serial { |
| + f, ok := fixtures[key] |
| + switch { |
| + case !ok || (f.door == nil && f.refused == nil): |
| + problems = append(problems, fmt.Sprintf("claimSerialDoors names %s, which is not a door or refusal fixture", key)) |
| + case strings.TrimSpace(reason) == "": |
| + problems = append(problems, fmt.Sprintf("claimSerialDoors names %s with no reason", key)) |
| + } |
| + } |
| + sort.Strings(problems) |
| + return problems |
| +} |
| + |
| func TestClaimConsoleMutationInvariant(t *testing.T) { |
| census := newClaimCase(t) |
| if !census.srv.cfg.HostClaim { |
| @@ -209,30 +251,59 @@ func TestClaimConsoleMutationInvariant(t *testing.T) { |
| } |
| sort.Strings(keys) |
| - reached := map[string]bool{} |
| - for _, key := range keys { |
| - f := claimFixtures[key] |
| - if f.refused != nil { |
| - t.Run(key, func(t *testing.T) { |
| + if problems := claimSerialDoorCensus(claimFixtures, claimSerialDoors); len(problems) != 0 { |
| + for _, p := range problems { |
| + t.Error(p) |
| + } |
| + t.FailNow() |
| + } |
| + |
| + // Doors and refusals, each on its own console. The group returns only |
| + // when every one of them has finished, parallel ones included, so the |
| + // writer check below sees every door's open run. |
| + var ( |
| + reachedMu sync.Mutex |
| + reached = map[string]bool{} |
| + ) |
| + t.Run("doors", func(t *testing.T) { |
| + slots := make(chan struct{}, claimCaseParallelism) |
| + run := func(t *testing.T, f claimFixture) { |
| + if f.refused != nil { |
| for _, p := range runClaimRefusal(t, f.refused) { |
| t.Error(p) |
| } |
| - }) |
| - continue |
| - } |
| - if f.door == nil { |
| - continue |
| - } |
| - t.Run(key, func(t *testing.T) { |
| + return |
| + } |
| problems, writers := runClaimDoor(t, f.door) |
| for _, p := range problems { |
| t.Error(p) |
| } |
| + reachedMu.Lock() |
| for _, w := range writers { |
| reached[w] = true |
| } |
| - }) |
| - } |
| + reachedMu.Unlock() |
| + } |
| + // The serial ones first, one at a time, while nothing else runs: |
| + // a parallel subtest only starts once this function has returned. |
| + for _, key := range keys { |
| + if f := claimFixtures[key]; (f.door != nil || f.refused != nil) && claimSerialDoors[key] != "" { |
| + t.Run(key, func(t *testing.T) { run(t, f) }) |
| + } |
| + } |
| + for _, key := range keys { |
| + if f := claimFixtures[key]; (f.door != nil || f.refused != nil) && claimSerialDoors[key] == "" { |
| + t.Run(key, func(t *testing.T) { |
| + t.Parallel() |
| + slots <- struct{}{} |
| + // Registered first, so it runs last: the slot is held |
| + // until the case's own cleanup (servers, store) is done. |
| + t.Cleanup(func() { <-slots }) |
| + run(t, f) |
| + }) |
| + } |
| + } |
| + }) |
| // Lifecycle variants: further actions on an entry above whose only |
| // effect is a host claim written under their own registration grant |