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