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