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 = TrafficCount::categorize(message, static_cast<protocol::MessageType>(99), false);
30 }
31
43
44 void
46 {
47 auto run = [&](TestCase const& tc) {
48 testcase(tc.name);
49 TrafficCount m_traffic;
50
51 auto const counts = m_traffic.getCounts();
52 std::for_each(counts.begin(), counts.end(), [&](auto const& pair) {
53 for (auto i = 0; i < tc.messageCount; ++i)
54 m_traffic.addCount(pair.first, tc.inbound, tc.size);
55 });
56
57 auto const counts_new = m_traffic.getCounts();
58 std::for_each(counts_new.begin(), counts_new.end(), [&](auto const& pair) {
59 BEAST_EXPECT(pair.second.bytesIn.load() == tc.expectedBytesIn);
60 BEAST_EXPECT(pair.second.bytesOut.load() == tc.expectedBytesOut);
61 BEAST_EXPECT(pair.second.messagesIn.load() == tc.expectedMessagesIn);
62 BEAST_EXPECT(pair.second.messagesOut.load() == tc.expectedMessagesOut);
63 });
64 };
65
66 auto const testcases = {
68 .name = "zero-counts",
69 .size = 0,
70 .inbound = false,
71 .messageCount = 0,
72 .expectedBytesIn = 0,
73 .expectedBytesOut = 0,
74 .expectedMessagesIn = 0,
75 .expectedMessagesOut = 0,
76 },
78 .name = "inbound-counts",
79 .size = 10,
80 .inbound = true,
81 .messageCount = 10,
82 .expectedBytesIn = 100,
83 .expectedBytesOut = 0,
84 .expectedMessagesIn = 10,
85 .expectedMessagesOut = 0,
86 },
88 .name = "outbound-counts",
89 .size = 10,
90 .inbound = false,
91 .messageCount = 10,
92 .expectedBytesIn = 0,
93 .expectedBytesOut = 100,
94 .expectedMessagesIn = 0,
95 .expectedMessagesOut = 10,
96 },
97 };
98
99 for (auto const& tc : testcases)
100 run(tc);
101 }
102
103 void
105 {
106 testcase("category-to-string");
107
108 // known category returns known string value
110
111 // return "unknown" for unknown categories
112 BEAST_EXPECT(TrafficCount::to_string(static_cast<TrafficCount::category>(1000)) == "unknown");
113 }
114
115 void
116 run() override
117 {
119 testAddCount();
120 testToString();
121 }
122};
123
124BEAST_DEFINE_TESTSUITE(traffic_count, overlay, xrpl);
125
126} // namespace test
127} // namespace xrpl
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
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)