Standardize the snprintf() failure messages, and add tests for libmemcached_util_getpid()
[awesomized/libmemcached] / libmemcached / util / pid.cc
index d9ba79c7b28bdd36aee9251fe1b8e83435296082..2d7a86ddd8a18c6e9b6d9cc0a8f2511ae05917ed 100644 (file)
@@ -56,31 +56,32 @@ pid_t libmemcached_util_getpid(const char *hostname, in_port_t port, memcached_r
   if (not memc_ptr)
   {
     *ret= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
-    return pid;
+    return -1;
   }
 
   memcached_return_t rc= memcached_server_add(memc_ptr, hostname, port);
   if (memcached_success(rc))
   {
     memcached_stat_st *stat= memcached_stat(memc_ptr, NULL, &rc);
-    if (stat and stat->pid > 0)
+    if (memcached_success(rc) and stat and stat->pid != -1)
     {
       pid= stat->pid;
     }
-    else if (memcached_failed(rc) and rc == MEMCACHED_SOME_ERRORS)
+    else if (memcached_success(rc))
+    {
+      rc= MEMCACHED_UNKNOWN_STAT_KEY; // Something went wrong if this happens
+    }
+    else if (rc == MEMCACHED_SOME_ERRORS) // Generic answer, we will now find the specific reason (if one exists)
     {
       memcached_server_instance_st instance=
         memcached_server_instance_by_position(memc_ptr, 0);
 
+      assert_msg(instance and instance->error_messages, " ");
       if (instance and instance->error_messages)
       {
         rc= memcached_server_error_return(instance);
       }
     }
-    else if (memcached_success(rc))
-    {
-      rc= MEMCACHED_UNKNOWN_STAT_KEY; // Something went wrong if this happens
-    }
 
     memcached_stat_free(memc_ptr, stat);
   }