Skip to content

Commit

Permalink
gc: use randutil.NewTestRand() instead of hardcoded seed
Browse files Browse the repository at this point in the history
Previously randomized tests used hardcoded sees 1 for testing.
This is giving us fixed coverage which can't find new failure modes.
This commit changes it to use randutil.NewTestRand() which uses
different seed by default, but could be overridden for failure
reproductions.

Release note: None
  • Loading branch information
aliher1911 committed Jul 19, 2022
1 parent ff663ee commit f971ace
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/kv/kvserver/gc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ go_test(
"gc_old_test.go",
"gc_random_test.go",
"gc_test.go",
"main_test.go",
],
embed = [":gc"],
deps = [
Expand Down
17 changes: 12 additions & 5 deletions pkg/kv/kvserver/gc/gc_random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -94,7 +95,6 @@ const intentAgeThreshold = 2 * time.Hour
// implementation. It runs both the new and old implementation and ensures
// that they produce exactly the same results on the same set of keys.
func TestRunNewVsOld(t *testing.T) {
rng := rand.New(rand.NewSource(1))
ctx := context.Background()
const N = 100000

Expand All @@ -117,6 +117,9 @@ func TestRunNewVsOld(t *testing.T) {
},
} {
t.Run(fmt.Sprintf("%v@%v,ttlSec=%v", tc.ds, tc.now, tc.ttlSec), func(t *testing.T) {
rng, seed := randutil.NewTestRand()
t.Logf("Using subtest seed: %d", seed)

eng := storage.NewDefaultInMemForTesting()
defer eng.Close()

Expand Down Expand Up @@ -152,7 +155,6 @@ func TestRunNewVsOld(t *testing.T) {
// BenchmarkRun benchmarks the old and implementations of Run with different
// data distributions.
func BenchmarkRun(b *testing.B) {
rng := rand.New(rand.NewSource(1))
ctx := context.Background()
runGC := func(eng storage.Engine, old bool, spec randomRunGCTestSpec) (Info, error) {
runGCFunc := Run
Expand All @@ -176,7 +178,7 @@ func BenchmarkRun(b *testing.B) {
return nil
})
}
makeTest := func(old bool, spec randomRunGCTestSpec) func(b *testing.B) {
makeTest := func(old bool, spec randomRunGCTestSpec, rng *rand.Rand) func(b *testing.B) {
return func(b *testing.B) {
eng := storage.NewDefaultInMemForTesting()
defer eng.Close()
Expand Down Expand Up @@ -207,8 +209,11 @@ func BenchmarkRun(b *testing.B) {
specs = append(specs, specsWithTTLs(lotsOfVersionsMidSizeRows, ts100, ttls)...)
for _, old := range []bool{true, false} {
b.Run(fmt.Sprintf("old=%v", old), func(b *testing.B) {
rng, seed := randutil.NewTestRand()
b.Logf("Using benchmark seed: %d", seed)

for _, spec := range specs {
b.Run(fmt.Sprint(spec.ds), makeTest(old, spec))
b.Run(fmt.Sprint(spec.ds), makeTest(old, spec, rng))
}
})
}
Expand Down Expand Up @@ -254,7 +259,9 @@ func TestNewVsInvariants(t *testing.T) {
},
} {
t.Run(fmt.Sprintf("%v@%v,ttl=%vsec", tc.ds, tc.now, tc.ttlSec), func(t *testing.T) {
rng := rand.New(rand.NewSource(1))
rng, seed := randutil.NewTestRand()
t.Logf("Using subtest seed: %d", seed)

desc := tc.ds.desc()
eng := storage.NewDefaultInMemForTesting()
defer eng.Close()
Expand Down
23 changes: 23 additions & 0 deletions pkg/kv/kvserver/gc/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package gc

import (
"os"
"testing"

"github.com/cockroachdb/cockroach/pkg/util/randutil"
)

func TestMain(m *testing.M) {
randutil.SeedForTests()
os.Exit(m.Run())
}

0 comments on commit f971ace

Please sign in to comment.