Browse Source

Fix usage of throw for server failure.

Brian Aker 12 years ago
parent
commit
5dce100bf5
9 changed files with 92 additions and 58 deletions
  1. 21 19
      libtest/cmdline.h
  2. 2 3
      libtest/fatal.cc
  3. 1 1
      libtest/fatal.hpp
  4. 1 1
      libtest/gearmand.cc
  5. 2 1
      libtest/has.cc
  6. 2 0
      libtest/port.h
  7. 39 33
      libtest/server.cc
  8. 6 0
      libtest/server_container.cc
  9. 18 0
      libtest/unittest.cc

+ 21 - 19
libtest/cmdline.h

@@ -56,6 +56,26 @@ public:
     INVALID= 127
   };
 
+  static const char* toString(error_t arg)
+  {
+    switch (arg)
+    {
+    case Application::SUCCESS:
+      return "EXIT_SUCCESS";
+
+    case Application::FAILURE:
+      return "EXIT_FAILURE";
+
+    case Application::INVALID:
+      return "127";
+
+    default:
+      break;
+    }
+
+    return "EXIT_UNKNOWN";
+  }
+
   class Pipe {
   public:
     Pipe(int);
@@ -185,25 +205,7 @@ private:
 
 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
 {
-  switch (arg)
-  {
-    case Application::SUCCESS:
-      output << "EXIT_SUCCESS";
-      break;
-
-    case Application::FAILURE:
-      output << "EXIT_FAILURE";
-      break;
-
-    case Application::INVALID:
-      output << "127";
-      break;
-
-    default:
-      output << "EXIT_UNKNOWN";
-  }
-
-  return output;
+  return output << Application::toString(arg);
 }
 
 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);

+ 2 - 3
libtest/fatal.cc

@@ -86,7 +86,7 @@ void fatal::increment_disabled_counter()
 }
 
 disconnected::disconnected(const char *file_arg, int line_arg, const char *func_arg,
-                           const char *instance, const in_port_t port,
+                           const std::string& instance, const in_port_t port,
                            const char *format, ...) :
   std::runtime_error(func_arg),
   _port(port),
@@ -94,14 +94,13 @@ disconnected::disconnected(const char *file_arg, int line_arg, const char *func_
   _file(file_arg),
   _func(func_arg)
 {
-  strncpy(_instance, instance, sizeof(_instance));
   va_list args;
   va_start(args, format);
   char last_error[BUFSIZ];
   (void)vsnprintf(last_error, sizeof(last_error), format, args);
   va_end(args);
 
-  snprintf(_error_message, sizeof(_error_message), "%s", last_error);
+  snprintf(_error_message, sizeof(_error_message), "%s:%u %s", instance.c_str(), uint32_t(port), last_error);
 }
 
 } // namespace libtest

+ 1 - 1
libtest/fatal.hpp

@@ -101,7 +101,7 @@ private:
 class disconnected : std::runtime_error
 {
 public:
-  disconnected(const char *file, int line, const char *func, const char *instance, const in_port_t port, const char *format, ...);
+  disconnected(const char *file, int line, const char *func, const std::string&, const in_port_t port, const char *format, ...);
 
   const char* what() const throw()
   {

+ 1 - 1
libtest/gearmand.cc

@@ -93,7 +93,7 @@ public:
       
       if (out_of_ban_killed() == false)
       {
-        Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
+        Error << hostname().c_str() << ":" << port() << " " << gearman_client_error(client);
       }
     }
     else

+ 2 - 1
libtest/has.cc

@@ -82,7 +82,8 @@ bool has_gearmand()
   {
     std::stringstream arg_buffer;
 
-    if (getenv("PWD") and strcmp(MEMCACHED_BINARY, "gearmand/gearmand") == 0)
+    if (getenv("PWD") and 
+        ((strcmp(GEARMAND_BINARY, "./gearmand/gearmand") == 0) or (strcmp(GEARMAND_BINARY, "gearmand/gearmand") == 0)))
     {
       arg_buffer << getenv("PWD");
       arg_buffer << "/";

+ 2 - 0
libtest/port.h

@@ -40,6 +40,8 @@
 
 #pragma once
 
+#define LIBTEST_FAIL_PORT 23
+
 namespace libtest {
 
 LIBTEST_API

+ 39 - 33
libtest/server.cc

@@ -178,6 +178,12 @@ bool Server::start()
   }
 #endif
 
+  if (port() == LIBTEST_FAIL_PORT)
+  {
+    throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
+                                hostname(), port(), "Called failure");
+  }
+
   if (getenv("YATL_PTRCHECK_SERVER"))
   {
     _app.use_ptrcheck();
@@ -189,15 +195,16 @@ bool Server::start()
 
   if (args(_app) == false)
   {
-    Error << "Could not build command()";
-    return false;
+    throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
+                                hostname(), port(), "Could not build command()");
   }
 
   libtest::release_port(_port);
   Application::error_t ret;
   if (Application::SUCCESS !=  (ret= _app.run()))
   {
-    Error << "Application::run() " << ret;
+    throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
+                                hostname(), port(), "Application::run() %s", libtest::Application::toString(ret));
     return false;
   }
   _running= _app.print();
@@ -225,11 +232,12 @@ bool Server::start()
 
         char buf[PATH_MAX];
         char *getcwd_buf= getcwd(buf, sizeof(buf));
-        throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
-                             "Unable to open pidfile in %s for: %s stderr:%s",
-                             getcwd_buf ? getcwd_buf : "",
-                             _running.c_str(),
-                             _app.stderr_c_str());
+        throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
+                                    hostname(), port(),
+                                    "Unable to open pidfile in %s for: %s stderr:%s",
+                                    getcwd_buf ? getcwd_buf : "",
+                                    _running.c_str(),
+                                    _app.stderr_c_str());
       }
     }
   }
@@ -265,40 +273,38 @@ bool Server::start()
       _app.slurp();
       if (kill_file(pid_file()) == false)
       {
-        libtest::fatal err(LIBYATL_DEFAULT_PARAM,
-                           "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
-                           this_wait,
-                           int(_running.size()), _running.c_str(),
-                           int(_app.stderr_result_length()), _app.stderr_c_str());
-
-        stream::cerr(err.file(), err.line(), err.func()) << err.what();
+        throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
+                                    hostname(), port(),
+                                    "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
+                                    this_wait,
+                                    int(_running.size()), _running.c_str(),
+                                    int(_app.stderr_result_length()), _app.stderr_c_str());
       }
       else
       {
-        libtest::fatal err(LIBYATL_DEFAULT_PARAM, 
-                           "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
-                           int(_app.pid()),
-                           _app.check() ? "true" : "false",
-                           this_wait,
-                           int(_running.size()), _running.c_str(),
-                           int(_app.stderr_result_length()), _app.stderr_c_str());
-
-        stream::cerr(err.file(), err.line(), err.func()) << err.what();
+        throw libtest::disconnected(LIBYATL_DEFAULT_PARAM, 
+                                    hostname(), port(),
+                                    "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
+                                    int(_app.pid()),
+                                    _app.check() ? "true" : "false",
+                                    this_wait,
+                                    int(_running.size()), _running.c_str(),
+                                    int(_app.stderr_result_length()), _app.stderr_c_str());
       }
     }
     else
     {
-      libtest::fatal err(LIBYATL_DEFAULT_PARAM,
-                         "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
-                         int(_app.pid()),
-                         _app.check() ? "true" : "false",
-                         this_wait,
-                         int(_running.size()), _running.c_str(),
-                         int(_app.stderr_result_length()), _app.stderr_c_str());
-
-      stream::cerr(err.file(), err.line(), err.func()) << err.what();
+      throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
+                                  hostname(), port(),
+                                  "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
+                                  int(_app.pid()),
+                                  _app.check() ? "true" : "false",
+                                  this_wait,
+                                  int(_running.size()), _running.c_str(),
+                                  int(_app.stderr_result_length()), _app.stderr_c_str());
     }
     _running.clear();
+
     return false;
   }
 

+ 6 - 0
libtest/server_container.cc

@@ -311,6 +311,12 @@ bool server_startup_st::start_server(const std::string& server_type, in_port_t t
       }
     }
   }
+  catch (libtest::disconnected err)
+  {
+    stream::cerr(err.file(), err.line(), err.func()) << err.what();
+    delete server;
+    return false;
+  }
   catch (...)
   {
     delete server;

+ 18 - 0
libtest/unittest.cc

@@ -328,6 +328,16 @@ static test_return_t test_skip_false_TEST(void *object)
   return TEST_SUCCESS;
 }
 
+static test_return_t server_startup_fail_TEST(void *object)
+{
+  server_startup_st *servers= (server_startup_st*)object;
+  test_true(servers);
+
+  test_compare(servers->start_server(testing_service, LIBTEST_FAIL_PORT, 0, NULL, true), false);
+
+  return TEST_SUCCESS;
+}
+
 static test_return_t server_startup_TEST(void *object)
 {
   server_startup_st *servers= (server_startup_st*)object;
@@ -799,6 +809,9 @@ static test_return_t check_for_gearman(void *)
 {
   test_skip(true, HAVE_LIBGEARMAN);
   test_skip(true, has_gearmand());
+
+  testing_service= "gearmand";
+
   return TEST_SUCCESS;
 }
 
@@ -806,6 +819,9 @@ static test_return_t check_for_drizzle(void *)
 {
   test_skip(true, HAVE_LIBDRIZZLE);
   test_skip(true, has_drizzled());
+
+  testing_service= "drizzled";
+
   return TEST_SUCCESS;
 }
 
@@ -821,6 +837,7 @@ test_st gearmand_tests[] ={
 #endif
   {"gearmand startup-shutdown", 0, gearmand_cycle_test },
   {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
+  {"server_startup(fail)", 0, server_startup_fail_TEST },
   {0, 0, 0}
 };
 
@@ -842,6 +859,7 @@ 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 },
+  {"server_startup(fail)", 0, server_startup_fail_TEST },
   {0, 0, 0}
 };