github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/ddl/bg_worker_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/kv"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/meta"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/model"
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/mock"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    25  )
    26  
    27  func (s *testDDLSuite) TestDropSchemaError(c *C) {
    28  	defer testleak.AfterTest(c)()
    29  	store := testCreateStore(c, "test_drop_schema")
    30  	defer store.Close()
    31  
    32  	lease := 50 * time.Millisecond
    33  	d := newDDL(store, nil, nil, lease)
    34  	defer d.close()
    35  
    36  	job := &model.Job{
    37  		SchemaID: 1,
    38  		Type:     model.ActionDropSchema,
    39  		Args: []interface{}{&model.DBInfo{
    40  			Name: model.CIStr{O: "test"},
    41  		}},
    42  	}
    43  	d.prepareBgJob(job)
    44  	d.startBgJob(job.Type)
    45  
    46  	time.Sleep(lease)
    47  	verifyBgJobState(c, d, job, model.JobDone)
    48  }
    49  
    50  func verifyBgJobState(c *C, d *ddl, job *model.Job, state model.JobState) {
    51  	kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error {
    52  		t := meta.NewMeta(txn)
    53  		historyBgJob, err := t.GetHistoryBgJob(job.ID)
    54  		c.Assert(err, IsNil)
    55  		c.Assert(historyBgJob, NotNil)
    56  		c.Assert(historyBgJob.State, Equals, state)
    57  
    58  		return nil
    59  	})
    60  }
    61  
    62  func (s *testDDLSuite) TestDropTableError(c *C) {
    63  	defer testleak.AfterTest(c)()
    64  	store := testCreateStore(c, "test_drop_table")
    65  	defer store.Close()
    66  
    67  	lease := 50 * time.Millisecond
    68  	d := newDDL(store, nil, nil, lease)
    69  	defer d.close()
    70  
    71  	dbInfo := testSchemaInfo(c, d, "test")
    72  	testCreateSchema(c, mock.NewContext(), d, dbInfo)
    73  
    74  	job := &model.Job{
    75  		SchemaID: dbInfo.ID,
    76  		Type:     model.ActionDropTable,
    77  		Args: []interface{}{&model.TableInfo{
    78  			ID:   1,
    79  			Name: model.CIStr{O: "t"},
    80  		}},
    81  	}
    82  	d.prepareBgJob(job)
    83  	d.startBgJob(job.Type)
    84  
    85  	time.Sleep(lease)
    86  	verifyBgJobState(c, d, job, model.JobDone)
    87  }
    88  
    89  func (s *testDDLSuite) TestInvalidBgJobType(c *C) {
    90  	defer testleak.AfterTest(c)()
    91  	store := testCreateStore(c, "test_invalid_bg_job_type")
    92  	defer store.Close()
    93  
    94  	lease := 50 * time.Millisecond
    95  	d := newDDL(store, nil, nil, lease)
    96  	defer d.close()
    97  
    98  	job := &model.Job{
    99  		SchemaID: 1,
   100  		TableID:  1,
   101  		Type:     model.ActionCreateTable,
   102  	}
   103  	d.prepareBgJob(job)
   104  	d.startBgJob(model.ActionDropTable)
   105  
   106  	time.Sleep(lease)
   107  	verifyBgJobState(c, d, job, model.JobCancelled)
   108  }