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 <string>
11
12namespace xrpl {
13
14class KeyCacheTest : public ::testing::Test
15{
16public:
17};
18
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
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
A generic endpoint for log messages.
Definition Journal.h:44
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:370
TEST_F(HardenedHashTest, user_types)
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:95