From: Brian Aker Date: Mon, 15 Aug 2011 22:14:05 +0000 (-0700) Subject: Add a method for checking error/returning error. X-Git-Tag: 0.52~25 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=03b30575c8531cf049bdfbeb993b0e973575aada;p=awesomized%2Flibmemcached Add a method for checking error/returning error. --- diff --git a/libmemcached/initialize_query.cc b/libmemcached/initialize_query.cc index 00e8e642..027b1566 100644 --- a/libmemcached/initialize_query.cc +++ b/libmemcached/initialize_query.cc @@ -39,7 +39,9 @@ memcached_return_t initialize_query(memcached_st *self) { if (not self) + { return MEMCACHED_INVALID_ARGUMENTS; + } self->query_id++; @@ -59,7 +61,9 @@ memcached_return_t initialize_query(memcached_st *self) memcached_return_t initialize_const_query(const memcached_st *self) { if (not self) + { return MEMCACHED_INVALID_ARGUMENTS; + } if (memcached_server_count(self) == 0) { diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index 58fff4df..8c5ca934 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -98,6 +98,32 @@ public: return memcached_strerror(NULL, rc); } + bool error(std::string& error_message) const + { + if (memcached_failed(memcached_last_error(memc))) + { + error_message+= memcached_last_error_message(memc); + return true; + } + + return false; + } + + bool error() const + { + if (memcached_failed(memcached_last_error(memc))) + { + return true; + } + + return false; + } + + bool error(memcached_return_t& arg) const + { + arg= memcached_last_error(memc); + return memcached_failed(arg); + } bool setBehavior(memcached_behavior_t flag, uint64_t data) { @@ -330,7 +356,7 @@ public: key.c_str(), key.length(), &value[0], value.size(), expiration, flags); - return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); + return memcached_success(rc); } /** diff --git a/libmemcached/stats.cc b/libmemcached/stats.cc index 05ceb0f2..d4d8b33b 100644 --- a/libmemcached/stats.cc +++ b/libmemcached/stats.cc @@ -462,21 +462,25 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_return_t *error) { + memcached_return_t unused; + if (error == NULL) + { + error= &unused; + } + memcached_return_t rc; if (memcached_failed(rc= initialize_query(self))) { - if (error) - *error= rc; + *error= rc; return NULL; } WATCHPOINT_ASSERT(error); - unlikely (self->flags.use_udp) + if (self->flags.use_udp) { - if (error) - *error= MEMCACHED_NOT_SUPPORTED; + *error= memcached_set_error(*self, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); return NULL; } @@ -485,8 +489,7 @@ memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_retu if (not stats) { - if (error) - *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; return NULL; } @@ -521,8 +524,7 @@ memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_retu } } - if (error) - *error= rc; + *error= rc; return stats; } diff --git a/libmemcached/storage.cc b/libmemcached/storage.cc index 0fc6e234..d9b5986d 100644 --- a/libmemcached/storage.cc +++ b/libmemcached/storage.cc @@ -355,8 +355,6 @@ static inline memcached_return_t memcached_send(memcached_st *ptr, uint64_t cas, memcached_storage_action_t verb) { - WATCHPOINT_ASSERT(!(value == NULL && value_length > 0)); - memcached_return_t rc; if (memcached_failed(rc= initialize_query(ptr))) { diff --git a/tests/plus.cpp b/tests/plus.cpp index 3bc85bc1..aad1abf1 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -59,18 +59,6 @@ using namespace std; using namespace memcache; using namespace libtest; -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); -} - static void populate_vector(vector &vec, const string &str) { vec.reserve(str.length()); @@ -86,7 +74,7 @@ static void copy_vec_to_string(vector &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"); @@ -104,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"); @@ -139,7 +127,7 @@ test_return_t increment_test(memcached_st *original) 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"); @@ -167,19 +155,7 @@ test_return_t basic_master_key_test(memcached_st *original) 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) +static test_return_t mget_test(memcached_st *original) { Memcache memc(original); memcached_return_t mc_rc; @@ -226,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; @@ -237,6 +213,57 @@ 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 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 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 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(basic_test) }, @@ -253,6 +280,7 @@ test_st tests[] ={ collection_st collection[] ={ {"block", 0, 0, tests}, + {"error()", 0, 0, error_tests}, {0, 0, 0, 0} };