github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/shardddl/pessimism/keeper_test.go (about)

     1  // Copyright 2020 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 pessimism
    15  
    16  import (
    17  	. "github.com/pingcap/check"
    18  )
    19  
    20  type testLockKeeper struct{}
    21  
    22  var _ = Suite(&testLockKeeper{})
    23  
    24  func (t *testLockKeeper) TestLockKeeper(c *C) {
    25  	var (
    26  		lk      = NewLockKeeper()
    27  		schema  = "foo"
    28  		table   = "bar"
    29  		DDLs    = []string{"ALTER TABLE bar ADD COLUMN c1 INT"}
    30  		task1   = "task1"
    31  		task2   = "task2"
    32  		source1 = "mysql-replica-1"
    33  		source2 = "mysql-replica-2"
    34  		info11  = NewInfo(task1, source1, schema, table, DDLs)
    35  		info12  = NewInfo(task1, source2, schema, table, DDLs)
    36  		info21  = NewInfo(task2, source1, schema, table, DDLs)
    37  	)
    38  
    39  	// lock with 2 sources.
    40  	lockID1, synced, remain, err := lk.TrySync(info11, []string{source1, source2})
    41  	c.Assert(err, IsNil)
    42  	c.Assert(lockID1, Equals, "task1-`foo`.`bar`")
    43  	c.Assert(synced, IsFalse)
    44  	c.Assert(remain, Equals, 1)
    45  	lockID1, synced, remain, err = lk.TrySync(info12, []string{source1, source2})
    46  	c.Assert(err, IsNil)
    47  	c.Assert(lockID1, Equals, "task1-`foo`.`bar`")
    48  	c.Assert(synced, IsTrue)
    49  	c.Assert(remain, Equals, 0)
    50  
    51  	// lock with only 1 source.
    52  	lockID2, synced, remain, err := lk.TrySync(info21, []string{source1})
    53  	c.Assert(err, IsNil)
    54  	c.Assert(lockID2, Equals, "task2-`foo`.`bar`")
    55  	c.Assert(synced, IsTrue)
    56  	c.Assert(remain, Equals, 0)
    57  
    58  	// find lock.
    59  	lock1 := lk.FindLock(lockID1)
    60  	c.Assert(lock1, NotNil)
    61  	c.Assert(lock1.ID, Equals, lockID1)
    62  	lock2 := lk.FindLock(lockID2)
    63  	c.Assert(lock2, NotNil)
    64  	c.Assert(lock2.ID, Equals, lockID2)
    65  	lockIDNotExists := "lock-not-exists"
    66  	c.Assert(lk.FindLock(lockIDNotExists), IsNil)
    67  
    68  	// all locks.
    69  	locks := lk.Locks()
    70  	c.Assert(locks, HasLen, 2)
    71  	c.Assert(locks[lockID1], Equals, lock1) // compare pointer
    72  	c.Assert(locks[lockID2], Equals, lock2)
    73  
    74  	// remove lock.
    75  	c.Assert(lk.RemoveLock(lockID1), IsTrue)
    76  	c.Assert(lk.RemoveLock(lockIDNotExists), IsFalse)
    77  	c.Assert(lk.Locks(), HasLen, 1)
    78  
    79  	// clear locks.
    80  	lk.Clear()
    81  
    82  	// no locks exist.
    83  	c.Assert(lk.Locks(), HasLen, 0)
    84  }