rippled
Loading...
Searching...
No Matches
TaggedCache_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
10/*
11I guess you can put some items in, make sure they're still there. Let some
12time pass, make sure they're gone. Keep a strong pointer to one of them, make
13sure you can still find it even after time passes. Create two objects with
14the same key, canonicalize them both and make sure you get the same object.
15Put an object in but keep a strong pointer to it, advance the clock a lot,
16then canonicalize a new object with the same key, make sure you get the
17original object.
18*/
19
21{
22public:
23 void
24 run() override
25 {
26 using namespace std::chrono_literals;
27 using namespace beast::severities;
28 test::SuiteJournal journal("TaggedCache_test", *this);
29
30 TestStopwatch clock;
31 clock.set(0);
32
33 using Key = LedgerIndex;
34 using Value = std::string;
35 using Cache = TaggedCache<Key, Value>;
36
37 Cache c("test", 1, 1s, clock, journal);
38
39 // Insert an item, retrieve it, and age it so it gets purged.
40 {
41 BEAST_EXPECT(c.getCacheSize() == 0);
42 BEAST_EXPECT(c.getTrackSize() == 0);
43 BEAST_EXPECT(!c.insert(1, "one"));
44 BEAST_EXPECT(c.getCacheSize() == 1);
45 BEAST_EXPECT(c.getTrackSize() == 1);
46
47 {
49 BEAST_EXPECT(c.retrieve(1, s));
50 BEAST_EXPECT(s == "one");
51 }
52
53 ++clock;
54 c.sweep();
55 BEAST_EXPECT(c.getCacheSize() == 0);
56 BEAST_EXPECT(c.getTrackSize() == 0);
57 }
58
59 // Insert an item, maintain a strong pointer, age it, and
60 // verify that the entry still exists.
61 {
62 BEAST_EXPECT(!c.insert(2, "two"));
63 BEAST_EXPECT(c.getCacheSize() == 1);
64 BEAST_EXPECT(c.getTrackSize() == 1);
65
66 {
67 auto p = c.fetch(2);
68 BEAST_EXPECT(p != nullptr);
69 ++clock;
70 c.sweep();
71 BEAST_EXPECT(c.getCacheSize() == 0);
72 BEAST_EXPECT(c.getTrackSize() == 1);
73 }
74
75 // Make sure its gone now that our reference is gone
76 ++clock;
77 c.sweep();
78 BEAST_EXPECT(c.getCacheSize() == 0);
79 BEAST_EXPECT(c.getTrackSize() == 0);
80 }
81
82 // Insert the same key/value pair and make sure we get the same result
83 {
84 BEAST_EXPECT(!c.insert(3, "three"));
85
86 {
87 auto const p1 = c.fetch(3);
88 auto p2 = std::make_shared<Value>("three");
89 c.canonicalize_replace_client(3, p2);
90 BEAST_EXPECT(p1.get() == p2.get());
91 }
92 ++clock;
93 c.sweep();
94 BEAST_EXPECT(c.getCacheSize() == 0);
95 BEAST_EXPECT(c.getTrackSize() == 0);
96 }
97
98 // Put an object in but keep a strong pointer to it, advance the clock a
99 // lot, then canonicalize a new object with the same key, make sure you
100 // get the original object.
101 {
102 // Put an object in
103 BEAST_EXPECT(!c.insert(4, "four"));
104 BEAST_EXPECT(c.getCacheSize() == 1);
105 BEAST_EXPECT(c.getTrackSize() == 1);
106
107 {
108 // Keep a strong pointer to it
109 auto const p1 = c.fetch(4);
110 BEAST_EXPECT(p1 != nullptr);
111 BEAST_EXPECT(c.getCacheSize() == 1);
112 BEAST_EXPECT(c.getTrackSize() == 1);
113 // Advance the clock a lot
114 ++clock;
115 c.sweep();
116 BEAST_EXPECT(c.getCacheSize() == 0);
117 BEAST_EXPECT(c.getTrackSize() == 1);
118 // Canonicalize a new object with the same key
119 auto p2 = std::make_shared<std::string>("four");
120 BEAST_EXPECT(c.canonicalize_replace_client(4, p2));
121 BEAST_EXPECT(c.getCacheSize() == 1);
122 BEAST_EXPECT(c.getTrackSize() == 1);
123 // Make sure we get the original object
124 BEAST_EXPECT(p1.get() == p2.get());
125 }
126
127 ++clock;
128 c.sweep();
129 BEAST_EXPECT(c.getCacheSize() == 0);
130 BEAST_EXPECT(c.getTrackSize() == 0);
131 }
132 }
133};
134
135BEAST_DEFINE_TESTSUITE(TaggedCache, basics, ripple);
136
137} // namespace ripple
A testsuite class.
Definition suite.h:52
void run() override
Runs the suite.
Map/cache combination.
Definition TaggedCache.h:43
T is_same_v
A namespace for easy access to logging severity values.
Definition Journal.h:11
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