1#include <xrpl/peerfinder/detail/Livecache.h>
3#include <xrpl/basics/chrono.h>
4#include <xrpl/basics/random.h>
5#include <xrpl/beast/net/IPAddressV4.h>
6#include <xrpl/beast/net/IPAddressV6.h>
7#include <xrpl/beast/net/IPEndpoint.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/beast/utility/PropertyStream.h>
10#include <xrpl/json/JsonPropertyStream.h>
11#include <xrpl/json/json_forwards.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/peerfinder/Types.h>
14#include <xrpl/peerfinder/detail/Tuning.h>
16#include <boost/algorithm/string/classification.hpp>
17#include <boost/algorithm/string/split.hpp>
18#include <boost/algorithm/string/trim.hpp>
19#include <boost/lexical_cast.hpp>
21#include <gtest/gtest.h>
22#include <helpers/TestSink.h>
35class LivecacheTest :
public ::testing::Test
44 static beast::ip::Endpoint
45 endpoint(std::uint16_t index,
bool v4 =
true)
47 auto const port =
static_cast<std::uint16_t
>(10000 + index);
51 auto const bytes = beast::ip::AddressV4::bytes_type{
54 static_cast<std::uint8_t
>((index / 256) % 256),
55 static_cast<std::uint8_t
>(index % 256),
62 auto const bytes = beast::ip::AddressV6::bytes_type{
77 static_cast<std::uint8_t
>((index / 256) % 256),
78 static_cast<std::uint8_t
>(index % 256),
86 addEndpoint(beast::ip::Endpoint
const& ep, std::uint32_t hops = 0)
88 cache_.insert(Endpoint{ep, hops});
92 Livecache<> cache_{clock_, journal()};
99 EXPECT_TRUE(cache_.empty());
101 for (
auto i = 0; i < 10; ++i)
102 addEndpoint(endpoint(i,
true));
104 EXPECT_FALSE(cache_.empty());
105 EXPECT_EQ(cache_.size(), 10u);
107 for (
auto i = 10; i < 20; ++i)
108 addEndpoint(endpoint(i,
false));
110 EXPECT_FALSE(cache_.empty());
111 EXPECT_EQ(cache_.size(), 20u);
114TEST_F(LivecacheTest, insert_update_keeps_lowest_hop_count)
116 auto const ep1 =
Endpoint{endpoint(1), 2};
118 ASSERT_EQ(cache_.size(), 1u);
119 EXPECT_EQ((cache_.hops.begin() + 2)->begin()->hops, 2u);
121 auto const ep2 =
Endpoint{ep1.address, 4};
123 EXPECT_EQ(cache_.size(), 1u);
124 EXPECT_EQ((cache_.hops.begin() + 2)->begin()->hops, 2u);
126 auto const ep3 =
Endpoint{ep1.address, 2};
128 EXPECT_EQ(cache_.size(), 1u);
129 EXPECT_EQ((cache_.hops.begin() + 2)->begin()->hops, 2u);
131 auto const ep4 =
Endpoint{ep1.address, 1};
133 EXPECT_EQ(cache_.size(), 1u);
134 EXPECT_EQ((cache_.hops.begin() + 1)->begin()->hops, 1u);
137TEST_F(LivecacheTest, hop_iterators_support_const_reverse_and_move_back)
139 auto const ep1 =
Endpoint{endpoint(1), 1};
140 auto const ep2 =
Endpoint{endpoint(2), 1};
144 auto hop = *(cache_.hops.begin() + 1);
145 ASSERT_NE(hop.begin(), hop.end());
146 ASSERT_NE(hop.cbegin(), hop.cend());
147 ASSERT_NE(hop.rbegin(), hop.rend());
148 ASSERT_NE(hop.crbegin(), hop.crend());
150 auto const firstAddress = hop.begin()->address;
151 hop.moveBack(hop.begin());
152 EXPECT_EQ(hop.rbegin()->address, firstAddress);
154 auto const& constHops = cache_.hops;
155 EXPECT_NE(constHops.begin(), constHops.end());
156 EXPECT_NE(constHops.cbegin(), constHops.cend());
157 EXPECT_NE(constHops.rbegin(), constHops.rend());
158 EXPECT_NE(constHops.crbegin(), constHops.crend());
160 auto const constHop = *(constHops.cbegin() + 1);
161 EXPECT_EQ(
std::distance(constHop.begin(), constHop.end()), 2);
162 EXPECT_EQ(
std::distance(constHop.cbegin(), constHop.cend()), 2);
163 EXPECT_EQ(
std::distance(constHop.rbegin(), constHop.rend()), 2);
164 EXPECT_EQ(
std::distance(constHop.crbegin(), constHop.crend()), 2);
167TEST_F(LivecacheTest, on_write_reports_entries_and_expiration)
169 cache_.insert(
Endpoint{endpoint(1), 1});
178 auto const& top = stream.top();
179 EXPECT_EQ(top[
"size"].asUInt(), 2u);
180 EXPECT_FALSE(top[
"hist"].asString().empty());
181 ASSERT_TRUE(top.isMember(
"entries"));
182 ASSERT_EQ(top[
"entries"].size(), 2u);
183 auto const& entry = top[
"entries"][
json::UInt{0}];
184 EXPECT_TRUE(entry.
isMember(
"hops"));
185 EXPECT_TRUE(entry.
isMember(
"address"));
186 EXPECT_TRUE(entry.
isMember(
"expires"));
189TEST_F(LivecacheTest, expire_removes_entries_after_ttl)
191 using namespace std::chrono_literals;
193 cache_.insert(
Endpoint{endpoint(1), 1});
194 ASSERT_EQ(cache_.size(), 1u);
197 EXPECT_EQ(cache_.size(), 1u);
201 EXPECT_EQ(cache_.size(), 1u);
205 EXPECT_TRUE(cache_.empty());
208TEST_F(LivecacheTest, expire_removes_multiple_entries_after_ttl)
210 using namespace std::chrono_literals;
212 cache_.insert(
Endpoint{endpoint(1), 1});
213 cache_.insert(
Endpoint{endpoint(2), 2});
217 EXPECT_TRUE(cache_.empty());
220TEST_F(LivecacheTest, histogram_counts_all_entries)
222 constexpr auto kNumEndpoints = 40;
224 for (
auto i = 0; i < kNumEndpoints; ++i)
229 auto const histogram = cache_.hops.histogram();
230 ASSERT_FALSE(histogram.empty());
233 boost::split(values, histogram, boost::algorithm::is_any_of(
","));
236 for (
auto const& value : values)
238 auto const count = boost::lexical_cast<int>(boost::trim_copy(value));
242 EXPECT_EQ(
sum, kNumEndpoints);
245TEST_F(LivecacheTest, shuffle_preserves_bucket_contents)
247 for (
auto i = 0; i < 100; ++i)
256 return rhs.hops < lhs.
hops || (rhs.hops == lhs.
hops && rhs.address < lhs.
address);
261 auto const sameEndpoints =
263 return lhs.size() == rhs.size() &&
264 std::equal(lhs.begin(), lhs.end(), rhs.begin(), sameEndpoint);
268 AllHops beforeSorted;
269 for (
auto i =
std::make_pair(0, cache_.hops.begin()); i.second != cache_.hops.end();
270 ++i.first, ++i.second)
277 cache_.hops.shuffle();
281 for (
auto i =
std::make_pair(0, cache_.hops.begin()); i.second != cache_.hops.end();
282 ++i.first, ++i.second)
289 auto allBucketsKeptOriginalOrder =
true;
290 for (
auto i = 0u; i < before.size(); ++i)
292 EXPECT_EQ(before[i].size(),
after[i].size());
293 allBucketsKeptOriginalOrder =
294 allBucketsKeptOriginalOrder && sameEndpoints(before[i],
after[i]);
295 EXPECT_TRUE(sameEndpoints(beforeSorted[i], afterSorted[i]));
297 EXPECT_FALSE(allBucketsKeptOriginalOrder);
T back_inserter(T... args)
Address const & address() const
Returns the address portion of this endpoint.
bool isMember(char const *key) const
Return true if the object has a member named key.
A PropertyStream::Sink which produces a json::Value of type ValueType::Object.
static TestSink & instance()
boost::asio::ip::address Address
boost::asio::ip::address_v6 AddressV6
boost::asio::ip::address_v4 AddressV4
constexpr std::uint32_t kMaxHops
constexpr std::chrono::seconds kLiveCacheSecondsToLive(30)
TEST_F(LivecacheTest, basic_insert)
static auto sum(TCollection const &col)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Integral randInt(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Describes a connectable peer address along with some metadata.
beast::ip::Endpoint address