Additional cleanup for config.h
[m6w6/libmemcached] / tests / test.c
index ff6bca680966e1125041e57a6efa737ba415a1b8..215218c5f068e5a3f9bfc26c26a419d03bf9cd72 100644 (file)
@@ -9,6 +9,9 @@
 /*
   Sample test application.
 */
+
+#include "config.h"
+
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -21,6 +24,7 @@
 #include <fnmatch.h>
 #include <stdint.h>
 
+#include "libmemcached/memcached.h"
 #include "test.h"
 
 static void world_stats_print(world_stats_st *stats)
@@ -37,7 +41,7 @@ static void world_stats_print(world_stats_st *stats)
   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)
 {
   long us, s;
 
@@ -105,6 +109,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[])
 {
@@ -119,16 +151,19 @@ 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)
   {
@@ -161,11 +196,17 @@ int main(int argc, char *argv[])
 
     stats.collection_total++;
 
-    if (world.collection_startup)
+    collection_rc= world.collection.startup(world_ptr);
+
+    if (collection_rc != TEST_SUCCESS)
+      goto skip_pre;
+
+    if (next->pre)
     {
-      collection_rc= world.test_startup(world_ptr);
+      collection_rc= world.runner->pre(next->pre, world_ptr);
     }
 
+skip_pre:
     switch (collection_rc)
     {
       case TEST_SUCCESS:
@@ -173,12 +214,12 @@ int main(int argc, char *argv[])
         break;
       case TEST_FAILURE:
         fprintf(stderr, "\n%s [ failed ]\n\n", next->name);
-        stats.failed++;
-        continue;
+        stats.collection_failed++;
+        goto cleanup;
       case TEST_SKIPPED:
         fprintf(stderr, "\n%s [ skipping ]\n\n", next->name);
-        stats.skipped++;
-        continue;
+        stats.collection_skipped++;
+        goto cleanup;
       case TEST_MEMORY_ALLOCATION_FAILURE:
       case TEST_MAXIMUM_RETURN:
       default:
@@ -186,6 +227,7 @@ int main(int argc, char *argv[])
         break;
     }
 
+
     for (x= 0; run->name; run++)
     {
       struct timeval start_time, end_time;
@@ -196,48 +238,54 @@ int main(int argc, char *argv[])
 
       fprintf(stderr, "Testing %s", run->name);
 
-      if (world.test_startup)
+      if (world.test.startup)
       {
-        world.test_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 && world.runner->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 && world.runner->post)
-      {
-        (void) world.runner->post(next->post, 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");
@@ -267,35 +315,28 @@ error:
 
       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 (failed)
-    {
-      stats.collection_failed++;
-    }
-
-    if (skipped)
+    if (next->post && world.runner->post)
     {
-      stats.collection_skipped++;
+      (void) world.runner->post(next->post, world_ptr);
     }
 
     if (! failed && ! skipped)
     {
       stats.collection_success++;
     }
+cleanup:
 
-    if (world.collection_shutdown)
-    {
-      world.collection_shutdown(world_ptr);
-    }
+    world.collection.shutdown(world_ptr);
   }
 
   if (stats.collection_failed || stats.collection_skipped)
@@ -321,5 +362,9 @@ error:
 
   world_stats_print(&stats);
 
+#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
+  sasl_done();
+#endif
+
   return stats.failed == 0 ? 0 : 1;
 }