Browse Source

Updating/merging to latest libtest

Brian Aker 12 years ago
parent
commit
f903ee45a9

+ 17 - 0
libtest/comparison.hpp

@@ -41,6 +41,7 @@
 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
 #include <libmemcached-1.0/memcached.h>
 #include <libmemcachedutil-1.0/ostream.hpp>
+#include <libtest/memcached.hpp>
 #endif
 
 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
@@ -121,6 +122,22 @@ bool _compare_zero(const char *file, int line, const char *func, T_comparable __
   return true;
 }
 
+template <class T1_comparable, class T2_comparable>
+bool _ne_compare(const char *file, int line, const char *func, T1_comparable __expected, T2_comparable __actual, bool io_error= true)
+{
+  if (__expected == __actual)
+  {
+    if (io_error)
+    {
+      libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"";
+    }
+
+    return false;
+  }
+
+  return true;
+}
+
 template <class T_comparable, class T_expression_string>
 bool _assert_truth(const char *file, int line, const char *func, T_comparable __truth, T_expression_string __expression, const char* __explain= NULL)
 {

+ 11 - 0
libtest/m4/memcached.m4

@@ -0,0 +1,11 @@
+AX_PROG_MYSQLD
+AX_WITH_PROG(MEMCACHED_BINARY, [memcached])
+AS_IF([test -f "$ac_cv_path_MEMCACHED_BINARY"],
+      [
+        AC_DEFINE([HAVE_MEMCACHED_BINARY], [1], [If Memcached binary is available])
+        AC_DEFINE_UNQUOTED([MEMCACHED_BINARY], "$ac_cv_path_MEMCACHED_BINARY", [Name of the memcached binary used in make test])
+       ],
+       [
+        AC_DEFINE([HAVE_MEMCACHED_BINARY], [1], [If Memcached binary is available])
+        AC_DEFINE([MEMCACHED_BINARY], ["memcached/memcached"], [Name of the memcached binary used in make test])
+      ])

+ 2 - 12
libtest/memcached.cc

@@ -150,7 +150,7 @@ public:
 
     if (memcached_failed(rc) or ret == false)
     {
-      Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
+      error(memcached_strerror(NULL, rc));
     }
 
     return ret;
@@ -332,16 +332,6 @@ public:
 
   bool ping()
   {
-    // Memcached is slow to start, so we need to do this
-    if (pid_file().empty() == false)
-    {
-      if (wait_for_pidfile() == false)
-      {
-        Error << "Pidfile was not found:" << pid_file();
-        return -1;
-      }
-    }
-
     memcached_return_t rc;
     bool ret;
 
@@ -356,7 +346,7 @@ public:
 
     if (memcached_failed(rc) or ret == false)
     {
-      Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
+      error(memcached_strerror(NULL, rc));
     }
 
     return ret;

+ 18 - 2
libtest/server.h

@@ -204,12 +204,25 @@ public:
     _log_file.clear();
   }
 
-  bool args(Application&);
-
   pid_t pid() const;
 
   bool has_pid() const;
 
+  virtual bool has_pid_file() const
+  {
+    return true;
+  }
+
+  const std::string& error()
+  {
+    return _error;
+  }
+
+  void error(std::string arg)
+  {
+    _error= arg;
+  }
+
   virtual bool wait_for_pidfile() const;
 
   bool check_pid(pid_t pid_arg) const
@@ -260,6 +273,9 @@ private:
   bool set_socket_file();
   void reset_pid();
   bool out_of_ban_killed_;
+  bool args(Application&);
+
+  std::string _error;
 };
 
 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);

+ 21 - 4
libtest/server_container.cc

@@ -55,6 +55,11 @@ static inline std::string &rtrim(std::string &s)
 
 namespace libtest {
 
+Server* server_startup_st::last()
+{
+  return servers.back();
+}
+
 void server_startup_st::push_server(Server *arg)
 {
   servers.push_back(arg);
@@ -176,6 +181,11 @@ bool server_startup_st::validate()
 }
 
 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
+{
+  construct.start_server(server_type, try_port, argc, argv, opt_startup_message);
+}
+
+bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
 {
   if (try_port <= 0)
   {
@@ -235,7 +245,7 @@ bool server_startup(server_startup_st& construct, const std::string& server_type
       {
         if (HAVE_LIBMEMCACHED)
         {
-          server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
+          server= build_memcached_sasl("localhost", try_port, username(), password());
         }
       }
     }
@@ -308,12 +318,14 @@ bool server_startup(server_startup_st& construct, const std::string& server_type
     throw;
   }
 
-  construct.push_server(server);
+  push_server(server);
 
   return true;
 }
 
-bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
+bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc,
+                                            const char *argv[],
+                                            const bool opt_startup_message)
 {
   (void)try_port;
   Outn();
@@ -403,7 +415,12 @@ bool server_startup_st::start_socket_server(const std::string& server_type, cons
     }
     else
     {
-      Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
+      if (opt_startup_message)
+      {
+        Outn();
+        Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
+        Outn();
+      }
     }
   }
   catch (...)

+ 3 - 1
libtest/server_container.h

@@ -68,7 +68,8 @@ public:
 
   bool validate();
 
-  bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
+  bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message= true);
+  bool start_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message= true);
 
   uint32_t count() const
   {
@@ -121,6 +122,7 @@ public:
   bool check() const;
 
   void push_server(Server *);
+  Server* last();
   Server *pop_server();
 
   unsigned long int servers_to_run() const

+ 7 - 1
libtest/test.h

@@ -92,6 +92,12 @@ do \
   } \
 } while (0)
 
+#define test_true_got(A, B) test_true(A);
+#define test_true_hint(A, B) test_true(A);
+
+#define test_compare_hint(A, B, C) test_compare(A, B);
+#define test_compare_got(A, B, C) test_compare(A, B);
+
 #define test_skip(__expected, __actual) \
 do \
 { \
@@ -144,7 +150,7 @@ do \
 #define test_ne_compare(__expected, __actual) \
 do \
 { \
-  if (libtest::_ne_compare_hint(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
+  if (libtest::_ne_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
   { \
     libtest::create_core(); \
     return TEST_FAILURE; \

+ 32 - 27
libtest/unittest.cc

@@ -51,6 +51,8 @@
 
 using namespace libtest;
 
+static std::string testing_service;
+
 static test_return_t LIBTOOL_COMMAND_test(void *)
 {
   test_true(getenv("LIBTOOL_COMMAND"));
@@ -325,39 +327,37 @@ static test_return_t test_skip_false_TEST(void *object)
   return TEST_SUCCESS;
 }
 
-static test_return_t memcached_cycle_test(void *object)
+static test_return_t server_startup_TEST(void *object)
 {
   server_startup_st *servers= (server_startup_st*)object;
   test_true(servers);
 
-  if (MEMCACHED_BINARY and HAVE_LIBMEMCACHED) 
-  {
-    test_true(has_memcached());
-    test_true(server_startup(*servers, "memcached", get_free_port(), 0, NULL));
+  test_true(servers->start_server(testing_service, get_free_port(), 0, NULL, true));
 
-    return TEST_SUCCESS;
-  }
+  test_true(servers->last());
+  pid_t last_pid= servers->last()->pid();
 
-  return TEST_SKIPPED;
+  test_compare(servers->last()->pid(), last_pid);
+  test_true(last_pid > 1);
+  test_compare(kill(last_pid, 0), 0);
+
+  test_true(servers->shutdown());
+#if 0
+  test_compare(servers->last()->pid(), -1);
+  test_compare(kill(last_pid, 0), -1);
+#endif
+
+  return TEST_SUCCESS;
 }
 
-static test_return_t memcached_socket_cycle_test(void *object)
+static test_return_t socket_server_startup_TEST(void *object)
 {
   server_startup_st *servers= (server_startup_st*)object;
   test_true(servers);
 
-  if (MEMCACHED_BINARY)
-  {
-    if (HAVE_LIBMEMCACHED)
-    {
-      test_true(has_memcached());
-      test_true(servers->start_socket_server("memcached", get_free_port(), 0, NULL));
+  test_true(servers->start_socket_server(testing_service, get_free_port(), 0, NULL, true));
 
-      return TEST_SUCCESS;
-    }
-  }
-
-  return TEST_SKIPPED;
+  return TEST_SUCCESS;
 }
 
 static test_return_t memcached_sasl_test(void *object)
@@ -821,18 +821,23 @@ test_st gearmand_tests[] ={
   {0, 0, 0}
 };
 
-static test_return_t check_for_libmemcached(void *)
+static test_return_t check_for_libmemcached(void* object)
 {
   test_skip(true, HAVE_LIBMEMCACHED);
   test_skip(true, has_memcached());
+
+  server_startup_st *servers= (server_startup_st*)object;
+  test_true(servers);
+  servers->clear();
+
+  testing_service= "memcached";
+
   return TEST_SUCCESS;
 }
 
-test_st memcached_tests[] ={
-  {"memcached startup-shutdown", 0, memcached_cycle_test },
-  {"memcached-light startup-shutdown", 0, memcached_light_cycle_TEST },
-  {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
-  {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test },
+test_st memcached_TESTS[] ={
+  {"memcached startup-shutdown", 0, server_startup_TEST },
+  {"memcached(socket file) startup-shutdown", 0, socket_server_startup_TEST },
   {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
   {0, 0, 0}
 };
@@ -983,7 +988,7 @@ collection_st collection[] ={
   {"directories", 0, 0, directories_tests},
   {"comparison", 0, 0, comparison_tests},
   {"gearmand", check_for_gearman, 0, gearmand_tests},
-  {"memcached", check_for_libmemcached, 0, memcached_tests},
+  {"memcached", check_for_libmemcached, 0, memcached_TESTS },
   {"drizzled", check_for_drizzle, 0, drizzled_tests},
   {"cmdline", 0, 0, cmdline_tests},
   {"application", 0, 0, application_tests},

+ 1 - 0
libtest/yatl.m4

@@ -7,3 +7,4 @@ AC_SUBST([LIBTEST_VERSION], [1.0])
 AC_CONFIG_FILES([libtest/version.h])
 
 m4_include([libtest/m4/mysql.m4])
+m4_include([libtest/m4/memcached.m4])