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__,
*/
unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
{
- memcached_return_t rc;
unsigned int x;
unsigned int retrieved;
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__,
#include <cassert>
#include <cstdlib>
#include <cstring>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <unistd.h>
#include <ctime>
#include <fnmatch.h>
#include <iostream>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
#include <signal.h>
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
LIBTEST_API
void set_max_port(in_port_t port);
+LIBTEST_API
+in_port_t get_free_port();
+
} // namespace libtest
#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;
}
{
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;
}
{
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;
}
{
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;
}
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);
{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 },
{"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}
};
};
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
#include "tests/libmemcached_world.h"
void get_world(Framework *world)
{ 0, 0, 0, 0 }
};
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
#include "libmemcached_world.h"
void get_world(Framework *world)
*
*/
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
-
#include <config.h>
#include <libtest/test.hpp>
*
*/
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
-
#include <config.h>
#include <libtest/test.hpp>
};
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
-
#include "tests/libmemcached_world.h"
void get_world(Framework *world)
Test cases
*/
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +20
#include <libmemcached/memcached.h>
static test_return_t pre_sasl(memcached_st *)
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)))
{
}
else
{
- port= in_port_t(TEST_PORT_BASE +x);
+ port= in_port_t(libtest::get_free_port());
}
const char *argv[1]= { "memcached" };
}
}
- 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;
}
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);
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;
}
}
- 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;
}
{0, 0, 0, 0}
};
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
#include "tests/libmemcached_world.h"
void get_world(Framework *world)
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}