Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor/cte_test.go: migrate test-infra to testify #27103

Merged
merged 21 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f80fcd5
testkit: introduce func ExecToErr
unconsolable Aug 11, 2021
756d1cd
executor/cte_test.go: migrate test-infra to testify
unconsolable Aug 11, 2021
dae42e5
executor/cte_test.go: defer close in TestSpillToDisk
unconsolable Aug 11, 2021
4040dce
Merge branch 'master' into issue-27097
unconsolable Aug 11, 2021
9eb3b38
executor/cte_test.go: make SetUpSuite before NewTestKit in TestSpillT…
unconsolable Aug 11, 2021
9cb1dd9
executor/cte_test.go: try to remove t.Parallel() to eliminate TLE
unconsolable Aug 11, 2021
55a490e
Merge branch 'master' into issue-27097
unconsolable Aug 11, 2021
3b08087
Merge branch 'master' into issue-27097
unconsolable Aug 14, 2021
7470965
executor/cte_test.go: try to restore t.Parallel()
unconsolable Aug 14, 2021
da35207
Merge branch 'master' into issue-27097
unconsolable Aug 16, 2021
3738658
executor/cte_test.go: remove t.Parallel() in TestCTESuite
unconsolable Aug 16, 2021
93e5d74
executor/cte_test.go: format import, remove global variable, try para…
unconsolable Aug 19, 2021
d686ecb
executor/cte_test.go: remove parallel
unconsolable Aug 19, 2021
c3d8789
executor/cte_test.go: remove suite, increase parallelism
unconsolable Aug 19, 2021
a9e298f
executor/cte_test.go: replace error check to EqualError
unconsolable Aug 19, 2021
456ca87
Merge branch 'master' into issue-27097
ti-chi-bot Aug 20, 2021
cbb248f
Merge branch 'master' into issue-27097
ti-chi-bot Aug 20, 2021
8a59758
Merge branch 'master' into issue-27097
ti-chi-bot Aug 20, 2021
8a5c21d
Merge branch 'master' into issue-27097
unconsolable Aug 23, 2021
dbaac8f
Merge branch 'master' into issue-27097
ti-chi-bot Aug 23, 2021
6abb07c
Merge branch 'master' into issue-27097
unconsolable Aug 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 64 additions & 58 deletions executor/cte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,67 @@ import (
"fmt"
"math/rand"
"sort"
"testing"

"github.com/pingcap/check"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testkit"
"github.com/stretchr/testify/require"
)

var _ = check.Suite(&CTETestSuite{&baseCTETestSuite{}})
var _ = check.SerialSuites(&CTESerialTestSuite{&baseCTETestSuite{}})

type baseCTETestSuite struct {
type CTETestSuite struct {
store kv.Storage
dom *domain.Domain
sessionCtx sessionctx.Context
session session.Session
ctx context.Context
close func()
}

type CTETestSuite struct {
*baseCTETestSuite
}

type CTESerialTestSuite struct {
*baseCTETestSuite
}

func (test *baseCTETestSuite) SetUpSuite(c *check.C) {
func SetUpSuite(t *testing.T) *CTETestSuite {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func SetUpSuite(t *testing.T) *CTETestSuite {
func setUpSuite(t *testing.T) *CTETestSuite {

We don't export it, do we?

var err error
test := new(CTETestSuite)

test.store, err = mockstore.NewMockStore()
c.Assert(err, check.IsNil)
require.NoError(t, err)

test.dom, err = session.BootstrapSession(test.store)
c.Assert(err, check.IsNil)
require.NoError(t, err)

test.sessionCtx = mock.NewContext()

test.session, err = session.CreateSession4Test(test.store)
c.Assert(err, check.IsNil)
require.NoError(t, err)
test.session.SetConnectionID(0)

test.ctx = context.Background()

test.close = func() {
test.dom.Close()
test.store.Close()
}

return test
}

func (test *baseCTETestSuite) TearDownSuite(c *check.C) {
test.dom.Close()
test.store.Close()
func TestCTESuite(t *testing.T) {
cteTestSuite := SetUpSuite(t)
defer cteTestSuite.close()

BasicCTE(t, cteTestSuite)
UnionDistinct(t, cteTestSuite)
CTEMaxRecursionDepth(t, cteTestSuite)
CTEWithLimit(t, cteTestSuite)
}

func (test *CTETestSuite) TestBasicCTE(c *check.C) {
tk := testkit.NewTestKit(c, test.store)
func BasicCTE(t *testing.T, suite *CTETestSuite) {
tk := testkit.NewTestKit(t, suite.store)
tk.MustExec("use test")

rows := tk.MustQuery("with recursive cte1 as (" +
Expand Down Expand Up @@ -121,23 +124,26 @@ func (test *CTETestSuite) TestBasicCTE(c *check.C) {
rows.Check(testkit.Rows("1 1", "2 1", "3 1", "4 1", "5 1"))
}

func (test *CTESerialTestSuite) TestSpillToDisk(c *check.C) {
func TestSpillToDisk(t *testing.T) {
test := SetUpSuite(t)
defer test.close()

defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
})

tk := testkit.NewTestKit(c, test.store)
tk := testkit.NewTestKit(t, test.store)
tk.MustExec("use test;")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testCTEStorageSpill", "return(true)"), check.IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testCTEStorageSpill", "return(true)"))
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/testCTEStorageSpill"), check.IsNil)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testCTEStorageSpill"))
tk.MustExec("set tidb_mem_quota_query = 1073741824;")
}()
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)"), check.IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)"))
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill"), check.IsNil)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill"))
}()

// Use duplicated rows to test UNION DISTINCT.
Expand All @@ -163,10 +169,10 @@ func (test *CTESerialTestSuite) TestSpillToDisk(c *check.C) {
"select c1 from cte1 order by c1;", rowNum)
rows := tk.MustQuery(sql)

memTracker := tk.Se.GetSessionVars().StmtCtx.MemTracker
diskTracker := tk.Se.GetSessionVars().StmtCtx.DiskTracker
c.Assert(memTracker.MaxConsumed(), check.Greater, int64(0))
c.Assert(diskTracker.MaxConsumed(), check.Greater, int64(0))
memTracker := tk.Session().GetSessionVars().StmtCtx.MemTracker
diskTracker := tk.Session().GetSessionVars().StmtCtx.DiskTracker
require.Greater(t, memTracker.MaxConsumed(), int64(0))
require.Greater(t, diskTracker.MaxConsumed(), int64(0))

sort.Ints(vals)
resRows := make([]string, 0, rowNum)
Expand All @@ -176,8 +182,8 @@ func (test *CTESerialTestSuite) TestSpillToDisk(c *check.C) {
rows.Check(testkit.Rows(resRows...))
}

func (test *CTETestSuite) TestUnionDistinct(c *check.C) {
tk := testkit.NewTestKit(c, test.store)
func UnionDistinct(t *testing.T, suite *CTETestSuite) {
tk := testkit.NewTestKit(t, suite.store)
tk.MustExec("use test;")

// Basic test. UNION/UNION ALL intersects.
Expand All @@ -200,14 +206,14 @@ func (test *CTETestSuite) TestUnionDistinct(c *check.C) {
rows.Check(testkit.Rows("1", "2", "3", "4"))
}

func (test *CTETestSuite) TestCTEMaxRecursionDepth(c *check.C) {
tk := testkit.NewTestKit(c, test.store)
func CTEMaxRecursionDepth(t *testing.T, suite *CTETestSuite) {
tk := testkit.NewTestKit(t, suite.store)
tk.MustExec("use test;")

tk.MustExec("set @@cte_max_recursion_depth = -1;")
err := tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 100) select * from cte1;")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value")
require.Error(t, err)
require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error())
// If there is no recursive part, query runs ok.
rows := tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;")
rows.Check(testkit.Rows("1", "2"))
Expand All @@ -216,11 +222,11 @@ func (test *CTETestSuite) TestCTEMaxRecursionDepth(c *check.C) {

tk.MustExec("set @@cte_max_recursion_depth = 0;")
err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 0) select * from cte1;")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value")
require.Error(t, err)
require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error())
err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 1) select * from cte1;")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value")
require.Error(t, err)
require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error())
// If there is no recursive part, query runs ok.
rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;")
rows.Check(testkit.Rows("1", "2"))
Expand All @@ -233,17 +239,17 @@ func (test *CTETestSuite) TestCTEMaxRecursionDepth(c *check.C) {
rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 1) select * from cte1;")
rows.Check(testkit.Rows("1"))
err = tk.QueryToErr("with recursive cte1(c1) as (select 1 union select c1 + 1 c1 from cte1 where c1 < 2) select * from cte1;")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 2 iterations. Try increasing @@cte_max_recursion_depth to a larger value")
require.Error(t, err)
require.Equal(t, "[executor:3636]Recursive query aborted after 2 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error())
// If there is no recursive part, query runs ok.
rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;")
rows.Check(testkit.Rows("1", "2"))
rows = tk.MustQuery("with cte1(c1) as (select 1 union select 2) select * from cte1 order by c1;")
rows.Check(testkit.Rows("1", "2"))
}

func (test *CTETestSuite) TestCTEWithLimit(c *check.C) {
tk := testkit.NewTestKit(c, test.store)
func CTEWithLimit(t *testing.T, suite *CTETestSuite) {
tk := testkit.NewTestKit(t, suite.store)
tk.MustExec("use test;")

// Basic recursive tests.
Expand All @@ -268,16 +274,16 @@ func (test *CTETestSuite) TestCTEWithLimit(c *check.C) {
rows.Check(testkit.Rows("2"))

err := tk.QueryToErr("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 1 offset 3) select * from cte1;")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 3 iterations. Try increasing @@cte_max_recursion_depth to a larger value")
require.Error(t, err)
require.Equal(t, "[executor:3636]Recursive query aborted after 3 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error())

tk.MustExec("set cte_max_recursion_depth=1000;")
rows = tk.MustQuery("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 5 offset 996) select * from cte1;")
rows.Check(testkit.Rows("996", "997", "998", "999", "1000"))

err = tk.QueryToErr("with recursive cte1(c1) as (select 0 union select c1 + 1 from cte1 limit 5 offset 997) select * from cte1;")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value")
require.Error(t, err)
require.Equal(t, "[executor:3636]Recursive query aborted after 1001 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error())

rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select c1 + 1 from cte1 limit 0 offset 1) select * from cte1")
rows.Check(testkit.Rows())
Expand Down Expand Up @@ -312,7 +318,7 @@ func (test *CTETestSuite) TestCTEWithLimit(c *check.C) {
// Error: ERROR 1221 (HY000): Incorrect usage of UNION and LIMIT.
// Limit can only be at the end of SQL stmt.
err = tk.ExecToErr("with recursive cte1(c1) as (select c1 from t1 limit 1 offset 1 union select c1 + 1 from cte1 limit 0 offset 1) select * from cte1")
c.Assert(err.Error(), check.Equals, "[planner:1221]Incorrect usage of UNION and LIMIT")
require.Equal(t, "[planner:1221]Incorrect usage of UNION and LIMIT", err.Error())

// Basic non-recusive tests.
rows = tk.MustQuery("with recursive cte1(c1) as (select 1 union select 2 order by 1 limit 1 offset 1) select * from cte1")
Expand Down Expand Up @@ -375,8 +381,8 @@ func (test *CTETestSuite) TestCTEWithLimit(c *check.C) {
rows.Check(testkit.Rows())
// MySQL err: ERROR 1365 (22012): Division by 0. Because it gives error when computing 1/c1.
err = tk.QueryToErr("with recursive cte1 as (select 1/c1 c1 from t1 union select c1 + 1 c1 from cte1 where c1 < 2 limit 1) select * from cte1;")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value")
require.Error(t, err)
require.Equal(t, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value", err.Error())

tk.MustExec("set cte_max_recursion_depth = 1000;")
tk.MustExec("drop table if exists t1;")
Expand Down
11 changes: 10 additions & 1 deletion testkit/testkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (tk *TestKit) QueryToErr(sql string, args ...interface{}) error {
tk.require.NoError(err, comment)
tk.require.NotNil(res, comment)
_, resErr := session.GetRows4Test(context.Background(), tk.session, res)
tk.require.Nil(res.Close())
tk.require.NoError(res.Close())
return resErr
}

Expand Down Expand Up @@ -149,6 +149,15 @@ func (tk *TestKit) Exec(sql string, args ...interface{}) (sqlexec.RecordSet, err
return rs, nil
}

// ExecToErr executes a sql statement and discard results.
func (tk *TestKit) ExecToErr(sql string, args ...interface{}) error {
res, err := tk.Exec(sql, args...)
if res != nil {
tk.require.NoError(res.Close())
}
return err
}

func newSession(t *testing.T, store kv.Storage) session.Session {
se, err := session.CreateSession4Test(store)
require.Nil(t, err)
Expand Down