proxygen
TestUtil.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cstdlib>
19 #include <list>
20 #include <vector>
21 
22 #include <folly/Memory.h>
24 #include <gtest/gtest.h>
26 
27 namespace wangle {
28 
30 
31 template<typename K, typename V, typename MutexT = std::mutex>
33  const std::vector<K>& keys,
34  const std::vector<V>& values) {
36  typedef FilePersistentCache<K, V, MutexT> CacheType;
37  size_t cacheCapacity = 10;
38  {
39  CacheType cache(filename, cacheCapacity, std::chrono::seconds(150));
40  EXPECT_FALSE(cache.get(keys[0]).hasValue());
41  EXPECT_FALSE(cache.get(keys[1]).hasValue());
42  cache.put(keys[0], values[0]);
43  cache.put(keys[1], values[1]);
44  EXPECT_EQ(cache.size(), 2);
45  EXPECT_EQ(cache.get(keys[0]).value(), values[0]);
46  EXPECT_EQ(cache.get(keys[1]).value(), values[1]);
47  }
48  {
49  CacheType cache(filename, cacheCapacity, std::chrono::seconds(150));
50  EXPECT_EQ(cache.size(), 2);
51  EXPECT_EQ(cache.get(keys[0]).value(), values[0]);
52  EXPECT_EQ(cache.get(keys[1]).value(), values[1]);
53  EXPECT_TRUE(cache.remove(keys[1]));
54  EXPECT_FALSE(cache.remove(keys[1]));
55  EXPECT_EQ(cache.size(), 1);
56  EXPECT_EQ(cache.get(keys[0]).value(), values[0]);
57  EXPECT_FALSE(cache.get(keys[1]).hasValue());
58  }
59  {
60  CacheType cache(filename, cacheCapacity, std::chrono::seconds(150));
61  EXPECT_EQ(cache.size(), 1);
62  EXPECT_EQ(cache.get(keys[0]).value(), values[0]);
63  EXPECT_FALSE(cache.get(keys[1]).hasValue());
64  cache.clear();
65  EXPECT_EQ(cache.size(), 0);
66  EXPECT_FALSE(cache.get(keys[0]).hasValue());
67  EXPECT_FALSE(cache.get(keys[1]).hasValue());
68  }
69  {
70  CacheType cache(filename, cacheCapacity, std::chrono::seconds(150));
71  EXPECT_EQ(cache.size(), 0);
72  EXPECT_FALSE(cache.get(keys[0]).hasValue());
73  EXPECT_FALSE(cache.get(keys[1]).hasValue());
74  }
75  EXPECT_TRUE(unlink(filename.c_str()) != -1);
76 }
77 
78 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
std::string getPersistentCacheFilename()
Definition: TestUtil.cpp:22
void testSimplePutGet(const std::vector< K > &keys, const std::vector< V > &values)
Definition: TestUtil.h:32
std::vector< int > values(1'000)