proxygen
LRUPersistentCacheTest.cpp File Reference

Go to the source code of this file.

Classes

class  MockPersistenceLayer
 
class  LRUPersistentCacheTest< MutexT >
 

Typedefs

using TestPersistenceLayer = CachePersistence< string, string >
 
using MutexTypes = ::testing::Types< std::mutex, folly::SharedMutex >
 

Functions

 TYPED_TEST_CASE (LRUPersistentCacheTest, MutexTypes)
 
template<typename T >
static shared_ptr< LRUPersistentCache< string, string, T > > createCache (size_t capacity, uint32_t syncMillis, std::unique_ptr< TestPersistenceLayer > persistence=nullptr)
 
template<typename T >
static shared_ptr< LRUPersistentCache< string, string, T > > createCacheWithExecutor (std::shared_ptr< folly::Executor > executor, std::unique_ptr< TestPersistenceLayer > persistence, std::chrono::milliseconds syncInterval, int retryLimit)
 
 TYPED_TEST (LRUPersistentCacheTest, NullPersistence)
 
 MATCHER_P (DynSize, n,"")
 
 TYPED_TEST (LRUPersistentCacheTest, SettingPersistence)
 
 TYPED_TEST (LRUPersistentCacheTest, SyncOnDestroy)
 
 TYPED_TEST (LRUPersistentCacheTest, SetPersistenceMidPersist)
 
 TYPED_TEST (LRUPersistentCacheTest, PersistNotCalled)
 
 TYPED_TEST (LRUPersistentCacheTest, PersistentSetBeforeSyncer)
 
 TYPED_TEST (LRUPersistentCacheTest, ClearKeepPersist)
 
 TYPED_TEST (LRUPersistentCacheTest, ClearDontKeepPersist)
 
 TYPED_TEST (LRUPersistentCacheTest, ExecutorCacheDeallocBeforeAdd)
 
 TYPED_TEST (LRUPersistentCacheTest, ExecutorCacheRunTask)
 
 TYPED_TEST (LRUPersistentCacheTest, ExecutorCacheRunTaskInline)
 
 TYPED_TEST (LRUPersistentCacheTest, ExecutorCacheRetries)
 
 TYPED_TEST (LRUPersistentCacheTest, ExecutorCacheSchduledAndDealloc)
 
 TYPED_TEST (LRUPersistentCacheTest, ExecutorCacheScheduleInterval)
 

Typedef Documentation

using MutexTypes = ::testing::Types<std::mutex, folly::SharedMutex>

Definition at line 36 of file LRUPersistentCacheTest.cpp.

Function Documentation

template<typename T >
static shared_ptr<LRUPersistentCache<string, string, T> > createCache ( size_t  capacity,
uint32_t  syncMillis,
std::unique_ptr< TestPersistenceLayer persistence = nullptr 
)
static

Definition at line 40 of file LRUPersistentCacheTest.cpp.

References folly::gen::move.

43  {
44  using TestCache = LRUPersistentCache<string, string, T>;
45  return std::make_shared<TestCache>(
46  capacity,
47  std::chrono::milliseconds(syncMillis),
48  3,
49  std::move(persistence));
50 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<typename T >
static shared_ptr<LRUPersistentCache<string, string, T> > createCacheWithExecutor ( std::shared_ptr< folly::Executor executor,
std::unique_ptr< TestPersistenceLayer persistence,
std::chrono::milliseconds  syncInterval,
int  retryLimit 
)
static

Definition at line 54 of file LRUPersistentCacheTest.cpp.

References folly::gen::move.

58  {
59  return std::make_shared<LRUPersistentCache<string, string, T>>(
61  10,
62  syncInterval,
63  retryLimit,
64  std::move(persistence));
65 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
MATCHER_P ( DynSize  ,
,
""   
)

Definition at line 126 of file LRUPersistentCacheTest.cpp.

126  {
127  return size_t(n) == arg.size();
128 }
TYPED_TEST ( LRUPersistentCacheTest  ,
NullPersistence   
)

Definition at line 113 of file LRUPersistentCacheTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::makeFuture(), and val.

113  {
114  // make sure things sync even without a persistence layer
115  auto cache = createCache<TypeParam>(10, 1, nullptr);
116  cache->put("k0", "v0");
117  makeFuture().delayed(std::chrono::milliseconds(20)).thenValue(
118  [cache](auto&&) {
119  auto val = cache->get("k0");
120  EXPECT_TRUE(val);
121  EXPECT_EQ(*val, "v0");
122  EXPECT_FALSE(cache->hasPendingUpdates());
123  });
124 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
double val
Definition: String.cpp:273
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TYPED_TEST ( LRUPersistentCacheTest  ,
SettingPersistence   
)

Definition at line 130 of file LRUPersistentCacheTest.cpp.

References folly::dynamic::array(), folly::data(), EXPECT_CALL, folly::gen::move, testing::Return(), and folly::gen::seq().

130  {
131  auto cache = createCache<TypeParam>(10, 10, nullptr);
132  cache->put("k0", "v0");
133  folly::dynamic data = dynamic::array(dynamic::array("k1", "v1"));
134  InSequence seq;
135  EXPECT_CALL(*this->persistence, load_())
136  .Times(1)
137  .WillOnce(Return(data));
138  EXPECT_CALL(*this->persistence, persist_(DynSize(2)))
139  .Times(1)
140  .WillOnce(Return(true));
141  cache->setPersistence(std::move(this->persistence));
142 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Gen seq(Value first, Value last)
Definition: Base.h:484
#define EXPECT_CALL(obj, call)
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
SyncOnDestroy   
)

Definition at line 144 of file LRUPersistentCacheTest.cpp.

References testing::_, EXPECT_CALL, folly::gen::move, and testing::Return().

144  {
145  auto cache = createCache<TypeParam>(10, 10000, nullptr);
146  cache->setSyncOnDestroy(true);
147  auto persistence = this->persistence.get();
148  cache->setPersistence(std::move(this->persistence));
149  cache->put("k0", "v0");
150  EXPECT_CALL(*persistence, persist_(_))
151  .Times(1)
152  .WillOnce(Return(true));
153  cache.reset();
154 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
SetPersistenceMidPersist   
)

Definition at line 156 of file LRUPersistentCacheTest.cpp.

References folly::dynamic::array(), EXPECT_CALL, MockPersistenceLayer::getLastPersistedVersionConcrete(), testing::Invoke(), folly::makeFuture(), folly::gen::move, ON_CALL, and testing::Return().

156  {
157  // Setup a cache with no persistence layer
158  // Add some items
159  // Add a persistence layer, that during persist will call a function
160  // to set a new persistence layer on the cache
161  // Ensure that the new layer is called with the data
162  auto cache = createCache<TypeParam>(10, 10, nullptr);
163  cache->put("k0", "v0");
164  cache->put("k1", "v1");
165 
166  EXPECT_CALL(*this->persistence, load_())
167  .Times(1)
168  .WillOnce(Return(dynamic::array()));
169 
170  auto func = [cache](const folly::dynamic& /* kv */ ) {
171  // The cache persistence that we'll set during a call to persist
172  auto p2 = make_unique<MockPersistenceLayer>();
173  ON_CALL(*p2, getLastPersistedVersion())
174  .WillByDefault(
175  Invoke(
176  p2.get(),
178  EXPECT_CALL(*p2, load_())
179  .Times(1)
180  .WillOnce(Return(dynamic::array()));
181  EXPECT_CALL(*p2, persist_(DynSize(2)))
182  .Times(1)
183  .WillOnce(Return(true));
184 
185  cache->setPersistence(std::move(p2));
186  return true;
187  };
188  EXPECT_CALL(*this->persistence, persist_(DynSize(2)))
189  .Times(1)
190  .WillOnce(Invoke(func));
191 
192  cache->setPersistence(std::move(this->persistence));
193  makeFuture().delayed(std::chrono::milliseconds(100)).get();
194 }
CacheDataVersion getLastPersistedVersionConcrete() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define ON_CALL(obj, call)
#define EXPECT_CALL(obj, call)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
PersistNotCalled   
)

Definition at line 196 of file LRUPersistentCacheTest.cpp.

References testing::_, folly::dynamic::array(), folly::data(), EXPECT_CALL, EXPECT_EQ, folly::gen::move, and testing::Return().

196  {
197  folly::dynamic data = dynamic::array(dynamic::array("k1", "v1"));
198  EXPECT_CALL(*this->persistence, load_())
199  .Times(1)
200  .WillOnce(Return(data));
201  EXPECT_CALL(*this->persistence, persist_(_))
202  .Times(0)
203  .WillOnce(Return(false));
204  auto cache = createCache<TypeParam>(10, 10, std::move(this->persistence));
205  EXPECT_EQ(cache->size(), 1);
206 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
PersistentSetBeforeSyncer   
)

Definition at line 208 of file LRUPersistentCacheTest.cpp.

References testing::AtLeast(), EXPECT_CALL, MockPersistenceLayer::getLastPersistedVersionConcrete(), testing::Invoke(), and folly::gen::move.

208  {
209  EXPECT_CALL(*this->persistence, getLastPersistedVersion())
210  .Times(AtLeast(1))
211  .WillRepeatedly(
212  Invoke(
213  this->persistence.get(),
215  auto cache = createCache<TypeParam>(10, 10, std::move(this->persistence));
216 }
GTEST_API_ Cardinality AtLeast(int n)
CacheDataVersion getLastPersistedVersionConcrete() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_CALL(obj, call)
TYPED_TEST ( LRUPersistentCacheTest  ,
ClearKeepPersist   
)

Definition at line 218 of file LRUPersistentCacheTest.cpp.

References EXPECT_CALL, and folly::gen::move.

218  {
219  EXPECT_CALL(*this->persistence, clear()).Times(0);
220  auto cache = createCache<TypeParam>(10, 10, std::move(this->persistence));
221  cache->clear();
222 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_CALL(obj, call)
TYPED_TEST ( LRUPersistentCacheTest  ,
ClearDontKeepPersist   
)

Definition at line 224 of file LRUPersistentCacheTest.cpp.

References EXPECT_CALL, and folly::gen::move.

224  {
225  EXPECT_CALL(*this->persistence, clear()).Times(1);
226  auto cache = createCache<TypeParam>(10, 10, std::move(this->persistence));
227  cache->clear(true);
228 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_CALL(obj, call)
TYPED_TEST ( LRUPersistentCacheTest  ,
ExecutorCacheDeallocBeforeAdd   
)

Definition at line 230 of file LRUPersistentCacheTest.cpp.

References folly::gen::move.

230  {
231  auto cache = createCacheWithExecutor<TypeParam>(
232  this->manualExecutor,
233  std::move(this->persistence),
234  std::chrono::milliseconds::zero(),
235  1);
236  cache.reset();
237  // Nothing should happen here
238  this->manualExecutor->drain();
239 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
TYPED_TEST ( LRUPersistentCacheTest  ,
ExecutorCacheRunTask   
)

Definition at line 241 of file LRUPersistentCacheTest.cpp.

References folly::dynamic::array(), folly::data(), EXPECT_CALL, MockPersistenceLayer::getLastPersistedVersionConcrete(), testing::Invoke(), folly::gen::move, and testing::Return().

241  {
242  folly::dynamic data = dynamic::array(dynamic::array("k1", "v1"));
243  EXPECT_CALL(*this->persistence, load_())
244  .Times(1)
245  .WillOnce(Return(data));
246  auto rawPersistence = this->persistence.get();
247  auto cache = createCacheWithExecutor<TypeParam>(
248  this->manualExecutor,
249  std::move(this->persistence),
250  std::chrono::milliseconds::zero(),
251  1);
252  cache->put("k0", "v0");
253  EXPECT_CALL(*rawPersistence, getLastPersistedVersion())
254  .Times(1)
255  .WillOnce(Invoke(
256  rawPersistence,
258  EXPECT_CALL(*rawPersistence, persist_(DynSize(2)))
259  .Times(1)
260  .WillOnce(Return(true));
261  this->manualExecutor->run();
262  cache.reset();
263 }
CacheDataVersion getLastPersistedVersionConcrete() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_CALL(obj, call)
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
ExecutorCacheRunTaskInline   
)

Definition at line 265 of file LRUPersistentCacheTest.cpp.

References folly::dynamic::array(), folly::data(), EXPECT_CALL, MockPersistenceLayer::getLastPersistedVersionConcrete(), testing::Invoke(), folly::gen::move, and testing::Return().

265  {
266  folly::dynamic data = dynamic::array(dynamic::array("k1", "v1"));
267  EXPECT_CALL(*this->persistence, load_())
268  .Times(1)
269  .WillOnce(Return(data));
270  auto rawPersistence = this->persistence.get();
271  auto cache = createCacheWithExecutor<TypeParam>(
272  this->inlineExecutor,
273  std::move(this->persistence),
274  std::chrono::milliseconds::zero(),
275  1);
276  EXPECT_CALL(*rawPersistence, getLastPersistedVersion())
277  .Times(1)
278  .WillOnce(Invoke(
279  rawPersistence,
281  EXPECT_CALL(*rawPersistence, persist_(DynSize(2)))
282  .Times(1)
283  .WillOnce(Return(true));
284  cache->put("k0", "v0");
285 
286  EXPECT_CALL(*rawPersistence, getLastPersistedVersion())
287  .Times(1)
288  .WillOnce(Invoke(
289  rawPersistence,
291  EXPECT_CALL(*rawPersistence, persist_(DynSize(3)))
292  .Times(1)
293  .WillOnce(Return(true));
294  cache->put("k2", "v2");
295  cache.reset();
296 }
CacheDataVersion getLastPersistedVersionConcrete() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_CALL(obj, call)
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
ExecutorCacheRetries   
)

Definition at line 298 of file LRUPersistentCacheTest.cpp.

References testing::_, folly::dynamic::array(), EXPECT_CALL, MockPersistenceLayer::getLastPersistedVersionConcrete(), testing::Invoke(), folly::gen::move, testing::Return(), and MockPersistenceLayer::setPersistedVersionConcrete().

298  {
299  EXPECT_CALL(*this->persistence, load_())
300  .Times(1)
301  .WillOnce(Return(dynamic::array()));
302  auto rawPersistence = this->persistence.get();
303  auto cache = createCacheWithExecutor<TypeParam>(
304  this->manualExecutor,
305  std::move(this->persistence),
306  std::chrono::milliseconds::zero(),
307  2);
308  EXPECT_CALL(*rawPersistence, getLastPersistedVersion())
309  .WillRepeatedly(Invoke(
310  rawPersistence,
312 
313  cache->put("k0", "v0");
314  EXPECT_CALL(*rawPersistence, persist_(DynSize(1)))
315  .Times(1)
316  .WillOnce(Return(false));
317  this->manualExecutor->run();
318 
319  cache->put("k1", "v1");
320  EXPECT_CALL(*rawPersistence, persist_(DynSize(2)))
321  .Times(1)
322  .WillOnce(Return(false));
323  // reached retry limit, so we will set a version anyway
324  EXPECT_CALL(*rawPersistence, setPersistedVersion(_))
325  .Times(1)
326  .WillOnce(Invoke(
328  this->manualExecutor->run();
329 
330  cache.reset();
331 }
CacheDataVersion getLastPersistedVersionConcrete() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void setPersistedVersionConcrete(CacheDataVersion version)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
ExecutorCacheSchduledAndDealloc   
)

Definition at line 333 of file LRUPersistentCacheTest.cpp.

References folly::dynamic::array(), folly::data(), EXPECT_CALL, folly::gen::move, and testing::Return().

333  {
334  folly::dynamic data = dynamic::array(dynamic::array("k1", "v1"));
335  EXPECT_CALL(*this->persistence, load_())
336  .Times(1)
337  .WillOnce(Return(data));
338  auto cache = createCacheWithExecutor<TypeParam>(
339  this->manualExecutor,
340  std::move(this->persistence),
341  std::chrono::milliseconds::zero(),
342  1);
343  cache->put("k0", "v0");
344  cache->put("k2", "v2");
345 
346  // Kill cache first then try to run scheduled tasks. Nothing will run and no
347  // one should crash.
348  cache.reset();
349  this->manualExecutor->drain();
350 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_CALL(obj, call)
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
internal::ReturnAction< R > Return(R value)
TYPED_TEST ( LRUPersistentCacheTest  ,
ExecutorCacheScheduleInterval   
)

Definition at line 352 of file LRUPersistentCacheTest.cpp.

References testing::_, folly::dynamic::array(), EXPECT_CALL, MockPersistenceLayer::getLastPersistedVersionConcrete(), testing::Invoke(), folly::gen::move, and testing::Return().

352  {
353  EXPECT_CALL(*this->persistence, load_())
354  .Times(1)
355  .WillOnce(Return(dynamic::array()));
356  auto rawPersistence = this->persistence.get();
357  auto cache = createCacheWithExecutor<TypeParam>(
358  this->manualExecutor,
359  std::move(this->persistence),
360  std::chrono::milliseconds(60 * 60 * 1000),
361  1);
362  EXPECT_CALL(*rawPersistence, getLastPersistedVersion())
363  .WillRepeatedly(Invoke(
364  rawPersistence,
366 
367  cache->put("k0", "v0");
368  EXPECT_CALL(*rawPersistence, persist_(DynSize(1)))
369  .Times(1)
370  .WillOnce(Return(false));
371  this->manualExecutor->run();
372 
373  // None of the following will trigger a run
374  EXPECT_CALL(*rawPersistence, persist_(DynSize(2))).Times(0);
375  EXPECT_CALL(*rawPersistence, setPersistedVersion(_)).Times(0);
376  cache->put("k1", "v1");
377  this->manualExecutor->run();
378  cache.reset();
379  this->manualExecutor->drain();
380 }
CacheDataVersion getLastPersistedVersionConcrete() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TYPED_TEST_CASE ( LRUPersistentCacheTest  ,
MutexTypes   
)