This patch creates a couple of tests around how autoeject works.
[m6w6/libmemcached] / tests / plus.cpp
index 59ebf555def8193c44df885be73403de1c4eb429..aad1abf1f3060aee78253ebf5b8885a9d498d172 100644 (file)
@@ -1,7 +1,47 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Libmemcached library
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2006-2009 Brian Aker 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.
+ *
+ */
+
+#include <config.h>
+
 /*
   C++ interface test
 */
 #include <libmemcached/memcached.hpp>
+#include <libtest/test.hpp>
 
 #include <cstdio>
 #include <cstdlib>
 #include <unistd.h>
 #include <ctime>
 
-#include <libtest/server.h>
-
-#include <libtest/test.h>
-
 #include <string>
 #include <iostream>
 
 using namespace std;
 using namespace memcache;
-
-extern "C" {
-   test_return_t basic_test(memcached_st *memc);
-   test_return_t increment_test(memcached_st *memc);
-   test_return_t basic_master_key_test(memcached_st *memc);
-   test_return_t mget_result_function(memcached_st *memc);
-   test_return_t basic_behavior(memcached_st *memc);
-   test_return_t mget_test(memcached_st *memc);
-   memcached_return_t callback_counter(const memcached_st *,
-                                       memcached_result_st *,
-                                       void *context);
-}
+using namespace libtest;
 
 static void populate_vector(vector<char> &vec, const string &str)
 {
@@ -49,7 +74,7 @@ static void copy_vec_to_string(vector<char> &vec, string &str)
   }
 }
 
-test_return_t basic_test(memcached_st *memc)
+static test_return_t basic_test(memcached_st *memc)
 {
   Memcache foo(memc);
   const string value_set("This is some data");
@@ -67,7 +92,7 @@ test_return_t basic_test(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
-test_return_t increment_test(memcached_st *original)
+static test_return_t increment_test(memcached_st *original)
 {
   Memcache mcach(original);
   const string key("blah");
@@ -91,18 +116,18 @@ test_return_t increment_test(memcached_st *original)
   test_compare(int_inc_value, int_ret_value);
 
   test_true(mcach.increment(key, 1, &int_ret_value));
-  test_compare(2, int_ret_value);
+  test_compare(uint64_t(2), int_ret_value);
 
   test_true(mcach.increment(key, 1, &int_ret_value));
-  test_compare(3, int_ret_value);
+  test_compare(uint64_t(3), int_ret_value);
 
   test_true(mcach.increment(key, 5, &int_ret_value));
-  test_compare(8, int_ret_value);
+  test_compare(uint64_t(8), int_ret_value);
 
   return TEST_SUCCESS;
 }
 
-test_return_t basic_master_key_test(memcached_st *original)
+static test_return_t basic_master_key_test(memcached_st *original)
 {
   Memcache foo(original);
   const string value_set("Data for server A");
@@ -114,32 +139,23 @@ test_return_t basic_master_key_test(memcached_st *original)
 
   populate_vector(value, value_set);
 
-  foo.setByKey(master_key_a, key, value, 0, 0);
-  foo.getByKey(master_key_a, key, test_value);
+  test_true(foo.setByKey(master_key_a, key, value, 0, 0));
+  test_true(foo.getByKey(master_key_a, key, test_value));
 
-  test_true((memcmp(&value[0], &test_value[0], value.size()) == 0));
+  test_compare(value.size(), test_value.size());
+  test_memcmp(&value[0], &test_value[0], value.size());
 
   test_value.clear();
 
-  foo.getByKey(master_key_b, key, test_value);
-  test_true((memcmp(&value[0], &test_value[0], value.size()) == 0));
+#if 0
+  test_false(foo.getByKey(master_key_b, key, test_value));
+  test_zero(test_value.size());
+#endif
 
   return TEST_SUCCESS;
 }
 
-/* Count the results */
-memcached_return_t callback_counter(const memcached_st *,
-                                    memcached_result_st *,
-                                    void *context)
-{
-  unsigned int *counter= static_cast<unsigned int *>(context);
-
-  *counter= *counter + 1;
-
-  return MEMCACHED_SUCCESS;
-}
-
-test_return_t mget_test(memcached_st *original)
+static test_return_t mget_test(memcached_st *original)
 {
   Memcache memc(original);
   memcached_return_t mc_rc;
@@ -175,7 +191,7 @@ test_return_t mget_test(memcached_st *original)
 
   test_true(memc.mget(keys));
   size_t count= 0;
-  while ((mc_rc= memc.fetch(return_key, return_value)) == MEMCACHED_SUCCESS)
+  while (memcached_success(mc_rc= memc.fetch(return_key, return_value)))
   {
     test_compare(return_key.length(), return_value.size());
     test_memcmp(&return_value[0], return_key.c_str(), return_value.size());
@@ -186,7 +202,7 @@ test_return_t mget_test(memcached_st *original)
   return TEST_SUCCESS;
 }
 
-test_return_t basic_behavior(memcached_st *original)
+static test_return_t basic_behavior(memcached_st *original)
 {
   Memcache memc(original);
   uint64_t value= 1;
@@ -197,44 +213,97 @@ test_return_t basic_behavior(memcached_st *original)
   return TEST_SUCCESS;
 }
 
+static test_return_t error_test(memcached_st *)
+{
+  Memcache memc("--server=localhost:0");
+  std::vector<char> value;
+
+  test_false(memc.set("key", value, time_t(0), uint32_t(0)));
+
+  test_true(memc.error());
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t error_std_string_test(memcached_st *)
+{
+  Memcache memc("--server=localhost:0");
+  std::vector<char> value;
+
+  test_false(memc.set("key", value, time_t(0), uint32_t(0)));
+
+  std::string error_message;
+  test_true(memc.error(error_message));
+  test_false(error_message.empty());
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t error_memcached_return_t_test(memcached_st *)
+{
+  Memcache memc("--server=localhost:0");
+  std::vector<char> value;
+
+  test_false(memc.set("key", value, time_t(0), uint32_t(0)));
+
+  memcached_return_t rc;
+  test_true(memc.error(rc));
+  test_compare(MEMCACHED_CONNECTION_FAILURE, rc);
+
+  return TEST_SUCCESS;
+}
+
+#ifndef __INTEL_COMPILER
+#pragma GCC diagnostic ignored "-fpermissive"
+#endif
+
+test_st error_tests[] ={
+  { "error()", 0, error_test },
+  { "error(std::string&)", 0, error_std_string_test },
+  { "error(memcached_return_t&)", 0, error_memcached_return_t_test },
+  {0, 0, 0}
+};
+
 test_st tests[] ={
   { "basic", 0,
-    reinterpret_cast<test_callback_fn>(basic_test) },
+    reinterpret_cast<test_callback_fn*>(basic_test) },
   { "basic_master_key", 0,
-    reinterpret_cast<test_callback_fn>(basic_master_key_test) },
+    reinterpret_cast<test_callback_fn*>(basic_master_key_test) },
   { "increment_test", 0,
-    reinterpret_cast<test_callback_fn>(increment_test) },
+    reinterpret_cast<test_callback_fn*>(increment_test) },
   { "mget", 1,
-    reinterpret_cast<test_callback_fn>(mget_test) },
+    reinterpret_cast<test_callback_fn*>(mget_test) },
   { "basic_behavior", 0,
-    reinterpret_cast<test_callback_fn>(basic_behavior) },
+    reinterpret_cast<test_callback_fn*>(basic_behavior) },
   {0, 0, 0}
 };
 
 collection_st collection[] ={
   {"block", 0, 0, tests},
+  {"error()", 0, 0, error_tests},
   {0, 0, 0, 0}
 };
 
 #define SERVERS_TO_CREATE 5
 
+#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
 #include "libmemcached_world.h"
 
-void get_world(world_st *world)
+void get_world(Framework *world)
 {
   world->collections= collection;
 
-  world->create= reinterpret_cast<test_callback_create_fn>(world_create);
-  world->destroy= reinterpret_cast<test_callback_fn>(world_destroy);
+  world->_create= world_create;
+  world->_destroy= world_destroy;
 
-  world->test.startup= reinterpret_cast<test_callback_fn>(world_test_startup);
-  world->test.flush= reinterpret_cast<test_callback_fn>(world_flush);
-  world->test.pre_run= reinterpret_cast<test_callback_fn>(world_pre_run);
-  world->test.post_run= reinterpret_cast<test_callback_fn>(world_post_run);
-  world->test.on_error= reinterpret_cast<test_callback_error_fn>(world_on_error);
+  world->item._startup= reinterpret_cast<test_callback_fn*>(world_test_startup);
+  world->item._flush= reinterpret_cast<test_callback_fn*>(world_flush);
+  world->item.set_pre(reinterpret_cast<test_callback_fn*>(world_pre_run));
+  world->item.set_post(reinterpret_cast<test_callback_fn*>(world_post_run));
+  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->collection_startup= reinterpret_cast<test_callback_fn*>(world_container_startup);
+  world->collection_shutdown= reinterpret_cast<test_callback_fn*>(world_container_shutdown);
 
-  world->runner= &defualt_libmemcached_runner;
+  world->set_runner(&defualt_libmemcached_runner);
 }