Skip to content

Commit 2d182a5

Browse files
committed
all: make some txtar table tests parallel
Drops `GORACE=atexit_sleep_ms=10 go test -race` time on each of these packages as follows, as measured on my laptop: * encoding/jsonschema: from 5.8s to 2.4s * encoding/openapi: from 550ms to 350ms * internal/core/compile: from 650ms to 450ms * internal/core/export: from 550ms to 300ms * internal/mod/modload: from 550ms to 350ms Every bit helps, even when we're talking about fractions of a second, as these are the slower packages when testing `./...`. Given that CI has 8 CPUs and we have a concurrency limit, it's best to use all the CPUs as best we can. The savings are not just for `-race` either; for example, `go test` on encoding/jsonschema still drops from 1.4s to 0.6s without `-race`. However, the lack of parallelism on CPU-intensive tests is made much more apparent by the use of `go test -race`. CI is also at least twice as slow as my laptop, so a total saving of ~4s here should equate to at least 8s shaved off of CI with `-race`. Even the CI jobs using regular `go test` should save a second or two. Note that in the export package, we also had to fix two minor bugs where the tests were modifying globals rather than making copies and modifying those copies. This is a better approach even without parallelism, as otherwise we might alter the global state across tests. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I47191d3e40022b4165b63a17aa25654fbccd1acd Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1218328 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 3114a2f commit 2d182a5

File tree

10 files changed

+27
-2
lines changed

10 files changed

+27
-2
lines changed

encoding/jsonschema/decode_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ import (
6767
// As a special case, when this is "openapi", OpenAPI extraction
6868
// mode is enabled.
6969
func TestDecode(t *testing.T) {
70+
t.Parallel()
7071
test := cuetxtar.TxTarTest{
7172
Root: "./testdata/txtar",
7273
Name: "decode",
7374
Matrix: cuetdtest.FullMatrix,
7475
}
7576
test.Run(t, func(t *cuetxtar.Test) {
77+
t.Parallel()
7678
ctx := t.CueContext()
7779

7880
fsys, err := txtar.FS(t.Archive)
@@ -237,6 +239,7 @@ func TestDecode(t *testing.T) {
237239
}
238240

239241
func TestDecodeCRD(t *testing.T) {
242+
t.Parallel()
240243
test := cuetxtar.TxTarTest{
241244
Root: "./testdata/txtar",
242245
Name: "decodeCRD",
@@ -246,6 +249,7 @@ func TestDecodeCRD(t *testing.T) {
246249
if versStr, ok := t.Value("version"); !ok || versStr != "k8sCRD" {
247250
t.Skip("test not relevant to CRDs")
248251
}
252+
t.Parallel()
249253

250254
ctx := t.CueContext()
251255

@@ -308,6 +312,7 @@ func readSchema(ctx *cue.Context, fsys fs.FS) (cue.Value, error) {
308312
}
309313

310314
func TestMapURL(t *testing.T) {
315+
t.Parallel()
311316
v := cuecontext.New().CompileString(`
312317
type: "object"
313318
properties: x: $ref: "https://something.test/foo#/definitions/blah"
@@ -355,6 +360,7 @@ cannot determine CUE location for JSON Schema location id=https://something.test
355360
}
356361

357362
func TestMapRef(t *testing.T) {
363+
t.Parallel()
358364
v := cuecontext.New().CompileString(`
359365
type: "object"
360366
$id: "https://this.test"
@@ -400,6 +406,7 @@ x?: blah.#Foo.bar
400406
}
401407

402408
func TestMapRefExternalRefForInternalSchema(t *testing.T) {
409+
t.Parallel()
403410
v := cuecontext.New().CompileString(`
404411
type: "object"
405412
$id: "https://this.test"

encoding/jsonschema/external_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ const testDir = "testdata/external"
4949
// TestExternal runs the externally defined JSON Schema test suite,
5050
// as defined in https://github.com/json-schema-org/JSON-Schema-Test-Suite.
5151
func TestExternal(t *testing.T) {
52+
t.Parallel()
5253
tests, err := externaltest.ReadTestDir(testDir)
5354
qt.Assert(t, qt.IsNil(err))
5455

5556
// Group the tests under a single subtest so that we can use
5657
// t.Parallel and still guarantee that all tests have completed
5758
// by the end.
5859
cuetdtest.SmallMatrix.Run(t, "tests", func(t *testing.T, m *cuetdtest.M) {
60+
t.Parallel()
5961
// Run tests in deterministic order so we get some consistency between runs.
6062
for _, filename := range slices.Sorted(maps.Keys(tests)) {
6163
schemas := tests[filename]
6264
t.Run(testName(filename), func(t *testing.T) {
65+
t.Parallel()
6366
for _, s := range schemas {
6467
t.Run(testName(s.Description), func(t *testing.T) {
6568
runExternalSchemaTests(t, m, filename, s)

encoding/openapi/decode_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
//
5050
// Set CUE_UPDATE=1 to update test files with the corresponding output.
5151
func TestDecode(t *testing.T) {
52+
t.Parallel()
5253
err := filepath.WalkDir("testdata/script", func(fullpath string, entry fs.DirEntry, err error) error {
5354
if err != nil {
5455
return err
@@ -58,6 +59,7 @@ func TestDecode(t *testing.T) {
5859
}
5960

6061
t.Run(fullpath, func(t *testing.T) {
62+
t.Parallel()
6163
a, err := txtar.ParseFile(fullpath)
6264
if err != nil {
6365
t.Fatal(err)

encoding/openapi/openapi_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
)
3434

3535
func TestGenerateOpenAPI(t *testing.T) {
36+
t.Parallel()
3637
test := cuetxtar.TxTarTest{
3738
Root: "./testdata",
3839
Name: t.Name(),
@@ -68,6 +69,7 @@ func TestGenerateOpenAPI(t *testing.T) {
6869
}
6970

7071
test.Run(t, func(t *cuetxtar.Test) {
72+
t.Parallel()
7173
a := t.Instance()
7274
ctx := t.CueContext()
7375
v := ctx.BuildInstance(a)

internal/core/compile/compile_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestCompile(t *testing.T) {
4343
}
4444

4545
test.Run(t, func(t *cuetxtar.Test) {
46+
t.Parallel()
4647
r := runtime.New()
4748
// TODO: use high-level API.
4849

internal/core/export/export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ import (
3939
)
4040

4141
func TestDefinition(t *testing.T) {
42+
t.Parallel()
4243
test := cuetxtar.TxTarTest{
4344
Root: "./testdata/main",
4445
Name: "definition",
4546
Matrix: cuetdtest.FullMatrix,
4647
}
4748

4849
test.Run(t, func(t *cuetxtar.Test) {
50+
t.Parallel()
4951
r := t.Runtime()
5052
a := t.Instance()
5153

internal/core/export/self_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
)
3838

3939
func TestSelfContained(t *testing.T) {
40+
t.Parallel()
4041
test := cuetxtar.TxTarTest{
4142
Name: "self",
4243
Root: "./testdata/selfcontained",
@@ -53,6 +54,7 @@ func TestSelfContained(t *testing.T) {
5354
}
5455

5556
test.Run(t, func(t *cuetxtar.Test) {
57+
t.Parallel()
5658
r := t.CueContext()
5759

5860
a := t.Instances()
@@ -141,7 +143,7 @@ func patch(t *testing.T, r *cue.Context, orig *txtar.Archive, f *ast.File) cue.V
141143

142144
func doDiff(t *testing.T, v, w cue.Value) {
143145
var bb bytes.Buffer
144-
p := diff.Schema
146+
p := *diff.Schema
145147
p.SkipHidden = true
146148
d, script := p.Diff(v, w)
147149
if d != diff.Identity {

internal/core/export/value_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var exclude = map[string]string{
3737
}
3838

3939
func TestValue(t *testing.T) {
40+
t.Parallel()
4041
const debugValue = `
4142
-- in.cue --
4243
if false {
@@ -57,6 +58,7 @@ if false {
5758
}
5859

5960
test.Run(t, func(t *cuetxtar.Test) {
61+
t.Parallel()
6062
r := t.Runtime()
6163
a := t.Instance()
6264

@@ -70,7 +72,7 @@ if false {
7072
ctx := eval.NewContext(r, v)
7173
v.Finalize(ctx)
7274

73-
all := export.All
75+
all := *export.All
7476
all.ShowErrors = true
7577

7678
evalWithOptions := export.Profile{

internal/mod/modload/tidy_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import (
2020
)
2121

2222
func TestTidy(t *testing.T) {
23+
t.Parallel()
2324
files, err := filepath.Glob("testdata/tidy/*.txtar")
2425
qt.Assert(t, qt.IsNil(err))
2526
for _, f := range files {
2627
t.Run(f, func(t *testing.T) {
28+
t.Parallel()
2729
ar, err := txtar.ParseFile(f)
2830
qt.Assert(t, qt.IsNil(err))
2931
tfs, err := txtar.FS(ar)

internal/mod/modload/update_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import (
2020
)
2121

2222
func TestUpdateVersions(t *testing.T) {
23+
t.Parallel()
2324
files, err := filepath.Glob("testdata/updateversions/*.txtar")
2425
qt.Assert(t, qt.IsNil(err))
2526
for _, f := range files {
2627
t.Run(f, func(t *testing.T) {
28+
t.Parallel()
2729
ar, err := txtar.ParseFile(f)
2830
qt.Assert(t, qt.IsNil(err))
2931
tfs, err := txtar.FS(ar)

0 commit comments

Comments
 (0)