X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Fplus.cpp;h=aed34a4960f4079c03416e99f37f64efcae46a1f;hb=7c8b8a571a25e21046d00cd2ef7897504a5ef939;hp=d3674d6caf8ba53d5186094aaaa010e43eaf108d;hpb=daae2d944f6f7763ec6d69d0016d10efb5fdc0ed;p=awesomized%2Flibmemcached diff --git a/tests/plus.cpp b/tests/plus.cpp index d3674d6c..aed34a49 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -1,102 +1,253 @@ +/* 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.hh" +#include +#include -#include -#include -#include -#include +#include +#include +#include #include #include #include #include -#include -#include "server.h" +#include + +#include +#include -#include "test.h" +using namespace std; +using namespace memcache; +using namespace libtest; extern "C" { - test_return basic_test(memcached_st *memc); - uint8_t increment_test(memcached_st *memc); - test_return basic_master_key_test(memcached_st *memc); - void *world_create(void); - void world_destroy(void *p); + 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); +} + +static void populate_vector(vector &vec, const string &str) +{ + vec.reserve(str.length()); + vec.assign(str.begin(), str.end()); } -test_return basic_test(memcached_st *memc) +static void copy_vec_to_string(vector &vec, string &str) { - Memcached foo(memc); - const char *value_set= "This is some data"; - char *value; - size_t value_length; + str.clear(); + if (not vec.empty()) + { + str.assign(vec.begin(), vec.end()); + } +} + +test_return_t basic_test(memcached_st *memc) +{ + Memcache foo(memc); + const string value_set("This is some data"); + std::vector value; + std::vector test_value; - foo.set("mine", value_set, strlen(value_set)); - value= foo.get("mine", &value_length); + populate_vector(value, value_set); - assert((memcmp(value, value_set, value_length) == 0)); + test_true(foo.set("mine", value, 0, 0)); + test_true(foo.get("mine", test_value)); + + test_memcmp(&test_value[0], &value[0], test_value.size()); + test_false(foo.set("", value, 0, 0)); return TEST_SUCCESS; } -uint8_t increment_test(memcached_st *memc) +test_return_t increment_test(memcached_st *original) { - Memcached mcach(memc); - memcached_return rc; - const char *key= "inctest"; - const char *inc_value= "1"; - char *ret_value; + Memcache mcach(original); + const string key("blah"); + const string inc_value("1"); + std::vector inc_val; + vector ret_value; + string ret_string; uint64_t int_inc_value; uint64_t int_ret_value; - size_t value_length; - mcach.set(key, inc_value, strlen(inc_value)); - ret_value= mcach.get(key, &value_length); - printf("\nretvalue %s\n",ret_value); - int_inc_value= uint64_t(atol(inc_value)); - int_ret_value= uint64_t(atol(ret_value)); - assert(int_ret_value == int_inc_value); + populate_vector(inc_val, inc_value); + + 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); - rc= mcach.increment(key, 1, &int_ret_value); - assert(rc == MEMCACHED_SUCCESS); - assert(int_ret_value == 2); + int_inc_value= uint64_t(atol(inc_value.c_str())); + int_ret_value= uint64_t(atol(ret_string.c_str())); + test_compare(int_inc_value, int_ret_value); - rc= mcach.increment(key, 1, &int_ret_value); - assert(rc == MEMCACHED_SUCCESS); - assert(int_ret_value == 3); + test_true(mcach.increment(key, 1, &int_ret_value)); + test_compare(uint64_t(2), int_ret_value); - rc= mcach.increment(key, 5, &int_ret_value); - assert(rc == MEMCACHED_SUCCESS); - assert(int_ret_value == 8); + test_true(mcach.increment(key, 1, &int_ret_value)); + test_compare(uint64_t(3), int_ret_value); - return 0; + test_true(mcach.increment(key, 5, &int_ret_value)); + test_compare(uint64_t(8), int_ret_value); + + return TEST_SUCCESS; } -test_return basic_master_key_test(memcached_st *memc) +test_return_t basic_master_key_test(memcached_st *original) { - Memcached foo(memc); - const char *value_set= "Data for server A"; - const char *master_key_a= "server-a"; - const char *master_key_b= "server-b"; - const char *key= "xyz"; - char *value; - size_t value_length; + Memcache foo(original); + const string value_set("Data for server A"); + vector value; + vector test_value; + const string master_key_a("server-a"); + const string master_key_b("server-b"); + const string key("xyz"); + + populate_vector(value, value_set); - foo.set_by_key(master_key_a, key, value_set, strlen(value_set)); - value= foo.get_by_key(master_key_a, key, &value_length); + test_true(foo.setByKey(master_key_a, key, value, 0, 0)); + test_true(foo.getByKey(master_key_a, key, test_value)); - assert((memcmp(value, value_set, value_length) == 0)); + test_compare(value.size(), test_value.size()); + test_memcmp(&value[0], &test_value[0], value.size()); - value= foo.get_by_key(master_key_b, key, &value_length); - assert((memcmp(value, value_set, value_length) == 0)); + test_value.clear(); + +#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(context); + + *counter= *counter +1; + + return MEMCACHED_SUCCESS; +} + +test_return_t mget_test(memcached_st *original) +{ + Memcache memc(original); + memcached_return_t mc_rc; + vector keys; + vector< vector *> values; + keys.reserve(3); + keys.push_back("fudge"); + keys.push_back("son"); + keys.push_back("food"); + vector val1; + vector val2; + vector val3; + populate_vector(val1, "fudge"); + populate_vector(val2, "son"); + populate_vector(val3, "food"); + values.reserve(3); + values.push_back(&val1); + values.push_back(&val2); + values.push_back(&val3); + + string return_key; + vector return_value; + + /* We need to empty the server before we continue the test */ + test_true(memc.flush()); + + test_true(memc.mget(keys)); + + test_compare(MEMCACHED_NOTFOUND, + memc.fetch(return_key, return_value)); + + 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))) + { + test_compare(return_key.length(), return_value.size()); + test_memcmp(&return_value[0], return_key.c_str(), return_value.size()); + count++; + } + test_compare(values.size(), count); + + return TEST_SUCCESS; +} + +test_return_t basic_behavior(memcached_st *original) +{ + Memcache memc(original); + uint64_t value= 1; + test_true(memc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value)); + uint64_t behavior= memc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY); + test_compare(behavior, value); + + return TEST_SUCCESS; +} test_st tests[] ={ - {"basic", 0, basic_test }, - {"basic_master_key", 0, basic_master_key_test }, + { "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) }, + { "basic_behavior", 0, + reinterpret_cast(basic_behavior) }, {0, 0, 0} }; @@ -105,35 +256,25 @@ collection_st collection[] ={ {0, 0, 0, 0} }; -#define SERVERS_TO_CREATE 1 +#define SERVERS_TO_CREATE 5 + +#include "libmemcached_world.h" -extern "C" void *world_create(void) +void get_world(Framework *world) { - server_startup_st *construct; + world->collections= collection; - construct= (server_startup_st *)malloc(sizeof(server_startup_st)); - memset(construct, 0, sizeof(server_startup_st)); + world->_create= world_create; + world->_destroy= world_destroy; - construct->count= SERVERS_TO_CREATE; - server_startup(construct); + 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); - return construct; -} + world->collection_startup= reinterpret_cast(world_container_startup); + world->collection_shutdown= reinterpret_cast(world_container_shutdown); -void world_destroy(void *p) -{ - server_startup_st *construct= static_cast(p); - memcached_server_st *servers= - static_cast(construct->servers); - memcached_server_list_free(servers); - - server_shutdown(construct); - free(construct); -} - -void get_world(world_st *world) -{ - world->collections= collection; - world->create= world_create; - world->destroy= world_destroy; + world->set_runner(&defualt_libmemcached_runner); }