1 //===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/LockFileManager.h"
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/Path.h"
12 #include "llvm/Testing/Support/SupportHelpers.h"
13 #include "gtest/gtest.h"
17 using llvm::unittest::TempDir
;
21 TEST(LockFileManagerTest
, Basic
) {
22 TempDir
TmpDir("LockFileManagerTestDir", /*Unique*/ true);
24 SmallString
<64> LockedFile(TmpDir
.path());
25 sys::path::append(LockedFile
, "file.lock");
28 // The lock file should not exist, so we should successfully acquire it.
29 LockFileManager
Locked1(LockedFile
);
30 EXPECT_EQ(LockFileManager::LFS_Owned
, Locked1
.getState());
32 // Attempting to reacquire the lock should fail. Waiting on it would cause
33 // deadlock, so don't try that.
34 LockFileManager
Locked2(LockedFile
);
35 EXPECT_NE(LockFileManager::LFS_Owned
, Locked2
.getState());
38 // Now that the lock is out of scope, the file should be gone.
39 EXPECT_FALSE(sys::fs::exists(LockedFile
.str()));
42 TEST(LockFileManagerTest
, LinkLockExists
) {
43 TempDir
LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
45 SmallString
<64> LockedFile(LockFileManagerTestDir
.path());
46 sys::path::append(LockedFile
, "file");
48 SmallString
<64> FileLocK(LockFileManagerTestDir
.path());
49 sys::path::append(FileLocK
, "file.lock");
51 SmallString
<64> TmpFileLock(LockFileManagerTestDir
.path());
52 sys::path::append(TmpFileLock
, "file.lock-000");
55 std::error_code EC
= sys::fs::openFileForWrite(TmpFileLock
.str(), FD
);
61 EC
= sys::fs::create_link(TmpFileLock
.str(), FileLocK
.str());
64 EC
= sys::fs::remove(TmpFileLock
.str());
68 // The lock file doesn't point to a real file, so we should successfully
70 LockFileManager
Locked(LockedFile
);
71 EXPECT_EQ(LockFileManager::LFS_Owned
, Locked
.getState());
74 // Now that the lock is out of scope, the file should be gone.
75 EXPECT_FALSE(sys::fs::exists(LockedFile
.str()));
79 TEST(LockFileManagerTest
, RelativePath
) {
80 TempDir
LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
83 const char *OrigPath
= getcwd(PathBuf
, 1024);
84 ASSERT_FALSE(chdir(LockFileManagerTestDir
.c_str()));
86 TempDir
inner("inner");
87 SmallString
<64> LockedFile(inner
.path());
88 sys::path::append(LockedFile
, "file");
90 SmallString
<64> FileLock(LockedFile
);
94 // The lock file should not exist, so we should successfully acquire it.
95 LockFileManager
Locked(LockedFile
);
96 EXPECT_EQ(LockFileManager::LFS_Owned
, Locked
.getState());
97 EXPECT_TRUE(sys::fs::exists(FileLock
.str()));
100 // Now that the lock is out of scope, the file should be gone.
101 EXPECT_FALSE(sys::fs::exists(LockedFile
.str()));
102 EXPECT_FALSE(sys::fs::exists(FileLock
.str()));
104 ASSERT_FALSE(chdir(OrigPath
));
107 } // end anonymous namespace