Extend pool API for fetch/release. Fix concurrency issue in test case.
[awesomized/libmemcached] / tests / mem_functions.cc
index c6f627f7d36bb61430019339db9a21697132f829..cca23464e01ac16d9b63aa21e2c5cdef6ca3395d 100644 (file)
@@ -4192,41 +4192,6 @@ static test_return_t dump_test(memcached_st *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);
-  }
-
-  ~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();
-  }
-
-  sem_wait(&resource->lock);
-  // Release all of the memc we are holding
-  resource->rc= memcached_pool_push(resource->pool, resource->mmc);
-
-  pthread_exit(arg);
-}
 
 #define POOL_SIZE 10
 static test_return_t connection_pool_test(memcached_st *memc)
@@ -4234,55 +4199,21 @@ static test_return_t connection_pool_test(memcached_st *memc)
   memcached_pool_st* pool= memcached_pool_create(memc, 5, POOL_SIZE);
   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]);
+    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_null(memcached_pool_pop(pool, false, &rc));
-  test_compare(MEMCACHED_SUCCESS, rc);
-
-  /*
-    @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, mmc[9]);
-
-    test_zero(pthread_create(&tid, NULL, connection_release, &item));
-    mmc[9]= memcached_pool_pop(pool, true, &rc);
-    if (rc != MEMCACHED_SUCCESS)
-    {
-      sem_post(&item.lock);
-      pthread_join(tid, NULL);
-    }
-    else if (rc == MEMCACHED_TIMEOUT)
-    {
-      Error << "Possible timing issue with memcached_pool_pop";
-    }
-    else
-    {
-      test_compare(MEMCACHED_SUCCESS, rc);
-    }
-
-    sem_post(&item.lock);
-    pthread_join(tid, NULL);
-    if (rc == MEMCACHED_SUCCESS)
-    {
-      test_compare(MEMCACHED_SUCCESS, item.rc);
-      test_true(mmc[9]);
-    }
+    memcached_return_t rc;
+    test_null(memcached_pool_fetch(pool, NULL, &rc));
+    test_compare(MEMCACHED_NOTFOUND, rc);
   }
 
   // Release them..
@@ -4290,7 +4221,7 @@ static test_return_t connection_pool_test(memcached_st *memc)
   {
     if (mmc[x])
     {
-      test_compare(MEMCACHED_SUCCESS, memcached_pool_push(pool, mmc[x]));
+      test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[x]));
     }
   }
   test_true(memcached_pool_destroy(pool) == memc);
@@ -4303,19 +4234,22 @@ 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];
-  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]);
+    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_null(memcached_pool_pop(pool, false, &rc));
-  test_compare(MEMCACHED_SUCCESS, rc);
+  {
+    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,
@@ -4336,7 +4270,7 @@ static test_return_t connection_pool2_test(memcached_st *memc)
   // 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]));
   }
 
 
@@ -4344,22 +4278,137 @@ static test_return_t connection_pool2_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]);
 
   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)
+{
+  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;
+  int counter= 5;
+  do
+  {
+    struct timespec relative_time= { 0, 0 };
+    pop_memc= memcached_pool_fetch(pool, &relative_time, &rc);
+
+    if (memcached_failed(rc))
+    {
+      test_null(pop_memc);
+    }
+  } while (rc == MEMCACHED_TIMEOUT and --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);
 
@@ -5910,6 +5959,7 @@ test_st tests[] ={
   {"analyzer", true, (test_callback_fn*)analyzer_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},