1- #include < userver/multi-index-lru/container.hpp>
21#include < userver/multi-index-lru/expirable_container.hpp>
32#include < userver/utils/async.hpp>
4- #include < userver/engine/run_standalone.hpp>
53#include < userver/engine/task/task_with_result.hpp>
4+ #include < userver/utest/utest.hpp>
65
76#include < string>
87
9- #include < gtest/gtest.h>
108#include < boost/multi_index/hashed_index.hpp>
119#include < boost/multi_index/member.hpp>
1210#include < boost/multi_index/ordered_index.hpp>
@@ -47,8 +45,7 @@ class ExpirableUsersTest : public ::testing::Test {
4745 boost::multi_index::member<User, std::string, &User::name>>>>;
4846};
4947
50- TEST_F (ExpirableUsersTest, BasicOperations) {
51- userver::engine::RunStandalone ([&] {
48+ UTEST_F (ExpirableUsersTest, BasicOperations) {
5249 UserCacheExpirable cache (3 , std::chrono::seconds (10 )); // capacity=3, TTL=10s
5350
5451 // Test insertion
@@ -60,34 +57,32 @@ TEST_F(ExpirableUsersTest, BasicOperations) {
6057 EXPECT_EQ (cache.capacity (), 3 );
6158 EXPECT_FALSE (cache.empty ());
6259
63- // Test find by id
64- auto by_id = cache.find <IdTag>(1 );
65- ASSERT_NE (by_id, cache. end <IdTag> ());
60+ // Test get by id
61+ auto by_id = cache.get <IdTag>(1 );
62+ EXPECT_TRUE (by_id. has_value ());
6663 EXPECT_EQ (by_id->name , " Alice" );
6764
68- // Test find by email
69- auto by_email = cache.find <EmailTag>(" bob@test.com" );
70- ASSERT_NE (by_email, cache. end <EmailTag> ());
65+ // Test get by email
66+ auto by_email = cache.get <EmailTag>(" bob@test.com" );
67+ EXPECT_TRUE (by_email. has_value ());
7168 EXPECT_EQ (by_email->id , 2 );
7269
73- // Test find by name
74- auto by_name = cache.find <NameTag>(" Charlie" );
75- ASSERT_NE (by_name, cache. end <NameTag> ());
70+ // Test get by name
71+ auto by_name = cache.get <NameTag>(" Charlie" );
72+ EXPECT_TRUE (by_name. has_value ());
7673 EXPECT_EQ (by_name->email , " charlie@test.com" );
77- });
7874}
7975
80- TEST_F (ExpirableUsersTest, LRUEviction) {
81- userver::engine::RunStandalone ([&] {
76+ UTEST_F (ExpirableUsersTest, LRUEviction) {
8277 UserCacheExpirable cache (3 , std::chrono::seconds (10 ));
8378
8479 cache.insert (User{1 , " alice@test.com" , " Alice" });
8580 cache.insert (User{2 , " bob@test.com" , " Bob" });
8681 cache.insert (User{3 , " charlie@test.com" , " Charlie" });
8782
8883 // Access Alice and Charlie to make them recently used
89- cache.find <IdTag>(1 );
90- cache.find <IdTag>(3 );
84+ cache.get <IdTag>(1 );
85+ cache.get <IdTag>(3 );
9186
9287 // Add fourth element - Bob should be evicted (LRU)
9388 cache.insert (User{4 , " david@test.com" , " David" });
@@ -97,11 +92,9 @@ TEST_F(ExpirableUsersTest, LRUEviction) {
9792 EXPECT_TRUE (cache.contains <IdTag>(3 )); // Charlie remains
9893 EXPECT_TRUE (cache.contains <IdTag>(4 )); // David added
9994 EXPECT_EQ (cache.size (), 3 );
100- });
10195}
10296
103- TEST_F (ExpirableUsersTest, TTLExpiration) {
104- userver::engine::RunStandalone ([&] {
97+ UTEST_F (ExpirableUsersTest, TTLExpiration) {
10598 using namespace std ::chrono_literals;
10699
107100 UserCacheExpirable cache (100 , 100ms); // Very short TTL for testing
@@ -120,11 +113,10 @@ TEST_F(ExpirableUsersTest, TTLExpiration) {
120113 EXPECT_FALSE (cache.contains <IdTag>(1 ));
121114 EXPECT_FALSE (cache.contains <IdTag>(2 ));
122115 EXPECT_EQ (cache.size (), 0 );
123- });
124116}
125117
126- TEST_F (ExpirableUsersTest, TTLRefreshOnAccess) {
127- userver::engine::RunStandalone ([&] {
118+ UTEST_F (ExpirableUsersTest, TTLRefreshOnAccess) {
119+
128120 using namespace std ::chrono_literals;
129121
130122 UserCacheExpirable cache (100 , 190ms);
@@ -144,11 +136,10 @@ TEST_F(ExpirableUsersTest, TTLRefreshOnAccess) {
144136 // Wait for full TTL from last access
145137 std::this_thread::sleep_for (200ms);
146138 EXPECT_FALSE (cache.contains <IdTag>(1 ));
147- });
148139}
149140
150- TEST_F (ExpirableUsersTest, EraseOperations) {
151- userver::engine::RunStandalone ([&] {
141+ UTEST_F (ExpirableUsersTest, EraseOperations) {
142+
152143 UserCacheExpirable cache (3 , std::chrono::seconds (10 ));
153144
154145 cache.insert (User{1 , " alice@test.com" , " Alice" });
@@ -161,11 +152,10 @@ TEST_F(ExpirableUsersTest, EraseOperations) {
161152
162153 EXPECT_FALSE (cache.erase <IdTag>(999 )); // Non-existent
163154 EXPECT_EQ (cache.size (), 1 );
164- });
165155}
166156
167- TEST_F (ExpirableUsersTest, SetCapacity) {
168- userver::engine::RunStandalone ([&] {
157+ UTEST_F (ExpirableUsersTest, SetCapacity) {
158+
169159 UserCacheExpirable cache (5 , std::chrono::seconds (10 ));
170160
171161 // Fill cache
@@ -181,11 +171,10 @@ TEST_F(ExpirableUsersTest, SetCapacity) {
181171
182172 // Size should be <= new capacity
183173 EXPECT_LE (cache.size (), 3 );
184- });
185174}
186175
187- TEST_F (ExpirableUsersTest, Clear) {
188- userver::engine::RunStandalone ([&] {
176+ UTEST_F (ExpirableUsersTest, Clear) {
177+
189178 UserCacheExpirable cache (5 , std::chrono::seconds (10 ));
190179
191180 cache.insert (User{1 , " alice@test.com" , " Alice" });
@@ -200,11 +189,10 @@ TEST_F(ExpirableUsersTest, Clear) {
200189 EXPECT_TRUE (cache.empty ());
201190 EXPECT_FALSE (cache.contains <IdTag>(1 ));
202191 EXPECT_FALSE (cache.contains <IdTag>(2 ));
203- });
204192}
205193
206- TEST_F (ExpirableUsersTest, ThreadSafetyBasic) {
207- userver::engine::RunStandalone ([&] {
194+ UTEST_F (ExpirableUsersTest, ThreadSafetyBasic) {
195+
208196 UserCacheExpirable cache (100 , std::chrono::seconds (10 ));
209197
210198 constexpr int kCoroutines = 4 ;
@@ -220,7 +208,7 @@ TEST_F(ExpirableUsersTest, ThreadSafetyBasic) {
220208 cache.insert (User{id, std::to_string (id) + " @test.com" , " User" + std::to_string (id)});
221209
222210 if (id % 3 == 0 ) {
223- cache.find <IdTag>(id);
211+ cache.get <IdTag>(id);
224212 cache.contains <IdTag>(id);
225213 }
226214
@@ -236,7 +224,6 @@ TEST_F(ExpirableUsersTest, ThreadSafetyBasic) {
236224 }
237225
238226 EXPECT_LE (cache.size (), 100 );
239- });
240227}
241228} // namespace
242229
0 commit comments