Futex::futexWait returns FutexResult
[folly.git] / folly / test / FileLockTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/File.h>
18
19 #include <mutex>
20
21 #include <boost/thread/locks.hpp>
22 #include <glog/logging.h>
23
24 #include <folly/String.h>
25 #include <folly/Subprocess.h>
26 #include <folly/experimental/TestUtil.h>
27 #include <folly/experimental/io/FsUtil.h>
28 #include <folly/portability/GFlags.h>
29 #include <folly/portability/GTest.h>
30
31 using namespace folly;
32 using namespace folly::test;
33
34 DEFINE_bool(s, false, "get shared lock");
35 DEFINE_bool(x, false, "get exclusive lock");
36
37 TEST(File, Locks) {
38   typedef std::unique_lock<File> Lock;
39   typedef boost::shared_lock<File> SharedLock;
40
41   // Find out where we are.
42   static constexpr size_t pathLength = 2048;
43   char buf[pathLength + 1];
44   int r = readlink("/proc/self/exe", buf, pathLength);
45   CHECK(r != -1);
46   buf[r] = '\0';
47
48   fs::path me(buf);
49   auto helper_basename = "file_test_lock_helper";
50   fs::path helper;
51   if (fs::exists(me.parent_path() / helper_basename)) {
52     helper = me.parent_path() / helper_basename;
53   } else {
54     throw std::runtime_error(
55         folly::to<std::string>("cannot find helper ", helper_basename));
56   }
57
58   TemporaryFile tempFile;
59   File f(tempFile.fd());
60
61   enum LockMode { EXCLUSIVE, SHARED };
62   auto testLock = [&](LockMode mode, bool expectedSuccess) {
63     auto ret = Subprocess({helper.string(),
64                            mode == SHARED ? "-s" : "-x",
65                            tempFile.path().string()}).wait();
66     EXPECT_TRUE(ret.exited());
67     if (ret.exited()) {
68       EXPECT_EQ(expectedSuccess ? 0 : 42, ret.exitStatus());
69     }
70   };
71
72   // Make sure nothing breaks and things compile.
73   { Lock lock(f); }
74
75   { SharedLock lock(f); }
76
77   {
78     Lock lock(f, std::defer_lock);
79     EXPECT_TRUE(lock.try_lock());
80   }
81
82   {
83     SharedLock lock(f, boost::defer_lock);
84     EXPECT_TRUE(lock.try_lock());
85   }
86
87   // X blocks X
88   {
89     Lock lock(f);
90     testLock(EXCLUSIVE, false);
91   }
92
93   // X blocks S
94   {
95     Lock lock(f);
96     testLock(SHARED, false);
97   }
98
99   // S blocks X
100   {
101     SharedLock lock(f);
102     testLock(EXCLUSIVE, false);
103   }
104
105   // S does not block S
106   {
107     SharedLock lock(f);
108     testLock(SHARED, true);
109   }
110 }