Updating for simplified IO flush.
[awesomized/libmemcached] / tests / mem_udp.cc
index 8f6caad508039ef9f4bef6912c64447584aab45a..227cea633462933f481d9373d719e752a1095152 100644 (file)
@@ -1,11 +1,41 @@
-/* libMemcached Functions Test
- * Copyright (C) 2006-2009 Brian Aker
- * All rights reserved.
+/*  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.
  *
- * Use and distribution licensed under the BSD license.  See
- * the COPYING file in the parent directory for full text.
  */
 
+
 /*
   Sample test application.
 */
@@ -59,6 +89,8 @@ static test_return_t pre_binary(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
+typedef std::vector<uint16_t> Expected;
+
 static void increment_request_id(uint16_t *id)
 {
   (*id)++;
@@ -68,22 +100,17 @@ static void increment_request_id(uint16_t *id)
   }
 }
 
-static uint16_t *get_udp_request_ids(memcached_st *memc)
+static void get_udp_request_ids(memcached_st *memc, Expected &ids)
 {
-  uint16_t *ids= (uint16_t*)malloc(sizeof(uint16_t) * memcached_server_count(memc));
-  assert(ids);
-
   for (uint32_t x= 0; x < memcached_server_count(memc); x++)
   {
     memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
 
-    ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer);
+    ids.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer));
   }
-
-  return ids;
 }
 
-static test_return_t post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
+static test_return_t post_udp_op_check(memcached_st *memc, Expected& expected_req_ids)
 {
   (void)memc;
   (void)expected_req_ids;
@@ -114,8 +141,25 @@ static test_return_t init_udp(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
+static test_return_t init_udp_valgrind(memcached_st *memc)
+{
+  if (getenv("TESTS_ENVIRONMENT"))
+  {
+    return TEST_SKIPPED; 
+  }
+
+  test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
+
+  return TEST_SUCCESS;
+}
+
 static test_return_t binary_init_udp(memcached_st *memc)
 {
+  if (getenv("TESTS_ENVIRONMENT"))
+  {
+    return TEST_SKIPPED; 
+  }
+
   test_skip(TEST_SUCCESS, pre_binary(memc));
 
   return init_udp(memc);
@@ -174,26 +218,37 @@ static test_return_t set_udp_behavior_test(memcached_st *memc)
 
 static test_return_t udp_set_test(memcached_st *memc)
 {
-  unsigned int num_iters= 1025; //request id rolls over at 1024
+  // Assume we are running under valgrind, and bail 
+  if (getenv("TESTS_ENVIRONMENT"))
+  {
+    return TEST_SUCCESS; 
+  }
+
+  const unsigned int num_iters= 1025; //request id rolls over at 1024
+
+  test_true(memc);
 
   for (size_t x= 0; x < num_iters;x++)
   {
-    uint16_t *expected_ids= get_udp_request_ids(memc);
+    Expected expected_ids;
+    get_udp_request_ids(memc, expected_ids);
     unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
+    test_true(server_key < memcached_server_count(memc));
     memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
     size_t init_offset= instance->write_buffer_offset;
 
     memcached_return_t rc= memcached_set(memc, test_literal_param("foo"),
                                          test_literal_param("when we sanitize"),
-                                         (time_t)0, (uint32_t)0);
+                                         time_t(0), uint32_t(0));
     test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
     /** NB, the check below assumes that if new write_ptr is less than
      *  the original write_ptr that we have flushed. For large payloads, this
      *  maybe an invalid assumption, but for the small payload we have it is OK
      */
-    if (rc == MEMCACHED_SUCCESS or
-            instance->write_buffer_offset < init_offset)
+    if (rc == MEMCACHED_SUCCESS or instance->write_buffer_offset < init_offset)
+    {
       increment_request_id(&expected_ids[server_key]);
+    }
 
     if (rc == MEMCACHED_SUCCESS)
     {
@@ -204,7 +259,8 @@ static test_return_t udp_set_test(memcached_st *memc)
       test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
       test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
     }
-    test_true(post_udp_op_check(memc, expected_ids) == TEST_SUCCESS);
+
+    test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
   }
 
   return TEST_SUCCESS;
@@ -212,29 +268,39 @@ static test_return_t udp_set_test(memcached_st *memc)
 
 static test_return_t udp_buffered_set_test(memcached_st *memc)
 {
+  test_true(memc);
   test_compare(MEMCACHED_INVALID_ARGUMENTS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true));
   return TEST_SUCCESS;
 }
 
 static test_return_t udp_set_too_big_test(memcached_st *memc)
 {
+  test_true(memc);
   char value[MAX_UDP_DATAGRAM_LENGTH];
-  uint16_t *expected_ids= get_udp_request_ids(memc);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
 
   memset(value, int('f'), sizeof(value));
 
-  test_compare_hint(MEMCACHED_WRITE_FAILURE, memcached_set(memc, test_literal_param("bar"), value, sizeof(value), time_t(0), uint32_t(0)),
+  test_compare_hint(MEMCACHED_WRITE_FAILURE,
+                    memcached_set(memc, test_literal_param("bar"), 
+                                  test_literal_param(value),
+                                  time_t(0), uint32_t(0)),
                     memcached_last_error_message(memc));
 
-  return post_udp_op_check(memc,expected_ids);
+  return post_udp_op_check(memc, expected_ids);
 }
 
 static test_return_t udp_delete_test(memcached_st *memc)
 {
+  test_true(memc);
+
   //request id rolls over at 1024
   for (size_t x= 0; x < 1025; x++)
   {
-    uint16_t *expected_ids=get_udp_request_ids(memc);
+    Expected expected_ids;
+    get_udp_request_ids(memc, expected_ids);
+
     unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
     memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
     size_t init_offset= instance->write_buffer_offset;
@@ -256,8 +322,9 @@ static test_return_t udp_delete_test(memcached_st *memc)
       test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
       test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
     }
-    test_true(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
+    test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
   }
+
   return TEST_SUCCESS;
 }
 
@@ -269,7 +336,8 @@ static test_return_t udp_buffered_delete_test(memcached_st *memc)
 
 static test_return_t udp_verbosity_test(memcached_st *memc)
 {
-  uint16_t *expected_ids= get_udp_request_ids(memc);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
 
   for (size_t x= 0; x < memcached_server_count(memc); x++)
   {
@@ -278,19 +346,21 @@ static test_return_t udp_verbosity_test(memcached_st *memc)
 
   test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 3));
 
-  return post_udp_op_check(memc,expected_ids);
+  return post_udp_op_check(memc, expected_ids);
 }
 
 static test_return_t udp_quit_test(memcached_st *memc)
 {
-  uint16_t *expected_ids= get_udp_request_ids(memc);
+  Expected expected_ids;
   memcached_quit(memc);
+
   return post_udp_op_check(memc, expected_ids);
 }
 
 static test_return_t udp_flush_test(memcached_st *memc)
 {
-  uint16_t *expected_ids= get_udp_request_ids(memc);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
 
   for (size_t x= 0; x < memcached_server_count(memc); x++)
   {
@@ -307,7 +377,9 @@ static test_return_t udp_incr_test(memcached_st *memc)
                                                 test_literal_param("1"),
                                                 (time_t)0, (uint32_t)0));
 
-  uint16_t *expected_ids= get_udp_request_ids(memc);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
+
   unsigned int server_key= memcached_generate_hash(memc, test_literal_param("incr"));
   increment_request_id(&expected_ids[server_key]);
 
@@ -324,7 +396,9 @@ static test_return_t udp_decr_test(memcached_st *memc)
                                                 test_literal_param("1"),
                                                 (time_t)0, (uint32_t)0));
 
-  uint16_t *expected_ids= get_udp_request_ids(memc);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
+
   unsigned int server_key= memcached_generate_hash(memc, test_literal_param("decr"));
   increment_request_id(&expected_ids[server_key]);
 
@@ -339,9 +413,10 @@ static test_return_t udp_stat_test(memcached_st *memc)
 {
   memcached_return_t rc;
   char args[]= "";
-  uint16_t *expected_ids = get_udp_request_ids(memc);
-  memcached_stat_st *rv = memcached_stat(memc, args, &rc);
-  free(rv);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
+  memcached_stat_st *rv= memcached_stat(memc, args, &rc);
+  memcached_stat_free(memc, rv);
   test_compare(MEMCACHED_NOT_SUPPORTED, rc);
 
   return post_udp_op_check(memc, expected_ids);
@@ -349,7 +424,8 @@ static test_return_t udp_stat_test(memcached_st *memc)
 
 static test_return_t udp_version_test(memcached_st *memc)
 {
-  uint16_t *expected_ids = get_udp_request_ids(memc);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
 
   test_compare(MEMCACHED_NOT_SUPPORTED, memcached_version(memc));
 
@@ -360,7 +436,8 @@ static test_return_t udp_get_test(memcached_st *memc)
 {
   memcached_return_t rc;
   size_t vlen;
-  uint16_t *expected_ids = get_udp_request_ids(memc);
+  Expected expected_ids;
+  get_udp_request_ids(memc, expected_ids);
   test_null(memcached_get(memc, test_literal_param("foo"), &vlen, (uint32_t)0, &rc));
   test_compare(MEMCACHED_NOT_SUPPORTED, rc);
 
@@ -427,13 +504,13 @@ test_st upd_io_tests[] ={
 
 collection_st collection[] ={
   {"udp_setup", (test_callback_fn*)init_udp, 0, udp_setup_server_tests},
-  {"udp_io", (test_callback_fn*)init_udp, 0, upd_io_tests},
+  {"udp_io", (test_callback_fn*)init_udp_valgrind, 0, upd_io_tests},
   {"udp_binary_io", (test_callback_fn*)binary_init_udp, 0, upd_io_tests},
   {0, 0, 0, 0}
 };
 
 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
-#include "libmemcached_world.h"
+#include "tests/libmemcached_world.h"
 
 void get_world(Framework *world)
 {