rippled
Loading...
Searching...
No Matches
traffic_count_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2025 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/overlay/Message.h>
21#include <xrpld/overlay/detail/TrafficCount.h>
22
23#include <xrpl/beast/unit_test.h>
24#include <xrpl/protocol/messages.h>
25
26namespace ripple {
27
28namespace test {
29
31{
32public:
33 traffic_count_test() = default;
34
35 void
37 {
38 testcase("categorize");
39 protocol::TMPing message;
40 message.set_type(protocol::TMPing::ptPING);
41
42 // a known message is categorized to a proper category
43 auto const known =
44 TrafficCount::categorize(message, protocol::mtPING, false);
45 BEAST_EXPECT(known == TrafficCount::category::base);
46
47 // an unknown message type is categorized as unknown
49 message, static_cast<protocol::MessageType>(99), false);
51 }
52
64
65 void
67 {
68 auto run = [&](TestCase const& tc) {
69 testcase(tc.name);
70 TrafficCount m_traffic;
71
72 auto const counts = m_traffic.getCounts();
73 std::for_each(counts.begin(), counts.end(), [&](auto const& pair) {
74 for (auto i = 0; i < tc.messageCount; ++i)
75 m_traffic.addCount(pair.first, tc.inbound, tc.size);
76 });
77
78 auto const counts_new = m_traffic.getCounts();
80 counts_new.begin(), counts_new.end(), [&](auto const& pair) {
81 BEAST_EXPECT(
82 pair.second.bytesIn.load() == tc.expectedBytesIn);
83 BEAST_EXPECT(
84 pair.second.bytesOut.load() == tc.expectedBytesOut);
85 BEAST_EXPECT(
86 pair.second.messagesIn.load() == tc.expectedMessagesIn);
87 BEAST_EXPECT(
88 pair.second.messagesOut.load() ==
89 tc.expectedMessagesOut);
90 });
91 };
92
93 auto const testcases = {
95 .name = "zero-counts",
96 .size = 0,
97 .inbound = false,
98 .messageCount = 0,
99 .expectedBytesIn = 0,
100 .expectedBytesOut = 0,
101 .expectedMessagesIn = 0,
102 .expectedMessagesOut = 0,
103 },
104 TestCase{
105 .name = "inbound-counts",
106 .size = 10,
107 .inbound = true,
108 .messageCount = 10,
109 .expectedBytesIn = 100,
110 .expectedBytesOut = 0,
111 .expectedMessagesIn = 10,
112 .expectedMessagesOut = 0,
113 },
114 TestCase{
115 .name = "outbound-counts",
116 .size = 10,
117 .inbound = false,
118 .messageCount = 10,
119 .expectedBytesIn = 0,
120 .expectedBytesOut = 100,
121 .expectedMessagesIn = 0,
122 .expectedMessagesOut = 10,
123 },
124 };
125
126 for (auto const& tc : testcases)
127 run(tc);
128 }
129
130 void
132 {
133 testcase("category-to-string");
134
135 // known category returns known string value
136 BEAST_EXPECT(
138
139 // return "unknown" for unknown categories
140 BEAST_EXPECT(
142 static_cast<TrafficCount::category>(1000)) == "unknown");
143 }
144
145 void
146 run() override
147 {
149 testAddCount();
150 testToString();
151 }
152};
153
154BEAST_DEFINE_TESTSUITE(traffic_count, overlay, ripple);
155
156} // namespace test
157} // namespace ripple
A testsuite class.
Definition suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
TrafficCount is used to count ingress and egress wire bytes and number of messages.
auto const & getCounts() const
An up-to-date copy of all the counters.
static category categorize(::google::protobuf::Message const &message, protocol::MessageType type, bool inbound)
Given a protocol message, determine which traffic category it belongs to.
static std::string to_string(category cat)
void run() override
Runs the suite.
T for_each(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
T size(T... args)