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 <
string
>
11
12
namespace
xrpl
{
13
14
class
KeyCacheTest
:
public
::testing::Test
15
{
16
public
:
17
};
18
19
TEST_F
(
KeyCacheTest
, key_cache)
20
{
21
using namespace
std::chrono_literals;
22
TestStopwatch
clock;
23
clock.set(0);
24
25
using
Key =
std::string
;
26
using
Cache =
TaggedCache<Key, int, true>
;
27
28
beast::Journal
const
j{
TestSink::instance
()};
29
30
// Insert an item, retrieve it, and age it so it gets purged.
31
{
32
Cache c(
"test"
,
LedgerIndex
(1), 2s, clock, j);
33
34
EXPECT_EQ(c.size(), 0);
35
EXPECT_TRUE(c.insert(
"one"
));
36
EXPECT_FALSE(c.insert(
"one"
));
37
EXPECT_EQ(c.size(), 1);
38
EXPECT_TRUE(c.touchIfExists(
"one"
));
39
++clock;
40
c.sweep();
41
EXPECT_EQ(c.size(), 1);
42
++clock;
43
c.sweep();
44
EXPECT_EQ(c.size(), 0);
45
EXPECT_FALSE(c.touchIfExists(
"one"
));
46
}
47
48
// Insert two items, have one expire
49
{
50
Cache c(
"test"
,
LedgerIndex
(2), 2s, clock, j);
51
52
EXPECT_TRUE(c.insert(
"one"
));
53
EXPECT_EQ(c.size(), 1);
54
EXPECT_TRUE(c.insert(
"two"
));
55
EXPECT_EQ(c.size(), 2);
56
++clock;
57
c.sweep();
58
EXPECT_EQ(c.size(), 2);
59
EXPECT_TRUE(c.touchIfExists(
"two"
));
60
++clock;
61
c.sweep();
62
EXPECT_EQ(c.size(), 1);
63
}
64
65
// Insert three items (1 over limit), sweep
66
{
67
Cache c(
"test"
,
LedgerIndex
(2), 3s, clock, j);
68
69
EXPECT_TRUE(c.insert(
"one"
));
70
++clock;
71
EXPECT_TRUE(c.insert(
"two"
));
72
++clock;
73
EXPECT_TRUE(c.insert(
"three"
));
74
++clock;
75
EXPECT_EQ(c.size(), 3);
76
c.sweep();
77
EXPECT_LT(c.size(), 3);
78
}
79
}
80
81
}
// namespace xrpl
std::string
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::KeyCacheTest
Definition
KeyCache.cpp:15
xrpl::TaggedCache
Map/cache combination.
Definition
TaggedCache.h:67
xrpl::TestSink::instance
static TestSink & instance()
Definition
TestSink.h:12
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:382
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