Updated test harness, merged Trond's fix for OSX hang.
[awesomized/libmemcached] / tests / test.c
index e7e16f54dee1f5ae7bffde2da0b94a47397575e5..d0d5329245a6ed398308a267b6c8c67127cb596a 100644 (file)
@@ -2,7 +2,6 @@
   Sample test application.
 */
 #include <assert.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <time.h>
+#include <fnmatch.h>
+#include "server.h"
 
 #include "test.h"
 
-long int timedif(struct timeval a, struct timeval b)
+static long int timedif(struct timeval a, struct timeval b)
 {
   register int us, s;
 
-  us = a.tv_usec - b.tv_usec;
+  us = (int)(a.tv_usec - b.tv_usec);
   us /= 1000;
-  s = a.tv_sec - b.tv_sec;
+  s = (int)(a.tv_sec - b.tv_sec);
   s *= 1000;
   return s + us;
 }
 
+static const char *test_strerror(test_return_t code)
+{
+  switch (code) {
+  case TEST_SUCCESS:
+    return "ok";
+  case TEST_FAILURE:
+    return "failed";
+  case TEST_MEMORY_ALLOCATION_FAILURE:
+    return "memory allocation";
+  case TEST_SKIPPED:
+    return "skipped";
+  case TEST_MAXIMUM_RETURN:
+  default: 
+    fprintf(stderr, "Unknown return value\n");
+    assert(0);
+  }
+
+}
+
 int main(int argc, char *argv[])
 {
+  test_return_t failed;
   unsigned int x;
-  char *server_list;
   char *collection_to_run= NULL;
   char *wildcard= NULL;
+  server_startup_st *startup_ptr;
   memcached_server_st *servers;
+  world_st world;
   collection_st *collection;
+  collection_st *next;
+  void *world_ptr;
+
+  memset(&world, 0, sizeof(world_st));
+  get_world(&world);
+  collection= world.collections;
 
-  collection= gets_collections();
+  if (world.create)
+    world_ptr= world.create();
+  else 
+    world_ptr= NULL;
 
+  startup_ptr= (server_startup_st *)world_ptr;
+  servers= (memcached_server_st *)startup_ptr->servers;
 
   if (argc > 1)
     collection_to_run= argv[1];
@@ -42,67 +75,49 @@ int main(int argc, char *argv[])
   if (argc == 3)
     wildcard= argv[2];
 
-  if (!(server_list= getenv("MEMCACHED_SERVERS")))
-    server_list= "localhost";
-
-  printf("servers %s\n", server_list);
-  srandom(time(NULL));
-
-  servers= memcached_servers_parse(server_list);
-  assert(servers);
-
-  for (x= 0; x < memcached_server_list_count(servers); x++)
-  {
-    printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
-    assert(servers[x].stack_responses == 0);
-    assert(servers[x].fd == -1);
-    assert(servers[x].cursor_active == 0);
-  }
-
-  printf("\n");
-
-  collection_st *next;
   for (next= collection; next->name; next++)
   {
     test_st *run;
 
     run= next->tests;
-    if (collection_to_run && strcmp(collection_to_run, next->name))
+    if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
       continue;
 
     fprintf(stderr, "\n%s\n\n", next->name);
 
     for (x= 0; run->name; run++)
     {
-      if (wildcard && strcmp(wildcard, run->name))
-        continue;
-
-      fprintf(stderr, "Testing %s", run->name);
-
+      unsigned int loop;
       memcached_st *memc;
       memcached_return rc;
       struct timeval start_time, end_time;
+      long int load_time;
+
+      if (wildcard && fnmatch(wildcard, run->name, 0))
+        continue;
+
+      fprintf(stderr, "Testing %s", run->name);
 
       memc= memcached_create(NULL);
       assert(memc);
 
-      if (run->requires_flush)
-        memcached_flush(memc, 0);
-
       rc= memcached_server_push(memc, servers);
       assert(rc == MEMCACHED_SUCCESS);
 
-      unsigned int loop;
+      if (run->requires_flush)
+      {
+        memcached_flush(memc, 0);
+        memcached_quit(memc);
+      }
+
       for (loop= 0; loop < memcached_server_list_count(servers); loop++)
       {
-        assert(memc->hosts[loop].stack_responses == 0);
         assert(memc->hosts[loop].fd == -1);
         assert(memc->hosts[loop].cursor_active == 0);
       }
 
       if (next->pre)
       {
-        memcached_return rc;
         rc= next->pre(memc);
 
         if (rc != MEMCACHED_SUCCESS)
@@ -113,11 +128,12 @@ int main(int argc, char *argv[])
       }
 
       gettimeofday(&start_time, NULL);
-      run->function(memc);
+      failed= run->function(memc);
       gettimeofday(&end_time, NULL);
-      long int load_time= timedif(end_time, start_time);
-      fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000, 
-              load_time % 1000);
+      load_time= timedif(end_time, start_time);
+
+      fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ %s ]\n", load_time / 1000, 
+              load_time % 1000, test_strerror(failed));
 
       if (next->post)
         (void)next->post(memc);
@@ -130,7 +146,8 @@ error:
 
   fprintf(stderr, "All tests completed successfully\n\n");
 
-  memcached_server_list_free(servers);
+  if (world.destroy)
+    world.destroy(world_ptr);
 
   return 0;
 }