github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/filelock/filelock_test.go (about)

     1  // Copyright 2021 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 filelock
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  )
    22  
    23  type fileLockSuite struct{}
    24  
    25  var _ = check.SerialSuites(&fileLockSuite{})
    26  
    27  func Test(t *testing.T) { check.TestingT(t) }
    28  
    29  // TestBasicCase tests the basic scenarios of using FileLock and all operations are expected to be successful
    30  func (s *fileLockSuite) TestBasicCase(c *check.C) {
    31  	defer testleak.AfterTest(c)()
    32  
    33  	path := c.MkDir() + "/test_lock"
    34  
    35  	fileLock, err := NewFileLock(path)
    36  	c.Assert(err, check.IsNil)
    37  	defer fileLock.Close() //nolint:errcheck
    38  
    39  	fileLockCheck, err := NewFileLock(path)
    40  	c.Assert(err, check.IsNil)
    41  	defer fileLockCheck.Close() //nolint:errcheck
    42  
    43  	isLocked, err := fileLock.IsLocked()
    44  	c.Assert(err, check.IsNil)
    45  	c.Assert(isLocked, check.IsFalse)
    46  
    47  	err = fileLock.Lock()
    48  	c.Assert(err, check.IsNil)
    49  
    50  	isLocked, err = fileLockCheck.IsLocked()
    51  	c.Assert(err, check.IsNil)
    52  	c.Assert(isLocked, check.IsTrue)
    53  
    54  	err = fileLock.Unlock()
    55  	c.Assert(err, check.IsNil)
    56  
    57  	isLocked, err = fileLockCheck.IsLocked()
    58  	c.Assert(err, check.IsNil)
    59  	c.Assert(isLocked, check.IsFalse)
    60  
    61  	isLocked, err = fileLock.IsLocked()
    62  	c.Assert(err, check.IsNil)
    63  	c.Assert(isLocked, check.IsFalse)
    64  }
    65  
    66  // TestBadPath tests the case where the file path is not accessible
    67  func (s *fileLockSuite) TestBadPath(c *check.C) {
    68  	defer testleak.AfterTest(c)()
    69  
    70  	path := c.MkDir() + "/bad_path/bad_file"
    71  
    72  	_, err := NewFileLock(path)
    73  	c.Assert(err, check.ErrorMatches, ".*no such file.*")
    74  }
    75  
    76  // TestBadPath tests the case where the file is locked twice
    77  // We do not expect this to happen in TiCDC.
    78  func (s *fileLockSuite) TestDuplicateLocking(c *check.C) {
    79  	defer testleak.AfterTest(c)()
    80  
    81  	path := c.MkDir() + "/test_lock"
    82  
    83  	fileLock, err := NewFileLock(path)
    84  	c.Assert(err, check.IsNil)
    85  	defer fileLock.Close() //nolint:errcheck
    86  
    87  	fileLock2, err := NewFileLock(path)
    88  	c.Assert(err, check.IsNil)
    89  	defer fileLock2.Close() //nolint:errcheck
    90  
    91  	err = fileLock.Lock()
    92  	c.Assert(err, check.IsNil)
    93  
    94  	err = fileLock2.Lock()
    95  	c.Assert(err, check.ErrorMatches, ".*ErrConflictingFileLocks.*")
    96  }