Browse Source

Merge with libtest from libmemcached

Brian Aker 12 years ago
parent
commit
b15e8d786d

+ 16 - 12
libtest/cmdline.cc

@@ -2,7 +2,7 @@
  *
  *  Data Differential YATL (i.e. libtest)  library
  *
- *  Copyright (C) 2012 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
  *
  *  Redistribution and use in source and binary forms, with or without
  *  modification, are permitted provided that the following conditions are
@@ -59,6 +59,7 @@ using namespace libtest;
 #include <unistd.h>
 
 #include <algorithm>
+#include <stdexcept>
 
 #ifndef __USE_GNU
 static char **environ= NULL;
@@ -481,8 +482,20 @@ Application::error_t Application::join()
   }
   else if (waited_pid == -1)
   {
+    std::string error_string;
+    if (stdout_result_length())
+    {
+      error_string+= " stdout: ";
+      error_string+= stdout_c_str();
+    }
+
+    if (stderr_result_length())
+    {
+      error_string+= " stderr: ";
+      error_string+= stderr_c_str();
+    }
+    Error << "waitpid() returned errno:" << strerror(errno) << " " << error_string;
     _app_exit_state= Application::UNKNOWN;
-    Error << "waitpid() returned errno:" << strerror(errno);
   }
   else
   {
@@ -613,6 +626,7 @@ void Application::Pipe::reset()
       FATAL(strerror(errno));
     }
 
+    // Since either pipe2() was not found/called we set the pipe directly
     nonblock();
     cloexec();
   }
@@ -817,14 +831,4 @@ int exec_cmdline(const std::string& command, const char *args[], bool use_libtoo
   return int(app.join());
 }
 
-const char *gearmand_binary() 
-{
-  return GEARMAND_BINARY;
-}
-
-const char *drizzled_binary() 
-{
-  return DRIZZLED_BINARY;
-}
-
 } // namespace exec_cmdline

+ 0 - 3
libtest/cmdline.h

@@ -245,7 +245,4 @@ static inline std::ostream& operator<<(std::ostream& output, const enum Applicat
 
 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
 
-const char *gearmand_binary(); 
-const char *drizzled_binary();
-
 }

+ 0 - 1
libtest/collection.cc

@@ -54,7 +54,6 @@ static test_return_t runner_code(libtest::Framework* frame,
     assert(frame);
     assert(frame->runner());
     assert(run->test_fn);
-    assert(frame->creators_ptr());
     return_code= frame->runner()->main(run->test_fn, frame->creators_ptr());
   }
   // Special case where check for the testing of the exception

+ 108 - 0
libtest/exception.cc

@@ -0,0 +1,108 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ *  Data Differential YATL (i.e. libtest)  library
+ *
+ *  Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "libtest/yatlcon.h"
+#include <libtest/common.h>
+#include <cstdarg>
+
+namespace libtest {
+
+exception::exception(const char *file_arg, int line_arg, const char *func_arg):
+  std::exception(),
+  _line(line_arg),
+  _file(file_arg),
+  _func(func_arg),
+  _error_message(NULL),
+  _error_message_size(0)
+{
+}
+
+#ifndef __INTEL_COMPILER
+# pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#endif
+void exception::init(va_list args_)
+{
+  const char *format= va_arg(args_, const char *);
+  int error_message_length= vasprintf(&_error_message, format, args_);
+  assert(error_message_length != -1);
+  if (error_message_length > 0)
+  {
+    _error_message_size= error_message_length +1;
+  }
+}
+
+exception::~exception() throw()
+{
+  if (_error_message)
+  {
+    free(_error_message);
+  }
+}
+
+void exception::what(size_t length_, const char* message_)
+{
+  if (length_ > 0 and message_)
+  {
+    char *ptr= (char*) realloc(_error_message, length_ +1);
+    if (ptr)
+    {
+      _error_message= ptr;
+      memcpy(_error_message, message_, length_);
+      _error_message[length_]= 0;
+    }
+  }
+}
+
+exception::exception(const exception& other) :
+  std::exception(),
+  _line(other._line),
+  _file(other._file),
+  _func(other._func),
+  _error_message_size(0)
+{
+  if (other.length() > 0)
+  {
+    _error_message= (char*) malloc(other.length() +1);
+    if (_error_message)
+    {
+      memcpy(_error_message, other._error_message, other.length());
+      _error_message_size= other.length();
+    }
+  }
+}
+
+} // namespace libtest
+

+ 54 - 11
libtest/failed.h → libtest/exception.hpp

@@ -2,7 +2,7 @@
  *
  *  Data Differential YATL (i.e. libtest)  library
  *
- *  Copyright (C) 2012 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
  *
  *  Redistribution and use in source and binary forms, with or without
  *  modification, are permitted provided that the following conditions are
@@ -36,16 +36,59 @@
 
 #pragma once
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+namespace libtest {
 
-LIBTEST_INTERNAL_API
-  void push_failed_test(const char *collection, const char *test);
+class exception : public std::exception
+{
+public:
+  exception(const char *file, int line, const char *func);
 
-LIBTEST_INTERNAL_API
-  void print_failed_test(void);
+  exception( const exception& );
+
+  virtual ~exception() throw();
+
+  virtual const char* what() const throw()
+  {
+    if (_error_message)
+    {
+      return _error_message;
+    }
+
+    return "";
+  }
+
+  void what(size_t, const char*);
+
+  size_t length() const
+  {
+    return _error_message_size;
+  }
+
+  int line() const
+  {
+    return _line;
+  }
+
+  const char* file() const
+  {
+    return _file;
+  }
+
+  const char* func() const
+  {
+    return _func;
+  }
+
+protected:
+  void init(va_list);
+
+private:
+  int _line;
+  const char*  _file;
+  const char* _func;
+  char* _error_message;
+  size_t _error_message_size;
+};
+
+} // namespace libtest
 
-#ifdef __cplusplus
-}
-#endif

+ 3 - 27
libtest/fatal.hpp → libtest/exception/disconnected.hpp

@@ -2,7 +2,7 @@
  *
  *  Data Differential YATL (i.e. libtest)  library
  *
- *  Copyright (C) 2012 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
  *
  *  Redistribution and use in source and binary forms, with or without
  *  modification, are permitted provided that the following conditions are
@@ -36,20 +36,15 @@
 
 #pragma once
 
-#include <stdexcept>
+#include "libtest/exception.hpp"
 
 namespace libtest {
 
-class disconnected : public std::runtime_error
+class disconnected : public libtest::exception
 {
 public:
   disconnected(const char *file, int line, const char *func, const std::string&, const in_port_t port, ...);
 
-  const char* what() const throw()
-  {
-    return _error_message;
-  }
-
   disconnected(const disconnected&);
 
   // The following are just for unittesting the exception class
@@ -59,28 +54,9 @@ public:
   static uint32_t disabled_counter();
   static void increment_disabled_counter();
 
-  int line() const
-  {
-    return _line;
-  }
-
-  const char* file() const
-  {
-    return _file;
-  }
-
-  const char* func() const
-  {
-    return _func;
-  }
-
 private:
-  char _error_message[BUFSIZ];
   in_port_t _port;
   char _instance[BUFSIZ];
-  int _line;
-  const char*  _file;
-  const char* _func;
 };
 
 } // namespace libtest

+ 14 - 14
libtest/fatal.cc → libtest/exception/fatal.cc

@@ -36,6 +36,7 @@
 
 #include "libtest/yatlcon.h"
 #include <libtest/common.h>
+#include "libtest/exception.hpp"
 #include <cstdarg>
 
 namespace libtest {
@@ -43,7 +44,7 @@ namespace libtest {
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
 
 fatal::fatal(const char *file_arg, int line_arg, const char *func_arg, ...) :
-  __test_result(file_arg, line_arg, func_arg)
+  libtest::exception(file_arg, line_arg, func_arg)
 {
   va_list args;
   va_start(args, func_arg);
@@ -52,7 +53,7 @@ fatal::fatal(const char *file_arg, int line_arg, const char *func_arg, ...) :
 }
 
 fatal::fatal( const fatal& other ) :
-  __test_result(other)
+  libtest::exception(other)
 {
 }
 
@@ -89,11 +90,8 @@ void fatal::increment_disabled_counter() throw()
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
 disconnected::disconnected(const char *file_arg, int line_arg, const char *func_arg,
                            const std::string& instance, const in_port_t port, ...) :
-  std::runtime_error(func_arg),
-  _port(port),
-  _line(line_arg),
-  _file(file_arg),
-  _func(func_arg)
+  libtest::exception(file_arg, line_arg, func_arg),
+  _port(port)
 {
   va_list args;
   va_start(args, port);
@@ -102,17 +100,19 @@ disconnected::disconnected(const char *file_arg, int line_arg, const char *func_
   (void)vsnprintf(last_error, sizeof(last_error), format, args);
   va_end(args);
 
-  snprintf(_error_message, sizeof(_error_message), "%s:%u %s", instance.c_str(), uint32_t(port), last_error);
+  char buffer_error[BUFSIZ];
+  int error_length= snprintf(buffer_error, sizeof(buffer_error), "%s:%u %s", instance.c_str(), uint32_t(port), last_error);
+
+  if (error_length > 0)
+  {
+    what(size_t(error_length), buffer_error);
+  }
 }
 
 disconnected::disconnected(const disconnected& other):
-  std::runtime_error(other._func),
-  _port(other._port),
-  _line(other._line),
-  _file(other._file),
-  _func(other._func)
+  libtest::exception(other),
+  _port(other._port)
 {
-  strncpy(_error_message, other._error_message, BUFSIZ);
   strncpy(_instance, other._instance, BUFSIZ);
 }
 

+ 30 - 1
libtest/result/fatal.hpp → libtest/exception/fatal.hpp

@@ -38,7 +38,7 @@
 
 namespace libtest {
 
-class fatal : public __test_result
+class fatal : public libtest::exception
 {
 public:
   fatal(const char *file, int line, const char *func, ...);
@@ -52,7 +52,36 @@ public:
   static uint32_t disabled_counter() throw();
   static void increment_disabled_counter() throw();
 
+  test_return_t return_code() const
+  {
+    return TEST_SKIPPED;
+  }
+
 private:
 };
 
 } // namespace libtest
+
+#define FATAL(...) \
+do \
+{ \
+  throw libtest::fatal(LIBYATL_DEFAULT_PARAM, __VA_ARGS__); \
+} while (0)
+
+#define FATAL_IF(__expression, ...) \
+do \
+{ \
+  if ((__expression)) { \
+    throw libtest::fatal(LIBYATL_DEFAULT_PARAM, (#__expression)); \
+  } \
+} while (0)
+
+#define FATAL_IF_(__expression, ...) \
+do \
+{ \
+  if ((__expression)) { \
+    throw libtest::fatal(LIBYATL_DEFAULT_PARAM, __VA_ARGS__); \
+  } \
+} while (0)
+
+#define fatal_assert(__assert) if((__assert)) {} else { throw libtest::fatal(LIBYATL_DEFAULT_PARAM, #__assert); }

+ 1 - 1
libtest/framework.cc

@@ -68,7 +68,7 @@ Framework::Framework(libtest::SignalThread& signal_,
   get_world(this);
 }
 
-void Framework::collections(collection_st* collections_)
+void Framework::collections(collection_st collections_[])
 {
   for (collection_st *next= collections_; next and next->name; next++)
   {

+ 1 - 1
libtest/framework.h

@@ -72,7 +72,7 @@ public:
     _destroy= arg;
   }
 
-  void collections(collection_st* arg);
+  void collections(collection_st arg[]);
 
   void set_on_error(test_callback_error_fn *arg)
   {

Some files were not shown because too many files changed in this diff