Debian Codegraph @

llvm/unittests/Support/LockFileManagerTest.cpp

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