Skip to content

Commit

Permalink
opt: make max-stack opt tester option more reliable
Browse files Browse the repository at this point in the history
The `max-stack` opt tester option now run the test command in a separate
goroutine. A fresh stack makes tests using this setting more reliable.

It also reverts the increase of `max-stack` of a test from cockroachdb#133241
because it is no longer necessary.

Release note: None
  • Loading branch information
mgartner committed Nov 5, 2024
1 parent 9fb7348 commit dc2802e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 21 additions & 5 deletions pkg/sql/opt/testutils/opttester/opt_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"testing"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -566,6 +567,26 @@ func New(catalog cat.Catalog, sqlStr string) *OptTester {
// - max-stack: sets the maximum stack size for the goroutine that optimizes
// the query. See debug.SetMaxStack.
func (ot *OptTester) RunCommand(tb testing.TB, d *datadriven.TestData) string {
if ot.Flags.MaxStackBytes > 0 {
originalMaxStack := debug.SetMaxStack(ot.Flags.MaxStackBytes)
defer debug.SetMaxStack(originalMaxStack)
// Spawn a separate goroutine. A fresh stack makes tests using this
// setting more reliable.
var wg sync.WaitGroup
wg.Add(1)
var res string
go func() {
defer wg.Done()
res = ot.runCommandInternal(tb, d)
}()
wg.Wait()
return res
} else {
return ot.runCommandInternal(tb, d)
}
}

func (ot *OptTester) runCommandInternal(tb testing.TB, d *datadriven.TestData) string {
// Allow testcases to override the flags.
for _, a := range d.CmdArgs {
if err := ot.Flags.Set(a); err != nil {
Expand All @@ -588,11 +609,6 @@ func (ot *OptTester) RunCommand(tb testing.TB, d *datadriven.TestData) string {
ot.evalCtx.Placeholders = nil
ot.evalCtx.TxnIsoLevel = ot.Flags.TxnIsoLevel

if ot.Flags.MaxStackBytes > 0 {
originalMaxStack := debug.SetMaxStack(ot.Flags.MaxStackBytes)
defer debug.SetMaxStack(originalMaxStack)
}

switch d.Cmd {
case "exec-ddl":
testCatalog, ok := ot.catalog.(*testcat.Catalog)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/xform/testdata/rules/select
Original file line number Diff line number Diff line change
Expand Up @@ -2820,7 +2820,7 @@ CREATE TABLE t132669 (
# unnecessary recursion to trigger a stack overflow without having to make the
# `IN` list below huge - triggering a stack overflow with Go's default max stack
# size requires a list of ~1.6 million elements.
opt max-stack=125KB format=hide-all
opt max-stack=50KB format=hide-all
SELECT * FROM t132669
WHERE a IN (
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Expand Down

0 comments on commit dc2802e

Please sign in to comment.