beads/cmd/bd/swarm_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

358 lines
11 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
)
// bdSwarm runs "bd swarm" with the given args and returns raw stdout.
// Stderr (warnings, tips) is captured separately so it does not pollute
// callers that parse stdout.
func bdSwarm(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"swarm"}, 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 swarm %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdSwarmFail runs "bd swarm" expecting failure.
func bdSwarmFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"swarm"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd swarm %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
// bdSwarmJSON runs "bd swarm" with --json and parses the result as a map.
// Stderr is captured separately so warnings do not corrupt JSON parsing.
func bdSwarmJSON(t *testing.T, bd, dir string, args ...string) map[string]interface{} {
t.Helper()
fullArgs := append([]string{"swarm"}, args...)
fullArgs = append(fullArgs, "--json")
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd swarm --json %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.IndexAny(s, "{[")
if start < 0 {
t.Fatalf("no JSON in swarm stdout: %s\nstderr:\n%s", s, stderr.String())
}
var m map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &m); err != nil {
t.Fatalf("parse swarm JSON: %v\n%s\nstderr:\n%s", err, s, stderr.String())
}
return m
}
// createSwarmableEpic creates an epic with 3 children forming a DAG suitable for swarming.
func createSwarmableEpic(t *testing.T, bd, dir, prefix string) (epicID string, childIDs []string) {
t.Helper()
plan := fmt.Sprintf(`{
"nodes": [
{"key": "epic", "title": %q, "type": "epic"},
{"key": "child1", "title": %q, "type": "task", "parent_key": "epic"},
{"key": "child2", "title": %q, "type": "task", "parent_key": "epic"},
{"key": "child3", "title": %q, "type": "task", "parent_key": "epic"}
],
"edges": [
{"from_key": "child3", "to_key": "child1", "type": "blocks"}
]
}`, prefix+" epic", prefix+" child 1", prefix+" child 2", prefix+" child 3")
planFile := filepath.Join(dir, "swarm-graph-plan.json")
if err := os.WriteFile(planFile, []byte(plan), 0o600); err != nil {
t.Fatalf("write swarm graph plan: %v", err)
}
result := bdCreateGraph(t, bd, dir, planFile)
epicID = result.IDs["epic"]
childIDs = []string{result.IDs["child1"], result.IDs["child2"], result.IDs["child3"]}
allIDs := append([]string{epicID}, childIDs...)
for i, id := range allIDs {
if id == "" {
t.Fatalf("graph create returned an empty ID at index %d: %#v", i, result.IDs)
}
for j := 0; j < i; j++ {
if id == allIDs[j] {
t.Fatalf("graph create returned duplicate IDs at indexes %d and %d: %#v", j, i, result.IDs)
}
}
}
return epicID, childIDs
}
// setupEmbeddedSwarm is the TestEmbeddedSwarm* tests' workspace (prefix sw).
func setupEmbeddedSwarm(t *testing.T) (string, string) {
t.Helper()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "sw")
return bd, dir
}
func TestEmbeddedSwarmValidate(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, dir := setupEmbeddedSwarm(t)
epicID, _ := createSwarmableEpic(t, bd, dir, "Swarm")
// ===== swarm validate =====
t.Run("validate_epic", func(t *testing.T) {
out := bdSwarm(t, bd, dir, "validate", epicID)
if !strings.Contains(out, epicID) {
t.Errorf("expected epic ID in validate output: %s", out)
}
})
t.Run("validate_verbose", func(t *testing.T) {
m := bdSwarmJSON(t, bd, dir, "validate", epicID, "--verbose")
if _, ok := m["issues"]; !ok {
t.Error("expected 'issues' key with --verbose")
}
})
t.Run("validate_json", func(t *testing.T) {
m := bdSwarmJSON(t, bd, dir, "validate", epicID)
if _, ok := m["swarmable"]; !ok {
t.Error("expected 'swarmable' key in JSON")
}
if _, ok := m["total_issues"]; !ok {
t.Error("expected 'total_issues' key in JSON")
}
})
t.Run("validate_non_swarmable", func(t *testing.T) {
// Epic with no children — validate succeeds but warns
emptyEpic := bdCreate(t, bd, dir, "Empty epic", "--type", "epic")
out := bdSwarm(t, bd, dir, "validate", emptyEpic.ID)
if !strings.Contains(out, "no children") && !strings.Contains(out, "0") {
t.Errorf("expected warning about no children: %s", out)
}
})
}
func TestEmbeddedSwarmCreate(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, dir := setupEmbeddedSwarm(t)
// ===== swarm create =====
t.Run("create_swarm", func(t *testing.T) {
eID, _ := createSwarmableEpic(t, bd, dir, "Create")
out := bdSwarm(t, bd, dir, "create", eID)
if !strings.Contains(out, "Created swarm molecule") {
t.Errorf("expected 'Created swarm molecule': %s", out)
}
})
t.Run("create_auto_wrap_single", func(t *testing.T) {
// Creating swarm on a non-epic should auto-wrap
task := bdCreate(t, bd, dir, "Auto wrap task", "--type", "task")
out := bdSwarm(t, bd, dir, "create", task.ID)
if !strings.Contains(out, "Created swarm molecule") && !strings.Contains(out, "Auto-wrapping") {
t.Errorf("expected auto-wrap or creation: %s", out)
}
})
t.Run("create_coordinator", func(t *testing.T) {
eID, _ := createSwarmableEpic(t, bd, dir, "Coord")
m := bdSwarmJSON(t, bd, dir, "create", eID, "--coordinator", "alice")
if m["coordinator"] != "alice" {
t.Errorf("expected coordinator=alice, got %v", m["coordinator"])
}
})
}
func TestEmbeddedSwarmCreateForceAndErrors(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, dir := setupEmbeddedSwarm(t)
t.Run("create_force", func(t *testing.T) {
eID, _ := createSwarmableEpic(t, bd, dir, "Force")
bdSwarm(t, bd, dir, "create", eID)
// Second create without --force should fail
bdSwarmFail(t, bd, dir, "create", eID)
// With --force should succeed
bdSwarm(t, bd, dir, "create", eID, "--force")
})
t.Run("create_json_output", func(t *testing.T) {
eID, _ := createSwarmableEpic(t, bd, dir, "JSON")
m := bdSwarmJSON(t, bd, dir, "create", eID)
if _, ok := m["swarm_id"]; !ok {
t.Error("expected 'swarm_id' in JSON")
}
if _, ok := m["epic_id"]; !ok {
t.Error("expected 'epic_id' in JSON")
}
})
t.Run("create_error_existing", func(t *testing.T) {
eID, _ := createSwarmableEpic(t, bd, dir, "Exist")
bdSwarm(t, bd, dir, "create", eID)
bdSwarmFail(t, bd, dir, "create", eID)
})
}
func TestEmbeddedSwarmStatusList(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, dir := setupEmbeddedSwarm(t)
// ===== swarm status =====
t.Run("status_shows_progress", func(t *testing.T) {
eID, _ := createSwarmableEpic(t, bd, dir, "Status")
bdSwarm(t, bd, dir, "create", eID)
out := bdSwarm(t, bd, dir, "status", eID)
if len(out) == 0 {
t.Error("expected non-empty status output")
}
})
t.Run("status_json", func(t *testing.T) {
eID, _ := createSwarmableEpic(t, bd, dir, "StatJSON")
bdSwarm(t, bd, dir, "create", eID)
m := bdSwarmJSON(t, bd, dir, "status", eID)
if len(m) == 0 {
t.Error("expected non-empty JSON status")
}
})
// ===== swarm list =====
t.Run("list_shows_swarms", func(t *testing.T) {
out := bdSwarm(t, bd, dir, "list")
// We've created several swarms above
if len(out) == 0 {
t.Error("expected non-empty list output")
}
})
t.Run("list_json", func(t *testing.T) {
fullArgs := []string{"swarm", "list", "--json"}
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("swarm list --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
// Should be valid JSON on stdout (warnings, if any, are on stderr).
s := strings.TrimSpace(stdout.String())
if !strings.HasPrefix(s, "{") && !strings.HasPrefix(s, "[") {
t.Errorf("expected JSON output on stdout: %s\nstderr: %s", s, stderr.String())
}
})
t.Run("list_empty", func(t *testing.T) {
dir2, _, _ := bdInit(t, bd, "--prefix", "sw2")
out := bdSwarm(t, bd, dir2, "list")
if !strings.Contains(out, "No swarm") && !strings.Contains(out, "no swarm") && len(strings.TrimSpace(out)) > 0 {
// Just verify no crash — empty list message varies
}
})
}
// TestEmbeddedSwarmConcurrent exercises swarm operations concurrently.
func TestEmbeddedSwarmConcurrent(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", "swc")
// Create several swarmable epics.
var epicIDs []string
for i := 0; i < 4; i++ {
eID, _ := createSwarmableEpic(t, bd, dir, fmt.Sprintf("Conc%d", i))
epicIDs = append(epicIDs, eID)
}
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}
eID := epicIDs[worker%len(epicIDs)]
queries := [][]string{
{"validate", eID, "--json"},
{"validate", eID, "--verbose", "--json"},
{"validate", eID, "--json"},
{"list", "--json"},
{"validate", eID, "--json"},
{"list", "--json"},
{"validate", eID, "--json"},
{"validate", eID, "--verbose", "--json"},
}
q := queries[worker%len(queries)]
args := append([]string{"swarm"}, q...)
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("worker %d: %v\n%s", worker, 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") && !strings.Contains(r.err.Error(), "exclusive lock") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}