proxygen
AtForkTest.cpp File Reference
#include <folly/detail/AtFork.h>
#include <folly/portability/GTest.h>
#include <glog/logging.h>
#include <atomic>
#include <mutex>
#include <thread>

Go to the source code of this file.

Functions

 TEST (ThreadLocal, AtFork)
 
 TEST (ThreadLocal, AtForkOrdering)
 

Function Documentation

TEST ( ThreadLocal  ,
AtFork   
)

Definition at line 25 of file AtForkTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, testing::gmock_matchers_test::foo, folly::detail::AtFork::registerHandler(), folly::detail::AtFork::unregisterHandler(), and folly::detail::distributed_mutex::wait().

25  {
26  int foo;
27  bool forked = false;
29  &foo,
30  [&] {
31  forked = true;
32  return true;
33  },
34  [] {},
35  [] {});
36  auto pid = fork();
37  if (pid) {
38  int status;
39  auto pid2 = wait(&status);
40  EXPECT_EQ(status, 0);
41  EXPECT_EQ(pid, pid2);
42  } else {
43  exit(0);
44  }
45  EXPECT_TRUE(forked);
46  forked = false;
48  pid = fork();
49  if (pid) {
50  int status;
51  auto pid2 = wait(&status);
52  EXPECT_EQ(status, 0);
53  EXPECT_EQ(pid, pid2);
54  } else {
55  exit(0);
56  }
57  EXPECT_FALSE(forked);
58 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
bool wait(Waiter *waiter, bool shouldSleep, Waiter *&next)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static void unregisterHandler(void *object)
Definition: AtFork.cpp:118
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
static void registerHandler(void *object, folly::Function< bool()> prepare, folly::Function< void()> parent, folly::Function< void()> child)
Definition: AtFork.cpp:108
TEST ( ThreadLocal  ,
AtForkOrdering   
)

Definition at line 60 of file AtForkTest.cpp.

References a, b, EXPECT_EQ, testing::gmock_matchers_test::foo, foo2(), g(), mutex, folly::detail::AtFork::registerHandler(), and folly::detail::distributed_mutex::wait().

60  {
61  std::atomic<bool> done{false};
62  std::atomic<bool> started{false};
63  std::mutex a;
64  std::mutex b;
65  int foo;
66  int foo2;
68  &foo,
69  [&] { return a.try_lock(); },
70  [&] { a.unlock(); },
71  [&] { a.unlock(); });
73  &foo2,
74  [&] { return b.try_lock(); },
75  [&] { b.unlock(); },
76  [&] { b.unlock(); });
77 
78  auto thr = std::thread([&]() {
79  std::lock_guard<std::mutex> g(a);
80  started = true;
81  usleep(100);
82  std::lock_guard<std::mutex> g2(b);
83  });
84  while (!started) {
85  }
86  auto pid = fork();
87  if (pid) {
88  int status;
89  auto pid2 = wait(&status);
90  EXPECT_EQ(status, 0);
91  EXPECT_EQ(pid, pid2);
92  } else {
93  exit(0);
94  }
95  done = true;
96  thr.join();
97 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
FOLLY_NOINLINE void foo2()
bool wait(Waiter *waiter, bool shouldSleep, Waiter *&next)
char a
std::mutex mutex
g_t g(f_t)
static void registerHandler(void *object, folly::Function< bool()> prepare, folly::Function< void()> parent, folly::Function< void()> child)
Definition: AtFork.cpp:108