Add an exception class for tossing resource error.
[m6w6/libmemcached] / libtest / test.cc
index 52dd03e7b4e60263b744b42e68df826fe9faa545..92c1226b24ffca5e7b686926b99753c20051200c 100644 (file)
@@ -188,257 +188,286 @@ int main(int argc, char *argv[])
   }
 
   int exit_code;
-  do {
-    exit_code= EXIT_SUCCESS;
-    Framework *world= new Framework();
 
-    if (world == NULL)
-    {
-      Error << "Failed to create Framework()";
-      return EXIT_FAILURE;
-    }
-
-    assert(sigignore(SIGPIPE) == 0);
-
-    libtest::SignalThread signal;
-    if (not signal.setup())
-    {
-      Error << "Failed to setup signals";
-      return EXIT_FAILURE;
-    }
-
-    Stats stats;
-
-    get_world(world);
-
-    test_return_t error;
-    void *creators_ptr= world->create(error);
+  try {
+    do {
+      exit_code= EXIT_SUCCESS;
+      Framework *world= new Framework();
 
-    switch (error)
-    {
-    case TEST_SUCCESS:
-      break;
-
-    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;
-    }
-
-    if (getenv("TEST_COLLECTION"))
-    {
-      if (strlen(getenv("TEST_COLLECTION")))
+      if (world == NULL)
       {
-        collection_to_run= getenv("TEST_COLLECTION");
+        Error << "Failed to create Framework()";
+        return EXIT_FAILURE;
       }
-    }
-
-    if (collection_to_run.empty() == false)
-    {
-      Out << "Only testing " <<  collection_to_run;
-    }
 
-    char *wildcard= NULL;
-    if (argc == 3)
-    {
-      wildcard= argv[2];
-    }
-
-    for (collection_st *next= world->collections; next and next->name and (not signal.is_shutdown()); next++)
-    {
-      bool failed= false;
-      bool skipped= false;
+      assert(sigignore(SIGPIPE) == 0);
 
-      if (collection_to_run.empty() == false and fnmatch(collection_to_run.c_str(), next->name, 0))
+      libtest::SignalThread signal;
+      if (not signal.setup())
       {
-        continue;
+        Error << "Failed to setup signals";
+        return EXIT_FAILURE;
       }
 
-      stats.collection_total++;
+      Stats stats;
 
-      test_return_t collection_rc= world->startup(creators_ptr);
+      get_world(world);
 
-      if (collection_rc == TEST_SUCCESS and next->pre)
-      {
-        collection_rc= world->runner()->pre(next->pre, creators_ptr);
-      }
+      test_return_t error;
+      void *creators_ptr= world->create(error);
 
-      switch (collection_rc)
+      switch (error)
       {
       case TEST_SUCCESS:
         break;
 
+      case TEST_SKIPPED:
+        Out << "SKIP " << argv[0];
+        delete world;
+        return EXIT_SUCCESS;
+
       case TEST_FATAL:
       case TEST_FAILURE:
-        Out << next->name << " [ failed ]";
-        failed= true;
-        signal.set_shutdown(SHUTDOWN_GRACEFUL);
-        goto cleanup;
+      case TEST_MEMORY_ALLOCATION_FAILURE:
+        delete world;
+        return EXIT_FAILURE;
+      }
 
-      case TEST_SKIPPED:
-        Out << next->name << " [ skipping ]";
-        skipped= true;
-        goto cleanup;
+      if (getenv("TEST_COLLECTION"))
+      {
+        if (strlen(getenv("TEST_COLLECTION")))
+        {
+          collection_to_run= getenv("TEST_COLLECTION");
+        }
+      }
 
-      case TEST_MEMORY_ALLOCATION_FAILURE:
-        test_assert(0, "Allocation failure, or unknown return");
+      if (collection_to_run.empty() == false)
+      {
+        Out << "Only testing " <<  collection_to_run;
       }
 
-      Out << "Collection: " << next->name;
+      char *wildcard= NULL;
+      if (argc == 3)
+      {
+        wildcard= argv[2];
+      }
 
-      for (test_st *run= next->tests; run->name; run++)
+      for (collection_st *next= world->collections; next and next->name and (not signal.is_shutdown()); next++)
       {
-        struct timeval start_time, end_time;
-        long int load_time= 0;
+        bool failed= false;
+        bool skipped= false;
 
-        if (wildcard && fnmatch(wildcard, run->name, 0))
+        if (collection_to_run.empty() == false and fnmatch(collection_to_run.c_str(), next->name, 0))
         {
           continue;
         }
 
-        test_return_t return_code;
-        try {
-          if (test_success(return_code= world->item.startup(creators_ptr)))
+        stats.collection_total++;
+
+        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);
+        }
+
+        switch (collection_rc)
+        {
+        case TEST_SUCCESS:
+          break;
+
+        case TEST_FATAL:
+        case TEST_FAILURE:
+          Out << next->name << " [ failed ]";
+          failed= true;
+          signal.set_shutdown(SHUTDOWN_GRACEFUL);
+          goto cleanup;
+
+        case TEST_SKIPPED:
+          Out << next->name << " [ skipping ]";
+          skipped= true;
+          goto cleanup;
+
+        case TEST_MEMORY_ALLOCATION_FAILURE:
+          test_assert(0, "Allocation failure, or unknown return");
+        }
+
+        Out << "Collection: " << next->name;
+
+        for (test_st *run= next->tests; run->name; run++)
+        {
+          struct timeval start_time, end_time;
+          long int load_time= 0;
+
+          if (wildcard && fnmatch(wildcard, run->name, 0))
           {
-            if (test_success(return_code= world->item.flush(creators_ptr, run)))
+            continue;
+          }
+
+          test_return_t return_code;
+          try {
+            if (test_success(return_code= world->item.startup(creators_ptr)))
             {
-              // @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.flush(creators_ptr, run)))
               {
-                { // Runner Code
-                  gettimeofday(&start_time, NULL);
-                  assert(world->runner());
-                  assert(run->test_fn);
-                  return_code= world->runner()->run(run->test_fn, creators_ptr);
-                  gettimeofday(&end_time, NULL);
-                  load_time= timedif(end_time, start_time);
+                // @note pre will fail is SKIPPED is returned
+                if (test_success(return_code= world->item.pre(creators_ptr)))
+                {
+                  { // Runner Code
+                    gettimeofday(&start_time, NULL);
+                    assert(world->runner());
+                    assert(run->test_fn);
+                    try 
+                    {
+                      return_code= world->runner()->run(run->test_fn, creators_ptr);
+                    }
+                    // Special case where check for the testing of the exception
+                    // system.
+                    catch (libtest::fatal &e)
+                    {
+                      if (fatal::is_disabled())
+                      {
+                        fatal::increment_disabled_counter();
+                        return_code= TEST_SUCCESS;
+                      }
+                      else
+                      {
+                        throw;
+                      }
+                    }
+
+                    gettimeofday(&end_time, NULL);
+                    load_time= timedif(end_time, start_time);
+                  }
                 }
-              }
 
-              // @todo do something if post fails
-              (void)world->item.post(creators_ptr);
+                // @todo do something if post fails
+                (void)world->item.post(creators_ptr);
+              }
+              else if (return_code == TEST_SKIPPED)
+              { }
+              else if (return_code == TEST_FAILURE)
+              {
+                Error << " item.flush(failure)";
+                signal.set_shutdown(SHUTDOWN_GRACEFUL);
+              }
             }
             else if (return_code == TEST_SKIPPED)
             { }
             else if (return_code == TEST_FAILURE)
             {
-              Error << " item.flush(failure)";
+              Error << " item.startup(failure)";
               signal.set_shutdown(SHUTDOWN_GRACEFUL);
             }
           }
-          else if (return_code == TEST_SKIPPED)
-          { }
-          else if (return_code == TEST_FAILURE)
+
+          catch (std::exception &e)
+          {
+            Error << "Exception was thrown: " << e.what();
+            return_code= TEST_FAILURE;
+          }
+          catch (...)
           {
-            Error << " item.startup(failure)";
+            Error << "Unknown exception occurred";
+            return_code= TEST_FAILURE;
+          }
+
+          stats.total++;
+
+          switch (return_code)
+          {
+          case TEST_SUCCESS:
+            Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
+            stats.success++;
+            break;
+
+          case TEST_FATAL:
+          case TEST_FAILURE:
+            stats.failed++;
+            failed= true;
+            Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
+            break;
+
+          case TEST_SKIPPED:
+            stats.skipped++;
+            skipped= true;
+            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");
+          }
+
+          if (test_failed(world->on_error(return_code, creators_ptr)))
+          {
+            Error << "Failed while running on_error()";
             signal.set_shutdown(SHUTDOWN_GRACEFUL);
+            break;
           }
         }
 
-        catch (std::exception &e)
-        {
-          Error << "Exception was thrown: " << e.what();
-          return_code= TEST_FAILURE;
-        }
-        catch (...)
+        (void) world->runner()->post(next->post, creators_ptr);
+
+cleanup:
+        if (failed == false and skipped == false)
         {
-          Error << "Unknown exception occurred";
-          return_code= TEST_FAILURE;
+          stats.collection_success++;
         }
 
-        stats.total++;
-
-        switch (return_code)
+        if (failed)
         {
-        case TEST_SUCCESS:
-          Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
-          stats.success++;
-          break;
-
-        case TEST_FATAL:
-        case TEST_FAILURE:
-          stats.failed++;
-          failed= true;
-          Out << "\tTesting " << run->name <<  "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
-          break;
-
-        case TEST_SKIPPED:
-          stats.skipped++;
-          skipped= true;
-          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");
+          stats.collection_failed++;
         }
 
-        if (test_failed(world->on_error(return_code, creators_ptr)))
+        if (skipped)
         {
-          Error << "Failed while running on_error()";
-          signal.set_shutdown(SHUTDOWN_GRACEFUL);
-          break;
+          stats.collection_skipped++;
         }
-      }
 
-      (void) world->runner()->post(next->post, creators_ptr);
+        world->shutdown(creators_ptr);
+        Outn();
+      }
 
-cleanup:
-      if (failed == false and skipped == false)
+      if (not signal.is_shutdown())
       {
-        stats.collection_success++;
+        signal.set_shutdown(SHUTDOWN_GRACEFUL);
       }
 
-      if (failed)
+      shutdown_t status= signal.get_shutdown();
+      if (status == SHUTDOWN_FORCED)
       {
-        stats.collection_failed++;
+        Out << "Tests were aborted.";
+        exit_code= EXIT_FAILURE;
       }
-
-      if (skipped)
+      else if (stats.collection_failed)
       {
-        stats.collection_skipped++;
+        Out << "Some test failed.";
+        exit_code= EXIT_FAILURE;
+      }
+      else if (stats.collection_skipped and stats.collection_failed and stats.collection_success)
+      {
+        Out << "Some tests were skipped.";
+      }
+      else if (stats.collection_success and stats.collection_failed == 0)
+      {
+        Out << "All tests completed successfully.";
       }
 
-      world->shutdown(creators_ptr);
-      Outn();
-    }
-
-    if (not signal.is_shutdown())
-    {
-      signal.set_shutdown(SHUTDOWN_GRACEFUL);
-    }
-
-    shutdown_t status= signal.get_shutdown();
-    if (status == SHUTDOWN_FORCED)
-    {
-      Out << "Tests were aborted.";
-      exit_code= EXIT_FAILURE;
-    }
-    else if (stats.collection_failed)
-    {
-      Out << "Some test failed.";
-      exit_code= EXIT_FAILURE;
-    }
-    else if (stats.collection_skipped and stats.collection_failed and stats.collection_success)
-    {
-      Out << "Some tests were skipped.";
-    }
-    else if (stats.collection_success and stats.collection_failed == 0)
-    {
-      Out << "All tests completed successfully.";
-    }
-
-    stats_print(&stats);
+      stats_print(&stats);
 
-    delete world;
+      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);
+      Outn(); // Generate a blank to break up the messages if make check/test has been run
+    } while (exit_code == EXIT_SUCCESS and opt_repeat);
+  }
+  catch (libtest::fatal& e)
+  {
+    std::cerr << e.what() << std::endl;
+  }
+  catch (...)
+  {
+    std::cerr << "Unknown exception halted execution" << std::endl;
+  }
 
   return exit_code;
 }