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 "gtest/gtest.h"
19 TEST(LockFileManagerTest
, Basic
) {
20 SmallString
<64> TmpDir
;
22 EC
= sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir
);
25 SmallString
<64> LockedFile(TmpDir
);
26 sys::path::append(LockedFile
, "file.lock");
29 // The lock file should not exist, so we should successfully acquire it.
30 LockFileManager
Locked1(LockedFile
);
31 EXPECT_EQ(LockFileManager::LFS_Owned
, Locked1
.getState());
33 // Attempting to reacquire the lock should fail. Waiting on it would cause
34 // deadlock, so don't try that.
35 LockFileManager
Locked2(LockedFile
);
36 EXPECT_NE(LockFileManager::LFS_Owned
, Locked2
.getState());
39 // Now that the lock is out of scope, the file should be gone.
40 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile
)));
42 EC
= sys::fs::remove(StringRef(TmpDir
));
46 TEST(LockFileManagerTest
, LinkLockExists
) {
47 SmallString
<64> TmpDir
;
49 EC
= sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir
);
52 SmallString
<64> LockedFile(TmpDir
);
53 sys::path::append(LockedFile
, "file");
55 SmallString
<64> FileLocK(TmpDir
);
56 sys::path::append(FileLocK
, "file.lock");
58 SmallString
<64> TmpFileLock(TmpDir
);
59 sys::path::append(TmpFileLock
, "file.lock-000");
62 EC
= sys::fs::openFileForWrite(StringRef(TmpFileLock
), FD
);
68 EC
= sys::fs::create_link(TmpFileLock
.str(), FileLocK
.str());
71 EC
= sys::fs::remove(StringRef(TmpFileLock
));
75 // The lock file doesn't point to a real file, so we should successfully
77 LockFileManager
Locked(LockedFile
);
78 EXPECT_EQ(LockFileManager::LFS_Owned
, Locked
.getState());
81 // Now that the lock is out of scope, the file should be gone.
82 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile
)));
84 EC
= sys::fs::remove(StringRef(TmpDir
));
89 TEST(LockFileManagerTest
, RelativePath
) {
90 SmallString
<64> TmpDir
;
92 EC
= sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir
);
96 const char *OrigPath
= getcwd(PathBuf
, 1024);
97 ASSERT_FALSE(chdir(TmpDir
.c_str()));
99 sys::fs::create_directory("inner");
100 SmallString
<64> LockedFile("inner");
101 sys::path::append(LockedFile
, "file");
103 SmallString
<64> FileLock(LockedFile
);
107 // The lock file should not exist, so we should successfully acquire it.
108 LockFileManager
Locked(LockedFile
);
109 EXPECT_EQ(LockFileManager::LFS_Owned
, Locked
.getState());
110 EXPECT_TRUE(sys::fs::exists(FileLock
.str()));
113 // Now that the lock is out of scope, the file should be gone.
114 EXPECT_FALSE(sys::fs::exists(LockedFile
.str()));
115 EXPECT_FALSE(sys::fs::exists(FileLock
.str()));
117 EC
= sys::fs::remove("inner");
120 ASSERT_FALSE(chdir(OrigPath
));
122 EC
= sys::fs::remove(StringRef(TmpDir
));
126 } // end anonymous namespace