github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/tikv/lock_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 ) 19 20 type testLockSuite struct { 21 store *tikvStore 22 } 23 24 var _ = Suite(&testLockSuite{}) 25 26 func (s *testLockSuite) SetUpTest(c *C) { 27 s.store = NewMockTikvStore().(*tikvStore) 28 } 29 30 func (s *testLockSuite) lockKey(c *C, key, value, primaryKey, primaryValue []byte, commitPrimary bool) { 31 txn, err := newTiKVTxn(s.store) 32 c.Assert(err, IsNil) 33 if len(value) > 0 { 34 err = txn.Set(key, value) 35 } else { 36 err = txn.Delete(key) 37 } 38 c.Assert(err, IsNil) 39 if len(primaryValue) > 0 { 40 err = txn.Set(primaryKey, primaryValue) 41 } else { 42 err = txn.Delete(primaryKey) 43 } 44 c.Assert(err, IsNil) 45 committer, err := newTxnCommitter(txn) 46 c.Assert(err, IsNil) 47 committer.keys = [][]byte{primaryKey, key} 48 49 err = committer.prewriteKeys(committer.keys) 50 c.Assert(err, IsNil) 51 52 if commitPrimary { 53 committer.commitTS, err = s.store.oracle.GetTimestamp() 54 c.Assert(err, IsNil) 55 err = committer.commitKeys([][]byte{primaryKey}) 56 c.Assert(err, IsNil) 57 } 58 } 59 60 func (s *testLockSuite) putAlphabets(c *C) { 61 for ch := byte('a'); ch <= byte('z'); ch++ { 62 s.putKV(c, []byte{ch}, []byte{ch}) 63 } 64 } 65 66 func (s *testLockSuite) putKV(c *C, key, value []byte) { 67 txn, err := s.store.Begin() 68 c.Assert(err, IsNil) 69 err = txn.Set(key, value) 70 c.Assert(err, IsNil) 71 err = txn.Commit() 72 c.Assert(err, IsNil) 73 } 74 75 func (s *testLockSuite) TestScanLockResolve(c *C) { 76 s.putAlphabets(c) 77 s.putKV(c, []byte("c"), []byte("cc")) 78 s.lockKey(c, []byte("c"), []byte("c"), []byte("z1"), []byte("z1"), true) 79 s.lockKey(c, []byte("d"), []byte("dd"), []byte("z2"), []byte("z2"), false) 80 s.lockKey(c, []byte("foo"), []byte("foo"), []byte("z3"), []byte("z3"), false) 81 s.putKV(c, []byte("bar"), []byte("bar")) 82 s.lockKey(c, []byte("bar"), nil, []byte("z4"), []byte("z4"), true) 83 84 txn, err := s.store.Begin() 85 c.Assert(err, IsNil) 86 iter, err := txn.Seek([]byte("a")) 87 c.Assert(err, IsNil) 88 for ch := byte('a'); ch <= byte('z'); ch++ { 89 c.Assert(iter.Valid(), IsTrue) 90 c.Assert([]byte(iter.Key()), BytesEquals, []byte{ch}) 91 c.Assert([]byte(iter.Value()), BytesEquals, []byte{ch}) 92 c.Assert(iter.Next(), IsNil) 93 } 94 }