27 template<
typename MutexT>
30 using MutexTypes = ::testing::Types<std::mutex, folly::SharedMutex>;
36 vector<KeyType> keys = {
"key1",
"key2"};
37 vector<ValType>
values = {
"value1",
"value2"};
38 testSimplePutGet<KeyType, ValType, TypeParam>(keys,
values);
43 using ValType = double;
44 vector<KeyType> keys = {1, 2};
45 vector<ValType>
values = {3.0, 4.0};
46 testSimplePutGet<KeyType, ValType, TypeParam>(keys,
values);
51 using ValType = list<string>;
52 vector<KeyType> keys = {
"key1",
"key2"};
54 {ValType({
"fma",
"shijin"}), ValType({
"foo",
"bar"})};
55 testSimplePutGet<KeyType, ValType, TypeParam>(keys,
values);
60 using ValType = map<string, list<string>>;
61 vector<KeyType> keys = {
"cool",
"not cool"};
64 {
"NYC", {
"fma",
"shijin"}},
65 {
"MPK", {
"ranjeeth",
"dsp"}}
68 {
"MPK", {
"subodh",
"blake"}},
72 testSimplePutGet<KeyType, ValType, TypeParam>(keys,
values);
76 using KeyType = pair<string, string>;
77 using ValType = map<string, list<string>>;
78 vector<KeyType> keys = {
79 make_pair(
"cool",
"what the=?"),
80 make_pair(
"not_cool",
"how on *& earth?")
84 {
"NYC", {
"fma",
"shijin kong$"}},
85 {
"MPK", {
"ranjeeth",
"dsp"}}
88 {
"MPK", {
"subodh",
"blake"}},
92 testSimplePutGet<KeyType, ValType, TypeParam>(keys,
values);
95 template<
typename K,
typename V,
typename MutexT>
98 size_t cacheCapacity = 10;
101 O_WRONLY | O_CREAT | O_TRUNC,
106 CacheType cache(filename, cacheCapacity, chrono::seconds(1));
115 testEmptyFile<KeyType, ValType, TypeParam>();
120 using ValType = map<string, list<string>>;
121 testEmptyFile<KeyType, ValType, TypeParam>();
125 template<
typename K,
typename V,
typename MutexT>
128 size_t cacheCapacity = 10;
131 O_WRONLY | O_CREAT | O_TRUNC,
140 CacheType cache(filename, cacheCapacity, chrono::seconds(1));
149 testInvalidFile<KeyType, ValType, TypeParam>(
string(
"{\"k1\":\"v1\",1}"));
154 using ValType = map<string, list<string>>;
155 testInvalidFile<KeyType, ValType, TypeParam>(
string(
"{\"k1\":\"v1\"}"));
159 template<
typename K,
typename V,
typename MutexT>
162 const vector<K>& keys,
166 size_t cacheCapacity = 10;
169 O_WRONLY | O_CREAT | O_TRUNC,
178 CacheType cache(filename, cacheCapacity, chrono::seconds(1));
180 for (
size_t i = 0;
i < keys.size(); ++
i) {
190 vector<KeyType> keys = {
"key1",
"key2"};
191 vector<ValType>
values = {
"value1",
"value2"};
192 std::string content =
"[[\"key1\",\"value1\"], [\"key2\",\"value2\"]]";
193 testValidFile<KeyType, ValType, TypeParam>(content, keys,
values);
199 size_t cacheCapacity = 10;
201 cacheCapacity, chrono::seconds(1));
202 for (
int i = 0;
i < 10; ++
i) {
228 vector<KeyType> keys = {
"key1",
"key2"};
229 vector<ValType>
values = {
"value1",
"value2"};
230 std::string content =
"{\"key1\":\"value1\", \"key2\":\"value2\"}";
233 size_t cacheCapacity = 10;
236 O_WRONLY | O_CREAT | O_TRUNC,
248 CacheType cache(filename, cacheCapacity, chrono::seconds(1));
252 cache.put(
"key1",
"value1");
253 cache.put(
"key2",
"value2");
255 EXPECT_EQ(cache.get(
"key1").value(),
"value1");
256 EXPECT_EQ(cache.get(
"key2").value(),
"value2");
260 CacheType cache(filename, cacheCapacity, chrono::seconds(1));
262 EXPECT_EQ(cache.get(
"key1").value(),
"value1");
263 EXPECT_EQ(cache.get(
"key2").value(),
"value2");
272 auto cache1 = std::make_unique<CacheType>(
273 cacheFile, 10, std::chrono::seconds(3));
275 auto cache2 = std::make_unique<CacheType>(
276 cacheFile, 10, std::chrono::seconds(3));
288 auto sharedCache = std::make_unique<CacheType>(
289 cacheFile, 10, std::chrono::seconds(10));
293 auto threadFunc = [&
b, numThreads](CacheType* cache,
int tNum) {
294 auto key1 = folly::to<string>(
"key", tNum);
295 auto nextNum = (tNum + 1) % numThreads;
296 auto key2 = folly::to<string>(
"key", nextNum);
298 for (
int i = 0;
i < 100; ++
i) {
306 cache->put(key1, tNum);
309 std::vector<std::thread>
threads;
310 for (
int i = 0;
i < numThreads; ++
i) {
311 threads.push_back(std::thread(threadFunc, sharedCache.get(),
i));
313 for (
int i = 0;
i < numThreads; ++
i) {
316 EXPECT_EQ(sharedCache->size(), numThreads);
317 for (
auto i = 0;
i < numThreads; ++
i) {
318 auto key = folly::to<string>(
"key",
i);
319 auto val = sharedCache->get(key);
folly::Future< bool > wait()
void testValidFile(const std::string &content, const vector< K > &keys, const vector< V > &values)
#define EXPECT_EQ(val1, val2)
TYPED_TEST(FilePersistentCacheTest, stringTypesGetPutTest)
folly::Optional< V > get(const K &key) override
std::vector< std::thread::id > threads
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
ssize_t writeFull(int fd, const void *buf, size_t count)
::testing::Types< std::mutex, folly::SharedMutex > MutexTypes
#define EXPECT_TRUE(condition)
int openNoInt(const char *name, int flags, mode_t mode)
FOLLY_CPP14_CONSTEXPR const Value & value() const &
void testInvalidFile(const std::string &content)
#define EXPECT_FALSE(condition)
std::string getPersistentCacheFilename()
TYPED_TEST_CASE(FilePersistentCacheTest, MutexTypes)
std::vector< int > values(1'000)
void put(const K &key, const V &val) override