Merge in pid/ping status.
[m6w6/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() :
24 parent(NULL),
25 memc(NULL)
26 { }
27 };
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /* Prototypes for functions we will pass to test framework */
34 libmemcached_test_container_st *world_create(test_return_t *error);
35 test_return_t world_test_startup(libmemcached_test_container_st *);
36 test_return_t world_flush(libmemcached_test_container_st *container);
37 test_return_t world_pre_run(libmemcached_test_container_st *);
38
39 test_return_t world_post_run(libmemcached_test_container_st *);
40 test_return_t world_on_error(test_return_t, libmemcached_test_container_st *);
41 test_return_t world_destroy(libmemcached_test_container_st *);
42
43 /**
44 @note generic shutdown/startup for libmemcached tests.
45 */
46 test_return_t world_container_startup(libmemcached_test_container_st *container);
47 test_return_t world_container_shutdown(libmemcached_test_container_st *container);
48
49 libmemcached_test_container_st *world_create(test_return_t *error)
50 {
51 libmemcached_test_container_st *global_container= new libmemcached_test_container_st();
52 if (not server_startup(&global_container->construct))
53 {
54 *error= TEST_FAILURE;
55 return NULL;
56 }
57
58 *error= TEST_SUCCESS;
59
60 return global_container;
61 }
62
63 test_return_t world_container_startup(libmemcached_test_container_st *container)
64 {
65 char buffer[BUFSIZ];
66
67 test_compare_got(MEMCACHED_SUCCESS,
68 libmemcached_check_configuration(container->construct.server_list.c_str(), container->construct.server_list.size(),
69 buffer, sizeof(buffer)),
70 buffer);
71
72 assert(not container->parent);
73 container->parent= memcached(container->construct.server_list.c_str(), container->construct.server_list.size());
74 test_true(container->parent);
75
76 return TEST_SUCCESS;
77 }
78
79 test_return_t world_container_shutdown(libmemcached_test_container_st *container)
80 {
81 memcached_free(container->parent);
82 container->parent= NULL;
83
84 return TEST_SUCCESS;
85 }
86
87 test_return_t world_test_startup(libmemcached_test_container_st *container)
88 {
89 assert(container);
90 assert(not container->memc);
91 assert(container->parent);
92 container->memc= memcached_clone(NULL, container->parent);
93 test_true(container->memc);
94
95 return TEST_SUCCESS;
96 }
97
98 test_return_t world_flush(libmemcached_test_container_st *container)
99 {
100 assert(container->memc);
101 memcached_flush(container->memc, 0);
102 memcached_quit(container->memc);
103
104 return TEST_SUCCESS;
105 }
106
107 test_return_t world_pre_run(libmemcached_test_container_st *container)
108 {
109 assert(container->memc);
110 for (uint32_t loop= 0; loop < memcached_server_list_count(container->memc->servers); loop++)
111 {
112 memcached_server_instance_st instance=
113 memcached_server_instance_by_position(container->memc, loop);
114
115 test_compare(-1, instance->fd);
116 test_compare(0, instance->cursor_active);
117 }
118
119 return TEST_SUCCESS;
120 }
121
122
123 test_return_t world_post_run(libmemcached_test_container_st *container)
124 {
125 test_true(container->memc);
126
127 return TEST_SUCCESS;
128 }
129
130 test_return_t world_on_error(test_return_t test_state, libmemcached_test_container_st *container)
131 {
132 (void)test_state;
133 assert(container->memc);
134 memcached_free(container->memc);
135 container->memc= NULL;
136
137 return TEST_SUCCESS;
138 }
139
140 test_return_t world_destroy(libmemcached_test_container_st *container)
141 {
142 server_startup_st *construct= &container->construct;
143
144 server_shutdown(construct);
145
146 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
147 sasl_done();
148 #endif
149
150 delete container;
151
152 return TEST_SUCCESS;
153 }
154
155 typedef test_return_t (*libmemcached_test_callback_fn)(memcached_st *);
156 static test_return_t _runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
157 {
158 if (func)
159 {
160 assert(container);
161 assert(container->memc);
162 return func(container->memc);
163 }
164 else
165 {
166 return TEST_SUCCESS;
167 }
168 }
169
170 static test_return_t _pre_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
171 {
172 if (func)
173 {
174 return func(container->parent);
175 }
176 else
177 {
178 return TEST_SUCCESS;
179 }
180 }
181
182 static test_return_t _post_runner_default(libmemcached_test_callback_fn func, libmemcached_test_container_st *container)
183 {
184 if (func)
185 {
186 return func(container->parent);
187 }
188 else
189 {
190 return TEST_SUCCESS;
191 }
192 }
193
194 #ifdef __cplusplus
195 }
196 #endif
197
198 #ifdef __cplusplus
199
200 static Runner defualt_libmemcached_runner= {
201 reinterpret_cast<test_callback_runner_fn*>(_pre_runner_default),
202 reinterpret_cast<test_callback_runner_fn*>(_runner_default),
203 reinterpret_cast<test_callback_runner_fn*>(_post_runner_default)
204 };
205
206 #else
207
208 static Runner defualt_libmemcached_runner= {
209 (test_callback_runner_fn)_pre_runner_default,
210 (test_callback_runner_fn)_runner_default,
211 (test_callback_runner_fn)_post_runner_default
212 };
213
214 #endif