1#include <xrpl/resource/detail/Logic.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/basics/random.h>
6#include <xrpl/beast/insight/NullCollector.h>
7#include <xrpl/beast/net/IPAddressV4.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/resource/Charge.h>
10#include <xrpl/resource/Consumer.h>
11#include <xrpl/resource/Disposition.h>
12#include <xrpl/resource/Gossip.h>
13#include <xrpl/resource/detail/Tuning.h>
15#include <boost/utility/base_from_member.hpp>
17#include <gtest/gtest.h>
18#include <helpers/TestSink.h>
34 class TestLogic :
private boost::base_from_member<TestStopwatch>,
public Logic
37 using clock_type = boost::base_from_member<TestStopwatch>;
41 :
Logic(
beast::insight::NullCollector::make(), member, journal)
66 gossip.
items.reserve(n);
71 beast::ip::AddressV4::bytes_type
const d = {{
78 gossip.
items.push_back(std::move(item));
92 Consumer c{logic.newInboundEndpoint(addr)};
108 ASSERT_TRUE(warned) <<
"Loop count exceeded without warning";
111 bool dropped =
false;
124 ASSERT_TRUE(dropped) <<
"Loop count exceeded without dropping";
129 Consumer const c{logic.newInboundEndpoint(addr)};
130 logic.periodicActivity();
135 bool readmitted =
false;
137 using namespace std::chrono_literals;
144 logic.periodicActivity();
145 Consumer const c{logic.newInboundEndpoint(addr)};
153 EXPECT_TRUE(readmitted) <<
"Dropped Consumer left on blacklist too long";
162 Consumer c{logic.newUnlimitedEndpoint(addr)};
178 EXPECT_FALSE(warned) <<
"Should loop forever with no warning";
183 static constexpr auto kDecayTicks = 128uz;
189 Consumer c{logic.newInboundEndpoint(address)};
191 JLOG(j_.info()) <<
"Charging " << c.
toString() <<
" " << fee <<
" per second";
193 for (
auto tick = 0uz; tick < kDecayTicks; ++tick)
195 JLOG(j_.info()) <<
"Time= " << logic.clock().now().time_since_epoch().count()
196 <<
", Balance = " << c.
balance();
203 Consumer c{logic.newInboundEndpoint(address)};
205 JLOG(j_.info()) <<
"Charging " << c.
toString() <<
" " << fee <<
" per second";
206 for (
auto tick = 0uz; tick < kDecayTicks; ++tick)
209 JLOG(j_.info()) <<
"Time= " << logic.clock().now().time_since_epoch().count()
210 <<
", Balance = " << c.
balance();
220 static constexpr auto kGossipSources = 5uz;
233 beast::ip::AddressV4::bytes_type
const d = {{
240 g.
items.push_back(std::move(item));
242 logic.importConsumers(
"g", g);
A generic endpoint for log messages.
A version-independent IP address and port combination.
static Endpoint fromString(std::string const &s)
static TestSink & instance()
An endpoint that consumes resources.
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
int balance()
Returns the credit balance representing consumption.
std::string toString() const
Return a human readable string uniquely identifying this consumer.
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Disposition disposition() const
Returns the current disposition of this consumer.
Logic(beast::insight::Collector::ptr const &collector, clock_type &clock, beast::Journal journal)
boost::base_from_member< TestStopwatch > clock_type
TestLogic(beast::Journal journal)
static Gossip makeGossip()
boost::asio::ip::address_v4 AddressV4
static constexpr std::chrono::seconds kSecondsUntilExpiration
@ Warn
Consumer should be disconnected for excess consumption.
static constexpr auto kDropThreshold
TEST_F(ResourceManagerTest, limited_warn_drop)
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Describes a single consumer.
beast::ip::Endpoint address
Data format for exchanging consumption information across peers.
std::vector< Item > items