LockFileManagerTest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "llvm/Support/LockFileManager.h"
  9. #include "llvm/Support/FileSystem.h"
  10. #include "llvm/Support/Path.h"
  11. #include "gtest/gtest.h"
  12. #include <memory>
  13. using namespace llvm;
  14. namespace {
  15. TEST(LockFileManagerTest, Basic) {
  16. SmallString<64> TmpDir;
  17. std::error_code EC;
  18. EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
  19. ASSERT_FALSE(EC);
  20. SmallString<64> LockedFile(TmpDir);
  21. sys::path::append(LockedFile, "file.lock");
  22. {
  23. // The lock file should not exist, so we should successfully acquire it.
  24. LockFileManager Locked1(LockedFile);
  25. EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
  26. // Attempting to reacquire the lock should fail. Waiting on it would cause
  27. // deadlock, so don't try that.
  28. LockFileManager Locked2(LockedFile);
  29. EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
  30. }
  31. // Now that the lock is out of scope, the file should be gone.
  32. EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
  33. EC = sys::fs::remove(StringRef(TmpDir));
  34. ASSERT_FALSE(EC);
  35. }
  36. TEST(LockFileManagerTest, LinkLockExists) {
  37. SmallString<64> TmpDir;
  38. std::error_code EC;
  39. EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
  40. ASSERT_FALSE(EC);
  41. SmallString<64> LockedFile(TmpDir);
  42. sys::path::append(LockedFile, "file");
  43. SmallString<64> FileLocK(TmpDir);
  44. sys::path::append(FileLocK, "file.lock");
  45. SmallString<64> TmpFileLock(TmpDir);
  46. sys::path::append(TmpFileLock, "file.lock-000");
  47. int FD;
  48. EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD);
  49. ASSERT_FALSE(EC);
  50. int Ret = close(FD);
  51. ASSERT_EQ(Ret, 0);
  52. EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
  53. ASSERT_FALSE(EC);
  54. EC = sys::fs::remove(StringRef(TmpFileLock));
  55. ASSERT_FALSE(EC);
  56. {
  57. // The lock file doesn't point to a real file, so we should successfully
  58. // acquire it.
  59. LockFileManager Locked(LockedFile);
  60. EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
  61. }
  62. // Now that the lock is out of scope, the file should be gone.
  63. EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
  64. EC = sys::fs::remove(StringRef(TmpDir));
  65. ASSERT_FALSE(EC);
  66. }
  67. TEST(LockFileManagerTest, RelativePath) {
  68. SmallString<64> TmpDir;
  69. std::error_code EC;
  70. EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
  71. ASSERT_FALSE(EC);
  72. char PathBuf[1024];
  73. const char *OrigPath = getcwd(PathBuf, 1024);
  74. ASSERT_FALSE(chdir(TmpDir.c_str()));
  75. sys::fs::create_directory("inner");
  76. SmallString<64> LockedFile("inner");
  77. sys::path::append(LockedFile, "file");
  78. SmallString<64> FileLock(LockedFile);
  79. FileLock += ".lock";
  80. {
  81. // The lock file should not exist, so we should successfully acquire it.
  82. LockFileManager Locked(LockedFile);
  83. EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
  84. EXPECT_TRUE(sys::fs::exists(FileLock.str()));
  85. }
  86. // Now that the lock is out of scope, the file should be gone.
  87. EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
  88. EXPECT_FALSE(sys::fs::exists(FileLock.str()));
  89. EC = sys::fs::remove("inner");
  90. ASSERT_FALSE(EC);
  91. ASSERT_FALSE(chdir(OrigPath));
  92. EC = sys::fs::remove(StringRef(TmpDir));
  93. ASSERT_FALSE(EC);
  94. }
  95. } // end anonymous namespace