Skip to content

Commit cd617d4

Browse files
fix(cmd): use selection order for parallel rain repo indexes
Co-authored-by: Ben Schellenberger <bschellenberger2600@users.noreply.github.com>
1 parent e00bcd3 commit cd617d4

2 files changed

Lines changed: 80 additions & 8 deletions

File tree

‎cmd/rain_parallel_test.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"io"
67
"os"
78
"strings"
@@ -67,3 +68,70 @@ func TestRunRainOnRepos_ParallelStallDoesNotBlockHealthy(t *testing.T) {
6768
t.Fatalf("parallel rain took %v; expected ~%ds timeout bound", elapsed, timeoutSec)
6869
}
6970
}
71+
72+
func TestRunRainReposParallel_PreservesSelectionIndex(t *testing.T) {
73+
baseURL, cleanup := startStallTCPListener(t)
74+
defer cleanup()
75+
76+
scenario := testutil.NewScenario(t)
77+
remote := scenario.CreateBareRepo("upstream")
78+
fast := scenario.CreateRepo("repo-alpha").
79+
AddFile("README.md", "ok\n").
80+
Commit("init").
81+
WithRemote("origin", remote)
82+
fast.Push("origin", fast.GetDefaultBranch())
83+
84+
stall := testutil.CreateTestRepo(t, testutil.RepoOptions{
85+
Name: "repo-beta",
86+
Remotes: map[string]string{
87+
"origin": baseURL + "/stall.git",
88+
},
89+
})
90+
91+
repos := []git.Repository{
92+
{Name: "repo-alpha", Path: fast.Path()},
93+
{Name: "repo-beta", Path: stall},
94+
}
95+
96+
oldJSON := rainJSON
97+
rainJSON = true
98+
defer func() { rainJSON = oldJSON }()
99+
100+
oldStdout := os.Stdout
101+
r, w, _ := os.Pipe()
102+
os.Stdout = w
103+
defer func() { os.Stdout = oldStdout }()
104+
105+
totals, reports := runRainReposParallel(&registry.Registry{}, repos, git.RainOptions{FetchTimeoutSec: 2}, false, 2)
106+
_ = w.Close()
107+
var out bytes.Buffer
108+
_, _ = io.Copy(&out, r)
109+
110+
if totals.updated < 1 {
111+
t.Fatalf("expected at least one updated repo, totals=%+v", totals)
112+
}
113+
if len(reports) != 2 {
114+
t.Fatalf("expected 2 json reports, got %d", len(reports))
115+
}
116+
117+
byName := map[string]jsonRepoResult{}
118+
for _, rep := range reports {
119+
byName[rep.Name] = rep
120+
}
121+
if byName["repo-alpha"].Index != 1 || byName["repo-alpha"].Total != 2 {
122+
t.Fatalf("repo-alpha index/total = %d/%d, want 1/2", byName["repo-alpha"].Index, byName["repo-alpha"].Total)
123+
}
124+
if byName["repo-beta"].Index != 2 || byName["repo-beta"].Total != 2 {
125+
t.Fatalf("repo-beta index/total = %d/%d, want 2/2", byName["repo-beta"].Index, byName["repo-beta"].Total)
126+
}
127+
128+
var doc jsonRunDocument
129+
if err := json.Unmarshal([]byte(out.String()), &doc); err == nil && len(doc.Repositories) > 0 {
130+
for _, rep := range doc.Repositories {
131+
want := byName[rep.Name].Index
132+
if rep.Index != want {
133+
t.Fatalf("stdout json repo %s index %d, want %d", rep.Name, rep.Index, want)
134+
}
135+
}
136+
}
137+
}

‎cmd/rain_workers.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@ import (
1111
"github.com/git-rain/git-rain/internal/registry"
1212
)
1313

14+
type parallelRepoJob struct {
15+
repo git.Repository
16+
index int // 1-based position in the selected repos slice
17+
}
18+
1419
// runRainReposParallel runs fetch/rain for repos using the same bounded worker
1520
// pool as the default streaming CLI path.
1621
func runRainReposParallel(reg *registry.Registry, repos []git.Repository, rainOpts git.RainOptions, fullSync bool, fetchWorkers int) (rainTotals, []jsonRepoResult) {
1722
if len(repos) == 0 {
1823
return rainTotals{}, nil
1924
}
2025

26+
total := len(repos)
2127
workers := fetchWorkerCount(fetchWorkers)
22-
repoChan := make(chan git.Repository, len(repos))
23-
for _, repo := range repos {
24-
repoChan <- repo
28+
repoChan := make(chan parallelRepoJob, len(repos))
29+
for i, repo := range repos {
30+
repoChan <- parallelRepoJob{repo: repo, index: i + 1}
2531
}
2632
close(repoChan)
2733

@@ -31,9 +37,7 @@ func runRainReposParallel(reg *registry.Registry, repos []git.Repository, rainOp
3137
skipped int64
3238
frozen int64
3339
failed int64
34-
seq int64
3540
)
36-
total := int64(len(repos))
3741
var jsonReports []jsonRepoResult
3842
var jsonReportsMu sync.Mutex
3943

@@ -43,13 +47,13 @@ func runRainReposParallel(reg *registry.Registry, repos []git.Repository, rainOp
4347
workersWG.Add(1)
4448
go func() {
4549
defer workersWG.Done()
46-
for repo := range repoChan {
50+
for job := range repoChan {
51+
repo := job.repo
4752
if runCtx != nil && runCtx.Err() != nil {
4853
continue
4954
}
50-
current := int(atomic.AddInt64(&seq, 1))
5155
var buf strings.Builder
52-
delta, report := runRainOnRepo(reg, repo, rainOpts, fullSync, current, int(total), &buf, !rainJSON)
56+
delta, report := runRainOnRepo(reg, repo, rainOpts, fullSync, job.index, total, &buf, !rainJSON)
5357

5458
printMu.Lock()
5559
if !rainJSON {

0 commit comments

Comments
 (0)