blob: 552053d46e843512fe943b2ad6d2284c321ccfa5 [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001//===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===//
2//
Chih-Hung Hsieh08600532019-12-19 15:55:38 -08003// 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 Palantff3f07a2019-07-11 16:15:26 -07006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/LockFileManager.h"
10#include "llvm/Support/FileSystem.h"
11#include "llvm/Support/Path.h"
Chris Wailese3116c42021-07-13 14:40:48 -070012#include "llvm/Testing/Support/SupportHelpers.h"
Inna Palantff3f07a2019-07-11 16:15:26 -070013#include "gtest/gtest.h"
14#include <memory>
15
16using namespace llvm;
Chris Wailese3116c42021-07-13 14:40:48 -070017using llvm::unittest::TempDir;
Inna Palantff3f07a2019-07-11 16:15:26 -070018
19namespace {
20
21TEST(LockFileManagerTest, Basic) {
Chris Wailese3116c42021-07-13 14:40:48 -070022 TempDir TmpDir("LockFileManagerTestDir", /*Unique*/ true);
Inna Palantff3f07a2019-07-11 16:15:26 -070023
Chris Wailese3116c42021-07-13 14:40:48 -070024 SmallString<64> LockedFile(TmpDir.path());
Inna Palantff3f07a2019-07-11 16:15:26 -070025 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 Wailesbcf972c2021-10-21 11:03:28 -070039 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
Inna Palantff3f07a2019-07-11 16:15:26 -070040}
41
42TEST(LockFileManagerTest, LinkLockExists) {
Chris Wailese3116c42021-07-13 14:40:48 -070043 TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
Inna Palantff3f07a2019-07-11 16:15:26 -070044
Chris Wailese3116c42021-07-13 14:40:48 -070045 SmallString<64> LockedFile(LockFileManagerTestDir.path());
Inna Palantff3f07a2019-07-11 16:15:26 -070046 sys::path::append(LockedFile, "file");
47
Chris Wailese3116c42021-07-13 14:40:48 -070048 SmallString<64> FileLocK(LockFileManagerTestDir.path());
Inna Palantff3f07a2019-07-11 16:15:26 -070049 sys::path::append(FileLocK, "file.lock");
50
Chris Wailese3116c42021-07-13 14:40:48 -070051 SmallString<64> TmpFileLock(LockFileManagerTestDir.path());
Inna Palantff3f07a2019-07-11 16:15:26 -070052 sys::path::append(TmpFileLock, "file.lock-000");
53
54 int FD;
Chris Wailesbcf972c2021-10-21 11:03:28 -070055 std::error_code EC = sys::fs::openFileForWrite(TmpFileLock.str(), FD);
Inna Palantff3f07a2019-07-11 16:15:26 -070056 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 Wailesbcf972c2021-10-21 11:03:28 -070064 EC = sys::fs::remove(TmpFileLock.str());
Inna Palantff3f07a2019-07-11 16:15:26 -070065 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 Wailesbcf972c2021-10-21 11:03:28 -070075 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
Inna Palantff3f07a2019-07-11 16:15:26 -070076}
77
78
79TEST(LockFileManagerTest, RelativePath) {
Chris Wailese3116c42021-07-13 14:40:48 -070080 TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
Inna Palantff3f07a2019-07-11 16:15:26 -070081
82 char PathBuf[1024];
83 const char *OrigPath = getcwd(PathBuf, 1024);
Chris Wailese3116c42021-07-13 14:40:48 -070084 ASSERT_FALSE(chdir(LockFileManagerTestDir.c_str()));
Inna Palantff3f07a2019-07-11 16:15:26 -070085
Chris Wailese3116c42021-07-13 14:40:48 -070086 TempDir inner("inner");
87 SmallString<64> LockedFile(inner.path());
Inna Palantff3f07a2019-07-11 16:15:26 -070088 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 Palantff3f07a2019-07-11 16:15:26 -0700104 ASSERT_FALSE(chdir(OrigPath));
Inna Palantff3f07a2019-07-11 16:15:26 -0700105}
106
107} // end anonymous namespace