Updating for 1.0.2 release
[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_FATAL;
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
52 in_port_t max_port= TEST_PORT_BASE;
53 for (uint32_t x= 0; x < servers.count(); x++)
54 {
55 in_port_t port;
56
57 char variable_buffer[1024];
58 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
59
60 char *var;
61 if ((var= getenv(variable_buffer)))
62 {
63 port= in_port_t(atoi(var));
64 }
65 else
66 {
67 port= in_port_t(TEST_PORT_BASE +x);
68 }
69
70 max_port= port;
71 const char *argv[1]= { "memcached" };
72 if (servers.sasl())
73 {
74 if (not server_startup(servers, "memcached-sasl", port, 1, argv))
75 {
76 error= TEST_FATAL;
77 return NULL;
78 }
79 }
80 else
81 {
82 if (not server_startup(servers, "memcached", port, 1, argv))
83 {
84 error= TEST_FATAL;
85 return NULL;
86 }
87 }
88 }
89
90 if (servers.socket())
91 {
92 if (servers.sasl())
93 {
94 const char *argv[1]= { "memcached" };
95 if (not servers.start_socket_server("memcached-sasl", max_port +1, 1, argv))
96 {
97 error= TEST_FATAL;
98 return NULL;
99 }
100 }
101 else
102 {
103 const char *argv[1]= { "memcached" };
104 if (not servers.start_socket_server("memcached", max_port +1, 1, argv))
105 {
106 error= TEST_FATAL;
107 return NULL;
108 }
109 }
110 }
111
112
113 libmemcached_test_container_st *global_container= new libmemcached_test_container_st(servers);
114 if (global_container == NULL)
115 {
116 error= TEST_MEMORY_ALLOCATION_FAILURE;
117 return NULL;
118 }
119
120 error= TEST_SUCCESS;
121
122 return global_container;
123 }
124
125 static test_return_t world_container_startup(libmemcached_test_container_st *container)
126 {
127 char buffer[BUFSIZ];
128
129 test_compare_got(MEMCACHED_SUCCESS,
130 libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
131 buffer, sizeof(buffer)),
132 container->construct.option_string().c_str());
133
134 test_null(container->parent);
135 container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size());
136 test_true(container->parent);
137 test_compare(MEMCACHED_SUCCESS, memcached_version(container->parent));
138
139 if (container->construct.sasl())
140 {
141 if (memcached_failed(memcached_behavior_set(container->parent, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
142 {
143 memcached_free(container->parent);
144 return TEST_FAILURE;
145 }
146
147 if (memcached_failed(memcached_set_sasl_auth_data(container->parent, container->construct.username().c_str(), container->construct.password().c_str())))
148 {
149 memcached_free(container->parent);
150 return TEST_FAILURE;
151 }
152 }
153
154 for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host)
155 {
156 memcached_server_instance_st instance=
157 memcached_server_instance_by_position(container->parent, host);
158
159 if (instance->type == MEMCACHED_CONNECTION_TCP)
160 {
161 test_true_got(memcached_server_port(instance) >= TEST_PORT_BASE, memcached_server_port(instance));
162 }
163 }
164
165 return TEST_SUCCESS;
166 }
167
168 static test_return_t world_container_shutdown(libmemcached_test_container_st *container)
169 {
170 memcached_free(container->parent);
171 container->parent= NULL;
172
173 return TEST_SUCCESS;
174 }
175
176 static test_return_t world_test_startup(libmemcached_test_container_st *container)
177 {
178 test_true(container);
179 test_null(container->memc);
180 test_true(container->parent);
181 container->memc= memcached_clone(NULL, container->parent);
182 test_true(container->memc);
183
184 return TEST_SUCCESS;
185 }
186
187 test_return_t world_flush(libmemcached_test_container_st *container);
188 test_return_t world_flush(libmemcached_test_container_st *container)
189 {
190 test_true(container->memc);
191 memcached_flush(container->memc, 0);
192 memcached_quit(container->memc);
193
194 return TEST_SUCCESS;
195 }
196
197 static test_return_t world_pre_run(libmemcached_test_container_st *container)
198 {
199 test_true(container->memc);
200 for (uint32_t loop= 0; loop < memcached_server_list_count(container->memc->servers); loop++)
201 {
202 memcached_server_instance_st instance=
203 memcached_server_instance_by_position(container->memc, loop);
204
205 test_compare(-1, instance->fd);
206 test_compare(0U, instance->cursor_active);
207 }
208
209 return TEST_SUCCESS;
210 }
211
212
213 static test_return_t world_post_run(libmemcached_test_container_st *container)
214 {
215 test_true(container->memc);
216
217 return TEST_SUCCESS;
218 }
219
220 static test_return_t world_on_error(test_return_t , libmemcached_test_container_st *container)
221 {
222 test_true(container->memc);
223 memcached_free(container->memc);
224 container->memc= NULL;
225
226 return TEST_SUCCESS;
227 }
228
229 static bool world_destroy(void *object)
230 {
231 libmemcached_test_container_st *container= (libmemcached_test_container_st *)object;
232 #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
233 if (LIBMEMCACHED_WITH_SASL_SUPPORT)
234 {
235 sasl_done();
236 }
237 #endif
238
239 delete container;
240
241 return TEST_SUCCESS;
242 }
243
244 typedef test_return_t (*libmemcached_test_callback_fn)(memcached_st *);
245
246 static test_return_t _runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
247 {
248 if (func)
249 {
250 test_true(container);
251 test_true(container->memc);
252 return func(container->memc);
253 }
254
255 return TEST_SUCCESS;
256 }
257
258 static test_return_t _pre_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
259 {
260 if (func)
261 {
262 return func(container->parent);
263 }
264
265 return TEST_SUCCESS;
266 }
267
268 static test_return_t _post_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
269 {
270 if (func)
271 {
272 return func(container->parent);
273 }
274
275 return TEST_SUCCESS;
276 }
277
278 class LibmemcachedRunner : public Runner {
279 public:
280 test_return_t run(test_callback_fn* func, void *object)
281 {
282 return _runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
283 }
284
285 test_return_t pre(test_callback_fn* func, void *object)
286 {
287 return _pre_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
288 }
289
290 test_return_t post(test_callback_fn* func, void *object)
291 {
292 return _post_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
293 }
294 };
295
296 static LibmemcachedRunner defualt_libmemcached_runner;