xref: /llvm-project/llvm/unittests/Support/LockFileManagerTest.cpp (revision 7de8ea3d00f393e0847495d9cd6660204c10e75c)
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 
10 #include "llvm/Support/LockFileManager.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/Path.h"
13 
14 #include "gtest/gtest.h"
15 
16 #include <memory>
17 
18 using namespace llvm;
19 
20 namespace {
21 
22 TEST(LockFileManagerTest, Basic) {
23   SmallString<64> TmpDir;
24   error_code EC;
25   EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
26   ASSERT_FALSE(EC);
27 
28   SmallString<64> LockedFile(TmpDir);
29   sys::path::append(LockedFile, "file.lock");
30 
31   {
32     // The lock file should not exist, so we should successfully acquire it.
33     LockFileManager Locked1(LockedFile);
34     EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
35 
36     // Attempting to reacquire the lock should fail.  Waiting on it would cause
37     // deadlock, so don't try that.
38     LockFileManager Locked2(LockedFile);
39     EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
40   }
41 
42   // Now that the lock is out of scope, the file should be gone.
43   EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
44 
45   sys::fs::remove_all(StringRef(TmpDir));
46 }
47 
48 } // end anonymous namespace
49