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