proxygen
ReadMostlySharedPtrTest.cpp File Reference
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <folly/Memory.h>
#include <folly/experimental/ReadMostlySharedPtr.h>
#include <folly/portability/GTest.h>
#include <folly/synchronization/Baton.h>

Go to the source code of this file.

Classes

class  ReadMostlySharedPtrTest
 
class  TestObject
 
class  Coordinator
 
class  TestRefCount
 

Functions

 TEST_F (ReadMostlySharedPtrTest, BasicStores)
 
 TEST_F (ReadMostlySharedPtrTest, BasicLoads)
 
 TEST_F (ReadMostlySharedPtrTest, LoadsFromThreads)
 
 TEST_F (ReadMostlySharedPtrTest, Ctor)
 
 TEST_F (ReadMostlySharedPtrTest, ClearingCache)
 
 TEST_F (ReadMostlySharedPtrTest, ReadMostlyMainPtrDeleter)
 
 TEST_F (ReadMostlySharedPtrTest, nullptr)
 
 TEST_F (ReadMostlySharedPtrTest, getStdShared)
 

Variables

const unsigned int TEST_TIMEOUT = 10
 
size_t useGlobalCalls = 0
 

Function Documentation

TEST_F ( ReadMostlySharedPtrTest  ,
BasicStores   
)

Definition at line 80 of file ReadMostlySharedPtrTest.cpp.

References EXPECT_EQ, ptr, and folly::ReadMostlyMainPtr< T, RefCount >::reset().

80  {
82 
83  // Store 1.
84  std::atomic<int> cnt1{0};
85  ptr.reset(std::make_unique<TestObject>(1, cnt1));
86  EXPECT_EQ(1, cnt1.load());
87 
88  // Store 2, check that 1 is destroyed.
89  std::atomic<int> cnt2{0};
90  ptr.reset(std::make_unique<TestObject>(2, cnt2));
91  EXPECT_EQ(1, cnt2.load());
92  EXPECT_EQ(0, cnt1.load());
93 
94  // Store nullptr, check that 2 is destroyed.
95  ptr.reset(nullptr);
96  EXPECT_EQ(0, cnt2.load());
97 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( ReadMostlySharedPtrTest  ,
BasicLoads   
)

Definition at line 99 of file ReadMostlySharedPtrTest.cpp.

References EXPECT_EQ, folly::ReadMostlyMainPtr< T, RefCount >::get(), ptr, folly::ReadMostlyMainPtr< T, RefCount >::reset(), folly::ReadMostlySharedPtr< T, RefCount >::reset(), and x.

99  {
100  std::atomic<int> cnt2{0};
102 
103  {
105 
106  // Check that ptr is initially nullptr.
107  EXPECT_EQ(ptr.get(), nullptr);
108 
109  std::atomic<int> cnt1{0};
110  ptr.reset(std::make_unique<TestObject>(1, cnt1));
111  EXPECT_EQ(1, cnt1.load());
112 
113  x = ptr;
114  EXPECT_EQ(1, x->value);
115 
116  ptr.reset(std::make_unique<TestObject>(2, cnt2));
117  EXPECT_EQ(1, cnt2.load());
118  EXPECT_EQ(1, cnt1.load());
119 
120  x = ptr;
121  EXPECT_EQ(2, x->value);
122  EXPECT_EQ(0, cnt1.load());
123 
124  ptr.reset(nullptr);
125  EXPECT_EQ(1, cnt2.load());
126  }
127 
128  EXPECT_EQ(1, cnt2.load());
129 
130  x.reset();
131  EXPECT_EQ(0, cnt2.load());
132 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
TEST_F ( ReadMostlySharedPtrTest  ,
LoadsFromThreads   
)

Definition at line 134 of file ReadMostlySharedPtrTest.cpp.

References Coordinator::completed(), EXPECT_EQ, folly::ReadMostlyMainPtr< T, RefCount >::getShared(), ptr, Coordinator::requestAndWait(), folly::ReadMostlyMainPtr< T, RefCount >::reset(), and Coordinator::waitForRequest().

134  {
135  std::atomic<int> cnt{0};
136 
137  {
139  Coordinator loads[7];
140 
141  std::thread t1([&] {
142  loads[0].waitForRequest();
143  EXPECT_EQ(ptr.getShared(), nullptr);
144  loads[0].completed();
145 
146  loads[3].waitForRequest();
147  EXPECT_EQ(2, ptr.getShared()->value);
148  loads[3].completed();
149 
150  loads[4].waitForRequest();
151  EXPECT_EQ(4, ptr.getShared()->value);
152  loads[4].completed();
153 
154  loads[5].waitForRequest();
155  EXPECT_EQ(5, ptr.getShared()->value);
156  loads[5].completed();
157  });
158 
159  std::thread t2([&] {
160  loads[1].waitForRequest();
161  EXPECT_EQ(1, ptr.getShared()->value);
162  loads[1].completed();
163 
164  loads[2].waitForRequest();
165  EXPECT_EQ(2, ptr.getShared()->value);
166  loads[2].completed();
167 
168  loads[6].waitForRequest();
169  EXPECT_EQ(5, ptr.getShared()->value);
170  loads[6].completed();
171  });
172 
173  loads[0].requestAndWait();
174 
175  ptr.reset(std::make_unique<TestObject>(1, cnt));
176  loads[1].requestAndWait();
177 
178  ptr.reset(std::make_unique<TestObject>(2, cnt));
179  loads[2].requestAndWait();
180  loads[3].requestAndWait();
181 
182  ptr.reset(std::make_unique<TestObject>(3, cnt));
183  ptr.reset(std::make_unique<TestObject>(4, cnt));
184  loads[4].requestAndWait();
185 
186  ptr.reset(std::make_unique<TestObject>(5, cnt));
187  loads[5].requestAndWait();
188  loads[6].requestAndWait();
189 
190  EXPECT_EQ(1, cnt.load());
191 
192  t1.join();
193  t2.join();
194  }
195 
196  EXPECT_EQ(0, cnt.load());
197 }
void * ptr
ReadMostlySharedPtr< T, RefCount > getShared() const
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( ReadMostlySharedPtrTest  ,
Ctor   
)

Definition at line 199 of file ReadMostlySharedPtrTest.cpp.

References EXPECT_EQ, folly::ReadMostlyMainPtr< T, RefCount >::getShared(), and ptr.

199  {
200  std::atomic<int> cnt1{0};
201  {
202  ReadMostlyMainPtr<TestObject> ptr(std::make_unique<TestObject>(1, cnt1));
203 
204  EXPECT_EQ(1, ptr.getShared()->value);
205  }
206 
207  EXPECT_EQ(0, cnt1.load());
208 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( ReadMostlySharedPtrTest  ,
ClearingCache   
)

Definition at line 210 of file ReadMostlySharedPtrTest.cpp.

References c, Coordinator::completed(), EXPECT_EQ, folly::ReadMostlyMainPtr< T, RefCount >::getShared(), ptr, Coordinator::requestAndWait(), folly::ReadMostlyMainPtr< T, RefCount >::reset(), folly::pushmi::detail::t, and Coordinator::waitForRequest().

210  {
211  std::atomic<int> cnt1{0};
212  std::atomic<int> cnt2{0};
213 
215 
216  // Store 1.
217  ptr.reset(std::make_unique<TestObject>(1, cnt1));
218 
219  Coordinator c;
220 
221  std::thread t([&] {
222  // Cache the pointer for this thread.
223  ptr.getShared();
224  c.requestAndWait();
225  });
226 
227  // Wait for the thread to cache pointer.
228  c.waitForRequest();
229  EXPECT_EQ(1, cnt1.load());
230 
231  // Store 2 and check that 1 is destroyed.
232  ptr.reset(std::make_unique<TestObject>(2, cnt2));
233  EXPECT_EQ(0, cnt1.load());
234 
235  // Unblock thread.
236  c.completed();
237  t.join();
238 }
void * ptr
ReadMostlySharedPtr< T, RefCount > getShared() const
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char c

Definition at line 277 of file ReadMostlySharedPtrTest.cpp.

References folly::ReadMostlyMainPtrDeleter< RefCount >::add(), EXPECT_EQ, folly::gen::move, and useGlobalCalls.

277  {
279  {
280  ReadMostlyMainPtr<int, TestRefCount> ptr1(std::make_shared<int>(42));
281  ReadMostlyMainPtr<int, TestRefCount> ptr2(std::make_shared<int>(42));
282  }
283 
285 
286  useGlobalCalls = 0;
287  {
288  ReadMostlyMainPtr<int, TestRefCount> ptr1(std::make_shared<int>(42));
289  ReadMostlyMainPtr<int, TestRefCount> ptr2(std::make_shared<int>(42));
290 
292  deleter.add(std::move(ptr1));
293  deleter.add(std::move(ptr2));
294  }
295 
297 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void add(ReadMostlyMainPtr< T, RefCount > ptr) noexcept
size_t useGlobalCalls
TEST_F ( ReadMostlySharedPtrTest  ,
nullptr   
)

Definition at line 299 of file ReadMostlySharedPtrTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_NE, EXPECT_TRUE, and ptr.

299  {
300  {
302  EXPECT_TRUE(nptr == nullptr);
303  EXPECT_TRUE(nullptr == nptr);
304  EXPECT_EQ(nptr, nullptr);
305  EXPECT_EQ(nullptr, nptr);
306  EXPECT_FALSE(nptr);
307  EXPECT_TRUE(!nptr);
308 
309  ReadMostlyMainPtr<int, TestRefCount> ptr(std::make_shared<int>(42));
310  EXPECT_FALSE(ptr == nullptr);
311  EXPECT_FALSE(nullptr == ptr);
312  EXPECT_NE(ptr, nullptr);
313  EXPECT_NE(nullptr, ptr);
314  EXPECT_FALSE(!ptr);
315  EXPECT_TRUE(ptr);
316  }
317  {
319  EXPECT_TRUE(nptr == nullptr);
320  EXPECT_TRUE(nullptr == nptr);
321  EXPECT_EQ(nptr, nullptr);
322  EXPECT_EQ(nullptr, nptr);
323  EXPECT_FALSE(nptr);
324  EXPECT_TRUE(!nptr);
325 
326  ReadMostlyMainPtr<int, TestRefCount> ptr(std::make_shared<int>(42));
327  EXPECT_FALSE(ptr == nullptr);
328  EXPECT_FALSE(nullptr == ptr);
329  EXPECT_NE(ptr, nullptr);
330  EXPECT_NE(nullptr, ptr);
331  EXPECT_FALSE(!ptr);
332  EXPECT_TRUE(ptr);
333  }
334 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_F ( ReadMostlySharedPtrTest  ,
getStdShared   
)

Definition at line 336 of file ReadMostlySharedPtrTest.cpp.

References folly::ReadMostlyMainPtr< T, RefCount >::getShared(), folly::ReadMostlyMainPtr< T, RefCount >::getStdShared(), folly::ReadMostlyMainPtr< T, RefCount >::reset(), and SUCCEED.

336  {
337  const ReadMostlyMainPtr<int> rmmp1(std::make_shared<int>(42));
338 
340  rmmp2.reset(rmmp1.getStdShared());
341 
342  const ReadMostlySharedPtr<int> rmsp1 = rmmp1.getShared();
343  ReadMostlySharedPtr<int> rmsp2(rmsp1);
344 
345  // No conditions to check; we just wanted to ensure this compiles.
346  SUCCEED();
347 }
#define SUCCEED()
Definition: gtest.h:1831

Variable Documentation

const unsigned int TEST_TIMEOUT = 10
size_t useGlobalCalls = 0

Definition at line 240 of file ReadMostlySharedPtrTest.cpp.

Referenced by TEST_F(), and TestRefCount::useGlobal().