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