Skip to content

Commit 34db331

Browse files
author
David Cavazos
committed
print outputs immediately
1 parent 9a7d172 commit 34db331

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

.github/workflows/samples-tools/cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func runAllCmd(configFile string, script string) {
140140
fmt.Fprintf(os.Stderr, "❌ error finding packages.\n%v\n", err)
141141
}
142142

143-
maxGoroutines := 32
143+
maxGoroutines := 16
144144
failed := runAll(packages, script, maxGoroutines)
145145

146146
fmt.Printf(strings.Repeat("-", 80) + "\n")

.github/workflows/samples-tools/cmd/runAll.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,21 @@ import (
2525

2626
// runAll runs all the commands in parallel.
2727
func runAll(packages []string, script string, maxGoroutines int) int {
28-
failures := mapParallel(maxGoroutines, packages, func(pkg string) string {
29-
// TODO(dcavazos): measure time spent on each test and print it along the results
30-
output, err := exec.Command("bash", script, pkg).CombinedOutput()
31-
if err != nil {
32-
fmt.Fprintf(os.Stderr, "%v\n❌ FAILED: %v\n%v\n%v\n%v\n\n",
33-
strings.Repeat("=", 80),
34-
pkg,
35-
strings.Repeat("-", 80),
36-
err,
37-
string(output),
38-
)
39-
return pkg
40-
} else {
41-
fmt.Printf("✅ %v\n", pkg)
42-
}
43-
return ""
44-
})
28+
failures := make([]string, len(packages))
29+
guard := make(chan struct{}, maxGoroutines)
30+
for i, pkg := range packages {
31+
guard <- struct{}{} // block if channel is filled
32+
go func() {
33+
output, err := exec.Command("bash", script, pkg).CombinedOutput()
34+
if err != nil {
35+
printError(os.Stderr, pkg, err, string(output))
36+
failures[i] = pkg
37+
} else {
38+
fmt.Printf("✅ %v\n", pkg)
39+
}
40+
<-guard
41+
}()
42+
}
4543

4644
failed := 0
4745
for _, failure := range failures {
@@ -52,16 +50,12 @@ func runAll(packages []string, script string, maxGoroutines int) int {
5250
return failed
5351
}
5452

55-
// mapParallel applies a function in parallel to each item in a slice.
56-
func mapParallel[a, b any](maxGoroutines int, items []a, fn func(a) b) []b {
57-
result := make([]b, len(items))
58-
guard := make(chan struct{}, maxGoroutines)
59-
for i, item := range items {
60-
guard <- struct{}{} // block if channel is filled
61-
go func(i int, item a) {
62-
result[i] = fn(item)
63-
<-guard
64-
}(i, item)
65-
}
66-
return result
53+
func printError(f *os.File, pkg string, err error, output string) {
54+
fmt.Fprintf(f, "%v\n❌ FAILED: %v\n%v\n%v\n%v\n\n",
55+
strings.Repeat("=", 80),
56+
pkg,
57+
strings.Repeat("-", 80),
58+
err,
59+
string(output),
60+
)
6761
}

0 commit comments

Comments
 (0)