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 xrpl {
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 = TrafficCount::categorize(message, protocol::mtPING, false);
25 BEAST_EXPECT(known == TrafficCount::category::base);
26
27 // an unknown message type is categorized as unknown
28 auto const unknown =
29 TrafficCount::categorize(message, static_cast<protocol::MessageType>(99), false);
31 }
32
44
45 void
47 {
48 auto run = [&](TestCase const& tc) {
49 testcase(tc.name);
50 TrafficCount m_traffic;
51
52 auto const counts = m_traffic.getCounts();
53 std::for_each(counts.begin(), counts.end(), [&](auto const& pair) {
54 for (auto i = 0; i < tc.messageCount; ++i)
55 m_traffic.addCount(pair.first, tc.inbound, tc.size);
56 });
57
58 auto const counts_new = m_traffic.getCounts();
59 std::for_each(counts_new.begin(), counts_new.end(), [&](auto const& pair) {
60 BEAST_EXPECT(pair.second.bytesIn.load() == tc.expectedBytesIn);
61 BEAST_EXPECT(pair.second.bytesOut.load() == tc.expectedBytesOut);
62 BEAST_EXPECT(pair.second.messagesIn.load() == tc.expectedMessagesIn);
63 BEAST_EXPECT(pair.second.messagesOut.load() == tc.expectedMessagesOut);
64 });
65 };
66
67 auto const testcases = {
69 .name = "zero-counts",
70 .size = 0,
71 .inbound = false,
72 .messageCount = 0,
73 .expectedBytesIn = 0,
74 .expectedBytesOut = 0,
75 .expectedMessagesIn = 0,
76 .expectedMessagesOut = 0,
77 },
79 .name = "inbound-counts",
80 .size = 10,
81 .inbound = true,
82 .messageCount = 10,
83 .expectedBytesIn = 100,
84 .expectedBytesOut = 0,
85 .expectedMessagesIn = 10,
86 .expectedMessagesOut = 0,
87 },
89 .name = "outbound-counts",
90 .size = 10,
91 .inbound = false,
92 .messageCount = 10,
93 .expectedBytesIn = 0,
94 .expectedBytesOut = 100,
95 .expectedMessagesIn = 0,
96 .expectedMessagesOut = 10,
97 },
98 };
99
100 for (auto const& tc : testcases)
101 run(tc);
102 }
103
104 void
106 {
107 testcase("category-to-string");
108
109 // known category returns known string value
111
112 // return "unknown" for unknown categories
113 BEAST_EXPECT(
114 TrafficCount::to_string(static_cast<TrafficCount::category>(1000)) == "unknown");
115 }
116
117 void
118 run() override
119 {
121 testAddCount();
122 testToString();
123 }
124};
125
126BEAST_DEFINE_TESTSUITE(traffic_count, overlay, xrpl);
127
128} // namespace test
129} // namespace xrpl
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:150
TrafficCount is used to count ingress and egress wire bytes and number of messages.
static std::string to_string(category cat)
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.
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:5
T size(T... args)