Update testings/docs via merge from build trunk.
[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/callback_counter.h"
47
48 #include "clients/generator.h"
49 #include "clients/execute.h"
50
51 #define GLOBAL_COUNT 10000
52 #define GLOBAL2_COUNT 100
53
54 static pairs_st *global_pairs;
55 static const char *global_keys[GLOBAL_COUNT];
56 static size_t global_keys_length[GLOBAL_COUNT];
57 static size_t global_count= 0;
58
59 test_return_t cleanup_pairs(memcached_st *memc)
60 {
61 (void)memc;
62 pairs_free(global_pairs);
63
64 return TEST_SUCCESS;
65 }
66
67 test_return_t generate_pairs(memcached_st *)
68 {
69 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
70 global_count= GLOBAL_COUNT;
71
72 for (size_t x= 0; x < global_count; x++)
73 {
74 global_keys[x]= global_pairs[x].key;
75 global_keys_length[x]= global_pairs[x].key_length;
76 }
77
78 return TEST_SUCCESS;
79 }
80
81 test_return_t generate_large_pairs(memcached_st *)
82 {
83 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
84 global_count= GLOBAL2_COUNT;
85
86 for (size_t x= 0; x < global_count; x++)
87 {
88 global_keys[x]= global_pairs[x].key;
89 global_keys_length[x]= global_pairs[x].key_length;
90 }
91
92 return TEST_SUCCESS;
93 }
94
95 test_return_t generate_data(memcached_st *memc)
96 {
97 unsigned int check_execute= execute_set(memc, global_pairs, global_count);
98
99 test_compare_warn_hint(global_count, check_execute, "Possible false, positive, memcached may have ejected key/value based on memory needs");
100
101 return TEST_SUCCESS;
102 }
103
104 test_return_t generate_data_with_stats(memcached_st *memc)
105 {
106 unsigned int check_execute= execute_set(memc, global_pairs, global_count);
107
108 test_compare(check_execute, global_count);
109
110 // @todo hosts used size stats
111 memcached_return_t rc;
112 memcached_stat_st *stat_p= memcached_stat(memc, NULL, &rc);
113 test_true(stat_p);
114
115 for (uint32_t host_index= 0; host_index < memcached_server_count(memc); host_index++)
116 {
117 /* This test was changes so that "make test" would work properlly */
118 if (DEBUG)
119 {
120 memcached_server_instance_st instance=
121 memcached_server_instance_by_position(memc, host_index);
122
123 printf("\nserver %u|%s|%u bytes: %llu\n", host_index, instance->hostname, instance->port, (unsigned long long)(stat_p + host_index)->bytes);
124 }
125 test_true((unsigned long long)(stat_p + host_index)->bytes);
126 }
127
128 memcached_stat_free(NULL, stat_p);
129
130 return TEST_SUCCESS;
131 }
132
133 test_return_t generate_buffer_data(memcached_st *memc)
134 {
135 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
136 generate_data(memc);
137
138 return TEST_SUCCESS;
139 }
140
141 test_return_t get_read_count(memcached_st *memc)
142 {
143 memcached_st *memc_clone= memcached_clone(NULL, memc);
144 test_true(memc_clone);
145
146 memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
147
148 {
149 char *return_value;
150 size_t return_value_length;
151 uint32_t flags;
152 uint32_t count;
153
154 for (size_t x= count= 0; x < global_count; x++)
155 {
156 memcached_return_t rc;
157 return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
158 &return_value_length, &flags, &rc);
159 if (rc == MEMCACHED_SUCCESS)
160 {
161 count++;
162 if (return_value)
163 {
164 free(return_value);
165 }
166 }
167 }
168 }
169
170 memcached_free(memc_clone);
171
172 return TEST_SUCCESS;
173 }
174
175 test_return_t get_read(memcached_st *memc)
176 {
177 size_t keys_returned= 0;
178 for (size_t x= 0; x < global_count; x++)
179 {
180 size_t return_value_length;
181 uint32_t flags;
182 memcached_return_t rc;
183 char *return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
184 &return_value_length, &flags, &rc);
185 /*
186 test_true(return_value);
187 test_compare(MEMCACHED_SUCCESS, rc);
188 */
189 if (rc == MEMCACHED_SUCCESS && return_value)
190 {
191 keys_returned++;
192 free(return_value);
193 }
194 }
195 test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
196
197 return TEST_SUCCESS;
198 }
199
200 test_return_t mget_read(memcached_st *memc)
201 {
202
203 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
204
205 test_compare(MEMCACHED_SUCCESS,
206 memcached_mget(memc, global_keys, global_keys_length, global_count));
207
208 // Go fetch the keys and test to see if all of them were returned
209 {
210 unsigned int keys_returned;
211 test_compare(TEST_SUCCESS, fetch_all_results(memc, keys_returned));
212 test_true(keys_returned > 0);
213 test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
214 }
215
216 return TEST_SUCCESS;
217 }
218
219 test_return_t mget_read_result(memcached_st *memc)
220 {
221
222 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
223
224 test_compare(MEMCACHED_SUCCESS,
225 memcached_mget(memc, global_keys, global_keys_length, global_count));
226
227 /* Turn this into a help function */
228 {
229 memcached_result_st results_obj;
230 memcached_result_st *results= memcached_result_create(memc, &results_obj);
231 test_true(results);
232
233 memcached_return_t rc;
234 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
235 {
236 if (rc == MEMCACHED_IN_PROGRESS)
237 {
238 continue;
239 }
240
241 test_true(results);
242 test_compare(MEMCACHED_SUCCESS, rc);
243 }
244 test_compare(MEMCACHED_END, rc);
245
246 memcached_result_free(&results_obj);
247 }
248
249 return TEST_SUCCESS;
250 }
251
252 test_return_t mget_read_partial_result(memcached_st *memc)
253 {
254
255 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
256
257 test_compare(MEMCACHED_SUCCESS,
258 memcached_mget(memc, global_keys, global_keys_length, global_count));
259
260 // We will scan for just one key
261 {
262 memcached_result_st results_obj;
263 memcached_result_st *results= memcached_result_create(memc, &results_obj);
264
265 memcached_return_t rc;
266 results= memcached_fetch_result(memc, results, &rc);
267 test_true(results);
268 test_compare(MEMCACHED_SUCCESS, rc);
269
270 memcached_result_free(&results_obj);
271 }
272
273 // We already have a read happening, lets start up another one.
274 test_compare(MEMCACHED_SUCCESS,
275 memcached_mget(memc, global_keys, global_keys_length, global_count));
276 {
277 memcached_result_st results_obj;
278 memcached_result_st *results= memcached_result_create(memc, &results_obj);
279 test_true(results);
280 test_false(memcached_is_allocated(results));
281
282 memcached_return_t rc;
283 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
284 {
285 test_true(results);
286 test_compare(MEMCACHED_SUCCESS, rc);
287 }
288 test_compare(MEMCACHED_END, rc);
289
290 memcached_result_free(&results_obj);
291 }
292
293 return TEST_SUCCESS;
294 }
295
296 test_return_t mget_read_function(memcached_st *memc)
297 {
298 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
299
300 test_compare(MEMCACHED_SUCCESS,
301 memcached_mget(memc, global_keys, global_keys_length, global_count));
302
303 memcached_execute_fn callbacks[]= { &callback_counter };
304 size_t counter= 0;
305 test_compare(MEMCACHED_SUCCESS,
306 memcached_fetch_execute(memc, callbacks, (void *)&counter, 1));
307
308 return TEST_SUCCESS;
309 }
310
311 test_return_t delete_generate(memcached_st *memc)
312 {
313 size_t total= 0;
314 for (size_t x= 0; x < global_count; x++)
315 {
316 if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
317 {
318 total++;
319 }
320 }
321 test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
322
323 return TEST_SUCCESS;
324 }
325
326 test_return_t delete_buffer_generate(memcached_st *memc)
327 {
328 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
329
330 size_t total= 0;
331 for (size_t x= 0; x < global_count; x++)
332 {
333 if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
334 {
335 total++;
336 }
337 }
338 test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
339
340 return TEST_SUCCESS;
341 }
342
343 test_return_t mget_read_internal_result(memcached_st *memc)
344 {
345
346 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
347
348 test_compare(MEMCACHED_SUCCESS,
349 memcached_mget(memc, global_keys, global_keys_length, global_count));
350 {
351 memcached_result_st *results= NULL;
352 memcached_return_t rc;
353 while ((results= memcached_fetch_result(memc, results, &rc)))
354 {
355 test_true(results);
356 test_compare(MEMCACHED_SUCCESS, rc);
357 }
358 test_compare(MEMCACHED_END, rc);
359
360 memcached_result_free(results);
361 }
362
363 return TEST_SUCCESS;
364 }
365