X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Ftest.c;h=1de861ab6d7c5970e9b422c374668091f8fa66bb;hb=5845b33395414c577e0d88cc13a011db55cffa02;hp=2edcacd26af38e14c2789c77da6128ac9e00133a;hpb=1f34771f7ba527e3afb675f1a448d6c5ab66596b;p=awesomized%2Flibmemcached diff --git a/tests/test.c b/tests/test.c index 2edcacd2..1de861ab 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1,199 +1,141 @@ -#include /* Sample test application. */ #include -#include #include #include #include +#include +#include +#include +#include +#include -void init_test(void) +#include "test.h" + +long int timedif(struct timeval a, struct timeval b) { - memcached_st memc; + register int us, s; - (void)memcached_init(&memc); - memcached_deinit(&memc); + us = a.tv_usec - b.tv_usec; + us /= 1000; + s = a.tv_sec - b.tv_sec; + s *= 1000; + return s + us; } -void allocation_test(void) +int main(int argc, char *argv[]) { - memcached_st *memc; - memc= memcached_init(NULL); - assert(memc); - memcached_deinit(memc); -} + unsigned int x; + char *server_list; + char *collection_to_run= NULL; + char *wildcard= NULL; + memcached_server_st *servers; + collection_st *collection; + uint8_t failed; + + collection= gets_collections(); + -void set_test(void) -{ - memcached_st *memc; - char *foo; - memcached_return rc; - char *key= "foo"; - char *value= "when we sanitize"; - - memc= memcached_init(NULL); - assert(memc); - rc= memcached_set(memc, key, strlen(key), - value, strlen(value), - (time_t)0, (uint16_t)0); - assert(rc == MEMCACHED_SUCCESS); - assert(foo); - - memcached_deinit(memc); -} + if (argc > 1) + collection_to_run= argv[1]; -void add_test(void) -{ - memcached_st *memc; - char *foo; - memcached_return rc; - char *key= "foo"; - char *value= "when we sanitize"; - - memc= memcached_init(NULL); - assert(memc); - rc= memcached_add(memc, key, strlen(key), - value, strlen(value), - (time_t)0, (uint16_t)0); - assert(rc == MEMCACHED_NOTSTORED); - assert(foo); - - memcached_deinit(memc); -} + if (argc == 3) + wildcard= argv[2]; -void replace_test(void) -{ - memcached_st *memc; - char *foo; - memcached_return rc; - char *key= "foo"; - char *value= "when we sanitize"; - - memc= memcached_init(NULL); - assert(memc); - rc= memcached_replace(memc, key, strlen(key), - value, strlen(value), - (time_t)0, (uint16_t)0); - assert(rc == MEMCACHED_SUCCESS); - assert(foo); - - memcached_deinit(memc); -} + if (!(server_list= getenv("MEMCACHED_SERVERS"))) + server_list= "localhost"; -void delete_test(void) -{ - memcached_st *memc; - char *foo; - memcached_return rc; - char *key= "foo"; - char *value= "when we sanitize"; - - memc= memcached_init(NULL); - assert(memc); - rc= memcached_set(memc, key, strlen(key), - value, strlen(value), - (time_t)0, (uint16_t)0); - assert(rc == MEMCACHED_SUCCESS); - assert(foo); - - rc= memcached_delete(memc, key, strlen(key), (time_t)0); - assert(rc == MEMCACHED_SUCCESS); - assert(foo); - - memcached_deinit(memc); -} + printf("servers %s\n", server_list); + srandom(time(NULL)); -void flush_test(void) -{ - memcached_st *memc; - memcached_return rc; + servers= memcached_servers_parse(server_list); + assert(servers); - memc= memcached_init(NULL); - assert(memc); - rc= memcached_flush(memc, 0); - assert(rc == MEMCACHED_SUCCESS); + for (x= 0; x < memcached_server_list_count(servers); x++) + { + 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); + } - memcached_deinit(memc); -} + printf("\n"); -void get_test(void) -{ - memcached_st *memc; - memcached_return rc; - char *key= "foo"; - char *string; - size_t string_length; - uint16_t flags; - - memc= memcached_init(NULL); - assert(memc); - - string= memcached_get(memc, key, strlen(key), - &string_length, &flags, &rc); - - assert(string_length == 0); - assert(!string); - - memcached_deinit(memc); -} + collection_st *next; + for (next= collection; next->name; next++) + { + test_st *run; -void get_test2(void) -{ - memcached_st *memc; - char *foo; - memcached_return rc; - char *key= "foo"; - char *value= "when we sanitize"; - char *string; - size_t string_length; - uint16_t flags; - - memc= memcached_init(NULL); - assert(memc); - rc= memcached_set(memc, key, strlen(key), - value, strlen(value), - (time_t)0, (uint16_t)0); - assert(rc == MEMCACHED_SUCCESS); - - string= memcached_get(memc, key, strlen(key), - &string_length, &flags, &rc); - - assert(string_length == strlen(value)); - assert(!memcmp(string, value, string_length)); - - free(string); - - memcached_deinit(memc); -} + run= next->tests; + if (collection_to_run && strcmp(collection_to_run, next->name)) + continue; -void stats_hostname_test(void) -{ - memcached_return rc; - memcached_stat_st stat; - rc= memcached_stat_hostname(&stat, NULL, - "localhost", - MEMCACHED_DEFAULT_PORT); -} + fprintf(stderr, "\n%s\n\n", next->name); + + for (x= 0; run->name; run++) + { + if (wildcard && strcmp(wildcard, run->name)) + 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); + + rc= memcached_server_push(memc, servers); + assert(rc == MEMCACHED_SUCCESS); + + unsigned int loop; + for (loop= 0; loop < memcached_server_list_count(servers); loop++) + { + assert(memc->hosts[loop].stack_responses == 0); + assert(memc->hosts[loop].fd == -1); + assert(memc->hosts[loop].cursor_active == 0); + } + + if (next->pre) + { + memcached_return rc; + rc= next->pre(memc); + + if (rc != MEMCACHED_SUCCESS) + { + fprintf(stderr, "\t\t\t\t\t [ skipping ]\n"); + goto error; + } + } + + gettimeofday(&start_time, NULL); + failed= run->function(memc); + gettimeofday(&end_time, NULL); + long int load_time= timedif(end_time, start_time); + if (failed) + fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000, + load_time % 1000); + else + fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000, + load_time % 1000); + + if (next->post) + (void)next->post(memc); + + assert(memc); +error: + memcached_free(memc); + } + } + + fprintf(stderr, "All tests completed successfully\n\n"); + + memcached_server_list_free(servers); -int main(void) -{ - /* Clean the server before beginning testing */ - flush_test(); - - init_test(); - allocation_test(); - set_test(); - add_test(); - replace_test(); - flush_test(); - delete_test(); - flush_test(); - get_test(); - get_test2(); - stats_hostname_test(); - - /* Clean up whatever we might have left */ - flush_test(); return 0; }