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

statistics: migrate test-infra to testify for handle/update_test.go #30006

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion statistics/handle/gc_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/require"
)

func createTestKitAndDom(t *testing.T) (*testkit.TestKit, *domain.Domain, func()) {
func createTestKitAndDom(t testing.TB) (*testkit.TestKit, *domain.Domain, func()) {
store, dom, err := newStoreWithBootstrap()
require.NoError(t, err)
clean := func() {
Expand Down
55 changes: 54 additions & 1 deletion statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,61 @@ import (
"github.com/pingcap/tidb/util/israce"
"github.com/pingcap/tidb/util/ranger"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
"github.com/tikv/client-go/v2/oracle"
)

func TestT(t *testing.T) {
TestingT(t)
}

// TODO replace cleanEnv with createTestKitAndDom in gc_series_test.go when migrate this file
var _ = SerialSuites(&testSerialStatsSuite{})

type testSerialStatsSuite struct {
store kv.Storage
do *domain.Domain
}

type testSuiteBase struct {
store kv.Storage
do *domain.Domain
}

var _ = Suite(&testStatsSuite{})

type testStatsSuite struct {
testSuiteBase
}

func (s *testSerialStatsSuite) SetUpSuite(c *C) {
testleak.BeforeTest()
// Add the hook here to avoid data race.
var err error
s.store, s.do, err = newStoreWithBootstrap()
c.Assert(err, IsNil)
}

func (s *testSerialStatsSuite) TearDownSuite(c *C) {
s.do.Close()
s.store.Close()
testleak.AfterTest(c)()
}

func (s *testSuiteBase) SetUpSuite(c *C) {
testleak.BeforeTest()
// Add the hook here to avoid data race.
var err error
s.store, s.do, err = newStoreWithBootstrap()
c.Assert(err, IsNil)
}

func (s *testSuiteBase) TearDownSuite(c *C) {
s.do.Close()
s.store.Close()
testleak.AfterTest(c)()
}

// TODO replace cleanEnv with createTestKitAndDom in gc_test.go when migrate this file
func cleanEnv(c *C, store kv.Storage, do *domain.Domain) {
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
Expand Down Expand Up @@ -170,6 +217,7 @@ func (s *testStatsSuite) TestStatsCacheMemTracker(c *C) {
c.Assert(statsTbl.Pseudo, IsFalse)
}

// TODO requireTableEqual
func assertTableEqual(c *C, a *statistics.Table, b *statistics.Table) {
c.Assert(a.Count, Equals, b.Count)
c.Assert(a.ModifyCount, Equals, b.ModifyCount)
Expand Down Expand Up @@ -508,6 +556,7 @@ func (s *testStatsSuite) TestLoadHist(c *C) {
func (s *testStatsSuite) TestInitStats(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("set @@tidb_analyze_version = 1")
testKit.MustExec("use test")
testKit.MustExec("create table t(a int, b int, c int, primary key(a), key idx(b))")
testKit.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,7,8)")
Expand Down Expand Up @@ -584,6 +633,7 @@ func (s *testStatsSuite) TestReloadExtStatsLockRelease(c *C) {
func (s *testStatsSuite) TestLoadStats(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("set @@tidb_analyze_version = 1")
testKit.MustExec("use test")
testKit.MustExec("create table t(a int, b int, c int, primary key(a), key idx(b))")
testKit.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3)")
Expand Down Expand Up @@ -796,6 +846,7 @@ func (s *testStatsSuite) TestAnalyzeVirtualCol(c *C) {
func (s *testStatsSuite) TestShowGlobalStats(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_analyze_version = 1")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("set @@tidb_partition_prune_mode = 'static'")
Expand Down Expand Up @@ -2469,6 +2520,7 @@ func (s *testStatsSuite) TestDuplicateFMSketch(c *C) {
func (s *testStatsSuite) TestIndexFMSketch(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_analyze_version = 1")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, c int, index ia(a), index ibc(b, c)) partition by hash(a) partitions 3")
Expand Down Expand Up @@ -2900,6 +2952,7 @@ func (s *testSerialStatsSuite) TestFastAnalyzeColumnHistWithNullValue(c *C) {
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t (a int)")
testKit.MustExec("insert into t values (1), (2), (3), (4), (NULL)")
testKit.MustExec("set @@tidb_analyze_version = 1")
testKit.MustExec("set @@tidb_enable_fast_analyze=1")
defer testKit.MustExec("set @@tidb_enable_fast_analyze=0")
testKit.MustExec("analyze table t with 0 topn, 2 buckets")
Expand Down
58 changes: 57 additions & 1 deletion statistics/handle/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,76 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package handle
package handle_test

import (
"fmt"
"math"
"os"
"strings"
"testing"

"github.com/pingcap/log"
"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var registeredHook *logHook

func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
testbridge.WorkaroundGoCheckFlags()
registeredHook = registerHook()
goleak.VerifyTestMain(m, opts...)
}

func registerHook() *logHook {
conf := &log.Config{Level: os.Getenv("log_level"), File: log.FileLogConfig{}}
_, r, _ := log.InitLogger(conf)
hook := &logHook{r.Core, ""}
lg := zap.New(hook)
log.ReplaceGlobals(lg, r)
return hook
}

type logHook struct {
zapcore.Core
results string
}

func (h *logHook) Write(entry zapcore.Entry, fields []zapcore.Field) error {
message := entry.Message
if idx := strings.Index(message, "[stats"); idx != -1 {
h.results = h.results + message
for _, f := range fields {
h.results = h.results + ", " + f.Key + "=" + h.field2String(f)
}
}
return nil
}

func (h *logHook) field2String(field zapcore.Field) string {
switch field.Type {
case zapcore.StringType:
return field.String
case zapcore.Int64Type, zapcore.Int32Type, zapcore.Uint32Type, zapcore.Uint64Type:
return fmt.Sprintf("%v", field.Integer)
case zapcore.Float64Type:
return fmt.Sprintf("%v", math.Float64frombits(uint64(field.Integer)))
case zapcore.StringerType:
return field.Interface.(fmt.Stringer).String()
}
return "not support"
}

func (h *logHook) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if h.Enabled(e.Level) {
return ce.AddCore(e, h)
}
return ce
}
Loading