1818 */
1919
2020#include < gtest/gtest.h>
21+ #include < chrono>
2122
2223#include < cache/DefaultCacheImpl.h>
2324#include < olp/core/utils/Dir.h>
2425
26+ namespace {
2527namespace cache = olp::cache;
28+ class DefaultCacheImplTest : public ::testing::Test {
29+ void TearDown () override { olp::utils::Dir::Remove (cache_path_); }
30+
31+ protected:
32+ const std::string cache_path_ =
33+ olp::utils::Dir::TempDirectory () + " /unittest" ;
34+ };
2635
27- namespace {
2836class DefaultCacheImplHelper : public cache ::DefaultCacheImpl {
2937 public:
3038 explicit DefaultCacheImplHelper (const cache::CacheSettings& settings)
31- : DefaultCacheImpl(settings) {}
39+ : cache:: DefaultCacheImpl(settings) {}
3240
3341 bool HasLruCache () const { return GetMutableCacheLru ().get () != nullptr ; }
3442
@@ -95,14 +103,12 @@ class DefaultCacheImplHelper : public cache::DefaultCacheImpl {
95103 }
96104};
97105
98- TEST (DefaultCacheImplTest, LruCache) {
99- std::string cache_path = olp::utils::Dir::TempDirectory () + " /unittest" ;
100-
106+ TEST_F (DefaultCacheImplTest, LruCache) {
101107 {
102108 SCOPED_TRACE (" Successful creation" );
103109
104- olp:: cache::CacheSettings settings;
105- settings.disk_path_mutable = cache_path ;
110+ cache::CacheSettings settings;
111+ settings.disk_path_mutable = cache_path_ ;
106112 DefaultCacheImplHelper cache (settings);
107113 cache.Open ();
108114
@@ -112,8 +118,8 @@ TEST(DefaultCacheImplTest, LruCache) {
112118 {
113119 SCOPED_TRACE (" kLeastRecentlyUsed eviction policy" );
114120
115- olp:: cache::CacheSettings settings;
116- settings.disk_path_mutable = cache_path ;
121+ cache::CacheSettings settings;
122+ settings.disk_path_mutable = cache_path_ ;
117123 settings.eviction_policy = cache::EvictionPolicy::kLeastRecentlyUsed ;
118124 DefaultCacheImplHelper cache (settings);
119125 cache.Open ();
@@ -124,8 +130,8 @@ TEST(DefaultCacheImplTest, LruCache) {
124130 {
125131 SCOPED_TRACE (" Close" );
126132
127- olp:: cache::CacheSettings settings;
128- settings.disk_path_mutable = cache_path ;
133+ cache::CacheSettings settings;
134+ settings.disk_path_mutable = cache_path_ ;
129135 DefaultCacheImplHelper cache (settings);
130136 cache.Open ();
131137 cache.Close ();
@@ -136,8 +142,8 @@ TEST(DefaultCacheImplTest, LruCache) {
136142 {
137143 SCOPED_TRACE (" No Open() call" );
138144
139- olp:: cache::CacheSettings settings;
140- settings.disk_path_mutable = cache_path ;
145+ cache::CacheSettings settings;
146+ settings.disk_path_mutable = cache_path_ ;
141147 DefaultCacheImplHelper cache (settings);
142148
143149 EXPECT_FALSE (cache.HasLruCache ());
@@ -146,7 +152,7 @@ TEST(DefaultCacheImplTest, LruCache) {
146152 {
147153 SCOPED_TRACE (" Default settings" );
148154
149- olp:: cache::CacheSettings settings;
155+ cache::CacheSettings settings;
150156 DefaultCacheImplHelper cache (settings);
151157 cache.Open ();
152158
@@ -156,8 +162,8 @@ TEST(DefaultCacheImplTest, LruCache) {
156162 {
157163 SCOPED_TRACE (" No disk cache size limit" );
158164
159- olp:: cache::CacheSettings settings;
160- settings.disk_path_mutable = cache_path ;
165+ cache::CacheSettings settings;
166+ settings.disk_path_mutable = cache_path_ ;
161167 settings.max_disk_storage = std::uint64_t (-1 );
162168 DefaultCacheImplHelper cache (settings);
163169 cache.Open ();
@@ -168,8 +174,8 @@ TEST(DefaultCacheImplTest, LruCache) {
168174 {
169175 SCOPED_TRACE (" kNone eviction policy" );
170176
171- olp:: cache::CacheSettings settings;
172- settings.disk_path_mutable = cache_path ;
177+ cache::CacheSettings settings;
178+ settings.disk_path_mutable = cache_path_ ;
173179 settings.eviction_policy = cache::EvictionPolicy::kNone ;
174180 DefaultCacheImplHelper cache (settings);
175181 cache.Open ();
@@ -178,8 +184,8 @@ TEST(DefaultCacheImplTest, LruCache) {
178184 }
179185}
180186
181- TEST (DefaultCacheImplTest, LruCachePut) {
182- olp:: cache::CacheSettings settings;
187+ TEST_F (DefaultCacheImplTest, LruCachePut) {
188+ cache::CacheSettings settings;
183189 settings.disk_path_mutable = olp::utils::Dir::TempDirectory () + " /unittest" ;
184190
185191 {
@@ -213,8 +219,8 @@ TEST(DefaultCacheImplTest, LruCachePut) {
213219 }
214220}
215221
216- TEST (DefaultCacheImplTest, LruCacheGetPromote) {
217- olp:: cache::CacheSettings settings;
222+ TEST_F (DefaultCacheImplTest, LruCacheGetPromote) {
223+ cache::CacheSettings settings;
218224 settings.disk_path_mutable = olp::utils::Dir::TempDirectory () + " /unittest" ;
219225 constexpr auto key1{" somekey1" };
220226 constexpr auto key2{" somekey2" };
@@ -249,8 +255,8 @@ TEST(DefaultCacheImplTest, LruCacheGetPromote) {
249255 }
250256}
251257
252- TEST (DefaultCacheImplTest, LruCacheRemove) {
253- olp:: cache::CacheSettings settings;
258+ TEST_F (DefaultCacheImplTest, LruCacheRemove) {
259+ cache::CacheSettings settings;
254260 settings.disk_path_mutable = olp::utils::Dir::TempDirectory () + " /unittest" ;
255261 constexpr auto key1{" somekey1" };
256262 constexpr auto key2{" somekey2" };
@@ -321,8 +327,91 @@ TEST(DefaultCacheImplTest, LruCacheRemove) {
321327 EXPECT_FALSE (cache.ContainsLru (key2));
322328 }
323329}
330+ TEST_F (DefaultCacheImplTest, MutableCacheExpired) {
331+ SCOPED_TRACE (" Expiry mutable cache" );
332+ const std::string key1{" somekey1" };
333+ const std::string key2{" somekey2" };
334+ const std::string data_string{" this is key's data" };
335+ constexpr auto expiry = 1 ;
336+ std::vector<unsigned char > binary_data = {1 , 2 , 3 };
337+ const auto data_ptr =
338+ std::make_shared<std::vector<unsigned char >>(binary_data);
339+
340+ cache::CacheSettings settings1;
341+ settings1.disk_path_mutable = cache_path_;
342+ DefaultCacheImplHelper cache1 (settings1);
343+ cache1.Open ();
344+ cache1.Clear ();
345+
346+ cache1.Put (key1, data_ptr, expiry);
347+ cache1.Put (key2, data_string, [=]() { return data_string; }, expiry);
348+ cache1.Close ();
349+
350+ cache::CacheSettings settings2;
351+ settings2.disk_path_mutable = cache_path_;
352+ DefaultCacheImplHelper cache2 (settings2);
353+ cache2.Open ();
324354
325- TEST (DefaultCacheImplTest, MutableCacheSize) {
355+ const auto value = cache2.Get (key1);
356+ const auto value2 =
357+ cache2.Get (key2, [](const std::string& value) { return value; });
358+
359+ EXPECT_FALSE (value.get () == nullptr );
360+ EXPECT_EQ (value2.type (), typeid (std::string));
361+ auto str = boost::any_cast<std::string>(value2);
362+ EXPECT_EQ (str, data_string);
363+
364+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
365+
366+ EXPECT_TRUE (cache2.Get (key1).get () == nullptr );
367+ EXPECT_TRUE (
368+ cache2.Get (key2, [](const std::string& value) { return value; }).empty ());
369+ cache2.Close ();
370+ }
371+
372+ TEST_F (DefaultCacheImplTest, ProtectedCacheExpired) {
373+ SCOPED_TRACE (" Expiry protected cache" );
374+ const std::string key1{" somekey1" };
375+ const std::string key2{" somekey2" };
376+ const std::string data_string{" this is key's data" };
377+ constexpr auto expiry = 1 ;
378+ std::vector<unsigned char > binary_data = {1 , 2 , 3 };
379+ const auto data_ptr =
380+ std::make_shared<std::vector<unsigned char >>(binary_data);
381+
382+ cache::CacheSettings settings1;
383+ settings1.disk_path_mutable = cache_path_;
384+ DefaultCacheImplHelper cache1 (settings1);
385+ cache1.Open ();
386+ cache1.Clear ();
387+
388+ cache1.Put (key1, data_ptr, expiry);
389+ cache1.Put (key2, data_string, [=]() { return data_string; }, expiry);
390+ cache1.Close ();
391+
392+ cache::CacheSettings settings2;
393+ settings2.disk_path_protected = cache_path_;
394+ DefaultCacheImplHelper cache2 (settings2);
395+ cache2.Open ();
396+
397+ const auto value = cache2.Get (key1);
398+ const auto value2 =
399+ cache2.Get (key2, [](const std::string& value) { return value; });
400+
401+ EXPECT_FALSE (value.get () == nullptr );
402+ EXPECT_EQ (value2.type (), typeid (std::string));
403+ auto str = boost::any_cast<std::string>(value2);
404+ EXPECT_EQ (str, data_string);
405+
406+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
407+
408+ EXPECT_TRUE (cache2.Get (key1).get () == nullptr );
409+ EXPECT_TRUE (
410+ cache2.Get (key2, [](const std::string& value) { return value; }).empty ());
411+ cache2.Close ();
412+ }
413+
414+ TEST_F (DefaultCacheImplTest, MutableCacheSize) {
326415 const std::string key1{" somekey1" };
327416 const std::string key2{" somekey2" };
328417 const std::string key3{" anotherkey1" };
@@ -333,9 +422,8 @@ TEST(DefaultCacheImplTest, MutableCacheSize) {
333422 const auto data_ptr =
334423 std::make_shared<std::vector<unsigned char >>(binary_data);
335424
336- std::string cache_path = olp::utils::Dir::TempDirectory () + " /unittest" ;
337- olp::cache::CacheSettings settings;
338- settings.disk_path_mutable = cache_path;
425+ cache::CacheSettings settings;
426+ settings.disk_path_mutable = cache_path_;
339427
340428 {
341429 SCOPED_TRACE (" Put decode" );
@@ -478,8 +566,8 @@ TEST(DefaultCacheImplTest, MutableCacheSize) {
478566 const auto prefix = " somekey" ;
479567 const auto data_size = 1024u ;
480568 std::vector<unsigned char > binary_data (data_size);
481- olp:: cache::CacheSettings settings;
482- settings.disk_path_mutable = cache_path ;
569+ cache::CacheSettings settings;
570+ settings.disk_path_mutable = cache_path_ ;
483571 settings.eviction_policy = cache::EvictionPolicy::kNone ;
484572 settings.max_disk_storage = 2u * 1024u * 1024u ;
485573 DefaultCacheImplHelper cache (settings);
@@ -525,17 +613,15 @@ TEST(DefaultCacheImplTest, MutableCacheSize) {
525613 }
526614}
527615
528- TEST (DefaultCacheImplTest, LruCacheEviction) {
529- std::string cache_path = olp::utils::Dir::TempDirectory () + " /unittest" ;
530-
616+ TEST_F (DefaultCacheImplTest, LruCacheEviction) {
531617 {
532618 SCOPED_TRACE (" kNone evicts nothing" );
533619
534620 const auto prefix = " somekey" ;
535621 const auto data_size = 1024u ;
536622 std::vector<unsigned char > binary_data (data_size);
537- olp:: cache::CacheSettings settings;
538- settings.disk_path_mutable = cache_path ;
623+ cache::CacheSettings settings;
624+ settings.disk_path_mutable = cache_path_ ;
539625 settings.eviction_policy = cache::EvictionPolicy::kNone ;
540626 settings.max_disk_storage = 2u * 1024u * 1024u ;
541627 DefaultCacheImplHelper cache (settings);
@@ -581,8 +667,8 @@ TEST(DefaultCacheImplTest, LruCacheEviction) {
581667 const auto prefix{" somekey" };
582668 const auto data_size = 1024u ;
583669 std::vector<unsigned char > binary_data (data_size);
584- olp:: cache::CacheSettings settings;
585- settings.disk_path_mutable = cache_path ;
670+ cache::CacheSettings settings;
671+ settings.disk_path_mutable = cache_path_ ;
586672 settings.eviction_policy = cache::EvictionPolicy::kLeastRecentlyUsed ;
587673 settings.max_disk_storage = 2u * 1024u * 1024u ;
588674 DefaultCacheImplHelper cache (settings);
0 commit comments