Skip to content

Commit

Permalink
internal/mod/zip: make all tests parallel
Browse files Browse the repository at this point in the history
I'm not sure why x/mod/zip only makes the sub-tests parallel.
Two of these tests deal with very large zip files,
but they don't create those large files in in-memory buffers;
the Create test writes to io.Discard, and Unzip uses temporary files.

Moreover, their subtests were already parallel, so we were already
creating multiple such large files at once anyway.

This helps slightly with `go test`, dropping from 2.9s to 1.6s
while CPU usage increases from 150% to 280% on my laptop.
The real difference is with `go test -race`, which we use in CI:
I see a drop from 80s to 43s, again using multiple CPUs better.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I92cd8828ae2b11a7b83568c6145e020e3b54d754
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1170018
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mvdan committed Sep 27, 2023
1 parent 6c51cf0 commit b004dd6
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/mod/zip/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func formatCheckedFiles(cf modzip.CheckedFiles) string {
}

func TestCheckFilesWithDirWithTrailingSlash(t *testing.T) {
t.Parallel()
// When checking a zip file,
files := []fakeFile{{
name: "cue.mod/",
Expand All @@ -221,6 +222,7 @@ func TestCheckFilesWithDirWithTrailingSlash(t *testing.T) {
// focuses on how multiple errors and omissions are reported, rather than trying
// to cover every case.
func TestCheckFiles(t *testing.T) {
t.Parallel()
testPaths, err := filepath.Glob(filepath.FromSlash("testdata/check_files/*.txt"))
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -274,6 +276,7 @@ func TestCheckFiles(t *testing.T) {
// test focuses on how multiple errors and omissions are reported, rather than
// trying to cover every case.
func TestCheckDir(t *testing.T) {
t.Parallel()
testPaths, err := filepath.Glob(filepath.FromSlash("testdata/check_dir/*.txt"))
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -324,6 +327,7 @@ func TestCheckDir(t *testing.T) {
// covered by TestUnzip, so this test focuses on how multiple errors are
// reported, rather than trying to cover every case.
func TestCheckZip(t *testing.T) {
t.Parallel()
testPaths, err := filepath.Glob(filepath.FromSlash("testdata/check_zip/*.txt"))
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -373,6 +377,7 @@ func TestCheckZip(t *testing.T) {
}

func TestCreate(t *testing.T) {
t.Parallel()
testDir := filepath.FromSlash("testdata/create")
testInfos, err := os.ReadDir(testDir)
if err != nil {
Expand Down Expand Up @@ -458,6 +463,7 @@ func shouldExclude(f *zip.File) bool {
}

func TestCreateFromDir(t *testing.T) {
t.Parallel()
testDir := filepath.FromSlash("testdata/create_from_dir")
testInfos, err := os.ReadDir(testDir)
if err != nil {
Expand Down Expand Up @@ -513,6 +519,7 @@ func TestCreateFromDir(t *testing.T) {
}

func TestCreateFromDirSpecial(t *testing.T) {
t.Parallel()
for _, test := range []struct {
desc string
setup func(t *testing.T, tmpDir string) string
Expand Down Expand Up @@ -589,6 +596,7 @@ func TestCreateFromDirSpecial(t *testing.T) {
}

func TestUnzip(t *testing.T) {
t.Parallel()
testDir := filepath.FromSlash("testdata/unzip")
testInfos, err := os.ReadDir(testDir)
if err != nil {
Expand Down Expand Up @@ -714,6 +722,7 @@ func TestCreateSizeLimits(t *testing.T) {
if testing.Short() {
t.Skip("creating large files takes time")
}
t.Parallel()
tests := append(sizeLimitTests[:], sizeLimitTest{
// negative file size may happen when size is represented as uint64
// but is cast to int64, as is the case in zip files.
Expand Down Expand Up @@ -775,6 +784,7 @@ func TestUnzipSizeLimits(t *testing.T) {
if testing.Short() {
t.Skip("creating large files takes time")
}
t.Parallel()
for _, test := range sizeLimitTests {
test := test
t.Run(test.desc, func(t *testing.T) {
Expand Down Expand Up @@ -842,6 +852,7 @@ func TestUnzipSizeLimitsSpecial(t *testing.T) {
t.Skip("skipping test; creating large files takes time")
}

t.Parallel()
for _, test := range []struct {
desc string
wantErr string
Expand Down

0 comments on commit b004dd6

Please sign in to comment.