tests: fix #77
[awesomized/libmemcached] / tests / libmemcached-1.0 / pool.cc
index f1d013598e5d98f114a0508097d4687e04b730f7..457cf962ea62413f4abd40802a63ad8a370002da 100644 (file)
@@ -35,7 +35,7 @@
  *
  */
 
-#include <config.h>
+#include <mem_config.h>
 #include <libtest/test.hpp>
 
 using namespace libtest;
@@ -53,6 +53,9 @@ using namespace libtest;
 #include <tests/pool.h>
 
 #include <pthread.h>
+#include <poll.h>
+
+#include "libmemcached/instance.hpp"
 
 #ifndef __INTEL_COMPILER
 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
@@ -61,20 +64,19 @@ using namespace libtest;
 
 test_return_t memcached_pool_test(memcached_st *)
 {
-  memcached_return_t rc;
   const char *config_string= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com --POOL-MIN=10 --POOL-MAX=32";
 
   char buffer[2048];
-  rc= libmemcached_check_configuration(config_string, sizeof(config_string) -1, buffer, sizeof(buffer));
 
-  test_true_got(rc != MEMCACHED_SUCCESS, buffer);
+  test_compare(libmemcached_check_configuration(config_string, sizeof(config_string) -1, buffer, sizeof(buffer)), MEMCACHED_PARSE_ERROR);
 
   memcached_pool_st* pool= memcached_pool(config_string, strlen(config_string));
-  test_true_got(pool, strerror(errno));
+  test_true(pool);
 
+  memcached_return_t rc;
   memcached_st *memc= memcached_pool_pop(pool, false, &rc);
 
-  test_true(rc == MEMCACHED_SUCCESS);
+  test_compare(rc, MEMCACHED_SUCCESS);
   test_true(memc);
 
   /*
@@ -189,7 +191,7 @@ test_return_t connection_pool2_test(memcached_st *memc)
     test_compare(MEMCACHED_SUCCESS, rc);
   }
 
-  test_compare(UINT64_C(9999), memcached_behavior_get(mmc[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
+  test_compare(uint64_t(9999), memcached_behavior_get(mmc[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
   test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[1]));
   test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[0]));
 
@@ -200,7 +202,7 @@ test_return_t connection_pool2_test(memcached_st *memc)
     test_compare(MEMCACHED_SUCCESS, rc);
   }
 
-  test_compare(UINT64_C(9999), memcached_behavior_get(mmc[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
+  test_compare(uint64_t(9999), memcached_behavior_get(mmc[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
   test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[0]));
 
   test_true(memcached_pool_destroy(pool) == memc);
@@ -238,13 +240,10 @@ struct test_pool_context_st {
   }
 };
 
-static void* connection_release(void *arg)
+static __attribute__((noreturn)) void* connection_release(void *arg)
 {
   test_pool_context_st *resource= static_cast<test_pool_context_st *>(arg);
-  if (resource == NULL)
-  {
-    fatal_message("resource == NULL");
-  }
+  FATAL_IF(resource == NULL);
 
   // Release all of the memc we are holding 
   resource->rc= memcached_pool_release(resource->pool, resource->mmc);
@@ -341,7 +340,7 @@ static memcached_st * create_single_instance_memcached(const memcached_st *origi
    * I only want to hit _one_ server so I know the number of requests I'm
    * sending in the pipeline.
    */
-  memcached_server_instance_st instance= memcached_server_instance_by_position(original_memc, 0);
+  const memcached_instance_st * instance= memcached_server_instance_by_position(original_memc, 0);
 
   char server_string[1024];
   int server_string_length;
@@ -394,17 +393,11 @@ static bool _running= false;
 static void set_running(const bool arg)
 {
   int error;
-  if ((error= pthread_mutex_lock(&mutex)) != 0)
-  {
-    fatal_message(strerror(error));
-  }
+  FATAL_IF_((error= pthread_mutex_lock(&mutex)) != 0, strerror(error));
 
   _running= arg;
 
-  if ((error= pthread_mutex_unlock(&mutex)) != 0)
-  {
-    fatal_message(strerror(error));
-  }
+  FATAL_IF_((error= pthread_mutex_unlock(&mutex)) != 0, strerror(error));
 }
 
 static bool running()
@@ -412,17 +405,11 @@ static bool running()
   int error;
   bool ret;
   
-  if ((error= pthread_mutex_lock(&mutex)) != 0)
-  {
-    fatal_message(strerror(error));
-  }
+  FATAL_IF_((error= pthread_mutex_lock(&mutex)) != 0, strerror(error));
 
   ret= _running;
 
-  if ((error= pthread_mutex_unlock(&mutex)) != 0)
-  {
-    fatal_message(strerror(error));
-  }
+  FATAL_IF_((error= pthread_mutex_unlock(&mutex)) != 0, strerror(error));
 
   return ret;
 }
@@ -459,27 +446,9 @@ static void *worker_thread(void *ctx)
   return NULL;
 }
 
-static void sig_handler(int sig)
-{
-  switch (sig)
-  {
-  case SIGPROF:
-    set_running(false);
-    break;
-
-    break;
-
-  default:
-    Error << __func__ << " caught unknown signal";
-    break;
-  }
-}
-
 #define NUM_THREADS 20
 test_return_t regression_bug_962815(memcached_st *memc)
 {
-  test_skip_valgrind();
-
   pthread_t pid[NUM_THREADS];
 
   test_false(running());
@@ -493,40 +462,36 @@ test_return_t regression_bug_962815(memcached_st *memc)
 
   set_running(true);
 
-  struct itimerval new_value;
-  struct itimerval old_value;
-  memset(&new_value, 0, sizeof(itimerval));
-  memset(&old_value, 0, sizeof(itimerval));
-
-  new_value.it_interval.tv_sec=0;
-  new_value.it_interval.tv_usec=10*1000;
-  new_value.it_value.tv_sec=0;
-  new_value.it_value.tv_usec=10*1000;
-
-  test_true(signal(SIGPROF, sig_handler) != SIG_ERR);
-  test_compare(0, setitimer(ITIMER_PROF, &new_value, &old_value));
-
   for (size_t x=0; x < NUM_THREADS; x++)
   {
     test_compare(0, pthread_create(&pid[x], NULL, worker_thread, (void*)pool));
   }
 
-  for (size_t x=0; x < NUM_THREADS; x++)
   {
-    test_compare(0, pthread_join(pid[x], NULL));
-  }
+    pollfd fds[1];
+    memset(fds, 0, sizeof(pollfd));
+    fds[0].fd= -1; //STDIN_FILENO;
+    fds[0].events= POLLIN;
+    fds[0].revents= 0;
 
-  if (pool)
-  {
-    memcached_pool_destroy(pool);
+    int active_fd;
+    if ((active_fd= poll(fds, 1, 5000)) == -1)
+    {
+      Error << "poll() failed with:" << strerror(errno);
+    }
+    test_zero(active_fd);
+
+    set_running(false);
   }
 
-  if (master)
+  for (size_t x=0; x < NUM_THREADS; x++)
   {
-    memcached_free(master);
+    test_compare(0, pthread_join(pid[x], NULL));
   }
-  test_true(signal(SIGPROF, SIG_DFL) != SIG_ERR);
-  test_compare(0, setitimer(ITIMER_PROF, &old_value, NULL));
+
+  memcached_pool_destroy(pool);
+
+  memcached_free(master);
 
   return TEST_SUCCESS;
 }