Update to fix more of the key cleanup.
[awesomized/libmemcached] / tests / libmemcached-1.0 / mem_functions.cc
index c2b0a50ae28d91b43d3a0db4ff064c25df45b4f5..caac4c0630bdced294e59d745280ab8ed2a7956f 100644 (file)
@@ -68,7 +68,6 @@
 #include <libtest/server.h>
 
 #include "clients/generator.h"
-#include "clients/execute.h"
 
 #define SMALL_STRING_LEN 1024
 
 #include "tests/ketama.h"
 #include "tests/namespace.h"
 #include "tests/parser.h"
+#include "tests/libmemcached-1.0/callback_counter.h"
 #include "tests/libmemcached-1.0/dump.h"
 #include "tests/libmemcached-1.0/fetch_all_results.h"
+#include "tests/libmemcached-1.0/generate.h"
 #include "tests/libmemcached-1.0/haldenbrand.h"
 #include "tests/libmemcached-1.0/stat.h"
 #include "tests/touch.h"
@@ -100,10 +101,7 @@ using namespace libtest;
 
 #include "tests/hash_results.h"
 
-#define GLOBAL_COUNT 10000
-#define GLOBAL2_COUNT 100
-#define SERVERS_TO_CREATE 5
-static uint32_t global_count= GLOBAL2_COUNT;
+#include "tests/libmemcached-1.0/servers_to_create.h"
 
 #define UUID_STRING_MAXLENGTH 36
 
@@ -137,6 +135,7 @@ public:
 
         uuid_unparse(out, &key_buffer[0]);
         _keys[x]= strdup(&key_buffer[0]);
+        (_keys[x])[UUID_STRING_MAXLENGTH]= 'x';
       }
       else // We just use a number and pad the string if UUID is not available
       {
@@ -209,10 +208,6 @@ private:
     std::vector<size_t> _lengths;
 };
 
-static pairs_st *global_pairs;
-static const char *global_keys[GLOBAL_COUNT];
-static size_t global_keys_length[GLOBAL_COUNT];
-
 /**
   @note This should be testing to see if the server really supports the binary protocol.
 */
@@ -1851,16 +1846,6 @@ static test_return_t mget_result_alloc_test(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
-/* Count the results */
-static memcached_return_t callback_counter(const memcached_st*, memcached_result_st*, void *context)
-{
-  size_t *counter= (size_t *)context;
-
-  *counter= *counter + 1;
-
-  return MEMCACHED_SUCCESS;
-}
-
 static test_return_t mget_result_function(memcached_st *memc)
 {
   const char *keys[]= {"fudge", "son", "food"};
@@ -1951,25 +1936,20 @@ static test_return_t mget_execute(memcached_st *original_memc)
   memcached_st *memc= create_single_instance_memcached(original_memc, "--BINARY-PROTOCOL");
   test_true(memc);
 
-  size_t max_keys= 20480;
-
-
-  char **keys= static_cast<char **>(calloc(max_keys, sizeof(char*)));
-  size_t *key_length=static_cast<size_t *>(calloc(max_keys, sizeof(size_t)));
+  keys_st keys(20480);
 
   /* First add all of the items.. */
   char blob[1024] = {0};
 
-  for (size_t x= 0; x < max_keys; ++x)
+  for (size_t x= 0; x < keys.size(); ++x)
   {
-    char k[251];
-
-    key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%lu", (unsigned long)x);
-    keys[x]= strdup(k);
-    test_true(keys[x] != NULL);
     uint64_t query_id= memcached_query_id(memc);
-    memcached_return_t rc= memcached_add(memc, keys[x], key_length[x], blob, sizeof(blob), 0, 0);
-    test_true_got(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED, memcached_strerror(NULL, rc));
+    memcached_return_t rc= memcached_add(memc,
+                                         keys.key_at(x), keys.length_at(x),
+                                         blob, sizeof(blob),
+                                         0, 0);
+    test_true_got(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED,
+                  memcached_strerror(NULL, rc));
     test_compare(query_id +1, memcached_query_id(memc));
   }
 
@@ -1977,8 +1957,9 @@ static test_return_t mget_execute(memcached_st *original_memc)
   size_t counter= 0;
   memcached_execute_fn callbacks[]= { &callback_counter };
   test_compare(MEMCACHED_SUCCESS, 
-               memcached_mget_execute(memc, (const char**)keys, key_length,
-                                      max_keys, callbacks, &counter, 1));
+               memcached_mget_execute(memc,
+                                      keys.keys_ptr(), keys.lengths_ptr(),
+                                      keys.size(), callbacks, &counter, 1));
 
   {
     uint64_t query_id= memcached_query_id(memc);
@@ -1987,16 +1968,8 @@ static test_return_t mget_execute(memcached_st *original_memc)
     test_compare(query_id, memcached_query_id(memc));
 
     /* Verify that we got all of the items */
-    test_true(counter == max_keys);
-  }
-
-  /* Release all allocated resources */
-  for (size_t x= 0; x < max_keys; ++x)
-  {
-    free(keys[x]);
+    test_compare(keys.size(), counter);
   }
-  free(keys);
-  free(key_length);
 
   memcached_free(memc);
 
@@ -2004,6 +1977,7 @@ static test_return_t mget_execute(memcached_st *original_memc)
 }
 
 #define REGRESSION_BINARY_VS_BLOCK_COUNT  20480
+static pairs_st *global_pairs;
 
 static test_return_t key_setup(memcached_st *memc)
 {
@@ -2014,9 +1988,8 @@ static test_return_t key_setup(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
-static test_return_t key_teardown(memcached_st *memc)
+static test_return_t key_teardown(memcached_st *)
 {
-  (void)memc;
   pairs_free(global_pairs);
 
   return TEST_SUCCESS;
@@ -3064,311 +3037,6 @@ static test_return_t result_alloc(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
-static test_return_t cleanup_pairs(memcached_st *memc)
-{
-  (void)memc;
-  pairs_free(global_pairs);
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t generate_pairs(memcached_st *)
-{
-  global_pairs= pairs_generate(GLOBAL_COUNT, 400);
-  global_count= GLOBAL_COUNT;
-
-  for (size_t x= 0; x < global_count; x++)
-  {
-    global_keys[x]= global_pairs[x].key;
-    global_keys_length[x]=  global_pairs[x].key_length;
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t generate_large_pairs(memcached_st *)
-{
-  global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
-  global_count= GLOBAL2_COUNT;
-
-  for (size_t x= 0; x < global_count; x++)
-  {
-    global_keys[x]= global_pairs[x].key;
-    global_keys_length[x]=  global_pairs[x].key_length;
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t generate_data(memcached_st *memc)
-{
-  unsigned int check_execute= execute_set(memc, global_pairs, global_count);
-
-  test_compare_warn_hint(global_count, check_execute, "Possible false, positive, memcached may have ejected key/value based on memory needs");
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t generate_data_with_stats(memcached_st *memc)
-{
-  unsigned int check_execute= execute_set(memc, global_pairs, global_count);
-
-  test_compare(check_execute, global_count);
-
-  // @todo hosts used size stats
-  memcached_return_t rc;
-  memcached_stat_st *stat_p= memcached_stat(memc, NULL, &rc);
-  test_true(stat_p);
-
-  for (uint32_t host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
-  {
-    /* This test was changes so that "make test" would work properlly */
-    if (DEBUG)
-    {
-      memcached_server_instance_st instance=
-        memcached_server_instance_by_position(memc, host_index);
-
-      printf("\nserver %u|%s|%u bytes: %llu\n", host_index, instance->hostname, instance->port, (unsigned long long)(stat_p + host_index)->bytes);
-    }
-    test_true((unsigned long long)(stat_p + host_index)->bytes);
-  }
-
-  memcached_stat_free(NULL, stat_p);
-
-  return TEST_SUCCESS;
-}
-static test_return_t generate_buffer_data(memcached_st *memc)
-{
-  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
-  generate_data(memc);
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t get_read_count(memcached_st *memc)
-{
-  memcached_st *memc_clone= memcached_clone(NULL, memc);
-  test_true(memc_clone);
-
-  memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
-
-  {
-    char *return_value;
-    size_t return_value_length;
-    uint32_t flags;
-    uint32_t count;
-
-    for (size_t x= count= 0; x < global_count; x++)
-    {
-      memcached_return_t rc;
-      return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
-                                  &return_value_length, &flags, &rc);
-      if (rc == MEMCACHED_SUCCESS)
-      {
-        count++;
-        if (return_value)
-        {
-          free(return_value);
-        }
-      }
-    }
-  }
-
-  memcached_free(memc_clone);
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t get_read(memcached_st *memc)
-{
-  size_t keys_returned= 0;
-  for (size_t x= 0; x < global_count; x++)
-  {
-    size_t return_value_length;
-    uint32_t flags;
-    memcached_return_t rc;
-    char *return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
-                                      &return_value_length, &flags, &rc);
-    /*
-      test_true(return_value);
-      test_compare(MEMCACHED_SUCCESS, rc);
-    */
-    if (rc == MEMCACHED_SUCCESS && return_value)
-    {
-      keys_returned++;
-      free(return_value);
-    }
-  }
-  test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t mget_read(memcached_st *memc)
-{
-
-  test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
-
-  test_compare(MEMCACHED_SUCCESS,
-               memcached_mget(memc, global_keys, global_keys_length, global_count));
-
-  // Go fetch the keys and test to see if all of them were returned
-  {
-    unsigned int keys_returned;
-    test_compare(TEST_SUCCESS, fetch_all_results(memc, keys_returned));
-    test_true(keys_returned > 0);
-    test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t mget_read_result(memcached_st *memc)
-{
-
-  test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
-
-  test_compare(MEMCACHED_SUCCESS,
-               memcached_mget(memc, global_keys, global_keys_length, global_count));
-
-  /* Turn this into a help function */
-  {
-    memcached_result_st results_obj;
-    memcached_result_st *results= memcached_result_create(memc, &results_obj);
-    test_true(results);
-
-    memcached_return_t rc;
-    while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
-    {
-      if (rc == MEMCACHED_IN_PROGRESS)
-      {
-        continue;
-      }
-
-      test_true(results);
-      test_compare(MEMCACHED_SUCCESS, rc);
-    }
-    test_compare(MEMCACHED_END, rc);
-
-    memcached_result_free(&results_obj);
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t mget_read_internal_result(memcached_st *memc)
-{
-
-  test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
-
-  test_compare(MEMCACHED_SUCCESS,
-               memcached_mget(memc, global_keys, global_keys_length, global_count));
-  {
-    memcached_result_st *results= NULL;
-    memcached_return_t rc;
-    while ((results= memcached_fetch_result(memc, results, &rc)))
-    {
-      test_true(results);
-      test_compare(MEMCACHED_SUCCESS, rc);
-    }
-    test_compare(MEMCACHED_END, rc);
-
-    memcached_result_free(results);
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t mget_read_partial_result(memcached_st *memc)
-{
-
-  test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
-
-  test_compare(MEMCACHED_SUCCESS,
-               memcached_mget(memc, global_keys, global_keys_length, global_count));
-
-  // We will scan for just one key
-  {
-    memcached_result_st results_obj;
-    memcached_result_st *results= memcached_result_create(memc, &results_obj);
-
-    memcached_return_t rc;
-    results= memcached_fetch_result(memc, results, &rc);
-    test_true(results);
-    test_compare(MEMCACHED_SUCCESS, rc);
-
-    memcached_result_free(&results_obj);
-  }
-
-  // We already have a read happening, lets start up another one.
-  test_compare(MEMCACHED_SUCCESS,
-               memcached_mget(memc, global_keys, global_keys_length, global_count));
-  {
-    memcached_result_st results_obj;
-    memcached_result_st *results= memcached_result_create(memc, &results_obj);
-    test_true(results);
-    test_false(memcached_is_allocated(results));
-
-    memcached_return_t rc;
-    while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
-    {
-      test_true(results);
-      test_compare(MEMCACHED_SUCCESS, rc);
-    }
-    test_compare(MEMCACHED_END, rc);
-
-    memcached_result_free(&results_obj);
-  }
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t mget_read_function(memcached_st *memc)
-{
-  test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
-
-  test_compare(MEMCACHED_SUCCESS,
-               memcached_mget(memc, global_keys, global_keys_length, global_count));
-
-  memcached_execute_fn callbacks[]= { &callback_counter };
-  size_t counter= 0;
-  test_compare(MEMCACHED_SUCCESS, 
-               memcached_fetch_execute(memc, callbacks, (void *)&counter, 1));
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t delete_generate(memcached_st *memc)
-{
-  size_t total= 0;
-  for (size_t x= 0; x < global_count; x++)
-  {
-    if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
-    {
-      total++;
-    }
-  }
-  test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
-
-  return TEST_SUCCESS;
-}
-
-static test_return_t delete_buffer_generate(memcached_st *memc)
-{
-  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
-
-  size_t total= 0;
-  for (size_t x= 0; x < global_count; x++)
-  {
-    if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
-    {
-      total++;
-    }
-  }
-  test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
-
-  return TEST_SUCCESS;
-}
 
 static test_return_t add_host_test1(memcached_st *memc)
 {
@@ -3750,8 +3418,7 @@ static test_return_t deprecated_set_memory_alloc(memcached_st *memc)
   void *test_ptr= NULL;
   void *cb_ptr= NULL;
   {
-    memcached_malloc_fn malloc_cb=
-      (memcached_malloc_fn)my_malloc;
+    memcached_malloc_fn malloc_cb= (memcached_malloc_fn)my_malloc;
     cb_ptr= *(void **)&malloc_cb;
     memcached_return_t rc;