github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/ekv/txn_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 ekv
    15  
    16  import (
    17  	"errors"
    18  	"time"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    22  )
    23  
    24  var _ = Suite(&testTxnSuite{})
    25  
    26  type testTxnSuite struct {
    27  }
    28  
    29  func (s *testTxnSuite) SetUpTest(c *C) {
    30  }
    31  
    32  func (s *testTxnSuite) TearDownTest(c *C) {
    33  }
    34  
    35  func (s *testTxnSuite) TestBackOff(c *C) {
    36  	defer testleak.AfterTest(c)()
    37  	mustBackOff(c, 1, 2)
    38  	mustBackOff(c, 2, 4)
    39  	mustBackOff(c, 3, 8)
    40  	mustBackOff(c, 100000, 100)
    41  }
    42  
    43  func mustBackOff(c *C, cnt uint, sleep int) {
    44  	c.Assert(BackOff(cnt), LessEqual, sleep*int(time.Millisecond))
    45  }
    46  
    47  func (s *testTxnSuite) TestRetryExceedCountError(c *C) {
    48  	defer testleak.AfterTest(c)()
    49  	defer func(cnt uint) {
    50  		maxRetryCnt = cnt
    51  	}(maxRetryCnt)
    52  
    53  	maxRetryCnt = 5
    54  	err := RunInNewTxn(&mockStorage{}, true, func(txn Transaction) error {
    55  		return nil
    56  	})
    57  	c.Assert(err, NotNil)
    58  
    59  	err = RunInNewTxn(&mockStorage{}, true, func(txn Transaction) error {
    60  		return ErrTxnRetryable
    61  	})
    62  	c.Assert(err, NotNil)
    63  
    64  	err = RunInNewTxn(&mockStorage{}, true, func(txn Transaction) error {
    65  		return errors.New("do not retry")
    66  	})
    67  	c.Assert(err, NotNil)
    68  
    69  	var cfg InjectionConfig
    70  	err1 := errors.New("foo")
    71  	cfg.SetGetError(err1)
    72  	cfg.SetCommitError(err1)
    73  	storage := NewInjectedStore(newMockStorage(), &cfg)
    74  	err = RunInNewTxn(storage, true, func(txn Transaction) error {
    75  		return nil
    76  	})
    77  	c.Assert(err, NotNil)
    78  }
    79  
    80  func (s *testTxnSuite) TestBasicFunc(c *C) {
    81  	if IsMockCommitErrorEnable() {
    82  		defer MockCommitErrorEnable()
    83  	} else {
    84  		defer MockCommitErrorDisable()
    85  	}
    86  
    87  	MockCommitErrorEnable()
    88  	c.Assert(IsMockCommitErrorEnable(), IsTrue)
    89  	MockCommitErrorDisable()
    90  	c.Assert(IsMockCommitErrorEnable(), IsFalse)
    91  }