Update testing to allow for distributed testing.
[m6w6/libmemcached] / tests / libmemcached-1.0 / generate.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached Client and Server
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <config.h>
39 #include <libtest/test.hpp>
40
41 #include <libmemcached/util.h>
42 #include <libmemcached/is.h>
43
44 #include <tests/libmemcached-1.0/generate.h>
45 #include <tests/libmemcached-1.0/fetch_all_results.h>
46 #include "tests/libmemcached-1.0/servers_to_create.h"
47 #include "tests/libmemcached-1.0/callback_counter.h"
48
49 #include "clients/generator.h"
50 #include "clients/execute.h"
51
52 #define GLOBAL_COUNT 10000
53 #define GLOBAL2_COUNT 100
54
55 static pairs_st *global_pairs;
56 static const char *global_keys[GLOBAL_COUNT];
57 static size_t global_keys_length[GLOBAL_COUNT];
58 static size_t global_count= 0;
59
60 test_return_t cleanup_pairs(memcached_st *memc)
61 {
62 (void)memc;
63 pairs_free(global_pairs);
64
65 return TEST_SUCCESS;
66 }
67
68 test_return_t generate_pairs(memcached_st *)
69 {
70 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
71 global_count= GLOBAL_COUNT;
72
73 for (size_t x= 0; x < global_count; x++)
74 {
75 global_keys[x]= global_pairs[x].key;
76 global_keys_length[x]= global_pairs[x].key_length;
77 }
78
79 return TEST_SUCCESS;
80 }
81
82 test_return_t generate_large_pairs(memcached_st *)
83 {
84 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
85 global_count= GLOBAL2_COUNT;
86
87 for (size_t x= 0; x < global_count; x++)
88 {
89 global_keys[x]= global_pairs[x].key;
90 global_keys_length[x]= global_pairs[x].key_length;
91 }
92
93 return TEST_SUCCESS;
94 }
95
96 test_return_t generate_data(memcached_st *memc)
97 {
98 unsigned int check_execute= execute_set(memc, global_pairs, global_count);
99
100 test_compare_warn_hint(global_count, check_execute, "Possible false, positive, memcached may have ejected key/value based on memory needs");
101
102 return TEST_SUCCESS;
103 }
104
105 test_return_t generate_data_with_stats(memcached_st *memc)
106 {
107 unsigned int check_execute= execute_set(memc, global_pairs, global_count);
108
109 test_compare(check_execute, global_count);
110
111 // @todo hosts used size stats
112 memcached_return_t rc;
113 memcached_stat_st *stat_p= memcached_stat(memc, NULL, &rc);
114 test_true(stat_p);
115
116 for (uint32_t host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
117 {
118 /* This test was changes so that "make test" would work properlly */
119 if (DEBUG)
120 {
121 memcached_server_instance_st instance=
122 memcached_server_instance_by_position(memc, host_index);
123
124 printf("\nserver %u|%s|%u bytes: %llu\n", host_index, instance->hostname, instance->port, (unsigned long long)(stat_p + host_index)->bytes);
125 }
126 test_true((unsigned long long)(stat_p + host_index)->bytes);
127 }
128
129 memcached_stat_free(NULL, stat_p);
130
131 return TEST_SUCCESS;
132 }
133
134 test_return_t generate_buffer_data(memcached_st *memc)
135 {
136 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
137 generate_data(memc);
138
139 return TEST_SUCCESS;
140 }
141
142 test_return_t get_read_count(memcached_st *memc)
143 {
144 memcached_st *memc_clone= memcached_clone(NULL, memc);
145 test_true(memc_clone);
146
147 memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
148
149 {
150 char *return_value;
151 size_t return_value_length;
152 uint32_t flags;
153 uint32_t count;
154
155 for (size_t x= count= 0; x < global_count; x++)
156 {
157 memcached_return_t rc;
158 return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
159 &return_value_length, &flags, &rc);
160 if (rc == MEMCACHED_SUCCESS)
161 {
162 count++;
163 if (return_value)
164 {
165 free(return_value);
166 }
167 }
168 }
169 }
170
171 memcached_free(memc_clone);
172
173 return TEST_SUCCESS;
174 }
175
176 test_return_t get_read(memcached_st *memc)
177 {
178 size_t keys_returned= 0;
179 for (size_t x= 0; x < global_count; x++)
180 {
181 size_t return_value_length;
182 uint32_t flags;
183 memcached_return_t rc;
184 char *return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
185 &return_value_length, &flags, &rc);
186 /*
187 test_true(return_value);
188 test_compare(MEMCACHED_SUCCESS, rc);
189 */
190 if (rc == MEMCACHED_SUCCESS && return_value)
191 {
192 keys_returned++;
193 free(return_value);
194 }
195 }
196 test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
197
198 return TEST_SUCCESS;
199 }
200
201 test_return_t mget_read(memcached_st *memc)
202 {
203
204 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
205
206 test_compare(MEMCACHED_SUCCESS,
207 memcached_mget(memc, global_keys, global_keys_length, global_count));
208
209 // Go fetch the keys and test to see if all of them were returned
210 {
211 unsigned int keys_returned;
212 test_compare(TEST_SUCCESS, fetch_all_results(memc, keys_returned));
213 test_true(keys_returned > 0);
214 test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
215 }
216
217 return TEST_SUCCESS;
218 }
219
220 test_return_t mget_read_result(memcached_st *memc)
221 {
222
223 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
224
225 test_compare(MEMCACHED_SUCCESS,
226 memcached_mget(memc, global_keys, global_keys_length, global_count));
227
228 /* Turn this into a help function */
229 {
230 memcached_result_st results_obj;
231 memcached_result_st *results= memcached_result_create(memc, &results_obj);
232 test_true(results);
233
234 memcached_return_t rc;
235 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
236 {
237 if (rc == MEMCACHED_IN_PROGRESS)
238 {
239 continue;
240 }
241
242 test_true(results);
243 test_compare(MEMCACHED_SUCCESS, rc);
244 }
245 test_compare(MEMCACHED_END, rc);
246
247 memcached_result_free(&results_obj);
248 }
249
250 return TEST_SUCCESS;
251 }
252
253 test_return_t mget_read_partial_result(memcached_st *memc)
254 {
255
256 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
257
258 test_compare(MEMCACHED_SUCCESS,
259 memcached_mget(memc, global_keys, global_keys_length, global_count));
260
261 // We will scan for just one key
262 {
263 memcached_result_st results_obj;
264 memcached_result_st *results= memcached_result_create(memc, &results_obj);
265
266 memcached_return_t rc;
267 results= memcached_fetch_result(memc, results, &rc);
268 test_true(results);
269 test_compare(MEMCACHED_SUCCESS, rc);
270
271 memcached_result_free(&results_obj);
272 }
273
274 // We already have a read happening, lets start up another one.
275 test_compare(MEMCACHED_SUCCESS,
276 memcached_mget(memc, global_keys, global_keys_length, global_count));
277 {
278 memcached_result_st results_obj;
279 memcached_result_st *results= memcached_result_create(memc, &results_obj);
280 test_true(results);
281 test_false(memcached_is_allocated(results));
282
283 memcached_return_t rc;
284 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
285 {
286 test_true(results);
287 test_compare(MEMCACHED_SUCCESS, rc);
288 }
289 test_compare(MEMCACHED_END, rc);
290
291 memcached_result_free(&results_obj);
292 }
293
294 return TEST_SUCCESS;
295 }
296
297 test_return_t mget_read_function(memcached_st *memc)
298 {
299 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
300
301 test_compare(MEMCACHED_SUCCESS,
302 memcached_mget(memc, global_keys, global_keys_length, global_count));
303
304 memcached_execute_fn callbacks[]= { &callback_counter };
305 size_t counter= 0;
306 test_compare(MEMCACHED_SUCCESS,
307 memcached_fetch_execute(memc, callbacks, (void *)&counter, 1));
308
309 return TEST_SUCCESS;
310 }
311
312 test_return_t delete_generate(memcached_st *memc)
313 {
314 size_t total= 0;
315 for (size_t x= 0; x < global_count; x++)
316 {
317 if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
318 {
319 total++;
320 }
321 }
322 test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
323
324 return TEST_SUCCESS;
325 }
326
327 test_return_t delete_buffer_generate(memcached_st *memc)
328 {
329 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
330
331 size_t total= 0;
332 for (size_t x= 0; x < global_count; x++)
333 {
334 if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
335 {
336 total++;
337 }
338 }
339 test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
340
341 return TEST_SUCCESS;
342 }
343
344 test_return_t mget_read_internal_result(memcached_st *memc)
345 {
346
347 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
348
349 test_compare(MEMCACHED_SUCCESS,
350 memcached_mget(memc, global_keys, global_keys_length, global_count));
351 {
352 memcached_result_st *results= NULL;
353 memcached_return_t rc;
354 while ((results= memcached_fetch_result(memc, results, &rc)))
355 {
356 test_true(results);
357 test_compare(MEMCACHED_SUCCESS, rc);
358 }
359 test_compare(MEMCACHED_END, rc);
360
361 memcached_result_free(results);
362 }
363
364 return TEST_SUCCESS;
365 }
366