xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
overlay
traffic_count_test.cpp
1
#include <xrpld/overlay/detail/TrafficCount.h>
2
3
#include <xrpl/beast/unit_test/suite.h>
4
5
#include <xrpl.pb.h>
6
7
#include <
algorithm
>
8
#include <
cstddef
>
9
#include <
cstdint
>
10
11
namespace
xrpl::test
{
12
13
class
traffic_count_test
:
public
beast::unit_test::Suite
14
{
15
public
:
16
traffic_count_test
() =
default
;
17
18
void
19
testCategorize
()
20
{
21
testcase
(
"categorize"
);
22
protocol::TMPing message;
23
message.set_type(protocol::TMPing::ptPING);
24
25
// a known message is categorized to a proper category
26
auto
const
known =
TrafficCount::categorize
(message, protocol::mtPING,
false
);
27
BEAST_EXPECT(known ==
TrafficCount::Category::Base
);
28
29
// cluster messages have a category of their own, in both directions; the same lookup
30
// serves outbound traffic, so a missing entry would hide them from a node's report
31
// whichever way they traveled
32
BEAST_EXPECT(
33
TrafficCount::categorize
(message, protocol::mtCLUSTER,
true
) ==
34
TrafficCount::Category::Cluster
);
35
BEAST_EXPECT(
36
TrafficCount::categorize
(message, protocol::mtCLUSTER,
false
) ==
37
TrafficCount::Category::Cluster
);
38
39
// an unknown message type is categorized as unknown
40
auto
const
unknown =
41
TrafficCount::categorize
(message,
static_cast<
protocol::MessageType
>
(99),
false
);
42
BEAST_EXPECT(unknown ==
TrafficCount::Category::Unknown
);
43
}
44
45
void
46
testAttribute
()
47
{
48
testcase
(
"attribute"
);
49
50
auto
const
outsider =
TrafficCount::IsFromCluster::No
;
51
auto
const
member =
TrafficCount::IsFromCluster::Yes
;
52
53
// Cluster is read as traffic between configured cluster members, and any
54
// peer can send that type, so a sender outside the cluster is held to
55
// unknown rather than counted as one.
56
BEAST_EXPECT(
57
TrafficCount::attribute
(
TrafficCount::Category::Cluster
, outsider) ==
58
TrafficCount::Category::Unknown
);
59
BEAST_EXPECT(
60
TrafficCount::attribute
(
TrafficCount::Category::Cluster
, member) ==
61
TrafficCount::Category::Cluster
);
62
63
// Every other category is derived from the message alone, so membership
64
// does not enter into it either way. Base is the first category and
65
// Unknown the last, so the walk covers all of them.
66
for
(
auto
i =
static_cast<
std::size_t
>
(
TrafficCount::Category::Base
);
67
i <= static_cast<std::size_t>(
TrafficCount::Category::Unknown
);
68
++i)
69
{
70
auto
const
cat =
static_cast<
TrafficCount::Category
>
(i);
71
if
(cat ==
TrafficCount::Category::Cluster
)
72
continue
;
73
74
BEAST_EXPECT(
TrafficCount::attribute
(cat, outsider) == cat);
75
BEAST_EXPECT(
TrafficCount::attribute
(cat, member) == cat);
76
}
77
}
78
79
struct
TestCase
80
{
81
std::string
name
;
82
int
size
;
83
bool
inbound
;
84
int
messageCount
;
85
std::uint64_t
expectedBytesIn
;
86
std::uint64_t
expectedBytesOut
;
87
std::uint64_t
expectedMessagesIn
;
88
std::uint64_t
expectedMessagesOut
;
89
};
90
91
void
92
testAddCount
()
93
{
94
auto
run
= [&](
TestCase
const
& tc) {
95
testcase
(tc.name);
96
TrafficCount
traffic;
97
98
auto
const
counts = traffic.
getCounts
();
99
std::ranges::for_each
(counts, [&](
auto
const
& pair) {
100
for
(
auto
i = 0; i < tc.messageCount; ++i)
101
traffic.
addCount
(pair.first, tc.inbound, tc.size);
102
});
103
104
auto
const
countsNew = traffic.
getCounts
();
105
std::ranges::for_each
(countsNew, [&](
auto
const
& pair) {
106
BEAST_EXPECT(pair.second.bytesIn.load() == tc.expectedBytesIn);
107
BEAST_EXPECT(pair.second.bytesOut.load() == tc.expectedBytesOut);
108
BEAST_EXPECT(pair.second.messagesIn.load() == tc.expectedMessagesIn);
109
BEAST_EXPECT(pair.second.messagesOut.load() == tc.expectedMessagesOut);
110
});
111
};
112
113
auto
const
testcases = {
114
TestCase
{
115
.name =
"zero-counts"
,
116
.size = 0,
117
.inbound =
false
,
118
.messageCount = 0,
119
.expectedBytesIn = 0,
120
.expectedBytesOut = 0,
121
.expectedMessagesIn = 0,
122
.expectedMessagesOut = 0,
123
},
124
TestCase
{
125
.name =
"inbound-counts"
,
126
.size = 10,
127
.inbound =
true
,
128
.messageCount = 10,
129
.expectedBytesIn = 100,
130
.expectedBytesOut = 0,
131
.expectedMessagesIn = 10,
132
.expectedMessagesOut = 0,
133
},
134
TestCase
{
135
.name =
"outbound-counts"
,
136
.size = 10,
137
.inbound =
false
,
138
.messageCount = 10,
139
.expectedBytesIn = 0,
140
.expectedBytesOut = 100,
141
.expectedMessagesIn = 0,
142
.expectedMessagesOut = 10,
143
},
144
};
145
146
for
(
auto
const
& tc : testcases)
147
run
(tc);
148
}
149
150
void
151
testToString
()
152
{
153
testcase
(
"category-to-string"
);
154
155
// known category returns known string value
156
BEAST_EXPECT(
TrafficCount::toString
(
TrafficCount::Category::Total
) ==
"total"
);
157
158
// return "unknown" for unknown categories
159
BEAST_EXPECT(
160
TrafficCount::toString
(
static_cast<
TrafficCount::Category
>
(1000)) ==
"unknown"
);
161
}
162
163
void
164
run
()
override
165
{
166
testCategorize
();
167
testAttribute
();
168
testAddCount
();
169
testToString
();
170
}
171
};
172
173
BEAST_DEFINE_TESTSUITE
(traffic_count, overlay,
xrpl
);
174
175
}
// namespace xrpl::test
algorithm
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
xrpl::TrafficCount
TrafficCount is used to count ingress and egress wire bytes and number of messages.
Definition
TrafficCount.h:36
xrpl::TrafficCount::IsFromCluster::Yes
@ Yes
Definition
TrafficCount.h:201
xrpl::TrafficCount::IsFromCluster::No
@ No
Definition
TrafficCount.h:201
xrpl::TrafficCount::attribute
static Category attribute(Category cat, IsFromCluster isFromCluster)
Limits a category to what the sender can report.
Definition
TrafficCount.cpp:139
xrpl::TrafficCount::categorize
static Category categorize(::google::protobuf::Message const &message, protocol::MessageType type, bool inbound)
Given a protocol message, determine which traffic category it belongs to.
Definition
TrafficCount.cpp:31
xrpl::TrafficCount::getCounts
auto const & getCounts() const
An up-to-date copy of all the counters.
Definition
TrafficCount.h:253
xrpl::TrafficCount::addCount
void addCount(Category cat, bool inbound, int bytes)
Account for traffic associated with the given category.
Definition
TrafficCount.h:224
xrpl::TrafficCount::Category
Category
Definition
TrafficCount.h:72
xrpl::TrafficCount::Category::Base
@ Base
Definition
TrafficCount.h:73
xrpl::TrafficCount::Category::Cluster
@ Cluster
Definition
TrafficCount.h:75
xrpl::TrafficCount::Category::Unknown
@ Unknown
Definition
TrafficCount.h:184
xrpl::TrafficCount::Category::Total
@ Total
Definition
TrafficCount.h:182
xrpl::TrafficCount::toString
static std::string toString(Category cat)
Definition
TrafficCount.h:259
xrpl::test::traffic_count_test::traffic_count_test
traffic_count_test()=default
xrpl::test::traffic_count_test::testCategorize
void testCategorize()
Definition
traffic_count_test.cpp:19
xrpl::test::traffic_count_test::testAttribute
void testAttribute()
Definition
traffic_count_test.cpp:46
xrpl::test::traffic_count_test::testToString
void testToString()
Definition
traffic_count_test.cpp:151
xrpl::test::traffic_count_test::testAddCount
void testAddCount()
Definition
traffic_count_test.cpp:92
xrpl::test::traffic_count_test::run
void run() override
Runs the suite.
Definition
traffic_count_test.cpp:164
cstddef
cstdint
std::ranges::for_each
T for_each(T... args)
std::uint64_t
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
std::size_t
xrpl::test::traffic_count_test::TestCase
Definition
traffic_count_test.cpp:80
xrpl::test::traffic_count_test::TestCase::size
int size
Definition
traffic_count_test.cpp:82
xrpl::test::traffic_count_test::TestCase::expectedBytesOut
std::uint64_t expectedBytesOut
Definition
traffic_count_test.cpp:86
xrpl::test::traffic_count_test::TestCase::inbound
bool inbound
Definition
traffic_count_test.cpp:83
xrpl::test::traffic_count_test::TestCase::messageCount
int messageCount
Definition
traffic_count_test.cpp:84
xrpl::test::traffic_count_test::TestCase::expectedBytesIn
std::uint64_t expectedBytesIn
Definition
traffic_count_test.cpp:85
xrpl::test::traffic_count_test::TestCase::expectedMessagesOut
std::uint64_t expectedMessagesOut
Definition
traffic_count_test.cpp:88
xrpl::test::traffic_count_test::TestCase::name
std::string name
Definition
traffic_count_test.cpp:81
xrpl::test::traffic_count_test::TestCase::expectedMessagesIn
std::uint64_t expectedMessagesIn
Definition
traffic_count_test.cpp:87
Generated by
1.17.0