testing: freebsd [travis skip]
[m6w6/libmemcached] / test / lib / Cluster.cpp
index c3c625cf04eb225069a08d4b5f732ea68132eade..54de9398564e25c5cba32f328fe56299448b7bcd 100644 (file)
@@ -7,10 +7,22 @@ Cluster::Cluster(Server serv, uint16_t cnt)
 : count{cnt}
 , proto{move(serv)}
 {
-  if (!cnt) {
-    count = thread::hardware_concurrency()/2 ?: 4;
+  if (count < 4) {
+      count = stoi(getenv_else("MEMCACHED_CLUSTER", "4"));
+  }
+  if (!count) {
+    count = 1;
+  }
+  for (int i = 0; i < count; ++i) {
+    cluster.push_back(proto);
   }
-  reset();
+}
+
+Cluster::Cluster(vector<Server> servers)
+: count{servers.size()}
+, cluster{move(servers)}
+{
+
 }
 
 Cluster::~Cluster() {
@@ -22,14 +34,6 @@ const vector<Server> &Cluster::getServers() const {
   return cluster;
 }
 
-void Cluster::reset() {
-  pids.clear();
-  cluster.clear();
-  for (int i = 0; i < count; ++i) {
-    cluster.push_back(proto);
-  }
-}
-
 bool Cluster::start() {
   bool started = true;
 
@@ -42,11 +46,15 @@ bool Cluster::start() {
   return started;
 }
 
-void Cluster::stop() {
+void Cluster::stop(bool graceful) {
   for (auto &server : cluster) {
     server.drain();
-    // no cookies for memcached; TERM is just too slow
-    server.signal(SIGKILL);
+    if (graceful) {
+      server.stop();
+    } else {
+      // no cookies for memcached; TERM is just too slow
+      server.signal(SIGKILL);
+    }
   }
 }
 
@@ -108,3 +116,9 @@ void Cluster::wait() {
     }
   }
 }
+
+bool Cluster::restart() {
+  stop();
+  wait();
+  return start();
+}