From e688b9451701a66bb65ff8babc333d2f7cca78a0 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 1 Jul 2021 17:36:26 -0400 Subject: [PATCH] go/packages: parallelize most tests This reduces the overall running time on my workstation from ~44s to ~17s. For golang/go#46764 Change-Id: I94e3c5bf160599687f7aa16513bb7b7e977f14b4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/332350 Trust: Bryan C. Mills Run-TryBot: Bryan C. Mills gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Rebecca Stambler --- go/packages/overlay_test.go | 6 ++++++ go/packages/packages_test.go | 23 ++++++++++++++++++++++- go/packages/packagestest/export.go | 2 ++ go/packages/stdlib_test.go | 2 +- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/go/packages/overlay_test.go b/go/packages/overlay_test.go index 65c7fc9ae8..baa931589d 100644 --- a/go/packages/overlay_test.go +++ b/go/packages/overlay_test.go @@ -496,6 +496,7 @@ func testOverlayNewPackageAndTest(t *testing.T, exporter packagestest.Exporter) } func TestAdHocOverlays(t *testing.T) { + t.Parallel() testenv.NeedsTool(t, "go") // This test doesn't use packagestest because we are testing ad-hoc packages, @@ -551,6 +552,7 @@ const A = 1 // TestOverlayModFileChanges tests the behavior resulting from having files // from multiple modules in overlays. func TestOverlayModFileChanges(t *testing.T) { + t.Parallel() testenv.NeedsTool(t, "go") // Create two unrelated modules in a temporary directory. @@ -620,6 +622,8 @@ func main() {} } func TestOverlayGOPATHVendoring(t *testing.T) { + t.Parallel() + exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/fake", Files: map[string]interface{}{ @@ -1042,6 +1046,8 @@ func Hi() { // This does not use go/packagestest because it needs to write a replace // directive with an absolute path in one of the module's go.mod files. func TestOverlaysInReplace(t *testing.T) { + t.Parallel() + // Create module b.com in a temporary directory. Do not add any Go files // on disk. tmpPkgs, err := ioutil.TempDir("", "modules") diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 963c009dc4..afc0a80b58 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -57,6 +57,7 @@ func TestMain(m *testing.M) { // testAllOrModules tests f against all packagestest exporters in long mode, // but only against the Modules exporter in short mode. func testAllOrModules(t *testing.T, f func(*testing.T, packagestest.Exporter)) { + t.Parallel() packagestest.TestAll(t, func(t *testing.T, exporter packagestest.Exporter) { t.Helper() @@ -70,6 +71,7 @@ func testAllOrModules(t *testing.T, f func(*testing.T, packagestest.Exporter)) { t.Fatalf("unexpected exporter %q", exporter.Name()) } + t.Parallel() f(t, exporter) }) } @@ -95,6 +97,7 @@ func testAllOrModules(t *testing.T, f func(*testing.T, packagestest.Exporter)) { // The zero-value of Config has LoadFiles mode. func TestLoadZeroConfig(t *testing.T) { testenv.NeedsGoPackages(t) + t.Parallel() initial, err := packages.Load(nil, "hash") if err != nil { @@ -328,6 +331,8 @@ func testLoadImportsTestVariants(t *testing.T, exporter packagestest.Exporter) { } func TestLoadAbsolutePath(t *testing.T) { + t.Parallel() + exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/gopatha", Files: map[string]interface{}{ @@ -356,6 +361,8 @@ func TestLoadAbsolutePath(t *testing.T) { } func TestVendorImports(t *testing.T) { + t.Parallel() + exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/fake", Files: map[string]interface{}{ @@ -911,6 +918,8 @@ func testParseFileModifyAST(t *testing.T, exporter packagestest.Exporter) { } func TestAdHocPackagesBadImport(t *testing.T) { + t.Parallel() + // This test doesn't use packagestest because we are testing ad-hoc packages, // which are outside of $GOPATH and outside of a module. tmp, err := ioutil.TempDir("", "a") @@ -1399,6 +1408,8 @@ func testJSON(t *testing.T, exporter packagestest.Exporter) { } func TestRejectInvalidQueries(t *testing.T) { + t.Parallel() + queries := []string{"key=", "key=value"} cfg := &packages.Config{ Mode: packages.LoadImports, @@ -1437,7 +1448,11 @@ func testPatternPassthrough(t *testing.T, exporter packagestest.Exporter) { } -func TestConfigDefaultEnv(t *testing.T) { testAllOrModules(t, testConfigDefaultEnv) } +func TestConfigDefaultEnv(t *testing.T) { + // packagestest.TestAll instead of testAllOrModulesParallel because this test + // can't be parallelized (it modifies the environment). + packagestest.TestAll(t, testConfigDefaultEnv) +} func testConfigDefaultEnv(t *testing.T, exporter packagestest.Exporter) { const driverJSON = `{ "Roots": ["gopackagesdriver"], @@ -1829,6 +1844,7 @@ func TestLoadImportsC(t *testing.T) { // See https://golang.org/issue/27100. t.Skip(`skipping on plan9; for some reason "net [syscall.test]" is not loaded`) } + t.Parallel() testenv.NeedsGoPackages(t) cfg := &packages.Config{ @@ -1887,7 +1903,10 @@ func testCgoNoSyntax(t *testing.T, exporter packagestest.Exporter) { packages.NeedName | packages.NeedImports, } for _, mode := range modes { + mode := mode t.Run(fmt.Sprint(mode), func(t *testing.T) { + t.Parallel() + exported.Config.Mode = mode pkgs, err := packages.Load(exported.Config, "golang.org/fake/c") if err != nil { @@ -2658,6 +2677,8 @@ func main() { } func TestEmptyEnvironment(t *testing.T) { + t.Parallel() + cfg := &packages.Config{ Env: []string{"FOO=BAR"}, } diff --git a/go/packages/packagestest/export.go b/go/packages/packagestest/export.go index 2b93d2c2b9..5dea613f0b 100644 --- a/go/packages/packagestest/export.go +++ b/go/packages/packagestest/export.go @@ -159,6 +159,7 @@ var All []Exporter func TestAll(t *testing.T, f func(*testing.T, Exporter)) { t.Helper() for _, e := range All { + e := e // in case f calls t.Parallel t.Run(e.Name(), func(t *testing.T) { t.Helper() f(t, e) @@ -172,6 +173,7 @@ func TestAll(t *testing.T, f func(*testing.T, Exporter)) { func BenchmarkAll(b *testing.B, f func(*testing.B, Exporter)) { b.Helper() for _, e := range All { + e := e // in case f calls t.Parallel b.Run(e.Name(), func(b *testing.B) { b.Helper() f(b, e) diff --git a/go/packages/stdlib_test.go b/go/packages/stdlib_test.go index 254f459b93..84ea8ada74 100644 --- a/go/packages/stdlib_test.go +++ b/go/packages/stdlib_test.go @@ -21,7 +21,7 @@ import ( func TestStdlibMetadata(t *testing.T) { // TODO(adonovan): see if we can get away without this hack. // if runtime.GOOS == "android" { - // t.Skipf("incomplete std lib on %s", runtime.GOOS) + // t.Skipf("incomplete std lib on %s", runtime.GOOS) // } testenv.NeedsGoPackages(t)