X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Ftest.c;h=793bcbd7264d08af68112aefa033e4e931af0980;hb=7c986323bd4eece0d805f4df17eb03ea094f84f6;hp=d0d5329245a6ed398308a267b6c8c67127cb596a;hpb=12ff9b29129ec9764dc4f6c5859743a574bab32c;p=m6w6%2Flibmemcached diff --git a/tests/test.c b/tests/test.c index d0d53292..793bcbd7 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1,3 +1,11 @@ +/* uTest + * Copyright (C) 2006-2009 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + */ + /* Sample test application. */ @@ -7,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +23,14 @@ #include "test.h" +static void world_stats_print(world_stats_st *stats) +{ + fprintf(stderr, "Total\t\t\t\t%u\n", stats->total); + fprintf(stderr, "\tFailed\t\t\t%u\n", stats->failed); + fprintf(stderr, "\tSkipped\t\t\t%u\n", stats->skipped); + fprintf(stderr, "\tSucceeded\t\t%u\n", stats->success); +} + static long int timedif(struct timeval a, struct timeval b) { register int us, s; @@ -37,38 +54,82 @@ static const char *test_strerror(test_return_t code) case TEST_SKIPPED: return "skipped"; case TEST_MAXIMUM_RETURN: - default: + default: fprintf(stderr, "Unknown return value\n"); - assert(0); + abort(); + } + +} + +void create_core(void) +{ + if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL) + { + pid_t pid= fork(); + + if (pid == 0) + { + abort(); + } + else + { + while (waitpid(pid, NULL, 0) != pid) + { + ; + } + } } +} + +static test_return_t _runner_default(test_callback_fn func, void *p) +{ + if (func) + { + return func(p); + } + else + { + return TEST_SUCCESS; + } } +static world_runner_st defualt_runners= { + _runner_default, + _runner_default, + _runner_default +}; + + int main(int argc, char *argv[]) { - test_return_t failed; + test_return_t return_code; unsigned int x; char *collection_to_run= NULL; char *wildcard= NULL; - server_startup_st *startup_ptr; - memcached_server_st *servers; world_st world; collection_st *collection; collection_st *next; void *world_ptr; - memset(&world, 0, sizeof(world_st)); + world_stats_st stats; + + memset(&stats, 0, sizeof(stats)); + memset(&world, 0, sizeof(world)); get_world(&world); + + if (! world.runner) + { + world.runner= &defualt_runners; + } + collection= world.collections; if (world.create) world_ptr= world.create(); - else + else world_ptr= NULL; - startup_ptr= (server_startup_st *)world_ptr; - servers= (memcached_server_st *)startup_ptr->servers; - if (argc > 1) collection_to_run= argv[1]; @@ -87,60 +148,88 @@ int main(int argc, char *argv[]) for (x= 0; run->name; run++) { - unsigned int loop; - memcached_st *memc; - memcached_return rc; struct timeval start_time, end_time; - long int load_time; + long int load_time= 0; if (wildcard && fnmatch(wildcard, run->name, 0)) continue; fprintf(stderr, "Testing %s", run->name); - memc= memcached_create(NULL); - assert(memc); - - rc= memcached_server_push(memc, servers); - assert(rc == MEMCACHED_SUCCESS); + if (world.collection_startup) + { + world.collection_startup(world_ptr); + } - if (run->requires_flush) + if (run->requires_flush && world.flush) { - memcached_flush(memc, 0); - memcached_quit(memc); + world.flush(world_ptr); } - for (loop= 0; loop < memcached_server_list_count(servers); loop++) + if (world.pre_run) { - assert(memc->hosts[loop].fd == -1); - assert(memc->hosts[loop].cursor_active == 0); + world.pre_run(world_ptr); } + if (next->pre) { - rc= next->pre(memc); + return_code= world.runner->pre(next->pre, world_ptr); - if (rc != MEMCACHED_SUCCESS) + if (return_code != TEST_SUCCESS) { - fprintf(stderr, "\t\t\t\t\t [ skipping ]\n"); goto error; } } gettimeofday(&start_time, NULL); - failed= run->function(memc); + return_code= world.runner->run(run->test_fn, world_ptr); gettimeofday(&end_time, NULL); load_time= timedif(end_time, start_time); - fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ %s ]\n", load_time / 1000, - load_time % 1000, test_strerror(failed)); - if (next->post) - (void)next->post(memc); + { + (void) world.runner->pre(next->pre, world_ptr); + } + + if (world.post_run) + { + world.post_run(world_ptr); + } - assert(memc); error: - memcached_free(memc); + stats.total++; + + fprintf(stderr, "\t\t\t\t\t"); + + switch (return_code) + { + case TEST_SUCCESS: + fprintf(stderr, "%ld.%03ld ", load_time / 1000, load_time % 1000); + stats.success++; + break; + case TEST_FAILURE: + stats.failed++; + break; + case TEST_SKIPPED: + stats.skipped++; + break; + case TEST_MEMORY_ALLOCATION_FAILURE: + case TEST_MAXIMUM_RETURN: + default: + break; + } + + fprintf(stderr, "[ %s ]\n", test_strerror(return_code)); + + if (world.on_error) + { + test_return_t rc; + rc= world.on_error(return_code, world_ptr); + + if (rc != TEST_SUCCESS) + break; + } } } @@ -149,5 +238,7 @@ error: if (world.destroy) world.destroy(world_ptr); - return 0; + world_stats_print(&stats); + + return stats.failed == 0 ? 0 : 1; }