Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | //===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===// |
| 2 | // |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 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 |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Support/LockFileManager.h" |
| 10 | #include "llvm/Support/FileSystem.h" |
| 11 | #include "llvm/Support/Path.h" |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 12 | #include "llvm/Testing/Support/SupportHelpers.h" |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | #include <memory> |
| 15 | |
| 16 | using namespace llvm; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 17 | using llvm::unittest::TempDir; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST(LockFileManagerTest, Basic) { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 22 | TempDir TmpDir("LockFileManagerTestDir", /*Unique*/ true); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 23 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 24 | SmallString<64> LockedFile(TmpDir.path()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 25 | sys::path::append(LockedFile, "file.lock"); |
| 26 | |
| 27 | { |
| 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()); |
| 31 | |
| 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()); |
| 36 | } |
| 37 | |
| 38 | // Now that the lock is out of scope, the file should be gone. |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 39 | EXPECT_FALSE(sys::fs::exists(LockedFile.str())); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(LockFileManagerTest, LinkLockExists) { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 43 | TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 44 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 45 | SmallString<64> LockedFile(LockFileManagerTestDir.path()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 46 | sys::path::append(LockedFile, "file"); |
| 47 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 48 | SmallString<64> FileLocK(LockFileManagerTestDir.path()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 49 | sys::path::append(FileLocK, "file.lock"); |
| 50 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 51 | SmallString<64> TmpFileLock(LockFileManagerTestDir.path()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 52 | sys::path::append(TmpFileLock, "file.lock-000"); |
| 53 | |
| 54 | int FD; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 55 | std::error_code EC = sys::fs::openFileForWrite(TmpFileLock.str(), FD); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 56 | ASSERT_FALSE(EC); |
| 57 | |
| 58 | int Ret = close(FD); |
| 59 | ASSERT_EQ(Ret, 0); |
| 60 | |
| 61 | EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str()); |
| 62 | ASSERT_FALSE(EC); |
| 63 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 64 | EC = sys::fs::remove(TmpFileLock.str()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 65 | ASSERT_FALSE(EC); |
| 66 | |
| 67 | { |
| 68 | // The lock file doesn't point to a real file, so we should successfully |
| 69 | // acquire it. |
| 70 | LockFileManager Locked(LockedFile); |
| 71 | EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState()); |
| 72 | } |
| 73 | |
| 74 | // Now that the lock is out of scope, the file should be gone. |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 75 | EXPECT_FALSE(sys::fs::exists(LockedFile.str())); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | TEST(LockFileManagerTest, RelativePath) { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 80 | TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 81 | |
| 82 | char PathBuf[1024]; |
| 83 | const char *OrigPath = getcwd(PathBuf, 1024); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 84 | ASSERT_FALSE(chdir(LockFileManagerTestDir.c_str())); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 85 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 86 | TempDir inner("inner"); |
| 87 | SmallString<64> LockedFile(inner.path()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 88 | sys::path::append(LockedFile, "file"); |
| 89 | |
| 90 | SmallString<64> FileLock(LockedFile); |
| 91 | FileLock += ".lock"; |
| 92 | |
| 93 | { |
| 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())); |
| 98 | } |
| 99 | |
| 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())); |
| 103 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 104 | ASSERT_FALSE(chdir(OrigPath)); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // end anonymous namespace |