github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/tikv/mock-tikv/mvcc_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 mocktikv
    15  
    16  import (
    17  	"testing"
    18  
    19  	. "github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/kvproto/pkg/kvrpcpb"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/codec"
    22  )
    23  
    24  func TestT(t *testing.T) {
    25  	TestingT(t)
    26  }
    27  
    28  type testMockTiKVSuite struct {
    29  	store *MvccStore
    30  }
    31  
    32  var _ = Suite(&testMockTiKVSuite{})
    33  
    34  func encodeKey(s string) []byte {
    35  	return codec.EncodeBytes(nil, []byte(s))
    36  }
    37  
    38  func encodeKeys(ss []string) [][]byte {
    39  	var keys [][]byte
    40  	for _, s := range ss {
    41  		keys = append(keys, encodeKey(s))
    42  	}
    43  	return keys
    44  }
    45  
    46  func putMutations(kvpairs ...string) []*kvrpcpb.Mutation {
    47  	var mutations []*kvrpcpb.Mutation
    48  	for i := 0; i < len(kvpairs); i += 2 {
    49  		mutations = append(mutations, &kvrpcpb.Mutation{
    50  			Op:    kvrpcpb.Op_Put.Enum(),
    51  			Key:   encodeKey(kvpairs[i]),
    52  			Value: []byte(kvpairs[i+1]),
    53  		})
    54  	}
    55  	return mutations
    56  }
    57  
    58  func (s *testMockTiKVSuite) SetUpTest(c *C) {
    59  	s.store = NewMvccStore()
    60  }
    61  
    62  func (s *testMockTiKVSuite) mustGetNone(c *C, key string, ts uint64) {
    63  	val, err := s.store.Get(encodeKey(key), ts)
    64  	c.Assert(err, IsNil)
    65  	c.Assert(val, IsNil)
    66  }
    67  
    68  func (s *testMockTiKVSuite) mustGetErr(c *C, key string, ts uint64) {
    69  	val, err := s.store.Get(encodeKey(key), ts)
    70  	c.Assert(err, NotNil)
    71  	c.Assert(val, IsNil)
    72  }
    73  
    74  func (s *testMockTiKVSuite) mustGetOK(c *C, key string, ts uint64, expect string) {
    75  	val, err := s.store.Get(encodeKey(key), ts)
    76  	c.Assert(err, IsNil)
    77  	c.Assert(string(val), Equals, expect)
    78  }
    79  
    80  func (s *testMockTiKVSuite) mustPutOK(c *C, key, value string, startTS, commitTS uint64) {
    81  	errs := s.store.Prewrite(putMutations(key, value), encodeKey(key), startTS)
    82  	for _, err := range errs {
    83  		c.Assert(err, IsNil)
    84  	}
    85  	err := s.store.Commit([][]byte{encodeKey(key)}, startTS, commitTS)
    86  	c.Assert(err, IsNil)
    87  }
    88  
    89  func (s *testMockTiKVSuite) mustDeleteOK(c *C, key string, startTS, commitTS uint64) {
    90  	mutations := []*kvrpcpb.Mutation{
    91  		{
    92  			Op:  kvrpcpb.Op_Del.Enum(),
    93  			Key: encodeKey(key),
    94  		},
    95  	}
    96  	errs := s.store.Prewrite(mutations, encodeKey(key), startTS)
    97  	for _, err := range errs {
    98  		c.Assert(err, IsNil)
    99  	}
   100  	err := s.store.Commit([][]byte{encodeKey(key)}, startTS, commitTS)
   101  	c.Assert(err, IsNil)
   102  }
   103  
   104  func (s *testMockTiKVSuite) mustScanOK(c *C, start string, limit int, ts uint64, expect ...string) {
   105  	pairs := s.store.Scan(encodeKey(start), nil, limit, ts)
   106  	c.Assert(len(pairs)*2, Equals, len(expect))
   107  	for i := 0; i < len(pairs); i++ {
   108  		c.Assert(pairs[i].Err, IsNil)
   109  		c.Assert(pairs[i].Key, BytesEquals, encodeKey(expect[i*2]))
   110  		c.Assert(string(pairs[i].Value), Equals, expect[i*2+1])
   111  	}
   112  }
   113  
   114  func (s *testMockTiKVSuite) mustPrewriteOK(c *C, mutations []*kvrpcpb.Mutation, primary string, startTS uint64) {
   115  	errs := s.store.Prewrite(mutations, encodeKey(primary), startTS)
   116  	for _, err := range errs {
   117  		c.Assert(err, IsNil)
   118  	}
   119  }
   120  
   121  func (s *testMockTiKVSuite) mustCommitOK(c *C, keys []string, startTS, commitTS uint64) {
   122  	err := s.store.Commit(encodeKeys(keys), startTS, commitTS)
   123  	c.Assert(err, IsNil)
   124  }
   125  
   126  func (s *testMockTiKVSuite) mustCommitErr(c *C, keys []string, startTS, commitTS uint64) {
   127  	err := s.store.Commit(encodeKeys(keys), startTS, commitTS)
   128  	c.Assert(err, NotNil)
   129  }
   130  
   131  func (s *testMockTiKVSuite) mustRollbackOK(c *C, keys []string, startTS uint64) {
   132  	err := s.store.Rollback(encodeKeys(keys), startTS)
   133  	c.Assert(err, IsNil)
   134  }
   135  
   136  func (s *testMockTiKVSuite) mustRollbackErr(c *C, keys []string, startTS uint64) {
   137  	err := s.store.Rollback(encodeKeys(keys), startTS)
   138  	c.Assert(err, NotNil)
   139  }
   140  
   141  func (s *testMockTiKVSuite) mustCommitThenGetOK(c *C, key string, lockTS, commitTS, getTS uint64, expect string) {
   142  	val, err := s.store.CommitThenGet(encodeKey(key), lockTS, commitTS, getTS)
   143  	c.Assert(err, IsNil)
   144  	c.Assert(string(val), Equals, expect)
   145  }
   146  
   147  func (s *testMockTiKVSuite) mustRollbackThenGetOK(c *C, key string, lockTS uint64, expect string) {
   148  	val, err := s.store.RollbackThenGet(encodeKey(key), lockTS)
   149  	c.Assert(err, IsNil)
   150  	c.Assert(string(val), Equals, expect)
   151  }
   152  
   153  func (s *testMockTiKVSuite) TestGet(c *C) {
   154  	s.mustGetNone(c, "x", 10)
   155  	s.mustPutOK(c, "x", "x", 5, 10)
   156  	s.mustGetNone(c, "x", 9)
   157  	s.mustGetOK(c, "x", 10, "x")
   158  	s.mustGetOK(c, "x", 11, "x")
   159  }
   160  
   161  func (s *testMockTiKVSuite) TestDelete(c *C) {
   162  	s.mustPutOK(c, "x", "x5-10", 5, 10)
   163  	s.mustDeleteOK(c, "x", 15, 20)
   164  	s.mustGetNone(c, "x", 5)
   165  	s.mustGetNone(c, "x", 9)
   166  	s.mustGetOK(c, "x", 10, "x5-10")
   167  	s.mustGetOK(c, "x", 19, "x5-10")
   168  	s.mustGetNone(c, "x", 20)
   169  	s.mustGetNone(c, "x", 21)
   170  }
   171  
   172  func (s *testMockTiKVSuite) TestCleanupRollback(c *C) {
   173  	s.mustPutOK(c, "secondary", "s-0", 1, 2)
   174  	s.mustPrewriteOK(c, putMutations("primary", "p-5", "secondary", "s-5"), "primary", 5)
   175  	s.mustGetErr(c, "secondary", 8)
   176  	s.mustGetErr(c, "secondary", 12)
   177  	s.mustCommitOK(c, []string{"primary"}, 5, 10)
   178  	s.mustRollbackErr(c, []string{"primary"}, 5)
   179  	s.mustCommitThenGetOK(c, "secondary", 5, 10, 8, "s-0")
   180  	s.mustCommitThenGetOK(c, "secondary", 5, 10, 12, "s-5")
   181  	s.mustCommitThenGetOK(c, "secondary", 5, 10, 8, "s-0")
   182  	s.mustCommitThenGetOK(c, "secondary", 5, 10, 12, "s-5")
   183  }
   184  
   185  func (s *testMockTiKVSuite) TestScan(c *C) {
   186  	// ver10: A(10) - B(_) - C(10) - D(_) - E(10)
   187  	s.mustPutOK(c, "A", "A10", 5, 10)
   188  	s.mustPutOK(c, "C", "C10", 5, 10)
   189  	s.mustPutOK(c, "E", "E10", 5, 10)
   190  
   191  	checkV10 := func() {
   192  		s.mustScanOK(c, "", 0, 10)
   193  		s.mustScanOK(c, "", 1, 10, "A", "A10")
   194  		s.mustScanOK(c, "", 2, 10, "A", "A10", "C", "C10")
   195  		s.mustScanOK(c, "", 3, 10, "A", "A10", "C", "C10", "E", "E10")
   196  		s.mustScanOK(c, "", 4, 10, "A", "A10", "C", "C10", "E", "E10")
   197  		s.mustScanOK(c, "A", 3, 10, "A", "A10", "C", "C10", "E", "E10")
   198  		s.mustScanOK(c, "A\x00", 3, 10, "C", "C10", "E", "E10")
   199  		s.mustScanOK(c, "C", 4, 10, "C", "C10", "E", "E10")
   200  		s.mustScanOK(c, "F", 1, 10)
   201  	}
   202  	checkV10()
   203  
   204  	// ver20: A(10) - B(20) - C(10) - D(20) - E(10)
   205  	s.mustPutOK(c, "B", "B20", 15, 20)
   206  	s.mustPutOK(c, "D", "D20", 15, 20)
   207  
   208  	checkV20 := func() {
   209  		s.mustScanOK(c, "", 5, 20, "A", "A10", "B", "B20", "C", "C10", "D", "D20", "E", "E10")
   210  		s.mustScanOK(c, "C", 5, 20, "C", "C10", "D", "D20", "E", "E10")
   211  		s.mustScanOK(c, "D\x00", 1, 20, "E", "E10")
   212  	}
   213  	checkV10()
   214  	checkV20()
   215  
   216  	// ver30: A(_) - B(20) - C(10) - D(_) - E(10)
   217  	s.mustDeleteOK(c, "A", 25, 30)
   218  	s.mustDeleteOK(c, "D", 25, 30)
   219  
   220  	checkV30 := func() {
   221  		s.mustScanOK(c, "", 5, 30, "B", "B20", "C", "C10", "E", "E10")
   222  		s.mustScanOK(c, "A", 1, 30, "B", "B20")
   223  		s.mustScanOK(c, "C\x00", 5, 30, "E", "E10")
   224  	}
   225  	checkV10()
   226  	checkV20()
   227  	checkV30()
   228  
   229  	// ver40: A(_) - B(_) - C(40) - D(40) - E(10)
   230  	s.mustDeleteOK(c, "B", 35, 40)
   231  	s.mustPutOK(c, "C", "C40", 35, 40)
   232  	s.mustPutOK(c, "D", "D40", 35, 40)
   233  
   234  	checkV40 := func() {
   235  		s.mustScanOK(c, "", 5, 40, "C", "C40", "D", "D40", "E", "E10")
   236  		s.mustScanOK(c, "", 5, 100, "C", "C40", "D", "D40", "E", "E10")
   237  	}
   238  	checkV10()
   239  	checkV20()
   240  	checkV30()
   241  	checkV40()
   242  }