X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Fplus.cpp;h=7de8164f7db8868df15b8a6a04361db52a2dc84f;hb=5cb5e0d2dacc49ea143b8e059b0f90251d0e2c23;hp=0d588749c0404d3aa8ef90320c380aaeb94df0a2;hpb=a4456cc10079f2e6f648befc91657f2723c825e5;p=m6w6%2Flibmemcached diff --git a/tests/plus.cpp b/tests/plus.cpp index 0d588749..7de8164f 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -1,38 +1,63 @@ +/* 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 + /* C++ interface test */ -#include "libmemcached/memcached.hpp" +#include +#include -#include -#include -#include -#include +#include +#include +#include #include #include #include #include -#include -#include "server.h" - -#include "test.h" +#include #include #include 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 &vec, const string &str) { @@ -43,13 +68,13 @@ static void populate_vector(vector &vec, const string &str) static void copy_vec_to_string(vector &vec, string &str) { str.clear(); - if (! vec.empty()) + if (not vec.empty()) { str.assign(vec.begin(), vec.end()); } } -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"); @@ -58,31 +83,19 @@ test_return_t basic_test(memcached_st *memc) populate_vector(value, value_set); - foo.set("mine", value, 0, 0); - foo.get("mine", test_value); - - assert((memcmp(&test_value[0], &value[0], test_value.size()) == 0)); + test_true(foo.set("mine", value, 0, 0)); + test_true(foo.get("mine", test_value)); - /* - * Simple test of the exceptions here...this should throw an exception - * saying that the key is empty. - */ - try - { - foo.set("", value, 0, 0); - } - catch (Error &err) - { - return TEST_SUCCESS; - } + test_compare(test_value.size(), value.size()); + test_memcmp(&test_value[0], &value[0], test_value.size()); + test_false(foo.set("", value, 0, 0)); - return TEST_FAILURE; + return TEST_SUCCESS; } -test_return_t increment_test(memcached_st *memc) +static test_return_t increment_test(memcached_st *original) { - Memcache mcach(memc); - bool rc; + Memcache mcach(original); const string key("blah"); const string inc_value("1"); std::vector inc_val; @@ -93,40 +106,31 @@ test_return_t increment_test(memcached_st *memc) populate_vector(inc_val, inc_value); - rc= mcach.set(key, inc_val, 0, 0); - if (rc == false) - { - return TEST_FAILURE; - } - mcach.get(key, ret_value); - if (ret_value.empty()) - { - return TEST_FAILURE; - } + test_true(mcach.set(key, inc_val, 0, 0)); + + test_true(mcach.get(key, ret_value)); + test_false(ret_value.empty()); copy_vec_to_string(ret_value, ret_string); int_inc_value= uint64_t(atol(inc_value.c_str())); int_ret_value= uint64_t(atol(ret_string.c_str())); - assert(int_ret_value == int_inc_value); + test_compare(int_inc_value, int_ret_value); - rc= mcach.increment(key, 1, &int_ret_value); - assert(rc == true); - assert(int_ret_value == 2); + test_true(mcach.increment(key, 1, &int_ret_value)); + test_compare(uint64_t(2), int_ret_value); - rc= mcach.increment(key, 1, &int_ret_value); - assert(rc == true); - assert(int_ret_value == 3); + test_true(mcach.increment(key, 1, &int_ret_value)); + test_compare(uint64_t(3), int_ret_value); - rc= mcach.increment(key, 5, &int_ret_value); - assert(rc == true); - assert(int_ret_value == 8); + test_true(mcach.increment(key, 5, &int_ret_value)); + test_compare(uint64_t(8), int_ret_value); return TEST_SUCCESS; } -test_return_t basic_master_key_test(memcached_st *memc) +static test_return_t basic_master_key_test(memcached_st *original) { - Memcache foo(memc); + Memcache foo(original); const string value_set("Data for server A"); vector value; vector test_value; @@ -136,78 +140,25 @@ test_return_t basic_master_key_test(memcached_st *memc) 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)); - assert((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); - assert((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) +static test_return_t mget_test(memcached_st *original) { - unsigned int *counter= static_cast(context); - - *counter= *counter + 1; - - return MEMCACHED_SUCCESS; -} - -test_return_t mget_result_function(memcached_st *memc) -{ - Memcache mc(memc); - bool rc; - string key1("fudge"); - string key2("son"); - string key3("food"); - vector keys; - vector< vector *> values; - vector val1; - vector val2; - vector val3; - populate_vector(val1, key1); - populate_vector(val2, key2); - populate_vector(val3, key3); - keys.reserve(3); - keys.push_back(key1); - keys.push_back(key2); - keys.push_back(key3); - values.reserve(3); - values.push_back(&val1); - values.push_back(&val2); - values.push_back(&val3); - unsigned int counter; - memcached_execute_fn callbacks[1]; - - /* We need to empty the server before we continue the test */ - rc= mc.flush(0); - rc= mc.setAll(keys, values, 50, 9); - assert(rc == true); - - rc= mc.mget(keys); - assert(rc == true); - - callbacks[0]= &callback_counter; - counter= 0; - rc= mc.fetchExecute(callbacks, static_cast(&counter), 1); - - assert(counter == 3); - - return TEST_SUCCESS; -} - -test_return_t mget_test(memcached_st *memc) -{ - Memcache mc(memc); - bool rc; + Memcache memc(original); memcached_return_t mc_rc; vector keys; vector< vector *> values; @@ -230,87 +181,124 @@ test_return_t mget_test(memcached_st *memc) vector return_value; /* We need to empty the server before we continue the test */ - rc= mc.flush(0); - assert(rc == true); + test_true(memc.flush()); + + test_true(memc.mget(keys)); - rc= mc.mget(keys); - assert(rc == true); + test_compare(MEMCACHED_NOTFOUND, + memc.fetch(return_key, return_value)); - while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END) + test_true(memc.setAll(keys, values, 50, 9)); + + test_true(memc.mget(keys)); + size_t count= 0; + while (memcached_success(mc_rc= memc.fetch(return_key, return_value))) { - assert(return_value.size() != 0); - return_value.clear(); + test_compare(return_key.length(), return_value.size()); + test_memcmp(&return_value[0], return_key.c_str(), return_value.size()); + count++; } - assert(mc_rc == MEMCACHED_END); + test_compare(values.size(), count); - rc= mc.setAll(keys, values, 50, 9); - assert(rc == true); + return TEST_SUCCESS; +} - rc= mc.mget(keys); - assert(rc == true); +static test_return_t basic_behavior(memcached_st *original) +{ + Memcache memc(original); + test_true(memc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, true)); + test_compare(true, memc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY)); - while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END) - { - assert(return_key.length() == return_value.size()); - assert(!memcmp(&return_value[0], return_key.c_str(), return_value.size())); - } + return TEST_SUCCESS; +} + +static test_return_t error_test(memcached_st *) +{ + Memcache memc("--server=localhost:178"); + std::vector value; + + test_false(memc.set("key", value, time_t(0), uint32_t(0))); + + test_true(memc.error()); return TEST_SUCCESS; } -test_return_t basic_behavior(memcached_st *memc) +static test_return_t error_std_string_test(memcached_st *) { - Memcache mc(memc); - bool rc; - uint64_t value = 1; - rc = mc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value); - assert(rc); - uint64_t behavior = mc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY); - assert(behavior == value); + Memcache memc("--server=localhost:178"); + std::vector 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:178"); + std::vector 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; +} + +test_st error_tests[] ={ + { "error()", false, reinterpret_cast(error_test) }, + { "error(std::string&)", false, reinterpret_cast(error_std_string_test) }, + { "error(memcached_return_t&)", false, reinterpret_cast(error_memcached_return_t_test) }, + {0, 0, 0} +}; + test_st tests[] ={ - { "basic", 0, - reinterpret_cast(basic_test) }, - { "basic_master_key", 0, - reinterpret_cast(basic_master_key_test) }, - { "increment_test", 0, - reinterpret_cast(increment_test) }, - { "mget", 1, - reinterpret_cast(mget_test) }, - { "mget_result_function", 1, - reinterpret_cast(mget_result_function) }, - { "basic_behavior", 0, - reinterpret_cast(basic_behavior) }, + { "basic", false, + reinterpret_cast(basic_test) }, + { "basic_master_key", false, + reinterpret_cast(basic_master_key_test) }, + { "increment_test", false, + reinterpret_cast(increment_test) }, + { "mget", true, + reinterpret_cast(mget_test) }, + { "basic_behavior", false, + reinterpret_cast(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(world_create); - world->destroy= reinterpret_cast(world_destroy); + world->_create= world_create; + world->_destroy= world_destroy; - world->test.startup= reinterpret_cast(world_test_startup); - world->test.flush= reinterpret_cast(world_flush); - world->test.pre_run= reinterpret_cast(world_pre_run); - world->test.post_run= reinterpret_cast(world_post_run); - world->test.on_error= reinterpret_cast(world_on_error); + world->item._startup= reinterpret_cast(world_test_startup); + world->item._flush= reinterpret_cast(world_flush); + world->item.set_pre(reinterpret_cast(world_pre_run)); + world->item.set_post(reinterpret_cast(world_post_run)); + world->_on_error= reinterpret_cast(world_on_error); - world->collection.startup= reinterpret_cast(world_container_startup); - world->collection.shutdown= reinterpret_cast(world_container_shutdown); + world->collection_startup= reinterpret_cast(world_container_startup); + world->collection_shutdown= reinterpret_cast(world_container_shutdown); - world->runner= &defualt_libmemcached_runner; + world->set_runner(&defualt_libmemcached_runner); }