xrpld
Loading...
Searching...
No Matches
KeyCache.cpp
1#include <xrpl/basics/TaggedCache.h>
2#include <xrpl/basics/TaggedCache.ipp> // IWYU pragma: keep
3#include <xrpl/basics/chrono.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/protocol/Protocol.h>
6
7#include <gtest/gtest.h>
8#include <helpers/TestSink.h>
9
10#include <chrono>
11#include <string>
12
13namespace xrpl {
14
15class KeyCacheTest : public ::testing::Test
16{
17public:
19
20 static constexpr std::chrono::seconds kExpiration{2};
21
22protected:
25};
26
28{
29 using namespace std::chrono_literals;
30 TestStopwatch clock;
31 clock.set(0);
32
33 using Key = std::string;
34 using Cache = TaggedCache<Key, int, true>;
35
37
38 // Insert an item, retrieve it, and age it so it gets purged.
39 {
40 Cache c("test", LedgerIndex(1), 2s, clock, j);
41
42 EXPECT_EQ(c.size(), 0);
43 EXPECT_TRUE(c.insert("one"));
44 EXPECT_FALSE(c.insert("one"));
45 EXPECT_EQ(c.size(), 1);
46 EXPECT_TRUE(c.touchIfExists("one"));
47 ++clock;
48 c.sweep();
49 EXPECT_EQ(c.size(), 1);
50 ++clock;
51 c.sweep();
52 EXPECT_EQ(c.size(), 0);
53 EXPECT_FALSE(c.touchIfExists("one"));
54 }
55
56 // Insert two items, have one expire
57 {
58 Cache c("test", LedgerIndex(2), 2s, clock, j);
59
60 EXPECT_TRUE(c.insert("one"));
61 EXPECT_EQ(c.size(), 1);
62 EXPECT_TRUE(c.insert("two"));
63 EXPECT_EQ(c.size(), 2);
64 ++clock;
65 c.sweep();
66 EXPECT_EQ(c.size(), 2);
67 EXPECT_TRUE(c.touchIfExists("two"));
68 ++clock;
69 c.sweep();
70 EXPECT_EQ(c.size(), 1);
71 }
72
73 // Insert three items (1 over limit), sweep
74 {
75 Cache c("test", LedgerIndex(2), 3s, clock, j);
76
77 EXPECT_TRUE(c.insert("one"));
78 ++clock;
79 EXPECT_TRUE(c.insert("two"));
80 ++clock;
81 EXPECT_TRUE(c.insert("three"));
82 ++clock;
83 EXPECT_EQ(c.size(), 3);
84 c.sweep();
85 EXPECT_LT(c.size(), 3);
86 }
87}
88
89TEST_F(KeyCacheTest, touch_if_exists_counts_hits_and_misses)
90{
91 Cache c("test", LedgerIndex(1), kExpiration, clock_, j_);
92 EXPECT_FLOAT_EQ(c.getHitRate(), 0.0f);
93
94 EXPECT_TRUE(c.insert("one"));
95 EXPECT_TRUE(c.touchIfExists("one"));
96 EXPECT_FALSE(c.touchIfExists("two"));
97
98 // One hit and one miss, so the rate the reporters read is fifty percent.
99 EXPECT_FLOAT_EQ(c.getHitRate(), 50.0f);
100}
101
102TEST_F(KeyCacheTest, sweep_counts_removals)
103{
104 Cache c("test", LedgerIndex(2), kExpiration, clock_, j_);
105
106 EXPECT_TRUE(c.insert("one"));
107 EXPECT_TRUE(c.insert("two"));
108 EXPECT_EQ(c.getCacheSize(), 2);
109 EXPECT_EQ(c.getTrackSize(), 2);
110
111 // A repeat insert half way to expiry refreshes the entry and is not counted again.
112 clock_.advance(kExpiration / 2);
113 EXPECT_FALSE(c.insert("two"));
114 EXPECT_EQ(c.getCacheSize(), 2);
115
116 // At the expiry of "one", only "one" ages out: the refreshed "two" has half its age left, so
117 // the cache count follows the map down by one.
118 clock_.advance(kExpiration / 2);
119 c.sweep();
120 EXPECT_EQ(c.getCacheSize(), 1);
121 EXPECT_EQ(c.getTrackSize(), 1);
122
123 // Another half expiry later "two" ages out as well.
124 clock_.advance(kExpiration / 2);
125 c.sweep();
126 EXPECT_EQ(c.getCacheSize(), 0);
127 EXPECT_EQ(c.getTrackSize(), 0);
128}
129
130} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
beast::Journal const j_
Definition KeyCache.cpp:23
static constexpr std::chrono::seconds kExpiration
Definition KeyCache.cpp:20
TestStopwatch clock_
ManualClock starts at zero.
Definition KeyCache.cpp:24
TaggedCache< std::string, int, true > Cache
Definition KeyCache.cpp:18
Map/cache combination.
Definition TaggedCache.h:67
static TestSink & instance()
Definition TestSink.h:12
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:409
TEST_F(HardenedHashTest, user_types)
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:95