1#include <test/unit_test/SuiteJournal.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/unit_test/suite.h>
9#include <xrpl/beast/utility/Journal.h>
10#include <xrpl/resource/Charge.h>
11#include <xrpl/resource/Consumer.h>
12#include <xrpl/resource/Disposition.h>
13#include <xrpl/resource/Gossip.h>
14#include <xrpl/resource/detail/Logic.h>
15#include <xrpl/resource/detail/Tuning.h>
17#include <boost/utility/base_from_member.hpp>
29 class TestLogic :
private boost::base_from_member<TestStopwatch>,
public Logic
33 using clock_type = boost::base_from_member<TestStopwatch>;
37 :
Logic(
beast::insight::NullCollector::make(), member, journal)
61 gossip.
items.reserve(n);
66 beast::IP::AddressV4::bytes_type
const d = {
69 gossip.
items.push_back(item);
108 fail(
"Loop count exceeded without warning");
125 fail(
"Should loop forever with no warning");
139 fail(
"Loop count exceeded without dropping");
166 fail(
"Dropped consumer not put on blacklist");
177 bool readmitted =
false;
179 using namespace std::chrono_literals;
197 fail(
"Dropped Consumer left on blacklist too long");
212 for (
int i = 0; i < 5; ++i)
215 for (
int i = 0; i < 5; ++i)
231 beast::IP::AddressV4::bytes_type
const d = {{192, 0, 2, 1}};
233 g.
items.push_back(item);
251 JLOG(j.
info()) <<
"Charging " << c.
toString() <<
" " << fee <<
" per second";
253 for (
int i = 0; i < 128; ++i)
255 JLOG(j.
info()) <<
"Time= " << logic.
clock().
now().time_since_epoch().count()
256 <<
", Balance = " << c.
balance();
265 JLOG(j.
info()) <<
"Charging " << c.
toString() <<
" " << fee <<
" per second";
266 for (
int i = 0; i < 128; ++i)
269 JLOG(j.
info()) <<
"Time= " << logic.
clock().
now().time_since_epoch().count()
270 <<
", Balance = " << c.
balance();
A version-independent IP address and port combination.
static Endpoint fromString(std::string const &s)
A generic endpoint for log messages.
time_point now() const override
Returns the current time.
void pass()
Record a successful test condition.
void fail(String const &reason, char const *file, int line)
Record a failure.
TestcaseT testcase
Memberspace for declaring test cases.
An endpoint that consumes resources.
std::string toString() const
Return a human readable string uniquely identifying this consumer.
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Disposition disposition() const
Returns the current disposition of this consumer.
int balance()
Returns the credit balance representing consumption.
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Consumer newInboundEndpoint(beast::IP::Endpoint const &address)
Consumer newUnlimitedEndpoint(beast::IP::Endpoint const &address)
Create endpoint that should not have resource limits applied.
void importConsumers(std::string const &origin, Gossip const &gossip)
Logic(beast::insight::Collector::ptr const &collector, clock_type &clock, beast::Journal journal)
TestLogic(beast::Journal journal)
boost::base_from_member< TestStopwatch > clock_type
void testImport(beast::Journal j)
void testCharges(beast::Journal j)
void run() override
Runs the suite.
static void createGossip(Gossip &gossip)
void testImports(beast::Journal j)
void testDrop(beast::Journal j, bool limited)
boost::asio::ip::address_v4 AddressV4
Severity
Severity level / threshold of a Journal message.
@ Warn
Consumer should be disconnected for excess consumption.
static constexpr std::chrono::seconds kSecondsUntilExpiration
BEAST_DEFINE_TESTSUITE(ResourceManager, resource, xrpl)
static constexpr auto kDropThreshold
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::enable_if_t< std::is_integral_v< Integral >, Integral > randInt()
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