beads/cmd/bd/children_embedded_test.go
Julian Knutsen 0d865460b1
ci(bazel): split the slow embedded cmd tests and run the cmd block as 2x50 shards (#7438)
//cmd/bd:bd_embedded_test was the embedded lane's floor: at Bazel's 50-shard
cap per rule, each shard's wall time was its slowest top-level test, and
~40 tests ran 65-130s on rbe-west.

- 34 slow TestEmbedded* functions are split into 89 smaller top-level
  functions. Subtests and table cases are moved verbatim: no assertion,
  flag or data changes, the same t.Run set per file, the same skip guard and
  t.Parallel. Subtests that read state earlier ones left stay together, in
  order; each new function repeats the shared setup it needs.
- The manifest's cmd/bd block is now 100 shards, run by two targets:
  bd_embedded_test (shards 1-50) and the new bd_embedded_part2_test
  (51-100), through go_test_manifest_shard.sh --shard-offset/--shard-total.
  check_shard_coverage.py takes SHARDS@OFFSET/TOTAL and fails unless the
  targets' ranges tile the block exactly once, so every test runs in exactly
  one shard of one target; the policy tests read both rules' ranges from
  cmd/bd/BUILD.bazel.
- Durations re-measured on rbe-west with the split tests and repacked. The
  frozen legacy 20-shard block drops the renamed names (no workflow reads
  it; new names hash-distribute as before).

Not split: the *Concurrent stress tests (their bd subprocesses queue on the
workspace's single writer lock: TestEmbeddedUpdateConcurrent is ~100
serialized bd commands), TestEmbeddedPurgeWispsPlaneRetention (one stateful
sequence), and the MaxRowsList* tests (setup is 21 sequential creates every
subtest reads).

rbe-west, both targets, --nocache_test_results: slowest shard 138-163s ->
109s (TestEmbeddedUpdateConcurrent alone; every other shard <=86s), median
80-84s -> 40s, shard-seconds 4.1-4.4k -> 4.2k, critical path 213-306s ->
113s.

Bead: ga-vnycm2.17

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-09 20:30:52 +00:00

179 lines
5.4 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdChildren runs "bd children" with the given args and returns stdout.
func bdChildren(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"children"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd children %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
func TestEmbeddedChildrenBasicAndEmpty(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "ch")
t.Run("children_basic", func(t *testing.T) {
parent := bdCreate(t, bd, dir, "Parent epic", "--type", "epic")
child1 := bdCreate(t, bd, dir, "Child task 1", "--type", "task")
child2 := bdCreate(t, bd, dir, "Child task 2", "--type", "task")
bdDepAdd(t, bd, dir, child1.ID, parent.ID, "--type", "parent-child")
bdDepAdd(t, bd, dir, child2.ID, parent.ID, "--type", "parent-child")
out := bdChildren(t, bd, dir, parent.ID)
if !strings.Contains(out, child1.ID) {
t.Errorf("expected child1 %s in output: %s", child1.ID, out)
}
if !strings.Contains(out, child2.ID) {
t.Errorf("expected child2 %s in output: %s", child2.ID, out)
}
})
t.Run("children_empty", func(t *testing.T) {
parent := bdCreate(t, bd, dir, "No children parent", "--type", "task")
out := bdChildren(t, bd, dir, parent.ID)
// Should not error, may show empty message
_ = out
})
t.Run("children_nonexistent_parent", func(t *testing.T) {
cmd := exec.Command(bd, "children", "ch-nonexistent999")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected children of nonexistent to fail, got: %s", out)
}
})
}
func TestEmbeddedChildrenJSONAndAllStatuses(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "ch")
t.Run("children_json", func(t *testing.T) {
parent := bdCreate(t, bd, dir, "JSON parent", "--type", "epic")
child := bdCreate(t, bd, dir, "JSON child", "--type", "task")
bdDepAdd(t, bd, dir, child.ID, parent.ID, "--type", "parent-child")
cmd := exec.Command(bd, "children", parent.ID, "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd children --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.Index(s, "[")
if start >= 0 {
var issues []map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &issues); err != nil {
t.Fatalf("parse children JSON: %v\n%s", err, s)
}
if len(issues) != 1 {
t.Errorf("expected 1 child, got %d", len(issues))
}
}
})
t.Run("children_includes_all_statuses", func(t *testing.T) {
parent := bdCreate(t, bd, dir, "All status parent", "--type", "epic")
openChild := bdCreate(t, bd, dir, "Open child", "--type", "task")
closedChild := bdCreate(t, bd, dir, "Closed child", "--type", "task")
bdDepAdd(t, bd, dir, openChild.ID, parent.ID, "--type", "parent-child")
bdDepAdd(t, bd, dir, closedChild.ID, parent.ID, "--type", "parent-child")
bdClose(t, bd, dir, closedChild.ID)
out := bdChildren(t, bd, dir, parent.ID)
if !strings.Contains(out, openChild.ID) {
t.Errorf("expected open child in output: %s", out)
}
if !strings.Contains(out, closedChild.ID) {
t.Errorf("expected closed child in output (--all implied): %s", out)
}
})
}
// TestEmbeddedChildrenConcurrent exercises children listing concurrently.
func TestEmbeddedChildrenConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "cx")
// Create parents with children
var parentIDs []string
for i := 0; i < 4; i++ {
parent := bdCreate(t, bd, dir, fmt.Sprintf("Concurrent parent %d", i), "--type", "epic")
for j := 0; j < 2; j++ {
child := bdCreate(t, bd, dir, fmt.Sprintf("Child %d-%d", i, j), "--type", "task")
bdDepAdd(t, bd, dir, child.ID, parent.ID, "--type", "parent-child")
}
parentIDs = append(parentIDs, parent.ID)
}
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
parentID := parentIDs[worker%len(parentIDs)]
cmd := exec.Command(bd, "children", parentID, "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("children %s: %v\n%s", parentID, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}