LockFileManagerTest.cpp 3.5 KB

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