From: Brian Aker Date: Sun, 19 Feb 2012 00:14:16 +0000 (-0800) Subject: Switch to using bind() to just find free ports to use while testing. X-Git-Tag: 1.0.5~30 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=a18c65b0ee1a4721a4f1298f52f8b3446cf7c600;p=awesomized%2Flibmemcached Switch to using bind() to just find free ports to use while testing. --- diff --git a/clients/execute.cc b/clients/execute.cc index b2769545..f877851e 100644 --- a/clients/execute.cc +++ b/clients/execute.cc @@ -27,7 +27,7 @@ unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int numbe memcached_return_t rc= memcached_set(memc, pairs[x].key, pairs[x].key_length, pairs[x].value, pairs[x].value_length, 0, 0); - if (rc != MEMCACHED_SUCCESS and rc != MEMCACHED_BUFFERED) + if (memcached_failed(rc)) { fprintf(stderr, "%s:%d Failure on insert (%s) of %.*s\n", __FILE__, __LINE__, @@ -52,7 +52,6 @@ unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int numbe */ unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of) { - memcached_return_t rc; unsigned int x; unsigned int retrieved; @@ -64,10 +63,11 @@ unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int numbe unsigned int fetch_key= (unsigned int)((unsigned int)random() % number_of); + memcached_return_t rc; char *value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length, &value_length, &flags, &rc); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) { fprintf(stderr, "%s:%d Failure on read(%s) of %.*s\n", __FILE__, __LINE__, diff --git a/libtest/port.cc b/libtest/port.cc index 36939e14..c1f2f945 100644 --- a/libtest/port.cc +++ b/libtest/port.cc @@ -24,14 +24,15 @@ #include #include #include -#include -#include -#include -#include -#include #include #include #include +#include +#include +#include +#include +#include +#include #include @@ -74,4 +75,39 @@ void set_max_port(in_port_t port) global_max_port= port; } +in_port_t get_free_port() +{ + in_port_t ret_port= in_port_t(0); + int sd; + if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != -1) + { + int optval= 1; + if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != -1) + { + struct sockaddr_in sin; + sin.sin_port= 0; + sin.sin_addr.s_addr= 0; + sin.sin_addr.s_addr= INADDR_ANY; + sin.sin_family= AF_INET; + + if (bind(sd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in) ) != -1) + { + socklen_t addrlen= sizeof(sin); + + if (listen(sd, 100) != -1) + { + if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1) + { + ret_port= sin.sin_port; + } + } + } + } + + close(sd); + } + + return ret_port; +} + } // namespace libtest diff --git a/libtest/port.h b/libtest/port.h index 20891254..d6fbcb25 100644 --- a/libtest/port.h +++ b/libtest/port.h @@ -40,4 +40,7 @@ in_port_t max_port(); LIBTEST_API void set_max_port(in_port_t port); +LIBTEST_API +in_port_t get_free_port(); + } // namespace libtest diff --git a/libtest/unittest.cc b/libtest/unittest.cc index 2d171dfd..21dcbc74 100644 --- a/libtest/unittest.cc +++ b/libtest/unittest.cc @@ -237,7 +237,7 @@ static test_return_t gearmand_cycle_test(void *object) #endif const char *argv[1]= { "cycle_gearmand" }; - test_true(server_startup(*servers, "gearmand", 9999, 1, argv)); + test_true(server_startup(*servers, "gearmand", get_free_port(), 1, argv)); return TEST_SUCCESS; } @@ -251,7 +251,7 @@ static test_return_t memcached_cycle_test(void *object) { test_true(has_memcached_binary()); const char *argv[1]= { "cycle_memcached" }; - test_true(server_startup(*servers, "memcached", 9998, 1, argv)); + test_true(server_startup(*servers, "memcached", get_free_port(), 1, argv)); return TEST_SUCCESS; } @@ -270,7 +270,7 @@ static test_return_t memcached_socket_cycle_test(void *object) { test_true(has_memcached_binary()); const char *argv[1]= { "cycle_memcached" }; - test_true(servers->start_socket_server("memcached", 9997, 1, argv)); + test_true(servers->start_socket_server("memcached", get_free_port(), 1, argv)); return TEST_SUCCESS; } @@ -295,7 +295,7 @@ static test_return_t memcached_sasl_test(void *object) { test_true(has_memcached_sasl_binary()); const char *argv[1]= { "cycle_memcached_sasl" }; - test_true(server_startup(*servers, "memcached-sasl", 9996, 1, argv)); + test_true(server_startup(*servers, "memcached-sasl", get_free_port(), 1, argv)); return TEST_SUCCESS; } @@ -506,6 +506,13 @@ static test_return_t application_wait_services_BINARY2(void *) return TEST_SUCCESS; } +static test_return_t get_free_port_TEST(void *) +{ + in_port_t ret_port; + test_true_hint((ret_port= get_free_port()), ret_port); + return TEST_SUCCESS; +} + static test_return_t check_for_gearman(void *) { test_skip(true, HAVE_LIBGEARMAN); @@ -590,6 +597,11 @@ test_st cmdline_tests[] ={ {0, 0, 0} }; +test_st get_free_port_TESTS[] ={ + {"get_free_port()", 0, get_free_port_TEST }, + {0, 0, 0} +}; + test_st application_tests[] ={ {"vchar_t", 0, vchar_t_TEST }, {"true", 0, application_true_BINARY }, @@ -625,6 +637,7 @@ collection_st collection[] ={ {"cmdline", 0, 0, cmdline_tests}, {"application", 0, 0, application_tests}, {"http", check_for_curl, 0, http_tests}, + {"get_free_port()", 0, 0, get_free_port_TESTS}, {0, 0, 0, 0} }; diff --git a/tests/cycle.cc b/tests/cycle.cc index 64d2b860..a762d00b 100644 --- a/tests/cycle.cc +++ b/tests/cycle.cc @@ -127,7 +127,6 @@ collection_st collection[] ={ }; -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 #include "tests/libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/failure.cc b/tests/failure.cc index cbc165e0..1348e50c 100644 --- a/tests/failure.cc +++ b/tests/failure.cc @@ -204,7 +204,6 @@ collection_st collection[] ={ { 0, 0, 0, 0 } }; -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 #include "libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/libmemcached-1.0/all_tests.cc b/tests/libmemcached-1.0/all_tests.cc index 8561c959..7fae6d31 100644 --- a/tests/libmemcached-1.0/all_tests.cc +++ b/tests/libmemcached-1.0/all_tests.cc @@ -35,8 +35,6 @@ * */ -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 - #include #include diff --git a/tests/libmemcached-1.0/all_tests_socket.cc b/tests/libmemcached-1.0/all_tests_socket.cc index 9698cc75..ccb1d3c8 100644 --- a/tests/libmemcached-1.0/all_tests_socket.cc +++ b/tests/libmemcached-1.0/all_tests_socket.cc @@ -35,8 +35,6 @@ * */ -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 - #include #include diff --git a/tests/libmemcached-1.0/atomsmasher.cc b/tests/libmemcached-1.0/atomsmasher.cc index 73162d3a..ad4483ed 100644 --- a/tests/libmemcached-1.0/atomsmasher.cc +++ b/tests/libmemcached-1.0/atomsmasher.cc @@ -275,8 +275,6 @@ collection_st collection[] ={ }; -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 - #include "tests/libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/libmemcached-1.0/sasl.cc b/tests/libmemcached-1.0/sasl.cc index 70b42987..ee8b5e67 100644 --- a/tests/libmemcached-1.0/sasl.cc +++ b/tests/libmemcached-1.0/sasl.cc @@ -43,7 +43,6 @@ using namespace libtest; Test cases */ -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +20 #include static test_return_t pre_sasl(memcached_st *) diff --git a/tests/libmemcached_world.h b/tests/libmemcached_world.h index 2e7aa25d..0d3b57df 100644 --- a/tests/libmemcached_world.h +++ b/tests/libmemcached_world.h @@ -78,11 +78,10 @@ static void *world_create(libtest::server_startup_st& servers, test_return_t& er for (uint32_t x= 0; x < servers.count(); x++) { - in_port_t port; - char variable_buffer[1024]; snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x); + in_port_t port; char *var; if ((var= getenv(variable_buffer))) { @@ -90,7 +89,7 @@ static void *world_create(libtest::server_startup_st& servers, test_return_t& er } else { - port= in_port_t(TEST_PORT_BASE +x); + port= in_port_t(libtest::get_free_port()); } const char *argv[1]= { "memcached" }; @@ -153,17 +152,6 @@ static test_return_t world_container_startup(libmemcached_test_container_st *con } } - for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host) - { - memcached_server_instance_st instance= - memcached_server_instance_by_position(container->parent, host); - - if (instance->type == MEMCACHED_CONNECTION_TCP) - { - test_true_got(memcached_server_port(instance) >= TEST_PORT_BASE, memcached_server_port(instance)); - } - } - return TEST_SUCCESS; } @@ -201,8 +189,7 @@ static test_return_t world_pre_run(libmemcached_test_container_st *container) test_true(container->memc); for (uint32_t loop= 0; loop < memcached_server_list_count(container->memc->servers); loop++) { - memcached_server_instance_st instance= - memcached_server_instance_by_position(container->memc, loop); + memcached_server_instance_st instance= memcached_server_instance_by_position(container->memc, loop); test_compare(-1, instance->fd); test_compare(0U, instance->cursor_active); diff --git a/tests/libmemcached_world_socket.h b/tests/libmemcached_world_socket.h index b034ca61..3e410084 100644 --- a/tests/libmemcached_world_socket.h +++ b/tests/libmemcached_world_socket.h @@ -63,11 +63,10 @@ static void *world_create(libtest::server_startup_st& servers, test_return_t& er return NULL; } - in_port_t max_port= TEST_PORT_BASE; for (uint32_t x= 0; x < servers.count(); x++) { const char *argv[1]= { "memcached" }; - if (not servers.start_socket_server("memcached", max_port +1, 1, argv)) + if (not servers.start_socket_server("memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FATAL; return NULL; @@ -116,17 +115,6 @@ static test_return_t world_container_startup(libmemcached_test_container_st *con } } - for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host) - { - memcached_server_instance_st instance= - memcached_server_instance_by_position(container->parent, host); - - if (instance->type == MEMCACHED_CONNECTION_TCP) - { - test_true_got(memcached_server_port(instance) >= TEST_PORT_BASE, memcached_server_port(instance)); - } - } - return TEST_SUCCESS; } diff --git a/tests/mem_udp.cc b/tests/mem_udp.cc index 1ef95bec..ff631921 100644 --- a/tests/mem_udp.cc +++ b/tests/mem_udp.cc @@ -563,7 +563,6 @@ collection_st collection[] ={ {0, 0, 0, 0} }; -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 #include "tests/libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/memcapable.cc b/tests/memcapable.cc index 49848188..048333ee 100644 --- a/tests/memcapable.cc +++ b/tests/memcapable.cc @@ -114,7 +114,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memcapable" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memcat.cc b/tests/memcat.cc index 4b1096ac..bb496a1e 100644 --- a/tests/memcat.cc +++ b/tests/memcat.cc @@ -143,7 +143,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memcat" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memcp.cc b/tests/memcp.cc index 698041e6..7ce19101 100644 --- a/tests/memcp.cc +++ b/tests/memcp.cc @@ -102,7 +102,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memcp" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memdump.cc b/tests/memdump.cc index 790d1a68..80a3fe6b 100644 --- a/tests/memdump.cc +++ b/tests/memdump.cc @@ -129,7 +129,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memdump" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memexist.cc b/tests/memexist.cc index 9de01fa3..091a8bbe 100644 --- a/tests/memexist.cc +++ b/tests/memexist.cc @@ -141,7 +141,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memexist" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memflush.cc b/tests/memflush.cc index 97544708..5339f4e2 100644 --- a/tests/memflush.cc +++ b/tests/memflush.cc @@ -102,7 +102,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memflush" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memrm.cc b/tests/memrm.cc index 382693cd..23003bee 100644 --- a/tests/memrm.cc +++ b/tests/memrm.cc @@ -140,7 +140,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memrm" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memslap.cc b/tests/memslap.cc index f353978d..55c89111 100644 --- a/tests/memslap.cc +++ b/tests/memslap.cc @@ -174,7 +174,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memslap" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memstat.cc b/tests/memstat.cc index 33a14e33..c99836ac 100644 --- a/tests/memstat.cc +++ b/tests/memstat.cc @@ -123,7 +123,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memstat" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; } diff --git a/tests/memtouch.cc b/tests/memtouch.cc index 2710cb6c..0b28cbc7 100644 --- a/tests/memtouch.cc +++ b/tests/memtouch.cc @@ -135,7 +135,7 @@ static void *world_create(server_startup_st& servers, test_return_t& error) } const char *argv[1]= { "memtouch" }; - if (not server_startup(servers, "memcached", MEMCACHED_DEFAULT_PORT +10, 1, argv)) + if (not server_startup(servers, "memcached", libtest::get_free_port(), 1, argv)) { error= TEST_FAILURE; }