github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/kv/txn_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 kv
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    21  )
    22  
    23  var _ = Suite(&testTxnSuite{})
    24  
    25  type testTxnSuite struct {
    26  }
    27  
    28  func (s *testTxnSuite) SetUpTest(c *C) {
    29  }
    30  
    31  func (s *testTxnSuite) TearDownTest(c *C) {
    32  }
    33  
    34  func (s *testTxnSuite) TestBackOff(c *C) {
    35  	defer testleak.AfterTest(c)()
    36  	mustBackOff(c, 1, 2)
    37  	mustBackOff(c, 2, 4)
    38  	mustBackOff(c, 3, 8)
    39  	mustBackOff(c, 100000, 100)
    40  }
    41  
    42  func mustBackOff(c *C, cnt, sleep int) {
    43  	c.Assert(BackOff(cnt), LessEqual, sleep*int(time.Millisecond))
    44  }
    45  
    46  // mockTxn is a txn that returns a retryAble error when called Commit.
    47  type mockTxn struct {
    48  }
    49  
    50  // Always returns a retryable error
    51  func (t *mockTxn) Commit() error {
    52  	return ErrRetryable
    53  }
    54  
    55  func (t *mockTxn) Rollback() error {
    56  	return nil
    57  }
    58  
    59  func (t *mockTxn) String() string {
    60  	return ""
    61  }
    62  
    63  func (t *mockTxn) LockKeys(keys ...Key) error {
    64  	return nil
    65  }
    66  
    67  func (t *mockTxn) SetOption(opt Option, val interface{}) {
    68  	return
    69  }
    70  
    71  func (t *mockTxn) DelOption(opt Option) {
    72  	return
    73  }
    74  
    75  func (t *mockTxn) IsReadOnly() bool {
    76  	return true
    77  }
    78  
    79  func (t *mockTxn) GetClient() Client {
    80  	return nil
    81  }
    82  
    83  func (t *mockTxn) StartTS() uint64 {
    84  	return uint64(0)
    85  }
    86  func (t *mockTxn) Get(k Key) ([]byte, error) {
    87  	return nil, nil
    88  }
    89  
    90  func (t *mockTxn) Seek(k Key) (Iterator, error) {
    91  	return nil, nil
    92  }
    93  
    94  func (t *mockTxn) SeekReverse(k Key) (Iterator, error) {
    95  	return nil, nil
    96  }
    97  
    98  func (t *mockTxn) Set(k Key, v []byte) error {
    99  	return nil
   100  }
   101  func (t *mockTxn) Delete(k Key) error {
   102  	return nil
   103  }
   104  
   105  // mockStorage is used to start a must commit-failed txn
   106  type mockStorage struct {
   107  }
   108  
   109  func (s *mockStorage) Begin() (Transaction, error) {
   110  	return &mockTxn{}, nil
   111  
   112  }
   113  func (s *mockStorage) GetSnapshot(ver Version) (Snapshot, error) {
   114  	return nil, nil
   115  }
   116  func (s *mockStorage) Close() error {
   117  	return nil
   118  }
   119  
   120  func (s *mockStorage) UUID() string {
   121  	return ""
   122  }
   123  
   124  // CurrentVersion returns current max committed version.
   125  func (s *mockStorage) CurrentVersion() (Version, error) {
   126  	return Version{uint64(1)}, nil
   127  }
   128  
   129  func (s *testTxnSuite) TestRetryExceedCountError(c *C) {
   130  	defer testleak.AfterTest(c)()
   131  	maxRetryCnt = 5
   132  	err := RunInNewTxn(&mockStorage{}, true, func(txn Transaction) error {
   133  		return nil
   134  	})
   135  	c.Assert(err, NotNil)
   136  	maxRetryCnt = 100
   137  }