From: Brian Aker Date: Tue, 15 Jun 2010 06:31:25 +0000 (-0700) Subject: Merge in version util. X-Git-Tag: 0.41~16 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=573d3bb262f51461d7ed4ba06a723a8555637bba;p=awesomized%2Flibmemcached Merge in version util. --- diff --git a/libmemcached/include.am b/libmemcached/include.am index ad8ebc0f..3321a4f6 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -123,13 +123,15 @@ nobase_include_HEADERS+= \ libmemcached/memcached_util.h \ libmemcached/util.h \ libmemcached/util/ping.h \ - libmemcached/util/pool.h + libmemcached/util/pool.h \ + libmemcached/util/version.h lib_LTLIBRARIES+= libmemcached/libmemcachedutil.la endif libmemcached_libmemcachedutil_la_SOURCES= \ libmemcached/util/ping.c \ - libmemcached/util/pool.c + libmemcached/util/pool.c \ + libmemcached/util/version.c libmemcached_libmemcachedutil_la_LIBADD= libmemcached/libmemcached.la libmemcached_libmemcachedutil_la_LDFLAGS= ${AM_LDFLAGS} -version-info 0:0:0 libmemcached_libmemcachedutil_la_DEPENDENCIES= libmemcached/libmemcached.la diff --git a/libmemcached/memcached_util.h b/libmemcached/memcached_util.h index 925745e7..427d7843 100644 --- a/libmemcached/memcached_util.h +++ b/libmemcached/memcached_util.h @@ -17,5 +17,6 @@ #include #include +#include #endif /* __LIBMEMCACHED__MEMCACHED_UTIL_H__ */ diff --git a/libmemcached/util/ping.c b/libmemcached/util/ping.c index 001cd596..3d5471a3 100644 --- a/libmemcached/util/ping.c +++ b/libmemcached/util/ping.c @@ -13,7 +13,7 @@ #include "libmemcached/memcached_util.h" -bool libmemcached_ping(const char *hostname, in_port_t port, memcached_return_t *ret) +bool libmemcached_util_ping(const char *hostname, in_port_t port, memcached_return_t *ret) { memcached_return_t rc; memcached_st memc, *memc_ptr; diff --git a/libmemcached/util/ping.h b/libmemcached/util/ping.h index 4ac677ad..095ee00e 100644 --- a/libmemcached/util/ping.h +++ b/libmemcached/util/ping.h @@ -17,7 +17,7 @@ extern "C" { #endif LIBMEMCACHED_API -bool libmemcached_ping(const char *hostname, in_port_t port, memcached_return_t *ret); +bool libmemcached_util_ping(const char *hostname, in_port_t port, memcached_return_t *ret); #ifdef __cplusplus } diff --git a/libmemcached/util/version.c b/libmemcached/util/version.c new file mode 100644 index 00000000..000db326 --- /dev/null +++ b/libmemcached/util/version.c @@ -0,0 +1,58 @@ +/* LibMemcached + * Copyright (C) 2010 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: connect to all hosts, and make sure they meet a minimum version + * + */ + +/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */ +#include "libmemcached/common.h" +#include "libmemcached/memcached_util.h" + +struct local_context +{ + uint8_t major_version; + uint8_t micro_version; + uint8_t minor_version; + + bool truth; +}; + +static memcached_return_t check_server_version(const memcached_st *ptr __attribute__((unused)), + const memcached_server_st *instance, + void *context) +{ + /* Do Nothing */ + struct local_context *check= (struct local_context *)context; + + if (instance->major_version >= check->major_version && + instance->micro_version >= check->micro_version && + instance->minor_version >= check->minor_version) + { + return MEMCACHED_SUCCESS; + } + + check->truth= false; + + return MEMCACHED_FAILURE; +} + +bool libmemcached_util_version_check(memcached_st *memc, + uint8_t major_version, + uint8_t micro_version, + uint8_t minor_version) +{ + memcached_server_fn callbacks[1]; + memcached_version(memc); + + struct local_context check= { .major_version= major_version, .micro_version= micro_version, .minor_version= minor_version, .truth= true }; + + callbacks[0]= check_server_version; + memcached_server_cursor(memc, callbacks, (void *)&check, 1); + + return check.truth; +} diff --git a/libmemcached/util/version.h b/libmemcached/util/version.h new file mode 100644 index 00000000..7d0b0260 --- /dev/null +++ b/libmemcached/util/version.h @@ -0,0 +1,29 @@ +/* LibMemcached + * Copyright (C) 2010 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: connect to all hosts, and make sure they meet a minimum version + * + */ + +#ifndef __LIBMEMCACHED_UTIL_VERSION_H__ +#define __LIBMEMCACHED_UTIL_VERSION_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +LIBMEMCACHED_API + bool libmemcached_util_version_check(memcached_st *memc, + uint8_t major_version, + uint8_t micro_version, + uint8_t minor_version); + +#ifdef __cplusplus +} +#endif + +#endif /* __LIBMEMCACHED__UTIL_VERSION_H__ */ diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 9bd9334e..e12ad3f7 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -3592,7 +3592,6 @@ static test_return_t pre_nonblock_binary(memcached_st *memc) { memcached_return_t rc= MEMCACHED_FAILURE; memcached_st *memc_clone; - memcached_server_instance_st instance; memc_clone= memcached_clone(NULL, memc); test_true(memc_clone); @@ -3600,9 +3599,7 @@ static test_return_t pre_nonblock_binary(memcached_st *memc) // will not toggle protocol on an connection. memcached_version(memc_clone); - instance= memcached_server_instance_by_position(memc_clone, 0); - - if (instance->major_version >= 1 && instance->minor_version > 2) + if (libmemcached_util_version_check(memc_clone, 1, 2, 0)) { memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0); rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); @@ -3728,7 +3725,6 @@ static test_return_t pre_binary(memcached_st *memc) { memcached_return_t rc= MEMCACHED_FAILURE; memcached_st *memc_clone; - memcached_server_instance_st instance; memc_clone= memcached_clone(NULL, memc); test_true(memc_clone); @@ -3736,9 +3732,7 @@ static test_return_t pre_binary(memcached_st *memc) // will not toggle protocol on an connection. memcached_version(memc_clone); - instance= memcached_server_instance_by_position(memc_clone, 0); - - if (instance->major_version >= 1 && instance->minor_version > 2) + if (libmemcached_util_version_check(memc_clone, 1, 2, 0)) { rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); test_true(rc == MEMCACHED_SUCCESS); @@ -4072,13 +4066,7 @@ static test_return_t enable_cas(memcached_st *memc) { unsigned int set= 1; - memcached_server_instance_st instance= - memcached_server_instance_by_position(memc, 0); - - memcached_version(memc); - - if ((instance->major_version >= 1 && (instance->minor_version == 2 && instance->micro_version >= 4)) - || instance->minor_version > 2) + if (libmemcached_util_version_check(memc, 1, 2, 4)) { memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set); @@ -4097,7 +4085,9 @@ static test_return_t check_for_1_2_3(memcached_st *memc) if ((instance->major_version >= 1 && (instance->minor_version == 2 && instance->micro_version >= 4)) || instance->minor_version > 2) + { return TEST_SUCCESS; + } return TEST_SKIPPED; } @@ -4426,6 +4416,46 @@ static test_return_t connection_pool_test(memcached_st *memc) return TEST_SUCCESS; } +static test_return_t util_version_test(memcached_st *memc) +{ + bool if_successful; + + if_successful= libmemcached_util_version_check(memc, 0, 0, 0); + test_true(if_successful == true); + + if_successful= libmemcached_util_version_check(memc, 9, 9, 9); + test_true(if_successful == false); + + memcached_server_instance_st instance= + memcached_server_instance_by_position(memc, 0); + + memcached_version(memc); + + // We only use one binary when we test, so this should be just fine. + if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version, instance->minor_version); + test_true(if_successful == true); + + if (instance->minor_version > 0) + if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version, instance->minor_version -1); + else if (instance->micro_version > 0) + if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version - 1, instance->minor_version); + else if (instance->major_version > 0) + if_successful= libmemcached_util_version_check(memc, instance->major_version -1, instance->micro_version, instance->minor_version); + + test_true(if_successful == true); + + if (instance->minor_version > 0) + if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version, instance->minor_version +1); + else if (instance->micro_version > 0) + if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version +1, instance->minor_version); + else if (instance->major_version > 0) + if_successful= libmemcached_util_version_check(memc, instance->major_version +1, instance->micro_version, instance->minor_version); + + test_true(if_successful == false); + + return TEST_SUCCESS; +} + static test_return_t ping_test(memcached_st *memc) { memcached_return_t rc; @@ -4433,11 +4463,11 @@ static test_return_t ping_test(memcached_st *memc) memcached_server_instance_by_position(memc, 0); // Test both the version that returns a code, and the one that does not. - test_true(libmemcached_ping(memcached_server_name(instance), - memcached_server_port(instance), NULL)); + test_true(libmemcached_util_ping(memcached_server_name(instance), + memcached_server_port(instance), NULL)); - test_true(libmemcached_ping(memcached_server_name(instance), - memcached_server_port(instance), &rc)); + test_true(libmemcached_util_ping(memcached_server_name(instance), + memcached_server_port(instance), &rc)); test_true(rc == MEMCACHED_SUCCESS); @@ -5549,12 +5579,8 @@ static test_return_t regression_bug_463297(memcached_st *memc) test_true(memc_clone != NULL); test_true(memcached_version(memc_clone) == MEMCACHED_SUCCESS); - memcached_server_instance_st instance= - memcached_server_instance_by_position(memc_clone, 0); - if (instance->major_version > 1 || - (instance->major_version == 1 && - instance->minor_version > 2)) + if (libmemcached_util_version_check(memc_clone, 1, 1, 2)) { /* Binary protocol doesn't support deferred delete */ memcached_st *bin_clone= memcached_clone(NULL, memc); @@ -6109,6 +6135,7 @@ test_st tests[] ={ #ifdef HAVE_LIBMEMCACHEDUTIL {"connectionpool", 1, (test_callback_fn)connection_pool_test }, {"ping", 1, (test_callback_fn)ping_test }, + {"util_version", 1, (test_callback_fn)util_version_test }, #endif {"test_get_last_disconnect", 1, (test_callback_fn)test_get_last_disconnect}, {"verbosity", 1, (test_callback_fn)test_verbosity}, diff --git a/tests/server.c b/tests/server.c index 7c2b91c6..543d5bac 100644 --- a/tests/server.c +++ b/tests/server.c @@ -81,7 +81,7 @@ void server_startup(server_startup_st *construct) sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u", MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE); } - if (libmemcached_ping("localhost", (in_port_t)(x + TEST_PORT_BASE), NULL)) + if (libmemcached_util_ping("localhost", (in_port_t)(x + TEST_PORT_BASE), NULL)) { fprintf(stderr, "Server on port %u already exists\n", x + TEST_PORT_BASE); }