968c235e6f027cc089061307682469c1b91d8b08
[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 #define SERVERS_TO_CREATE 5
31 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
32
33 static void *world_create(server_startup_st& servers, test_return_t& error)
34 {
35 in_port_t max_port;
36 for (uint32_t x= 0; x < SERVERS_TO_CREATE; x++)
37 {
38 in_port_t port;
39
40 char variable_buffer[1024];
41 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
42
43 char *var;
44 if ((var= getenv(variable_buffer)))
45 {
46 port= in_port_t(atoi(var));
47 }
48 else
49 {
50 port= in_port_t(TEST_PORT_BASE +x);
51 }
52
53 max_port= port;
54 const char *argv[1]= { "memcached" };
55 if (not server_startup(servers, "memcached", port, 1, argv))
56 {
57 error= TEST_FAILURE;
58 return NULL;
59 }
60 }
61
62 if (servers.socket())
63 {
64 const char *argv[1]= { "memcached" };
65 if (not servers.start_socket_server("memcached", max_port +1, 1, argv))
66 {
67 error= TEST_FAILURE;
68 return NULL;
69 }
70 }
71
72
73 libmemcached_test_container_st *global_container= new libmemcached_test_container_st(servers);
74 if (global_container == NULL)
75 {
76 error= TEST_MEMORY_ALLOCATION_FAILURE;
77 return NULL;
78 }
79
80 error= TEST_SUCCESS;
81
82 return global_container;
83 }
84
85 static test_return_t world_container_startup(libmemcached_test_container_st *container)
86 {
87 char buffer[BUFSIZ];
88
89 test_compare_got(MEMCACHED_SUCCESS,
90 libmemcached_check_configuration(container->construct.option_string().c_str(), container->construct.option_string().size(),
91 buffer, sizeof(buffer)),
92 container->construct.option_string().c_str());
93
94 test_true(not container->parent);
95 container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size());
96 test_true(container->parent);
97
98 for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host)
99 {
100 memcached_server_instance_st instance=
101 memcached_server_instance_by_position(container->parent, host);
102
103 if (instance->type == MEMCACHED_CONNECTION_TCP)
104 {
105 test_true_got(memcached_server_port(instance) >= TEST_PORT_BASE, memcached_server_port(instance));
106 }
107 }
108
109 return TEST_SUCCESS;
110 }
111
112 static test_return_t world_container_shutdown(libmemcached_test_container_st *container)
113 {
114 memcached_free(container->parent);
115 container->parent= NULL;
116
117 return TEST_SUCCESS;
118 }
119
120 static test_return_t world_test_startup(libmemcached_test_container_st *container)
121 {
122 test_true(container);
123 test_true(not container->memc);
124 test_true(container->parent);
125 container->memc= memcached_clone(NULL, container->parent);
126 test_true(container->memc);
127
128 return TEST_SUCCESS;
129 }
130
131 test_return_t world_flush(libmemcached_test_container_st *container);
132 test_return_t world_flush(libmemcached_test_container_st *container)
133 {
134 test_true(container->memc);
135 memcached_flush(container->memc, 0);
136 memcached_quit(container->memc);
137
138 return TEST_SUCCESS;
139 }
140
141 static test_return_t world_pre_run(libmemcached_test_container_st *container)
142 {
143 test_true(container->memc);
144 for (uint32_t loop= 0; loop < memcached_server_list_count(container->memc->servers); loop++)
145 {
146 memcached_server_instance_st instance=
147 memcached_server_instance_by_position(container->memc, loop);
148
149 test_compare(-1, instance->fd);
150 test_compare(0U, instance->cursor_active);
151 }
152
153 return TEST_SUCCESS;
154 }
155
156
157 static test_return_t world_post_run(libmemcached_test_container_st *container)
158 {
159 test_true(container->memc);
160
161 return TEST_SUCCESS;
162 }
163
164 static test_return_t world_on_error(test_return_t test_state, libmemcached_test_container_st *container)
165 {
166 (void)test_state;
167 test_true(container->memc);
168 memcached_free(container->memc);
169 container->memc= NULL;
170
171 return TEST_SUCCESS;
172 }
173
174 static bool world_destroy(void *object)
175 {
176 libmemcached_test_container_st *container= (libmemcached_test_container_st *)object;
177 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
178 sasl_done();
179 #endif
180
181 delete container;
182
183 return TEST_SUCCESS;
184 }
185
186 typedef test_return_t (*libmemcached_test_callback_fn)(memcached_st *);
187
188 static test_return_t _runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
189 {
190 if (func)
191 {
192 test_true(container);
193 test_true(container->memc);
194 return func(container->memc);
195 }
196
197 return TEST_SUCCESS;
198 }
199
200 static test_return_t _pre_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
201 {
202 if (func)
203 {
204 return func(container->parent);
205 }
206
207 return TEST_SUCCESS;
208 }
209
210 static test_return_t _post_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
211 {
212 if (func)
213 {
214 return func(container->parent);
215 }
216
217 return TEST_SUCCESS;
218 }
219
220 class LibmemcachedRunner : public Runner {
221 public:
222 test_return_t run(test_callback_fn* func, void *object)
223 {
224 return _runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
225 }
226
227 test_return_t pre(test_callback_fn* func, void *object)
228 {
229 return _pre_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
230 }
231
232 test_return_t post(test_callback_fn* func, void *object)
233 {
234 return _post_runner_default(libmemcached_test_callback_fn(func), (libmemcached_test_container_st*)object);
235 }
236 };
237
238 static LibmemcachedRunner defualt_libmemcached_runner;