[InstCombine] Signed saturation patterns
[llvm-complete.git] / unittests / Support / LockFileManagerTest.cpp
blob5f1fe0fd3aeacb2fbf6225b4ac29d57b04dacd7b
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/LockFileManager.h"
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/Path.h"
12 #include "gtest/gtest.h"
13 #include <memory>
15 using namespace llvm;
17 namespace {
19 TEST(LockFileManagerTest, Basic) {
20 SmallString<64> TmpDir;
21 std::error_code EC;
22 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
23 ASSERT_FALSE(EC);
25 SmallString<64> LockedFile(TmpDir);
26 sys::path::append(LockedFile, "file.lock");
29 // The lock file should not exist, so we should successfully acquire it.
30 LockFileManager Locked1(LockedFile);
31 EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
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_NE(LockFileManager::LFS_Owned, Locked2.getState());
39 // Now that the lock is out of scope, the file should be gone.
40 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
42 EC = sys::fs::remove(StringRef(TmpDir));
43 ASSERT_FALSE(EC);
46 TEST(LockFileManagerTest, LinkLockExists) {
47 SmallString<64> TmpDir;
48 std::error_code EC;
49 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
50 ASSERT_FALSE(EC);
52 SmallString<64> LockedFile(TmpDir);
53 sys::path::append(LockedFile, "file");
55 SmallString<64> FileLocK(TmpDir);
56 sys::path::append(FileLocK, "file.lock");
58 SmallString<64> TmpFileLock(TmpDir);
59 sys::path::append(TmpFileLock, "file.lock-000");
61 int FD;
62 EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD);
63 ASSERT_FALSE(EC);
65 int Ret = close(FD);
66 ASSERT_EQ(Ret, 0);
68 EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
69 ASSERT_FALSE(EC);
71 EC = sys::fs::remove(StringRef(TmpFileLock));
72 ASSERT_FALSE(EC);
75 // The lock file doesn't point to a real file, so we should successfully
76 // acquire it.
77 LockFileManager Locked(LockedFile);
78 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
81 // Now that the lock is out of scope, the file should be gone.
82 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
84 EC = sys::fs::remove(StringRef(TmpDir));
85 ASSERT_FALSE(EC);
89 TEST(LockFileManagerTest, RelativePath) {
90 SmallString<64> TmpDir;
91 std::error_code EC;
92 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
93 ASSERT_FALSE(EC);
95 char PathBuf[1024];
96 const char *OrigPath = getcwd(PathBuf, 1024);
97 ASSERT_FALSE(chdir(TmpDir.c_str()));
99 sys::fs::create_directory("inner");
100 SmallString<64> LockedFile("inner");
101 sys::path::append(LockedFile, "file");
103 SmallString<64> FileLock(LockedFile);
104 FileLock += ".lock";
107 // The lock file should not exist, so we should successfully acquire it.
108 LockFileManager Locked(LockedFile);
109 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
110 EXPECT_TRUE(sys::fs::exists(FileLock.str()));
113 // Now that the lock is out of scope, the file should be gone.
114 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
115 EXPECT_FALSE(sys::fs::exists(FileLock.str()));
117 EC = sys::fs::remove("inner");
118 ASSERT_FALSE(EC);
120 ASSERT_FALSE(chdir(OrigPath));
122 EC = sys::fs::remove(StringRef(TmpDir));
123 ASSERT_FALSE(EC);
126 } // end anonymous namespace