1 //===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/LockFileManager.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/Path.h"
13 #include "gtest/gtest.h"
17 using std::error_code;
21 TEST(LockFileManagerTest, Basic) {
22 SmallString<64> TmpDir;
24 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
27 SmallString<64> LockedFile(TmpDir);
28 sys::path::append(LockedFile, "file.lock");
31 // The lock file should not exist, so we should successfully acquire it.
32 LockFileManager Locked1(LockedFile);
33 EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
35 // Attempting to reacquire the lock should fail. Waiting on it would cause
36 // deadlock, so don't try that.
37 LockFileManager Locked2(LockedFile);
38 EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
41 // Now that the lock is out of scope, the file should be gone.
42 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
44 EC = sys::fs::remove(StringRef(TmpDir));
48 TEST(LockFileManagerTest, LinkLockExists) {
49 SmallString<64> TmpDir;
51 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
54 SmallString<64> LockedFile(TmpDir);
55 sys::path::append(LockedFile, "file");
57 SmallString<64> FileLocK(TmpDir);
58 sys::path::append(FileLocK, "file.lock");
60 SmallString<64> TmpFileLock(TmpDir);
61 sys::path::append(TmpFileLock, "file.lock-000");
64 EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD, sys::fs::F_None);
70 EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
73 EC = sys::fs::remove(StringRef(TmpFileLock));
77 // The lock file doesn't point to a real file, so we should successfully
79 LockFileManager Locked(LockedFile);
80 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
83 // Now that the lock is out of scope, the file should be gone.
84 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
86 EC = sys::fs::remove(StringRef(TmpDir));
91 TEST(LockFileManagerTest, RelativePath) {
92 SmallString<64> TmpDir;
94 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
98 const char *OrigPath = getcwd(PathBuf, 1024);
99 chdir(TmpDir.c_str());
101 sys::fs::create_directory("inner");
102 SmallString<64> LockedFile("inner");
103 sys::path::append(LockedFile, "file");
105 SmallString<64> FileLock(LockedFile);
109 // The lock file should not exist, so we should successfully acquire it.
110 LockFileManager Locked(LockedFile);
111 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
112 EXPECT_TRUE(sys::fs::exists(FileLock.str()));
115 // Now that the lock is out of scope, the file should be gone.
116 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
117 EXPECT_FALSE(sys::fs::exists(FileLock.str()));
119 EC = sys::fs::remove("inner");
124 EC = sys::fs::remove(StringRef(TmpDir));
128 } // end anonymous namespace