From: Brian Aker Date: Fri, 1 Feb 2008 04:47:16 +0000 (-0800) Subject: Refactored tests. X-Git-Tag: _20~30 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=3994a4a1ef81dfcf1e893ddb08e923c65e337ebd;p=m6w6%2Flibmemcached Refactored tests. Added test for UDP protocol (some operations now work, though far from being done well). --- diff --git a/tests/Makefile.am b/tests/Makefile.am index e76d1c14..5e50f097 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -15,15 +15,18 @@ EXTRA_DIST = output.res output2.res\ LIBS = -noinst_HEADERS = test.h -noinst_PROGRAMS = testapp testplus +noinst_HEADERS = test.h server.h +noinst_PROGRAMS = testapp testplus udptest -testapp_SOURCES = test.c function.c ../src/generator.c ../src/execute.c +testapp_SOURCES = test.c function.c ../src/generator.c ../src/execute.c server.c testapp_LDADD = $(LDADDS) -testplus_SOURCES = test.c plus.cpp +testplus_SOURCES = test.c plus.cpp server.c testplus_LDADD = $(LDADDS) ../lib/libmemcached.la +udptest_SOURCES = test.c udp.c server.c +udptest_LDADD = $(LDADDS) ../lib/libmemcached.la + record: ./testapp > output.res diff --git a/tests/function.c b/tests/function.c index 9c18645d..2b01a268 100644 --- a/tests/function.c +++ b/tests/function.c @@ -11,6 +11,7 @@ #include #include #include +#include "server.h" #include "../lib/common.h" #include "../src/generator.h" #include "../src/execute.h" @@ -2440,7 +2441,34 @@ collection_st collection[] ={ {0, 0, 0, 0} }; -collection_st *gets_collections(void) +#define SERVERS_TO_CREATE 5 + +void *world_create(void) +{ + server_startup_st *construct; + + construct= (server_startup_st *)malloc(sizeof(server_startup_st)); + memset(construct, 0, sizeof(server_startup_st)); + construct->count= SERVERS_TO_CREATE; + construct->udp= 0; + server_startup(construct); + + return construct; +} + +void world_destroy(void *p) +{ + server_startup_st *construct= (server_startup_st *)p; + memcached_server_st *servers= (memcached_server_st *)construct->servers; + memcached_server_list_free(servers); + + server_shutdown(construct); + free(construct); +} + +void get_world(world_st *world) { - return collection; + world->collections= collection; + world->create= world_create; + world->destroy= world_destroy; } diff --git a/tests/plus.cpp b/tests/plus.cpp index 32dbcf0b..cb0b0837 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "server.h" #include "test.h" @@ -39,7 +40,36 @@ collection_st collection[] ={ {0, 0, 0, 0} }; -collection_st *gets_collections(void) +#define SERVERS_TO_CREATE 1 + +void *world_create(void) +{ + unsigned int x; + memcached_server_st *servers; + server_startup_st *construct; + + construct= (server_startup_st *)malloc(sizeof(server_startup_st)); + memset(construct, 0, sizeof(server_startup_st)); + + construct->count= SERVERS_TO_CREATE; + server_startup(construct); + + return construct; +} + +void world_destroy(void *p) +{ + server_startup_st *construct= (server_startup_st *)p; + memcached_server_st *servers= (memcached_server_st *)construct->servers; + memcached_server_list_free(servers); + + server_shutdown(construct); + free(construct); +} + +void get_world(world_st *world) { - return collection; + world->collections= collection; + world->create= world_create; + world->destroy= world_destroy; } diff --git a/tests/server.c b/tests/server.c new file mode 100644 index 00000000..c8e1d98f --- /dev/null +++ b/tests/server.c @@ -0,0 +1,87 @@ +/* + Startup, and shutdown the memcached servers. +*/ + +#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10 +#include +#include +#include +#include +#include +#include +#include +#include "server.h" + +void server_startup(server_startup_st *construct) +{ + unsigned int x; + + if ((construct->server_list= getenv("MEMCACHED_SERVERS"))) + { + printf("servers %s\n", construct->server_list); + construct->servers= memcached_servers_parse(construct->server_list); + construct->server_list= NULL; + construct->count= 0; + } + else + { + WATCHPOINT; + { + char server_string_buffer[8096]; + char *end_ptr; + end_ptr= server_string_buffer; + + for (x= 0; x < construct->count; x++) + { + char buffer[1024]; /* Nothing special for number */ + int count; + + if (construct->udp) + sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -U %u", x, x+ TEST_PORT_BASE); + else + sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -p %u", x, x+ TEST_PORT_BASE); + system(buffer); + count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE); + end_ptr+= count; + } + *end_ptr= 0; + + construct->server_list= strdup(server_string_buffer); + } + printf("servers %s\n", construct->server_list); + construct->servers= memcached_servers_parse(construct->server_list); + } + + assert(construct->servers); + + srandom(time(NULL)); + + for (x= 0; x < memcached_server_list_count(construct->servers); x++) + { + printf("\t%s : %u\n", construct->servers[x].hostname, construct->servers[x].port); + assert(construct->servers[x].fd == -1); + assert(construct->servers[x].cursor_active == 0); + } + + printf("\n"); +} + +void server_shutdown(server_startup_st *construct) +{ + unsigned int x; + + if (construct->server_list) + { + for (x= 0; x < construct->count; x++) + { + char buffer[1024]; /* Nothing special for number */ + sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x); + system(buffer); + + sprintf(buffer, "/tmp/%umemc.pid", x); + unlink(buffer); + } + + free(construct->server_list); + } +} diff --git a/tests/server.h b/tests/server.h new file mode 100644 index 00000000..d287f11e --- /dev/null +++ b/tests/server.h @@ -0,0 +1,25 @@ +/* + Server startup and shutdown functions. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef struct server_startup_st server_startup_st; + +struct server_startup_st +{ + uint8_t count; + uint8_t udp; + memcached_server_st *servers; + char *server_list; +}; + +void server_startup(server_startup_st *construct); +void server_shutdown(server_startup_st *construct); + +#ifdef __cplusplus +} +#endif diff --git a/tests/test.c b/tests/test.c index 653a3ddc..6c6bea64 100644 --- a/tests/test.c +++ b/tests/test.c @@ -11,12 +11,10 @@ #include #include #include +#include "server.h" #include "test.h" -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10 -#define TEST_SERVERS 5 - long int timedif(struct timeval a, struct timeval b) { register int us, s; @@ -28,59 +26,28 @@ long int timedif(struct timeval a, struct timeval b) return s + us; } -char *server_startup() -{ - unsigned int x; - char server_string_buffer[8096]; - char *end_ptr; - - end_ptr= server_string_buffer; - - for (x= 0; x < TEST_SERVERS; x++) - { - char buffer[1024]; /* Nothing special for number */ - int count; - - sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -p %u", x, x+ TEST_PORT_BASE); - system(buffer); - count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE); - end_ptr+= count; - } - *end_ptr= 0; - - return strdup(server_string_buffer); -} - -void server_shutdown(char *server_string) -{ - unsigned int x; - - if (server_string) - { - for (x= 0; x < TEST_SERVERS; x++) - { - char buffer[1024]; /* Nothing special for number */ - sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x); - system(buffer); - } - - free(server_string); - } -} - int main(int argc, char *argv[]) { unsigned int x; - char *server_list; 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; uint8_t failed; + void *world_ptr; + + memset(&world, 0, sizeof(world_st)); + get_world(&world); + collection= world.collections; - collection= gets_collections(); + if (world.create) + world_ptr= world.create(); + startup_ptr= (server_startup_st *)world_ptr; + servers= (memcached_server_st *)startup_ptr->servers; if (argc > 1) collection_to_run= argv[1]; @@ -88,32 +55,6 @@ int main(int argc, char *argv[]) if (argc == 3) wildcard= argv[2]; - if ((server_list= getenv("MEMCACHED_SERVERS"))) - { - printf("servers %s\n", server_list); - servers= memcached_servers_parse(server_list); - server_list= NULL; - } - else - { - server_list= server_startup(); - printf("servers %s\n", server_list); - servers= memcached_servers_parse(server_list); - } - assert(servers); - - srandom(time(NULL)); - - - for (x= 0; x < memcached_server_list_count(servers); x++) - { - printf("\t%s : %u\n", servers[x].hostname, servers[x].port); - assert(servers[x].fd == -1); - assert(servers[x].cursor_active == 0); - } - - printf("\n"); - for (next= collection; next->name; next++) { test_st *run; @@ -189,9 +130,8 @@ error: fprintf(stderr, "All tests completed successfully\n\n"); - memcached_server_list_free(servers); - - server_shutdown(server_list); + if (world.destroy) + world.destroy(world_ptr); return 0; } diff --git a/tests/test.h b/tests/test.h index 8bc4d7a9..1c6b87be 100644 --- a/tests/test.h +++ b/tests/test.h @@ -8,6 +8,7 @@ extern "C" { #include #include "../lib/common.h" +typedef struct world_st world_st; typedef struct collection_st collection_st; typedef struct test_st test_st; @@ -24,8 +25,14 @@ struct collection_st { test_st *tests; }; +struct world_st { + collection_st *collections; + void *(*create)(void); + void (*destroy)(void *collection_object); +}; + /* How we make all of this work :) */ -collection_st *gets_collections(void); +void get_world(world_st *world); #ifdef __cplusplus } diff --git a/tests/udp.c b/tests/udp.c new file mode 100644 index 00000000..df73b261 --- /dev/null +++ b/tests/udp.c @@ -0,0 +1,71 @@ +/* + Sample test application. +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "test.h" +#include "server.h" + +uint8_t set_test(memcached_st *memc) +{ + memcached_return rc; + char *key= "foo"; + char *value= "when we sanitize"; + + rc= memcached_set(memc, key, strlen(key), + value, strlen(value), + (time_t)0, (uint32_t)0); + assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); + + return 0; +} + +test_st tests[] ={ + {"set", 1, set_test }, + {0, 0, 0} +}; + +collection_st collection[] ={ + {"udp", 0, 0, tests}, + {0, 0, 0, 0} +}; + +#define SERVERS_TO_CREATE 1 + +void *world_create(void) +{ + server_startup_st *construct; + + construct= (server_startup_st *)malloc(sizeof(server_startup_st)); + memset(construct, 0, sizeof(server_startup_st)); + construct->count= SERVERS_TO_CREATE; + construct->udp= 1; + server_startup(construct); + + return construct; +} + +void world_destroy(void *p) +{ + server_startup_st *construct= (server_startup_st *)p; + memcached_server_st *servers= (memcached_server_st *)construct->servers; + memcached_server_list_free(servers); + + server_shutdown(construct); + free(construct); +} + +void get_world(world_st *world) +{ + world->collections= collection; + world->create= world_create; + world->destroy= world_destroy; +}