Each test suite below spawns a thread to recv the client messages over UDP as if it were a real statsd server Note that we could just synchronously recv metrics and not use a thread but doing the test async has the advantage that we can test the threaded batching mode in a straightforward way. The server thread basically just keeps storing metrics in an vector until it hears a special one signalin
| 10 | // advantage that we can test the threaded batching mode in a straightforward way. The server thread basically |
| 11 | // just keeps storing metrics in an vector until it hears a special one signaling the test is over and bails |
| 12 | void mock(StatsdServer& server, std::vector<std::string>& messages) { |
| 13 | do { |
| 14 | // Grab the messages that are waiting |
| 15 | auto recvd = server.receive(); |
| 16 | |
| 17 | // Split the messages on '\n' |
| 18 | auto start = std::string::npos; |
| 19 | do { |
| 20 | // Keep this message |
| 21 | auto end = recvd.find('\n', ++start); |
| 22 | messages.emplace_back(recvd.substr(start, end)); |
| 23 | start = end; |
| 24 | |
| 25 | // Bail if we found the special quit message |
| 26 | if (messages.back().find("DONE") != std::string::npos) { |
| 27 | messages.pop_back(); |
| 28 | return; |
| 29 | } |
| 30 | } while (start != std::string::npos); |
| 31 | } while (server.errorMessage().empty() && !messages.back().empty()); |
| 32 | } |
| 33 | |
| 34 | template <typename SocketWrapper> |
| 35 | void throwOnError(const SocketWrapper& wrapped, bool expectEmpty = true, const std::string& extraMessage = "") { |