1#include <userver/multi-index-lru/container.hpp>
2#include <userver/utest/utest.hpp>
6#include <boost/multi_index/hashed_index.hpp>
7#include <boost/multi_index/identity.hpp>
8#include <boost/multi_index/member.hpp>
9#include <boost/multi_index/ordered_index.hpp>
11USERVER_NAMESPACE_BEGIN
15class LRUUsersTest :
public ::testing::Test {
17 void SetUp() override {}
28 bool operator==(
const User& other)
const {
29 return id == other.id && email == other.email && name == other.name;
33 using UserCache = multi_index_lru::Container<
35 boost::multi_index::indexed_by<
36 boost::multi_index::ordered_unique<
37 boost::multi_index::tag<IdTag>,
38 boost::multi_index::member<User,
int, &User::id>>,
39 boost::multi_index::ordered_unique<
40 boost::multi_index::tag<EmailTag>,
41 boost::multi_index::member<User, std::string, &User::email>>,
42 boost::multi_index::ordered_non_unique<
43 boost::multi_index::tag<NameTag>,
44 boost::multi_index::member<User, std::string, &User::name>>>>;
47UTEST_F(LRUUsersTest, EraseByKeyReportsPresence) {
49 cache.insert({.id = 1, .email =
"a", .name =
"A"});
50 cache.insert({.id = 2, .email =
"b", .name =
"B"});
52 EXPECT_TRUE(cache.erase<IdTag>(2));
53 EXPECT_EQ(cache.size(), 1);
54 EXPECT_FALSE(cache.erase<IdTag>(2));
55 EXPECT_TRUE(cache.erase<IdTag>(1));
56 EXPECT_TRUE(cache.empty());
57 EXPECT_FALSE(cache.erase<IdTag>(1));
60UTEST_F(LRUUsersTest, BasicOperations) {
64 cache.emplace(User{.id = 1, .email =
"alice@test.com", .name =
"Alice"});
65 cache.emplace(User{.id = 2, .email =
"bob@test.com", .name =
"Bob"});
66 cache.emplace(User{.id = 3, .email =
"charlie@test.com", .name =
"Charlie"});
68 EXPECT_EQ(cache.size(), 3);
71 auto by_id = cache.find<IdTag,
int>(1);
72 EXPECT_NE(by_id, cache.end<IdTag>());
73 EXPECT_EQ(by_id->name,
"Alice");
76 auto by_email = cache.find<EmailTag, std::string>(
"bob@test.com");
77 EXPECT_NE(by_email, cache.end<EmailTag>());
78 EXPECT_EQ(by_email->id, 2);
81 auto by_name = cache.find<NameTag, std::string>(
"Charlie");
82 EXPECT_NE(by_name, cache.end<NameTag>());
83 EXPECT_EQ(by_name->email,
"charlie@test.com");
85 auto it = cache.find<EmailTag, std::string>(
"alice@test.com");
86 EXPECT_NE(it, cache.end<EmailTag>());
89UTEST_F(LRUUsersTest, LRUEviction) {
92 cache.emplace(User{.id = 1, .email =
"alice@test.com", .name =
"Alice"});
93 cache.emplace(User{.id = 2, .email =
"bob@test.com", .name =
"Bob"});
94 cache.emplace(User{.id = 3, .email =
"charlie@test.com", .name =
"Charlie"});
101 cache.emplace(User{.id = 4, .email =
"david@test.com", .name =
"David"});
103 EXPECT_FALSE((cache.contains<IdTag>(2)));
104 EXPECT_TRUE((cache.contains<IdTag>(1)));
105 EXPECT_TRUE((cache.contains<IdTag>(3)));
106 EXPECT_TRUE((cache.contains<IdTag>(4)));
109UTEST_F(LRUUsersTest, GetNoUpdateDoesNotChangeLru) {
112 cache.emplace(User{.id = 1, .email =
"alice@test.com", .name =
"Alice"});
113 cache.emplace(User{.id = 2, .email =
"bob@test.com", .name =
"Bob"});
114 cache.emplace(User{.id = 3, .email =
"charlie@test.com", .name =
"Charlie"});
116 auto it = cache.find_no_update<IdTag>(1);
117 EXPECT_NE(it, cache.end<IdTag>());
118 EXPECT_EQ(it->name,
"Alice");
120 cache.emplace(User{.id = 4, .email =
"david@test.com", .name =
"David"});
122 EXPECT_FALSE((cache.contains<IdTag>(1)));
123 EXPECT_TRUE((cache.contains<IdTag>(2)));
124 EXPECT_TRUE((cache.contains<IdTag>(3)));
125 EXPECT_TRUE((cache.contains<IdTag>(4)));
128UTEST_F(LRUUsersTest, EqualRangeUpdatesLruForAllMatches) {
131 cache.emplace(User{.id = 1, .email =
"john1@test.com", .name =
"John"});
132 cache.emplace(User{.id = 2, .email =
"john2@test.com", .name =
"John"});
133 cache.emplace(User{.id = 3, .email =
"alice@test.com", .name =
"Alice"});
134 cache.emplace(User{.id = 4, .email =
"bob@test.com", .name =
"Bob"});
136 auto [begin, end] = cache.equal_range<NameTag, std::string>(
"John");
138 for (
auto it = begin; it != end; ++it) {
143 cache.emplace(User{.id = 5, .email =
"eve@test.com", .name =
"Eve"});
145 EXPECT_TRUE((cache.contains<IdTag>(1)));
146 EXPECT_TRUE((cache.contains<IdTag>(2)));
147 EXPECT_FALSE((cache.contains<IdTag>(3)));
148 EXPECT_TRUE((cache.contains<IdTag>(4)));
149 EXPECT_TRUE((cache.contains<IdTag>(5)));
152UTEST_F(LRUUsersTest, EqualRangeNoUpdateDoesNotChangeLru) {
155 cache.emplace(User{.id = 1, .email =
"john1@test.com", .name =
"John"});
156 cache.emplace(User{.id = 2, .email =
"john2@test.com", .name =
"John"});
157 cache.emplace(User{.id = 3, .email =
"alice@test.com", .name =
"Alice"});
158 cache.emplace(User{.id = 4, .email =
"bob@test.com", .name =
"Bob"});
160 auto [begin, end] = cache.equal_range_no_update<NameTag, std::string>(
"John");
162 for (
auto it = begin; it != end; ++it) {
167 cache.emplace(User{.id = 5, .email =
"eve@test.com", .name =
"Eve"});
169 EXPECT_FALSE((cache.contains<IdTag>(1)));
170 EXPECT_TRUE((cache.contains<IdTag>(2)));
171 EXPECT_TRUE((cache.contains<IdTag>(3)));
172 EXPECT_TRUE((cache.contains<IdTag>(4)));
173 EXPECT_TRUE((cache.contains<IdTag>(5)));
176UTEST_F(LRUUsersTest, EqualRangeWorksWithEmptyRange) {
178 cache.emplace(User{.id = 1, .email =
"alice@test.com", .name =
"Alice"});
180 auto [begin, end] = cache.equal_range<NameTag, std::string>(
"Nonexistent");
181 EXPECT_EQ(begin, end);
184UTEST_F(LRUUsersTest, EqualRangeNoUpdateWorksWithEmptyRange) {
186 cache.emplace(User{.id = 1, .email =
"alice@test.com", .name =
"Alice"});
188 auto [begin, end] = cache.equal_range_no_update<NameTag, std::string>(
"Nonexistent");
189 EXPECT_EQ(begin, end);
192class ProductsTest :
public ::testing::Test {
202 bool operator==(
const Product& other)
const {
203 return sku == other.sku && name == other.name && price == other.price;
207 using ProductCache = multi_index_lru::Container<
209 boost::multi_index::indexed_by<
210 boost::multi_index::ordered_unique<
211 boost::multi_index::tag<SkuTag>,
212 boost::multi_index::member<Product, std::string, &Product::sku>>,
213 boost::multi_index::ordered_unique<
214 boost::multi_index::tag<NameTag>,
215 boost::multi_index::member<Product, std::string, &Product::name>>>>;
218UTEST_F(ProductsTest, BasicProductOperations) {
219 ProductCache cache(2);
221 cache.emplace(Product{.sku =
"A1", .name =
"Laptop", .price = 999.99});
222 cache.emplace(Product{.sku =
"A2", .name =
"Mouse", .price = 29.99});
224 auto laptop = cache.find<SkuTag, std::string>(
"A1");
225 EXPECT_NE(laptop, cache.end<SkuTag>());
226 EXPECT_EQ(laptop->name,
"Laptop");
229UTEST_F(ProductsTest, ProductEviction) {
230 ProductCache cache(2);
232 cache.emplace(Product{.sku =
"A1", .name =
"Laptop", .price = 999.99});
233 cache.emplace(Product{.sku =
"A2", .name =
"Mouse", .price = 29.99});
236 cache.find<SkuTag>(
"A1");
237 cache.emplace(Product{.sku =
"A3", .name =
"Keyboard", .price = 79.99});
239 EXPECT_TRUE((cache.contains<SkuTag, std::string>(
"A1")));
240 EXPECT_TRUE((cache.contains<SkuTag, std::string>(
"A3")));
241 EXPECT_FALSE((cache.contains<SkuTag, std::string>(
"A2")));
243 EXPECT_NE(cache.find<NameTag>(
"Keyboard"), cache.end<NameTag>());
244 EXPECT_EQ(cache.find<NameTag>(
"Mouse"), cache.end<NameTag>());
247class ProductsTestWithAllocator :
public ProductsTest {
251 static std::atomic<size_t> count;
252 static void increment() { count++; }
253 static size_t get() {
return count.load(); }
254 static void reset() { count = 0; }
257 template <
typename T>
258 class CountingAllocator :
public std::allocator<T> {
260 CountingAllocator() =
default;
261 template <
typename U>
262 CountingAllocator(
const CountingAllocator<U>&) {}
264 T* allocate(size_t n) {
265 Counter::increment();
266 return std::allocator<T>::allocate(n);
269 static size_t get_count() {
return Counter::get(); }
270 static void reset_count() { Counter::reset(); }
272 template <
typename U>
274 using other = CountingAllocator<U>;
278 using ProductCache = multi_index_lru::Container<
280 boost::multi_index::indexed_by<
281 boost::multi_index::ordered_unique<
282 boost::multi_index::tag<SkuTag>,
283 boost::multi_index::member<Product, std::string, &Product::sku>>,
284 boost::multi_index::ordered_unique<
285 boost::multi_index::tag<NameTag>,
286 boost::multi_index::member<Product, std::string, &Product::name>>>,
287 CountingAllocator<Product>>;
290std::atomic<size_t> ProductsTestWithAllocator::Counter::count{0};
292UTEST_F(ProductsTestWithAllocator, AllocationsCheck) {
293 ProductCache cache(20);
295 for (
int i = 0; i < 1000; ++i) {
296 cache.insert(Product{.sku =
"A" + std::to_string(i), .name =
"Laptop_" + std::to_string(i), .price = 999.99});
298 const auto first_allocations_count = ProductsTestWithAllocator::CountingAllocator<
int>::get_count();
301 for (
int i = 0; i < 1000; ++i) {
302 cache.insert(Product{.sku =
"A" + std::to_string(i), .name =
"Laptop_" + std::to_string(i), .price = 999.99});
306 EXPECT_EQ(first_allocations_count, ProductsTestWithAllocator::CountingAllocator<
int>::get_count());
308 cache.insert(Product{.sku =
"B_0", .name =
"C_0", .price = 999.99});
309 cache.erase(cache.find<NameTag>(
"C_0"));
310 cache.insert(Product{.sku =
"B_1", .name =
"C_1", .price = 999.99});
311 cache.erase(cache.find<SkuTag>(
"B_1"));
312 cache.insert(Product{.sku =
"B_2", .name =
"C_2", .price = 999.99});
315 EXPECT_EQ(first_allocations_count, ProductsTestWithAllocator::CountingAllocator<
int>::get_count());
318UTEST(Snippet, SimpleUsage) {
326 MyValueT my_value{.key =
"some_key", .val = 1};
328 using MyLruCache = multi_index_lru::Container<
330 boost::multi_index::indexed_by<boost::multi_index::hashed_unique<
331 boost::multi_index::tag<MyTag>,
332 boost::multi_index::member<MyValueT, std::string, &MyValueT::key>>>>;
334 MyLruCache cache(1000);
335 cache.insert(my_value);
336 auto it = cache.find<MyTag>(
"some_key");
337 EXPECT_NE(it, cache.end<MyTag>());