Sync libtest.
[m6w6/libmemcached] / libtest / test.cc
index 92c1226b24ffca5e7b686926b99753c20051200c..fb18e0ea892772767961cd06c285c40c1dd298b9 100644 (file)
@@ -19,6 +19,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <config.h>
 #include <libtest/common.h>
 
 #include <cassert>
 
 #include <signal.h>
 
-#if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
-#include <curl/curl.h>
-#endif
-
 #ifndef __INTEL_COMPILER
 #pragma GCC diagnostic ignored "-Wold-style-cast"
 #endif
@@ -74,33 +71,13 @@ static long int timedif(struct timeval a, struct timeval b)
   return s + us;
 }
 
-static void cleanup_curl(void)
-{
-#if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
-  curl_global_cleanup();
-#endif
-}
-
 #include <getopt.h>
 #include <unistd.h>
 
 int main(int argc, char *argv[])
 {
-#if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
-  if (curl_global_init(CURL_GLOBAL_ALL))
-  {
-    Error << "curl_global_init(CURL_GLOBAL_ALL) failed";
-    return EXIT_FAILURE;
-  }
-#endif
-
-  if (atexit(cleanup_curl))
-  {
-    Error << "atexit() failed";
-    return EXIT_FAILURE;
-  }
-
   bool opt_repeat= false;
+  bool opt_quiet= false;
   std::string collection_to_run;
 
   // Options parsing
@@ -108,14 +85,17 @@ int main(int argc, char *argv[])
     enum long_option_t {
       OPT_LIBYATL_VERSION,
       OPT_LIBYATL_MATCH_COLLECTION,
+      OPT_LIBYATL_QUIET,
       OPT_LIBYATL_REPEAT
     };
 
     static struct option long_options[]=
     {
-      {"repeat", no_argument, NULL, OPT_LIBYATL_REPEAT},
-      {"collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION},
-      {0, 0, 0, 0}
+      { "version", no_argument, NULL, OPT_LIBYATL_VERSION },
+      { "quiet", no_argument, NULL, OPT_LIBYATL_QUIET },
+      { "repeat", no_argument, NULL, OPT_LIBYATL_REPEAT },
+      { "collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION },
+      { 0, 0, 0, 0 }
     };
 
     int option_index= 0;
@@ -132,6 +112,10 @@ int main(int argc, char *argv[])
       case OPT_LIBYATL_VERSION:
         break;
 
+      case OPT_LIBYATL_QUIET:
+        opt_quiet= true;
+        break;
+
       case OPT_LIBYATL_REPEAT:
         opt_repeat= true;
         break;
@@ -153,11 +137,27 @@ int main(int argc, char *argv[])
 
   srandom((unsigned int)time(NULL));
 
-  if (getenv("LIBTEST_QUIET") and strcmp(getenv("LIBTEST_QUIET"), "0") == 0)
+  int repeat;
+  if (bool(getenv("YATL_REPEAT")) and (repeat= atoi(getenv("YATL_REPEAT"))))
   {
-    close(STDOUT_FILENO);
+    opt_repeat= true;
+  }
+
+  if ((getenv("YATL_QUIET") and strcmp(getenv("YATL_QUIET"), "0") == 0) or opt_quiet)
+  {
+    opt_quiet= true;
   }
   else if (getenv("JENKINS_URL"))
+  {
+    if (getenv("YATL_QUIET") and strcmp(getenv("YATL_QUIET"), "1") == 0)
+    { }
+    else
+    {
+      opt_quiet= true;
+    }
+  }
+
+  if (opt_quiet)
   {
     close(STDOUT_FILENO);
   }
@@ -192,18 +192,12 @@ int main(int argc, char *argv[])
   try {
     do {
       exit_code= EXIT_SUCCESS;
-      Framework *world= new Framework();
-
-      if (world == NULL)
-      {
-        Error << "Failed to create Framework()";
-        return EXIT_FAILURE;
-      }
+      Framework world;
 
-      assert(sigignore(SIGPIPE) == 0);
+      fatal_assert(sigignore(SIGPIPE) == 0);
 
       libtest::SignalThread signal;
-      if (not signal.setup())
+      if (signal.setup() == false)
       {
         Error << "Failed to setup signals";
         return EXIT_FAILURE;
@@ -211,10 +205,10 @@ int main(int argc, char *argv[])
 
       Stats stats;
 
-      get_world(world);
+      get_world(&world);
 
       test_return_t error;
-      void *creators_ptr= world->create(error);
+      void *creators_ptr= world.create(error);
 
       switch (error)
       {
@@ -223,13 +217,9 @@ int main(int argc, char *argv[])
 
       case TEST_SKIPPED:
         Out << "SKIP " << argv[0];
-        delete world;
         return EXIT_SUCCESS;
 
-      case TEST_FATAL:
       case TEST_FAILURE:
-      case TEST_MEMORY_ALLOCATION_FAILURE:
-        delete world;
         return EXIT_FAILURE;
       }
 
@@ -252,7 +242,7 @@ int main(int argc, char *argv[])
         wildcard= argv[2];
       }
 
-      for (collection_st *next= world->collections; next and next->name and (not signal.is_shutdown()); next++)
+      for (collection_st *next= world.collections; next and next->name and (not signal.is_shutdown()); next++)
       {
         bool failed= false;
         bool skipped= false;
@@ -264,11 +254,11 @@ int main(int argc, char *argv[])
 
         stats.collection_total++;
 
-        test_return_t collection_rc= world->startup(creators_ptr);
+        test_return_t collection_rc= world.startup(creators_ptr);
 
         if (collection_rc == TEST_SUCCESS and next->pre)
         {
-          collection_rc= world->runner()->pre(next->pre, creators_ptr);
+          collection_rc= world.runner()->pre(next->pre, creators_ptr);
         }
 
         switch (collection_rc)
@@ -276,7 +266,6 @@ int main(int argc, char *argv[])
         case TEST_SUCCESS:
           break;
 
-        case TEST_FATAL:
         case TEST_FAILURE:
           Out << next->name << " [ failed ]";
           failed= true;
@@ -288,8 +277,8 @@ int main(int argc, char *argv[])
           skipped= true;
           goto cleanup;
 
-        case TEST_MEMORY_ALLOCATION_FAILURE:
-          test_assert(0, "Allocation failure, or unknown return");
+        default:
+          throw fatal_message("invalid return code");
         }
 
         Out << "Collection: " << next->name;
@@ -306,20 +295,20 @@ int main(int argc, char *argv[])
 
           test_return_t return_code;
           try {
-            if (test_success(return_code= world->item.startup(creators_ptr)))
+            if (test_success(return_code= world.item.startup(creators_ptr)))
             {
-              if (test_success(return_code= world->item.flush(creators_ptr, run)))
+              if (test_success(return_code= world.item.flush(creators_ptr, run)))
               {
                 // @note pre will fail is SKIPPED is returned
-                if (test_success(return_code= world->item.pre(creators_ptr)))
+                if (test_success(return_code= world.item.pre(creators_ptr)))
                 {
                   { // Runner Code
                     gettimeofday(&start_time, NULL);
-                    assert(world->runner());
+                    assert(world.runner());
                     assert(run->test_fn);
                     try 
                     {
-                      return_code= world->runner()->run(run->test_fn, creators_ptr);
+                      return_code= world.runner()->run(run->test_fn, creators_ptr);
                     }
                     // Special case where check for the testing of the exception
                     // system.
@@ -342,7 +331,7 @@ int main(int argc, char *argv[])
                 }
 
                 // @todo do something if post fails
-                (void)world->item.post(creators_ptr);
+                (void)world.item.post(creators_ptr);
               }
               else if (return_code == TEST_SKIPPED)
               { }
@@ -361,6 +350,11 @@ int main(int argc, char *argv[])
             }
           }
 
+          catch (libtest::fatal &e)
+          {
+            Error << "Fatal exception was thrown: " << e.what();
+            return_code= TEST_FAILURE;
+          }
           catch (std::exception &e)
           {
             Error << "Exception was thrown: " << e.what();
@@ -381,7 +375,6 @@ int main(int argc, char *argv[])
             stats.success++;
             break;
 
-          case TEST_FATAL:
           case TEST_FAILURE:
             stats.failed++;
             failed= true;
@@ -394,11 +387,11 @@ int main(int argc, char *argv[])
             Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
             break;
 
-          case TEST_MEMORY_ALLOCATION_FAILURE:
-            test_assert(0, "Memory Allocation Error");
+          default:
+            throw fatal_message("invalid return code");
           }
 
-          if (test_failed(world->on_error(return_code, creators_ptr)))
+          if (test_failed(world.on_error(return_code, creators_ptr)))
           {
             Error << "Failed while running on_error()";
             signal.set_shutdown(SHUTDOWN_GRACEFUL);
@@ -406,7 +399,7 @@ int main(int argc, char *argv[])
           }
         }
 
-        (void) world->runner()->post(next->post, creators_ptr);
+        (void) world.runner()->post(next->post, creators_ptr);
 
 cleanup:
         if (failed == false and skipped == false)
@@ -424,7 +417,7 @@ cleanup:
           stats.collection_skipped++;
         }
 
-        world->shutdown(creators_ptr);
+        world.shutdown(creators_ptr);
         Outn();
       }
 
@@ -455,8 +448,6 @@ cleanup:
 
       stats_print(&stats);
 
-      delete world;
-
       Outn(); // Generate a blank to break up the messages if make check/test has been run
     } while (exit_code == EXIT_SUCCESS and opt_repeat);
   }
@@ -464,9 +455,13 @@ cleanup:
   {
     std::cerr << e.what() << std::endl;
   }
+  catch (std::exception& e)
+  {
+    std::cerr << e.what() << std::endl;
+  }
   catch (...)
   {
-    std::cerr << "Unknown exception halted execution" << std::endl;
+    std::cerr << "Unknown exception halted execution." << std::endl;
   }
 
   return exit_code;