Merge build trunk.
[awesomized/libmemcached] / tests / libmemcached_world.h
1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Description: This is the startup bits for any libmemcached test.
9 *
10 */
11
12 #pragma once
13
14 #include <cassert>
15
16 /* The structure we use for the test system */
17 struct libmemcached_test_container_st
18 {
19 server_startup_st& construct;
20 memcached_st *parent;
21 memcached_st *memc;
22
23 libmemcached_test_container_st(server_startup_st &construct_arg) :
24 construct(construct_arg),
25 parent(NULL),
26 memc(NULL)
27 { }
28 };
29
30 static void *world_create(server_startup_st& servers, test_return_t& error)
31 {
32 if (HAVE_MEMCACHED_BINARY == 0)
33 {
34 error= TEST_SKIPPED;
35 return NULL;
36 }
37
38 if (servers.sasl() and (LIBMEMCACHED_WITH_SASL_SUPPORT == 0 or MEMCACHED_SASL_BINARY == 0))
39 {
40 error= TEST_SKIPPED;
41 return NULL;
42 }
43
44 // Assume we are running under valgrind, and bail
45 if (servers.sasl() and getenv("TESTS_ENVIRONMENT"))
46 {
47 error= TEST_SKIPPED;
48 return NULL;
49 }
50
51 in_port_t max_port= TEST_PORT_BASE;
52 for (uint32_t x= 0; x < servers.count(); x++)
53 {
54 in_port_t port;
55
56 char variable_buffer[1024];
57 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
58
59 char *var;
60 if ((var= getenv(variable_buffer)))
61 {
62 port= in_port_t(atoi(var));
63 }
64 else
65 {
66 port= in_port_t(TEST_PORT_BASE +x);
67 }
68
69 max_port= port;
70 const char *argv[1]= { "memcached" };
71 if (servers.sasl())
72 {
73 if (not server_startup(servers, "memcached-sasl", port, 1, argv))
74 {
75 error= TEST_FATAL;
76 return NULL;
77 }
78 }
79 else
80 {
81 if (not server_startup(servers, "memcached", port, 1, argv))
82 {
83 error= TEST_FATAL;
84 return NULL;
85 }
86 }
87 }
88
89 if (servers.socket())
90 {
91 if (servers.sasl())
92 {
93 const char *argv[1]= { "memcached" };
94 if (not servers.start_socket_server("memcached-sasl", max_port +1, 1, argv))
95 {
96 error= TEST_FATAL;
97 return NULL;
98 }
99 }
100 else
101 {
102 const char *argv[1]= { "memcached" };
103 if (not servers.start_socket_server("memcached", max_port +1, 1, argv))
104 {
105 error= TEST_FATAL;
106 return NULL;
107 }
108 }
109 }
110
111
112 libmemcached_test_container_st *global_container= new libmemcached_test_container_st(servers);
113 if (global_container == NULL)
114 {
115 error= TEST_MEMORY_ALLOCATION_FAILURE;
116 return NULL;
117 }
118
119 error= TEST_SUCCESS;
120
121 return global_container;
122 }
123
124 static test_return_t world_container_startup(libmemcached_test_container_st *container)
125 {
126 char buffer[BUFSIZ];
127
128 test_compare_got(MEMCACHED_SUCCESS,
129 libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
130 buffer, sizeof(buffer)),
131 container->construct.option_string().c_str());
132
133 test_null(container->parent);
134 container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size());
135 test_true(container->parent);
136 test_compare(MEMCACHED_SUCCESS, memcached_version(container->parent));
137
138 if (container->construct.sasl())
139 {
140 if (memcached_failed(memcached_behavior_set(container->parent, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
141 {
142 memcached_free(container->parent);
143 return TEST_FAILURE;
144 }
145
146 if (memcached_failed(memcached_set_sasl_auth_data(container->parent, container->construct.username().c_str(), container->construct.password().c_str())))
147 {
148 memcached_free(container->parent);
149 return TEST_FAILURE;
150 }
151 }
152
153 for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host)
154 {
155 memcached_server_instance_st instance=
156 memcached_server_instance_by_position(container->parent, host);
157
158 if (instance->type == MEMCACHED_CONNECTION_TCP)
159 {
160 test_true_got(memcached_server_port(instance) >= TEST_PORT_BASE, memcached_server_port(instance));
161 }
162 }
163
164 return TEST_SUCCESS;
165 }
166
167 static test_return_t world_container_shutdown(libmemcached_test_container_st *container)
168 {
169 memcached_free(container->parent);
170 container->parent= NULL;
171
172 return TEST_SUCCESS;
173 }
174
175 static test_return_t world_test_startup(libmemcached_test_container_st *container)
176 {
177 test_true(container);
178 test_null(container->memc);
179 test_true(container->parent);
180 container->memc= memcached_clone(NULL, container->parent);
181 test_true(container->memc);
182
183 return TEST_SUCCESS;
184 }
185
186 test_return_t world_flush(libmemcached_test_container_st *container);
187 test_return_t world_flush(libmemcached_test_container_st *container)
188 {
189 test_true(container->memc);
190 memcached_flush(container->memc, 0);
191 memcached_quit(container->memc);
192
193 return TEST_SUCCESS;
194 }
195
196 static test_return_t world_pre_run(libmemcached_test_container_st *container)
197 {
198 test_true(container->memc);
199 for (uint32_t loop= 0; loop < memcached_server_list_count(container->memc->servers); loop++)
200 {
201 memcached_server_instance_st instance=
202 memcached_server_instance_by_position(container->memc, loop);
203
204 test_compare(-1, instance->fd);
205 test_compare(0U, instance->cursor_active);
206 }
207
208 return TEST_SUCCESS;
209 }
210
211
212 static test_return_t world_post_run(libmemcached_test_container_st *container)
213 {
214 test_true(container->memc);
215
216 return TEST_SUCCESS;
217 }
218
219 static test_return_t world_on_error(test_return_t , libmemcached_test_container_st *container)
220 {
221 test_true(container->memc);
222 memcached_free(container->memc);
223 container->memc= NULL;
224
225 return TEST_SUCCESS;
226 }
227
228 static bool world_destroy(void *object)
229 {
230 libmemcached_test_container_st *container= (libmemcached_test_container_st *)object;
231 #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
232 if (LIBMEMCACHED_WITH_SASL_SUPPORT)
233 {
234 sasl_done();
235 }
236 #endif
237
238 delete container;
239
240 return TEST_SUCCESS;
241 }
242
243 typedef test_return_t (*libmemcached_test_callback_fn)(memcached_st *);
244
245 static test_return_t _runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
246 {
247 if (func)
248 {
249 test_true(container);
250 test_true(container->memc);
251 test_return_t ret;
252 try {
253 ret= func(container->memc);
254 }
255 catch (std::exception& e)
256 {
257 Error << e.what();
258 return TEST_FAILURE;
259 }
260
261 return ret;
262 }
263
264 return TEST_SUCCESS;
265 }
266
267 static test_return_t _pre_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
268 {
269 if (func)
270 {
271 return func(container->parent);
272 }
273
274 return TEST_SUCCESS;
275 }
276
277 static test_return_t _post_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
278 {
279 if (func)
280 {
281 return func(container->parent);
282 }
283
284 return TEST_SUCCESS;
285 }
286
287 class LibmemcachedRunner : public Runner {
288 public:
289 test_return_t run(test_callback_fn* func, void *object)
290 {
291 return _runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
292 }
293
294 test_return_t pre(test_callback_fn* func, void *object)
295 {
296 return _pre_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
297 }
298
299 test_return_t post(test_callback_fn* func, void *object)
300 {
301 return _post_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
302 }
303 };
304
305 static LibmemcachedRunner defualt_libmemcached_runner;