github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/deadlock/deadlock_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 deadlock 15 16 import ( 17 "fmt" 18 "testing" 19 20 . "github.com/whtcorpsinc/check" 21 ) 22 23 func TestT(t *testing.T) { 24 TestingT(t) 25 } 26 27 var _ = Suite(&testDeadlockSuite{}) 28 29 type testDeadlockSuite struct{} 30 31 func (s *testDeadlockSuite) TestDeadlock(c *C) { 32 detector := NewDetector() 33 err := detector.Detect(1, 2, 100) 34 c.Assert(err, IsNil) 35 err = detector.Detect(2, 3, 200) 36 c.Assert(err, IsNil) 37 err = detector.Detect(3, 1, 300) 38 c.Assert(err, NotNil) 39 c.Assert(err.Error(), Equals, fmt.Sprintf("deadlock(200)")) 40 detector.CleanUp(2) 41 list2 := detector.waitForMap[2] 42 c.Assert(list2, IsNil) 43 44 // After cycle is broken, no deadlock now. 45 err = detector.Detect(3, 1, 300) 46 c.Assert(err, IsNil) 47 list3 := detector.waitForMap[3] 48 c.Assert(list3.txns, HasLen, 1) 49 50 // Different keyHash grows the list. 51 err = detector.Detect(3, 1, 400) 52 c.Assert(err, IsNil) 53 c.Assert(list3.txns, HasLen, 2) 54 55 // Same waitFor and key hash doesn't grow the list. 56 err = detector.Detect(3, 1, 400) 57 c.Assert(err, IsNil) 58 c.Assert(list3.txns, HasLen, 2) 59 60 detector.CleanUpWaitFor(3, 1, 300) 61 c.Assert(list3.txns, HasLen, 1) 62 detector.CleanUpWaitFor(3, 1, 400) 63 list3 = detector.waitForMap[3] 64 c.Assert(list3, IsNil) 65 detector.Expire(1) 66 c.Assert(detector.waitForMap, HasLen, 1) 67 detector.Expire(2) 68 c.Assert(detector.waitForMap, HasLen, 0) 69 }