github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/fsutil/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 fsutil
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  // TestBasicCase tests the basic scenarios of using FileLock and all operations are expected to be successful
    23  func TestBasicCase(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	path := t.TempDir() + "/test_lock"
    27  
    28  	fileLock, err := NewFileLock(path)
    29  	require.Nil(t, err)
    30  	defer fileLock.Close() //nolint:errcheck
    31  
    32  	fileLockCheck, err := NewFileLock(path)
    33  	require.Nil(t, err)
    34  	defer fileLockCheck.Close() //nolint:errcheck
    35  
    36  	isLocked, err := fileLock.IsLocked()
    37  	require.Nil(t, err)
    38  	require.False(t, isLocked)
    39  
    40  	err = fileLock.Lock()
    41  	require.Nil(t, err)
    42  
    43  	isLocked, err = fileLockCheck.IsLocked()
    44  	require.Nil(t, err)
    45  	require.True(t, isLocked)
    46  
    47  	err = fileLock.Unlock()
    48  	require.Nil(t, err)
    49  
    50  	isLocked, err = fileLockCheck.IsLocked()
    51  	require.Nil(t, err)
    52  	require.False(t, isLocked)
    53  
    54  	isLocked, err = fileLock.IsLocked()
    55  	require.Nil(t, err)
    56  	require.False(t, isLocked)
    57  }
    58  
    59  // TestBadPath tests the case where the file path is not accessible
    60  func TestBadPath(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	path := t.TempDir() + "/bad_path/bad_file"
    64  
    65  	_, err := NewFileLock(path)
    66  	require.Regexp(t, ".*no such file.*", err)
    67  }
    68  
    69  // TestBadPath tests the case where the file is locked twice
    70  // We do not expect this to happen in TiCDC.
    71  func TestDuplicateLocking(t *testing.T) {
    72  	t.Parallel()
    73  
    74  	path := t.TempDir() + "/test_lock"
    75  
    76  	fileLock, err := NewFileLock(path)
    77  	require.Nil(t, err)
    78  	defer fileLock.Close() //nolint:errcheck
    79  
    80  	fileLock2, err := NewFileLock(path)
    81  	require.Nil(t, err)
    82  	defer fileLock2.Close() //nolint:errcheck
    83  
    84  	err = fileLock.Lock()
    85  	require.Nil(t, err)
    86  
    87  	err = fileLock2.Lock()
    88  	require.Regexp(t, ".*ErrConflictingFileLocks.*", err)
    89  }