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