rippled
Loading...
Searching...
No Matches
traffic_count_test.cpp
1#include <xrpld/overlay/Message.h>
2#include <xrpld/overlay/detail/TrafficCount.h>
3
4#include <xrpl/beast/unit_test.h>
5#include <xrpl/protocol/messages.h>
6
7namespace ripple {
8
9namespace test {
10
12{
13public:
14 traffic_count_test() = default;
15
16 void
18 {
19 testcase("categorize");
20 protocol::TMPing message;
21 message.set_type(protocol::TMPing::ptPING);
22
23 // a known message is categorized to a proper category
24 auto const known =
25 TrafficCount::categorize(message, protocol::mtPING, false);
26 BEAST_EXPECT(known == TrafficCount::category::base);
27
28 // an unknown message type is categorized as unknown
30 message, static_cast<protocol::MessageType>(99), false);
32 }
33
45
46 void
48 {
49 auto run = [&](TestCase const& tc) {
50 testcase(tc.name);
51 TrafficCount m_traffic;
52
53 auto const counts = m_traffic.getCounts();
54 std::for_each(counts.begin(), counts.end(), [&](auto const& pair) {
55 for (auto i = 0; i < tc.messageCount; ++i)
56 m_traffic.addCount(pair.first, tc.inbound, tc.size);
57 });
58
59 auto const counts_new = m_traffic.getCounts();
61 counts_new.begin(), counts_new.end(), [&](auto const& pair) {
62 BEAST_EXPECT(
63 pair.second.bytesIn.load() == tc.expectedBytesIn);
64 BEAST_EXPECT(
65 pair.second.bytesOut.load() == tc.expectedBytesOut);
66 BEAST_EXPECT(
67 pair.second.messagesIn.load() == tc.expectedMessagesIn);
68 BEAST_EXPECT(
69 pair.second.messagesOut.load() ==
70 tc.expectedMessagesOut);
71 });
72 };
73
74 auto const testcases = {
76 .name = "zero-counts",
77 .size = 0,
78 .inbound = false,
79 .messageCount = 0,
80 .expectedBytesIn = 0,
81 .expectedBytesOut = 0,
82 .expectedMessagesIn = 0,
83 .expectedMessagesOut = 0,
84 },
86 .name = "inbound-counts",
87 .size = 10,
88 .inbound = true,
89 .messageCount = 10,
90 .expectedBytesIn = 100,
91 .expectedBytesOut = 0,
92 .expectedMessagesIn = 10,
93 .expectedMessagesOut = 0,
94 },
96 .name = "outbound-counts",
97 .size = 10,
98 .inbound = false,
99 .messageCount = 10,
100 .expectedBytesIn = 0,
101 .expectedBytesOut = 100,
102 .expectedMessagesIn = 0,
103 .expectedMessagesOut = 10,
104 },
105 };
106
107 for (auto const& tc : testcases)
108 run(tc);
109 }
110
111 void
113 {
114 testcase("category-to-string");
115
116 // known category returns known string value
117 BEAST_EXPECT(
119
120 // return "unknown" for unknown categories
121 BEAST_EXPECT(
123 static_cast<TrafficCount::category>(1000)) == "unknown");
124 }
125
126 void
127 run() override
128 {
130 testAddCount();
131 testToString();
132 }
133};
134
135BEAST_DEFINE_TESTSUITE(traffic_count, overlay, ripple);
136
137} // namespace test
138} // namespace ripple
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
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:6
T size(T... args)