xrpld
Loading...
Searching...
No Matches
KeyCache_test.cpp
1#include <test/unit_test/SuiteJournal.h>
2
3#include <xrpl/basics/TaggedCache.h>
4#include <xrpl/basics/TaggedCache.ipp> // IWYU pragma: keep
5#include <xrpl/basics/chrono.h>
6#include <xrpl/beast/unit_test/suite.h>
7#include <xrpl/protocol/Protocol.h>
8
9#include <string>
10
11namespace xrpl {
12
14{
15public:
16 void
17 run() override
18 {
19 using namespace std::chrono_literals;
20 TestStopwatch clock;
21 clock.set(0);
22
23 using Key = std::string;
24 using Cache = TaggedCache<Key, int, true>;
25
26 test::SuiteJournal j("KeyCacheTest", *this);
27
28 // Insert an item, retrieve it, and age it so it gets purged.
29 {
30 Cache c("test", LedgerIndex(1), 2s, clock, j);
31
32 BEAST_EXPECT(c.size() == 0);
33 BEAST_EXPECT(c.insert("one"));
34 BEAST_EXPECT(!c.insert("one"));
35 BEAST_EXPECT(c.size() == 1);
36 BEAST_EXPECT(c.touchIfExists("one"));
37 ++clock;
38 c.sweep();
39 BEAST_EXPECT(c.size() == 1);
40 ++clock;
41 c.sweep();
42 BEAST_EXPECT(c.size() == 0);
43 BEAST_EXPECT(!c.touchIfExists("one"));
44 }
45
46 // Insert two items, have one expire
47 {
48 Cache c("test", LedgerIndex(2), 2s, clock, j);
49
50 BEAST_EXPECT(c.insert("one"));
51 BEAST_EXPECT(c.size() == 1);
52 BEAST_EXPECT(c.insert("two"));
53 BEAST_EXPECT(c.size() == 2);
54 ++clock;
55 c.sweep();
56 BEAST_EXPECT(c.size() == 2);
57 BEAST_EXPECT(c.touchIfExists("two"));
58 ++clock;
59 c.sweep();
60 BEAST_EXPECT(c.size() == 1);
61 }
62
63 // Insert three items (1 over limit), sweep
64 {
65 Cache c("test", LedgerIndex(2), 3s, clock, j);
66
67 BEAST_EXPECT(c.insert("one"));
68 ++clock;
69 BEAST_EXPECT(c.insert("two"));
70 ++clock;
71 BEAST_EXPECT(c.insert("three"));
72 ++clock;
73 BEAST_EXPECT(c.size() == 3);
74 c.sweep();
75 BEAST_EXPECT(c.size() < 3);
76 }
77 }
78};
79
81
82} // namespace xrpl
A testsuite class.
Definition suite.h:50
void run() override
Runs the suite.
Map/cache combination.
Definition TaggedCache.h:59
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:259
TaggedCache< uint256, int, true > KeyCache
Definition KeyCache.h:8
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:90
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)