github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/stat_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package dbs
    15  
    16  import (
    17  	"context"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/failpoint"
    21  	"github.com/whtcorpsinc/milevadb/types"
    22  )
    23  
    24  var _ = Suite(&testStatSuite{})
    25  var _ = SerialSuites(&testSerialStatSuite{})
    26  
    27  type testStatSuite struct {
    28  }
    29  
    30  func (s *testStatSuite) SetUpSuite(c *C) {
    31  }
    32  
    33  func (s *testStatSuite) TearDownSuite(c *C) {
    34  }
    35  
    36  type testSerialStatSuite struct {
    37  }
    38  
    39  func (s *testStatSuite) getDBSSchemaVer(c *C, d *dbs) int64 {
    40  	m, err := d.Stats(nil)
    41  	c.Assert(err, IsNil)
    42  	v := m[dbsSchemaVersion]
    43  	return v.(int64)
    44  }
    45  
    46  func (s *testSerialStatSuite) TestDBSStatsInfo(c *C) {
    47  	causetstore := testCreateStore(c, "test_stat")
    48  	defer causetstore.Close()
    49  
    50  	d := testNewDBSAndStart(
    51  		context.Background(),
    52  		c,
    53  		WithStore(causetstore),
    54  		WithLease(testLease),
    55  	)
    56  	defer d.Stop()
    57  
    58  	dbInfo := testSchemaInfo(c, d, "test")
    59  	testCreateSchema(c, testNewContext(d), d, dbInfo)
    60  	tblInfo := testBlockInfo(c, d, "t", 2)
    61  	ctx := testNewContext(d)
    62  	testCreateBlock(c, ctx, d, dbInfo, tblInfo)
    63  
    64  	t := testGetBlock(c, d, dbInfo.ID, tblInfo.ID)
    65  	// insert t values (1, 1), (2, 2), (3, 3)
    66  	_, err := t.AddRecord(ctx, types.MakeCausets(1, 1))
    67  	c.Assert(err, IsNil)
    68  	_, err = t.AddRecord(ctx, types.MakeCausets(2, 2))
    69  	c.Assert(err, IsNil)
    70  	_, err = t.AddRecord(ctx, types.MakeCausets(3, 3))
    71  	c.Assert(err, IsNil)
    72  	txn, err := ctx.Txn(true)
    73  	c.Assert(err, IsNil)
    74  	err = txn.Commit(context.Background())
    75  	c.Assert(err, IsNil)
    76  
    77  	job := buildCreateIdxJob(dbInfo, tblInfo, true, "idx", "c1")
    78  
    79  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/checkBackfillWorkerNum", `return(true)`), IsNil)
    80  	defer func() {
    81  		c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/checkBackfillWorkerNum"), IsNil)
    82  	}()
    83  
    84  	done := make(chan error, 1)
    85  	go func() {
    86  		done <- d.doDBSJob(ctx, job)
    87  	}()
    88  
    89  	exit := false
    90  	for !exit {
    91  		select {
    92  		case err := <-done:
    93  			c.Assert(err, IsNil)
    94  			exit = true
    95  		case <-TestCheckWorkerNumCh:
    96  			varMap, err := d.Stats(nil)
    97  			c.Assert(err, IsNil)
    98  			c.Assert(varMap[dbsJobReorgHandle], Equals, "1")
    99  		}
   100  	}
   101  }