1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Description: This is the startup bits for any libmemcached test.
16 /* The structure we use for the test system */
17 struct libmemcached_test_container_st
19 server_startup_st
& construct
;
23 libmemcached_test_container_st(server_startup_st
&construct_arg
) :
24 construct(construct_arg
),
30 #define SERVERS_TO_CREATE 5
31 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
33 static void *world_create(server_startup_st
& servers
, test_return_t
& error
)
36 for (uint32_t x
= 0; x
< SERVERS_TO_CREATE
; x
++)
40 char variable_buffer
[1024];
41 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBMEMCACHED_PORT_%u", x
);
44 if ((var
= getenv(variable_buffer
)))
46 port
= in_port_t(atoi(var
));
50 port
= in_port_t(TEST_PORT_BASE
+x
);
54 const char *argv
[1]= { "memcached" };
55 if (not server_startup(servers
, "memcached", port
, 1, argv
))
62 const char *argv
[1]= { "memcached" };
63 if (not servers
.start_socket_server("memcached", max_port
+1, 1, argv
))
70 libmemcached_test_container_st
*global_container
= new libmemcached_test_container_st(servers
);
71 if (global_container
== NULL
)
73 error
= TEST_MEMORY_ALLOCATION_FAILURE
;
79 return global_container
;
82 static test_return_t
world_container_startup(libmemcached_test_container_st
*container
)
86 test_compare_got(MEMCACHED_SUCCESS
,
87 libmemcached_check_configuration(container
->construct
.option_string().c_str(), container
->construct
.option_string().size(),
88 buffer
, sizeof(buffer
)),
89 container
->construct
.option_string().c_str());
91 test_true(not container
->parent
);
92 container
->parent
= memcached(container
->construct
.option_string().c_str(), container
->construct
.option_string().size());
93 test_true(container
->parent
);
95 for (uint32_t host
= 0; host
< memcached_server_count(container
->parent
); ++host
)
97 memcached_server_instance_st instance
=
98 memcached_server_instance_by_position(container
->parent
, host
);
100 if (instance
->type
== MEMCACHED_CONNECTION_TCP
)
102 test_true_got(memcached_server_port(instance
) >= TEST_PORT_BASE
, memcached_server_port(instance
));
109 static test_return_t
world_container_shutdown(libmemcached_test_container_st
*container
)
111 memcached_free(container
->parent
);
112 container
->parent
= NULL
;
117 static test_return_t
world_test_startup(libmemcached_test_container_st
*container
)
119 test_true(container
);
120 test_true(not container
->memc
);
121 test_true(container
->parent
);
122 container
->memc
= memcached_clone(NULL
, container
->parent
);
123 test_true(container
->memc
);
128 test_return_t
world_flush(libmemcached_test_container_st
*container
);
129 test_return_t
world_flush(libmemcached_test_container_st
*container
)
131 test_true(container
->memc
);
132 memcached_flush(container
->memc
, 0);
133 memcached_quit(container
->memc
);
138 static test_return_t
world_pre_run(libmemcached_test_container_st
*container
)
140 test_true(container
->memc
);
141 for (uint32_t loop
= 0; loop
< memcached_server_list_count(container
->memc
->servers
); loop
++)
143 memcached_server_instance_st instance
=
144 memcached_server_instance_by_position(container
->memc
, loop
);
146 test_compare(-1, instance
->fd
);
147 test_compare(0U, instance
->cursor_active
);
154 static test_return_t
world_post_run(libmemcached_test_container_st
*container
)
156 test_true(container
->memc
);
161 static test_return_t
world_on_error(test_return_t test_state
, libmemcached_test_container_st
*container
)
164 test_true(container
->memc
);
165 memcached_free(container
->memc
);
166 container
->memc
= NULL
;
171 static bool world_destroy(void *object
)
173 libmemcached_test_container_st
*container
= (libmemcached_test_container_st
*)object
;
174 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
183 typedef test_return_t (*libmemcached_test_callback_fn
)(memcached_st
*);
185 static test_return_t
_runner_default(libmemcached_test_callback_fn func
, libmemcached_test_container_st
*container
)
189 test_true(container
);
190 test_true(container
->memc
);
191 return func(container
->memc
);
197 static test_return_t
_pre_runner_default(libmemcached_test_callback_fn func
, libmemcached_test_container_st
*container
)
201 return func(container
->parent
);
207 static test_return_t
_post_runner_default(libmemcached_test_callback_fn func
, libmemcached_test_container_st
*container
)
211 return func(container
->parent
);
217 class LibmemcachedRunner
: public Runner
{
219 test_return_t
run(test_callback_fn
* func
, void *object
)
221 return _runner_default(libmemcached_test_callback_fn(func
), (libmemcached_test_container_st
*)object
);
224 test_return_t
pre(test_callback_fn
* func
, void *object
)
226 return _pre_runner_default(libmemcached_test_callback_fn(func
), (libmemcached_test_container_st
*)object
);
229 test_return_t
post(test_callback_fn
* func
, void *object
)
231 return _post_runner_default(libmemcached_test_callback_fn(func
), (libmemcached_test_container_st
*)object
);
235 static LibmemcachedRunner defualt_libmemcached_runner
;