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