From 7fe039cf6a316d960d45d31e4f25c415e297f5c5 Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Sat, 23 Nov 2024 00:45:23 +0000 Subject: [PATCH] fix(blueprint-test): add goroutines max --- infra/blueprint-test/pkg/golden/golden.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/infra/blueprint-test/pkg/golden/golden.go b/infra/blueprint-test/pkg/golden/golden.go index ee0590cd0af..fd79d4dba8a 100644 --- a/infra/blueprint-test/pkg/golden/golden.go +++ b/infra/blueprint-test/pkg/golden/golden.go @@ -22,19 +22,20 @@ import ( "os" "path" "strings" - "sync" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils" "github.com/mitchellh/go-testing-interface" "github.com/stretchr/testify/assert" "github.com/tidwall/gjson" + "golang.org/x/sync/errgroup" ) const ( gfDir = "testdata" gfPerms = 0755 gfUpdateEnvVar = "UPDATE_GOLDEN" + gGoroutinesMax = 24 ) type GoldenFile struct { @@ -160,13 +161,17 @@ func (g *GoldenFile) JSONEq(a *assert.Assertions, got gjson.Result, jsonPath str // JSONPathEqs asserts that json content in jsonPaths for got and goldenfile are the same func (g *GoldenFile) JSONPathEqs(a *assert.Assertions, got gjson.Result, jsonPaths []string) { - var wg sync.WaitGroup - wg.Add(len(jsonPaths)) - for _, path := range jsonPaths { - go func(a *assert.Assertions, got gjson.Result, path string) { - defer wg.Done() - g.JSONEq(a, got, path) - }(a, got, path) + syncGroup := new(errgroup.Group) + syncGroup.SetLimit(gGoroutinesMax) + g.t.Logf("Checking %d JSON paths with max %d goroutines", len(jsonPaths), gGoroutinesMax) + for _, jsonPath := range jsonPaths { + jsonPath := jsonPath + syncGroup.Go(func() error { + g.JSONEq(a, got, jsonPath) + return nil + }) + } + if err := syncGroup.Wait(); err != nil { + g.t.Fatal(err) } - wg.Wait() }