fix: simplify parallel map using channels (#582)

This commit is contained in:
Craig Andrews
2025-07-03 11:43:10 +01:00
committed by GitHub
parent 2ace57404b
commit 73c012c76c
2 changed files with 45 additions and 32 deletions

View File

@@ -0,0 +1,23 @@
package util_test
import (
"strconv"
"strings"
"testing"
"time"
"github.com/sst/opencode/internal/util"
)
func TestWriteStringsPar(t *testing.T) {
items := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
sb := strings.Builder{}
util.WriteStringsPar(&sb, items, func(i int) string {
// sleep for the inverse duration so that later items finish first
time.Sleep(time.Duration(10-i) * time.Millisecond)
return strconv.Itoa(i)
})
if sb.String() != "0123456789" {
t.Fatalf("expected 0123456789, got %s", sb.String())
}
}