Skip to content

Commit adeddb5

Browse files
committed
explain: fix rare flake in recently added test
Recently added `TestMaximumMemoryUsage` can encounter a rare flake when some metamorphic variables are set - the ComponentStats proto might be dropped from the trace, so we won't have "maximum memory usage" stat that the test expects. Fix this by adding a retry loop. Release note: None
1 parent 3647a54 commit adeddb5

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

pkg/sql/opt/exec/explain/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ go_test(
8989
"//pkg/util/log",
9090
"//pkg/util/treeprinter",
9191
"@com_github_cockroachdb_datadriven//:datadriven",
92+
"@com_github_cockroachdb_errors//:errors",
9293
"@com_github_stretchr_testify//assert",
9394
"@com_github_stretchr_testify//require",
9495
"@in_gopkg_yaml_v2//:yaml_v2",

pkg/sql/opt/exec/explain/output_test.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
3131
"github.com/cockroachdb/cockroach/pkg/util/log"
3232
"github.com/cockroachdb/datadriven"
33+
"github.com/cockroachdb/errors"
3334
"github.com/stretchr/testify/assert"
3435
"github.com/stretchr/testify/require"
3536
yaml "gopkg.in/yaml.v2"
@@ -417,19 +418,29 @@ func TestMaximumMemoryUsage(t *testing.T) {
417418
return err
418419
})
419420

420-
rows := db.QueryStr(t, "EXPLAIN ANALYZE SELECT max(v) FROM t GROUP BY bucket;")
421-
var output strings.Builder
422-
maxMemoryRE := regexp.MustCompile(`maximum memory usage: ([\d\.]+) MiB`)
423-
var maxMemoryUsage float64
424-
for _, row := range rows {
425-
output.WriteString(row[0])
426-
output.WriteString("\n")
427-
s := strings.TrimSpace(row[0])
428-
if matches := maxMemoryRE.FindStringSubmatch(s); len(matches) > 0 {
429-
var err error
430-
maxMemoryUsage, err = strconv.ParseFloat(matches[1], 64)
431-
require.NoError(t, err)
421+
// In rare cases (due to metamorphic randomization) we might drop the
422+
// ComponentStats proto that powers "maximum memory usage" stat from the
423+
// trace, so we add a retry loop around this.
424+
testutils.SucceedsSoon(t, func() error {
425+
rows := db.QueryStr(t, "EXPLAIN ANALYZE SELECT max(v) FROM t GROUP BY bucket;")
426+
var output strings.Builder
427+
maxMemoryRE := regexp.MustCompile(`maximum memory usage: ([\d\.]+) MiB`)
428+
var maxMemoryUsage float64
429+
for _, row := range rows {
430+
output.WriteString(row[0])
431+
output.WriteString("\n")
432+
s := strings.TrimSpace(row[0])
433+
if matches := maxMemoryRE.FindStringSubmatch(s); len(matches) > 0 {
434+
var err error
435+
maxMemoryUsage, err = strconv.ParseFloat(matches[1], 64)
436+
if err != nil {
437+
return err
438+
}
439+
}
432440
}
433-
}
434-
require.Greaterf(t, maxMemoryUsage, 5.0, "expected maximum memory usage to be at least 5 MiB, full output:\n\n%s", output.String())
441+
if maxMemoryUsage < 5.0 {
442+
return errors.Newf("expected maximum memory usage to be at least 5 MiB, full output:\n\n%s", output.String())
443+
}
444+
return nil
445+
})
435446
}

0 commit comments

Comments
 (0)