proxygen
FileLockTest.cpp File Reference
#include <folly/File.h>
#include <mutex>
#include <boost/thread/locks.hpp>
#include <glog/logging.h>
#include <folly/String.h>
#include <folly/Subprocess.h>
#include <folly/experimental/TestUtil.h>
#include <folly/experimental/io/FsUtil.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 DEFINE_bool (s, false,"get shared lock")
 
 DEFINE_bool (x, false,"get exclusive lock")
 
 TEST (File, Locks)
 

Function Documentation

DEFINE_bool ( s  ,
false  ,
"get shared lock"   
)
DEFINE_bool ( x  ,
false  ,
"get exclusive lock"   
)
TEST ( File  ,
Locks   
)

Definition at line 37 of file FileLockTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, f, folly::test::TemporaryFile::fd(), folly::lock(), mode, folly::test::TemporaryFile::path(), folly::detail::SHARED, gmock_test_utils::Subprocess, and folly::detail::distributed_mutex::wait().

37  {
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()})
66  .wait();
67  EXPECT_TRUE(ret.exited());
68  if (ret.exited()) {
69  EXPECT_EQ(expectedSuccess ? 0 : 42, ret.exitStatus());
70  }
71  };
72 
73  // Make sure nothing breaks and things compile.
74  { Lock lock(f); }
75 
76  { SharedLock lock(f); }
77 
78  {
79  Lock lock(f, std::defer_lock);
80  EXPECT_TRUE(lock.try_lock());
81  }
82 
83  {
84  SharedLock lock(f, boost::defer_lock);
85  EXPECT_TRUE(lock.try_lock());
86  }
87 
88  // X blocks X
89  {
90  Lock lock(f);
91  testLock(EXCLUSIVE, false);
92  }
93 
94  // X blocks S
95  {
96  Lock lock(f);
97  testLock(SHARED, false);
98  }
99 
100  // S blocks X
101  {
102  SharedLock lock(f);
103  testLock(EXCLUSIVE, false);
104  }
105 
106  // S does not block S
107  {
108  SharedLock lock(f);
109  testLock(SHARED, true);
110  }
111 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::Optional< PskKeyExchangeMode > mode
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
bool wait(Waiter *waiter, bool shouldSleep, Waiter *&next)
const fs::path & path() const
Definition: TestUtil.cpp:85
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859