20 #include <ripple/basics/chrono.h>
21 #include <ripple/basics/safe_cast.h>
22 #include <ripple/beast/clock/manual_clock.h>
23 #include <ripple/beast/unit_test.h>
24 #include <ripple/peerfinder/impl/Livecache.h>
25 #include <boost/algorithm/string.hpp>
26 #include <test/beast/IPEndpointCommon.h>
27 #include <test/unit_test/SuiteJournal.h>
30 namespace PeerFinder {
60 testcase(
"Basic Insert");
62 BEAST_EXPECT(c.
empty());
64 for (
auto i = 0; i < 10; ++i)
67 BEAST_EXPECT(!c.
empty());
68 BEAST_EXPECT(c.
size() == 10);
70 for (
auto i = 0; i < 10; ++i)
73 BEAST_EXPECT(!c.
empty());
74 BEAST_EXPECT(c.
size() == 20);
80 testcase(
"Insert/Update");
85 BEAST_EXPECT(c.
size() == 1);
87 BEAST_EXPECT((c.
hops.
begin() + 2)->begin()->hops == 2);
92 BEAST_EXPECT(c.
size() == 1);
94 BEAST_EXPECT((c.
hops.
begin() + 2)->begin()->hops == 2);
99 BEAST_EXPECT(c.
size() == 1);
101 BEAST_EXPECT((c.
hops.
begin() + 2)->begin()->hops == 2);
105 BEAST_EXPECT(c.
size() == 1);
107 BEAST_EXPECT((c.
hops.
begin() + 1)->begin()->hops == 1);
114 using namespace std::chrono_literals;
119 BEAST_EXPECT(c.
size() == 1);
121 BEAST_EXPECT(c.
size() == 1);
126 BEAST_EXPECT(c.
size() == 1);
130 BEAST_EXPECT(c.
empty());
136 testcase(
"Histogram");
137 constexpr
auto num_eps = 40;
139 for (
auto i = 0; i < num_eps; ++i)
142 ripple::rand_int<std::uint32_t>());
144 if (!BEAST_EXPECT(!h.empty()))
147 boost::split(v, h, boost::algorithm::is_any_of(
","));
149 for (
auto const& n : v)
151 auto val = boost::lexical_cast<int>(boost::trim_copy(n));
153 BEAST_EXPECT(val >= 0);
155 BEAST_EXPECT(
sum == num_eps);
163 for (
auto i = 0; i < 100; ++i)
176 all_hops before_sorted;
179 ++i.first, ++i.second)
190 before_sorted[i.first].begin(),
191 before_sorted[i.first].end(),
198 all_hops after_sorted;
201 ++i.first, ++i.second)
212 after_sorted[i.first].begin(),
213 after_sorted[i.first].end(),
219 bool all_match =
true;
220 for (
auto i = 0; i < before.size(); ++i)
222 BEAST_EXPECT(before[i].size() ==
after[i].size());
223 all_match = all_match && (before[i] ==
after[i]);
224 BEAST_EXPECT(before_sorted[i] == after_sorted[i]);
226 BEAST_EXPECT(!all_match);
std::string histogram() const
void advance(std::chrono::duration< Rep, Period > const &elapsed)
Advance the clock by a duration.
void add(beast::IP::Endpoint ep, C &c, std::uint32_t hops=0)
T back_inserter(T... args)
constexpr std::chrono::seconds liveCacheSecondsToLive(30)
cache_type::size_type size() const
Returns the number of entries in the cache.
void insert(Endpoint const &ep)
Creates or updates an existing Element based on a new message.
std::enable_if_t< std::is_integral< Integral >::value &&detail::is_engine< Engine >::value, Integral > rand_int(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
void shuffle()
Shuffle each hop list.
The Livecache holds the short-lived relayed Endpoint messages.
class ripple::PeerFinder::Livecache::hops_t hops
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool operator==(Endpoint const &a, Endpoint const &b)
void expire()
Erase entries whose time has expired.
test::SuiteJournal journal_
BEAST_DEFINE_TESTSUITE(Livecache, peerfinder, ripple)
bool empty() const
Returns true if the cache is empty.
Endpoint randomEP(bool v4=true)
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
A version-independent IP address and port combination.
constexpr std::uint32_t maxHops
Describes a connectible peer address along with some metadata.
static auto sum(TCollection const &col)
beast::IP::Endpoint address