package thin import ( "context" "crypto/rand" "encoding/hex" "os" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/runner/workspace" ) // Astra round 2: streaming the candidate into the checks tree's .git // never holds the serial step worker. If index-pack fails early (a full // disk) the upstream commands are cancelled and reaped instead of // blocking on an undrained pipe; if it stalls, the preparation deadline // ends it. Either way checkRepository returns an error promptly. func TestCheckRepositoryPackFailureNeverHangs(t *testing.T) { root := t.TempDir() work := filepath.Join(root, "work") gitT(t, root, "init", "-q", "-b", "main", work) // 4 MiB that does not compress: far beyond any pipe buffer. raw := make([]byte, 2<<20) rand.Read(raw) os.WriteFile(filepath.Join(work, "noise.txt"), []byte(hex.EncodeToString(raw)), 0644) gitT(t, work, "add", "-A") gitT(t, work, "commit", "-qm", "noise") cand := gitT(t, work, "rev-parse", "HEAD") for _, tc := range []struct{ name, indexPack string }{ {"fails", "exit 1"}, {"stalls", "exec sleep 600"}, } { t.Run(tc.name, func(t *testing.T) { dir := t.TempDir() wrapper := filepath.Join(dir, "git") script := "#!/bin/sh\nfor a in \"$@\"; do [ \"$a\" = index-pack ] && { " + tc.indexPack + "; }; done\nexec /usr/bin/git \"$@\"\n" if err := os.WriteFile(wrapper, []byte(script), 0700); err != nil { t.Fatal(err) } old := prepTimeout prepTimeout = 3 * time.Second t.Cleanup(func() { prepTimeout = old }) tree := workspace.TaskTree{Dir: filepath.Join(dir, "tree"), GitDir: filepath.Join(work, ".git"), Git: wrapper, Branch: "equipo/tk1-x", Base: cand} os.MkdirAll(tree.Dir, 0700) e := &Engineer{} e.O.Runner.GitPath = wrapper done := make(chan error, 1) start := time.Now() go func() { done <- e.checkRepository(context.Background(), tree, cand) }() select { case err := <-done: if err == nil { t.Fatal("a failed index-pack was taken for a checks repository") } t.Logf("refused after %s: %v", time.Since(start).Round(time.Millisecond), err) case <-time.After(30 * time.Second): t.Fatal("checkRepository hung on a failed index-pack") } }) } }