From 2d4e9205546d96b3bed8ec3df04a6d9ac2722ba6 Mon Sep 17 00:00:00 2001 From: feitian124 Date: Tue, 24 Aug 2021 21:32:26 +0800 Subject: [PATCH 1/9] table/tables: migrate test-infra to testify --- table/tables/index_test.go | 224 ++++++++++++++++++------------------- table/tables/main_test.go | 27 +++++ 2 files changed, 138 insertions(+), 113 deletions(-) create mode 100644 table/tables/main_test.go diff --git a/table/tables/index_test.go b/table/tables/index_test.go index 426e5e488d28c..b1f9591752a99 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -17,9 +17,9 @@ package tables_test import ( "context" "io" + "testing" "time" - . "github.com/pingcap/check" "github.com/pingcap/parser" "github.com/pingcap/parser/ast" "github.com/pingcap/parser/model" @@ -39,33 +39,31 @@ import ( "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/rowcodec" - "github.com/pingcap/tidb/util/testleak" + "github.com/stretchr/testify/require" ) -var _ = Suite(&testIndexSuite{}) - type testIndexSuite struct { s kv.Storage dom *domain.Domain } -func (s *testIndexSuite) SetUpSuite(c *C) { - testleak.BeforeTest() +var s = testIndexSuite{} + +func TestSetUpSuite(t *testing.T) { store, err := mockstore.NewMockStore() - c.Assert(err, IsNil) + require.Nil(t, err) s.s = store s.dom, err = session.BootstrapSession(store) - c.Assert(err, IsNil) + require.Nil(t, err) } -func (s *testIndexSuite) TearDownSuite(c *C) { +func TestTearDownSuite(t *testing.T) { s.dom.Close() err := s.s.Close() - c.Assert(err, IsNil) - testleak.AfterTest(c)() + require.Nil(t, err) } -func (s *testIndexSuite) TestIndex(c *C) { +func TestIndex(t *testing.T) { tblInfo := &model.TableInfo{ ID: 1, Indices: []*model.IndexInfo{ @@ -87,72 +85,72 @@ func (s *testIndexSuite) TestIndex(c *C) { // Test ununiq index. txn, err := s.s.Begin() - c.Assert(err, IsNil) + require.Nil(t, err) values := types.MakeDatums(1, 2) mockCtx := mock.NewContext() _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - c.Assert(err, IsNil) + require.Nil(t, err) it, err := index.SeekFirst(txn) - c.Assert(err, IsNil) + require.Nil(t, err) getValues, h, err := it.Next() - c.Assert(err, IsNil) - c.Assert(getValues, HasLen, 2) - c.Assert(getValues[0].GetInt64(), Equals, int64(1)) - c.Assert(getValues[1].GetInt64(), Equals, int64(2)) - c.Assert(h.IntValue(), Equals, int64(1)) + require.Nil(t, err) + require.Len(t, getValues, 2) + require.Equal(t, int64(1), getValues[0].GetInt64()) + require.Equal(t, int64(2), getValues[1].GetInt64()) + require.Equal(t, int64(1), h.IntValue()) it.Close() sc := &stmtctx.StatementContext{TimeZone: time.Local} exist, _, err := index.Exist(sc, txn, values, kv.IntHandle(100)) - c.Assert(err, IsNil) - c.Assert(exist, IsFalse) + require.Nil(t, err) + require.False(t, exist) exist, _, err = index.Exist(sc, txn, values, kv.IntHandle(1)) - c.Assert(err, IsNil) - c.Assert(exist, IsTrue) + require.Nil(t, err) + require.True(t, exist) err = index.Delete(sc, txn, values, kv.IntHandle(1)) - c.Assert(err, IsNil) + require.Nil(t, err) it, err = index.SeekFirst(txn) - c.Assert(err, IsNil) + require.Nil(t, err) _, _, err = it.Next() - c.Assert(terror.ErrorEqual(err, io.EOF), IsTrue, Commentf("err %v", err)) + require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) it.Close() _, err = index.Create(mockCtx, txn, values, kv.IntHandle(0), nil) - c.Assert(err, IsNil) + require.Nil(t, err) _, err = index.SeekFirst(txn) - c.Assert(err, IsNil) + require.Nil(t, err) _, hit, err := index.Seek(sc, txn, values) - c.Assert(err, IsNil) - c.Assert(hit, IsFalse) + require.Nil(t, err) + require.False(t, hit) err = index.Drop(txn) - c.Assert(err, IsNil) + require.Nil(t, err) it, hit, err = index.Seek(sc, txn, values) - c.Assert(err, IsNil) - c.Assert(hit, IsFalse) + require.Nil(t, err) + require.False(t, hit) _, _, err = it.Next() - c.Assert(terror.ErrorEqual(err, io.EOF), IsTrue, Commentf("err %v", err)) + require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) it.Close() it, err = index.SeekFirst(txn) - c.Assert(err, IsNil) + require.Nil(t, err) _, _, err = it.Next() - c.Assert(terror.ErrorEqual(err, io.EOF), IsTrue, Commentf("err %v", err)) + require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) it.Close() err = txn.Commit(context.Background()) - c.Assert(err, IsNil) + require.Nil(t, err) tblInfo = &model.TableInfo{ ID: 2, @@ -176,63 +174,63 @@ func (s *testIndexSuite) TestIndex(c *C) { // Test uniq index. txn, err = s.s.Begin() - c.Assert(err, IsNil) + require.Nil(t, err) _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - c.Assert(err, IsNil) + require.Nil(t, err) _, err = index.Create(mockCtx, txn, values, kv.IntHandle(2), nil) - c.Assert(err, NotNil) + require.NotNil(t, err) it, err = index.SeekFirst(txn) - c.Assert(err, IsNil) + require.Nil(t, err) getValues, h, err = it.Next() - c.Assert(err, IsNil) - c.Assert(getValues, HasLen, 2) - c.Assert(getValues[0].GetInt64(), Equals, int64(1)) - c.Assert(getValues[1].GetInt64(), Equals, int64(2)) - c.Assert(h.IntValue(), Equals, int64(1)) + require.Nil(t, err) + require.Len(t, getValues, 2) + require.Equal(t, int64(1), getValues[0].GetInt64()) + require.Equal(t, int64(2), getValues[1].GetInt64()) + require.Equal(t, int64(1), h.IntValue()) it.Close() exist, h, err = index.Exist(sc, txn, values, kv.IntHandle(1)) - c.Assert(err, IsNil) - c.Assert(h.IntValue(), Equals, int64(1)) - c.Assert(exist, IsTrue) + require.Nil(t, err) + require.Equal(t, int64(1), h.IntValue()) + require.True(t, exist) exist, h, err = index.Exist(sc, txn, values, kv.IntHandle(2)) - c.Assert(err, NotNil) - c.Assert(h.IntValue(), Equals, int64(1)) - c.Assert(exist, IsTrue) + require.NotNil(t, err) + require.Equal(t, int64(1), h.IntValue()) + require.True(t, exist) err = txn.Commit(context.Background()) - c.Assert(err, IsNil) + require.Nil(t, err) _, err = index.FetchValues(make([]types.Datum, 0), nil) - c.Assert(err, NotNil) + require.NotNil(t, err) txn, err = s.s.Begin() - c.Assert(err, IsNil) + require.Nil(t, err) // Test the function of Next when the value of unique key is nil. values2 := types.MakeDatums(nil, nil) _, err = index.Create(mockCtx, txn, values2, kv.IntHandle(2), nil) - c.Assert(err, IsNil) + require.Nil(t, err) it, err = index.SeekFirst(txn) - c.Assert(err, IsNil) + require.Nil(t, err) getValues, h, err = it.Next() - c.Assert(err, IsNil) - c.Assert(getValues, HasLen, 2) - c.Assert(getValues[0].GetInterface(), Equals, nil) - c.Assert(getValues[1].GetInterface(), Equals, nil) - c.Assert(h.IntValue(), Equals, int64(2)) + require.Nil(t, err) + require.Len(t, getValues, 2) + require.Equal(t, nil, getValues[0].GetInterface()) + require.Equal(t, nil, getValues[1].GetInterface()) + require.Equal(t, int64(2), h.IntValue()) it.Close() err = txn.Commit(context.Background()) - c.Assert(err, IsNil) + require.Nil(t, err) } -func (s *testIndexSuite) TestCombineIndexSeek(c *C) { +func TestCombineIndexSeek(t *testing.T) { tblInfo := &model.TableInfo{ ID: 1, Indices: []*model.IndexInfo{ @@ -254,26 +252,26 @@ func (s *testIndexSuite) TestCombineIndexSeek(c *C) { index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) txn, err := s.s.Begin() - c.Assert(err, IsNil) + require.Nil(t, err) mockCtx := mock.NewContext() values := types.MakeDatums("abc", "def") _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - c.Assert(err, IsNil) + require.Nil(t, err) index2 := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) sc := &stmtctx.StatementContext{TimeZone: time.Local} iter, hit, err := index2.Seek(sc, txn, types.MakeDatums("abc", nil)) - c.Assert(err, IsNil) + require.Nil(t, err) defer iter.Close() - c.Assert(hit, IsFalse) + require.False(t, hit) _, h, err := iter.Next() - c.Assert(err, IsNil) - c.Assert(h.IntValue(), Equals, int64(1)) + require.Nil(t, err) + require.Equal(t, int64(1), h.IntValue()) } -func (s *testIndexSuite) TestSingleColumnCommonHandle(c *C) { - tblInfo := buildTableInfo(c, "create table t (a varchar(255) primary key, u int unique, nu int, index nu (nu))") +func TestSingleColumnCommonHandle(t *testing.T) { + tblInfo := buildTableInfo(t, "create table t (a varchar(255) primary key, u int unique, nu int, index nu (nu))") var idxUnique, idxNonUnique table.Index for _, idxInfo := range tblInfo.Indices { idx := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo) @@ -284,7 +282,7 @@ func (s *testIndexSuite) TestSingleColumnCommonHandle(c *C) { } } txn, err := s.s.Begin() - c.Assert(err, IsNil) + require.Nil(t, err) mockCtx := mock.NewContext() sc := mockCtx.GetSessionVars().StmtCtx @@ -292,44 +290,44 @@ func (s *testIndexSuite) TestSingleColumnCommonHandle(c *C) { idxColVals := types.MakeDatums(1) handleColVals := types.MakeDatums("abc") encodedHandle, err := codec.EncodeKey(sc, nil, handleColVals...) - c.Assert(err, IsNil) + require.Nil(t, err) commonHandle, err := kv.NewCommonHandle(encodedHandle) - c.Assert(err, IsNil) + require.Nil(t, err) for _, idx := range []table.Index{idxUnique, idxNonUnique} { key, _, err := idx.GenIndexKey(sc, idxColVals, commonHandle, nil) - c.Assert(err, IsNil) + require.Nil(t, err) _, err = idx.Create(mockCtx, txn, idxColVals, commonHandle, nil) - c.Assert(err, IsNil) + require.Nil(t, err) val, err := txn.Get(context.Background(), key) - c.Assert(err, IsNil) + require.Nil(t, err) colVals, err := tablecodec.DecodeIndexKV(key, val, 1, tablecodec.HandleDefault, tables.BuildRowcodecColInfoForIndexColumns(idx.Meta(), tblInfo)) - c.Assert(err, IsNil) - c.Assert(colVals, HasLen, 2) + require.Nil(t, err) + require.Len(t, colVals, 2) _, d, err := codec.DecodeOne(colVals[0]) - c.Assert(err, IsNil) - c.Assert(d.GetInt64(), Equals, int64(1)) + require.Nil(t, err) + require.Equal(t, int64(1), d.GetInt64()) _, d, err = codec.DecodeOne(colVals[1]) - c.Assert(err, IsNil) - c.Assert(d.GetString(), Equals, "abc") + require.Nil(t, err) + require.Equal(t, "abc", d.GetString()) handle, err := tablecodec.DecodeIndexHandle(key, val, 1) - c.Assert(err, IsNil) - c.Assert(handle.IsInt(), IsFalse) - c.Assert(handle.Encoded(), BytesEquals, commonHandle.Encoded()) + require.Nil(t, err) + require.False(t, handle.IsInt()) + require.Equal(t, commonHandle.Encoded(), handle.Encoded()) unTouchedVal := append([]byte{1}, val[1:]...) unTouchedVal = append(unTouchedVal, kv.UnCommitIndexKVFlag) _, err = tablecodec.DecodeIndexKV(key, unTouchedVal, 1, tablecodec.HandleDefault, tables.BuildRowcodecColInfoForIndexColumns(idx.Meta(), tblInfo)) - c.Assert(err, IsNil) + require.Nil(t, err) } } -func (s *testIndexSuite) TestMultiColumnCommonHandle(c *C) { +func TestMultiColumnCommonHandle(t *testing.T) { collate.SetNewCollationEnabledForTest(true) defer collate.SetNewCollationEnabledForTest(false) - tblInfo := buildTableInfo(c, "create table t (a int, b int, u varchar(64) unique, nu varchar(64), primary key (a, b), index nu (nu))") + tblInfo := buildTableInfo(t, "create table t (a int, b int, u varchar(64) unique, nu varchar(64), primary key (a, b), index nu (nu))") var idxUnique, idxNonUnique table.Index for _, idxInfo := range tblInfo.Indices { idx := tables.NewIndex(tblInfo.ID, tblInfo, idxInfo) @@ -347,28 +345,28 @@ func (s *testIndexSuite) TestMultiColumnCommonHandle(c *C) { b = col } } - c.Assert(a, NotNil) - c.Assert(b, NotNil) + require.NotNil(t, a) + require.NotNil(t, b) txn, err := s.s.Begin() - c.Assert(err, IsNil) + require.Nil(t, err) mockCtx := mock.NewContext() sc := mockCtx.GetSessionVars().StmtCtx // create index for "insert t values (3, 2, "abc", "abc") idxColVals := types.MakeDatums("abc") handleColVals := types.MakeDatums(3, 2) encodedHandle, err := codec.EncodeKey(sc, nil, handleColVals...) - c.Assert(err, IsNil) + require.Nil(t, err) commonHandle, err := kv.NewCommonHandle(encodedHandle) - c.Assert(err, IsNil) + require.Nil(t, err) _ = idxNonUnique for _, idx := range []table.Index{idxUnique, idxNonUnique} { key, _, err := idx.GenIndexKey(sc, idxColVals, commonHandle, nil) - c.Assert(err, IsNil) + require.Nil(t, err) _, err = idx.Create(mockCtx, txn, idxColVals, commonHandle, nil) - c.Assert(err, IsNil) + require.Nil(t, err) val, err := txn.Get(context.Background(), key) - c.Assert(err, IsNil) + require.Nil(t, err) colInfo := tables.BuildRowcodecColInfoForIndexColumns(idx.Meta(), tblInfo) colInfo = append(colInfo, rowcodec.ColInfo{ ID: a.ID, @@ -381,28 +379,28 @@ func (s *testIndexSuite) TestMultiColumnCommonHandle(c *C) { Ft: rowcodec.FieldTypeFromModelColumn(b), }) colVals, err := tablecodec.DecodeIndexKV(key, val, 1, tablecodec.HandleDefault, colInfo) - c.Assert(err, IsNil) - c.Assert(colVals, HasLen, 3) + require.Nil(t, err) + require.Len(t, colVals, 3) _, d, err := codec.DecodeOne(colVals[0]) - c.Assert(err, IsNil) - c.Assert(d.GetString(), Equals, "abc") + require.Nil(t, err) + require.Equal(t, "abc", d.GetString()) _, d, err = codec.DecodeOne(colVals[1]) - c.Assert(err, IsNil) - c.Assert(d.GetInt64(), Equals, int64(3)) + require.Nil(t, err) + require.Equal(t, int64(3), d.GetInt64()) _, d, err = codec.DecodeOne(colVals[2]) - c.Assert(err, IsNil) - c.Assert(d.GetInt64(), Equals, int64(2)) + require.Nil(t, err) + require.Equal(t, int64(2), d.GetInt64()) handle, err := tablecodec.DecodeIndexHandle(key, val, 1) - c.Assert(err, IsNil) - c.Assert(handle.IsInt(), IsFalse) - c.Assert(handle.Encoded(), BytesEquals, commonHandle.Encoded()) + require.Nil(t, err) + require.False(t, handle.IsInt()) + require.Equal(t, commonHandle.Encoded(), handle.Encoded()) } } -func buildTableInfo(c *C, sql string) *model.TableInfo { +func buildTableInfo(t *testing.T, sql string) *model.TableInfo { stmt, err := parser.New().ParseOneStmt(sql, "", "") - c.Assert(err, IsNil) + require.Nil(t, err) tblInfo, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt)) - c.Assert(err, IsNil) + require.Nil(t, err) return tblInfo } diff --git a/table/tables/main_test.go b/table/tables/main_test.go new file mode 100644 index 0000000000000..f1dec8b6447f5 --- /dev/null +++ b/table/tables/main_test.go @@ -0,0 +1,27 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tables_test + +import ( + "testing" + + "github.com/pingcap/tidb/util/testbridge" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + testbridge.WorkaroundGoCheckFlags() + goleak.VerifyTestMain(m) +} From 23fb97ffcaa12786870e275ff2b7cc6ed0a82398 Mon Sep 17 00:00:00 2001 From: feitian124 Date: Tue, 24 Aug 2021 22:10:42 +0800 Subject: [PATCH 2/9] make test pass --- table/tables/index_test.go | 12 ++++++------ table/tables/main_test.go | 7 ++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/table/tables/index_test.go b/table/tables/index_test.go index b1f9591752a99..2629e3e7e9dfa 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -57,12 +57,6 @@ func TestSetUpSuite(t *testing.T) { require.Nil(t, err) } -func TestTearDownSuite(t *testing.T) { - s.dom.Close() - err := s.s.Close() - require.Nil(t, err) -} - func TestIndex(t *testing.T) { tblInfo := &model.TableInfo{ ID: 1, @@ -404,3 +398,9 @@ func buildTableInfo(t *testing.T, sql string) *model.TableInfo { require.Nil(t, err) return tblInfo } + +func TestTearDownSuite(t *testing.T) { + s.dom.Close() + err := s.s.Close() + require.Nil(t, err) +} diff --git a/table/tables/main_test.go b/table/tables/main_test.go index f1dec8b6447f5..be83c73b29f93 100644 --- a/table/tables/main_test.go +++ b/table/tables/main_test.go @@ -23,5 +23,10 @@ import ( func TestMain(m *testing.M) { testbridge.WorkaroundGoCheckFlags() - goleak.VerifyTestMain(m) + + opts := []goleak.Option{ + goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"), + goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), + } + goleak.VerifyTestMain(m, opts...) } From aa633538287e8969f4144d7660eeb7143c26fb9b Mon Sep 17 00:00:00 2001 From: feitian124 Date: Tue, 24 Aug 2021 23:24:35 +0800 Subject: [PATCH 3/9] use setup teardown --- table/tables/index_test.go | 17 ----------------- table/tables/main_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/table/tables/index_test.go b/table/tables/index_test.go index 2629e3e7e9dfa..2824c53072c09 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -28,9 +28,7 @@ import ( "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx/stmtctx" - "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/tablecodec" @@ -47,16 +45,6 @@ type testIndexSuite struct { dom *domain.Domain } -var s = testIndexSuite{} - -func TestSetUpSuite(t *testing.T) { - store, err := mockstore.NewMockStore() - require.Nil(t, err) - s.s = store - s.dom, err = session.BootstrapSession(store) - require.Nil(t, err) -} - func TestIndex(t *testing.T) { tblInfo := &model.TableInfo{ ID: 1, @@ -399,8 +387,3 @@ func buildTableInfo(t *testing.T, sql string) *model.TableInfo { return tblInfo } -func TestTearDownSuite(t *testing.T) { - s.dom.Close() - err := s.s.Close() - require.Nil(t, err) -} diff --git a/table/tables/main_test.go b/table/tables/main_test.go index be83c73b29f93..10b135e26e4b1 100644 --- a/table/tables/main_test.go +++ b/table/tables/main_test.go @@ -15,18 +15,53 @@ package tables_test import ( + "github.com/pingcap/tidb/session" + "github.com/pingcap/tidb/store/mockstore" + "log" + "os" "testing" "github.com/pingcap/tidb/util/testbridge" "go.uber.org/goleak" ) +var s = testIndexSuite{} + func TestMain(m *testing.M) { testbridge.WorkaroundGoCheckFlags() + setUpIndexSuite() + exitCode := m.Run() + tearDownIndexSuite() + if exitCode != 0 { + os.Exit(exitCode) + } + opts := []goleak.Option{ goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } goleak.VerifyTestMain(m, opts...) } + +func setUpIndexSuite() { + println("setUpIndexSuite") + store, err := mockstore.NewMockStore() + if err != nil { + log.Fatal(err) + } + s.s = store + s.dom, err = session.BootstrapSession(store) + if err != nil { + log.Fatal(err) + } +} + +func tearDownIndexSuite() { + println("tearDownIndexSuite") + s.dom.Close() + err := s.s.Close() + if err != nil { + log.Fatal(err) + } +} From 8a07dabcedbfc2f300a801e680aeaea524d85ba3 Mon Sep 17 00:00:00 2001 From: feitian124 Date: Tue, 24 Aug 2021 23:25:25 +0800 Subject: [PATCH 4/9] Revert "use setup teardown" This reverts commit aa633538287e8969f4144d7660eeb7143c26fb9b. --- table/tables/index_test.go | 17 +++++++++++++++++ table/tables/main_test.go | 35 ----------------------------------- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/table/tables/index_test.go b/table/tables/index_test.go index 2824c53072c09..2629e3e7e9dfa 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -28,7 +28,9 @@ import ( "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx/stmtctx" + "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/tablecodec" @@ -45,6 +47,16 @@ type testIndexSuite struct { dom *domain.Domain } +var s = testIndexSuite{} + +func TestSetUpSuite(t *testing.T) { + store, err := mockstore.NewMockStore() + require.Nil(t, err) + s.s = store + s.dom, err = session.BootstrapSession(store) + require.Nil(t, err) +} + func TestIndex(t *testing.T) { tblInfo := &model.TableInfo{ ID: 1, @@ -387,3 +399,8 @@ func buildTableInfo(t *testing.T, sql string) *model.TableInfo { return tblInfo } +func TestTearDownSuite(t *testing.T) { + s.dom.Close() + err := s.s.Close() + require.Nil(t, err) +} diff --git a/table/tables/main_test.go b/table/tables/main_test.go index 10b135e26e4b1..be83c73b29f93 100644 --- a/table/tables/main_test.go +++ b/table/tables/main_test.go @@ -15,53 +15,18 @@ package tables_test import ( - "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/store/mockstore" - "log" - "os" "testing" "github.com/pingcap/tidb/util/testbridge" "go.uber.org/goleak" ) -var s = testIndexSuite{} - func TestMain(m *testing.M) { testbridge.WorkaroundGoCheckFlags() - setUpIndexSuite() - exitCode := m.Run() - tearDownIndexSuite() - if exitCode != 0 { - os.Exit(exitCode) - } - opts := []goleak.Option{ goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } goleak.VerifyTestMain(m, opts...) } - -func setUpIndexSuite() { - println("setUpIndexSuite") - store, err := mockstore.NewMockStore() - if err != nil { - log.Fatal(err) - } - s.s = store - s.dom, err = session.BootstrapSession(store) - if err != nil { - log.Fatal(err) - } -} - -func tearDownIndexSuite() { - println("tearDownIndexSuite") - s.dom.Close() - err := s.s.Close() - if err != nil { - log.Fatal(err) - } -} From 5c837e242312f7cc441c7433886cc44311c4f1a3 Mon Sep 17 00:00:00 2001 From: feitian124 Date: Wed, 25 Aug 2021 23:47:01 +0800 Subject: [PATCH 5/9] use require.NoError --- table/tables/index_test.go | 114 ++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/table/tables/index_test.go b/table/tables/index_test.go index 2629e3e7e9dfa..a7acb61a222ec 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -51,10 +51,10 @@ var s = testIndexSuite{} func TestSetUpSuite(t *testing.T) { store, err := mockstore.NewMockStore() - require.Nil(t, err) + require.NoError(t, err) s.s = store s.dom, err = session.BootstrapSession(store) - require.Nil(t, err) + require.NoError(t, err) } func TestIndex(t *testing.T) { @@ -79,18 +79,18 @@ func TestIndex(t *testing.T) { // Test ununiq index. txn, err := s.s.Begin() - require.Nil(t, err) + require.NoError(t, err) values := types.MakeDatums(1, 2) mockCtx := mock.NewContext() _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - require.Nil(t, err) + require.NoError(t, err) it, err := index.SeekFirst(txn) - require.Nil(t, err) + require.NoError(t, err) getValues, h, err := it.Next() - require.Nil(t, err) + require.NoError(t, err) require.Len(t, getValues, 2) require.Equal(t, int64(1), getValues[0].GetInt64()) require.Equal(t, int64(2), getValues[1].GetInt64()) @@ -98,38 +98,38 @@ func TestIndex(t *testing.T) { it.Close() sc := &stmtctx.StatementContext{TimeZone: time.Local} exist, _, err := index.Exist(sc, txn, values, kv.IntHandle(100)) - require.Nil(t, err) + require.NoError(t, err) require.False(t, exist) exist, _, err = index.Exist(sc, txn, values, kv.IntHandle(1)) - require.Nil(t, err) + require.NoError(t, err) require.True(t, exist) err = index.Delete(sc, txn, values, kv.IntHandle(1)) - require.Nil(t, err) + require.NoError(t, err) it, err = index.SeekFirst(txn) - require.Nil(t, err) + require.NoError(t, err) _, _, err = it.Next() require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) it.Close() _, err = index.Create(mockCtx, txn, values, kv.IntHandle(0), nil) - require.Nil(t, err) + require.NoError(t, err) _, err = index.SeekFirst(txn) - require.Nil(t, err) + require.NoError(t, err) _, hit, err := index.Seek(sc, txn, values) - require.Nil(t, err) + require.NoError(t, err) require.False(t, hit) err = index.Drop(txn) - require.Nil(t, err) + require.NoError(t, err) it, hit, err = index.Seek(sc, txn, values) - require.Nil(t, err) + require.NoError(t, err) require.False(t, hit) _, _, err = it.Next() @@ -137,14 +137,14 @@ func TestIndex(t *testing.T) { it.Close() it, err = index.SeekFirst(txn) - require.Nil(t, err) + require.NoError(t, err) _, _, err = it.Next() require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) it.Close() err = txn.Commit(context.Background()) - require.Nil(t, err) + require.NoError(t, err) tblInfo = &model.TableInfo{ ID: 2, @@ -168,19 +168,19 @@ func TestIndex(t *testing.T) { // Test uniq index. txn, err = s.s.Begin() - require.Nil(t, err) + require.NoError(t, err) _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - require.Nil(t, err) + require.NoError(t, err) _, err = index.Create(mockCtx, txn, values, kv.IntHandle(2), nil) require.NotNil(t, err) it, err = index.SeekFirst(txn) - require.Nil(t, err) + require.NoError(t, err) getValues, h, err = it.Next() - require.Nil(t, err) + require.NoError(t, err) require.Len(t, getValues, 2) require.Equal(t, int64(1), getValues[0].GetInt64()) require.Equal(t, int64(2), getValues[1].GetInt64()) @@ -188,7 +188,7 @@ func TestIndex(t *testing.T) { it.Close() exist, h, err = index.Exist(sc, txn, values, kv.IntHandle(1)) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, int64(1), h.IntValue()) require.True(t, exist) @@ -198,22 +198,22 @@ func TestIndex(t *testing.T) { require.True(t, exist) err = txn.Commit(context.Background()) - require.Nil(t, err) + require.NoError(t, err) _, err = index.FetchValues(make([]types.Datum, 0), nil) require.NotNil(t, err) txn, err = s.s.Begin() - require.Nil(t, err) + require.NoError(t, err) // Test the function of Next when the value of unique key is nil. values2 := types.MakeDatums(nil, nil) _, err = index.Create(mockCtx, txn, values2, kv.IntHandle(2), nil) - require.Nil(t, err) + require.NoError(t, err) it, err = index.SeekFirst(txn) - require.Nil(t, err) + require.NoError(t, err) getValues, h, err = it.Next() - require.Nil(t, err) + require.NoError(t, err) require.Len(t, getValues, 2) require.Equal(t, nil, getValues[0].GetInterface()) require.Equal(t, nil, getValues[1].GetInterface()) @@ -221,7 +221,7 @@ func TestIndex(t *testing.T) { it.Close() err = txn.Commit(context.Background()) - require.Nil(t, err) + require.NoError(t, err) } func TestCombineIndexSeek(t *testing.T) { @@ -246,21 +246,21 @@ func TestCombineIndexSeek(t *testing.T) { index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) txn, err := s.s.Begin() - require.Nil(t, err) + require.NoError(t, err) mockCtx := mock.NewContext() values := types.MakeDatums("abc", "def") _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - require.Nil(t, err) + require.NoError(t, err) index2 := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) sc := &stmtctx.StatementContext{TimeZone: time.Local} iter, hit, err := index2.Seek(sc, txn, types.MakeDatums("abc", nil)) - require.Nil(t, err) + require.NoError(t, err) defer iter.Close() require.False(t, hit) _, h, err := iter.Next() - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, int64(1), h.IntValue()) } @@ -276,7 +276,7 @@ func TestSingleColumnCommonHandle(t *testing.T) { } } txn, err := s.s.Begin() - require.Nil(t, err) + require.NoError(t, err) mockCtx := mock.NewContext() sc := mockCtx.GetSessionVars().StmtCtx @@ -284,29 +284,29 @@ func TestSingleColumnCommonHandle(t *testing.T) { idxColVals := types.MakeDatums(1) handleColVals := types.MakeDatums("abc") encodedHandle, err := codec.EncodeKey(sc, nil, handleColVals...) - require.Nil(t, err) + require.NoError(t, err) commonHandle, err := kv.NewCommonHandle(encodedHandle) - require.Nil(t, err) + require.NoError(t, err) for _, idx := range []table.Index{idxUnique, idxNonUnique} { key, _, err := idx.GenIndexKey(sc, idxColVals, commonHandle, nil) - require.Nil(t, err) + require.NoError(t, err) _, err = idx.Create(mockCtx, txn, idxColVals, commonHandle, nil) - require.Nil(t, err) + require.NoError(t, err) val, err := txn.Get(context.Background(), key) - require.Nil(t, err) + require.NoError(t, err) colVals, err := tablecodec.DecodeIndexKV(key, val, 1, tablecodec.HandleDefault, tables.BuildRowcodecColInfoForIndexColumns(idx.Meta(), tblInfo)) - require.Nil(t, err) + require.NoError(t, err) require.Len(t, colVals, 2) _, d, err := codec.DecodeOne(colVals[0]) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, int64(1), d.GetInt64()) _, d, err = codec.DecodeOne(colVals[1]) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, "abc", d.GetString()) handle, err := tablecodec.DecodeIndexHandle(key, val, 1) - require.Nil(t, err) + require.NoError(t, err) require.False(t, handle.IsInt()) require.Equal(t, commonHandle.Encoded(), handle.Encoded()) @@ -314,7 +314,7 @@ func TestSingleColumnCommonHandle(t *testing.T) { unTouchedVal = append(unTouchedVal, kv.UnCommitIndexKVFlag) _, err = tablecodec.DecodeIndexKV(key, unTouchedVal, 1, tablecodec.HandleDefault, tables.BuildRowcodecColInfoForIndexColumns(idx.Meta(), tblInfo)) - require.Nil(t, err) + require.NoError(t, err) } } @@ -343,24 +343,24 @@ func TestMultiColumnCommonHandle(t *testing.T) { require.NotNil(t, b) txn, err := s.s.Begin() - require.Nil(t, err) + require.NoError(t, err) mockCtx := mock.NewContext() sc := mockCtx.GetSessionVars().StmtCtx // create index for "insert t values (3, 2, "abc", "abc") idxColVals := types.MakeDatums("abc") handleColVals := types.MakeDatums(3, 2) encodedHandle, err := codec.EncodeKey(sc, nil, handleColVals...) - require.Nil(t, err) + require.NoError(t, err) commonHandle, err := kv.NewCommonHandle(encodedHandle) - require.Nil(t, err) + require.NoError(t, err) _ = idxNonUnique for _, idx := range []table.Index{idxUnique, idxNonUnique} { key, _, err := idx.GenIndexKey(sc, idxColVals, commonHandle, nil) - require.Nil(t, err) + require.NoError(t, err) _, err = idx.Create(mockCtx, txn, idxColVals, commonHandle, nil) - require.Nil(t, err) + require.NoError(t, err) val, err := txn.Get(context.Background(), key) - require.Nil(t, err) + require.NoError(t, err) colInfo := tables.BuildRowcodecColInfoForIndexColumns(idx.Meta(), tblInfo) colInfo = append(colInfo, rowcodec.ColInfo{ ID: a.ID, @@ -373,19 +373,19 @@ func TestMultiColumnCommonHandle(t *testing.T) { Ft: rowcodec.FieldTypeFromModelColumn(b), }) colVals, err := tablecodec.DecodeIndexKV(key, val, 1, tablecodec.HandleDefault, colInfo) - require.Nil(t, err) + require.NoError(t, err) require.Len(t, colVals, 3) _, d, err := codec.DecodeOne(colVals[0]) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, "abc", d.GetString()) _, d, err = codec.DecodeOne(colVals[1]) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, int64(3), d.GetInt64()) _, d, err = codec.DecodeOne(colVals[2]) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, int64(2), d.GetInt64()) handle, err := tablecodec.DecodeIndexHandle(key, val, 1) - require.Nil(t, err) + require.NoError(t, err) require.False(t, handle.IsInt()) require.Equal(t, commonHandle.Encoded(), handle.Encoded()) } @@ -393,14 +393,14 @@ func TestMultiColumnCommonHandle(t *testing.T) { func buildTableInfo(t *testing.T, sql string) *model.TableInfo { stmt, err := parser.New().ParseOneStmt(sql, "", "") - require.Nil(t, err) + require.NoError(t, err) tblInfo, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt)) - require.Nil(t, err) + require.NoError(t, err) return tblInfo } func TestTearDownSuite(t *testing.T) { s.dom.Close() err := s.s.Close() - require.Nil(t, err) + require.NoError(t, err) } From e1babfa1c16dd9419183a739b58d2280881e7e10 Mon Sep 17 00:00:00 2001 From: feitian124 Date: Thu, 26 Aug 2021 22:53:03 +0800 Subject: [PATCH 6/9] use buildMock to get mock store and close --- table/tables/index_test.go | 50 ++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/table/tables/index_test.go b/table/tables/index_test.go index a7acb61a222ec..dd565f5d11edc 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -26,7 +26,6 @@ import ( "github.com/pingcap/parser/mysql" "github.com/pingcap/parser/terror" "github.com/pingcap/tidb/ddl" - "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx/stmtctx" @@ -42,22 +41,22 @@ import ( "github.com/stretchr/testify/require" ) -type testIndexSuite struct { - s kv.Storage - dom *domain.Domain -} - -var s = testIndexSuite{} - -func TestSetUpSuite(t *testing.T) { +func buildMock(t *testing.T) (kv.Storage, func()) { store, err := mockstore.NewMockStore() require.NoError(t, err) - s.s = store - s.dom, err = session.BootstrapSession(store) + dom, err := session.BootstrapSession(store) require.NoError(t, err) + + closeFunc := func() { + dom.Close() + err := store.Close() + require.NoError(t, err) + } + return store, closeFunc } func TestIndex(t *testing.T) { + t.Parallel() tblInfo := &model.TableInfo{ ID: 1, Indices: []*model.IndexInfo{ @@ -78,7 +77,9 @@ func TestIndex(t *testing.T) { index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) // Test ununiq index. - txn, err := s.s.Begin() + s, closeFunc := buildMock(t) + defer closeFunc() + txn, err := s.Begin() require.NoError(t, err) values := types.MakeDatums(1, 2) @@ -167,7 +168,7 @@ func TestIndex(t *testing.T) { index = tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) // Test uniq index. - txn, err = s.s.Begin() + txn, err = s.Begin() require.NoError(t, err) _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) @@ -203,7 +204,7 @@ func TestIndex(t *testing.T) { _, err = index.FetchValues(make([]types.Datum, 0), nil) require.NotNil(t, err) - txn, err = s.s.Begin() + txn, err = s.Begin() require.NoError(t, err) // Test the function of Next when the value of unique key is nil. @@ -225,6 +226,7 @@ func TestIndex(t *testing.T) { } func TestCombineIndexSeek(t *testing.T) { + t.Parallel() tblInfo := &model.TableInfo{ ID: 1, Indices: []*model.IndexInfo{ @@ -245,7 +247,9 @@ func TestCombineIndexSeek(t *testing.T) { } index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) - txn, err := s.s.Begin() + s, closeFunc := buildMock(t) + defer closeFunc() + txn, err := s.Begin() require.NoError(t, err) mockCtx := mock.NewContext() @@ -265,6 +269,7 @@ func TestCombineIndexSeek(t *testing.T) { } func TestSingleColumnCommonHandle(t *testing.T) { + t.Parallel() tblInfo := buildTableInfo(t, "create table t (a varchar(255) primary key, u int unique, nu int, index nu (nu))") var idxUnique, idxNonUnique table.Index for _, idxInfo := range tblInfo.Indices { @@ -275,7 +280,9 @@ func TestSingleColumnCommonHandle(t *testing.T) { idxNonUnique = idx } } - txn, err := s.s.Begin() + s, closeFunc := buildMock(t) + defer closeFunc() + txn, err := s.Begin() require.NoError(t, err) mockCtx := mock.NewContext() @@ -319,6 +326,7 @@ func TestSingleColumnCommonHandle(t *testing.T) { } func TestMultiColumnCommonHandle(t *testing.T) { + t.Parallel() collate.SetNewCollationEnabledForTest(true) defer collate.SetNewCollationEnabledForTest(false) tblInfo := buildTableInfo(t, "create table t (a int, b int, u varchar(64) unique, nu varchar(64), primary key (a, b), index nu (nu))") @@ -342,7 +350,9 @@ func TestMultiColumnCommonHandle(t *testing.T) { require.NotNil(t, a) require.NotNil(t, b) - txn, err := s.s.Begin() + s, closeFunc := buildMock(t) + defer closeFunc() + txn, err := s.Begin() require.NoError(t, err) mockCtx := mock.NewContext() sc := mockCtx.GetSessionVars().StmtCtx @@ -398,9 +408,3 @@ func buildTableInfo(t *testing.T, sql string) *model.TableInfo { require.NoError(t, err) return tblInfo } - -func TestTearDownSuite(t *testing.T) { - s.dom.Close() - err := s.s.Close() - require.NoError(t, err) -} From f487249bfff3a9d71c72b2f067903c28ef23303d Mon Sep 17 00:00:00 2001 From: feitian124 Date: Thu, 26 Aug 2021 23:05:48 +0800 Subject: [PATCH 7/9] fix failed test --- table/tables/index_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/table/tables/index_test.go b/table/tables/index_test.go index dd565f5d11edc..e688c1f145b90 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -56,7 +56,7 @@ func buildMock(t *testing.T) (kv.Storage, func()) { } func TestIndex(t *testing.T) { - t.Parallel() + //t.Parallel() tblInfo := &model.TableInfo{ ID: 1, Indices: []*model.IndexInfo{ @@ -226,7 +226,7 @@ func TestIndex(t *testing.T) { } func TestCombineIndexSeek(t *testing.T) { - t.Parallel() + // t.Parallel() tblInfo := &model.TableInfo{ ID: 1, Indices: []*model.IndexInfo{ From 36b727ad81984a865735d064e56f68edd58c5cfc Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 27 Aug 2021 07:47:40 +0800 Subject: [PATCH 8/9] reuse existing testkits Signed-off-by: tison --- table/tables/index_test.go | 45 +++++++++++++------------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/table/tables/index_test.go b/table/tables/index_test.go index e688c1f145b90..7a9694c8cf418 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -27,12 +27,11 @@ import ( "github.com/pingcap/parser/terror" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx/stmtctx" - "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/tablecodec" + "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/codec" "github.com/pingcap/tidb/util/collate" @@ -41,20 +40,6 @@ import ( "github.com/stretchr/testify/require" ) -func buildMock(t *testing.T) (kv.Storage, func()) { - store, err := mockstore.NewMockStore() - require.NoError(t, err) - dom, err := session.BootstrapSession(store) - require.NoError(t, err) - - closeFunc := func() { - dom.Close() - err := store.Close() - require.NoError(t, err) - } - return store, closeFunc -} - func TestIndex(t *testing.T) { //t.Parallel() tblInfo := &model.TableInfo{ @@ -77,9 +62,9 @@ func TestIndex(t *testing.T) { index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) // Test ununiq index. - s, closeFunc := buildMock(t) - defer closeFunc() - txn, err := s.Begin() + store, clean := testkit.CreateMockStore(t) + defer clean() + txn, err := store.Begin() require.NoError(t, err) values := types.MakeDatums(1, 2) @@ -168,7 +153,7 @@ func TestIndex(t *testing.T) { index = tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) // Test uniq index. - txn, err = s.Begin() + txn, err = store.Begin() require.NoError(t, err) _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) @@ -204,7 +189,7 @@ func TestIndex(t *testing.T) { _, err = index.FetchValues(make([]types.Datum, 0), nil) require.NotNil(t, err) - txn, err = s.Begin() + txn, err = store.Begin() require.NoError(t, err) // Test the function of Next when the value of unique key is nil. @@ -247,9 +232,9 @@ func TestCombineIndexSeek(t *testing.T) { } index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) - s, closeFunc := buildMock(t) - defer closeFunc() - txn, err := s.Begin() + store, clean := testkit.CreateMockStore(t) + defer clean() + txn, err := store.Begin() require.NoError(t, err) mockCtx := mock.NewContext() @@ -280,9 +265,9 @@ func TestSingleColumnCommonHandle(t *testing.T) { idxNonUnique = idx } } - s, closeFunc := buildMock(t) - defer closeFunc() - txn, err := s.Begin() + store, clean := testkit.CreateMockStore(t) + defer clean() + txn, err := store.Begin() require.NoError(t, err) mockCtx := mock.NewContext() @@ -350,9 +335,9 @@ func TestMultiColumnCommonHandle(t *testing.T) { require.NotNil(t, a) require.NotNil(t, b) - s, closeFunc := buildMock(t) - defer closeFunc() - txn, err := s.Begin() + store, clean := testkit.CreateMockStore(t) + defer clean() + txn, err := store.Begin() require.NoError(t, err) mockCtx := mock.NewContext() sc := mockCtx.GetSessionVars().StmtCtx From c79f51a88c14344e92e4dfa6a375ec9e0a5f52da Mon Sep 17 00:00:00 2001 From: feitian124 Date: Sun, 5 Sep 2021 13:03:40 +0800 Subject: [PATCH 9/9] mv 2 test cases which can not parallel to separate file --- table/tables/index_serial_test.go | 244 ++++++++++++++++++++++++++++++ table/tables/index_test.go | 218 -------------------------- 2 files changed, 244 insertions(+), 218 deletions(-) create mode 100644 table/tables/index_serial_test.go diff --git a/table/tables/index_serial_test.go b/table/tables/index_serial_test.go new file mode 100644 index 0000000000000..a8fb4849c1304 --- /dev/null +++ b/table/tables/index_serial_test.go @@ -0,0 +1,244 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tables_test + +import ( + "context" + "io" + "testing" + "time" + + "github.com/pingcap/parser/model" + "github.com/pingcap/parser/mysql" + "github.com/pingcap/parser/terror" + "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/sessionctx/stmtctx" + "github.com/pingcap/tidb/table/tables" + "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/types" + "github.com/pingcap/tidb/util/mock" + "github.com/stretchr/testify/require" +) + +func TestIndex(t *testing.T) { + tblInfo := &model.TableInfo{ + ID: 1, + Indices: []*model.IndexInfo{ + { + ID: 2, + Name: model.NewCIStr("test"), + Columns: []*model.IndexColumn{ + {Offset: 0}, + {Offset: 1}, + }, + }, + }, + Columns: []*model.ColumnInfo{ + {ID: 1, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 0, FieldType: *types.NewFieldType(mysql.TypeVarchar)}, + {ID: 2, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 1, FieldType: *types.NewFieldType(mysql.TypeString)}, + }, + } + index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) + + // Test ununiq index. + store, clean := testkit.CreateMockStore(t) + defer clean() + txn, err := store.Begin() + require.NoError(t, err) + + values := types.MakeDatums(1, 2) + mockCtx := mock.NewContext() + _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) + require.NoError(t, err) + + it, err := index.SeekFirst(txn) + require.NoError(t, err) + + getValues, h, err := it.Next() + require.NoError(t, err) + require.Len(t, getValues, 2) + require.Equal(t, int64(1), getValues[0].GetInt64()) + require.Equal(t, int64(2), getValues[1].GetInt64()) + require.Equal(t, int64(1), h.IntValue()) + it.Close() + sc := &stmtctx.StatementContext{TimeZone: time.Local} + exist, _, err := index.Exist(sc, txn, values, kv.IntHandle(100)) + require.NoError(t, err) + require.False(t, exist) + + exist, _, err = index.Exist(sc, txn, values, kv.IntHandle(1)) + require.NoError(t, err) + require.True(t, exist) + + err = index.Delete(sc, txn, values, kv.IntHandle(1)) + require.NoError(t, err) + + it, err = index.SeekFirst(txn) + require.NoError(t, err) + + _, _, err = it.Next() + require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) + it.Close() + + _, err = index.Create(mockCtx, txn, values, kv.IntHandle(0), nil) + require.NoError(t, err) + + _, err = index.SeekFirst(txn) + require.NoError(t, err) + + _, hit, err := index.Seek(sc, txn, values) + require.NoError(t, err) + require.False(t, hit) + + err = index.Drop(txn) + require.NoError(t, err) + + it, hit, err = index.Seek(sc, txn, values) + require.NoError(t, err) + require.False(t, hit) + + _, _, err = it.Next() + require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) + it.Close() + + it, err = index.SeekFirst(txn) + require.NoError(t, err) + + _, _, err = it.Next() + require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) + it.Close() + + err = txn.Commit(context.Background()) + require.NoError(t, err) + + tblInfo = &model.TableInfo{ + ID: 2, + Indices: []*model.IndexInfo{ + { + ID: 3, + Name: model.NewCIStr("test"), + Unique: true, + Columns: []*model.IndexColumn{ + {Offset: 0}, + {Offset: 1}, + }, + }, + }, + Columns: []*model.ColumnInfo{ + {ID: 1, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 0, FieldType: *types.NewFieldType(mysql.TypeVarchar)}, + {ID: 2, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 1, FieldType: *types.NewFieldType(mysql.TypeString)}, + }, + } + index = tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) + + // Test uniq index. + txn, err = store.Begin() + require.NoError(t, err) + + _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) + require.NoError(t, err) + + _, err = index.Create(mockCtx, txn, values, kv.IntHandle(2), nil) + require.NotNil(t, err) + + it, err = index.SeekFirst(txn) + require.NoError(t, err) + + getValues, h, err = it.Next() + require.NoError(t, err) + require.Len(t, getValues, 2) + require.Equal(t, int64(1), getValues[0].GetInt64()) + require.Equal(t, int64(2), getValues[1].GetInt64()) + require.Equal(t, int64(1), h.IntValue()) + it.Close() + + exist, h, err = index.Exist(sc, txn, values, kv.IntHandle(1)) + require.NoError(t, err) + require.Equal(t, int64(1), h.IntValue()) + require.True(t, exist) + + exist, h, err = index.Exist(sc, txn, values, kv.IntHandle(2)) + require.NotNil(t, err) + require.Equal(t, int64(1), h.IntValue()) + require.True(t, exist) + + err = txn.Commit(context.Background()) + require.NoError(t, err) + + _, err = index.FetchValues(make([]types.Datum, 0), nil) + require.NotNil(t, err) + + txn, err = store.Begin() + require.NoError(t, err) + + // Test the function of Next when the value of unique key is nil. + values2 := types.MakeDatums(nil, nil) + _, err = index.Create(mockCtx, txn, values2, kv.IntHandle(2), nil) + require.NoError(t, err) + it, err = index.SeekFirst(txn) + require.NoError(t, err) + getValues, h, err = it.Next() + require.NoError(t, err) + require.Len(t, getValues, 2) + require.Equal(t, nil, getValues[0].GetInterface()) + require.Equal(t, nil, getValues[1].GetInterface()) + require.Equal(t, int64(2), h.IntValue()) + it.Close() + + err = txn.Commit(context.Background()) + require.NoError(t, err) +} + +func TestCombineIndexSeek(t *testing.T) { + tblInfo := &model.TableInfo{ + ID: 1, + Indices: []*model.IndexInfo{ + { + ID: 2, + Name: model.NewCIStr("test"), + Columns: []*model.IndexColumn{ + {Offset: 1}, + {Offset: 2}, + }, + }, + }, + Columns: []*model.ColumnInfo{ + {Offset: 0}, + {Offset: 1}, + {Offset: 2}, + }, + } + index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) + + store, clean := testkit.CreateMockStore(t) + defer clean() + txn, err := store.Begin() + require.NoError(t, err) + + mockCtx := mock.NewContext() + values := types.MakeDatums("abc", "def") + _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) + require.NoError(t, err) + + index2 := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) + sc := &stmtctx.StatementContext{TimeZone: time.Local} + iter, hit, err := index2.Seek(sc, txn, types.MakeDatums("abc", nil)) + require.NoError(t, err) + defer iter.Close() + require.False(t, hit) + _, h, err := iter.Next() + require.NoError(t, err) + require.Equal(t, int64(1), h.IntValue()) +} diff --git a/table/tables/index_test.go b/table/tables/index_test.go index 7a9694c8cf418..e0ce25dc2f2d5 100644 --- a/table/tables/index_test.go +++ b/table/tables/index_test.go @@ -16,18 +16,13 @@ package tables_test import ( "context" - "io" "testing" - "time" "github.com/pingcap/parser" "github.com/pingcap/parser/ast" "github.com/pingcap/parser/model" - "github.com/pingcap/parser/mysql" - "github.com/pingcap/parser/terror" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/tablecodec" @@ -40,219 +35,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestIndex(t *testing.T) { - //t.Parallel() - tblInfo := &model.TableInfo{ - ID: 1, - Indices: []*model.IndexInfo{ - { - ID: 2, - Name: model.NewCIStr("test"), - Columns: []*model.IndexColumn{ - {Offset: 0}, - {Offset: 1}, - }, - }, - }, - Columns: []*model.ColumnInfo{ - {ID: 1, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 0, FieldType: *types.NewFieldType(mysql.TypeVarchar)}, - {ID: 2, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 1, FieldType: *types.NewFieldType(mysql.TypeString)}, - }, - } - index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) - - // Test ununiq index. - store, clean := testkit.CreateMockStore(t) - defer clean() - txn, err := store.Begin() - require.NoError(t, err) - - values := types.MakeDatums(1, 2) - mockCtx := mock.NewContext() - _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - require.NoError(t, err) - - it, err := index.SeekFirst(txn) - require.NoError(t, err) - - getValues, h, err := it.Next() - require.NoError(t, err) - require.Len(t, getValues, 2) - require.Equal(t, int64(1), getValues[0].GetInt64()) - require.Equal(t, int64(2), getValues[1].GetInt64()) - require.Equal(t, int64(1), h.IntValue()) - it.Close() - sc := &stmtctx.StatementContext{TimeZone: time.Local} - exist, _, err := index.Exist(sc, txn, values, kv.IntHandle(100)) - require.NoError(t, err) - require.False(t, exist) - - exist, _, err = index.Exist(sc, txn, values, kv.IntHandle(1)) - require.NoError(t, err) - require.True(t, exist) - - err = index.Delete(sc, txn, values, kv.IntHandle(1)) - require.NoError(t, err) - - it, err = index.SeekFirst(txn) - require.NoError(t, err) - - _, _, err = it.Next() - require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) - it.Close() - - _, err = index.Create(mockCtx, txn, values, kv.IntHandle(0), nil) - require.NoError(t, err) - - _, err = index.SeekFirst(txn) - require.NoError(t, err) - - _, hit, err := index.Seek(sc, txn, values) - require.NoError(t, err) - require.False(t, hit) - - err = index.Drop(txn) - require.NoError(t, err) - - it, hit, err = index.Seek(sc, txn, values) - require.NoError(t, err) - require.False(t, hit) - - _, _, err = it.Next() - require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) - it.Close() - - it, err = index.SeekFirst(txn) - require.NoError(t, err) - - _, _, err = it.Next() - require.Truef(t, terror.ErrorEqual(err, io.EOF), "err %v", err) - it.Close() - - err = txn.Commit(context.Background()) - require.NoError(t, err) - - tblInfo = &model.TableInfo{ - ID: 2, - Indices: []*model.IndexInfo{ - { - ID: 3, - Name: model.NewCIStr("test"), - Unique: true, - Columns: []*model.IndexColumn{ - {Offset: 0}, - {Offset: 1}, - }, - }, - }, - Columns: []*model.ColumnInfo{ - {ID: 1, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 0, FieldType: *types.NewFieldType(mysql.TypeVarchar)}, - {ID: 2, Name: model.NewCIStr("c2"), State: model.StatePublic, Offset: 1, FieldType: *types.NewFieldType(mysql.TypeString)}, - }, - } - index = tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) - - // Test uniq index. - txn, err = store.Begin() - require.NoError(t, err) - - _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - require.NoError(t, err) - - _, err = index.Create(mockCtx, txn, values, kv.IntHandle(2), nil) - require.NotNil(t, err) - - it, err = index.SeekFirst(txn) - require.NoError(t, err) - - getValues, h, err = it.Next() - require.NoError(t, err) - require.Len(t, getValues, 2) - require.Equal(t, int64(1), getValues[0].GetInt64()) - require.Equal(t, int64(2), getValues[1].GetInt64()) - require.Equal(t, int64(1), h.IntValue()) - it.Close() - - exist, h, err = index.Exist(sc, txn, values, kv.IntHandle(1)) - require.NoError(t, err) - require.Equal(t, int64(1), h.IntValue()) - require.True(t, exist) - - exist, h, err = index.Exist(sc, txn, values, kv.IntHandle(2)) - require.NotNil(t, err) - require.Equal(t, int64(1), h.IntValue()) - require.True(t, exist) - - err = txn.Commit(context.Background()) - require.NoError(t, err) - - _, err = index.FetchValues(make([]types.Datum, 0), nil) - require.NotNil(t, err) - - txn, err = store.Begin() - require.NoError(t, err) - - // Test the function of Next when the value of unique key is nil. - values2 := types.MakeDatums(nil, nil) - _, err = index.Create(mockCtx, txn, values2, kv.IntHandle(2), nil) - require.NoError(t, err) - it, err = index.SeekFirst(txn) - require.NoError(t, err) - getValues, h, err = it.Next() - require.NoError(t, err) - require.Len(t, getValues, 2) - require.Equal(t, nil, getValues[0].GetInterface()) - require.Equal(t, nil, getValues[1].GetInterface()) - require.Equal(t, int64(2), h.IntValue()) - it.Close() - - err = txn.Commit(context.Background()) - require.NoError(t, err) -} - -func TestCombineIndexSeek(t *testing.T) { - // t.Parallel() - tblInfo := &model.TableInfo{ - ID: 1, - Indices: []*model.IndexInfo{ - { - ID: 2, - Name: model.NewCIStr("test"), - Columns: []*model.IndexColumn{ - {Offset: 1}, - {Offset: 2}, - }, - }, - }, - Columns: []*model.ColumnInfo{ - {Offset: 0}, - {Offset: 1}, - {Offset: 2}, - }, - } - index := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) - - store, clean := testkit.CreateMockStore(t) - defer clean() - txn, err := store.Begin() - require.NoError(t, err) - - mockCtx := mock.NewContext() - values := types.MakeDatums("abc", "def") - _, err = index.Create(mockCtx, txn, values, kv.IntHandle(1), nil) - require.NoError(t, err) - - index2 := tables.NewIndex(tblInfo.ID, tblInfo, tblInfo.Indices[0]) - sc := &stmtctx.StatementContext{TimeZone: time.Local} - iter, hit, err := index2.Seek(sc, txn, types.MakeDatums("abc", nil)) - require.NoError(t, err) - defer iter.Close() - require.False(t, hit) - _, h, err := iter.Next() - require.NoError(t, err) - require.Equal(t, int64(1), h.IntValue()) -} - func TestSingleColumnCommonHandle(t *testing.T) { t.Parallel() tblInfo := buildTableInfo(t, "create table t (a varchar(255) primary key, u int unique, nu int, index nu (nu))")