xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
basics
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
13
namespace
xrpl
{
14
15
class
KeyCacheTest
:
public
::testing::Test
16
{
17
public
:
18
using
Cache
=
TaggedCache<std::string, int, true>
;
19
20
static
constexpr
std::chrono::seconds
kExpiration
{2};
21
22
protected
:
23
beast::Journal
const
j_
{
TestSink::instance
()};
24
TestStopwatch
clock_
;
25
};
26
27
TEST_F
(
KeyCacheTest
, key_cache)
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
36
beast::Journal
const
j{
TestSink::instance
()};
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
89
TEST_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
102
TEST_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
std::string
chrono
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::KeyCacheTest
Definition
KeyCache.cpp:16
xrpl::KeyCacheTest::j_
beast::Journal const j_
Definition
KeyCache.cpp:23
xrpl::KeyCacheTest::kExpiration
static constexpr std::chrono::seconds kExpiration
Definition
KeyCache.cpp:20
xrpl::KeyCacheTest::clock_
TestStopwatch clock_
ManualClock starts at zero.
Definition
KeyCache.cpp:24
xrpl::KeyCacheTest::Cache
TaggedCache< std::string, int, true > Cache
Definition
KeyCache.cpp:18
xrpl::TaggedCache
Map/cache combination.
Definition
TaggedCache.h:67
xrpl::TestSink::instance
static TestSink & instance()
Definition
TestSink.h:12
std::chrono::seconds
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::LedgerIndex
std::uint32_t LedgerIndex
A ledger index.
Definition
Protocol.h:409
xrpl::TEST_F
TEST_F(HardenedHashTest, user_types)
Definition
hardened_hash.cpp:213
xrpl::TestStopwatch
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition
chrono.h:95
string
Generated by
1.17.0