Skip to content

Commit

Permalink
Merge r1902015 from apr/trunk:
Browse files Browse the repository at this point in the history
* tests: Check for memcache/redis servers only if memcache/redis tests configured
  to run.

git-svn-id: https://svn.apache.org/repos/asf/apr/apr-util/branches/1.7.x@1920387 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Ivan Zhakov committed Sep 2, 2024
1 parent c58276b commit acda774
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 163 deletions.
212 changes: 130 additions & 82 deletions test/testmemcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,94 @@ static int randval(apr_uint32_t high)
return i > 0 ? i : 1;
}

/* use apr_socket stuff to see if there is in fact a memcached server
* running on PORT.
*/
static apr_status_t check_mc(void)
{
apr_pool_t *pool = p;
apr_status_t rv;
apr_socket_t *sock = NULL;
apr_sockaddr_t *sa;
struct iovec vec[2];
apr_size_t written;
char buf[128];
apr_size_t len;

rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, 0, pool);
if (rv != APR_SUCCESS) {
return rv;
}

rv = apr_sockaddr_info_get(&sa, HOST, APR_INET, PORT, 0, pool);
if (rv != APR_SUCCESS) {
return rv;
}

rv = apr_socket_timeout_set(sock, 1 * APR_USEC_PER_SEC);
if (rv != APR_SUCCESS) {
return rv;
}

rv = apr_socket_connect(sock, sa);
if (rv != APR_SUCCESS) {
return rv;
}

rv = apr_socket_timeout_set(sock, -1);
if (rv != APR_SUCCESS) {
return rv;
}

vec[0].iov_base = "version";
vec[0].iov_len = sizeof("version") - 1;

vec[1].iov_base = "\r\n";
vec[1].iov_len = sizeof("\r\n") - 1;

rv = apr_socket_sendv(sock, vec, 2, &written);
if (rv != APR_SUCCESS) {
return rv;
}

len = sizeof(buf);
rv = apr_socket_recv(sock, buf, &len);
if (rv != APR_SUCCESS) {
return rv;
}

if (strncmp(buf, "VERSION", sizeof("VERSION") - 1) != 0) {
rv = APR_EGENERAL;
}

apr_socket_close(sock);
return rv;
}

static int has_memcache_server()
{
static int has_memcache_server_state = -1;

if (has_memcache_server_state < 0) {
apr_status_t rv;

/* check for a running memcached on the typical port before
* trying to run the tests. succeed if we don't find one.
*/
rv = check_mc();
if (rv == APR_SUCCESS) {
has_memcache_server_state = 1;
}
else {
has_memcache_server_state = 0;
abts_log_message("Error %d occurred attempting to reach memcached "
"on %s:%d. Skipping apr_memcache tests...",
rv, HOST, PORT);
}
}

return has_memcache_server_state;
}
/*
* general test to make sure we can create the memcache struct and add
* some servers, but not more than we tell it we can add
Expand All @@ -140,6 +228,11 @@ static void test_memcache_create(abts_case * tc, void *data)
apr_uint32_t i;
apr_uint32_t hash;

if (!has_memcache_server()) {
ABTS_SKIP(tc, data, "Memcache server not found.");
return;
}

rv = apr_memcache_create(pool, max_servers, 0, &memcache);
ABTS_ASSERT(tc, "memcache create failed", rv == APR_SUCCESS);

Expand Down Expand Up @@ -208,6 +301,11 @@ static void test_memcache_user_funcs(abts_case * tc, void *data)
my_hash_server_baton *baton =
apr_pcalloc(pool, sizeof(my_hash_server_baton));

if (!has_memcache_server()) {
ABTS_SKIP(tc, data, "Memcache server not found.");
return;
}

rv = apr_memcache_create(pool, max_servers, 0, &memcache);
ABTS_ASSERT(tc, "memcache create failed", rv == APR_SUCCESS);

Expand Down Expand Up @@ -252,6 +350,11 @@ static void test_memcache_meta(abts_case * tc, void *data)
char *result;
apr_status_t rv;

if (!has_memcache_server()) {
ABTS_SKIP(tc, data, "Memcache server not found.");
return;
}

rv = apr_memcache_create(pool, 1, 0, &memcache);
ABTS_ASSERT(tc, "memcache create failed", rv == APR_SUCCESS);

Expand Down Expand Up @@ -316,6 +419,11 @@ static void test_memcache_addreplace(abts_case * tc, void *data)
char *result;
apr_size_t len;

if (!has_memcache_server()) {
ABTS_SKIP(tc, data, "Memcache server not found.");
return;
}

rv = apr_memcache_create(pool, 1, 0, &memcache);
ABTS_ASSERT(tc, "memcache create failed", rv == APR_SUCCESS);

Expand Down Expand Up @@ -375,6 +483,11 @@ static void test_memcache_incrdecr(abts_case * tc, void *data)
apr_size_t len;
apr_uint32_t i;

if (!has_memcache_server()) {
ABTS_SKIP(tc, data, "Memcache server not found.");
return;
}

rv = apr_memcache_create(pool, 1, 0, &memcache);
ABTS_ASSERT(tc, "memcache create failed", rv == APR_SUCCESS);

Expand Down Expand Up @@ -427,6 +540,11 @@ static void test_memcache_multiget(abts_case * tc, void *data)
apr_hash_index_t *hi;
apr_uint32_t i;

if (!has_memcache_server()) {
ABTS_SKIP(tc, data, "Memcache server not found.");
return;
}

rv = apr_memcache_create(pool, 1, 0, &memcache);
ABTS_ASSERT(tc, "memcache create failed", rv == APR_SUCCESS);

Expand Down Expand Up @@ -495,6 +613,11 @@ static void test_memcache_setget(abts_case * tc, void *data)
char *result;
apr_size_t len;

if (!has_memcache_server()) {
ABTS_SKIP(tc, data, "Memcache server not found.");
return;
}

rv = apr_memcache_create(pool, 1, 0, &memcache);
ABTS_ASSERT(tc, "memcache create failed", rv == APR_SUCCESS);

Expand Down Expand Up @@ -538,70 +661,6 @@ static void test_memcache_setget(abts_case * tc, void *data)
}
}

/* use apr_socket stuff to see if there is in fact a memcached server
* running on PORT.
*/
static apr_status_t check_mc(void)
{
apr_pool_t *pool = p;
apr_status_t rv;
apr_socket_t *sock = NULL;
apr_sockaddr_t *sa;
struct iovec vec[2];
apr_size_t written;
char buf[128];
apr_size_t len;

rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, 0, pool);
if(rv != APR_SUCCESS) {
return rv;
}

rv = apr_sockaddr_info_get(&sa, HOST, APR_INET, PORT, 0, pool);
if(rv != APR_SUCCESS) {
return rv;
}

rv = apr_socket_timeout_set(sock, 1 * APR_USEC_PER_SEC);
if (rv != APR_SUCCESS) {
return rv;
}

rv = apr_socket_connect(sock, sa);
if (rv != APR_SUCCESS) {
return rv;
}

rv = apr_socket_timeout_set(sock, -1);
if (rv != APR_SUCCESS) {
return rv;
}

vec[0].iov_base = "version";
vec[0].iov_len = sizeof("version") - 1;

vec[1].iov_base = "\r\n";
vec[1].iov_len = sizeof("\r\n") -1;

rv = apr_socket_sendv(sock, vec, 2, &written);
if (rv != APR_SUCCESS) {
return rv;
}

len = sizeof(buf);
rv = apr_socket_recv(sock, buf, &len);
if(rv != APR_SUCCESS) {
return rv;
}

if(strncmp(buf, "VERSION", sizeof("VERSION")-1) != 0) {
rv = APR_EGENERAL;
}

apr_socket_close(sock);
return rv;
}

static void test_connection_validation(abts_case *tc, void *data)
{
apr_status_t rv;
Expand Down Expand Up @@ -675,24 +734,13 @@ abts_suite *testmemcache(abts_suite * suite)
{
apr_status_t rv;
suite = ADD_SUITE(suite);
/* check for a running memcached on the typical port before
* trying to run the tests. succeed if we don't find one.
*/
rv = check_mc();
if (rv == APR_SUCCESS) {
abts_run_test(suite, test_memcache_create, NULL);
abts_run_test(suite, test_memcache_user_funcs, NULL);
abts_run_test(suite, test_memcache_meta, NULL);
abts_run_test(suite, test_memcache_setget, NULL);
abts_run_test(suite, test_memcache_multiget, NULL);
abts_run_test(suite, test_memcache_addreplace, NULL);
abts_run_test(suite, test_memcache_incrdecr, NULL);
}
else {
abts_log_message("Error %d occurred attempting to reach memcached "
"on %s:%d. Skipping apr_memcache tests...",
rv, HOST, PORT);
}
abts_run_test(suite, test_memcache_create, NULL);
abts_run_test(suite, test_memcache_user_funcs, NULL);
abts_run_test(suite, test_memcache_meta, NULL);
abts_run_test(suite, test_memcache_setget, NULL);
abts_run_test(suite, test_memcache_multiget, NULL);
abts_run_test(suite, test_memcache_addreplace, NULL);
abts_run_test(suite, test_memcache_incrdecr, NULL);
abts_run_test(suite, test_connection_validation, NULL);

return suite;
Expand Down
Loading

0 comments on commit acda774

Please sign in to comment.