Update all of the build/spec files.
[awesomized/libmemcached] / tests / mem_functions.cc
index 1c5f7d4f6bc7038c95341f0f879d2fe368b1ab1c..3d2869ce23bfa0f9ca6fa83e7d365d5be1a7ebf0 100644 (file)
   Test cases
 */
 
-#include <libmemcached/memcached.h>
+#include <libmemcached-1.0/memcached.h>
 #include <libmemcached/is.h>
 #include <libmemcached/server_instance.h>
 
-#include <libhashkit/hashkit.h>
+#include <libhashkit-1.0/hashkit.h>
 
 #include <cassert>
 #include <cerrno>
 #include <memory>
 #include <pthread.h>
+#include <semaphore.h>
 #include <signal.h>
 #include <sys/stat.h>
 #include <sys/time.h>
@@ -77,6 +78,7 @@
 #include "tests/ketama.h"
 #include "tests/namespace.h"
 #include "tests/parser.h"
+#include "tests/callbacks.h"
 #include "tests/pool.h"
 #include "tests/print.h"
 #include "tests/replication.h"
@@ -85,7 +87,7 @@
 
 using namespace libtest;
 
-#include <libmemcached/memcached_util.h>
+#include <libmemcached/util.h>
 
 #include "hash_results.h"
 
@@ -824,7 +826,6 @@ static test_return_t bad_key_test(memcached_st *memc)
   const char *key= "foo bad";
   uint32_t flags;
   memcached_st *memc_clone;
-  size_t max_keylen= 0xffff;
 
   uint64_t query_id= memcached_query_id(memc);
   
@@ -886,8 +887,6 @@ static test_return_t bad_key_test(memcached_st *memc)
                  memcached_mget_by_key(memc_clone, "foo daddy", 9, keys, key_lengths, 1));
     test_compare(query_id +1, memcached_query_id(memc_clone));
 
-    max_keylen= 250;
-
     /* The following test should be moved to the end of this function when the
        memcached server is updated to allow max size length of the keys in the
        binary protocol
@@ -896,15 +895,16 @@ static test_return_t bad_key_test(memcached_st *memc)
                  memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_NAMESPACE, NULL));
 
     std::vector <char> longkey;
-    longkey.insert(longkey.end(), max_keylen +1, 'a');
-    test_compare(longkey.size(), max_keylen +1);
+    longkey.insert(longkey.end(), MEMCACHED_MAX_KEY, 'a');
+    test_compare(longkey.size(), size_t(MEMCACHED_MAX_KEY));
     {
       size_t string_length;
-      test_null(memcached_get(memc_clone, &longkey[0], max_keylen, &string_length, &flags, &rc));
+      // We subtract 1
+      test_null(memcached_get(memc_clone, &longkey[0], longkey.size() -1, &string_length, &flags, &rc));
       test_compare(MEMCACHED_NOTFOUND, rc);
       test_zero(string_length);
 
-      test_null(memcached_get(memc_clone, &longkey[0], max_keylen +1, &string_length, &flags, &rc));
+      test_null(memcached_get(memc_clone, &longkey[0], longkey.size(), &string_length, &flags, &rc));
       test_compare(MEMCACHED_BAD_KEY_PROVIDED, rc);
       test_zero(string_length);
     }
@@ -982,28 +982,6 @@ static test_return_t read_through(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
-static memcached_return_t delete_trigger(memcached_st *,
-                                         const char *key,
-                                         size_t key_length)
-{
-  assert(key);
-  assert(key_length);
-
-  return MEMCACHED_SUCCESS;
-}
-
-static test_return_t delete_through(memcached_st *memc)
-{
-  memcached_trigger_delete_key_fn callback;
-
-  callback= (memcached_trigger_delete_key_fn)delete_trigger;
-
-  test_compare(MEMCACHED_SUCCESS, 
-               memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, *(void**)&callback));
-
-  return TEST_SUCCESS;
-}
-
 static test_return_t get_test(memcached_st *memc)
 {
   memcached_return_t rc;
@@ -3734,7 +3712,7 @@ static test_return_t selection_of_namespace_tests(memcached_st *memc)
   /* Make sure be default none exists */
   value= (char*)memcached_callback_get(memc, MEMCACHED_CALLBACK_NAMESPACE, &rc);
   test_null(value);
-  test_compare_got(MEMCACHED_FAILURE, rc, memcached_strerror(NULL, rc));
+  test_compare_got(MEMCACHED_SUCCESS, rc, memcached_strerror(NULL, rc));
 
   /* Test a clean set */
   test_compare(MEMCACHED_SUCCESS,
@@ -3750,8 +3728,8 @@ static test_return_t selection_of_namespace_tests(memcached_st *memc)
                memcached_callback_set(memc, MEMCACHED_CALLBACK_NAMESPACE, NULL));
 
   value= (char*)memcached_callback_get(memc, MEMCACHED_CALLBACK_NAMESPACE, &rc);
-  test_false(value);
-  test_compare_got(MEMCACHED_FAILURE, rc, memcached_strerror(NULL, rc));
+  test_null(value);
+  test_compare_got(MEMCACHED_SUCCESS, rc, memcached_strerror(NULL, rc));
 
   /* Now setup for main test */
   test_compare(MEMCACHED_SUCCESS,
@@ -3771,9 +3749,8 @@ static test_return_t selection_of_namespace_tests(memcached_st *memc)
                  memcached_callback_set(memc, MEMCACHED_CALLBACK_NAMESPACE, NULL));
 
     value= (char*)memcached_callback_get(memc, MEMCACHED_CALLBACK_NAMESPACE, &rc);
-    test_false(value);
-    test_true(rc == MEMCACHED_FAILURE);
-    test_true(value == NULL);
+    test_null(value);
+    test_compare(MEMCACHED_SUCCESS, rc);
 
     /* Test a long key for failure */
     /* TODO, extend test to determine based on setting, what result should be */
@@ -3803,10 +3780,10 @@ static test_return_t set_namespace(memcached_st *memc)
   const char *key= "mine";
   char *value;
 
-  /* Make sure be default none exists */
+  // Make sure we default to a null namespace
   value= (char*)memcached_callback_get(memc, MEMCACHED_CALLBACK_NAMESPACE, &rc);
   test_null(value);
-  test_compare_got(MEMCACHED_FAILURE, rc, memcached_strerror(NULL, rc));
+  test_compare_got(MEMCACHED_SUCCESS, rc, memcached_strerror(NULL, rc));
 
   /* Test a clean set */
   test_compare(MEMCACHED_SUCCESS,
@@ -4159,10 +4136,9 @@ static test_return_t noreply_test(memcached_st *memc)
 static test_return_t analyzer_test(memcached_st *memc)
 {
   memcached_return_t rc;
-  memcached_stat_st *memc_stat;
   memcached_analysis_st *report;
 
-  memc_stat= memcached_stat(memc, NULL, &rc);
+  memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc);
   test_compare(MEMCACHED_SUCCESS, rc);
   test_true(memc_stat);
 
@@ -4194,68 +4170,85 @@ static test_return_t dump_test(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
-struct test_pool_context_st {
-  memcached_pool_st* pool;
-  memcached_st* mmc;
-};
-
-static void* connection_release(void *arg)
-{
-  test_pool_context_st *resource= static_cast<test_pool_context_st *>(arg);
-
-  usleep(250);
-  // Release all of the memc we are holding
-  assert(memcached_success(memcached_pool_push(resource->pool, resource->mmc)));
-  return arg;
-}
 
 #define POOL_SIZE 10
 static test_return_t connection_pool_test(memcached_st *memc)
 {
   memcached_pool_st* pool= memcached_pool_create(memc, 5, POOL_SIZE);
-  test_true(pool != NULL);
+  test_true(pool);
   memcached_st *mmc[POOL_SIZE];
-  memcached_return_t rc;
 
   // Fill up our array that we will store the memc that are in the pool
   for (size_t x= 0; x < POOL_SIZE; ++x)
   {
-    mmc[x]= memcached_pool_pop(pool, false, &rc);
-    test_true(mmc[x] != NULL);
+    memcached_return_t rc;
+    mmc[x]= memcached_pool_fetch(pool, NULL, &rc);
     test_compare(MEMCACHED_SUCCESS, rc);
+    test_true(mmc[x]);
   }
 
   // All memc should be gone
-  test_true(memcached_pool_pop(pool, false, &rc) == NULL);
-  test_compare(MEMCACHED_SUCCESS, rc);
+  {
+    memcached_return_t rc;
+    test_null(memcached_pool_fetch(pool, NULL, &rc));
+    test_compare(MEMCACHED_NOTFOUND, rc);
+  }
 
-  pthread_t tid;
-  test_pool_context_st item= { pool, mmc[9] };
+  // Release them..
+  for (size_t x= 0; x < POOL_SIZE; ++x)
+  {
+    if (mmc[x])
+    {
+      test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[x]));
+    }
+  }
+  test_true(memcached_pool_destroy(pool) == memc);
 
-  pthread_create(&tid, NULL, connection_release, &item);
-  mmc[9]= memcached_pool_pop(pool, true, &rc);
-  test_compare(MEMCACHED_SUCCESS, rc);
-  pthread_join(tid, NULL);
-  test_true(mmc[9]);
-  const char *key= "key";
-  size_t keylen= strlen(key);
+  return TEST_SUCCESS;
+}
+
+static test_return_t connection_pool2_test(memcached_st *memc)
+{
+  memcached_pool_st* pool= memcached_pool_create(memc, 5, POOL_SIZE);
+  test_true(pool);
+  memcached_st *mmc[POOL_SIZE];
+
+  // Fill up our array that we will store the memc that are in the pool
+  for (size_t x= 0; x < POOL_SIZE; ++x)
+  {
+    memcached_return_t rc;
+    mmc[x]= memcached_pool_fetch(pool, NULL, &rc);
+    test_compare(MEMCACHED_SUCCESS, rc);
+    test_true(mmc[x]);
+  }
+
+  // All memc should be gone
+  {
+    memcached_return_t rc;
+    test_null(memcached_pool_fetch(pool, NULL, &rc));
+    test_compare(MEMCACHED_NOTFOUND, rc);
+  }
 
   // verify that I can do ops with all connections
   test_compare(MEMCACHED_SUCCESS,
-               memcached_set(mmc[0], key, keylen, "0", 1, 0, 0));
+               memcached_set(mmc[0],
+                             test_literal_param("key"),
+                             "0", 1, 0, 0));
 
   for (uint64_t x= 0; x < POOL_SIZE; ++x)
   {
     uint64_t number_value;
     test_compare(MEMCACHED_SUCCESS,
-                 memcached_increment(mmc[x], key, keylen, 1, &number_value));
+                 memcached_increment(mmc[x], 
+                                     test_literal_param("key"),
+                                     1, &number_value));
     test_compare(number_value, (x+1));
   }
 
   // Release them..
   for (size_t x= 0; x < POOL_SIZE; ++x)
   {
-    test_compare(MEMCACHED_SUCCESS, memcached_pool_push(pool, mmc[x]));
+    test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[x]));
   }
 
 
@@ -4263,22 +4256,148 @@ static test_return_t connection_pool_test(memcached_st *memc)
    * of the connections in the pool. It should however be enabled
    * when I push the item into the pool
    */
-  mmc[0]= memcached_pool_pop(pool, false, &rc);
+  mmc[0]= memcached_pool_fetch(pool, NULL, NULL);
   test_true(mmc[0]);
 
-  rc= memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999);
-  test_compare(MEMCACHED_SUCCESS, rc);
+  test_compare(MEMCACHED_SUCCESS,
+               memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999));
 
-  mmc[1]= memcached_pool_pop(pool, false, &rc);
-  test_true(mmc[1]);
+  {
+    memcached_return_t rc;
+    mmc[1]= memcached_pool_fetch(pool, NULL, &rc);
+    test_true(mmc[1]);
+    test_compare(MEMCACHED_SUCCESS, rc);
+  }
 
   test_compare(UINT64_C(9999), memcached_behavior_get(mmc[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
-  test_compare(MEMCACHED_SUCCESS, memcached_pool_push(pool, mmc[1]));
-  test_compare(MEMCACHED_SUCCESS, memcached_pool_push(pool, mmc[0]));
+  test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[1]));
+  test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[0]));
+
+  {
+    memcached_return_t rc;
+    mmc[0]= memcached_pool_fetch(pool, NULL, &rc);
+    test_true(mmc[0]);
+    test_compare(MEMCACHED_SUCCESS, rc);
+  }
 
-  mmc[0]= memcached_pool_pop(pool, false, &rc);
   test_compare(UINT64_C(9999), memcached_behavior_get(mmc[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
-  test_compare(MEMCACHED_SUCCESS, memcached_pool_push(pool, mmc[0]));
+  test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[0]));
+
+  test_true(memcached_pool_destroy(pool) == memc);
+
+  return TEST_SUCCESS;
+}
+
+struct test_pool_context_st {
+  volatile memcached_return_t rc;
+  memcached_pool_st* pool;
+  memcached_st* mmc;
+  sem_t _lock;
+
+  test_pool_context_st(memcached_pool_st *pool_arg, memcached_st *memc_arg):
+    rc(MEMCACHED_FAILURE),
+    pool(pool_arg),
+    mmc(memc_arg)
+  {
+    sem_init(&_lock, 0, 0);
+  }
+
+  void wait()
+  {
+    sem_wait(&_lock);
+  }
+
+  void release()
+  {
+    sem_post(&_lock);
+  }
+
+  ~test_pool_context_st()
+  {
+    sem_destroy(&_lock);
+  }
+};
+
+static void* connection_release(void *arg)
+{
+  test_pool_context_st *resource= static_cast<test_pool_context_st *>(arg);
+  assert(resource);
+  if (resource == NULL)
+  {
+    abort();
+  }
+
+  // Release all of the memc we are holding 
+  resource->rc= memcached_pool_release(resource->pool, resource->mmc);
+  resource->release();
+
+  pthread_exit(arg);
+}
+
+static test_return_t connection_pool3_test(memcached_st *memc)
+{
+#ifdef __APPLE__
+  return TEST_SKIPPED;
+#endif
+
+  memcached_pool_st* pool= memcached_pool_create(memc, 1, 1);
+  test_true(pool);
+
+  memcached_st *pool_memc;
+  {
+    memcached_return_t rc;
+    pool_memc= memcached_pool_fetch(pool, NULL, &rc);
+    test_compare(MEMCACHED_SUCCESS, rc);
+    test_true(pool_memc);
+  }
+
+  /*
+    @note This comment was written to describe what was believed to be the original authors intent.
+
+    This portion of the test creates a thread that will wait until told to free a memcached_st
+    that will be grabbed by the main thread.
+
+    It is believed that this tests whether or not we are handling ownership correctly.
+  */
+  pthread_t tid;
+  test_pool_context_st item(pool, pool_memc);
+
+  test_zero(pthread_create(&tid, NULL, connection_release, &item));
+  item.wait();
+
+  memcached_return_t rc;
+  memcached_st *pop_memc;
+  // We do a hard loop, and try N times
+  int counter= 5;
+  do
+  {
+    struct timespec relative_time= { 0, 0 };
+    pop_memc= memcached_pool_fetch(pool, &relative_time, &rc);
+
+    if (memcached_success(rc))
+    {
+      break;
+    }
+
+    if (memcached_failed(rc))
+    {
+      test_null(pop_memc);
+      test_true(rc != MEMCACHED_TIMEOUT); // As long as relative_time is zero, MEMCACHED_TIMEOUT is invalid
+    }
+  } while (--counter);
+
+  if (memcached_failed(rc)) // Cleanup thread since we will exit once we test.
+  {
+    pthread_join(tid, NULL);
+    test_compare(MEMCACHED_SUCCESS, rc);
+  }
+
+  {
+    int pthread_ret= pthread_join(tid, NULL);
+    test_true(pthread_ret == 0 or pthread_ret == ESRCH);
+  }
+  test_compare(MEMCACHED_SUCCESS, rc);
+  test_true(pool_memc == pop_memc);
 
   test_true(memcached_pool_destroy(pool) == memc);
 
@@ -4428,13 +4547,9 @@ static test_return_t hsieh_avaibility_test (memcached_st *memc)
 {
   test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH));
 
-  memcached_return_t expected_rc= MEMCACHED_INVALID_ARGUMENTS;
-#ifdef HAVE_HSIEH_HASH
-  expected_rc= MEMCACHED_SUCCESS;
-#endif
-  memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
-                                                (uint64_t)MEMCACHED_HASH_HSIEH);
-  test_compare(expected_rc, rc);
+  test_compare(MEMCACHED_SUCCESS, 
+               memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
+                                      (uint64_t)MEMCACHED_HASH_HSIEH));
 
   return TEST_SUCCESS;
 }
@@ -4443,13 +4558,8 @@ static test_return_t murmur_avaibility_test (memcached_st *memc)
 {
   test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR));
 
-  memcached_return_t expected_rc= MEMCACHED_INVALID_ARGUMENTS;
-#ifdef HAVE_MURMUR_HASH
-  expected_rc= MEMCACHED_SUCCESS;
-#endif
-  memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
-                                                (uint64_t)MEMCACHED_HASH_MURMUR);
-  test_compare(expected_rc, rc);
+  test_compare(MEMCACHED_SUCCESS,
+               memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR));
 
   return TEST_SUCCESS;
 }
@@ -5833,10 +5943,12 @@ test_st tests[] ={
   {"bad_key", true, (test_callback_fn*)bad_key_test },
   {"memcached_server_cursor", true, (test_callback_fn*)memcached_server_cursor_test },
   {"read_through", true, (test_callback_fn*)read_through },
-  {"delete_through", true, (test_callback_fn*)delete_through },
+  {"delete_through", true, (test_callback_fn*)test_MEMCACHED_CALLBACK_DELETE_TRIGGER },
   {"noreply", true, (test_callback_fn*)noreply_test},
   {"analyzer", true, (test_callback_fn*)analyzer_test},
-  {"connectionpool", true, (test_callback_fn*)connection_pool_test },
+  {"memcached_pool_st", true, (test_callback_fn*)connection_pool_test },
+  {"memcached_pool_st #2", true, (test_callback_fn*)connection_pool2_test },
+  {"memcached_pool_st #3", true, (test_callback_fn*)connection_pool3_test },
   {"memcached_pool_test", true, (test_callback_fn*)memcached_pool_test },
   {"test_get_last_disconnect", true, (test_callback_fn*)test_get_last_disconnect},
   {"verbosity", true, (test_callback_fn*)test_verbosity},
@@ -5856,6 +5968,7 @@ test_st behavior_tests[] ={
   {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", false, (test_callback_fn*)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test},
   {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", false, (test_callback_fn*)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test},
   {"MEMCACHED_BEHAVIOR_POLL_TIMEOUT", false, (test_callback_fn*)MEMCACHED_BEHAVIOR_POLL_TIMEOUT_test},
+  {"MEMCACHED_CALLBACK_DELETE_TRIGGER_and_MEMCACHED_BEHAVIOR_NOREPLY", false, (test_callback_fn*)test_MEMCACHED_CALLBACK_DELETE_TRIGGER_and_MEMCACHED_BEHAVIOR_NOREPLY},
   {0, 0, 0}
 };