X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libtest%2Ftest.cc;h=ef10028c5800160dabb687448ec433b0bc0ba321;hb=560784bd3bc77b292761ef878dfb5f6205054946;hp=89e7c07112e674c6a8a2be1e9c40cbf5c2a9733b;hpb=b1e692afdbd3823e57c649fe82e073f3b0743ada;p=m6w6%2Flibmemcached diff --git a/libtest/test.cc b/libtest/test.cc index 89e7c071..ef10028c 100644 --- a/libtest/test.cc +++ b/libtest/test.cc @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include @@ -35,36 +36,12 @@ #include -#include -#include - #ifndef __INTEL_COMPILER #pragma GCC diagnostic ignored "-Wold-style-cast" #endif using namespace libtest; -static char global_socket[1024]; - -const char *default_socket() -{ - assert(global_socket[0]); - return global_socket; -} - -bool test_is_local() -{ - return (getenv("LIBTEST_LOCAL")); -} - -void set_default_socket(const char *socket) -{ - if (socket) - { - strncpy(global_socket, socket, strlen(socket)); - } -} - static void stats_print(Stats *stats) { if (stats->collection_failed == 0 and stats->collection_success == 0) @@ -94,51 +71,99 @@ static long int timedif(struct timeval a, struct timeval b) return s + us; } -const char *test_strerror(test_return_t code) +#include +#include + +int main(int argc, char *argv[]) { - switch (code) { - case TEST_SUCCESS: - return "ok"; + bool opt_massive= false; + unsigned long int opt_repeat= 1; // Run all tests once + bool opt_quiet= false; + std::string collection_to_run; - case TEST_FAILURE: - return "failed"; + // Options parsing + { + enum long_option_t { + OPT_LIBYATL_VERSION, + OPT_LIBYATL_MATCH_COLLECTION, + OPT_LIBYATL_MASSIVE, + OPT_LIBYATL_QUIET, + OPT_LIBYATL_REPEAT + }; + + static struct option long_options[]= + { + { "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 }, + { "massive", no_argument, NULL, OPT_LIBYATL_MASSIVE }, + { 0, 0, 0, 0 } + }; + + int option_index= 0; + while (1) + { + int option_rv= getopt_long(argc, argv, "", long_options, &option_index); + if (option_rv == -1) + { + break; + } - case TEST_MEMORY_ALLOCATION_FAILURE: - return "memory allocation"; + switch (option_rv) + { + case OPT_LIBYATL_VERSION: + break; + + case OPT_LIBYATL_QUIET: + opt_quiet= true; + break; - case TEST_SKIPPED: - return "skipped"; + case OPT_LIBYATL_REPEAT: + opt_repeat= strtoul(optarg, (char **) NULL, 10); + break; - case TEST_FATAL: - break; + case OPT_LIBYATL_MATCH_COLLECTION: + collection_to_run= optarg; + break; + + case OPT_LIBYATL_MASSIVE: + opt_massive= true; + break; + + case '?': + /* getopt_long already printed an error message. */ + Error << "unknown option to getopt_long()"; + exit(EXIT_FAILURE); + + default: + break; + } + } } - return "failed"; -} + srandom((unsigned int)time(NULL)); -void create_core(void) -{ - if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL) + if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10) > 1)) { - pid_t pid= fork(); + opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10); + } - if (pid == 0) - { - abort(); - } + if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet) + { + opt_quiet= true; + } + else if (getenv("JENKINS_URL")) + { + if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0)) + { } else { - while (waitpid(pid, NULL, 0) != pid) {}; + opt_quiet= true; } } -} - -static Framework *world= NULL; -int main(int argc, char *argv[]) -{ - srandom((unsigned int)time(NULL)); - if (getenv("LIBTEST_QUIET")) + if (opt_quiet) { close(STDOUT_FILENO); } @@ -168,241 +193,287 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - world= new Framework(); + int exit_code; - if (not world) - { - Error << "Failed to create Framework()"; - return EXIT_FAILURE; - } + try { + do { + exit_code= EXIT_SUCCESS; + Framework world; - libtest::SignalThread signal; - if (not signal.setup()) - { - return EXIT_FAILURE; - } + fatal_assert(sigignore(SIGPIPE) == 0); - Stats stats; + libtest::SignalThread signal; + if (signal.setup() == false) + { + Error << "Failed to setup signals"; + return EXIT_FAILURE; + } - get_world(world); + Stats stats; - test_return_t error; - void *creators_ptr= world->create(error); + get_world(&world); - 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: - Error << argv[0] << " failed in Framework::create()"; - delete world; - return EXIT_FAILURE; - } + test_return_t error; + void *creators_ptr= world.create(error); - char *collection_to_run= NULL; - if (argc > 1) - { - collection_to_run= argv[1]; - } - else if (getenv("TEST_COLLECTION")) - { - collection_to_run= getenv("TEST_COLLECTION"); - } + switch (error) + { + case TEST_SUCCESS: + break; - if (collection_to_run) - { - Out << "Only testing " << collection_to_run; - } + case TEST_SKIPPED: + Out << "SKIP " << argv[0]; + return EXIT_SUCCESS; - char *wildcard= NULL; - if (argc == 3) - { - wildcard= argv[2]; - } + case TEST_FAILURE: + return EXIT_FAILURE; + } - for (collection_st *next= world->collections; next->name and (not signal.is_shutdown()); next++) - { - test_return_t collection_rc= TEST_SUCCESS; - bool failed= false; - bool skipped= false; + if (getenv("YATL_COLLECTION_TO_RUN")) + { + if (strlen(getenv("YATL_COLLECTION_TO_RUN"))) + { + collection_to_run= getenv("YATL_COLLECTION_TO_RUN"); + } + } - if (collection_to_run && fnmatch(collection_to_run, next->name, 0)) - continue; + if (collection_to_run.compare("none") == 0) + { + return EXIT_SUCCESS; + } - stats.collection_total++; + if (collection_to_run.empty() == false) + { + Out << "Only testing " << collection_to_run; + } - collection_rc= world->startup(creators_ptr); + char *wildcard= NULL; + if (argc == 3) + { + wildcard= argv[2]; + } - if (collection_rc == TEST_SUCCESS and next->pre) - { - collection_rc= world->runner()->pre(next->pre, creators_ptr); - } + for (collection_st *next= world.collections; next and next->name and (not signal.is_shutdown()); next++) + { + bool failed= false; + bool skipped= false; - 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"); - } + if (collection_to_run.empty() == false and fnmatch(collection_to_run.c_str(), next->name, 0)) + { + continue; + } - Out << "Collection: " << next->name; + stats.collection_total++; - for (test_st *run= next->tests; run->name; run++) - { - struct timeval start_time, end_time; - long int load_time= 0; + test_return_t collection_rc= world.startup(creators_ptr); - if (wildcard && fnmatch(wildcard, run->name, 0)) - { - continue; - } + if (collection_rc == TEST_SUCCESS and next->pre) + { + collection_rc= world.runner()->pre(next->pre, creators_ptr); + } - test_return_t return_code; - if (test_success(return_code= world->item.startup(creators_ptr))) - { - if (test_success(return_code= world->item.flush(creators_ptr, run))) + switch (collection_rc) { - // @note pre will fail is SKIPPED is returned - if (test_success(return_code= world->item.pre(creators_ptr))) + case TEST_SUCCESS: + break; + + 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; + + default: + fatal_message("invalid return code"); + } + + 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)) { - { // 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); + continue; + } + + test_return_t return_code; + try { + if (test_success(return_code= world.item.startup(creators_ptr))) + { + 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))) + { + { // 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); + } + 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.startup(failure)"; + signal.set_shutdown(SHUTDOWN_GRACEFUL); } } - // @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); + 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(); + return_code= TEST_FAILURE; + } + catch (...) + { + 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_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; + + default: + fatal_message("invalid return code"); + } + + if (test_failed(world.on_error(return_code, creators_ptr))) + { + Error << "Failed while running on_error()"; + signal.set_shutdown(SHUTDOWN_GRACEFUL); + break; + } } - } - else if (return_code == TEST_SKIPPED) - { } - else if (return_code == TEST_FAILURE) - { - Error << " item.startup(failure)"; - signal.set_shutdown(SHUTDOWN_GRACEFUL); - } - stats.total++; + (void) world.runner()->post(next->post, creators_ptr); - 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; +cleanup: + if (failed == false and skipped == false) + { + stats.collection_success++; + } - case TEST_FATAL: - case TEST_FAILURE: - stats.failed++; - failed= true; - Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]"; - break; + if (failed) + { + stats.collection_failed++; + } - case TEST_SKIPPED: - stats.skipped++; - skipped= true; - Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]"; - break; + if (skipped) + { + stats.collection_skipped++; + } - case TEST_MEMORY_ALLOCATION_FAILURE: - test_assert(0, "Memory Allocation Error"); + world.shutdown(creators_ptr); + Outn(); } - if (test_failed(world->on_error(return_code, creators_ptr))) + if (not signal.is_shutdown()) { - Error << "Failed while running on_error()"; signal.set_shutdown(SHUTDOWN_GRACEFUL); - break; } - } - - (void) world->runner()->post(next->post, creators_ptr); - -cleanup: - if (failed == false and skipped == false) - { - stats.collection_success++; - } - if (failed) - { - stats.collection_failed++; - } - - if (skipped) - { - stats.collection_skipped++; - } - - world->shutdown(creators_ptr); - Outn(); - } + 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."; + } - if (not signal.is_shutdown()) - { - signal.set_shutdown(SHUTDOWN_GRACEFUL); - } + stats_print(&stats); - int exit_code= EXIT_SUCCESS; - shutdown_t status= signal.get_shutdown(); - if (status == SHUTDOWN_FORCED) - { - Out << "Tests were aborted."; - exit_code= EXIT_FAILURE; + Outn(); // Generate a blank to break up the messages if make check/test has been run + } while (exit_code == EXIT_SUCCESS and --opt_repeat); } - else if (stats.collection_failed) + catch (libtest::fatal& e) { - Out << "Some test failed."; - exit_code= EXIT_FAILURE; + std::cerr << e.what() << std::endl; } - else if (stats.collection_skipped and stats.collection_failed and stats.collection_success) + catch (std::exception& e) { - Out << "Some tests were skipped."; + std::cerr << e.what() << std::endl; } - else if (stats.collection_success and stats.collection_failed == 0) + catch (...) { - Out << "All tests completed successfully."; + std::cerr << "Unknown exception halted execution." << std::endl; } - stats_print(&stats); - - delete world; - - Outn(); // Generate a blank to break up the messages if make check/test has been run - return exit_code; }