2013-08-07 09:22:04 +08:00
|
|
|
//===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-07 09:22:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/LockFileManager.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2020-07-06 20:55:49 +08:00
|
|
|
#include "llvm/Testing/Support/SupportHelpers.h"
|
2013-08-07 09:22:04 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace llvm;
|
2020-07-06 20:55:49 +08:00
|
|
|
using llvm::unittest::TempDir;
|
2013-08-07 09:22:04 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(LockFileManagerTest, Basic) {
|
2020-07-06 20:55:49 +08:00
|
|
|
TempDir TmpDir("LockFileManagerTestDir", /*Unique*/ true);
|
2013-08-07 09:22:04 +08:00
|
|
|
|
2020-07-06 20:55:49 +08:00
|
|
|
SmallString<64> LockedFile(TmpDir.path());
|
2013-08-07 09:22:04 +08:00
|
|
|
sys::path::append(LockedFile, "file.lock");
|
|
|
|
|
|
|
|
{
|
|
|
|
// The lock file should not exist, so we should successfully acquire it.
|
|
|
|
LockFileManager Locked1(LockedFile);
|
|
|
|
EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
|
|
|
|
|
|
|
|
// Attempting to reacquire the lock should fail. Waiting on it would cause
|
|
|
|
// deadlock, so don't try that.
|
|
|
|
LockFileManager Locked2(LockedFile);
|
|
|
|
EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the lock is out of scope, the file should be gone.
|
2021-07-09 04:30:14 +08:00
|
|
|
EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
|
2013-08-07 09:22:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-21 09:25:37 +08:00
|
|
|
TEST(LockFileManagerTest, LinkLockExists) {
|
2020-07-06 20:55:49 +08:00
|
|
|
TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
|
2014-03-21 09:25:37 +08:00
|
|
|
|
2020-07-06 20:55:49 +08:00
|
|
|
SmallString<64> LockedFile(LockFileManagerTestDir.path());
|
2014-03-21 09:25:37 +08:00
|
|
|
sys::path::append(LockedFile, "file");
|
|
|
|
|
2020-07-06 20:55:49 +08:00
|
|
|
SmallString<64> FileLocK(LockFileManagerTestDir.path());
|
2014-03-21 09:25:37 +08:00
|
|
|
sys::path::append(FileLocK, "file.lock");
|
|
|
|
|
2020-07-06 20:55:49 +08:00
|
|
|
SmallString<64> TmpFileLock(LockFileManagerTestDir.path());
|
2014-03-21 09:25:37 +08:00
|
|
|
sys::path::append(TmpFileLock, "file.lock-000");
|
|
|
|
|
2014-03-25 21:19:03 +08:00
|
|
|
int FD;
|
2021-07-09 04:30:14 +08:00
|
|
|
std::error_code EC = sys::fs::openFileForWrite(TmpFileLock.str(), FD);
|
2014-03-25 21:19:03 +08:00
|
|
|
ASSERT_FALSE(EC);
|
|
|
|
|
|
|
|
int Ret = close(FD);
|
|
|
|
ASSERT_EQ(Ret, 0);
|
|
|
|
|
2014-03-21 09:25:37 +08:00
|
|
|
EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
|
|
|
|
ASSERT_FALSE(EC);
|
2014-03-25 21:19:03 +08:00
|
|
|
|
2021-07-09 04:30:14 +08:00
|
|
|
EC = sys::fs::remove(TmpFileLock.str());
|
2014-03-25 21:19:03 +08:00
|
|
|
ASSERT_FALSE(EC);
|
2014-03-21 09:25:37 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
// The lock file doesn't point to a real file, so we should successfully
|
|
|
|
// acquire it.
|
|
|
|
LockFileManager Locked(LockedFile);
|
|
|
|
EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the lock is out of scope, the file should be gone.
|
2021-07-09 04:30:14 +08:00
|
|
|
EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
|
2014-03-21 09:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-03-21 10:31:56 +08:00
|
|
|
|
|
|
|
TEST(LockFileManagerTest, RelativePath) {
|
2020-07-06 20:55:49 +08:00
|
|
|
TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
|
2014-03-21 10:31:56 +08:00
|
|
|
|
|
|
|
char PathBuf[1024];
|
|
|
|
const char *OrigPath = getcwd(PathBuf, 1024);
|
2020-09-09 06:53:01 +08:00
|
|
|
ASSERT_FALSE(chdir(LockFileManagerTestDir.c_str()));
|
2014-03-21 10:31:56 +08:00
|
|
|
|
2020-07-06 20:55:49 +08:00
|
|
|
TempDir inner("inner");
|
|
|
|
SmallString<64> LockedFile(inner.path());
|
2014-03-21 10:31:56 +08:00
|
|
|
sys::path::append(LockedFile, "file");
|
|
|
|
|
|
|
|
SmallString<64> FileLock(LockedFile);
|
|
|
|
FileLock += ".lock";
|
|
|
|
|
|
|
|
{
|
|
|
|
// The lock file should not exist, so we should successfully acquire it.
|
|
|
|
LockFileManager Locked(LockedFile);
|
|
|
|
EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
|
|
|
|
EXPECT_TRUE(sys::fs::exists(FileLock.str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the lock is out of scope, the file should be gone.
|
|
|
|
EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
|
|
|
|
EXPECT_FALSE(sys::fs::exists(FileLock.str()));
|
|
|
|
|
2014-08-13 19:17:41 +08:00
|
|
|
ASSERT_FALSE(chdir(OrigPath));
|
2014-03-24 07:55:57 +08:00
|
|
|
}
|
2014-03-21 10:31:56 +08:00
|
|
|
|
2013-08-07 09:22:04 +08:00
|
|
|
} // end anonymous namespace
|