Small updates/wins around non-block IO
[m6w6/libmemcached] / tests / test.c
index f95141b107ee557db0ee09b85723c8f31ef5a5aa..45ee3ab46e9f07454d68384f78fdd9117470018d 100644 (file)
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <unistd.h>
 #include <time.h>
 #include <fnmatch.h>
-#include "server.h"
+#include <stdint.h>
 
+#include "libmemcached/memcached.h"
 #include "test.h"
 
 static void world_stats_print(world_stats_st *stats)
 {
+  fputc('\n', stderr);
+  fprintf(stderr, "Total Collections\t\t\t\t%u\n", stats->collection_total);
+  fprintf(stderr, "\tFailed Collections\t\t\t%u\n", stats->collection_failed);
+  fprintf(stderr, "\tSkipped Collections\t\t\t%u\n", stats->collection_skipped);
+  fprintf(stderr, "\tSucceeded Collections\t\t%u\n", stats->collection_success);
+  fputc('\n', stderr);
   fprintf(stderr, "Total\t\t\t\t%u\n", stats->total);
   fprintf(stderr, "\tFailed\t\t\t%u\n", stats->failed);
   fprintf(stderr, "\tSkipped\t\t\t%u\n", stats->skipped);
   fprintf(stderr, "\tSucceeded\t\t%u\n", stats->success);
 }
 
-static long int timedif(struct timeval a, struct timeval b)
+long int timedif(struct timeval a, struct timeval b)
 {
-  register int us, s;
+  long us, s;
 
   us = (int)(a.tv_usec - b.tv_usec);
   us /= 1000;
@@ -41,7 +49,7 @@ static long int timedif(struct timeval a, struct timeval b)
   return s + us;
 }
 
-static const char *test_strerror(test_return_t code)
+const char *test_strerror(test_return_t code)
 {
   switch (code) {
   case TEST_SUCCESS:
@@ -57,13 +65,26 @@ static const char *test_strerror(test_return_t code)
     fprintf(stderr, "Unknown return value\n");
     abort();
   }
-
 }
 
 void create_core(void)
 {
-   if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL && fork() == 0)
-     abort();
+  if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL)
+  {
+    pid_t pid= fork();
+
+    if (pid == 0)
+    {
+      abort();
+    }
+    else
+    {
+      while (waitpid(pid, NULL, 0) != pid)
+      {
+        ;
+      }
+    }
+  }
 }
 
 
@@ -85,6 +106,34 @@ static world_runner_st defualt_runners= {
   _runner_default
 };
 
+static test_return_t _default_callback(void *p)
+{
+  (void)p;
+
+  return TEST_SUCCESS;
+}
+
+static inline void set_default_fn(test_callback_fn *fn)
+{
+  if (*fn == NULL)
+  {
+    *fn= _default_callback;
+  }
+}
+
+static collection_st *init_world(world_st *world)
+{
+  if (! world->runner)
+  {
+    world->runner= &defualt_runners;
+  }
+
+  set_default_fn(&world->collection.startup);
+  set_default_fn(&world->collection.shutdown);
+
+  return world->collections;
+}
+
 
 int main(int argc, char *argv[])
 {
@@ -99,21 +148,31 @@ int main(int argc, char *argv[])
 
   world_stats_st stats;
 
+#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
+  if (sasl_client_init(NULL) != SASL_OK)
+  {
+     fprintf(stderr, "Failed to initialize sasl library!\n");
+     return 1;
+  }
+#endif
+
   memset(&stats, 0, sizeof(stats));
   memset(&world, 0, sizeof(world));
   get_world(&world);
 
-  if (! world.runner)
-  {
-    world.runner= &defualt_runners;
-  }
-
-  collection= world.collections;
+  collection= init_world(&world);
 
   if (world.create)
-    world_ptr= world.create();
+  {
+    test_return_t error;
+    world_ptr= world.create(&error);
+    if (error != TEST_SUCCESS)
+      exit(1);
+  }
   else
+  {
     world_ptr= NULL;
+  }
 
   if (argc > 1)
     collection_to_run= argv[1];
@@ -123,13 +182,48 @@ int main(int argc, char *argv[])
 
   for (next= collection; next->name; next++)
   {
+    test_return_t collection_rc= TEST_SUCCESS;
     test_st *run;
+    bool failed= false;
+    bool skipped= false;
 
     run= next->tests;
     if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
       continue;
 
-    fprintf(stderr, "\n%s\n\n", next->name);
+    stats.collection_total++;
+
+    collection_rc= world.collection.startup(world_ptr);
+
+    if (collection_rc != TEST_SUCCESS)
+      goto skip_pre;
+
+    if (next->pre)
+    {
+      collection_rc= world.runner->pre(next->pre, world_ptr);
+    }
+
+skip_pre:
+    switch (collection_rc)
+    {
+      case TEST_SUCCESS:
+        fprintf(stderr, "\n%s\n\n", next->name);
+        break;
+      case TEST_FAILURE:
+        fprintf(stderr, "\n%s [ failed ]\n\n", next->name);
+        stats.collection_failed++;
+        goto cleanup;
+      case TEST_SKIPPED:
+        fprintf(stderr, "\n%s [ skipping ]\n\n", next->name);
+        stats.collection_skipped++;
+        goto cleanup;
+      case TEST_MEMORY_ALLOCATION_FAILURE:
+      case TEST_MAXIMUM_RETURN:
+      default:
+        assert(0);
+        break;
+    }
+
 
     for (x= 0; run->name; run++)
     {
@@ -141,48 +235,54 @@ int main(int argc, char *argv[])
 
       fprintf(stderr, "Testing %s", run->name);
 
-      if (world.collection_startup)
+      if (world.test.startup)
       {
-        world.collection_startup(world_ptr);
+        world.test.startup(world_ptr);
       }
 
-      if (run->requires_flush && world.flush)
+      if (run->requires_flush && world.test.flush)
       {
-        world.flush(world_ptr);
+        world.test.flush(world_ptr);
       }
 
-      if (world.pre_run)
+      if (world.test.pre_run)
       {
-        world.pre_run(world_ptr);
+        world.test.pre_run(world_ptr);
       }
 
 
-      if (next->pre)
+      // Runner code
       {
-        return_code= world.runner->pre(next->pre, world_ptr);
-
-        if (return_code != TEST_SUCCESS)
+#if 0
+        if (next->pre && world.runner->pre)
         {
-          goto error;
+          return_code= world.runner->pre(next->pre, world_ptr);
+
+          if (return_code != TEST_SUCCESS)
+          {
+            goto error;
+          }
         }
-      }
+#endif
 
-      gettimeofday(&start_time, NULL);
-      return_code= world.runner->run(run->test_fn, world_ptr);
-      gettimeofday(&end_time, NULL);
-      load_time= timedif(end_time, start_time);
+        gettimeofday(&start_time, NULL);
+        return_code= world.runner->run(run->test_fn, world_ptr);
+        gettimeofday(&end_time, NULL);
+        load_time= timedif(end_time, start_time);
 
-      if (next->post)
-      {
-        (void) world.runner->pre(next->pre, world_ptr);
+#if 0
+        if (next->post && world.runner->post)
+        {
+          (void) world.runner->post(next->post, world_ptr);
+        }
+#endif
       }
 
-      if (world.post_run)
+      if (world.test.post_run)
       {
-        world.post_run(world_ptr);
+        world.test.post_run(world_ptr);
       }
 
-error:
       stats.total++;
 
       fprintf(stderr, "\t\t\t\t\t");
@@ -195,35 +295,73 @@ error:
         break;
       case TEST_FAILURE:
         stats.failed++;
+        failed= true;
         break;
       case TEST_SKIPPED:
         stats.skipped++;
+        skipped= true;
         break;
       case TEST_MEMORY_ALLOCATION_FAILURE:
+        fprintf(stderr, "Exhausted memory, quitting\n");
+        abort();
       case TEST_MAXIMUM_RETURN:
       default:
+        assert(0); // Coding error.
         break;
       }
 
       fprintf(stderr, "[ %s ]\n", test_strerror(return_code));
 
-      if (world.on_error)
+      if (world.test.on_error)
       {
         test_return_t rc;
-        rc= world.on_error(return_code, world_ptr);
+        rc= world.test.on_error(return_code, world_ptr);
 
         if (rc != TEST_SUCCESS)
           break;
       }
     }
+
+    if (next->post && world.runner->post)
+    {
+      (void) world.runner->post(next->post, world_ptr);
+    }
+
+    if (! failed && ! skipped)
+    {
+      stats.collection_success++;
+    }
+cleanup:
+
+    world.collection.shutdown(world_ptr);
   }
 
-  fprintf(stderr, "All tests completed successfully\n\n");
+  if (stats.collection_failed || stats.collection_skipped)
+  {
+    fprintf(stderr, "Some test failures and/or skipped test occurred.\n\n");
+  }
+  else
+  {
+    fprintf(stderr, "All tests completed successfully\n\n");
+  }
 
   if (world.destroy)
-    world.destroy(world_ptr);
+  {
+    test_return_t error;
+    error= world.destroy(world_ptr);
+
+    if (error != TEST_SUCCESS)
+    {
+      fprintf(stderr, "Failure during shutdown.\n");
+      stats.failed++; // We do this to make our exit code return 1
+    }
+  }
 
   world_stats_print(&stats);
 
+#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
+  sasl_done();
+#endif
+
   return stats.failed == 0 ? 0 : 1;
 }