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