blob: b80cdf9b66f6e9d5829a1f1d8111c24464ebafeb [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
Argyrios Kyrtzidis37575692014-03-21 01:25:37 +000047TEST(LockFileManagerTest, LinkLockExists) {
48 SmallString<64> TmpDir;
49 error_code EC;
50 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
51 ASSERT_FALSE(EC);
52
53 SmallString<64> LockedFile(TmpDir);
54 sys::path::append(LockedFile, "file");
55
56 SmallString<64> FileLocK(TmpDir);
57 sys::path::append(FileLocK, "file.lock");
58
59 SmallString<64> TmpFileLock(TmpDir);
60 sys::path::append(TmpFileLock, "file.lock-000");
61
62 EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
NAKAMURA Takumi126a6c02014-03-23 23:55:57 +000063#if defined(_WIN32)
64 // Win32 cannot create link with nonexistent file, since create_link is
65 // implemented as hard link.
66 ASSERT_EQ(EC, errc::no_such_file_or_directory);
67#else
Argyrios Kyrtzidis37575692014-03-21 01:25:37 +000068 ASSERT_FALSE(EC);
NAKAMURA Takumi126a6c02014-03-23 23:55:57 +000069#endif
Argyrios Kyrtzidis37575692014-03-21 01:25:37 +000070
71 {
72 // The lock file doesn't point to a real file, so we should successfully
73 // acquire it.
74 LockFileManager Locked(LockedFile);
75 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
76 }
77
78 // Now that the lock is out of scope, the file should be gone.
79 EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
80
81 EC = sys::fs::remove(StringRef(TmpDir));
82 ASSERT_FALSE(EC);
83}
84
Argyrios Kyrtzidis531a5be2014-03-21 02:31:56 +000085
86TEST(LockFileManagerTest, RelativePath) {
87 SmallString<64> TmpDir;
88 error_code EC;
89 EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
90 ASSERT_FALSE(EC);
91
92 char PathBuf[1024];
93 const char *OrigPath = getcwd(PathBuf, 1024);
94 chdir(TmpDir.c_str());
95
96 sys::fs::create_directory("inner");
97 SmallString<64> LockedFile("inner");
98 sys::path::append(LockedFile, "file");
99
100 SmallString<64> FileLock(LockedFile);
101 FileLock += ".lock";
102
103 {
104 // The lock file should not exist, so we should successfully acquire it.
105 LockFileManager Locked(LockedFile);
106 EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
107 EXPECT_TRUE(sys::fs::exists(FileLock.str()));
108 }
109
110 // Now that the lock is out of scope, the file should be gone.
111 EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
112 EXPECT_FALSE(sys::fs::exists(FileLock.str()));
113
114 EC = sys::fs::remove("inner");
115 ASSERT_FALSE(EC);
116 EC = sys::fs::remove(StringRef(TmpDir));
NAKAMURA Takumi126a6c02014-03-23 23:55:57 +0000117#if defined(_WIN32)
118 // Win32 cannot remove working directory.
119 ASSERT_EQ(EC, errc::permission_denied);
120#else
Argyrios Kyrtzidis531a5be2014-03-21 02:31:56 +0000121 ASSERT_FALSE(EC);
NAKAMURA Takumi126a6c02014-03-23 23:55:57 +0000122#endif
Argyrios Kyrtzidis531a5be2014-03-21 02:31:56 +0000123
124 chdir(OrigPath);
NAKAMURA Takumi126a6c02014-03-23 23:55:57 +0000125
126#if defined(_WIN32)
127 EC = sys::fs::remove(StringRef(TmpDir));
128 ASSERT_FALSE(EC);
NAKAMURA Takumid1c22be2014-03-22 00:27:17 +0000129#endif
NAKAMURA Takumi126a6c02014-03-23 23:55:57 +0000130}
Argyrios Kyrtzidis531a5be2014-03-21 02:31:56 +0000131
Reid Kleckner7de8ea32013-08-07 01:22:04 +0000132} // end anonymous namespace