Update all of libtest around runner.
authorBrian Aker <brian@tangent.org>
Sun, 22 Apr 2012 00:31:31 +0000 (17:31 -0700)
committerBrian Aker <brian@tangent.org>
Sun, 22 Apr 2012 00:31:31 +0000 (17:31 -0700)
21 files changed:
clients/generator.cc
libtest/framework.cc
libtest/framework.h
libtest/main.cc
tests/failure.cc
tests/include.am
tests/libmemcached-1.0/all_tests.cc
tests/libmemcached-1.0/all_tests.h
tests/libmemcached-1.0/all_tests_socket.cc
tests/libmemcached-1.0/atomsmasher.cc
tests/libmemcached-1.0/mem_functions.cc
tests/libmemcached-1.0/plus.cpp
tests/libmemcached-1.0/replication.cc
tests/libmemcached-1.0/sasl.cc
tests/libmemcached_test_container.h [new file with mode: 0644]
tests/libmemcached_world.h
tests/libmemcached_world_socket.h
tests/mem_udp.cc
tests/memc.h [new file with mode: 0644]
tests/replication.h
tests/runner.h

index c62f9a32214cb8af49086781f2cfab0e759a8553..d24ad139bad45f1d5dd601bec9ad454286393ccc 100644 (file)
@@ -53,7 +53,9 @@ void pairs_free(pairs_st *pairs)
   {
     free(pairs[x].key);
     if (pairs[x].value)
+    {
       free(pairs[x].value);
+    }
   }
 
   free(pairs);
index 73e227e429be74b9788a7db0af3248eb28e3f1b0..b9d817b5ab457e9d01178312941a5f56b036046f 100644 (file)
 
 using namespace libtest;
 
-static test_return_t _default_callback(void*)
-{
-  return TEST_SUCCESS;
-}
-
 Framework::Framework() :
   collections(NULL),
   _create(NULL),
   _destroy(NULL),
-  collection_startup(_default_callback),
-  collection_shutdown(_default_callback),
-  _on_error(NULL),
   _runner(NULL),
   _socket(false),
   _creators_ptr(NULL)
@@ -56,46 +48,6 @@ Framework::~Framework()
   delete _runner;
 }
 
-test_return_t Framework::Item::flush(void* arg, test_st* run)
-{
-  if (run->requires_flush and _flush)
-  {
-    return _flush(arg);
-  }
-
-  return TEST_SUCCESS;
-}
-
-test_return_t Framework::on_error(const test_return_t rc, void* arg)
-{
-  if (_on_error and test_failed(_on_error(rc, arg)))
-  {
-    return TEST_FAILURE;
-  }
-
-  return TEST_SUCCESS;
-}
-
-test_return_t Framework::startup(void* arg)
-{
-  if (collection_startup)
-  {
-    return collection_startup(arg);
-  }
-
-  return TEST_SUCCESS;
-}
-
-test_return_t Framework::Item::startup(void* arg)
-{
-  if (_startup)
-  {
-    return _startup(arg);
-  }
-
-  return TEST_SUCCESS;
-}
-
 libtest::Runner *Framework::runner()
 {
   if (_runner == NULL)
index 04dc29da89be2ef5a5c93cab75450fa509c4658f..f4f64070cbb7dbe2afc66bf7f4258612a0df9135 100644 (file)
@@ -37,75 +37,9 @@ public:
   test_callback_create_fn *_create;
   test_callback_destroy_fn *_destroy;
 
-  /* This is called a the beginning of any collection run. */
-  test_callback_fn *collection_startup;
-
-  /* This is called a the end of any collection run. */
-  test_callback_fn *collection_shutdown;
-
-  void set_collection_shutdown(test_callback_error_fn *arg)
-  {
-    _on_error= arg;
-  }
-
 public:
   void* create(test_return_t& arg);
 
-  test_return_t startup(void*);
-
-  test_return_t shutdown(void* arg)
-  {
-    if (collection_shutdown)
-    {
-      return collection_shutdown(arg);
-    }
-
-    return TEST_SUCCESS;
-  }
-
-  /**
-    These are run before/after the test. If implemented. Their execution is not controlled
-    by the test.
-  */
-  class Item {
-  public:
-    /* This is called a the beginning of any run. */
-    test_callback_fn *_startup;
-
-    test_return_t startup(void*);
-
-    /* 
-      This called on a test if the test requires a flush call (the bool is
-      from test_st) 
-    */
-    test_callback_fn *_flush;
-
-  public:
-
-    Item() :
-      _startup(NULL),
-      _flush(NULL)
-    { }
-
-    void set_startup(test_callback_fn *arg)
-    {
-      _startup= arg;
-    }
-
-    void set_collection(test_callback_fn *arg)
-    {
-      _flush= arg;
-    }
-
-    void set_flush(test_callback_fn *arg)
-    {
-      _flush= arg;
-    }
-
-    test_return_t flush(void* arg, test_st* run);
-
-  } item;
-
   /**
     If an error occurs during the test, this is called.
   */
index 697cce1c4782610db3e0eeee3cbeb5735839f4bd..b785fed8474d1499ad5291ae6ac6ea37f69d3f88 100644 (file)
 #include <cassert>
 #include <cstdlib>
 #include <cstring>
+#include <ctime>
+#include <fnmatch.h>
+#include <iostream>
+#include <memory>
+#include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <ctime>
-#include <fnmatch.h>
-#include <iostream>
 
 #include <signal.h>
 
@@ -71,6 +72,44 @@ static long int timedif(struct timeval a, struct timeval b)
   return s + us;
 }
 
+static test_return_t runner_code(Framework* frame,
+                                 test_st* run, 
+                                 void* creators_ptr, 
+                                 long int& load_time)
+{ // Runner Code
+
+  struct timeval start_time, end_time;
+
+  gettimeofday(&start_time, NULL);
+  assert(frame->runner());
+  assert(run->test_fn);
+
+  test_return_t return_code;
+  try 
+  {
+    return_code= frame->runner()->run(run->test_fn, creators_ptr);
+  }
+  // Special case where check for the testing of the exception
+  // system.
+  catch (libtest::fatal &e)
+  {
+    if (fatal::is_disabled())
+    {
+      fatal::increment_disabled_counter();
+      return_code= TEST_SUCCESS;
+    }
+    else
+    {
+      throw;
+    }
+  }
+
+  gettimeofday(&end_time, NULL);
+  load_time= timedif(end_time, start_time);
+
+  return return_code;
+}
+
 #include <getopt.h>
 #include <unistd.h>
 
@@ -195,10 +234,12 @@ int main(int argc, char *argv[])
 
   int exit_code;
 
-  try {
-    do {
+  try 
+  {
+    do
+    {
       exit_code= EXIT_SUCCESS;
-      Framework world;
+      std::auto_ptr<Framework> frame(new Framework);
 
       fatal_assert(sigignore(SIGPIPE) == 0);
 
@@ -211,10 +252,10 @@ int main(int argc, char *argv[])
 
       Stats stats;
 
-      get_world(&world);
+      get_world(frame.get());
 
       test_return_t error;
-      void *creators_ptr= world.create(error);
+      void *creators_ptr= frame->create(error);
 
       switch (error)
       {
@@ -253,11 +294,8 @@ int main(int argc, char *argv[])
         wildcard= argv[2];
       }
 
-      for (collection_st *next= world.collections; next and next->name and (not signal.is_shutdown()); next++)
+      for (collection_st *next= frame->collections; next and next->name and (not signal.is_shutdown()); next++)
       {
-        bool failed= false;
-        bool skipped= false;
-
         if (collection_to_run.empty() == false and fnmatch(collection_to_run.c_str(), next->name, 0))
         {
           continue;
@@ -265,152 +303,111 @@ int main(int argc, char *argv[])
 
         stats.collection_total++;
 
-        test_return_t collection_rc= world.startup(creators_ptr);
-
-        if (collection_rc == TEST_SUCCESS and next->pre)
-        {
-          collection_rc= world.runner()->pre(next->pre, creators_ptr);
-        }
-
-        switch (collection_rc)
-        {
-        case TEST_SUCCESS:
-          break;
-
-        case TEST_FAILURE:
-          Out << next->name << " [ failed ]";
-          failed= true;
-          signal.set_shutdown(SHUTDOWN_GRACEFUL);
-          goto cleanup;
-
-        case TEST_SKIPPED:
-          Out << next->name << " [ skipping ]";
-          skipped= true;
-          goto cleanup;
-
-        default:
-          fatal_message("invalid return code");
-        }
-
-        Out << "Collection: " << next->name;
-
-        for (test_st *run= next->tests; run->name; run++)
+        bool failed= false;
+        bool skipped= false;
+        test_return_t collection_rc;
+        if (test_success(collection_rc= frame->runner()->pre(next->pre, creators_ptr)))
         {
-          struct timeval start_time, end_time;
-          long int load_time= 0;
+          Out << "Collection: " << next->name;
 
-          if (wildcard && fnmatch(wildcard, run->name, 0))
+          for (test_st *run= next->tests; run->name; run++)
           {
-            continue;
-          }
+            long int load_time= 0;
 
-          test_return_t return_code;
-          try {
-            if (test_success(return_code= world.item.startup(creators_ptr)))
+            if (wildcard && fnmatch(wildcard, run->name, 0))
             {
-              if (run->requires_flush)
-              {
-                return_code= world.runner()->flush(creators_ptr);
-              }
+              continue;
+            }
 
-              if (test_success(return_code))
+            test_return_t return_code;
+            try 
+            {
+              if (run->requires_flush)
               {
-                { // Runner Code
-                  gettimeofday(&start_time, NULL);
-                  assert(world.runner());
-                  assert(run->test_fn);
-                  try 
-                  {
-                    return_code= world.runner()->run(run->test_fn, creators_ptr);
-                  }
-                  // Special case where check for the testing of the exception
-                  // system.
-                  catch (libtest::fatal &e)
-                  {
-                    if (fatal::is_disabled())
-                    {
-                      fatal::increment_disabled_counter();
-                      return_code= TEST_SUCCESS;
-                    }
-                    else
-                    {
-                      throw;
-                    }
-                  }
-
-                  gettimeofday(&end_time, NULL);
-                  load_time= timedif(end_time, start_time);
+                if (test_failed(frame->runner()->flush(creators_ptr)))
+                {
+                  Error << "frame->runner()->flush(creators_ptr)";
+                  continue;
                 }
               }
-              else if (return_code == TEST_SKIPPED)
+
+              return_code= runner_code(frame.get(), run, creators_ptr, load_time);
+
+              if (return_code == TEST_SKIPPED)
               { }
               else if (return_code == TEST_FAILURE)
               {
-                Error << " item.flush(failure)";
+#if 0
+                Error << " frame->runner()->run(failure)";
                 signal.set_shutdown(SHUTDOWN_GRACEFUL);
+#endif
               }
+
             }
-            else if (return_code == TEST_SKIPPED)
-            { }
-            else if (return_code == TEST_FAILURE)
+            catch (libtest::fatal &e)
             {
-              Error << " item.startup(failure)";
-              signal.set_shutdown(SHUTDOWN_GRACEFUL);
+              Error << "Fatal exception was thrown: " << e.what();
+              return_code= TEST_FAILURE;
+              throw;
+            }
+            catch (std::exception &e)
+            {
+              Error << "Exception was thrown: " << e.what();
+              return_code= TEST_FAILURE;
+              throw;
+            }
+            catch (...)
+            {
+              Error << "Unknown exception occurred";
+              return_code= TEST_FAILURE;
+              throw;
             }
-          }
-
-          catch (libtest::fatal &e)
-          {
-            Error << "Fatal exception was thrown: " << e.what();
-            return_code= TEST_FAILURE;
-          }
-          catch (std::exception &e)
-          {
-            Error << "Exception was thrown: " << e.what();
-            return_code= TEST_FAILURE;
-          }
-          catch (...)
-          {
-            Error << "Unknown exception occurred";
-            return_code= TEST_FAILURE;
-          }
 
-          stats.total++;
+            stats.total++;
 
-          switch (return_code)
-          {
-          case TEST_SUCCESS:
-            Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
-            stats.success++;
-            break;
-
-          case TEST_FAILURE:
-            stats.failed++;
-            failed= true;
-            Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
-            break;
-
-          case TEST_SKIPPED:
-            stats.skipped++;
-            skipped= true;
-            Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
-            break;
-
-          default:
-            fatal_message("invalid return code");
+            switch (return_code)
+            {
+            case TEST_SUCCESS:
+              Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
+              stats.success++;
+              break;
+
+            case TEST_FAILURE:
+              stats.failed++;
+              failed= true;
+              Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
+              break;
+
+            case TEST_SKIPPED:
+              stats.skipped++;
+              skipped= true;
+              Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
+              break;
+
+            default:
+              fatal_message("invalid return code");
+            }
+#if 0
+            @TODO add code here to allow for a collection to define a method to reset to allow tests to continue.
+#endif
           }
 
-          if (test_failed(world.on_error(return_code, creators_ptr)))
-          {
-            Error << "Failed while running on_error()";
-            signal.set_shutdown(SHUTDOWN_GRACEFUL);
-            break;
-          }
+          (void) frame->runner()->post(next->post, creators_ptr);
+        }
+        else if (collection_rc == TEST_FAILURE)
+        {
+          Out << next->name << " [ failed ]";
+          failed= true;
+#if 0
+          signal.set_shutdown(SHUTDOWN_GRACEFUL);
+#endif
+        }
+        else if (collection_rc == TEST_SKIPPED)
+        {
+          Out << next->name << " [ skipping ]";
+          skipped= true;
         }
 
-        (void) world.runner()->post(next->post, creators_ptr);
-
-cleanup:
         if (failed == false and skipped == false)
         {
           stats.collection_success++;
@@ -426,11 +423,10 @@ cleanup:
           stats.collection_skipped++;
         }
 
-        world.shutdown(creators_ptr);
         Outn();
       }
 
-      if (not signal.is_shutdown())
+      if (signal.is_shutdown() == false)
       {
         signal.set_shutdown(SHUTDOWN_GRACEFUL);
       }
@@ -450,7 +446,7 @@ cleanup:
       {
         Out << "Some tests were skipped.";
       }
-      else if (stats.collection_success and stats.collection_failed == 0)
+      else if (stats.collection_success and (stats.collection_failed == 0))
       {
         Out << "All tests completed successfully.";
       }
index 923cdf3878e63e3a44e85f493516f80ffde3e43f..cfee6cb7783da3142a950ff957a403841368ea0f 100644 (file)
@@ -218,12 +218,6 @@ void get_world(Framework *world)
   world->_create= (test_callback_create_fn*)world_create;
   world->_destroy= (test_callback_destroy_fn*)world_destroy;
 
-  world->item._startup= (test_callback_fn*)world_test_startup;
-  world->_on_error= (test_callback_error_fn*)world_on_error;
-
-  world->collection_startup= (test_callback_fn*)world_container_startup;
-  world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
-
   world->set_runner(new LibmemcachedRunner);
 
   global_framework= world;
index 9e87ca17b2e297e21f31480156bd97982709def4..defca32bef8da810d8674574ee63af2c77594190 100644 (file)
@@ -25,6 +25,7 @@ noinst_HEADERS+= tests/hash_results.h
 noinst_HEADERS+= tests/libmemcached_world.h
 noinst_HEADERS+= tests/libmemcached_world_socket.h
 noinst_HEADERS+= tests/runner.h
+noinst_HEADERS+= tests/libmemcached_test_container.h
 
 # Cycle should always run first
 tests_cycle_CFLAGS= $(AM_CFLAGS) $(NO_CONVERSION) $(NO_STRICT_ALIASING)
index a3615a81a2bb500dad7cd567f913a652bee79ca9..c9536da11ec8818275c5c9338c6d1d108083d047 100644 (file)
@@ -87,12 +87,6 @@ void get_world(Framework *world)
   world->_create= (test_callback_create_fn*)world_create;
   world->_destroy= (test_callback_destroy_fn*)world_destroy;
 
-  world->item._startup= (test_callback_fn*)world_test_startup;
-  world->_on_error= (test_callback_error_fn*)world_on_error;
-
-  world->collection_startup= (test_callback_fn*)world_container_startup;
-  world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
-
   world->set_runner(new LibmemcachedRunner);
 
   world->set_socket();
index 15d6ff28efe804abcf45d0aaab0cb7de63bc3823..6a03b10f971cb3e1468569ad8bfa9f35486db152 100644 (file)
@@ -238,6 +238,7 @@ test_st user_tests[] ={
 };
 
 test_st replication_tests[]= {
+  {"validate replication setup", true, (test_callback_fn*)check_replication_sanity_TEST },
   {"set", true, (test_callback_fn*)replication_set_test },
   {"get", false, (test_callback_fn*)replication_get_test },
   {"mget", false, (test_callback_fn*)replication_mget_test },
index 6037abe1120cd838a57045a166f9bffb23c13ff4..983f38aa212e4151baad6fdd16b45c7dff92df4c 100644 (file)
@@ -76,11 +76,5 @@ void get_world(Framework *world)
   world->_create= (test_callback_create_fn*)world_create;
   world->_destroy= (test_callback_destroy_fn*)world_destroy;
 
-  world->item._startup= (test_callback_fn*)world_test_startup;
-  world->_on_error= (test_callback_error_fn*)world_on_error;
-
-  world->collection_startup= (test_callback_fn*)world_container_startup;
-  world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
-
   world->set_runner(new LibmemcachedRunner);
 }
index 7ca5cbd498374fdf57fb5a32469d6197ab77c9a2..e0872368f28053ea8cb8180057426c225ac2baf2 100644 (file)
@@ -284,11 +284,5 @@ void get_world(Framework *world)
   world->_create= (test_callback_create_fn*)world_create;
   world->_destroy= (test_callback_destroy_fn*)world_destroy;
 
-  world->item._startup= (test_callback_fn*)world_test_startup;
-  world->_on_error= (test_callback_error_fn*)world_on_error;
-
-  world->collection_startup= (test_callback_fn*)world_container_startup;
-  world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
-
   world->set_runner(new LibmemcachedRunner);
 }
index e39c2a8bc8eccc134e4a30462f04637092eb7da4..5c0ada030325a56324fa00811b8d9e5f12d5d276 100644 (file)
@@ -1699,7 +1699,7 @@ test_return_t mget_execute(memcached_st *original_memc)
 }
 
 #define REGRESSION_BINARY_VS_BLOCK_COUNT  20480
-static pairs_st *global_pairs;
+static pairs_st *global_pairs= NULL;
 
 test_return_t key_setup(memcached_st *memc)
 {
@@ -1713,6 +1713,7 @@ test_return_t key_setup(memcached_st *memc)
 test_return_t key_teardown(memcached_st *)
 {
   pairs_free(global_pairs);
+  global_pairs= NULL;
 
   return TEST_SUCCESS;
 }
@@ -1722,9 +1723,14 @@ test_return_t block_add_regression(memcached_st *memc)
   /* First add all of the items.. */
   for (ptrdiff_t x= 0; x < REGRESSION_BINARY_VS_BLOCK_COUNT; ++x)
   {
-    char blob[1024] = {0};
-
-    memcached_return_t rc= memcached_add_by_key(memc, "bob", 3, global_pairs[x].key, global_pairs[x].key_length, blob, sizeof(blob), 0, 0);
+    libtest::vchar_t blob;
+    libtest::vchar::make(blob, 1024);
+
+    memcached_return_t rc= memcached_add_by_key(memc,
+                                                test_literal_param("bob"),
+                                                global_pairs[x].key, global_pairs[x].key_length,
+                                                &blob[0], blob.size(),
+                                                time_t(0), uint32_t(0));
     test_true_got(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE, memcached_strerror(NULL, rc));
   }
 
index d2f89053d934d50e90e912d2fd36d3c90c931437..8539c5e6dc9fe79c1f3f5d7f1f4d0f004198b8d5 100644 (file)
@@ -291,11 +291,5 @@ void get_world(Framework *world)
   world->_create= world_create;
   world->_destroy= world_destroy;
 
-  world->item._startup= reinterpret_cast<test_callback_fn*>(world_test_startup);
-  world->_on_error= reinterpret_cast<test_callback_error_fn*>(world_on_error);
-
-  world->collection_startup= reinterpret_cast<test_callback_fn*>(world_container_startup);
-  world->collection_shutdown= reinterpret_cast<test_callback_fn*>(world_container_shutdown);
-
   world->set_runner(new LibmemcachedRunner);
 }
index 60c62b8504b4382715b21f31bb71263f5c1449f9..da3f82b3b131e0738139b63c40243d48d13eea80 100644 (file)
@@ -45,6 +45,22 @@ using namespace libtest;
 #include <tests/replication.h>
 #include <tests/debug.h>
 
+#include "tests/libmemcached-1.0/setup_and_teardowns.h"
+
+test_return_t check_replication_sanity_TEST(memcached_st *memc)
+{
+  test_true(memc);
+  test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
+
+  /*
+   * Make sure that we store the item on all servers
+   * (master + replicas == number of servers)
+ */
+  test_compare(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS), uint64_t(memcached_server_count(memc) - 1));
+
+  return TEST_SUCCESS;
+}
+
 test_return_t replication_set_test(memcached_st *memc)
 {
   memcached_return_t rc;
@@ -116,8 +132,8 @@ test_return_t replication_get_test(memcached_st *memc)
       uint32_t flags;
       char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
                                       &len, &flags, &rc);
-      test_true(rc == MEMCACHED_SUCCESS);
-      test_true(val != NULL);
+      test_compare(MEMCACHED_SUCCESS, rc);
+      test_true(val);
       free(val);
     }
 
@@ -185,7 +201,7 @@ test_return_t replication_mget_test(memcached_st *memc)
       {
         hits++;
       }
-      test_true(hits == 4);
+      test_compare(4, hits);
       memcached_result_free(&result_obj);
     }
 
index 3a4dfdf1d3535aba4d26753ea414df4d6ced38d8..1c3d4a9947e3238c623c10494fa466ba7214c339 100644 (file)
@@ -106,12 +106,6 @@ void get_world(Framework *world)
   world->_create= (test_callback_create_fn*)world_create;
   world->_destroy= (test_callback_destroy_fn*)world_destroy;
 
-  world->item._startup= (test_callback_fn*)world_test_startup;
-  world->_on_error= (test_callback_error_fn*)world_on_error;
-
-  world->collection_startup= (test_callback_fn*)world_container_startup;
-  world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
-
   world->set_runner(new LibmemcachedRunner);
 
   world->set_sasl("memcached", "memcached");
diff --git a/tests/libmemcached_test_container.h b/tests/libmemcached_test_container.h
new file mode 100644 (file)
index 0000000..19fbc27
--- /dev/null
@@ -0,0 +1,81 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Libmemcached Client and Server 
+ *
+ *  Copyright (C) 2012 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#pragma once
+
+/* The structure we use for the test system */
+struct libmemcached_test_container_st
+{
+private:
+  memcached_st *_parent;
+
+public:
+  libtest::server_startup_st& construct;
+
+  libmemcached_test_container_st(libtest::server_startup_st &construct_arg) :
+    _parent(NULL),
+    construct(construct_arg)
+  { }
+
+  memcached_st* parent()
+  {
+    return _parent;
+  }
+
+  void parent(memcached_st* arg)
+  {
+    reset();
+    _parent= arg;
+  }
+
+  void reset()
+  {
+    if (_parent)
+    {
+      memcached_free(_parent);
+      _parent= NULL;
+    }
+  }
+
+  ~libmemcached_test_container_st()
+  {
+    reset();
+  }
+};
+
+
index 2d05a5d596e78d6a40ddabd874666c3d79843c7e..88329db4a85f595edf4fd083fbedd18ce1f25cc4 100644 (file)
 
 #pragma once
 
-/* The structure we use for the test system */
-struct libmemcached_test_container_st
-{
-  libtest::server_startup_st& construct;
-  memcached_st *parent;
-  memcached_st *memc;
-
-  libmemcached_test_container_st(libtest::server_startup_st &construct_arg) :
-    construct(construct_arg),
-    parent(NULL),
-    memc(NULL)
-  { }
-};
+#include "tests/libmemcached_test_container.h"
 
 static void *world_create(libtest::server_startup_st& servers, test_return_t& error)
 {
@@ -99,66 +87,6 @@ static void *world_create(libtest::server_startup_st& servers, test_return_t& er
   return global_container;
 }
 
-static test_return_t world_container_startup(libmemcached_test_container_st *container)
-{
-  char buffer[BUFSIZ];
-
-  test_compare_got(MEMCACHED_SUCCESS,
-                   libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
-                                                    buffer, sizeof(buffer)),
-                   container->construct.option_string().c_str());
-
-  test_null(container->parent);
-  container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size());
-  test_true(container->parent);
-  test_compare(MEMCACHED_SUCCESS, memcached_version(container->parent));
-
-  if (container->construct.sasl())
-  {
-    if (memcached_failed(memcached_behavior_set(container->parent, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
-    {
-      memcached_free(container->parent);
-      return TEST_FAILURE;
-    }
-
-    if (memcached_failed(memcached_set_sasl_auth_data(container->parent, container->construct.username().c_str(), container->construct.password().c_str())))
-    {
-      memcached_free(container->parent);
-      return TEST_FAILURE;
-    }
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t world_container_shutdown(libmemcached_test_container_st *container)
-{
-  memcached_free(container->parent);
-  container->parent= NULL;
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t world_test_startup(libmemcached_test_container_st *container)
-{
-  test_true(container);
-  test_null(container->memc);
-  test_true(container->parent);
-  container->memc= memcached_clone(NULL, container->parent);
-  test_true(container->memc);
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t world_on_error(test_return_t , libmemcached_test_container_st *container)
-{
-  test_true(container->memc);
-  memcached_free(container->memc);
-  container->memc= NULL;
-
-  return TEST_SUCCESS;
-}
-
 static bool world_destroy(void *object)
 {
   libmemcached_test_container_st *container= (libmemcached_test_container_st *)object;
index cda999516934c7b50873fe5c4a9b65f38f011248..9ea175e37243f6be521cff1f7b5f4a7f36f60d34 100644 (file)
 
 #include <cassert>
 
-/* The structure we use for the test system */
-struct libmemcached_test_container_st
-{
-  libtest::server_startup_st& construct;
-  memcached_st *parent;
-  memcached_st *memc;
-
-  libmemcached_test_container_st(libtest::server_startup_st &construct_arg) :
-    construct(construct_arg),
-    parent(NULL),
-    memc(NULL)
-  { }
-};
+#include "tests/libmemcached_test_container.h"
 
 static void *world_create(libtest::server_startup_st& servers, test_return_t& error)
 {
@@ -80,66 +68,6 @@ static void *world_create(libtest::server_startup_st& servers, test_return_t& er
   return global_container;
 }
 
-static test_return_t world_container_startup(libmemcached_test_container_st *container)
-{
-  char buffer[BUFSIZ];
-
-  test_compare_got(MEMCACHED_SUCCESS,
-                   libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
-                                                    buffer, sizeof(buffer)),
-                   container->construct.option_string().c_str());
-
-  test_null(container->parent);
-  container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size());
-  test_true(container->parent);
-  test_compare(MEMCACHED_SUCCESS, memcached_version(container->parent));
-
-  if (container->construct.sasl())
-  {
-    if (memcached_failed(memcached_behavior_set(container->parent, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
-    {
-      memcached_free(container->parent);
-      return TEST_FAILURE;
-    }
-
-    if (memcached_failed(memcached_set_sasl_auth_data(container->parent, container->construct.username().c_str(), container->construct.password().c_str())))
-    {
-      memcached_free(container->parent);
-      return TEST_FAILURE;
-    }
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t world_container_shutdown(libmemcached_test_container_st *container)
-{
-  memcached_free(container->parent);
-  container->parent= NULL;
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t world_test_startup(libmemcached_test_container_st *container)
-{
-  test_true(container);
-  test_null(container->memc);
-  test_true(container->parent);
-  container->memc= memcached_clone(NULL, container->parent);
-  test_true(container->memc);
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t world_on_error(test_return_t , libmemcached_test_container_st *container)
-{
-  test_true(container->memc);
-  memcached_free(container->memc);
-  container->memc= NULL;
-
-  return TEST_SUCCESS;
-}
-
 static bool world_destroy(void *object)
 {
   libmemcached_test_container_st *container= (libmemcached_test_container_st *)object;
index a07d4166c44915d8866dd56f688830a6a4951291..4a567962ff3399da7616aacce2bb1fe9d58905ef 100644 (file)
@@ -571,11 +571,5 @@ void get_world(Framework *world)
   world->_create= (test_callback_create_fn*)world_create;
   world->_destroy= (test_callback_destroy_fn*)world_destroy;
 
-  world->item._startup= (test_callback_fn*)world_test_startup;
-  world->_on_error= (test_callback_error_fn*)world_on_error;
-
-  world->collection_startup= (test_callback_fn*)world_container_startup;
-  world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
-
   world->set_runner(new LibmemcachedRunner);
 }
diff --git a/tests/memc.h b/tests/memc.h
new file mode 100644 (file)
index 0000000..fbe42c5
--- /dev/null
@@ -0,0 +1,80 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#pragma once
+
+class Memc {
+public:
+  Memc()
+  {
+    _memc= memcached_create(NULL);
+
+    if (_memc == NULL)
+    {
+      throw "memcached_create() failed";
+    }
+  }
+
+  Memc(memcached_st* arg)
+  {
+    _memc= memcached_clone(NULL, arg);
+
+    if (_memc == NULL)
+    {
+      throw "memcached_clone() failed";
+    }
+  }
+
+  memcached_st* operator&() const
+  { 
+    return _memc;
+  }
+
+  memcached_st* operator->() const
+  { 
+    return _memc;
+  }
+
+  ~Memc()
+  {
+    memcached_free(_memc);
+  }
+
+private:
+  memcached_st *_memc;
+
+};
index f287a624e1470f1c50c484d744dc4aff42b4d460..fb9cd88542710a2e49ac809819a7c1d79a723767 100644 (file)
@@ -50,3 +50,5 @@ test_return_t replication_randomize_mget_test(memcached_st *memc);
 test_return_t replication_randomize_mget_fail_test(memcached_st *memc);
 
 test_return_t replication_miss_test(memcached_st *memc);
+
+test_return_t check_replication_sanity_TEST(memcached_st*);
index 6c7eaaff91c07017466362891592ff909cf146ea..a4b1512d62a8e7ebf4dbc7925012e2b2449bf2f5 100644 (file)
@@ -39,6 +39,7 @@
 #pragma once
 
 #include "tests/libmemcached-1.0/generate.h"
+#include "tests/memc.h"
 
 class LibmemcachedRunner : public libtest::Runner {
 public:
@@ -54,9 +55,9 @@ public:
 
   test_return_t flush(libmemcached_test_container_st *container)
   {
-    test_true(container->memc);
-    memcached_flush(container->memc, 0);
-    memcached_quit(container->memc);
+    Memc memc(container->parent());
+    memcached_flush(&memc, 0);
+    memcached_quit(&memc);
 
     return TEST_SUCCESS;
   }
@@ -74,35 +75,66 @@ public:
 private:
   test_return_t _runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
   {
+    test_true(container);
+    test_true(container->parent());
+    Memc memc(container->parent());
+
     test_compare(true, check());
 
+    test_return_t ret= TEST_SUCCESS;
     if (func)
     {
       test_true(container);
-      test_true(container->memc);
-      test_return_t ret;
       try {
-        ret= func(container->memc);
+        ret= func(&memc);
       }
       catch (std::exception& e)
       {
         libtest::Error << e.what();
-        return TEST_FAILURE;
+        ret= TEST_FAILURE;
       }
-
-      return ret;
     }
 
-    return TEST_SUCCESS;
+    return ret;
   }
 
   test_return_t _pre_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
   {
+    container->reset();
+    {
+      char buffer[BUFSIZ];
+
+      test_compare_got(MEMCACHED_SUCCESS,
+                       libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
+                                                        buffer, sizeof(buffer)),
+                       container->construct.option_string().c_str());
+
+      test_null(container->parent());
+      container->parent(memcached(container->construct.option_string().c_str(), container->construct.option_string().size()));
+      test_true(container->parent());
+      test_compare(MEMCACHED_SUCCESS, memcached_version(container->parent()));
+
+      if (container->construct.sasl())
+      {
+        if (memcached_failed(memcached_behavior_set(container->parent(), MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
+        {
+          container->reset();
+          return TEST_FAILURE;
+        }
+
+        if (memcached_failed(memcached_set_sasl_auth_data(container->parent(), container->construct.username().c_str(), container->construct.password().c_str())))
+        {
+          container->reset();
+          return TEST_FAILURE;
+        }
+      }
+    }
+
     test_compare(true, check());
 
     if (func)
     {
-      return func(container->parent);
+      return func(container->parent());
     }
 
     return TEST_SUCCESS;
@@ -113,12 +145,14 @@ private:
     test_compare(true, check());
     cleanup_pairs(NULL);
 
+    test_return_t rc= TEST_SUCCESS;
     if (func)
     {
-      return func(container->parent);
+      rc= func(container->parent());
     }
+    container->reset();
 
-    return TEST_SUCCESS;
+    return rc;
   }
 };