+/* 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.hh"
+#include <libmemcached-1.0/memcached.hpp>
+#include <libtest/test.hpp>
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
-#include <time.h>
-#include "server.h"
-
-#include "test.h"
+#include <ctime>
#include <string>
+#include <iostream>
using namespace std;
+using namespace memcache;
+using namespace libtest;
+
+static void populate_vector(vector<char> &vec, const string &str)
+{
+ vec.reserve(str.length());
+ vec.assign(str.begin(), str.end());
+}
-extern "C" {
- void *world_create(void);
- void world_destroy(void *p);
+static void copy_vec_to_string(vector<char> &vec, string &str)
+{
+ str.clear();
+ if (not vec.empty())
+ {
+ str.assign(vec.begin(), vec.end());
+ }
}
-static test_return basic_test(memcached_st *memc)
+static test_return_t basic_test(memcached_st *memc)
{
- Memcached foo(memc);
+ Memcache foo(memc);
const string value_set("This is some data");
- string value;
- size_t value_length;
+ std::vector<char> value;
+ std::vector<char> test_value;
- foo.set("mine", value_set, 0, 0);
- value= foo.get("mine", &value_length);
+ populate_vector(value, value_set);
- assert((memcmp(value.c_str(), value_set.c_str(), 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;
}
-static test_return increment_test(memcached_st *memc)
+static test_return_t increment_test(memcached_st *original)
{
- Memcached mcach(memc);
- bool rc;
- const string key("inctest");
+ Memcache mcach(original);
+ const string key("blah");
const string inc_value("1");
- string ret_value;
+ std::vector<char> inc_val;
+ vector<char> ret_value;
+ string ret_string;
uint64_t int_inc_value;
uint64_t int_ret_value;
- size_t value_length;
- mcach.set(key, inc_value, 0, 0);
- ret_value= mcach.get(key, &value_length);
- printf("\nretvalue %s\n",ret_value.c_str());
+ 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);
+
int_inc_value= uint64_t(atol(inc_value.c_str()));
- int_ret_value= uint64_t(atol(ret_value.c_str()));
- assert(int_ret_value == int_inc_value);
+ 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 == 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;
}
-static test_return basic_master_key_test(memcached_st *memc)
+static test_return_t basic_master_key_test(memcached_st *original)
{
- Memcached foo(memc);
+ Memcache foo(original);
const string value_set("Data for server A");
+ vector<char> value;
+ vector<char> test_value;
const string master_key_a("server-a");
const string master_key_b("server-b");
const string key("xyz");
- string value;
- size_t value_length;
- foo.set_by_key(master_key_a, key, value_set, 0, 0);
- value= foo.get_by_key(master_key_a, key, &value_length);
+ populate_vector(value, value_set);
- assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
+ test_true(foo.setByKey(master_key_a, key, value, 0, 0));
+ test_true(foo.getByKey(master_key_a, key, test_value));
- value= foo.get_by_key(master_key_b, key, &value_length);
- assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
+ test_compare(value.size(), test_value.size());
+ test_memcmp(&value[0], &test_value[0], value.size());
- return TEST_SUCCESS;
-}
-
-/* Count the results */
-static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)),
- memcached_result_st *result __attribute__((unused)),
- void *context)
-{
- unsigned int *counter= static_cast<unsigned int *>(context);
+ test_value.clear();
- *counter= *counter + 1;
+#if 0
+ test_false(foo.getByKey(master_key_b, key, test_value));
+ test_zero(test_value.size());
+#endif
- return MEMCACHED_SUCCESS;
+ return TEST_SUCCESS;
}
-static test_return mget_result_function(memcached_st *memc)
+static test_return_t mget_test(memcached_st *original)
{
- Memcached mc(memc);
- bool rc;
- string key1("fudge");
- string key2("son");
- string key3("food");
+ Memcache memc(original);
+ memcached_return_t mc_rc;
vector<string> keys;
+ vector< vector<char> *> values;
keys.reserve(3);
- keys.push_back(key1);
- keys.push_back(key2);
- keys.push_back(key3);
- unsigned int counter;
- memcached_execute_function callbacks[1];
+ keys.push_back("fudge");
+ keys.push_back("son");
+ keys.push_back("food");
+ vector<char> val1;
+ vector<char> val2;
+ vector<char> 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<char> return_value;
/* We need to empty the server before we continue the test */
- rc= mc.flush(0);
- rc= mc.set_all(keys, keys, 50, 9);
- assert(rc == true);
+ test_true(memc.flush());
- rc= mc.mget(keys);
- assert(rc == true);
+ test_true(memc.mget(keys));
- callbacks[0]= &callback_counter;
- counter= 0;
- rc= mc.fetch_execute(callbacks, static_cast<void *>(&counter), 1);
+ test_compare(MEMCACHED_NOTFOUND,
+ memc.fetch(return_key, return_value));
- assert(counter == 3);
+ 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;
}
-static test_return mget_test(memcached_st *memc)
+static test_return_t basic_behavior(memcached_st *original)
{
- Memcached mc(memc);
- bool rc;
- memcached_return mc_rc;
- vector<string> keys;
- keys.reserve(3);
- keys.push_back("fudge");
- keys.push_back("son");
- keys.push_back("food");
- uint32_t flags;
+ Memcache memc(original);
+ test_true(memc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, true));
+ test_compare(true, memc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY));
- string return_key;
- size_t return_key_length;
- string return_value;
- size_t return_value_length;
+ return TEST_SUCCESS;
+}
- /* We need to empty the server before we continue the test */
- rc= mc.flush(0);
- assert(rc == true);
+static test_return_t error_test(memcached_st *)
+{
+ Memcache memc("--server=localhost:178");
+ std::vector<char> value;
- rc= mc.mget(keys);
- assert(rc == true);
+ test_false(memc.set("key", value, time_t(0), uint32_t(0)));
- while (mc.fetch(return_key, return_value, &return_key_length,
- &return_value_length, &flags, &mc_rc))
- {
- assert(return_value.length() != 0);
- }
- assert(return_value_length == 0);
- assert(mc_rc == MEMCACHED_END);
+ test_true(memc.error());
+
+ return TEST_SUCCESS;
+}
- rc= mc.set_all(keys, keys, 50, 9);
- assert(rc == true);
+static test_return_t error_std_string_test(memcached_st *)
+{
+ Memcache memc("--server=localhost:178");
+ std::vector<char> value;
- rc= mc.mget(keys);
- assert(rc == true);
+ test_false(memc.set("key", value, time_t(0), uint32_t(0)));
- while ((mc.fetch(return_key, return_value, &return_key_length,
- &return_value_length, &flags, &mc_rc)))
- {
- assert(return_value.length() != 0);
- assert(mc_rc == MEMCACHED_SUCCESS);
- assert(return_key_length == return_value_length);
- assert(!memcmp(return_value.c_str(), return_key.c_str(), return_value_length));
- }
+ 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<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;
+}
+
+test_st error_tests[] ={
+ { "error()", false, reinterpret_cast<test_callback_fn*>(error_test) },
+ { "error(std::string&)", false, reinterpret_cast<test_callback_fn*>(error_std_string_test) },
+ { "error(memcached_return_t&)", false, reinterpret_cast<test_callback_fn*>(error_memcached_return_t_test) },
+ {0, 0, 0}
+};
+
test_st tests[] ={
- { "basic", 0, basic_test },
- { "basic_master_key", 0, basic_master_key_test },
- { "increment_test", 0, increment_test },
- { "mget", 1, mget_test },
- { "mget_result_function", 1, mget_result_function },
+ { "basic", false,
+ reinterpret_cast<test_callback_fn*>(basic_test) },
+ { "basic_master_key", false,
+ reinterpret_cast<test_callback_fn*>(basic_master_key_test) },
+ { "increment_test", false,
+ reinterpret_cast<test_callback_fn*>(increment_test) },
+ { "mget", true,
+ reinterpret_cast<test_callback_fn*>(mget_test) },
+ { "basic_behavior", false,
+ 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 1
-
-extern "C" void *world_create(void)
-{
- server_startup_st *construct;
+#define SERVERS_TO_CREATE 5
- construct= (server_startup_st *)malloc(sizeof(server_startup_st));
- memset(construct, 0, sizeof(server_startup_st));
+#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
+#include "libmemcached_world.h"
- construct->count= SERVERS_TO_CREATE;
- server_startup(construct);
+void get_world(Framework *world)
+{
+ world->collections= collection;
- return construct;
-}
+ world->_create= world_create;
+ world->_destroy= world_destroy;
-void world_destroy(void *p)
-{
- server_startup_st *construct= static_cast<server_startup_st *>(p);
- memcached_server_st *servers=
- static_cast<memcached_server_st *>(construct->servers);
- memcached_server_list_free(servers);
+ 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);
- server_shutdown(construct);
- free(construct);
-}
+ world->collection_startup= reinterpret_cast<test_callback_fn*>(world_container_startup);
+ world->collection_shutdown= reinterpret_cast<test_callback_fn*>(world_container_shutdown);
-void get_world(world_st *world)
-{
- world->collections= collection;
- world->create= world_create;
- world->destroy= world_destroy;
+ world->set_runner(&defualt_libmemcached_runner);
}