X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Ftest.c;h=fb501fadc2edcc6d57270c2cd47ce52cdc21a5d8;hb=51d1a5f37c90faa416a5fbd9238aafbde718b074;hp=e7e16f54dee1f5ae7bffde2da0b94a47397575e5;hpb=fa543a0643aafcd5042080ca060f0418b95eb8ca;p=awesomized%2Flibmemcached diff --git a/tests/test.c b/tests/test.c index e7e16f54..fb501fad 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1,136 +1,258 @@ +/* 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. */ #include -#include #include #include #include #include #include +#include #include #include +#include #include "test.h" -long int timedif(struct timeval a, struct timeval b) +static void world_stats_print(world_stats_st *stats) { - register int us, s; + 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); +} - us = a.tv_usec - b.tv_usec; +static long int timedif(struct timeval a, struct timeval b) +{ + long us, s; + + us = (int)(a.tv_usec - b.tv_usec); us /= 1000; - s = a.tv_sec - b.tv_sec; + s = (int)(a.tv_sec - b.tv_sec); s *= 1000; return s + us; } +const char *test_strerror(test_return_t code) +{ + switch (code) { + case TEST_SUCCESS: + return "ok"; + case TEST_FAILURE: + return "failed"; + case TEST_MEMORY_ALLOCATION_FAILURE: + return "memory allocation"; + case TEST_SKIPPED: + return "skipped"; + case TEST_MAXIMUM_RETURN: + default: + fprintf(stderr, "Unknown return value\n"); + 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 return_code; unsigned int x; - char *server_list; char *collection_to_run= NULL; char *wildcard= NULL; - memcached_server_st *servers; + world_st world; collection_st *collection; + collection_st *next; + void *world_ptr; - collection= gets_collections(); - - - if (argc > 1) - collection_to_run= argv[1]; - - if (argc == 3) - wildcard= argv[2]; + world_stats_st stats; - if (!(server_list= getenv("MEMCACHED_SERVERS"))) - server_list= "localhost"; + memset(&stats, 0, sizeof(stats)); + memset(&world, 0, sizeof(world)); + get_world(&world); - printf("servers %s\n", server_list); - srandom(time(NULL)); + if (! world.runner) + { + world.runner= &defualt_runners; + } - servers= memcached_servers_parse(server_list); - assert(servers); + collection= world.collections; - for (x= 0; x < memcached_server_list_count(servers); x++) + if (world.create) + { + test_return_t error; + world_ptr= world.create(&error); + if (error != TEST_SUCCESS) + exit(1); + } + else { - printf("\t%s : %u\n", servers[x].hostname, servers[x].port); - assert(servers[x].stack_responses == 0); - assert(servers[x].fd == -1); - assert(servers[x].cursor_active == 0); + world_ptr= NULL; } - printf("\n"); + if (argc > 1) + collection_to_run= argv[1]; + + if (argc == 3) + wildcard= argv[2]; - collection_st *next; for (next= collection; next->name; next++) { test_st *run; run= next->tests; - if (collection_to_run && strcmp(collection_to_run, next->name)) + if (collection_to_run && fnmatch(collection_to_run, next->name, 0)) continue; fprintf(stderr, "\n%s\n\n", next->name); for (x= 0; run->name; run++) { - if (wildcard && strcmp(wildcard, run->name)) + struct timeval start_time, end_time; + long int load_time= 0; + + if (wildcard && fnmatch(wildcard, run->name, 0)) continue; fprintf(stderr, "Testing %s", run->name); - memcached_st *memc; - memcached_return rc; - struct timeval start_time, end_time; - - memc= memcached_create(NULL); - assert(memc); - - if (run->requires_flush) - memcached_flush(memc, 0); + if (world.collection_startup) + { + world.collection_startup(world_ptr); + } - rc= memcached_server_push(memc, servers); - assert(rc == MEMCACHED_SUCCESS); + if (run->requires_flush && world.flush) + { + world.flush(world_ptr); + } - unsigned int loop; - for (loop= 0; loop < memcached_server_list_count(servers); loop++) + if (world.pre_run) { - assert(memc->hosts[loop].stack_responses == 0); - assert(memc->hosts[loop].fd == -1); - assert(memc->hosts[loop].cursor_active == 0); + world.pre_run(world_ptr); } - if (next->pre) + + if (next->pre && world.runner->pre) { - memcached_return rc; - 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); - run->function(memc); + return_code= world.runner->run(run->test_fn, world_ptr); gettimeofday(&end_time, NULL); - long int load_time= timedif(end_time, start_time); - fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000, - load_time % 1000); + load_time= timedif(end_time, start_time); - if (next->post) - (void)next->post(memc); + if (next->post && world.runner->post) + { + (void) world.runner->post(next->post, 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; + } } } fprintf(stderr, "All tests completed successfully\n\n"); - memcached_server_list_free(servers); + if (world.destroy) + { + test_return_t error; + error= world.destroy(world_ptr); + + if (error != TEST_SUCCESS) + { + fprintf(stderr, "Failure during shutdown.\n"); + stats.failed++; // We do this to make our exit code return 1 + } + } + + world_stats_print(&stats); - return 0; + return stats.failed == 0 ? 0 : 1; }