@@ -58,6 +58,72 @@ void testErrorConditions() {
5858 throwOnError (client, false , " Should not be able to resolve a ridiculous ip" );
5959}
6060
61+ void testOverflow () {
62+ StatsdServer mock_server;
63+ std::vector<std::string> messages, expected;
64+ std::thread server (mock, std::ref (mock_server), std::ref (messages), /* split=*/ false );
65+
66+ // Set a new config that has the client send messages to a proper address that can be resolved
67+ StatsdClient client (" localhost" , 8125 , /* prefix=*/ " " , /* batchsize=*/ 48 , /* sendInterval=*/ 0 , /* gaugePrecision=*/ 3 );
68+ throwOnError (client);
69+
70+ // Send message that exhausts most of the batch limit (40/48)
71+ client.increment (" stat.timesWeUsed40CharsOfBufferSpace" );
72+ throwOnError (client);
73+ expected.emplace_back (" stat.timesWeUsed40CharsOfBufferSpace:1|c" );
74+
75+ // Send message that would go over the batch limit (50/48)
76+ // The second message shouldn't fit the batch and must be part of the next batch
77+ client.increment (" stat.i" );
78+ throwOnError (client);
79+ expected.emplace_back (" stat.i:1|c" );
80+
81+ // Flush messages
82+ client.flush ();
83+
84+ // Send messages that would exactly fit in the batch, newlines included (48/48)
85+ // Everything should be part of the same batch
86+ expected.emplace_back ();
87+ for (int idx = 0 ; idx < 5 ; idx++) {
88+ client.increment (" silly" );
89+ throwOnError (client);
90+ expected.back ().append (" silly:1|c\n " );
91+ }
92+ expected.back ().pop_back ();
93+
94+ // Flush messages
95+ client.flush ();
96+
97+ // With all messages flushed, send one giant message that would exceed the batch (50/48)
98+ // The message should arrive intact in one batch
99+ client.increment (" stat.veryImportantIfIncrementedComputerExplode" );
100+ throwOnError (client);
101+ expected.emplace_back (" stat.veryImportantIfIncrementedComputerExplode:1|c" );
102+
103+ // Flush messages
104+ client.flush ();
105+
106+ // Signal the mock server we are done (and flush so that the signal is sent)
107+ client.timing (" DONE" , 0 );
108+ client.flush ();
109+
110+ // Wait for the server to stop
111+ server.join ();
112+
113+ // Make sure we get the exactly correct output
114+ if (messages != expected) {
115+ std::cerr << " Unexpected stats received by server, got:" << std::endl;
116+ for (const auto & message : messages) {
117+ std::cerr << " - " << message << std::endl;
118+ }
119+ std::cerr << std::endl << " But we expected:" << std::endl;
120+ for (const auto & message : expected) {
121+ std::cerr << " - " << message << std::endl;
122+ }
123+ throw std::runtime_error (" Unexpected stats" );
124+ }
125+ }
126+
61127void testReconfigure () {
62128 StatsdServer server;
63129 throwOnError (server);
@@ -181,6 +247,8 @@ int main() {
181247
182248 // general things that should be errors
183249 testErrorConditions ();
250+ // ensure batch size is respected
251+ testOverflow ();
184252 // reconfiguring how you are sending
185253 testReconfigure ();
186254 // no batching
0 commit comments