github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/ddl/stat_test.go (about)

     1  // Copyright 2015 PingCAP, 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 ddl
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/model"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/mock"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    23  )
    24  
    25  var _ = Suite(&testStatSuite{})
    26  
    27  type testStatSuite struct {
    28  }
    29  
    30  func (s *testStatSuite) getDDLSchemaVer(c *C, d *ddl) int64 {
    31  	m, err := d.Stats()
    32  	c.Assert(err, IsNil)
    33  	v := m[ddlSchemaVersion]
    34  	return v.(int64)
    35  }
    36  
    37  func (s *testStatSuite) TestStat(c *C) {
    38  	defer testleak.AfterTest(c)()
    39  	store := testCreateStore(c, "test_stat")
    40  	defer store.Close()
    41  
    42  	lease := 50 * time.Millisecond
    43  
    44  	d := newDDL(store, nil, nil, lease)
    45  	defer d.close()
    46  
    47  	time.Sleep(lease)
    48  
    49  	dbInfo := testSchemaInfo(c, d, "test")
    50  	testCreateSchema(c, mock.NewContext(), d, dbInfo)
    51  
    52  	m, err := d.Stats()
    53  	c.Assert(err, IsNil)
    54  	c.Assert(m[ddlOwnerID], Equals, d.uuid)
    55  
    56  	job := &model.Job{
    57  		SchemaID: dbInfo.ID,
    58  		Type:     model.ActionDropSchema,
    59  		Args:     []interface{}{dbInfo.Name},
    60  	}
    61  
    62  	ctx := mock.NewContext()
    63  	done := make(chan error, 1)
    64  	go func() {
    65  		done <- d.doDDLJob(ctx, job)
    66  	}()
    67  
    68  	ticker := time.NewTicker(d.lease * 1)
    69  	defer ticker.Stop()
    70  
    71  	ver := s.getDDLSchemaVer(c, d)
    72  LOOP:
    73  	for {
    74  		select {
    75  		case <-ticker.C:
    76  			d.close()
    77  			c.Assert(s.getDDLSchemaVer(c, d), GreaterEqual, ver)
    78  			d.start()
    79  		case err := <-done:
    80  			c.Assert(err, IsNil)
    81  			m, err := d.Stats()
    82  			c.Assert(err, IsNil)
    83  			c.Assert(m[bgOwnerID], Equals, d.uuid)
    84  			break LOOP
    85  		}
    86  	}
    87  }