1 //===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/LockFileManager.h" 11 #include "llvm/Support/FileSystem.h" 12 #include "llvm/Support/Path.h" 13 #include "gtest/gtest.h" 14 #include <memory> 15 16 using namespace llvm; 17 18 namespace { 19 20 TEST(LockFileManagerTest, Basic) { 21 SmallString<64> TmpDir; 22 error_code EC; 23 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir); 24 ASSERT_FALSE(EC); 25 26 SmallString<64> LockedFile(TmpDir); 27 sys::path::append(LockedFile, "file.lock"); 28 29 { 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()); 33 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()); 38 } 39 40 // Now that the lock is out of scope, the file should be gone. 41 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile))); 42 43 EC = sys::fs::remove(StringRef(TmpDir)); 44 ASSERT_FALSE(EC); 45 } 46 47 TEST(LockFileManagerTest, LinkLockExists) { 48 SmallString<64> TmpDir; 49 error_code EC; 50 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir); 51 ASSERT_FALSE(EC); 52 53 SmallString<64> LockedFile(TmpDir); 54 sys::path::append(LockedFile, "file"); 55 56 SmallString<64> FileLocK(TmpDir); 57 sys::path::append(FileLocK, "file.lock"); 58 59 SmallString<64> TmpFileLock(TmpDir); 60 sys::path::append(TmpFileLock, "file.lock-000"); 61 62 EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str()); 63 ASSERT_FALSE(EC); 64 65 { 66 // The lock file doesn't point to a real file, so we should successfully 67 // acquire it. 68 LockFileManager Locked(LockedFile); 69 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState()); 70 } 71 72 // Now that the lock is out of scope, the file should be gone. 73 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile))); 74 75 EC = sys::fs::remove(StringRef(TmpDir)); 76 ASSERT_FALSE(EC); 77 } 78 79 80 TEST(LockFileManagerTest, RelativePath) { 81 SmallString<64> TmpDir; 82 error_code EC; 83 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir); 84 ASSERT_FALSE(EC); 85 86 char PathBuf[1024]; 87 const char *OrigPath = getcwd(PathBuf, 1024); 88 chdir(TmpDir.c_str()); 89 90 sys::fs::create_directory("inner"); 91 SmallString<64> LockedFile("inner"); 92 sys::path::append(LockedFile, "file"); 93 94 SmallString<64> FileLock(LockedFile); 95 FileLock += ".lock"; 96 97 { 98 // The lock file should not exist, so we should successfully acquire it. 99 LockFileManager Locked(LockedFile); 100 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState()); 101 EXPECT_TRUE(sys::fs::exists(FileLock.str())); 102 } 103 104 // Now that the lock is out of scope, the file should be gone. 105 EXPECT_FALSE(sys::fs::exists(LockedFile.str())); 106 EXPECT_FALSE(sys::fs::exists(FileLock.str())); 107 108 EC = sys::fs::remove("inner"); 109 ASSERT_FALSE(EC); 110 EC = sys::fs::remove(StringRef(TmpDir)); 111 ASSERT_FALSE(EC); 112 113 chdir(OrigPath); 114 } 115 116 } // end anonymous namespace 117