testing: ensure the server is properly listening
[awesomized/libmemcached] / test / lib / Server.cpp
index f31819e86ef8dfbc3fa99d9ded1335b34945d55c..8666b690ee602d5eb54a9ec8c843cbe24748f7f5 100644 (file)
@@ -1,5 +1,5 @@
 #include "Server.hpp"
-#include "Connection.hpp"
+#include "Retry.hpp"
 #include "ForkAndExec.hpp"
 
 #include <sys/wait.h>
@@ -130,7 +130,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 +142,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;