apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r1768023 - in /apr/apr-util/branches/1.6.x: include/apr_redis.h redis/apr_redis.c test/testredis.c
Date Fri, 04 Nov 2016 12:30:02 GMT
Author: jim
Date: Fri Nov  4 12:30:02 2016
New Revision: 1768023

URL: http://svn.apache.org/viewvc?rev=1768023&view=rev
Log:
Add in official PING and tests for setex and ping

Modified:
    apr/apr-util/branches/1.6.x/include/apr_redis.h
    apr/apr-util/branches/1.6.x/redis/apr_redis.c
    apr/apr-util/branches/1.6.x/test/testredis.c

Modified: apr/apr-util/branches/1.6.x/include/apr_redis.h
URL: http://svn.apache.org/viewvc/apr/apr-util/branches/1.6.x/include/apr_redis.h?rev=1768023&r1=1768022&r2=1768023&view=diff
==============================================================================
--- apr/apr-util/branches/1.6.x/include/apr_redis.h (original)
+++ apr/apr-util/branches/1.6.x/include/apr_redis.h Fri Nov  4 12:30:02 2016
@@ -350,6 +350,12 @@ APU_DECLARE(apr_status_t) apr_redis_decr
 
 
 /**
+ * Pings the server
+ * @param rs Server to ping
+ */
+APU_DECLARE(apr_status_t) apr_redis_ping(apr_redis_server_t *rs);
+
+/**
  * Gets multiple values from the server, allocating the values out of p
  * @param rc client to use
  * @param temp_pool Pool used for temporary allocations. May be cleared inside this

Modified: apr/apr-util/branches/1.6.x/redis/apr_redis.c
URL: http://svn.apache.org/viewvc/apr/apr-util/branches/1.6.x/redis/apr_redis.c?rev=1768023&r1=1768022&r2=1768023&view=diff
==============================================================================
--- apr/apr-util/branches/1.6.x/redis/apr_redis.c (original)
+++ apr/apr-util/branches/1.6.x/redis/apr_redis.c Fri Nov  4 12:30:02 2016
@@ -154,8 +154,6 @@ APU_DECLARE(apr_status_t) apr_redis_add_
     return rv;
 }
 
-static apr_status_t rc_ping(apr_redis_server_t *rs);
-
 APU_DECLARE(apr_redis_server_t *)
 apr_redis_find_server_hash(apr_redis_t *rc, const apr_uint32_t hash)
 {
@@ -168,7 +166,7 @@ apr_redis_find_server_hash(apr_redis_t *
 }
 
 APU_DECLARE(apr_redis_server_t *)
-    apr_redis_find_server_hash_default(void *baton, apr_redis_t *rc,
+apr_redis_find_server_hash_default(void *baton, apr_redis_t *rc,
                                    const apr_uint32_t hash)
 {
     apr_redis_server_t *rs = NULL;
@@ -195,7 +193,7 @@ APU_DECLARE(apr_redis_server_t *)
             /* Try the dead server, every 5 seconds */
             if (curtime - rs->btime > apr_time_from_sec(5)) {
                 rs->btime = curtime;
-                if (rc_ping(rs) == APR_SUCCESS) {
+                if (apr_redis_ping(rs) == APR_SUCCESS) {
                     make_server_live(rc, rs);
 #if APR_HAS_THREADS
                     apr_thread_mutex_unlock(rs->lock);
@@ -1085,7 +1083,8 @@ APU_DECLARE(apr_status_t)
     return rv;
 }
 
-apr_status_t rc_ping(apr_redis_server_t *rs)
+APU_DECLARE(apr_status_t)
+apr_redis_ping(apr_redis_server_t *rs)
 {
     apr_status_t rv;
     apr_size_t written;
@@ -1121,6 +1120,12 @@ apr_status_t rc_ping(apr_redis_server_t
     }
 
     rv = get_server_line(conn);
+    if (rv == APR_SUCCESS) {
+        /* we got *something*. Was it Redis? */
+        if (strncmp(conn->buffer, "+PONG", sizeof("+PONG")-1) != 0) {
+            rv = APR_EGENERAL;
+        }
+    }
     rs_release_conn(rs, conn);
     return rv;
 }

Modified: apr/apr-util/branches/1.6.x/test/testredis.c
URL: http://svn.apache.org/viewvc/apr/apr-util/branches/1.6.x/test/testredis.c?rev=1768023&r1=1768022&r2=1768023&view=diff
==============================================================================
--- apr/apr-util/branches/1.6.x/test/testredis.c (original)
+++ apr/apr-util/branches/1.6.x/test/testredis.c Fri Nov  4 12:30:02 2016
@@ -404,15 +404,149 @@ static void test_redis_setget(abts_case
     }
 }
 
+/* test setting and getting */
+
+static void test_redis_setexget(abts_case * tc, void *data)
+{
+    apr_pool_t *pool = p;
+    apr_status_t rv;
+    apr_redis_t *redis;
+    apr_redis_server_t *server;
+    apr_hash_t *tdata;
+    apr_hash_index_t *hi;
+    char *result;
+    apr_size_t len;
+
+    rv = apr_redis_create(pool, 1, 0, &redis);
+    ABTS_ASSERT(tc, "redis create failed", rv == APR_SUCCESS);
+
+    rv = apr_redis_server_create(pool, HOST, PORT, 0, 1, 1, 60, 60, &server);
+    ABTS_ASSERT(tc, "server create failed", rv == APR_SUCCESS);
+
+    rv = apr_redis_add_server(redis, server);
+    ABTS_ASSERT(tc, "server add failed", rv == APR_SUCCESS);
+
+    tdata = apr_hash_make(pool);
+
+    create_test_hash(pool, tdata);
+
+    for (hi = apr_hash_first(p, tdata); hi; hi = apr_hash_next(hi)) {
+    const void *k;
+    void *v;
+        const char *key;
+
+    apr_hash_this(hi, &k, NULL, &v);
+        key = k;
+
+    rv = apr_redis_ping(server);
+    ABTS_ASSERT(tc, "ping failed", rv == APR_SUCCESS);
+    rv = apr_redis_setex(redis, key, v, strlen(v), 10, 27);
+    ABTS_ASSERT(tc, "set failed", rv == APR_SUCCESS);
+    rv = apr_redis_getp(redis, pool, key, &result, &len, NULL);
+    ABTS_ASSERT(tc, "get failed", rv == APR_SUCCESS);
+    }
+
+    rv = apr_redis_getp(redis, pool, "nothere3423", &result, &len, NULL);
+
+    ABTS_ASSERT(tc, "get should have failed", rv != APR_SUCCESS);
+
+    for (hi = apr_hash_first(p, tdata); hi; hi = apr_hash_next(hi)) {
+    const void *k;
+    const char *key;
+
+    apr_hash_this(hi, &k, NULL, NULL);
+    key = k;
+
+    rv = apr_redis_delete(redis, key, 0);
+    ABTS_ASSERT(tc, "delete failed", rv == APR_SUCCESS);
+    }
+}
+
+/* use apr_socket stuff to see if there is in fact a Redis server
+ * running on PORT.
+ */
+static apr_status_t check_redis(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 = "PING";
+  vec[0].iov_len  = sizeof("PING") - 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, "+PONG", sizeof("+PONG")-1) != 0) {
+    rv = APR_EGENERAL;
+  }
+
+  apr_socket_close(sock);
+  return rv;
+}
+
 abts_suite *testredis(abts_suite * suite)
 {
+    apr_status_t rv;
     suite = ADD_SUITE(suite);
-    abts_run_test(suite, test_redis_create, NULL);
-    abts_run_test(suite, test_redis_user_funcs, NULL);
-    abts_run_test(suite, test_redis_meta, NULL);
-    abts_run_test(suite, test_redis_setget, NULL);
-    /* abts_run_test(suite, test_redis_multiget, NULL); */
-    abts_run_test(suite, test_redis_incrdecr, NULL);
+    /* check for a running redis on the typical port before
+     * trying to run the tests. succeed if we don't find one.
+     */
+    rv = check_redis();
+    if (rv == APR_SUCCESS) {
+        abts_run_test(suite, test_redis_create, NULL);
+        abts_run_test(suite, test_redis_user_funcs, NULL);
+        abts_run_test(suite, test_redis_meta, NULL);
+        abts_run_test(suite, test_redis_setget, NULL);
+        abts_run_test(suite, test_redis_setexget, NULL);
+        /* abts_run_test(suite, test_redis_multiget, NULL); */
+        abts_run_test(suite, test_redis_incrdecr, NULL);
+    }
+    else {
+        abts_log_message("Error %d occurred attempting to reach Redis "
+                         "on %s:%d.  Skipping apr_redis tests...",
+                         rv, HOST, PORT);
+    }
 
     return suite;
 }



Mime
View raw message