Use PRIu64 instead of %lld
[m6w6/libmemcached] / tests / server.c
index c96f5972dbda3a5c2b2e32df8ea43bed999e0fd9..f2717795af0af04c2d5f46960a8f0f6a861e0278 100644 (file)
 #include <assert.h>
 #include <signal.h>
 #include <libmemcached/memcached.h>
+#include <libmemcached/util.h>
 #include <unistd.h>
+#include <sys/time.h>
+
 #include "server.h"
 
 void server_startup(server_startup_st *construct)
 {
-  unsigned int x;
-
   if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
   {
     printf("servers %s\n", construct->server_list);
@@ -45,7 +46,7 @@ void server_startup(server_startup_st *construct)
       char *end_ptr;
       end_ptr= server_string_buffer;
 
-      for (x= 0; x < construct->count; x++)
+      for (uint32_t x= 0; x < construct->count; x++)
       {
         char buffer[1024]; /* Nothing special for number */
         int count;
@@ -73,20 +74,61 @@ void server_startup(server_startup_st *construct)
         if (x == 0)
         {
           sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128",
-                    MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
+                 MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
         }
         else
         {
           sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u",
-                    MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
+                 MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
         }
-        fprintf(stderr, "STARTING SERVER: %s\n", buffer);
-        status= system(buffer);
+       if (libmemcached_util_ping("localhost", (in_port_t)(x + TEST_PORT_BASE), NULL))
+       {
+         fprintf(stderr, "Server on port %u already exists\n", x + TEST_PORT_BASE);
+       }
+       else
+       {
+         status= system(buffer);
+         fprintf(stderr, "STARTING SERVER: %s  status:%d\n", buffer, status);
+       }
         count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
         end_ptr+= count;
       }
       *end_ptr= 0;
 
+      for (uint32_t x= 0; x < construct->count; x++)
+      {
+        uint32_t counter= 3;
+        char buffer[1024]; /* Nothing special for number */
+
+        snprintf(buffer, sizeof(buffer), "/tmp/%umemc.pid", x);
+
+        while (--counter)
+        {
+          int memcached_pid;
+
+          FILE *file;
+          file= fopen(buffer, "r");
+          if (file == NULL)
+          {
+#ifndef WIN32
+            struct timespec req= { .tv_sec= 0, .tv_nsec= 5000 };
+            nanosleep(&req, NULL);
+#endif
+            continue;
+          }
+          char *found= fgets(buffer, sizeof(buffer), file);
+          if (!found)
+          {
+            abort();
+          }
+          // Yes, we currently pitch this and don't make use of it.
+          memcached_pid= atoi(buffer);
+          fclose(file);
+        }
+
+
+      }
+
       construct->server_list= strdup(server_string_buffer);
     }
     printf("servers %s\n", construct->server_list);
@@ -97,7 +139,7 @@ void server_startup(server_startup_st *construct)
 
   srandom((unsigned int)time(NULL));
 
-  for (x= 0; x < memcached_server_list_count(construct->servers); x++)
+  for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
   {
     printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
     assert(construct->servers[x].fd == -1);
@@ -109,11 +151,9 @@ void server_startup(server_startup_st *construct)
 
 void server_shutdown(server_startup_st *construct)
 {
-  unsigned int x;
-
   if (construct->server_list)
   {
-    for (x= 0; x < construct->count; x++)
+    for (uint32_t x= 0; x < construct->count; x++)
     {
       char buffer[1024]; /* Nothing special for number */
       sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);