diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b4579106..0c1c4966a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,8 +19,15 @@ jobs: steps: - uses: actions/checkout@v2 - name: test & coverage report creation + # Some tests, notably TestRandomOperations, are extremely slow in CI + # with the race detector enabled, so we use -short when -race is + # enabled to reduce the number of slow tests, and then run without + # -short with -race disabled for a larger test set. + # + # We still run the same tests, just on smaller data sets with -short. run: | - go test ./... -mod=readonly -timeout 8m -race -coverprofile=coverage.txt -covermode=atomic + go test ./... -mod=readonly -timeout 5m -short -race -coverprofile=coverage.txt -covermode=atomic + go test ./... -mod=readonly -timeout 5m - uses: codecov/codecov-action@v1 with: file: ./coverage.txt diff --git a/export_test.go b/export_test.go index 45bdc4b81..2b27a7137 100644 --- a/export_test.go +++ b/export_test.go @@ -171,23 +171,26 @@ func TestExporter(t *testing.T) { } func TestExporter_Import(t *testing.T) { - testcases := map[string]struct { - tree *ImmutableTree - }{ - "empty tree": {tree: NewImmutableTree(db.NewMemDB(), 0)}, - "basic tree": {tree: setupExportTreeBasic(t)}, - "sized tree": {tree: setupExportTreeSized(t, 4096)}, - "random tree": {tree: setupExportTreeRandom(t)}, + testcases := map[string]*ImmutableTree{ + "empty tree": NewImmutableTree(db.NewMemDB(), 0), + "basic tree": setupExportTreeBasic(t), } - for desc, tc := range testcases { - tc := tc // appease scopelint + if !testing.Short() { + testcases["sized tree"] = setupExportTreeSized(t, 4096) + testcases["random tree"] = setupExportTreeRandom(t) + } + + for desc, tree := range testcases { + tree := tree t.Run(desc, func(t *testing.T) { - exporter := tc.tree.Export() + t.Parallel() + + exporter := tree.Export() defer exporter.Close() newTree, err := NewMutableTree(db.NewMemDB(), 0) require.NoError(t, err) - importer, err := newTree.Import(tc.tree.Version()) + importer, err := newTree.Import(tree.Version()) require.NoError(t, err) defer importer.Close() @@ -203,12 +206,12 @@ func TestExporter_Import(t *testing.T) { require.NoError(t, err) } - require.Equal(t, tc.tree.Hash(), newTree.Hash(), "Tree hash mismatch") - require.Equal(t, tc.tree.Size(), newTree.Size(), "Tree size mismatch") - require.Equal(t, tc.tree.Version(), newTree.Version(), "Tree version mismatch") + require.Equal(t, tree.Hash(), newTree.Hash(), "Tree hash mismatch") + require.Equal(t, tree.Size(), newTree.Size(), "Tree size mismatch") + require.Equal(t, tree.Version(), newTree.Version(), "Tree version mismatch") - tc.tree.Iterate(func(key, value []byte) bool { - index, _ := tc.tree.Get(key) + tree.Iterate(func(key, value []byte) bool { + index, _ := tree.Get(key) newIndex, newValue := newTree.Get(key) require.Equal(t, index, newIndex, "Index mismatch for key %v", key) require.Equal(t, value, newValue, "Value mismatch for key %v", key) diff --git a/tree_random_test.go b/tree_random_test.go index bcdfff2af..29e2fd425 100644 --- a/tree_random_test.go +++ b/tree_random_test.go @@ -15,6 +15,8 @@ import ( ) func TestRandomOperations(t *testing.T) { + // In short mode (specifically, when running in CI with the race detector), + // we only run the first couple of seeds. seeds := []int64{ 498727689, 756509998, @@ -28,9 +30,13 @@ func TestRandomOperations(t *testing.T) { 957382170, } - for _, seed := range seeds { - seed := seed + for i, seed := range seeds { + i, seed := i, seed t.Run(fmt.Sprintf("Seed %v", seed), func(t *testing.T) { + if testing.Short() && i >= 2 { + t.Skip("Skipping seed in short mode") + } + t.Parallel() // comment out to disable parallel tests, or use -parallel 1 testRandomOperations(t, seed) }) }