beads/cmd/bd/ready_embedded_test.go
Julian Knutsen 3c30a28d07
perf(ready): answer the page and its total in one pass (#6731)
* perf(issueops): answer a ready page and its total in one pass

`bd ready --json --limit 1` against a remote SQL store (~55 ms per round
trip) took 5.4 s and ~100 sequential round trips; `--limit 0` (557 rows)
took 3.8 s. Asking for one row was slower than asking for all of them. The
data is small, so the cost was purely statement count:

- a capped page re-ran the whole ready predicate in a SECOND transaction
  (CountReadyWorkInTx, behind a second defer-wake sweep) just to print
  "Showing 1 of 556";
- every leg re-probed the same facts: wisp_dependencies existence, wisps
  emptiness, and the future-deferred-parent scan (2 probes + 4 child
  queries), once per table family and again in the count pass.

This change:

- Adds a per-transaction probe (probeReadyWorkInTx): one statement of
  EXISTS subqueries answers wisps-present, wisp_dependencies-exists,
  future-deferred-parent-exists and (when sizing) issues/wisps ID collision;
  the deferred-parent children are then read in one UNION ALL instead of
  four queries. The facts and the rendered ID sets are shared by both
  families. A missing-table error (pre-wisp database) falls back to the old
  per-table probes, so their tolerance and error classification are
  unchanged.
- Adds GetReadyWorkWithCountsAndTotalInTx and
  DoltStorage.GetReadyWorkWithCountsAndTotal: the capped page's ID query
  carries COUNT(*) OVER (), evaluated over the full predicate before LIMIT,
  so the total costs no extra statement. The overlap count only runs when
  the probe saw an ID in both tables. Window functions are supported by
  every engine this code runs on (Dolt/go-mysql-server server and embedded,
  MySQL 8, Postgres, SQLite >= 3.25). The externaldeps decorator overrides
  the new method so its exclusions apply to page and total alike, and now
  also overrides ReadyCounter() so the text-mode total honors the same
  exclusions (it used to pass through to the inner store and count
  externally blocked issues); telemetry instruments the new method.
- CountReadyWorkInTx (ReadyCounter role, text `bd ready`) uses the probe
  and counts both families in one statement.

Whole-command statements for `bd ready --json`, counted from a local dolt
sql-server query log (13 of each are startup/config reads this change does
not touch):

  store                          --limit 1   --limit 0
  issues only                    28 -> 18    20 -> 17
  issues + wisps                 34 -> 19    23 -> 18
  issues + wisps + deferred epic 46 -> 20    29 -> 19

TestReadyWorkStatementBudget holds the per-transaction statement count at
or under today's budget on embedded Dolt. TestReadyWorkPageTotal and
TestReadyWorkPageTotalFilterMatrix check the page and total against
hand-written expected ready sets (wisps, deferred parent, blocked, ID
collision; sorts, label/assignee/priority/type/parent/deferred/exclude
filters) and every capped page against the unbounded listing's prefix.

stdout (plain and BD_JSON_ENVELOPE=1) and the stderr hint were
byte-identical before/after for every case above. The one intended output
change: with an external dependency present, the --json total (and the
text "of N") now excludes externally blocked issues, as the page already
did; before, the total counted them.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* perf(ready): take the --json total in-band instead of a second count pass

`bd ready --json --limit N` listed a page, then - when the page came back
full - opened a second store call (ReadyCounter.CountReady: a new read
transaction plus its own defer-wake sweep) only to print "Showing N of M"
on stderr and the pagination total in the JSON envelope. On a remote store
(~55 ms per round trip) that pass is why `--limit 1` (5.4 s) was slower
than `--limit 0` (3.8 s).

The --json branch now calls GetReadyWorkWithCountsAndTotal, which returns
the page and the size of the whole ready set from the same transaction.
The total is the same identity the ReadyCounter role promises, taken over
the listing's own filter, so the hint text, the envelope's pagination and
the stdout JSON are identical except that the total now honors
external-dependency exclusions (the externaldeps decorator applies them to
page and total alike; the old second pass counted externally blocked
issues). The text-mode branch still asks the ReadyCounter role, which the
previous commit made cheaper and made apply the same exclusions, so text
and --json report one number.

The embedded CLI test now pins the exact "Showing 2 of N" hint and the
envelope's pagination total against an unbounded listing.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 23:50:09 -07:00

537 lines
18 KiB
Go

//go:build cgo
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/steveyegge/beads/internal/types"
)
func TestEmbeddedReady(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", "rd")
bdCreate(t, bd, dir, "Ready test issue", "--type", "task")
// ===== Default =====
t.Run("ready_includes_open_issue_with_zero_dependencies", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "GH3268 zero dependency ready issue", "--type", "task", "--label", "gh3268-zero-deps")
cmd := exec.Command(bd, "ready", "--json", "--label", "gh3268-zero-deps")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
var ready []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &ready); err != nil {
t.Fatalf("parse ready JSON: %v\n%s", err, stdout.String())
}
if len(ready) != 1 {
t.Fatalf("ready count = %d, want 1: %s", len(ready), stdout.String())
}
if ready[0].ID != issue.ID {
t.Fatalf("ready ID = %s, want %s", ready[0].ID, issue.ID)
}
if ready[0].DependencyCount != 0 {
t.Fatalf("dependency_count = %d, want 0", ready[0].DependencyCount)
}
})
t.Run("ready_default", func(t *testing.T) {
cmd := exec.Command(bd, "ready")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Ready test issue") {
t.Errorf("expected issue in ready output: %s", stdout.String())
}
})
// ===== --json =====
t.Run("ready_json", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.IndexAny(s, "[{")
if start < 0 {
t.Fatalf("no JSON in ready --json output: %s", s)
}
if !json.Valid([]byte(s[start:])) {
t.Errorf("invalid JSON in ready output: %s", s[:min(200, len(s))])
}
})
t.Run("ready_json_truncation_hint", func(t *testing.T) {
for i := 0; i < 3; i++ {
bdCreate(t, bd, dir, fmt.Sprintf("Ready capped issue %d", i), "--type", "task")
}
cmd := exec.Command(bd, "ready", "--json", "--limit", "2")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
t.Fatalf("bd ready --json --limit 2 failed: %v\nstderr: %s\nstdout: %s", err, stderr.String(), out)
}
if !json.Valid(bytes.TrimSpace(out)) {
t.Fatalf("ready JSON stdout should remain parseable, got: %s", out)
}
if !strings.Contains(stderr.String(), "Use --limit 0 for all") {
t.Fatalf("expected truncation hint on stderr, got: %q", stderr.String())
}
// The "of N" is the whole ready set, now taken in the page's own
// transaction: it must be exactly what an unbounded listing returns,
// in the hint and in the envelope's pagination alike.
all := exec.Command(bd, "ready", "--json", "--limit", "0")
all.Dir = dir
all.Env = bdEnv(dir)
allOut, err := all.Output()
if err != nil {
t.Fatalf("bd ready --json --limit 0 failed: %v", err)
}
var unbounded []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(allOut), &unbounded); err != nil {
t.Fatalf("parse unbounded ready JSON: %v\n%s", err, allOut)
}
wantHint := fmt.Sprintf("Showing 2 of %d ready issues.", len(unbounded))
if !strings.Contains(stderr.String(), wantHint) {
t.Fatalf("hint = %q, want it to contain %q", stderr.String(), wantHint)
}
env := exec.Command(bd, "ready", "--json", "--limit", "2")
env.Dir = dir
env.Env = append(bdEnv(dir), "BD_JSON_ENVELOPE=1")
envOut, err := env.Output()
if err != nil {
t.Fatalf("bd ready --json --limit 2 (envelope) failed: %v", err)
}
var envelope struct {
Data []types.IssueWithCounts `json:"data"`
Pagination *PaginationMeta `json:"pagination"`
}
if err := json.Unmarshal(bytes.TrimSpace(envOut), &envelope); err != nil {
t.Fatalf("parse envelope: %v\n%s", err, envOut)
}
if envelope.Pagination == nil || !envelope.Pagination.Truncated ||
envelope.Pagination.Returned != 2 || envelope.Pagination.Total != len(unbounded) {
t.Fatalf("pagination = %+v, want returned=2 total=%d truncated", envelope.Pagination, len(unbounded))
}
})
t.Run("ready_claim_json", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Ready claim json", "--type", "task", "--label", "ready-claim-json")
cmd := exec.Command(bd, "ready", "--claim", "--json", "--label", "missing-label")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --claim --json with no matches failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
var empty []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &empty); err != nil {
t.Fatalf("parse empty claim JSON: %v\n%s", err, stdout.String())
}
if len(empty) != 0 {
t.Fatalf("expected no claimed issues for unmatched label, got %d", len(empty))
}
cmd = exec.Command(bd, "ready", "--claim", "--json", "--label", "ready-claim-json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout.Reset()
stderr.Reset()
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("bd ready --claim --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
var claimed []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &claimed); err != nil {
t.Fatalf("parse claim JSON: %v\n%s", err, stdout.String())
}
if len(claimed) != 1 {
t.Fatalf("expected one claimed issue, got %d: %s", len(claimed), stdout.String())
}
if claimed[0].ID != issue.ID {
t.Fatalf("claimed ID = %s, want %s", claimed[0].ID, issue.ID)
}
if claimed[0].Status != types.StatusInProgress {
t.Fatalf("claimed status = %s, want %s", claimed[0].Status, types.StatusInProgress)
}
if claimed[0].Assignee == "" {
t.Fatal("expected claimed issue to have assignee")
}
})
// ===== With Blockers =====
t.Run("ready_excludes_blocked", func(t *testing.T) {
blocker := bdCreate(t, bd, dir, "Blocker issue", "--type", "task")
blocked := bdCreate(t, bd, dir, "Blocked by blocker", "--type", "task")
// Add blocking dependency: blocked depends on blocker
cmd := exec.Command(bd, "dep", "add", blocked.ID, blocker.ID)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("dep add failed: %v\n%s", err, out)
}
cmd = exec.Command(bd, "ready")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
// The blocked issue should not appear in ready output
if strings.Contains(stdout.String(), "Blocked by blocker") {
t.Errorf("blocked issue should not appear in ready output: %s", stdout.String())
}
})
// ===== Exclude Label =====
t.Run("ready_exclude_label", func(t *testing.T) {
bdCreate(t, bd, dir, "Triage pending item", "--type", "task", "--label", "triage:pending")
bdCreate(t, bd, dir, "Normal ready item", "--type", "task")
cmd := exec.Command(bd, "ready", "--exclude-label", "triage:pending")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --exclude-label failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if strings.Contains(stdout.String(), "Triage pending item") {
t.Errorf("triage:pending issue should not appear with --exclude-label: %s", stdout.String())
}
if !strings.Contains(stdout.String(), "Normal ready item") {
t.Errorf("normal issue should still appear with --exclude-label: %s", stdout.String())
}
})
// ===== Label Any =====
readyIDs := func(t *testing.T, args ...string) ([]string, string) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd %s failed: %v\nstdout:\n%s\nstderr:\n%s",
strings.Join(args, " "), err, stdout.String(), stderr.String())
}
var issues []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &issues); err != nil {
t.Fatalf("parse JSON: %v\n%s", err, stdout.String())
}
ids := make([]string, 0, len(issues))
for _, issue := range issues {
ids = append(ids, issue.ID)
}
return ids, stdout.String()
}
t.Run("ready_label_any", func(t *testing.T) {
laneA := bdCreate(t, bd, dir, "Lane A work", "--type", "task", "--label", "lany:lane-a")
bdCreate(t, bd, dir, "Lane B work", "--type", "task", "--label", "lany:lane-b")
laneC := bdCreate(t, bd, dir, "Lane C work", "--type", "task", "--label", "lany:lane-c")
ids, raw := readyIDs(t, "ready", "--json", "--label-any", "lany:lane-a,lany:lane-c")
want := map[string]bool{laneA.ID: true, laneC.ID: true}
if len(ids) != len(want) {
t.Fatalf("--label-any returned %d issues, want %d — an unfiltered ready set means the OR-set clause was dropped: %s",
len(ids), len(want), raw)
}
for _, id := range ids {
if !want[id] {
t.Fatalf("--label-any returned %s, which carries neither requested label: %s", id, raw)
}
}
})
t.Run("ready_label_any_intersects_with_label", func(t *testing.T) {
both := bdCreate(t, bd, dir, "Tiered lane A work", "--type", "task",
"--label", "lmix:tier", "--label", "lmix:lane-a")
bdCreate(t, bd, dir, "Untiered lane A work", "--type", "task", "--label", "lmix:lane-a")
bdCreate(t, bd, dir, "Tiered lane B work", "--type", "task",
"--label", "lmix:tier", "--label", "lmix:lane-b")
ids, raw := readyIDs(t, "ready", "--json",
"--label", "lmix:tier", "--label-any", "lmix:lane-a,lmix:lane-c")
if len(ids) != 1 || ids[0] != both.ID {
t.Fatalf("--label with --label-any returned %v, want only %s (AND-set and OR-set must both apply): %s",
ids, both.ID, raw)
}
})
t.Run("ready_claim_label_any_fences_to_its_lane", func(t *testing.T) {
// An exhausted lane must claim nothing. If --label-any is dropped here
// the claim silently takes unfenced work while the caller believes it
// is fenced to its own lane.
bdCreate(t, bd, dir, "Fence lane B work", "--type", "task", "--label", "lfence:lane-b")
ids, raw := readyIDs(t, "ready", "--claim", "--json", "--label-any", "lfence:lane-a")
if len(ids) != 0 {
t.Fatalf("--claim --label-any on an empty lane claimed %v, want nothing: %s", ids, raw)
}
})
// ===== -C flag =====
t.Run("ready_with_C_flag", func(t *testing.T) {
// Run bd ready from a different directory using -C to point at the beads project
tmpDir := t.TempDir()
cmd := exec.Command(bd, "-C", dir, "ready")
cmd.Dir = tmpDir // Run from a directory with no .beads/
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd -C %s ready failed: %v\nstdout:\n%s\nstderr:\n%s", dir, err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Ready test issue") {
t.Errorf("expected issue in ready -C output: %s", stdout.String())
}
})
t.Run("ready_with_C_flag_invalid_path", func(t *testing.T) {
tmpDir := t.TempDir()
cmd := exec.Command(bd, "-C", filepath.Join(tmpDir, "missing"), "ready")
cmd.Dir = tmpDir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd -C missing ready succeeded unexpectedly:\n%s", out)
}
if !strings.Contains(string(out), "cannot use -C directory") {
t.Errorf("expected invalid -C path error, got: %s", out)
}
})
t.Run("ready_with_C_flag_does_not_leak_cwd", func(t *testing.T) {
// Verify that -C does not permanently mutate the process cwd.
// Two sequential invocations from the same tmpDir: the first uses -C to
// reach the project; the second omits -C and must fail (no .beads/ in tmpDir),
// proving BEADS_DIR was not leaked into the test process environment.
tmpDir := t.TempDir()
env := bdEnv(dir) // strips all BEADS_* vars
cmd1 := exec.Command(bd, "-C", dir, "ready")
cmd1.Dir = tmpDir
cmd1.Env = env
if out, err := cmd1.CombinedOutput(); err != nil {
t.Fatalf("first bd -C ready failed: %v\n%s", err, out)
}
cmd2 := exec.Command(bd, "ready")
cmd2.Dir = tmpDir
cmd2.Env = env // same env — BEADS_DIR must not have leaked
out2, err2 := cmd2.CombinedOutput()
if err2 == nil {
t.Fatalf("second bd ready (no -C) should have failed in tmpDir, got: %s", out2)
}
})
t.Run("offset_rejected_outside_proxied", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--offset", "1")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd ready --offset 1 in embedded mode should have failed, got: %s", out)
}
if !strings.Contains(string(out), "--offset is only supported under --proxied-server") {
t.Errorf("expected '--offset is only supported under --proxied-server' error, got: %s", out)
}
})
// Only the proxied route pages, so only it validates --offset. Outside
// proxied mode a negative value has always been ignored, not rejected:
// the RunE rejects --offset > 0 as proxied-only and never reads the flag
// again. Pinned live because the shared flag gatherer is one edit away
// from turning it into an exit-1 usage error.
// The cap is resolved before the sort policy is validated, so a typo'd
// BEADS_MAX_ROWS still tells the user their cap is being ignored even
// though the command is about to fail for an unrelated reason. Pinned
// live: the warning is a side effect of resolving, so it disappears the
// moment the resolution moves later in the sequence.
t.Run("malformed_max_rows_env_warns_before_the_sort_error", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--sort", "bogus")
cmd.Dir = dir
cmd.Env = append(bdEnv(dir), "BEADS_MAX_ROWS=bogus")
stdout, stderr, err := runCommandBuffers(t, cmd)
if err == nil {
t.Fatalf("bd ready --sort bogus should have failed; stdout:\n%s\nstderr:\n%s", stdout.String(), stderr.String())
}
warning := strings.Index(stderr.String(), "BEADS_MAX_ROWS=\"bogus\" is not a non-negative integer")
sortErr := strings.Index(stderr.String(), "invalid sort policy")
switch {
case warning < 0:
t.Errorf("expected the BEADS_MAX_ROWS warning, got stderr:\n%s", stderr.String())
case sortErr < 0:
t.Errorf("expected the sort-policy error, got stderr:\n%s", stderr.String())
case warning > sortErr:
t.Errorf("warning must precede the sort error, got stderr:\n%s", stderr.String())
}
})
t.Run("negative_offset_ignored_outside_proxied", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--offset", "-1")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --offset -1 in embedded mode should have listed ready work: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Ready test issue") {
t.Errorf("expected ready work in output, got: %s", stdout.String())
}
})
}
func TestEmbeddedReadyConcurrent(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", "rx")
bdCreate(t, bd, dir, "Ready concurrent issue", "--type", "task")
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}
cmd := exec.Command(bd, "ready")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("ready (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") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}
func TestEmbeddedReadyClaimConcurrent(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", "rc")
issue := bdCreate(t, bd, dir, "Ready claim concurrent issue", "--type", "task")
const numWorkers = 8
type workerResult struct {
worker int
claimed []types.IssueWithCounts
err error
out string
}
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}
out, err := bdRunWithFlockRetry(t, bd, dir, "ready", "--claim", "--json")
r.out = string(out)
if err != nil {
r.err = fmt.Errorf("ready --claim (worker %d): %v\n%s", worker, err, out)
results[worker] = r
return
}
if err := json.Unmarshal(bytes.TrimSpace(out), &r.claimed); err != nil {
r.err = fmt.Errorf("parse ready --claim JSON (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
claimCount := 0
for _, r := range results {
if r.err != nil {
t.Errorf("worker %d failed: %v", r.worker, r.err)
continue
}
if len(r.claimed) > 1 {
t.Errorf("worker %d claimed %d issues: %s", r.worker, len(r.claimed), r.out)
continue
}
if len(r.claimed) == 1 {
claimCount++
if r.claimed[0].ID != issue.ID {
t.Errorf("worker %d claimed %s, want %s", r.worker, r.claimed[0].ID, issue.ID)
}
}
}
if claimCount != 1 {
t.Fatalf("expected exactly one successful claim, got %d", claimCount)
}
got := bdShow(t, bd, dir, issue.ID)
if got.Status != types.StatusInProgress {
t.Fatalf("final status = %s, want %s", got.Status, types.StatusInProgress)
}
if got.Assignee == "" {
t.Fatal("expected final assignee to be set")
}
}