1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Libmemcached Client and Server
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
39 #include <libtest/test.hpp>
41 #include <libmemcached/util.h>
42 #include <libmemcached/is.h>
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"
48 #include "clients/generator.h"
49 #include "clients/execute.h"
51 #define GLOBAL_COUNT 10000
52 #define GLOBAL2_COUNT 100
54 using namespace libtest
;
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;
61 test_return_t
cleanup_pairs(memcached_st
*)
63 pairs_free(global_pairs
);
69 static test_return_t
generate_pairs(memcached_st
*)
71 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
72 global_count
= GLOBAL_COUNT
;
74 for (size_t x
= 0; x
< global_count
; x
++)
76 global_keys
[x
]= global_pairs
[x
].key
;
77 global_keys_length
[x
]= global_pairs
[x
].key_length
;
83 test_return_t
generate_large_pairs(memcached_st
*memc
)
85 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
86 global_count
= GLOBAL2_COUNT
;
88 for (size_t x
= 0; x
< global_count
; x
++)
90 global_keys
[x
]= global_pairs
[x
].key
;
91 global_keys_length
[x
]= global_pairs
[x
].key_length
;
94 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true);
95 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
97 test_compare_warn_hint(global_count
, check_execute
, "Possible false, positive, memcached may have ejected key/value based on memory needs");
102 test_return_t
generate_data(memcached_st
*memc
)
104 test_compare(TEST_SUCCESS
, generate_pairs(memc
));
106 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
108 test_compare_warn_hint(global_count
, check_execute
, "Possible false, positive, memcached may have ejected key/value based on memory needs");
113 test_return_t
generate_data_with_stats(memcached_st
*memc
)
115 test_compare(TEST_SUCCESS
, generate_pairs(memc
));
117 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
119 test_compare(check_execute
, global_count
);
121 // @todo hosts used size stats
122 memcached_return_t rc
;
123 memcached_stat_st
*stat_p
= memcached_stat(memc
, NULL
, &rc
);
126 for (uint32_t host_index
= 0; host_index
< memcached_server_count(memc
); host_index
++)
128 /* This test was changes so that "make test" would work properlly */
131 memcached_server_instance_st instance
=
132 memcached_server_instance_by_position(memc
, host_index
);
134 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
136 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
139 memcached_stat_free(NULL
, stat_p
);
144 test_return_t
generate_buffer_data(memcached_st
*memc
)
146 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true);
152 test_return_t
get_read_count(memcached_st
*memc
)
154 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
155 test_true(memc_clone
);
157 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
161 size_t return_value_length
;
165 for (size_t x
= count
= 0; x
< global_count
; x
++)
167 memcached_return_t rc
;
168 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
169 &return_value_length
, &flags
, &rc
);
170 if (rc
== MEMCACHED_SUCCESS
)
181 memcached_free(memc_clone
);
186 test_return_t
get_read(memcached_st
*memc
)
188 size_t keys_returned
= 0;
189 for (size_t x
= 0; x
< global_count
; x
++)
191 size_t return_value_length
;
193 memcached_return_t rc
;
194 char *return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
195 &return_value_length
, &flags
, &rc
);
197 test_true(return_value);
198 test_compare(MEMCACHED_SUCCESS, rc);
200 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
206 test_compare_warn_hint(global_count
, keys_returned
, "Possible false, positive, memcached may have ejected key/value based on memory needs");
211 test_return_t
mget_read(memcached_st
*memc
)
214 test_skip(true, bool(libmemcached_util_version_check(memc
, 1, 4, 4)));
216 test_compare(MEMCACHED_SUCCESS
,
217 memcached_mget(memc
, global_keys
, global_keys_length
, global_count
));
219 // Go fetch the keys and test to see if all of them were returned
221 unsigned int keys_returned
;
222 test_compare(TEST_SUCCESS
, fetch_all_results(memc
, keys_returned
));
223 test_true(keys_returned
> 0);
224 test_compare_warn_hint(global_count
, keys_returned
, "Possible false, positive, memcached may have ejected key/value based on memory needs");
230 test_return_t
mget_read_result(memcached_st
*memc
)
233 test_skip(true, bool(libmemcached_util_version_check(memc
, 1, 4, 4)));
235 test_compare(MEMCACHED_SUCCESS
,
236 memcached_mget(memc
, global_keys
, global_keys_length
, global_count
));
238 /* Turn this into a help function */
240 memcached_result_st results_obj
;
241 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
244 memcached_return_t rc
;
245 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
247 if (rc
== MEMCACHED_IN_PROGRESS
)
253 test_compare(MEMCACHED_SUCCESS
, rc
);
255 test_compare(MEMCACHED_END
, rc
);
257 memcached_result_free(&results_obj
);
263 test_return_t
mget_read_partial_result(memcached_st
*memc
)
266 test_skip(true, bool(libmemcached_util_version_check(memc
, 1, 4, 4)));
268 test_compare(MEMCACHED_SUCCESS
,
269 memcached_mget(memc
, global_keys
, global_keys_length
, global_count
));
271 // We will scan for just one key
273 memcached_result_st results_obj
;
274 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
276 memcached_return_t rc
;
277 results
= memcached_fetch_result(memc
, results
, &rc
);
279 test_compare(MEMCACHED_SUCCESS
, rc
);
281 memcached_result_free(&results_obj
);
284 // We already have a read happening, lets start up another one.
285 test_compare(MEMCACHED_SUCCESS
,
286 memcached_mget(memc
, global_keys
, global_keys_length
, global_count
));
288 memcached_result_st results_obj
;
289 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
291 test_false(memcached_is_allocated(results
));
293 memcached_return_t rc
;
294 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
297 test_compare(MEMCACHED_SUCCESS
, rc
);
299 test_compare(MEMCACHED_END
, rc
);
301 memcached_result_free(&results_obj
);
307 test_return_t
mget_read_function(memcached_st
*memc
)
309 test_skip(true, bool(libmemcached_util_version_check(memc
, 1, 4, 4)));
311 test_compare(MEMCACHED_SUCCESS
,
312 memcached_mget(memc
, global_keys
, global_keys_length
, global_count
));
314 memcached_execute_fn callbacks
[]= { &callback_counter
};
316 test_compare(MEMCACHED_SUCCESS
,
317 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
322 test_return_t
delete_generate(memcached_st
*memc
)
325 for (size_t x
= 0; x
< global_count
; x
++)
327 if (memcached_success(memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0)))
332 test_compare_warn_hint(global_count
, total
, "Possible false, positive, memcached may have ejected key/value based on memory needs");
337 test_return_t
delete_buffer_generate(memcached_st
*memc
)
339 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true);
342 for (size_t x
= 0; x
< global_count
; x
++)
344 if (memcached_success(memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0)))
349 test_compare_warn_hint(global_count
, total
, "Possible false, positive, memcached may have ejected key/value based on memory needs");
354 test_return_t
mget_read_internal_result(memcached_st
*memc
)
357 test_skip(true, bool(libmemcached_util_version_check(memc
, 1, 4, 4)));
359 test_compare(MEMCACHED_SUCCESS
,
360 memcached_mget(memc
, global_keys
, global_keys_length
, global_count
));
362 memcached_result_st
*results
= NULL
;
363 memcached_return_t rc
;
364 while ((results
= memcached_fetch_result(memc
, results
, &rc
)))
367 test_compare(MEMCACHED_SUCCESS
, rc
);
369 test_compare(MEMCACHED_END
, rc
);
371 memcached_result_free(results
);