18
18
*/
19
19
20
20
#include < gtest/gtest.h>
21
+ #include < chrono>
21
22
22
23
#include < cache/DefaultCacheImpl.h>
23
24
#include < olp/core/utils/Dir.h>
24
25
26
+ namespace {
25
27
namespace 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
+ };
26
35
27
- namespace {
28
36
class DefaultCacheImplHelper : public cache ::DefaultCacheImpl {
29
37
public:
30
38
explicit DefaultCacheImplHelper (const cache::CacheSettings& settings)
31
- : DefaultCacheImpl(settings) {}
39
+ : cache:: DefaultCacheImpl(settings) {}
32
40
33
41
bool HasLruCache () const { return GetMutableCacheLru ().get () != nullptr ; }
34
42
@@ -95,14 +103,12 @@ class DefaultCacheImplHelper : public cache::DefaultCacheImpl {
95
103
}
96
104
};
97
105
98
- TEST (DefaultCacheImplTest, LruCache) {
99
- std::string cache_path = olp::utils::Dir::TempDirectory () + " /unittest" ;
100
-
106
+ TEST_F (DefaultCacheImplTest, LruCache) {
101
107
{
102
108
SCOPED_TRACE (" Successful creation" );
103
109
104
- olp:: cache::CacheSettings settings;
105
- settings.disk_path_mutable = cache_path ;
110
+ cache::CacheSettings settings;
111
+ settings.disk_path_mutable = cache_path_ ;
106
112
DefaultCacheImplHelper cache (settings);
107
113
cache.Open ();
108
114
@@ -112,8 +118,8 @@ TEST(DefaultCacheImplTest, LruCache) {
112
118
{
113
119
SCOPED_TRACE (" kLeastRecentlyUsed eviction policy" );
114
120
115
- olp:: cache::CacheSettings settings;
116
- settings.disk_path_mutable = cache_path ;
121
+ cache::CacheSettings settings;
122
+ settings.disk_path_mutable = cache_path_ ;
117
123
settings.eviction_policy = cache::EvictionPolicy::kLeastRecentlyUsed ;
118
124
DefaultCacheImplHelper cache (settings);
119
125
cache.Open ();
@@ -124,8 +130,8 @@ TEST(DefaultCacheImplTest, LruCache) {
124
130
{
125
131
SCOPED_TRACE (" Close" );
126
132
127
- olp:: cache::CacheSettings settings;
128
- settings.disk_path_mutable = cache_path ;
133
+ cache::CacheSettings settings;
134
+ settings.disk_path_mutable = cache_path_ ;
129
135
DefaultCacheImplHelper cache (settings);
130
136
cache.Open ();
131
137
cache.Close ();
@@ -136,8 +142,8 @@ TEST(DefaultCacheImplTest, LruCache) {
136
142
{
137
143
SCOPED_TRACE (" No Open() call" );
138
144
139
- olp:: cache::CacheSettings settings;
140
- settings.disk_path_mutable = cache_path ;
145
+ cache::CacheSettings settings;
146
+ settings.disk_path_mutable = cache_path_ ;
141
147
DefaultCacheImplHelper cache (settings);
142
148
143
149
EXPECT_FALSE (cache.HasLruCache ());
@@ -146,7 +152,7 @@ TEST(DefaultCacheImplTest, LruCache) {
146
152
{
147
153
SCOPED_TRACE (" Default settings" );
148
154
149
- olp:: cache::CacheSettings settings;
155
+ cache::CacheSettings settings;
150
156
DefaultCacheImplHelper cache (settings);
151
157
cache.Open ();
152
158
@@ -156,8 +162,8 @@ TEST(DefaultCacheImplTest, LruCache) {
156
162
{
157
163
SCOPED_TRACE (" No disk cache size limit" );
158
164
159
- olp:: cache::CacheSettings settings;
160
- settings.disk_path_mutable = cache_path ;
165
+ cache::CacheSettings settings;
166
+ settings.disk_path_mutable = cache_path_ ;
161
167
settings.max_disk_storage = std::uint64_t (-1 );
162
168
DefaultCacheImplHelper cache (settings);
163
169
cache.Open ();
@@ -168,8 +174,8 @@ TEST(DefaultCacheImplTest, LruCache) {
168
174
{
169
175
SCOPED_TRACE (" kNone eviction policy" );
170
176
171
- olp:: cache::CacheSettings settings;
172
- settings.disk_path_mutable = cache_path ;
177
+ cache::CacheSettings settings;
178
+ settings.disk_path_mutable = cache_path_ ;
173
179
settings.eviction_policy = cache::EvictionPolicy::kNone ;
174
180
DefaultCacheImplHelper cache (settings);
175
181
cache.Open ();
@@ -178,8 +184,8 @@ TEST(DefaultCacheImplTest, LruCache) {
178
184
}
179
185
}
180
186
181
- TEST (DefaultCacheImplTest, LruCachePut) {
182
- olp:: cache::CacheSettings settings;
187
+ TEST_F (DefaultCacheImplTest, LruCachePut) {
188
+ cache::CacheSettings settings;
183
189
settings.disk_path_mutable = olp::utils::Dir::TempDirectory () + " /unittest" ;
184
190
185
191
{
@@ -213,8 +219,8 @@ TEST(DefaultCacheImplTest, LruCachePut) {
213
219
}
214
220
}
215
221
216
- TEST (DefaultCacheImplTest, LruCacheGetPromote) {
217
- olp:: cache::CacheSettings settings;
222
+ TEST_F (DefaultCacheImplTest, LruCacheGetPromote) {
223
+ cache::CacheSettings settings;
218
224
settings.disk_path_mutable = olp::utils::Dir::TempDirectory () + " /unittest" ;
219
225
constexpr auto key1{" somekey1" };
220
226
constexpr auto key2{" somekey2" };
@@ -249,8 +255,8 @@ TEST(DefaultCacheImplTest, LruCacheGetPromote) {
249
255
}
250
256
}
251
257
252
- TEST (DefaultCacheImplTest, LruCacheRemove) {
253
- olp:: cache::CacheSettings settings;
258
+ TEST_F (DefaultCacheImplTest, LruCacheRemove) {
259
+ cache::CacheSettings settings;
254
260
settings.disk_path_mutable = olp::utils::Dir::TempDirectory () + " /unittest" ;
255
261
constexpr auto key1{" somekey1" };
256
262
constexpr auto key2{" somekey2" };
@@ -321,8 +327,91 @@ TEST(DefaultCacheImplTest, LruCacheRemove) {
321
327
EXPECT_FALSE (cache.ContainsLru (key2));
322
328
}
323
329
}
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 ();
324
354
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) {
326
415
const std::string key1{" somekey1" };
327
416
const std::string key2{" somekey2" };
328
417
const std::string key3{" anotherkey1" };
@@ -333,9 +422,8 @@ TEST(DefaultCacheImplTest, MutableCacheSize) {
333
422
const auto data_ptr =
334
423
std::make_shared<std::vector<unsigned char >>(binary_data);
335
424
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_;
339
427
340
428
{
341
429
SCOPED_TRACE (" Put decode" );
@@ -478,8 +566,8 @@ TEST(DefaultCacheImplTest, MutableCacheSize) {
478
566
const auto prefix = " somekey" ;
479
567
const auto data_size = 1024u ;
480
568
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_ ;
483
571
settings.eviction_policy = cache::EvictionPolicy::kNone ;
484
572
settings.max_disk_storage = 2u * 1024u * 1024u ;
485
573
DefaultCacheImplHelper cache (settings);
@@ -525,17 +613,15 @@ TEST(DefaultCacheImplTest, MutableCacheSize) {
525
613
}
526
614
}
527
615
528
- TEST (DefaultCacheImplTest, LruCacheEviction) {
529
- std::string cache_path = olp::utils::Dir::TempDirectory () + " /unittest" ;
530
-
616
+ TEST_F (DefaultCacheImplTest, LruCacheEviction) {
531
617
{
532
618
SCOPED_TRACE (" kNone evicts nothing" );
533
619
534
620
const auto prefix = " somekey" ;
535
621
const auto data_size = 1024u ;
536
622
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_ ;
539
625
settings.eviction_policy = cache::EvictionPolicy::kNone ;
540
626
settings.max_disk_storage = 2u * 1024u * 1024u ;
541
627
DefaultCacheImplHelper cache (settings);
@@ -581,8 +667,8 @@ TEST(DefaultCacheImplTest, LruCacheEviction) {
581
667
const auto prefix{" somekey" };
582
668
const auto data_size = 1024u ;
583
669
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_ ;
586
672
settings.eviction_policy = cache::EvictionPolicy::kLeastRecentlyUsed ;
587
673
settings.max_disk_storage = 2u * 1024u * 1024u ;
588
674
DefaultCacheImplHelper cache (settings);
0 commit comments