github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/mockstore/deadlock/deadlock_test.go (about)

     1  // Copyright 2021 TiKV Authors
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/mockstore/deadlock/deadlock_test.go
    19  //
    20  
    21  // Copyright 2019 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package deadlock
    36  
    37  import (
    38  	"testing"
    39  
    40  	"github.com/stretchr/testify/assert"
    41  )
    42  
    43  func TestDeadlock(t *testing.T) {
    44  	assert := assert.New(t)
    45  	detector := NewDetector()
    46  	err := detector.Detect(1, 2, 100)
    47  	assert.Nil(err)
    48  	err = detector.Detect(2, 3, 200)
    49  	assert.Nil(err)
    50  	err = detector.Detect(3, 1, 300)
    51  	assert.EqualError(err, "deadlock(200)")
    52  	detector.CleanUp(2)
    53  	list2 := detector.waitForMap[2]
    54  	assert.Nil(list2)
    55  
    56  	// After cycle is broken, no deadlock now.
    57  	err = detector.Detect(3, 1, 300)
    58  	assert.Nil(err)
    59  	list3 := detector.waitForMap[3]
    60  	assert.Len(list3.txns, 1)
    61  
    62  	// Different keyHash grows the list.
    63  	err = detector.Detect(3, 1, 400)
    64  	assert.Nil(err)
    65  	assert.Len(list3.txns, 2)
    66  
    67  	// Same waitFor and key hash doesn't grow the list.
    68  	err = detector.Detect(3, 1, 400)
    69  	assert.Nil(err)
    70  	assert.Len(list3.txns, 2)
    71  
    72  	detector.CleanUpWaitFor(3, 1, 300)
    73  	assert.Len(list3.txns, 1)
    74  	detector.CleanUpWaitFor(3, 1, 400)
    75  	list3 = detector.waitForMap[3]
    76  	assert.Nil(list3)
    77  	detector.Expire(1)
    78  	assert.Len(detector.waitForMap, 1)
    79  	detector.Expire(2)
    80  	assert.Len(detector.waitForMap, 0)
    81  }