blob: 79f4519614e87e89a20841d38b22c8517d215398 [file] [log] [blame]
Reid Kleckner7de8ea32013-08-07 01:22:04 +00001//===- 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"
Reid Kleckner7de8ea32013-08-07 01:22:04 +000013#include "gtest/gtest.h"
Reid Kleckner7de8ea32013-08-07 01:22:04 +000014#include <memory>
15
16using namespace llvm;
17
18namespace {
19
20TEST(LockFileManagerTest, Basic) {
21 SmallString<64> TmpDir;
22 error_code EC;
23 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
24 ASSERT_FALSE(EC);
25
26 SmallString<64> LockedFile(TmpDir);
27 sys::path::append(LockedFile, "file.lock");
28
29 {
30 // The lock file should not exist, so we should successfully acquire it.
31 LockFileManager Locked1(LockedFile);
32 EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
33
34 // Attempting to reacquire the lock should fail. Waiting on it would cause
35 // deadlock, so don't try that.
36 LockFileManager Locked2(LockedFile);
37 EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
38 }
39
40 // Now that the lock is out of scope, the file should be gone.
41 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
42
Rafael Espindola78dcc032014-01-10 20:36:42 +000043 EC = sys::fs::remove(StringRef(TmpDir));
44 ASSERT_FALSE(EC);
Reid Kleckner7de8ea32013-08-07 01:22:04 +000045}
46
NAKAMURA Takumid1c22be2014-03-22 00:27:17 +000047#if !defined(_WIN32)
Argyrios Kyrtzidis37575692014-03-21 01:25:37 +000048TEST(LockFileManagerTest, LinkLockExists) {
49 SmallString<64> TmpDir;
50 error_code EC;
51 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
52 ASSERT_FALSE(EC);
53
54 SmallString<64> LockedFile(TmpDir);
55 sys::path::append(LockedFile, "file");
56
57 SmallString<64> FileLocK(TmpDir);
58 sys::path::append(FileLocK, "file.lock");
59
60 SmallString<64> TmpFileLock(TmpDir);
61 sys::path::append(TmpFileLock, "file.lock-000");
62
63 EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
64 ASSERT_FALSE(EC);
65
66 {
67 // The lock file doesn't point to a real file, so we should successfully
68 // acquire it.
69 LockFileManager Locked(LockedFile);
70 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
71 }
72
73 // Now that the lock is out of scope, the file should be gone.
74 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
75
76 EC = sys::fs::remove(StringRef(TmpDir));
77 ASSERT_FALSE(EC);
78}
79
Argyrios Kyrtzidis531a5be2014-03-21 02:31:56 +000080
81TEST(LockFileManagerTest, RelativePath) {
82 SmallString<64> TmpDir;
83 error_code EC;
84 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
85 ASSERT_FALSE(EC);
86
87 char PathBuf[1024];
88 const char *OrigPath = getcwd(PathBuf, 1024);
89 chdir(TmpDir.c_str());
90
91 sys::fs::create_directory("inner");
92 SmallString<64> LockedFile("inner");
93 sys::path::append(LockedFile, "file");
94
95 SmallString<64> FileLock(LockedFile);
96 FileLock += ".lock";
97
98 {
99 // The lock file should not exist, so we should successfully acquire it.
100 LockFileManager Locked(LockedFile);
101 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
102 EXPECT_TRUE(sys::fs::exists(FileLock.str()));
103 }
104
105 // Now that the lock is out of scope, the file should be gone.
106 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
107 EXPECT_FALSE(sys::fs::exists(FileLock.str()));
108
109 EC = sys::fs::remove("inner");
110 ASSERT_FALSE(EC);
111 EC = sys::fs::remove(StringRef(TmpDir));
112 ASSERT_FALSE(EC);
113
114 chdir(OrigPath);
115}
NAKAMURA Takumid1c22be2014-03-22 00:27:17 +0000116#endif
Argyrios Kyrtzidis531a5be2014-03-21 02:31:56 +0000117
Reid Kleckner7de8ea32013-08-07 01:22:04 +0000118} // end anonymous namespace