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