xrpld
Loading...
Searching...
No Matches
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 <cstdint>
9
10namespace xrpl::test {
11
13{
14public:
15 traffic_count_test() = default;
16
17 void
19 {
20 testcase("categorize");
21 protocol::TMPing message;
22 message.set_type(protocol::TMPing::ptPING);
23
24 // a known message is categorized to a proper category
25 auto const known = TrafficCount::categorize(message, protocol::mtPING, false);
26 BEAST_EXPECT(known == TrafficCount::Category::Base);
27
28 // an unknown message type is categorized as unknown
29 auto const unknown =
30 TrafficCount::categorize(message, static_cast<protocol::MessageType>(99), false);
31 BEAST_EXPECT(unknown == TrafficCount::Category::Unknown);
32 }
33
45
46 void
48 {
49 auto run = [&](TestCase const& tc) {
50 testcase(tc.name);
51 TrafficCount traffic;
52
53 auto const counts = traffic.getCounts();
54 std::ranges::for_each(counts, [&](auto const& pair) {
55 for (auto i = 0; i < tc.messageCount; ++i)
56 traffic.addCount(pair.first, tc.inbound, tc.size);
57 });
58
59 auto const countsNew = traffic.getCounts();
60 std::ranges::for_each(countsNew, [&](auto const& pair) {
61 BEAST_EXPECT(pair.second.bytesIn.load() == tc.expectedBytesIn);
62 BEAST_EXPECT(pair.second.bytesOut.load() == tc.expectedBytesOut);
63 BEAST_EXPECT(pair.second.messagesIn.load() == tc.expectedMessagesIn);
64 BEAST_EXPECT(pair.second.messagesOut.load() == tc.expectedMessagesOut);
65 });
66 };
67
68 auto const testcases = {
70 .name = "zero-counts",
71 .size = 0,
72 .inbound = false,
73 .messageCount = 0,
74 .expectedBytesIn = 0,
75 .expectedBytesOut = 0,
76 .expectedMessagesIn = 0,
77 .expectedMessagesOut = 0,
78 },
80 .name = "inbound-counts",
81 .size = 10,
82 .inbound = true,
83 .messageCount = 10,
84 .expectedBytesIn = 100,
85 .expectedBytesOut = 0,
86 .expectedMessagesIn = 10,
87 .expectedMessagesOut = 0,
88 },
90 .name = "outbound-counts",
91 .size = 10,
92 .inbound = false,
93 .messageCount = 10,
94 .expectedBytesIn = 0,
95 .expectedBytesOut = 100,
96 .expectedMessagesIn = 0,
97 .expectedMessagesOut = 10,
98 },
99 };
100
101 for (auto const& tc : testcases)
102 run(tc);
103 }
104
105 void
107 {
108 testcase("category-to-string");
109
110 // known category returns known string value
112
113 // return "unknown" for unknown categories
114 BEAST_EXPECT(
115 TrafficCount::toString(static_cast<TrafficCount::Category>(1000)) == "unknown");
116 }
117
118 void
119 run() override
120 {
122 testAddCount();
123 testToString();
124 }
125};
126
127BEAST_DEFINE_TESTSUITE(traffic_count, overlay, xrpl);
128
129} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
TrafficCount is used to count ingress and egress wire bytes and number of messages.
static Category categorize(::google::protobuf::Message const &message, protocol::MessageType type, bool inbound)
Given a protocol message, determine which traffic category it belongs to.
auto const & getCounts() const
An up-to-date copy of all the counters.
void addCount(Category cat, bool inbound, int bytes)
Account for traffic associated with the given category.
static std::string toString(Category cat)
void run() override
Runs the suite.
T for_each(T... args)
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5