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"
20 TEST(LockFileManagerTest, Basic) {
21 SmallString<64> TmpDir;
23 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
26 SmallString<64> LockedFile(TmpDir);
27 sys::path::append(LockedFile, "file.lock");
30 // The lock file should not exist, so we should successfully acquire it.
31 LockFileManager Locked1(LockedFile);
32 EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
34 // Attempting to reacquire the lock should fail. Waiting on it would cause
35 // deadlock, so don't try that.
36 LockFileManager Locked2(LockedFile);
37 EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
40 // Now that the lock is out of scope, the file should be gone.
41 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
43 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");
63 EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
67 // The lock file doesn't point to a real file, so we should successfully
69 LockFileManager Locked(LockedFile);
70 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
73 // Now that the lock is out of scope, the file should be gone.
74 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
76 EC = sys::fs::remove(StringRef(TmpDir));
81 TEST(LockFileManagerTest, RelativePath) {
82 SmallString<64> TmpDir;
84 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
88 const char *OrigPath = getcwd(PathBuf, 1024);
89 chdir(TmpDir.c_str());
91 sys::fs::create_directory("inner");
92 SmallString<64> LockedFile("inner");
93 sys::path::append(LockedFile, "file");
95 SmallString<64> FileLock(LockedFile);
99 // The lock file should not exist, so we should successfully acquire it.
100 LockFileManager Locked(LockedFile);
101 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
102 EXPECT_TRUE(sys::fs::exists(FileLock.str()));
105 // Now that the lock is out of scope, the file should be gone.
106 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
107 EXPECT_FALSE(sys::fs::exists(FileLock.str()));
109 EC = sys::fs::remove("inner");
111 EC = sys::fs::remove(StringRef(TmpDir));
118 } // end anonymous namespace