github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/tikv/split_test.go (about) 1 // Copyright 2016 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 tikv 15 16 import ( 17 . "github.com/insionng/yougam/libraries/pingcap/check" 18 "github.com/insionng/yougam/libraries/pingcap/tidb/kv" 19 "github.com/insionng/yougam/libraries/pingcap/tidb/store/tikv/mock-tikv" 20 ) 21 22 type testSplitSuite struct { 23 cluster *mocktikv.Cluster 24 store *tikvStore 25 } 26 27 var _ = Suite(&testSplitSuite{}) 28 29 func (s *testSplitSuite) SetUpTest(c *C) { 30 s.cluster = mocktikv.NewCluster() 31 mocktikv.BootstrapWithSingleStore(s.cluster) 32 mvccStore := mocktikv.NewMvccStore() 33 clientFactory := mockClientFactory(s.cluster, mvccStore) 34 s.store = newTikvStore("mock-tikv-store", mocktikv.NewPDClient(s.cluster), clientFactory) 35 } 36 37 func (s *testSplitSuite) begin(c *C) *tikvTxn { 38 txn, err := s.store.Begin() 39 c.Assert(err, IsNil) 40 return txn.(*tikvTxn) 41 } 42 43 func (s *testSplitSuite) split(c *C, regionID uint64, key []byte) { 44 newRegionID, peerID := s.cluster.AllocID(), s.cluster.AllocID() 45 s.cluster.Split(regionID, newRegionID, key, []uint64{peerID}, peerID) 46 } 47 48 func (s *testSplitSuite) TestSplitBatchGet(c *C) { 49 firstRegion, err := s.store.regionCache.GetRegion([]byte("a")) 50 c.Assert(err, IsNil) 51 52 txn := s.begin(c) 53 snapshot := newTiKVSnapshot(s.store, kv.Version{Ver: txn.StartTS()}) 54 multiGets, err := snapshot.makeBatchGetReqs([]kv.Key{kv.Key("a"), kv.Key("b"), kv.Key("c")}) 55 c.Assert(err, IsNil) 56 57 s.split(c, firstRegion.GetID(), []byte("b")) 58 s.store.regionCache.DropRegion(firstRegion.VerID()) 59 60 for _, g := range multiGets { 61 // mock-tikv will panic if it meets a not-in-region key. 62 snapshot.doBatchGet(g) 63 } 64 }