rippled
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>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/protocol/Protocol.h>
7
8namespace ripple {
9
11{
12public:
13 void
14 run() override
15 {
16 using namespace std::chrono_literals;
17 TestStopwatch clock;
18 clock.set(0);
19
20 using Key = std::string;
21 using Cache = TaggedCache<Key, int, true>;
22
23 test::SuiteJournal j("KeyCacheTest", *this);
24
25 // Insert an item, retrieve it, and age it so it gets purged.
26 {
27 Cache c("test", LedgerIndex(1), 2s, clock, j);
28
29 BEAST_EXPECT(c.size() == 0);
30 BEAST_EXPECT(c.insert("one"));
31 BEAST_EXPECT(!c.insert("one"));
32 BEAST_EXPECT(c.size() == 1);
33 BEAST_EXPECT(c.touch_if_exists("one"));
34 ++clock;
35 c.sweep();
36 BEAST_EXPECT(c.size() == 1);
37 ++clock;
38 c.sweep();
39 BEAST_EXPECT(c.size() == 0);
40 BEAST_EXPECT(!c.touch_if_exists("one"));
41 }
42
43 // Insert two items, have one expire
44 {
45 Cache c("test", LedgerIndex(2), 2s, clock, j);
46
47 BEAST_EXPECT(c.insert("one"));
48 BEAST_EXPECT(c.size() == 1);
49 BEAST_EXPECT(c.insert("two"));
50 BEAST_EXPECT(c.size() == 2);
51 ++clock;
52 c.sweep();
53 BEAST_EXPECT(c.size() == 2);
54 BEAST_EXPECT(c.touch_if_exists("two"));
55 ++clock;
56 c.sweep();
57 BEAST_EXPECT(c.size() == 1);
58 }
59
60 // Insert three items (1 over limit), sweep
61 {
62 Cache c("test", LedgerIndex(2), 3s, clock, j);
63
64 BEAST_EXPECT(c.insert("one"));
65 ++clock;
66 BEAST_EXPECT(c.insert("two"));
67 ++clock;
68 BEAST_EXPECT(c.insert("three"));
69 ++clock;
70 BEAST_EXPECT(c.size() == 3);
71 c.sweep();
72 BEAST_EXPECT(c.size() < 3);
73 }
74 }
75};
76
77BEAST_DEFINE_TESTSUITE(KeyCache, basics, ripple);
78
79} // namespace ripple
A testsuite class.
Definition suite.h:52
void run() override
Runs the suite.
Map/cache combination.
Definition TaggedCache.h:43
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:120