rippled
Loading...
Searching...
No Matches
Livecache_test.cpp
1#include <test/beast/IPEndpointCommon.h>
2#include <test/unit_test/SuiteJournal.h>
3
4#include <xrpld/peerfinder/detail/Livecache.h>
5
6#include <xrpl/basics/chrono.h>
7#include <xrpl/beast/clock/manual_clock.h>
8#include <xrpl/beast/unit_test.h>
9
10#include <boost/algorithm/string.hpp>
11
12namespace ripple {
13namespace PeerFinder {
14
15bool
16operator==(Endpoint const& a, Endpoint const& b)
17{
18 return (a.hops == b.hops && a.address == b.address);
19}
20
22{
25
26public:
27 Livecache_test() : journal_("Livecache_test", *this)
28 {
29 }
30
31 // Add the address as an endpoint
32 template <class C>
33 inline void
35 {
36 Endpoint cep{ep, hops};
37 c.insert(cep);
38 }
39
40 void
42 {
43 testcase("Basic Insert");
45 BEAST_EXPECT(c.empty());
46
47 for (auto i = 0; i < 10; ++i)
48 add(beast::IP::randomEP(true), c);
49
50 BEAST_EXPECT(!c.empty());
51 BEAST_EXPECT(c.size() == 10);
52
53 for (auto i = 0; i < 10; ++i)
54 add(beast::IP::randomEP(false), c);
55
56 BEAST_EXPECT(!c.empty());
57 BEAST_EXPECT(c.size() == 20);
58 }
59
60 void
62 {
63 testcase("Insert/Update");
65
66 auto ep1 = Endpoint{beast::IP::randomEP(), 2};
67 c.insert(ep1);
68 BEAST_EXPECT(c.size() == 1);
69 // third position list will contain the entry
70 BEAST_EXPECT((c.hops.begin() + 2)->begin()->hops == 2);
71
72 auto ep2 = Endpoint{ep1.address, 4};
73 // this will not change the entry has higher hops
74 c.insert(ep2);
75 BEAST_EXPECT(c.size() == 1);
76 // still in third position list
77 BEAST_EXPECT((c.hops.begin() + 2)->begin()->hops == 2);
78
79 auto ep3 = Endpoint{ep1.address, 2};
80 // this will not change the entry has the same hops as existing
81 c.insert(ep3);
82 BEAST_EXPECT(c.size() == 1);
83 // still in third position list
84 BEAST_EXPECT((c.hops.begin() + 2)->begin()->hops == 2);
85
86 auto ep4 = Endpoint{ep1.address, 1};
87 c.insert(ep4);
88 BEAST_EXPECT(c.size() == 1);
89 // now at second position list
90 BEAST_EXPECT((c.hops.begin() + 1)->begin()->hops == 1);
91 }
92
93 void
95 {
96 testcase("Expire");
97 using namespace std::chrono_literals;
99
100 auto ep1 = Endpoint{beast::IP::randomEP(), 1};
101 c.insert(ep1);
102 BEAST_EXPECT(c.size() == 1);
103 c.expire();
104 BEAST_EXPECT(c.size() == 1);
105 // verify that advancing to 1 sec before expiration
106 // leaves our entry intact
108 c.expire();
109 BEAST_EXPECT(c.size() == 1);
110 // now advance to the point of expiration
111 clock_.advance(1s);
112 c.expire();
113 BEAST_EXPECT(c.empty());
114 }
115
116 void
118 {
119 testcase("Histogram");
120 constexpr auto num_eps = 40;
122 for (auto i = 0; i < num_eps; ++i)
124 c,
125 ripple::rand_int<std::uint32_t>());
126 auto h = c.hops.histogram();
127 if (!BEAST_EXPECT(!h.empty()))
128 return;
130 boost::split(v, h, boost::algorithm::is_any_of(","));
131 auto sum = 0;
132 for (auto const& n : v)
133 {
134 auto val = boost::lexical_cast<int>(boost::trim_copy(n));
135 sum += val;
136 BEAST_EXPECT(val >= 0);
137 }
138 BEAST_EXPECT(sum == num_eps);
139 }
140
141 void
143 {
144 testcase("Shuffle");
146 for (auto i = 0; i < 100; ++i)
148 c,
150
153
154 auto cmp_EP = [](Endpoint const& a, Endpoint const& b) {
155 return (
156 b.hops < a.hops || (b.hops == a.hops && b.address < a.address));
157 };
158 all_hops before;
159 all_hops before_sorted;
160 for (auto i = std::make_pair(0, c.hops.begin());
161 i.second != c.hops.end();
162 ++i.first, ++i.second)
163 {
164 std::copy(
165 (*i.second).begin(),
166 (*i.second).end(),
167 std::back_inserter(before[i.first]));
168 std::copy(
169 (*i.second).begin(),
170 (*i.second).end(),
171 std::back_inserter(before_sorted[i.first]));
172 std::sort(
173 before_sorted[i.first].begin(),
174 before_sorted[i.first].end(),
175 cmp_EP);
176 }
177
178 c.hops.shuffle();
179
180 all_hops after;
181 all_hops after_sorted;
182 for (auto i = std::make_pair(0, c.hops.begin());
183 i.second != c.hops.end();
184 ++i.first, ++i.second)
185 {
186 std::copy(
187 (*i.second).begin(),
188 (*i.second).end(),
189 std::back_inserter(after[i.first]));
190 std::copy(
191 (*i.second).begin(),
192 (*i.second).end(),
193 std::back_inserter(after_sorted[i.first]));
194 std::sort(
195 after_sorted[i.first].begin(),
196 after_sorted[i.first].end(),
197 cmp_EP);
198 }
199
200 // each hop bucket should contain the same items
201 // before and after sort, albeit in different order
202 bool all_match = true;
203 for (auto i = 0; i < before.size(); ++i)
204 {
205 BEAST_EXPECT(before[i].size() == after[i].size());
206 all_match = all_match && (before[i] == after[i]);
207 BEAST_EXPECT(before_sorted[i] == after_sorted[i]);
208 }
209 BEAST_EXPECT(!all_match);
210 }
211
212 void
213 run() override
214 {
217 testExpire();
219 testShuffle();
220 }
221};
222
223BEAST_DEFINE_TESTSUITE(Livecache, peerfinder, ripple);
224
225} // namespace PeerFinder
226} // namespace ripple
T back_inserter(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
void advance(std::chrono::duration< Rep, Period > const &elapsed)
Advance the clock by a duration.
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
void shuffle()
Shuffle each hop list.
Definition Livecache.h:480
void run() override
Runs the suite.
void add(beast::IP::Endpoint ep, C &c, std::uint32_t hops=0)
The Livecache holds the short-lived relayed Endpoint messages.
Definition Livecache.h:179
cache_type::size_type size() const
Returns the number of entries in the cache.
Definition Livecache.h:357
void expire()
Erase entries whose time has expired.
Definition Livecache.h:388
void insert(Endpoint const &ep)
Creates or updates an existing Element based on a new message.
Definition Livecache.h:410
class ripple::PeerFinder::Livecache::hops_t hops
bool empty() const
Returns true if the cache is empty.
Definition Livecache.h:350
T copy(T... args)
T make_pair(T... args)
Endpoint randomEP(bool v4=true)
std::chrono::seconds constexpr liveCacheSecondsToLive(30)
bool operator==(Endpoint const &a, Endpoint const &b)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
static auto sum(TCollection const &col)
Definition BookStep.cpp:976
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:3247
T sort(T... args)
Describes a connectible peer address along with some metadata.