testing: UDP test
[awesomized/libmemcached] / test / lib / Server.cpp
index f31819e86ef8dfbc3fa99d9ded1335b34945d55c..af6e41ba63d5a1f010f3c9ca0de0aef13bd2d989 100644 (file)
@@ -1,5 +1,5 @@
 #include "Server.hpp"
-#include "Connection.hpp"
+#include "Retry.hpp"
 #include "ForkAndExec.hpp"
 
 #include <sys/wait.h>
@@ -39,11 +39,16 @@ static inline void pushArg(vector<char *> &arr, const string &arg) {
 
 optional<string> Server::handleArg(vector<char *> &arr, const string &arg, const arg_func_t &next_arg) {
   pushArg(arr, arg);
-  if (arg == "-p" || arg == "--port") {
+  if (arg == "-U" || arg == "--udp-port") {
+    auto port = next_arg(arg);
+    pushArg(arr, port);
+    pushArg(arr, "-p");
+    pushArg(arr, port);
+    socket_or_port = stoi(port);
+    return port;
+  } else if (arg == "-p" || arg == "--port") {
     auto port = next_arg(arg);
     pushArg(arr, port);
-//    pushArg(arr, "-U");
-//    pushArg(arr, port);
     socket_or_port = stoi(port);
     return port;
   } else if (arg == "-s" || arg == "--unix-socket") {
@@ -130,7 +135,7 @@ bool Server::isListening() {
   }
 
   Malloced stat(memcached_stat(*memc, nullptr, nullptr));
-  if (!*stat || !stat->pid) {
+  if (!*stat || !stat->pid || stat->pid == -1) {
     return false;
   }
   if (stat->pid != pid) {
@@ -142,6 +147,19 @@ bool Server::isListening() {
   return true;
 }
 
+bool Server::ensureListening() {
+  return Retry{[this] {
+    again:
+    start();
+    if (!isListening()) {
+      if (tryWait()){
+        goto again;
+      }
+    }
+    return isListening();
+  }}();
+}
+
 bool Server::stop() {
   if (!pid) {
     return true;
@@ -167,8 +185,19 @@ bool Server::check() {
 
 bool Server::wait(int flags) {
   if (pid && pid == waitpid(pid, &status, flags)) {
-    if (drain().length()) {
-      cerr << "Ouput of " << *this << ":\n" << output << endl;
+    if (drain().length() && output != "Signal handled: Terminated.\n") {
+      cerr << "Output of " << *this << ":\n";
+
+      istringstream iss{output};
+      string line;
+
+      while (getline(iss, line)) {
+        cerr << "  " << line << "\n";
+      }
+
+      if (output.back() != '\n') {
+        cerr << endl;
+      }
       output.clear();
     }
     pid = 0;