1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
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.
38 #include <mem_config.h>
39 #include <libtest/test.hpp>
41 #if defined(HAVE_LIBUUID) && HAVE_LIBUUID
42 # include <uuid/uuid.h>
49 #include <libmemcached-1.0/memcached.h>
50 #include "libmemcached/is.h"
51 #include "libmemcached/server_instance.h"
53 #include <libhashkit-1.0/hashkit.h>
55 #include <libtest/memcached.hpp>
60 #include <semaphore.h>
64 #include <sys/types.h>
69 #include <libtest/server.h>
71 #include "clients/generator.h"
73 #define SMALL_STRING_LEN 1024
75 #include <libtest/test.hpp>
77 using namespace libtest
;
79 #include <libmemcachedutil-1.0/util.h>
81 #include "tests/hash_results.h"
83 #include "tests/libmemcached-1.0/callback_counter.h"
84 #include "tests/libmemcached-1.0/fetch_all_results.h"
85 #include "tests/libmemcached-1.0/mem_functions.h"
86 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
87 #include "tests/print.h"
88 #include "tests/debug.h"
89 #include "tests/memc.hpp"
91 #define UUID_STRING_MAXLENGTH 36
93 #include "tests/keys.hpp"
95 #include "libmemcached/instance.hpp"
97 static memcached_st
* create_single_instance_memcached(const memcached_st
*original_memc
, const char *options
)
100 If no options are given, copy over at least the binary flag.
102 char options_buffer
[1024]= { 0 };
105 if (memcached_is_binary(original_memc
))
107 snprintf(options_buffer
, sizeof(options_buffer
), "--BINARY");
112 * I only want to hit _one_ server so I know the number of requests I'm
113 * sending in the pipeline.
115 memcached_server_instance_st instance
= memcached_server_instance_by_position(original_memc
, 0);
117 char server_string
[1024];
118 int server_string_length
;
119 if (instance
->type
== MEMCACHED_CONNECTION_UNIX_SOCKET
)
123 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--SOCKET=\"%s\" %s",
124 memcached_server_name(instance
), options
);
128 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--SOCKET=\"%s\"",
129 memcached_server_name(instance
));
136 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--server=%s:%d %s",
137 memcached_server_name(instance
), int(memcached_server_port(instance
)),
142 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--server=%s:%d",
143 memcached_server_name(instance
), int(memcached_server_port(instance
)));
147 if (server_string_length
<= 0)
152 char errror_buffer
[1024];
153 if (memcached_failed(libmemcached_check_configuration(server_string
, server_string_length
, errror_buffer
, sizeof(errror_buffer
))))
155 Error
<< "Failed to parse (" << server_string
<< ") " << errror_buffer
;
159 return memcached(server_string
, server_string_length
);
163 test_return_t
init_test(memcached_st
*not_used
)
168 (void)memcached_create(&memc
);
169 memcached_free(&memc
);
174 #define TEST_PORT_COUNT 7
175 in_port_t test_ports
[TEST_PORT_COUNT
];
177 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
178 memcached_server_instance_st server
,
182 size_t bigger
= *((size_t *)(context
));
184 fatal_assert(bigger
<= memcached_server_port(server
));
185 *((size_t *)(context
))= memcached_server_port(server
);
187 return MEMCACHED_SUCCESS
;
190 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
191 memcached_server_instance_st instance
,
195 FILE *stream
= (FILE *)context
;
198 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
199 memcached_server_name(instance
),
200 memcached_server_port(instance
),
201 instance
->major_version
,
202 instance
->minor_version
,
203 instance
->micro_version
);
205 return MEMCACHED_SUCCESS
;
208 test_return_t
server_sort_test(memcached_st
*ptr
)
210 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
212 memcached_return_t rc
;
213 memcached_server_fn callbacks
[1];
214 memcached_st
*local_memc
;
217 local_memc
= memcached_create(NULL
);
218 test_true(local_memc
);
219 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
221 for (uint32_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
223 test_ports
[x
]= (in_port_t
)random() % 64000;
224 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
225 test_compare(memcached_server_count(local_memc
), x
+1);
227 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
229 test_compare(MEMCACHED_SUCCESS
, rc
);
232 callbacks
[0]= server_display_function
;
233 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
236 memcached_free(local_memc
);
241 test_return_t
server_sort2_test(memcached_st
*ptr
)
243 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
244 memcached_server_fn callbacks
[1];
245 memcached_st
*local_memc
;
246 memcached_server_instance_st instance
;
249 local_memc
= memcached_create(NULL
);
250 test_true(local_memc
);
251 test_compare(MEMCACHED_SUCCESS
,
252 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1));
254 test_compare(MEMCACHED_SUCCESS
,
255 memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0));
256 instance
= memcached_server_instance_by_position(local_memc
, 0);
257 test_compare(in_port_t(43043), memcached_server_port(instance
));
259 test_compare(MEMCACHED_SUCCESS
,
260 memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0));
262 instance
= memcached_server_instance_by_position(local_memc
, 0);
263 test_compare(in_port_t(43042), memcached_server_port(instance
));
265 instance
= memcached_server_instance_by_position(local_memc
, 1);
266 test_compare(in_port_t(43043), memcached_server_port(instance
));
268 callbacks
[0]= server_display_function
;
269 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
272 memcached_free(local_memc
);
277 test_return_t
memcached_server_remove_test(memcached_st
*)
279 const char *server_string
= "--server=localhost:4444 --server=localhost:4445 --server=localhost:4446 --server=localhost:4447 --server=localhost --server=memcache1.memcache.bk.sapo.pt:11211 --server=memcache1.memcache.bk.sapo.pt:11212 --server=memcache1.memcache.bk.sapo.pt:11213 --server=memcache1.memcache.bk.sapo.pt:11214 --server=memcache2.memcache.bk.sapo.pt:11211 --server=memcache2.memcache.bk.sapo.pt:11212 --server=memcache2.memcache.bk.sapo.pt:11213 --server=memcache2.memcache.bk.sapo.pt:11214";
282 test_compare(MEMCACHED_SUCCESS
,
283 libmemcached_check_configuration(server_string
, strlen(server_string
), buffer
, sizeof(buffer
)));
284 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
287 memcached_server_fn callbacks
[1];
288 callbacks
[0]= server_print_callback
;
289 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
291 memcached_free(memc
);
296 static memcached_return_t
server_display_unsort_function(const memcached_st
*,
297 memcached_server_instance_st server
,
301 uint32_t x
= *((uint32_t *)(context
));
303 if (! (test_ports
[x
] == memcached_server_port(server
)))
305 fprintf(stderr
, "%lu -> %lu\n", (unsigned long)test_ports
[x
], (unsigned long)memcached_server_port(server
));
306 return MEMCACHED_FAILURE
;
309 *((uint32_t *)(context
))= ++x
;
311 return MEMCACHED_SUCCESS
;
314 test_return_t
server_unsort_test(memcached_st
*ptr
)
316 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
317 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
318 memcached_server_fn callbacks
[1];
319 memcached_st
*local_memc
;
322 local_memc
= memcached_create(NULL
);
323 test_true(local_memc
);
325 for (uint32_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
327 test_ports
[x
]= (in_port_t
)(random() % 64000);
328 test_compare(MEMCACHED_SUCCESS
,
329 memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0));
330 test_compare(memcached_server_count(local_memc
), x
+1);
332 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
336 callbacks
[0]= server_display_unsort_function
;
337 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
339 /* Now we sort old data! */
340 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
341 callbacks
[0]= server_display_function
;
342 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
345 memcached_free(local_memc
);
350 test_return_t
allocation_test(memcached_st
*not_used
)
354 memc
= memcached_create(NULL
);
356 memcached_free(memc
);
361 test_return_t
clone_test(memcached_st
*memc
)
365 memcached_st
*memc_clone
;
366 memc_clone
= memcached_clone(NULL
, NULL
);
367 test_true(memc_clone
);
368 memcached_free(memc_clone
);
371 /* Can we init from null? */
373 memcached_st
*memc_clone
;
374 memc_clone
= memcached_clone(NULL
, memc
);
375 test_true(memc_clone
);
378 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
379 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
380 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
381 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
384 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
385 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
386 test_true(memc_clone
->distribution
== memc
->distribution
);
387 { // Test all of the flags
388 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
389 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
390 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
391 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
392 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
393 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
394 test_true(memc_clone
->ketama
.weighted_
== memc
->ketama
.weighted_
);
395 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
396 test_true(memc_clone
->flags
.hash_with_namespace
== memc
->flags
.hash_with_namespace
);
397 test_true(memc_clone
->flags
.reply
== memc
->flags
.reply
);
398 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
399 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
400 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
402 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
403 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
404 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
405 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
406 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
407 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
408 test_true(memc_clone
->on_clone
== memc
->on_clone
);
409 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
410 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
411 test_true(memc_clone
->recv_size
== memc
->recv_size
);
412 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
413 test_true(memc_clone
->send_size
== memc
->send_size
);
414 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
415 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
416 test_true(memc_clone
->user_data
== memc
->user_data
);
418 memcached_free(memc_clone
);
421 /* Can we init from struct? */
423 memcached_st declared_clone
;
424 memcached_st
*memc_clone
;
425 memset(&declared_clone
, 0 , sizeof(memcached_st
));
426 memc_clone
= memcached_clone(&declared_clone
, NULL
);
427 test_true(memc_clone
);
428 memcached_free(memc_clone
);
431 /* Can we init from struct? */
433 memcached_st declared_clone
;
434 memcached_st
*memc_clone
;
435 memset(&declared_clone
, 0 , sizeof(memcached_st
));
436 memc_clone
= memcached_clone(&declared_clone
, memc
);
437 test_true(memc_clone
);
438 memcached_free(memc_clone
);
444 test_return_t
userdata_test(memcached_st
*memc
)
447 test_false(memcached_set_user_data(memc
, foo
));
448 test_true(memcached_get_user_data(memc
) == foo
);
449 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
454 test_return_t
connection_test(memcached_st
*memc
)
456 test_compare(MEMCACHED_SUCCESS
,
457 memcached_server_add_with_weight(memc
, "localhost", 0, 0));
462 test_return_t
libmemcached_string_behavior_test(memcached_st
*)
464 for (int x
= MEMCACHED_BEHAVIOR_NO_BLOCK
; x
< int(MEMCACHED_BEHAVIOR_MAX
); ++x
)
466 test_true(libmemcached_string_behavior(memcached_behavior_t(x
)));
468 test_compare(37, int(MEMCACHED_BEHAVIOR_MAX
));
473 test_return_t
libmemcached_string_distribution_test(memcached_st
*)
475 for (int x
= MEMCACHED_DISTRIBUTION_MODULA
; x
< int(MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
); ++x
)
477 test_true(libmemcached_string_distribution(memcached_server_distribution_t(x
)));
479 test_compare(7, int(MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
));
484 test_return_t
memcached_return_t_TEST(memcached_st
*memc
)
486 uint32_t values
[] = { 851992627U, 2337886783U, 4109241422U, 4001849190U,
487 982370485U, 1263635348U, 4242906218U, 3829656100U,
488 1891735253U, 334139633U, 2257084983U, 3351789013U,
489 13199785U, 2542027183U, 1097051614U, 199566778U,
490 2748246961U, 2465192557U, 1664094137U, 2405439045U,
491 1842224848U, 692413798U, 3479807801U, 919913813U,
492 4269430871U, 610793021U, 527273862U, 1437122909U,
493 2300930706U, 2943759320U, 674306647U, 2400528935U,
494 54481931U, 4186304426U, 1741088401U, 2979625118U,
495 4159057246U, 3425930182U, 2593724503U, 1868899624U,
496 1769812374U, 2302537950U, 1110330676U, 3365377466U,
497 1336171666U, 3021258493U, 2334992265U, 3861994737U,
498 3582734124U, 3365377466U };
500 // You have updated the memcache_error messages but not updated docs/tests.
501 for (int rc
= int(MEMCACHED_SUCCESS
); rc
< int(MEMCACHED_MAXIMUM_RETURN
); ++rc
)
504 const char *msg
= memcached_strerror(memc
, memcached_return_t(rc
));
505 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
506 MEMCACHED_HASH_JENKINS
);
507 if (values
[rc
] != hash_val
)
509 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the memcached_return_t_TEST\n");
510 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, memcached_return_t(rc
)), hash_val
);
512 test_compare(values
[rc
], hash_val
);
514 test_compare(49, int(MEMCACHED_MAXIMUM_RETURN
));
519 test_return_t
set_test(memcached_st
*memc
)
521 memcached_return_t rc
= memcached_set(memc
,
522 test_literal_param("foo"),
523 test_literal_param("when we sanitize"),
524 time_t(0), (uint32_t)0);
525 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
530 test_return_t
append_test(memcached_st
*memc
)
532 memcached_return_t rc
;
533 const char *in_value
= "we";
537 test_compare(MEMCACHED_SUCCESS
,
538 memcached_flush(memc
, 0));
540 test_compare(MEMCACHED_SUCCESS
,
542 test_literal_param(__func__
),
543 in_value
, strlen(in_value
),
544 time_t(0), uint32_t(0)));
546 test_compare(MEMCACHED_SUCCESS
,
547 memcached_append(memc
,
548 test_literal_param(__func__
),
549 " the", strlen(" the"),
550 time_t(0), uint32_t(0)));
552 test_compare(MEMCACHED_SUCCESS
,
553 memcached_append(memc
,
554 test_literal_param(__func__
),
555 " people", strlen(" people"),
556 time_t(0), uint32_t(0)));
558 char *out_value
= memcached_get(memc
,
559 test_literal_param(__func__
),
560 &value_length
, &flags
, &rc
);
561 test_memcmp(out_value
, "we the people", strlen("we the people"));
562 test_compare(strlen("we the people"), value_length
);
563 test_compare(MEMCACHED_SUCCESS
, rc
);
569 test_return_t
append_binary_test(memcached_st
*memc
)
571 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
573 test_compare(MEMCACHED_SUCCESS
,
574 memcached_flush(memc
, 0));
576 test_compare(MEMCACHED_SUCCESS
,
578 test_literal_param(__func__
),
580 time_t(0), uint32_t(0)));
583 for (uint32_t x
= 0; store_list
[x
] ; x
++)
585 test_compare(MEMCACHED_SUCCESS
,
586 memcached_append(memc
,
587 test_literal_param(__func__
),
588 (char *)&store_list
[x
], sizeof(uint32_t),
589 time_t(0), uint32_t(0)));
595 memcached_return_t rc
;
596 uint32_t *value
= (uint32_t *)memcached_get(memc
,
597 test_literal_param(__func__
),
598 &value_length
, &flags
, &rc
);
599 test_compare(value_length
, sizeof(uint32_t) * count
);
600 test_compare(MEMCACHED_SUCCESS
, rc
);
602 for (uint32_t counter
= uint32_t(count
), *ptr
= value
; counter
; counter
--)
604 test_compare(*ptr
, store_list
[count
- counter
]);
612 test_return_t
memcached_mget_mixed_memcached_get_TEST(memcached_st
*memc
)
616 for (libtest::vchar_ptr_t::iterator iter
= keys
.begin();
620 test_compare_hint(MEMCACHED_SUCCESS
,
624 time_t(0), uint32_t(0)),
625 memcached_last_error_message(memc
));
628 for (ptrdiff_t loop
= 0; loop
< 20; loop
++)
632 test_compare(MEMCACHED_SUCCESS
,
633 memcached_mget(memc
, keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
635 memcached_result_st
*results
= memcached_result_create(memc
, NULL
);
638 size_t result_count
= 0;
639 memcached_return_t rc
;
640 while (memcached_fetch_result(memc
, results
, &rc
))
644 test_true(keys
.size() >= result_count
);
648 int which_key
= random() % int(keys
.size());
651 memcached_return_t rc
;
652 char *out_value
= memcached_get(memc
, keys
.key_at(which_key
), keys
.length_at(which_key
),
653 &value_length
, &flags
, &rc
);
654 if (rc
== MEMCACHED_NOTFOUND
)
655 { } // It is possible that the value has been purged.
658 test_compare(MEMCACHED_SUCCESS
, rc
);
660 test_null(out_value
);
661 test_zero(value_length
);
669 test_return_t
cas2_test(memcached_st
*memc
)
671 const char *keys
[]= {"fudge", "son", "food"};
672 size_t key_length
[]= {5, 3, 4};
673 const char *value
= "we the people";
674 size_t value_length
= strlen("we the people");
676 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0));
678 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
680 for (uint32_t x
= 0; x
< 3; x
++)
682 test_compare(MEMCACHED_SUCCESS
,
683 memcached_set(memc
, keys
[x
], key_length
[x
],
684 keys
[x
], key_length
[x
],
685 time_t(50), uint32_t(9)));
688 test_compare(MEMCACHED_SUCCESS
,
689 memcached_mget(memc
, keys
, key_length
, 3));
691 memcached_result_st
*results
= memcached_result_create(memc
, NULL
);
694 memcached_return_t rc
;
695 results
= memcached_fetch_result(memc
, results
, &rc
);
697 test_true(results
->item_cas
);
698 test_compare(MEMCACHED_SUCCESS
, rc
);
699 test_true(memcached_result_cas(results
));
701 test_memcmp(value
, "we the people", strlen("we the people"));
702 test_compare(strlen("we the people"), value_length
);
703 test_compare(MEMCACHED_SUCCESS
, rc
);
705 memcached_result_free(results
);
710 test_return_t
cas_test(memcached_st
*memc
)
712 const char* keys
[2] = { __func__
, NULL
};
713 size_t keylengths
[2] = { strlen(__func__
), 0 };
715 memcached_result_st results_obj
;
717 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0));
719 test_skip(true, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
721 test_compare(MEMCACHED_SUCCESS
,
723 test_literal_param(__func__
),
724 test_literal_param("we the people"),
725 (time_t)0, (uint32_t)0));
727 test_compare(MEMCACHED_SUCCESS
,
728 memcached_mget(memc
, keys
, keylengths
, 1));
730 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
733 memcached_return_t rc
;
734 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
736 test_compare(MEMCACHED_SUCCESS
, rc
);
737 test_true(memcached_result_cas(results
));
738 test_memcmp("we the people", memcached_result_value(results
), test_literal_param_size("we the people"));
739 test_compare(test_literal_param_size("we the people"),
740 strlen(memcached_result_value(results
)));
742 uint64_t cas
= memcached_result_cas(results
);
745 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
746 test_true(rc
== MEMCACHED_END
);
747 test_true(results
== NULL
);
750 test_compare(MEMCACHED_SUCCESS
,
752 test_literal_param(__func__
),
753 test_literal_param("change the value"),
757 * The item will have a new cas value, so try to set it again with the old
758 * value. This should fail!
760 test_compare(MEMCACHED_DATA_EXISTS
,
762 test_literal_param(__func__
),
763 test_literal_param("change the value"),
766 memcached_result_free(&results_obj
);
772 test_return_t
prepend_test(memcached_st
*memc
)
774 const char *key
= "fig";
775 const char *value
= "people";
777 test_compare(MEMCACHED_SUCCESS
,
778 memcached_flush(memc
, 0));
780 test_compare(MEMCACHED_SUCCESS
,
781 memcached_set(memc
, key
, strlen(key
),
782 value
, strlen(value
),
783 time_t(0), uint32_t(0)));
785 test_compare(MEMCACHED_SUCCESS
,
786 memcached_prepend(memc
, key
, strlen(key
),
787 "the ", strlen("the "),
788 time_t(0), uint32_t(0)));
790 test_compare(MEMCACHED_SUCCESS
,
791 memcached_prepend(memc
, key
, strlen(key
),
792 "we ", strlen("we "),
793 time_t(0), uint32_t(0)));
797 memcached_return_t rc
;
798 char *out_value
= memcached_get(memc
, key
, strlen(key
),
799 &value_length
, &flags
, &rc
);
800 test_memcmp(out_value
, "we the people", strlen("we the people"));
801 test_compare(strlen("we the people"), value_length
);
802 test_compare(MEMCACHED_SUCCESS
, rc
);
809 Set the value, then quit to make sure it is flushed.
810 Come back in and test that add fails.
812 test_return_t
memcached_add_SUCCESS_TEST(memcached_st
*memc
)
814 memcached_return_t rc
;
815 test_null(memcached_get(memc
, test_literal_param(__func__
), NULL
, NULL
, &rc
));
816 test_compare(MEMCACHED_NOTFOUND
, rc
);
818 test_compare(MEMCACHED_SUCCESS
,
820 test_literal_param(__func__
),
821 test_literal_param("try something else"),
822 time_t(0), uint32_t(0)));
827 test_return_t
regression_1067242_TEST(memcached_st
*memc
)
829 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
,
830 test_literal_param(__func__
),
831 test_literal_param("-2"),
834 memcached_return_t rc
;
836 test_true((value
= memcached_get(memc
, test_literal_param(__func__
), NULL
, NULL
, &rc
)));
837 test_compare(MEMCACHED_SUCCESS
, rc
);
840 for (size_t x
= 0; x
< 10; x
++)
843 test_compare(MEMCACHED_CLIENT_ERROR
,
844 memcached_increment(memc
,
845 test_literal_param(__func__
), 1, &new_number
));
846 test_compare(MEMCACHED_CLIENT_ERROR
, memcached_last_error(memc
));
847 test_true((value
= memcached_get(memc
, test_literal_param(__func__
), NULL
, NULL
, &rc
)));
848 test_compare(MEMCACHED_SUCCESS
, rc
);
856 Set the value, then quit to make sure it is flushed.
857 Come back in and test that add fails.
859 test_return_t
add_test(memcached_st
*memc
)
861 test_compare(return_value_based_on_buffering(memc
),
863 test_literal_param(__func__
),
864 test_literal_param("when we sanitize"),
865 time_t(0), uint32_t(0)));
867 memcached_quit(memc
);
871 memcached_return_t rc
;
872 char *check_value
= memcached_get(memc
,
873 test_literal_param(__func__
),
874 &value_length
, &flags
, &rc
);
875 test_memcmp(check_value
, "when we sanitize", strlen("when we sanitize"));
876 test_compare(test_literal_param_size("when we sanitize"), value_length
);
877 test_compare(MEMCACHED_SUCCESS
, rc
);
880 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_DATA_EXISTS
: MEMCACHED_NOTSTORED
,
882 test_literal_param(__func__
),
883 test_literal_param("try something else"),
884 time_t(0), uint32_t(0)));
890 ** There was a problem of leaking filedescriptors in the initial release
891 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
892 ** systems it seems that the kernel is slow on reclaiming the resources
893 ** because the connects starts to time out (the test doesn't do much
894 ** anyway, so just loop 10 iterations)
896 test_return_t
add_wrapper(memcached_st
*memc
)
898 unsigned int max
= 10000;
906 for (uint32_t x
= 0; x
< max
; x
++)
914 test_return_t
replace_test(memcached_st
*memc
)
916 test_compare(return_value_based_on_buffering(memc
),
918 test_literal_param(__func__
),
919 test_literal_param("when we sanitize"),
920 time_t(0), uint32_t(0)));
922 test_compare(MEMCACHED_SUCCESS
,
923 memcached_replace(memc
,
924 test_literal_param(__func__
),
925 test_literal_param("first we insert some data"),
926 time_t(0), uint32_t(0)));
931 test_return_t
delete_test(memcached_st
*memc
)
933 test_compare(return_value_based_on_buffering(memc
),
935 test_literal_param(__func__
),
936 test_literal_param("when we sanitize"),
937 time_t(0), uint32_t(0)));
939 test_compare(return_value_based_on_buffering(memc
),
940 memcached_delete(memc
,
941 test_literal_param(__func__
),
947 test_return_t
flush_test(memcached_st
*memc
)
949 uint64_t query_id
= memcached_query_id(memc
);
950 test_compare(MEMCACHED_SUCCESS
,
951 memcached_flush(memc
, 0));
952 test_compare(query_id
+1, memcached_query_id(memc
));
957 static memcached_return_t
server_function(const memcached_st
*,
958 memcached_server_instance_st
,
962 return MEMCACHED_SUCCESS
;
965 test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
968 strncpy(context
, "foo bad", sizeof(context
));
969 memcached_server_fn callbacks
[1];
971 callbacks
[0]= server_function
;
972 memcached_server_cursor(memc
, callbacks
, context
, 1);
976 test_return_t
bad_key_test(memcached_st
*memc
)
978 memcached_return_t rc
;
979 const char *key
= "foo bad";
982 uint64_t query_id
= memcached_query_id(memc
);
984 // Just skip if we are in binary mode.
985 test_skip(false, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
987 test_compare(query_id
, memcached_query_id(memc
)); // We should not increase the query_id for memcached_behavior_get()
989 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
990 test_true(memc_clone
);
992 query_id
= memcached_query_id(memc_clone
);
993 test_compare(MEMCACHED_SUCCESS
,
994 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, true));
995 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
997 /* All keys are valid in the binary protocol (except for length) */
998 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == false)
1000 uint64_t before_query_id
= memcached_query_id(memc_clone
);
1002 size_t string_length
;
1003 char *string
= memcached_get(memc_clone
, key
, strlen(key
),
1004 &string_length
, &flags
, &rc
);
1005 test_compare(MEMCACHED_BAD_KEY_PROVIDED
, rc
);
1006 test_zero(string_length
);
1009 test_compare(before_query_id
+1, memcached_query_id(memc_clone
));
1011 query_id
= memcached_query_id(memc_clone
);
1012 test_compare(MEMCACHED_SUCCESS
,
1013 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, false));
1014 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
1016 size_t string_length
;
1017 char *string
= memcached_get(memc_clone
, key
, strlen(key
),
1018 &string_length
, &flags
, &rc
);
1019 test_compare(MEMCACHED_NOTFOUND
, rc
);
1020 test_zero(string_length
);
1024 /* Test multi key for bad keys */
1025 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
1026 size_t key_lengths
[] = { 7, 7, 7 };
1027 query_id
= memcached_query_id(memc_clone
);
1028 test_compare(MEMCACHED_SUCCESS
,
1029 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, true));
1030 test_compare(query_id
, memcached_query_id(memc_clone
));
1032 query_id
= memcached_query_id(memc_clone
);
1033 test_compare(MEMCACHED_BAD_KEY_PROVIDED
,
1034 memcached_mget(memc_clone
, keys
, key_lengths
, 3));
1035 test_compare(query_id
+1, memcached_query_id(memc_clone
));
1037 query_id
= memcached_query_id(memc_clone
);
1038 // Grouping keys are not required to follow normal key behaviors
1039 test_compare(MEMCACHED_SUCCESS
,
1040 memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1));
1041 test_compare(query_id
+1, memcached_query_id(memc_clone
));
1043 /* The following test should be moved to the end of this function when the
1044 memcached server is updated to allow max size length of the keys in the
1047 test_compare(MEMCACHED_SUCCESS
,
1048 memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_NAMESPACE
, NULL
));
1050 libtest::vchar_t longkey
;
1052 libtest::vchar_t::iterator it
= longkey
.begin();
1053 longkey
.insert(it
, MEMCACHED_MAX_KEY
, 'a');
1056 test_compare(longkey
.size(), size_t(MEMCACHED_MAX_KEY
));
1058 size_t string_length
;
1060 test_null(memcached_get(memc_clone
, &longkey
[0], longkey
.size() -1, &string_length
, &flags
, &rc
));
1061 test_compare(MEMCACHED_NOTFOUND
, rc
);
1062 test_zero(string_length
);
1064 test_null(memcached_get(memc_clone
, &longkey
[0], longkey
.size(), &string_length
, &flags
, &rc
));
1065 test_compare(MEMCACHED_BAD_KEY_PROVIDED
, rc
);
1066 test_zero(string_length
);
1070 /* Make sure zero length keys are marked as bad */
1072 test_compare(MEMCACHED_SUCCESS
,
1073 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, true));
1074 size_t string_length
;
1075 char *string
= memcached_get(memc_clone
, key
, 0,
1076 &string_length
, &flags
, &rc
);
1077 test_compare(MEMCACHED_BAD_KEY_PROVIDED
, rc
);
1078 test_zero(string_length
);
1082 memcached_free(memc_clone
);
1084 return TEST_SUCCESS
;
1087 #define READ_THROUGH_VALUE "set for me"
1088 static memcached_return_t
read_through_trigger(memcached_st
*, // memc
1090 size_t, // key_length,
1091 memcached_result_st
*result
)
1093 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
1096 #ifndef __INTEL_COMPILER
1097 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
1100 test_return_t
read_through(memcached_st
*memc
)
1102 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
1104 size_t string_length
;
1106 memcached_return_t rc
;
1107 char *string
= memcached_get(memc
,
1108 test_literal_param(__func__
),
1109 &string_length
, &flags
, &rc
);
1111 test_compare(MEMCACHED_NOTFOUND
, rc
);
1112 test_false(string_length
);
1115 test_compare(MEMCACHED_SUCCESS
,
1116 memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, *(void **)&cb
));
1118 string
= memcached_get(memc
,
1119 test_literal_param(__func__
),
1120 &string_length
, &flags
, &rc
);
1122 test_compare(MEMCACHED_SUCCESS
, rc
);
1123 test_compare(sizeof(READ_THROUGH_VALUE
) -1, string_length
);
1124 test_compare(0, string
[sizeof(READ_THROUGH_VALUE
) -1]);
1125 test_strcmp(READ_THROUGH_VALUE
, string
);
1128 string
= memcached_get(memc
,
1129 test_literal_param(__func__
),
1130 &string_length
, &flags
, &rc
);
1132 test_compare(MEMCACHED_SUCCESS
, rc
);
1134 test_compare(string_length
, sizeof(READ_THROUGH_VALUE
) -1);
1135 test_true(string
[sizeof(READ_THROUGH_VALUE
) -1] == 0);
1136 test_strcmp(READ_THROUGH_VALUE
, string
);
1139 return TEST_SUCCESS
;
1142 test_return_t
set_test2(memcached_st
*memc
)
1144 for (uint32_t x
= 0; x
< 10; x
++)
1146 test_compare(return_value_based_on_buffering(memc
),
1148 test_literal_param("foo"),
1149 test_literal_param("train in the brain"),
1150 time_t(0), uint32_t(0)));
1153 return TEST_SUCCESS
;
1156 test_return_t
set_test3(memcached_st
*memc
)
1158 size_t value_length
= 8191;
1160 libtest::vchar_t value
;
1161 value
.reserve(value_length
);
1162 for (uint32_t x
= 0; x
< value_length
; x
++)
1164 value
.push_back(char(x
% 127));
1167 /* The dump test relies on there being at least 32 items in memcached */
1168 for (uint32_t x
= 0; x
< 32; x
++)
1172 snprintf(key
, sizeof(key
), "foo%u", x
);
1174 uint64_t query_id
= memcached_query_id(memc
);
1175 test_compare(return_value_based_on_buffering(memc
),
1176 memcached_set(memc
, key
, strlen(key
),
1177 &value
[0], value
.size(),
1178 time_t(0), uint32_t(0)));
1179 test_compare(query_id
+1, memcached_query_id(memc
));
1182 return TEST_SUCCESS
;
1185 test_return_t
mget_end(memcached_st
*memc
)
1187 const char *keys
[]= { "foo", "foo2" };
1188 size_t lengths
[]= { 3, 4 };
1189 const char *values
[]= { "fjord", "41" };
1192 for (size_t x
= 0; x
< test_array_length(keys
); x
++)
1194 test_compare(MEMCACHED_SUCCESS
,
1196 keys
[x
], lengths
[x
],
1197 values
[x
], strlen(values
[x
]),
1198 time_t(0), uint32_t(0)));
1202 size_t string_length
;
1205 // retrieve both via mget
1206 test_compare(MEMCACHED_SUCCESS
,
1207 memcached_mget(memc
,
1209 test_array_length(keys
)));
1211 char key
[MEMCACHED_MAX_KEY
];
1213 memcached_return_t rc
;
1215 // this should get both
1216 for (size_t x
= 0; x
< test_array_length(keys
); x
++)
1218 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1220 test_compare(MEMCACHED_SUCCESS
, rc
);
1222 if (key_length
== 4)
1227 test_compare(string_length
, strlen(values
[val
]));
1228 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1232 // this should indicate end
1233 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1234 test_compare(MEMCACHED_END
, rc
);
1238 test_compare(MEMCACHED_SUCCESS
,
1239 memcached_mget(memc
, keys
, lengths
, 1));
1241 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1242 test_compare(key_length
, lengths
[0]);
1243 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1244 test_compare(string_length
, strlen(values
[0]));
1245 test_true(strncmp(values
[0], string
, string_length
) == 0);
1246 test_compare(MEMCACHED_SUCCESS
, rc
);
1249 // this should indicate end
1250 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1251 test_compare(MEMCACHED_END
, rc
);
1254 return TEST_SUCCESS
;
1257 /* Do not copy the style of this code, I just access hosts to testthis function */
1258 test_return_t
stats_servername_test(memcached_st
*memc
)
1260 memcached_stat_st memc_stat
;
1261 memcached_server_instance_st instance
=
1262 memcached_server_instance_by_position(memc
, 0);
1264 if (LIBMEMCACHED_WITH_SASL_SUPPORT
and memcached_get_sasl_callbacks(memc
))
1266 return TEST_SKIPPED
;
1269 test_compare(MEMCACHED_SUCCESS
, memcached_stat_servername(&memc_stat
, NULL
,
1270 memcached_server_name(instance
),
1271 memcached_server_port(instance
)));
1273 return TEST_SUCCESS
;
1276 test_return_t
increment_test(memcached_st
*memc
)
1278 uint64_t new_number
;
1280 test_compare(MEMCACHED_SUCCESS
,
1282 test_literal_param("number"),
1283 test_literal_param("0"),
1284 (time_t)0, (uint32_t)0));
1286 test_compare(MEMCACHED_SUCCESS
,
1287 memcached_increment(memc
, test_literal_param("number"), 1, &new_number
));
1288 test_compare(uint64_t(1), new_number
);
1290 test_compare(MEMCACHED_SUCCESS
,
1291 memcached_increment(memc
, test_literal_param("number"), 1, &new_number
));
1292 test_compare(uint64_t(2), new_number
);
1294 return TEST_SUCCESS
;
1297 test_return_t
increment_with_initial_test(memcached_st
*memc
)
1299 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1301 uint64_t new_number
;
1302 uint64_t initial
= 0;
1304 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1306 test_compare(MEMCACHED_SUCCESS
,
1307 memcached_increment_with_initial(memc
, test_literal_param("number"), 1, initial
, 0, &new_number
));
1308 test_compare(new_number
, initial
);
1310 test_compare(MEMCACHED_SUCCESS
,
1311 memcached_increment_with_initial(memc
, test_literal_param("number"), 1, initial
, 0, &new_number
));
1312 test_compare(new_number
, (initial
+1));
1314 return TEST_SUCCESS
;
1317 test_return_t
decrement_test(memcached_st
*memc
)
1319 test_compare(return_value_based_on_buffering(memc
),
1321 test_literal_param(__func__
),
1322 test_literal_param("3"),
1323 time_t(0), uint32_t(0)));
1325 // Make sure we flush the value we just set
1326 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1328 uint64_t new_number
;
1329 test_compare(MEMCACHED_SUCCESS
,
1330 memcached_decrement(memc
,
1331 test_literal_param(__func__
),
1333 test_compare(uint64_t(2), new_number
);
1335 test_compare(MEMCACHED_SUCCESS
,
1336 memcached_decrement(memc
,
1337 test_literal_param(__func__
),
1339 test_compare(uint64_t(1), new_number
);
1341 return TEST_SUCCESS
;
1344 test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1346 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1348 uint64_t initial
= 3;
1350 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1352 uint64_t new_number
;
1353 test_compare(MEMCACHED_SUCCESS
,
1354 memcached_decrement_with_initial(memc
,
1355 test_literal_param(__func__
),
1358 test_compare(new_number
, initial
);
1360 test_compare(MEMCACHED_SUCCESS
,
1361 memcached_decrement_with_initial(memc
,
1362 test_literal_param(__func__
),
1365 test_compare(new_number
, (initial
- 1));
1367 return TEST_SUCCESS
;
1370 test_return_t
increment_by_key_test(memcached_st
*memc
)
1372 const char *master_key
= "foo";
1373 const char *key
= "number";
1374 const char *value
= "0";
1376 test_compare(return_value_based_on_buffering(memc
),
1377 memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1379 value
, strlen(value
),
1380 time_t(0), uint32_t(0)));
1382 // Make sure we flush the value we just set
1383 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1385 uint64_t new_number
;
1386 test_compare(MEMCACHED_SUCCESS
,
1387 memcached_increment_by_key(memc
, master_key
, strlen(master_key
),
1388 key
, strlen(key
), 1, &new_number
));
1389 test_compare(uint64_t(1), new_number
);
1391 test_compare(MEMCACHED_SUCCESS
,
1392 memcached_increment_by_key(memc
, master_key
, strlen(master_key
),
1393 key
, strlen(key
), 1, &new_number
));
1394 test_compare(uint64_t(2), new_number
);
1396 return TEST_SUCCESS
;
1399 test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1401 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1403 uint64_t new_number
;
1404 const char *master_key
= "foo";
1405 const char *key
= "number";
1406 uint64_t initial
= 0;
1408 test_compare(MEMCACHED_SUCCESS
,
1409 memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1411 1, initial
, 0, &new_number
));
1412 test_compare(new_number
, initial
);
1414 test_compare(MEMCACHED_SUCCESS
,
1415 memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1417 1, initial
, 0, &new_number
));
1418 test_compare(new_number
, (initial
+1));
1420 return TEST_SUCCESS
;
1423 test_return_t
decrement_by_key_test(memcached_st
*memc
)
1425 uint64_t new_number
;
1426 const char *value
= "3";
1428 test_compare(return_value_based_on_buffering(memc
),
1429 memcached_set_by_key(memc
,
1430 test_literal_param("foo"),
1431 test_literal_param("number"),
1432 value
, strlen(value
),
1433 (time_t)0, (uint32_t)0));
1435 test_compare(MEMCACHED_SUCCESS
,
1436 memcached_decrement_by_key(memc
,
1437 test_literal_param("foo"),
1438 test_literal_param("number"),
1440 test_compare(uint64_t(2), new_number
);
1442 test_compare(MEMCACHED_SUCCESS
,
1443 memcached_decrement_by_key(memc
,
1444 test_literal_param("foo"),
1445 test_literal_param("number"),
1447 test_compare(uint64_t(1), new_number
);
1449 return TEST_SUCCESS
;
1452 test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1454 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1456 uint64_t new_number
;
1457 uint64_t initial
= 3;
1459 test_compare(MEMCACHED_SUCCESS
,
1460 memcached_decrement_with_initial_by_key(memc
,
1461 test_literal_param("foo"),
1462 test_literal_param("number"),
1463 1, initial
, 0, &new_number
));
1464 test_compare(new_number
, initial
);
1466 test_compare(MEMCACHED_SUCCESS
,
1467 memcached_decrement_with_initial_by_key(memc
,
1468 test_literal_param("foo"),
1469 test_literal_param("number"),
1470 1, initial
, 0, &new_number
));
1471 test_compare(new_number
, (initial
- 1));
1473 return TEST_SUCCESS
;
1475 test_return_t
binary_increment_with_prefix_test(memcached_st
*memc
)
1477 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1479 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)"namespace:"));
1481 test_compare(return_value_based_on_buffering(memc
),
1483 test_literal_param("number"),
1484 test_literal_param("0"),
1485 (time_t)0, (uint32_t)0));
1487 uint64_t new_number
;
1488 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
,
1489 test_literal_param("number"),
1491 test_compare(uint64_t(1), new_number
);
1493 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
,
1494 test_literal_param("number"),
1496 test_compare(uint64_t(2), new_number
);
1498 return TEST_SUCCESS
;
1501 test_return_t
quit_test(memcached_st
*memc
)
1503 const char *value
= "sanford and sun";
1505 test_compare(return_value_based_on_buffering(memc
),
1507 test_literal_param(__func__
),
1508 value
, strlen(value
),
1509 time_t(10), uint32_t(3)));
1510 memcached_quit(memc
);
1512 test_compare(return_value_based_on_buffering(memc
),
1514 test_literal_param(__func__
),
1515 value
, strlen(value
),
1516 time_t(50), uint32_t(9)));
1518 return TEST_SUCCESS
;
1521 test_return_t
mget_result_test(memcached_st
*memc
)
1523 const char *keys
[]= {"fudge", "son", "food"};
1524 size_t key_length
[]= {5, 3, 4};
1526 memcached_result_st results_obj
;
1527 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
1529 test_true(&results_obj
== results
);
1531 /* We need to empty the server before continueing test */
1532 test_compare(MEMCACHED_SUCCESS
,
1533 memcached_flush(memc
, 0));
1535 test_compare(MEMCACHED_SUCCESS
,
1536 memcached_mget(memc
, keys
, key_length
, 3));
1538 memcached_return_t rc
;
1539 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1544 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
))) { test_true(false); /* We should never see a value returned */ };
1545 test_false(results
);
1546 test_compare(MEMCACHED_NOTFOUND
, rc
);
1548 for (uint32_t x
= 0; x
< 3; x
++)
1550 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1551 keys
[x
], key_length
[x
],
1552 (time_t)50, (uint32_t)9);
1553 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
1556 test_compare(MEMCACHED_SUCCESS
,
1557 memcached_mget(memc
, keys
, key_length
, 3));
1559 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1562 test_true(&results_obj
== results
);
1563 test_compare(MEMCACHED_SUCCESS
, rc
);
1564 test_memcmp(memcached_result_key_value(results
),
1565 memcached_result_value(results
),
1566 memcached_result_length(results
));
1567 test_compare(memcached_result_key_length(results
), memcached_result_length(results
));
1570 memcached_result_free(&results_obj
);
1572 return TEST_SUCCESS
;
1575 test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1577 const char *keys
[]= {"fudge", "son", "food"};
1578 size_t key_length
[]= {5, 3, 4};
1580 memcached_result_st
*results
;
1582 /* We need to empty the server before continueing test */
1583 test_compare(MEMCACHED_SUCCESS
,
1584 memcached_flush(memc
, 0));
1586 test_compare(MEMCACHED_SUCCESS
,
1587 memcached_mget(memc
, keys
, key_length
, 3));
1589 memcached_return_t rc
;
1590 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1594 test_false(results
);
1595 test_compare(MEMCACHED_NOTFOUND
, rc
);
1597 for (uint32_t x
= 0; x
< 3; x
++)
1599 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1600 keys
[x
], key_length
[x
],
1601 (time_t)50, (uint32_t)9);
1602 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
1605 test_compare(MEMCACHED_SUCCESS
,
1606 memcached_mget(memc
, keys
, key_length
, 3));
1609 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1612 test_compare(MEMCACHED_SUCCESS
, rc
);
1613 test_compare(memcached_result_key_length(results
), memcached_result_length(results
));
1614 test_memcmp(memcached_result_key_value(results
),
1615 memcached_result_value(results
),
1616 memcached_result_length(results
));
1617 memcached_result_free(results
);
1621 return TEST_SUCCESS
;
1624 test_return_t
mget_result_function(memcached_st
*memc
)
1626 const char *keys
[]= {"fudge", "son", "food"};
1627 size_t key_length
[]= {5, 3, 4};
1629 memcached_execute_fn callbacks
[1];
1631 for (uint32_t x
= 0; x
< 3; x
++)
1633 test_compare(return_value_based_on_buffering(memc
),
1634 memcached_set(memc
, keys
[x
], key_length
[x
],
1635 keys
[x
], key_length
[x
],
1636 time_t(50), uint32_t(9)));
1638 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1639 memcached_quit(memc
);
1641 test_compare(MEMCACHED_SUCCESS
,
1642 memcached_mget(memc
, keys
, key_length
, 3));
1644 callbacks
[0]= &callback_counter
;
1647 test_compare(MEMCACHED_SUCCESS
,
1648 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
1650 test_compare(size_t(3), counter
);
1652 return TEST_SUCCESS
;
1655 test_return_t
mget_test(memcached_st
*memc
)
1657 const char *keys
[]= {"fudge", "son", "food"};
1658 size_t key_length
[]= {5, 3, 4};
1660 char return_key
[MEMCACHED_MAX_KEY
];
1661 size_t return_key_length
;
1663 size_t return_value_length
;
1665 test_compare(MEMCACHED_SUCCESS
,
1666 memcached_mget(memc
, keys
, key_length
, 3));
1669 memcached_return_t rc
;
1670 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1671 &return_value_length
, &flags
, &rc
)))
1673 test_true(return_value
);
1675 test_false(return_value
);
1676 test_zero(return_value_length
);
1677 test_compare(MEMCACHED_NOTFOUND
, rc
);
1679 for (uint32_t x
= 0; x
< 3; x
++)
1681 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1682 keys
[x
], key_length
[x
],
1683 (time_t)50, (uint32_t)9);
1684 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
1686 test_compare(MEMCACHED_SUCCESS
,
1687 memcached_mget(memc
, keys
, key_length
, 3));
1690 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1691 &return_value_length
, &flags
, &rc
)))
1693 test_true(return_value
);
1694 test_compare(MEMCACHED_SUCCESS
, rc
);
1695 if (not memc
->_namespace
)
1697 test_compare(return_key_length
, return_value_length
);
1698 test_memcmp(return_value
, return_key
, return_value_length
);
1704 return TEST_SUCCESS
;
1707 test_return_t
mget_execute(memcached_st
*original_memc
)
1709 test_skip(true, memcached_behavior_get(original_memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1711 memcached_st
*memc
= create_single_instance_memcached(original_memc
, "--BINARY-PROTOCOL");
1714 keys_st
keys(20480);
1716 /* First add all of the items.. */
1717 char blob
[1024] = {0};
1719 for (size_t x
= 0; x
< keys
.size(); ++x
)
1721 uint64_t query_id
= memcached_query_id(memc
);
1722 memcached_return_t rc
= memcached_add(memc
,
1723 keys
.key_at(x
), keys
.length_at(x
),
1726 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
1727 test_compare(query_id
+1, memcached_query_id(memc
));
1730 /* Try to get all of them with a large multiget */
1732 memcached_execute_fn callbacks
[]= { &callback_counter
};
1733 test_compare(MEMCACHED_SUCCESS
,
1734 memcached_mget_execute(memc
,
1735 keys
.keys_ptr(), keys
.lengths_ptr(),
1736 keys
.size(), callbacks
, &counter
, 1));
1739 uint64_t query_id
= memcached_query_id(memc
);
1740 test_compare(MEMCACHED_SUCCESS
,
1741 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
1742 test_compare(query_id
, memcached_query_id(memc
));
1744 /* Verify that we got all of the items */
1745 test_compare(keys
.size(), counter
);
1748 memcached_free(memc
);
1750 return TEST_SUCCESS
;
1753 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1754 static pairs_st
*global_pairs
= NULL
;
1756 test_return_t
key_setup(memcached_st
*memc
)
1758 test_skip(TEST_SUCCESS
, pre_binary(memc
));
1760 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1762 return TEST_SUCCESS
;
1765 test_return_t
key_teardown(memcached_st
*)
1767 pairs_free(global_pairs
);
1770 return TEST_SUCCESS
;
1773 test_return_t
block_add_regression(memcached_st
*memc
)
1775 /* First add all of the items.. */
1776 for (ptrdiff_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1778 libtest::vchar_t blob
;
1779 libtest::vchar::make(blob
, 1024);
1781 memcached_return_t rc
= memcached_add_by_key(memc
,
1782 test_literal_param("bob"),
1783 global_pairs
[x
].key
, global_pairs
[x
].key_length
,
1784 &blob
[0], blob
.size(),
1785 time_t(0), uint32_t(0));
1786 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
1788 Error
<< memcached_last_error_message(memc
);
1789 return TEST_SKIPPED
;
1791 test_compare(*memc
, MEMCACHED_SUCCESS
);
1792 test_compare(rc
, MEMCACHED_SUCCESS
);
1795 return TEST_SUCCESS
;
1798 test_return_t
binary_add_regression(memcached_st
*memc
)
1800 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
1801 return block_add_regression(memc
);
1804 test_return_t
get_stats_keys(memcached_st
*memc
)
1808 memcached_stat_st memc_stat
;
1809 memcached_return_t rc
;
1811 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1812 test_compare(MEMCACHED_SUCCESS
, rc
);
1813 for (ptr
= stat_list
; *ptr
; ptr
++)
1818 return TEST_SUCCESS
;
1821 test_return_t
version_string_test(memcached_st
*)
1823 test_strcmp(LIBMEMCACHED_VERSION_STRING
, memcached_lib_version());
1825 return TEST_SUCCESS
;
1828 test_return_t
get_stats(memcached_st
*memc
)
1830 memcached_return_t rc
;
1832 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1833 test_compare(MEMCACHED_SUCCESS
, rc
);
1834 test_true(memc_stat
);
1836 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1838 char **stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1839 test_compare(MEMCACHED_SUCCESS
, rc
);
1840 for (char **ptr
= stat_list
; *ptr
; ptr
++) {};
1845 memcached_stat_free(NULL
, memc_stat
);
1847 return TEST_SUCCESS
;
1850 test_return_t
add_host_test(memcached_st
*memc
)
1852 char servername
[]= "0.example.com";
1854 memcached_return_t rc
;
1855 memcached_server_st
*servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1856 test_compare(1U, memcached_server_list_count(servers
));
1858 for (unsigned int x
= 2; x
< 20; x
++)
1860 char buffer
[SMALL_STRING_LEN
];
1862 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1863 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1865 test_compare(MEMCACHED_SUCCESS
, rc
);
1866 test_compare(x
, memcached_server_list_count(servers
));
1869 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
1870 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
1872 memcached_server_list_free(servers
);
1874 return TEST_SUCCESS
;
1877 test_return_t
regression_1048945_TEST(memcached_st
*)
1879 memcached_return status
;
1881 memcached_server_st
* list
= memcached_server_list_append_with_weight(NULL
, "a", 11211, 0, &status
);
1882 test_compare(status
, MEMCACHED_SUCCESS
);
1884 list
= memcached_server_list_append_with_weight(list
, "b", 11211, 0, &status
);
1885 test_compare(status
, MEMCACHED_SUCCESS
);
1887 list
= memcached_server_list_append_with_weight(list
, "c", 11211, 0, &status
);
1888 test_compare(status
, MEMCACHED_SUCCESS
);
1890 memcached_st
* memc
= memcached_create(NULL
);
1892 status
= memcached_server_push(memc
, list
);
1893 memcached_server_list_free(list
);
1894 test_compare(status
, MEMCACHED_SUCCESS
);
1896 memcached_server_instance_st server
= memcached_server_by_key(memc
, test_literal_param(__func__
), &status
);
1898 test_compare(status
, MEMCACHED_SUCCESS
);
1900 memcached_free(memc
);
1902 return TEST_SUCCESS
;
1905 test_return_t
memcached_fetch_result_NOT_FOUND(memcached_st
*memc
)
1907 memcached_return_t rc
;
1909 const char *key
= "not_found";
1910 size_t key_length
= test_literal_param_size("not_found");
1912 test_compare(MEMCACHED_SUCCESS
,
1913 memcached_mget(memc
, &key
, &key_length
, 1));
1915 memcached_result_st
*result
= memcached_fetch_result(memc
, NULL
, &rc
);
1917 test_compare(MEMCACHED_NOTFOUND
, rc
);
1919 memcached_result_free(result
);
1921 return TEST_SUCCESS
;
1924 static memcached_return_t
clone_test_callback(memcached_st
*, memcached_st
*)
1926 return MEMCACHED_SUCCESS
;
1929 static memcached_return_t
cleanup_test_callback(memcached_st
*)
1931 return MEMCACHED_SUCCESS
;
1934 test_return_t
callback_test(memcached_st
*memc
)
1936 /* Test User Data */
1940 memcached_return_t rc
;
1942 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
));
1943 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1944 test_true(*test_ptr
== x
);
1947 /* Test Clone Callback */
1949 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1950 void *clone_cb_ptr
= *(void **)&clone_cb
;
1951 void *temp_function
= NULL
;
1953 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_cb_ptr
));
1954 memcached_return_t rc
;
1955 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1956 test_true(temp_function
== clone_cb_ptr
);
1957 test_compare(MEMCACHED_SUCCESS
, rc
);
1960 /* Test Cleanup Callback */
1962 memcached_cleanup_fn cleanup_cb
= (memcached_cleanup_fn
)cleanup_test_callback
;
1963 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1964 void *temp_function
= NULL
;
1965 memcached_return_t rc
;
1967 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_cb_ptr
));
1968 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1969 test_true(temp_function
== cleanup_cb_ptr
);
1972 return TEST_SUCCESS
;
1975 /* We don't test the behavior itself, we test the switches */
1976 test_return_t
behavior_test(memcached_st
*memc
)
1978 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
1979 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
));
1981 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
1982 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
));
1984 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, MEMCACHED_HASH_MD5
);
1985 test_compare(uint64_t(MEMCACHED_HASH_MD5
), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
));
1987 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
1988 test_zero(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
));
1990 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
1991 test_zero(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
));
1993 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, MEMCACHED_HASH_DEFAULT
);
1994 test_compare(uint64_t(MEMCACHED_HASH_DEFAULT
), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
));
1996 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, MEMCACHED_HASH_CRC
);
1997 test_compare(uint64_t(MEMCACHED_HASH_CRC
), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
));
1999 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
));
2001 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
));
2003 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
2004 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+1);
2005 test_compare((value
+1), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2007 return TEST_SUCCESS
;
2010 test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2012 test_compare(MEMCACHED_DEPRECATED
,
2013 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, true));
2015 // Platform dependent
2017 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2021 return TEST_SUCCESS
;
2025 test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2027 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, true);
2028 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2030 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2032 if (memcached_success(rc
))
2041 return TEST_SUCCESS
;
2045 test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2047 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, true);
2048 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2050 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2052 if (memcached_success(rc
))
2061 return TEST_SUCCESS
;
2064 /* Make sure we behave properly if server list has no values */
2065 test_return_t
user_supplied_bug4(memcached_st
*memc
)
2067 const char *keys
[]= {"fudge", "son", "food"};
2068 size_t key_length
[]= {5, 3, 4};
2070 /* Here we free everything before running a bunch of mget tests */
2071 memcached_servers_reset(memc
);
2074 /* We need to empty the server before continueing test */
2075 test_compare(MEMCACHED_NO_SERVERS
,
2076 memcached_flush(memc
, 0));
2078 test_compare(MEMCACHED_NO_SERVERS
,
2079 memcached_mget(memc
, keys
, key_length
, 3));
2082 unsigned int keys_returned
;
2083 memcached_return_t rc
;
2084 test_compare(TEST_SUCCESS
, fetch_all_results(memc
, keys_returned
, rc
));
2085 test_compare(MEMCACHED_NOTFOUND
, rc
);
2086 test_zero(keys_returned
);
2089 for (uint32_t x
= 0; x
< 3; x
++)
2091 test_compare(MEMCACHED_NO_SERVERS
,
2092 memcached_set(memc
, keys
[x
], key_length
[x
],
2093 keys
[x
], key_length
[x
],
2094 (time_t)50, (uint32_t)9));
2097 test_compare(MEMCACHED_NO_SERVERS
,
2098 memcached_mget(memc
, keys
, key_length
, 3));
2102 char return_key
[MEMCACHED_MAX_KEY
];
2103 memcached_return_t rc
;
2104 size_t return_key_length
;
2105 size_t return_value_length
;
2108 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2109 &return_value_length
, &flags
, &rc
)))
2111 test_true(return_value
);
2112 test_compare(MEMCACHED_SUCCESS
, rc
);
2113 test_true(return_key_length
== return_value_length
);
2114 test_memcmp(return_value
, return_key
, return_value_length
);
2120 return TEST_SUCCESS
;
2123 #define VALUE_SIZE_BUG5 1048064
2124 test_return_t
user_supplied_bug5(memcached_st
*memc
)
2126 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2127 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2129 size_t value_length
;
2131 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2133 for (uint32_t x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2135 insert_data
[x
]= (signed char)rand();
2138 test_compare(MEMCACHED_SUCCESS
,
2139 memcached_flush(memc
, 0));
2141 memcached_return_t rc
;
2142 test_null(memcached_get(memc
, keys
[0], key_length
[0], &value_length
, &flags
, &rc
));
2143 test_compare(MEMCACHED_SUCCESS
,
2144 memcached_mget(memc
, keys
, key_length
, 4));
2147 test_compare(TEST_SUCCESS
, fetch_all_results(memc
, count
, rc
));
2148 test_compare(MEMCACHED_NOTFOUND
, rc
);
2151 for (uint32_t x
= 0; x
< 4; x
++)
2153 test_compare(MEMCACHED_SUCCESS
,
2154 memcached_set(memc
, keys
[x
], key_length
[x
],
2155 insert_data
, VALUE_SIZE_BUG5
,
2156 (time_t)0, (uint32_t)0));
2159 for (uint32_t x
= 0; x
< 10; x
++)
2161 value
= memcached_get(memc
, keys
[0], key_length
[0],
2162 &value_length
, &flags
, &rc
);
2163 test_compare(rc
, MEMCACHED_SUCCESS
);
2167 test_compare(MEMCACHED_SUCCESS
,
2168 memcached_mget(memc
, keys
, key_length
, 4));
2170 test_compare(TEST_SUCCESS
, fetch_all_results(memc
, count
));
2171 test_compare(4U, count
);
2173 delete [] insert_data
;
2175 return TEST_SUCCESS
;
2178 test_return_t
user_supplied_bug6(memcached_st
*memc
)
2180 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2181 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2182 char return_key
[MEMCACHED_MAX_KEY
];
2183 size_t return_key_length
;
2185 size_t value_length
;
2187 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2189 for (uint32_t x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2191 insert_data
[x
]= (signed char)rand();
2194 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0));
2196 test_compare(TEST_SUCCESS
, confirm_keys_dont_exist(memc
, keys
, test_array_length(keys
)));
2198 // We will now confirm that memcached_mget() returns success, but we will
2199 // then check to make sure that no actual keys are returned.
2200 test_compare(MEMCACHED_SUCCESS
,
2201 memcached_mget(memc
, keys
, key_length
, 4));
2203 memcached_return_t rc
;
2205 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2206 &value_length
, &flags
, &rc
)))
2211 test_compare(MEMCACHED_NOTFOUND
, rc
);
2213 for (uint32_t x
= 0; x
< test_array_length(keys
); x
++)
2215 test_compare(MEMCACHED_SUCCESS
,
2216 memcached_set(memc
, keys
[x
], key_length
[x
],
2217 insert_data
, VALUE_SIZE_BUG5
,
2218 (time_t)0, (uint32_t)0));
2220 test_compare(TEST_SUCCESS
, confirm_keys_exist(memc
, keys
, test_array_length(keys
)));
2222 for (uint32_t x
= 0; x
< 2; x
++)
2224 value
= memcached_get(memc
, keys
[0], key_length
[0],
2225 &value_length
, &flags
, &rc
);
2229 test_compare(MEMCACHED_SUCCESS
,
2230 memcached_mget(memc
, keys
, key_length
, 4));
2231 /* We test for purge of partial complete fetches */
2232 for (count
= 3; count
; count
--)
2234 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2235 &value_length
, &flags
, &rc
);
2236 test_compare(MEMCACHED_SUCCESS
, rc
);
2237 test_memcmp(value
, insert_data
, value_length
);
2238 test_true(value_length
);
2242 delete [] insert_data
;
2244 return TEST_SUCCESS
;
2247 test_return_t
user_supplied_bug8(memcached_st
*)
2249 memcached_return_t rc
;
2251 memcached_st
*memc_clone
;
2253 memcached_server_st
*servers
;
2254 const char *server_list
= "memcache1.memcache.bk.sapo.pt:11211, memcache1.memcache.bk.sapo.pt:11212, memcache1.memcache.bk.sapo.pt:11213, memcache1.memcache.bk.sapo.pt:11214, memcache2.memcache.bk.sapo.pt:11211, memcache2.memcache.bk.sapo.pt:11212, memcache2.memcache.bk.sapo.pt:11213, memcache2.memcache.bk.sapo.pt:11214";
2256 servers
= memcached_servers_parse(server_list
);
2259 mine
= memcached_create(NULL
);
2260 rc
= memcached_server_push(mine
, servers
);
2261 test_compare(MEMCACHED_SUCCESS
, rc
);
2262 memcached_server_list_free(servers
);
2265 memc_clone
= memcached_clone(NULL
, mine
);
2267 memcached_quit(mine
);
2268 memcached_quit(memc_clone
);
2271 memcached_free(mine
);
2272 memcached_free(memc_clone
);
2274 return TEST_SUCCESS
;
2277 /* Test flag store/retrieve */
2278 test_return_t
user_supplied_bug7(memcached_st
*memc
)
2280 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2281 test_true(insert_data
);
2283 for (size_t x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2285 insert_data
[x
]= (signed char)rand();
2288 memcached_flush(memc
, 0);
2290 const char *keys
= "036790384900";
2291 size_t key_length
= strlen(keys
);
2292 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, keys
, key_length
,
2293 insert_data
, VALUE_SIZE_BUG5
,
2296 memcached_return_t rc
;
2297 size_t value_length
;
2299 char *value
= memcached_get(memc
, keys
, key_length
,
2300 &value_length
, &flags
, &rc
);
2301 test_compare(245U, flags
);
2305 test_compare(MEMCACHED_SUCCESS
, memcached_mget(memc
, &keys
, &key_length
, 1));
2307 char return_key
[MEMCACHED_MAX_KEY
];
2308 size_t return_key_length
;
2310 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2311 &value_length
, &flags
, &rc
);
2312 test_compare(uint32_t(245), flags
);
2315 delete [] insert_data
;
2318 return TEST_SUCCESS
;
2321 test_return_t
user_supplied_bug9(memcached_st
*memc
)
2323 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2324 size_t key_length
[3];
2328 char return_key
[MEMCACHED_MAX_KEY
];
2329 size_t return_key_length
;
2331 size_t return_value_length
;
2334 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2335 key_length
[1]= strlen("fudge&*@#");
2336 key_length
[2]= strlen("for^#@&$not");
2339 for (unsigned int x
= 0; x
< 3; x
++)
2341 memcached_return_t rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2342 keys
[x
], key_length
[x
],
2343 (time_t)50, (uint32_t)9);
2344 test_compare(MEMCACHED_SUCCESS
, rc
);
2347 memcached_return_t rc
= memcached_mget(memc
, keys
, key_length
, 3);
2348 test_compare(MEMCACHED_SUCCESS
, rc
);
2350 /* We need to empty the server before continueing test */
2351 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2352 &return_value_length
, &flags
, &rc
)) != NULL
)
2354 test_true(return_value
);
2358 test_compare(3U, count
);
2360 return TEST_SUCCESS
;
2363 /* We are testing with aggressive timeout to get failures */
2364 test_return_t
user_supplied_bug10(memcached_st
*memc
)
2366 test_skip(memc
->servers
[0].type
, MEMCACHED_CONNECTION_TCP
);
2368 size_t value_length
= 512;
2369 unsigned int set
= 1;
2370 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2372 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2373 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2374 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, uint64_t(0));
2376 libtest::vchar_t value
;
2377 value
.reserve(value_length
);
2378 for (uint32_t x
= 0; x
< value_length
; x
++)
2380 value
.push_back(char(x
% 127));
2383 for (unsigned int x
= 1; x
<= 100000; ++x
)
2385 memcached_return_t rc
= memcached_set(mclone
,
2386 test_literal_param("foo"),
2387 &value
[0], value
.size(),
2390 test_true((rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_WRITE_FAILURE
or rc
== MEMCACHED_BUFFERED
or rc
== MEMCACHED_TIMEOUT
or rc
== MEMCACHED_CONNECTION_FAILURE
2391 or rc
== MEMCACHED_SERVER_TEMPORARILY_DISABLED
));
2393 if (rc
== MEMCACHED_WRITE_FAILURE
or rc
== MEMCACHED_TIMEOUT
)
2399 memcached_free(mclone
);
2401 return TEST_SUCCESS
;
2405 We are looking failures in the async protocol
2407 test_return_t
user_supplied_bug11(memcached_st
*memc
)
2409 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2411 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, true);
2412 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, true);
2413 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, size_t(-1));
2415 test_compare(-1, int32_t(memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
)));
2418 libtest::vchar_t value
;
2420 for (unsigned int x
= 0; x
< 512; x
++)
2422 value
.push_back(char(x
% 127));
2425 for (unsigned int x
= 1; x
<= 100000; ++x
)
2427 memcached_return_t rc
= memcached_set(mclone
, test_literal_param("foo"), &value
[0], value
.size(), 0, 0);
2431 memcached_free(mclone
);
2433 return TEST_SUCCESS
;
2437 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2439 test_return_t
user_supplied_bug12(memcached_st
*memc
)
2441 memcached_return_t rc
;
2443 size_t value_length
;
2445 uint64_t number_value
;
2447 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2448 &value_length
, &flags
, &rc
);
2450 test_compare(MEMCACHED_NOTFOUND
, rc
);
2452 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2455 /* The binary protocol will set the key if it doesn't exist */
2456 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2458 test_compare(MEMCACHED_SUCCESS
, rc
);
2462 test_compare(MEMCACHED_NOTFOUND
, rc
);
2465 test_compare(MEMCACHED_SUCCESS
,
2466 memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0));
2468 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"), &value_length
, &flags
, &rc
);
2472 test_compare(MEMCACHED_SUCCESS
,
2473 memcached_increment(memc
, "autoincrement", strlen("autoincrement"), 1, &number_value
));
2474 test_compare(2UL, number_value
);
2476 return TEST_SUCCESS
;
2480 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2481 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2483 test_return_t
user_supplied_bug13(memcached_st
*memc
)
2485 char key
[] = "key34567890";
2487 char commandFirst
[]= "set key34567890 0 0 ";
2488 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2489 size_t commandLength
;
2491 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2493 size_t overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2495 for (size_t testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2497 char *overflow
= new (std::nothrow
) char[testSize
];
2498 test_true(overflow
);
2500 memset(overflow
, 'x', testSize
);
2501 test_compare(MEMCACHED_SUCCESS
,
2502 memcached_set(memc
, key
, strlen(key
),
2503 overflow
, testSize
, 0, 0));
2507 return TEST_SUCCESS
;
2512 Test values of many different sizes
2513 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2514 set key34567890 0 0 8169 \r\n
2515 is sent followed by buffer of size 8169, followed by 8169
2517 test_return_t
user_supplied_bug14(memcached_st
*memc
)
2519 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, true);
2521 libtest::vchar_t value
;
2522 value
.reserve(18000);
2523 for (ptrdiff_t x
= 0; x
< 18000; x
++)
2525 value
.push_back((char) (x
% 127));
2528 for (size_t current_length
= 1; current_length
< value
.size(); current_length
++)
2530 memcached_return_t rc
= memcached_set(memc
, test_literal_param("foo"),
2531 &value
[0], current_length
,
2532 (time_t)0, (uint32_t)0);
2533 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
2535 size_t string_length
;
2537 char *string
= memcached_get(memc
, test_literal_param("foo"),
2538 &string_length
, &flags
, &rc
);
2540 test_compare(MEMCACHED_SUCCESS
, rc
);
2541 test_compare(string_length
, current_length
);
2543 snprintf(buffer
, sizeof(buffer
), "%u", uint32_t(string_length
));
2544 test_memcmp(string
, &value
[0], string_length
);
2549 return TEST_SUCCESS
;
2553 Look for zero length value problems
2555 test_return_t
user_supplied_bug15(memcached_st
*memc
)
2557 for (uint32_t x
= 0; x
< 2; x
++)
2559 memcached_return_t rc
= memcached_set(memc
, test_literal_param("mykey"),
2561 (time_t)0, (uint32_t)0);
2563 test_compare(MEMCACHED_SUCCESS
, rc
);
2567 char *value
= memcached_get(memc
, test_literal_param("mykey"),
2568 &length
, &flags
, &rc
);
2570 test_compare(MEMCACHED_SUCCESS
, rc
);
2575 value
= memcached_get(memc
, test_literal_param("mykey"),
2576 &length
, &flags
, &rc
);
2578 test_compare(MEMCACHED_SUCCESS
, rc
);
2584 return TEST_SUCCESS
;
2587 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2588 test_return_t
user_supplied_bug16(memcached_st
*memc
)
2590 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, test_literal_param("mykey"),
2592 (time_t)0, UINT32_MAX
));
2597 memcached_return_t rc
;
2598 char *value
= memcached_get(memc
, test_literal_param("mykey"),
2599 &length
, &flags
, &rc
);
2601 test_compare(MEMCACHED_SUCCESS
, rc
);
2604 test_compare(flags
, UINT32_MAX
);
2606 return TEST_SUCCESS
;
2609 #if !defined(__sun) && !defined(__OpenBSD__)
2610 /* Check the validity of chinese key*/
2611 test_return_t
user_supplied_bug17(memcached_st
*memc
)
2613 const char *key
= "豆瓣";
2614 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2615 memcached_return_t rc
= memcached_set(memc
, key
, strlen(key
),
2616 value
, strlen(value
),
2619 test_compare(MEMCACHED_SUCCESS
, rc
);
2623 char *value2
= memcached_get(memc
, key
, strlen(key
),
2624 &length
, &flags
, &rc
);
2626 test_compare(length
, strlen(value
));
2627 test_compare(MEMCACHED_SUCCESS
, rc
);
2628 test_memcmp(value
, value2
, length
);
2631 return TEST_SUCCESS
;
2639 test_return_t
user_supplied_bug19(memcached_st
*)
2641 memcached_return_t res
;
2643 memcached_st
*memc
= memcached(test_literal_param("--server=localhost:11311/?100 --server=localhost:11312/?100"));
2645 memcached_server_instance_st server
= memcached_server_by_key(memc
, "a", 1, &res
);
2648 memcached_free(memc
);
2650 return TEST_SUCCESS
;
2653 /* CAS test from Andei */
2654 test_return_t
user_supplied_bug20(memcached_st
*memc
)
2656 const char *key
= "abc";
2657 size_t key_len
= strlen("abc");
2659 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
2661 test_compare(MEMCACHED_SUCCESS
,
2663 test_literal_param("abc"),
2664 test_literal_param("foobar"),
2665 (time_t)0, (uint32_t)0));
2667 test_compare(MEMCACHED_SUCCESS
,
2668 memcached_mget(memc
, &key
, &key_len
, 1));
2670 memcached_result_st result_obj
;
2671 memcached_result_st
*result
= memcached_result_create(memc
, &result_obj
);
2674 memcached_result_create(memc
, &result_obj
);
2675 memcached_return_t status
;
2676 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2679 test_compare(MEMCACHED_SUCCESS
, status
);
2681 memcached_result_free(result
);
2683 return TEST_SUCCESS
;
2686 /* Large mget() of missing keys with binary proto
2688 * If many binary quiet commands (such as getq's in an mget) fill the output
2689 * buffer and the server chooses not to respond, memcached_flush hangs. See
2690 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2693 /* sighandler_t function that always asserts false */
2694 static __attribute__((noreturn
)) void fail(int)
2700 test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2705 return TEST_SKIPPED
;
2707 void (*oldalarm
)(int);
2709 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
2710 test_true(memc_clone
);
2712 /* only binproto uses getq for mget */
2713 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
2715 /* empty the cache to ensure misses (hence non-responses) */
2716 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc_clone
, 0));
2718 keys_st
keys(key_count
);
2720 oldalarm
= signal(SIGALRM
, fail
);
2723 test_compare_got(MEMCACHED_SUCCESS
,
2724 memcached_mget(memc_clone
, keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()),
2725 memcached_last_error_message(memc_clone
));
2728 signal(SIGALRM
, oldalarm
);
2730 memcached_return_t rc
;
2732 char return_key
[MEMCACHED_MAX_KEY
];
2733 size_t return_key_length
;
2735 size_t return_value_length
;
2736 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2737 &return_value_length
, &flags
, &rc
)))
2739 test_false(return_value
); // There are no keys to fetch, so the value should never be returned
2741 test_compare(MEMCACHED_NOTFOUND
, rc
);
2742 test_zero(return_value_length
);
2743 test_zero(return_key_length
);
2744 test_false(return_key
[0]);
2745 test_false(return_value
);
2747 memcached_free(memc_clone
);
2749 return TEST_SUCCESS
;
2753 test_return_t
user_supplied_bug21(memcached_st
*memc
)
2755 test_skip(TEST_SUCCESS
, pre_binary(memc
));
2757 /* should work as of r580 */
2758 test_compare(TEST_SUCCESS
,
2759 _user_supplied_bug21(memc
, 10));
2761 /* should fail as of r580 */
2762 test_compare(TEST_SUCCESS
,
2763 _user_supplied_bug21(memc
, 1000));
2765 return TEST_SUCCESS
;
2768 test_return_t
ketama_TEST(memcached_st
*)
2770 test::Memc
memc("--server=10.0.1.1:11211 --server=10.0.1.2:11211");
2772 test_compare(MEMCACHED_SUCCESS
,
2773 memcached_behavior_set(&memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, true));
2775 test_compare(memcached_behavior_get(&memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
), uint64_t(1));
2777 test_compare(memcached_behavior_set(&memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
), MEMCACHED_SUCCESS
);
2779 test_compare(memcached_behavior_get(&memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
), MEMCACHED_HASH_MD5
);
2781 test_compare(memcached_behavior_set_distribution(&memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
), MEMCACHED_SUCCESS
);
2784 return TEST_SUCCESS
;
2787 test_return_t
output_ketama_weighted_keys(memcached_st
*)
2789 memcached_st
*memc
= memcached_create(NULL
);
2793 test_compare(MEMCACHED_SUCCESS
,
2794 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, true));
2796 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2797 test_compare(value
, uint64_t(1));
2799 test_compare(MEMCACHED_SUCCESS
,
2800 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
));
2802 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2803 test_true(value
== MEMCACHED_HASH_MD5
);
2806 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
2808 memcached_server_st
*server_pool
;
2809 server_pool
= memcached_servers_parse("10.0.1.1:11211,10.0.1.2:11211,10.0.1.3:11211,10.0.1.4:11211,10.0.1.5:11211,10.0.1.6:11211,10.0.1.7:11211,10.0.1.8:11211,192.168.1.1:11211,192.168.100.1:11211");
2810 memcached_server_push(memc
, server_pool
);
2812 // @todo this needs to be refactored to actually test something.
2815 if ((fp
= fopen("ketama_keys.txt", "w")))
2819 printf("cannot write to file ketama_keys.txt");
2820 return TEST_FAILURE
;
2823 for (int x
= 0; x
< 10000; x
++)
2826 snprintf(key
, sizeof(key
), "%d", x
);
2828 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
2829 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2830 in_port_t port
= memc
->hosts
[server_idx
].port
;
2831 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
2832 memcached_server_instance_st instance
=
2833 memcached_server_instance_by_position(memc
, host_index
);
2837 memcached_server_list_free(server_pool
);
2838 memcached_free(memc
);
2840 return TEST_SUCCESS
;
2844 test_return_t
result_static(memcached_st
*memc
)
2846 memcached_result_st result
;
2847 memcached_result_st
*result_ptr
= memcached_result_create(memc
, &result
);
2848 test_false(result
.options
.is_allocated
);
2849 test_true(memcached_is_initialized(&result
));
2850 test_true(result_ptr
);
2851 test_true(result_ptr
== &result
);
2853 memcached_result_free(&result
);
2855 test_false(result
.options
.is_allocated
);
2856 test_false(memcached_is_initialized(&result
));
2858 return TEST_SUCCESS
;
2861 test_return_t
result_alloc(memcached_st
*memc
)
2863 memcached_result_st
*result_ptr
= memcached_result_create(memc
, NULL
);
2864 test_true(result_ptr
);
2865 test_true(result_ptr
->options
.is_allocated
);
2866 test_true(memcached_is_initialized(result_ptr
));
2867 memcached_result_free(result_ptr
);
2869 return TEST_SUCCESS
;
2873 test_return_t
add_host_test1(memcached_st
*memc
)
2875 memcached_return_t rc
;
2876 char servername
[]= "0.example.com";
2878 memcached_server_st
*servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2880 test_compare(1U, memcached_server_list_count(servers
));
2882 for (uint32_t x
= 2; x
< 20; x
++)
2884 char buffer
[SMALL_STRING_LEN
];
2886 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
2887 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2889 test_compare(MEMCACHED_SUCCESS
, rc
);
2890 test_compare(x
, memcached_server_list_count(servers
));
2893 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
2894 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
2896 memcached_server_list_free(servers
);
2898 return TEST_SUCCESS
;
2902 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
2906 #ifdef HARD_MALLOC_TESTS
2907 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
2915 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
2919 #ifdef HARD_MALLOC_TESTS
2920 void *ret
= malloc(size
+ 8);
2923 ret
= (void*)((caddr_t
)ret
+ 8);
2926 void *ret
= malloc(size
);
2931 memset(ret
, 0xff, size
);
2938 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *)
2940 #ifdef HARD_MALLOC_TESTS
2941 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
2942 void *nmem
= realloc(real_ptr
, size
+ 8);
2947 ret
= (void*)((caddr_t
)nmem
+ 8);
2953 return realloc(mem
, size
);
2958 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *)
2960 #ifdef HARD_MALLOC_TESTS
2961 void *mem
= my_malloc(ptr
, nelem
* size
);
2964 memset(mem
, 0, nelem
* size
);
2970 return calloc(nelem
, size
);
2974 test_return_t
selection_of_namespace_tests(memcached_st
*memc
)
2976 memcached_return_t rc
;
2977 const char *key
= "mine";
2980 /* Make sure be default none exists */
2981 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
2983 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
2985 /* Test a clean set */
2986 test_compare(MEMCACHED_SUCCESS
,
2987 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, (void *)key
));
2989 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
2991 test_memcmp(value
, key
, 4);
2992 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
2994 /* Test that we can turn it off */
2995 test_compare(MEMCACHED_SUCCESS
,
2996 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, NULL
));
2998 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3000 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3002 /* Now setup for main test */
3003 test_compare(MEMCACHED_SUCCESS
,
3004 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, (void *)key
));
3006 value
= (char *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3008 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3009 test_memcmp(value
, key
, 4);
3011 /* Set to Zero, and then Set to something too large */
3014 memset(long_key
, 0, 255);
3016 test_compare(MEMCACHED_SUCCESS
,
3017 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, NULL
));
3019 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3021 test_compare(MEMCACHED_SUCCESS
, rc
);
3023 /* Test a long key for failure */
3024 /* TODO, extend test to determine based on setting, what result should be */
3025 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3026 test_compare(MEMCACHED_SUCCESS
,
3027 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, long_key
));
3029 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3030 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3031 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_SUCCESS
: MEMCACHED_BAD_KEY_PROVIDED
,
3032 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, long_key
));
3034 /* Test for a bad prefix, but with a short key */
3035 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_INVALID_ARGUMENTS
: MEMCACHED_SUCCESS
,
3036 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1));
3038 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_SUCCESS
: MEMCACHED_BAD_KEY_PROVIDED
,
3039 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, "dog cat"));
3042 return TEST_SUCCESS
;
3045 test_return_t
set_namespace(memcached_st
*memc
)
3047 memcached_return_t rc
;
3048 const char *key
= "mine";
3051 // Make sure we default to a null namespace
3052 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3054 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3056 /* Test a clean set */
3057 test_compare(MEMCACHED_SUCCESS
,
3058 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, (void *)key
));
3060 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3062 test_memcmp(value
, key
, 4);
3063 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3065 return TEST_SUCCESS
;
3068 test_return_t
set_namespace_and_binary(memcached_st
*memc
)
3070 test_return_if(pre_binary(memc
));
3071 test_return_if(set_namespace(memc
));
3073 return TEST_SUCCESS
;
3076 #ifdef MEMCACHED_ENABLE_DEPRECATED
3077 test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3079 void *test_ptr
= NULL
;
3082 memcached_malloc_fn malloc_cb
= (memcached_malloc_fn
)my_malloc
;
3083 cb_ptr
= *(void **)&malloc_cb
;
3084 memcached_return_t rc
;
3086 test_compare(MEMCACHED_SUCCESS
,
3087 memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
));
3088 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3089 test_compare(MEMCACHED_SUCCESS
, rc
);
3090 test_true(test_ptr
== cb_ptr
);
3094 memcached_realloc_fn realloc_cb
=
3095 (memcached_realloc_fn
)my_realloc
;
3096 cb_ptr
= *(void **)&realloc_cb
;
3097 memcached_return_t rc
;
3099 test_compare(MEMCACHED_SUCCESS
,
3100 memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
));
3101 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3102 test_compare(MEMCACHED_SUCCESS
, rc
);
3103 test_true(test_ptr
== cb_ptr
);
3107 memcached_free_fn free_cb
=
3108 (memcached_free_fn
)my_free
;
3109 cb_ptr
= *(void **)&free_cb
;
3110 memcached_return_t rc
;
3112 test_compare(MEMCACHED_SUCCESS
,
3113 memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
));
3114 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3115 test_compare(MEMCACHED_SUCCESS
, rc
);
3116 test_true(test_ptr
== cb_ptr
);
3119 return TEST_SUCCESS
;
3124 test_return_t
set_memory_alloc(memcached_st
*memc
)
3126 test_compare(MEMCACHED_INVALID_ARGUMENTS
,
3127 memcached_set_memory_allocators(memc
, NULL
, my_free
,
3128 my_realloc
, my_calloc
, NULL
));
3130 test_compare(MEMCACHED_SUCCESS
,
3131 memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3132 my_realloc
, my_calloc
, NULL
));
3134 memcached_malloc_fn mem_malloc
;
3135 memcached_free_fn mem_free
;
3136 memcached_realloc_fn mem_realloc
;
3137 memcached_calloc_fn mem_calloc
;
3138 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3139 &mem_realloc
, &mem_calloc
);
3141 test_true(mem_malloc
== my_malloc
);
3142 test_true(mem_realloc
== my_realloc
);
3143 test_true(mem_calloc
== my_calloc
);
3144 test_true(mem_free
== my_free
);
3146 return TEST_SUCCESS
;
3149 test_return_t
enable_consistent_crc(memcached_st
*memc
)
3151 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, MEMCACHED_DISTRIBUTION_CONSISTENT
));
3152 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
), uint64_t(MEMCACHED_DISTRIBUTION_CONSISTENT
));
3155 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3160 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
), uint64_t(MEMCACHED_DISTRIBUTION_CONSISTENT
));
3162 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
) != MEMCACHED_HASH_CRC
)
3164 return TEST_SKIPPED
;
3167 return TEST_SUCCESS
;
3170 test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
3173 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, MEMCACHED_DISTRIBUTION_CONSISTENT
);
3174 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
3179 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
), uint64_t(MEMCACHED_DISTRIBUTION_CONSISTENT
));
3181 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
) != MEMCACHED_HASH_HSIEH
)
3183 return TEST_SKIPPED
;
3186 return TEST_SUCCESS
;
3189 test_return_t
enable_cas(memcached_st
*memc
)
3191 if (libmemcached_util_version_check(memc
, 1, 2, 4))
3193 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true);
3195 return TEST_SUCCESS
;
3198 return TEST_SKIPPED
;
3201 test_return_t
check_for_1_2_3(memcached_st
*memc
)
3203 memcached_version(memc
);
3205 memcached_server_instance_st instance
=
3206 memcached_server_instance_by_position(memc
, 0);
3208 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
3209 or instance
->minor_version
> 2)
3211 return TEST_SUCCESS
;
3214 return TEST_SKIPPED
;
3217 test_return_t
MEMCACHED_BEHAVIOR_POLL_TIMEOUT_test(memcached_st
*memc
)
3219 const uint64_t timeout
= 100; // Not using, just checking that it sets
3221 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3223 test_compare(timeout
, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
));
3225 return TEST_SUCCESS
;
3228 test_return_t
noreply_test(memcached_st
*memc
)
3230 test_compare(MEMCACHED_SUCCESS
,
3231 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, true));
3232 test_compare(MEMCACHED_SUCCESS
,
3233 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
3234 test_compare(MEMCACHED_SUCCESS
,
3235 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
3236 test_compare(1LLU, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
));
3237 test_compare(1LLU, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
));
3238 test_compare(1LLU, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
));
3240 memcached_return_t ret
;
3241 for (int count
= 0; count
< 5; ++count
)
3243 for (size_t x
= 0; x
< 100; ++x
)
3245 char key
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
3246 int check_length
= snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
3247 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
3249 size_t len
= (size_t)check_length
;
3254 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3257 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3260 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3263 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3266 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3272 test_true_got(ret
== MEMCACHED_SUCCESS
or ret
== MEMCACHED_BUFFERED
,
3273 memcached_strerror(NULL
, ret
));
3277 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3278 ** API and is _ONLY_ done this way to verify that the library works the
3279 ** way it is supposed to do!!!!
3283 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
3285 memcached_server_instance_st instance
=
3286 memcached_server_instance_by_position(memc
, x
);
3287 no_msg
+=(int)(instance
->cursor_active
);
3290 test_true(no_msg
== 0);
3292 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
3295 ** Now validate that all items was set properly!
3297 for (size_t x
= 0; x
< 100; ++x
)
3301 int check_length
= snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
3303 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
3305 size_t len
= (size_t)check_length
;
3308 char* value
=memcached_get(memc
, key
, strlen(key
),
3309 &length
, &flags
, &ret
);
3310 // For the moment we will just go to the next key
3311 if (MEMCACHED_TIMEOUT
== ret
)
3315 test_true(ret
== MEMCACHED_SUCCESS
and value
!= NULL
);
3318 case 0: /* FALLTHROUGH */
3319 case 1: /* FALLTHROUGH */
3321 test_true(strncmp(value
, key
, len
) == 0);
3322 test_true(len
== length
);
3325 test_true(length
== len
* 2);
3328 test_true(length
== len
* 3);
3338 /* Try setting an illegal cas value (should not return an error to
3339 * the caller (because we don't expect a return message from the server)
3341 const char* keys
[]= {"0"};
3342 size_t lengths
[]= {1};
3345 memcached_result_st results_obj
;
3346 memcached_result_st
*results
;
3347 test_compare(MEMCACHED_SUCCESS
,
3348 memcached_mget(memc
, keys
, lengths
, 1));
3350 results
= memcached_result_create(memc
, &results_obj
);
3352 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3354 test_compare(MEMCACHED_SUCCESS
, ret
);
3355 uint64_t cas
= memcached_result_cas(results
);
3356 memcached_result_free(&results_obj
);
3358 test_compare(MEMCACHED_SUCCESS
,
3359 memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
));
3362 * The item will have a new cas value, so try to set it again with the old
3363 * value. This should fail!
3365 test_compare(MEMCACHED_SUCCESS
,
3366 memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
));
3367 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3368 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3369 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3372 return TEST_SUCCESS
;
3375 test_return_t
analyzer_test(memcached_st
*memc
)
3377 memcached_analysis_st
*report
;
3378 memcached_return_t rc
;
3380 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3381 test_compare(MEMCACHED_SUCCESS
, rc
);
3382 test_true(memc_stat
);
3384 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3385 test_compare(MEMCACHED_SUCCESS
, rc
);
3389 memcached_stat_free(NULL
, memc_stat
);
3391 return TEST_SUCCESS
;
3394 test_return_t
util_version_test(memcached_st
*memc
)
3396 test_compare(memcached_version(memc
), MEMCACHED_SUCCESS
);
3397 test_true(libmemcached_util_version_check(memc
, 0, 0, 0));
3399 bool if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
3401 // We expect failure
3404 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
3405 fprintf(stderr
, "\nDumping Server Information\n\n");
3406 memcached_server_fn callbacks
[1];
3408 callbacks
[0]= dump_server_information
;
3409 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
3410 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
3412 test_true(if_successful
== false);
3414 memcached_server_instance_st instance
=
3415 memcached_server_instance_by_position(memc
, 0);
3417 memcached_version(memc
);
3419 // We only use one binary when we test, so this should be just fine.
3420 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
3421 test_true(if_successful
== true);
3423 if (instance
->micro_version
> 0)
3425 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
3427 else if (instance
->minor_version
> 0)
3429 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
3431 else if (instance
->major_version
> 0)
3433 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
3436 test_true(if_successful
== true);
3438 if (instance
->micro_version
> 0)
3440 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
3442 else if (instance
->minor_version
> 0)
3444 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
3446 else if (instance
->major_version
> 0)
3448 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
3451 test_true(if_successful
== false);
3453 return TEST_SUCCESS
;
3456 test_return_t
getpid_connection_failure_test(memcached_st
*memc
)
3458 test_skip(memc
->servers
[0].type
, MEMCACHED_CONNECTION_TCP
);
3459 memcached_return_t rc
;
3460 memcached_server_instance_st instance
=
3461 memcached_server_instance_by_position(memc
, 0);
3463 // Test both the version that returns a code, and the one that does not.
3464 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3465 memcached_server_port(instance
) -1, NULL
) == -1);
3467 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3468 memcached_server_port(instance
) -1, &rc
) == -1);
3469 test_compare_got(MEMCACHED_CONNECTION_FAILURE
, rc
, memcached_strerror(memc
, rc
));
3471 return TEST_SUCCESS
;
3475 test_return_t
getpid_test(memcached_st
*memc
)
3477 memcached_return_t rc
;
3478 memcached_server_instance_st instance
=
3479 memcached_server_instance_by_position(memc
, 0);
3481 // Test both the version that returns a code, and the one that does not.
3482 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3483 memcached_server_port(instance
), NULL
) > -1);
3485 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3486 memcached_server_port(instance
), &rc
) > -1);
3487 test_compare(MEMCACHED_SUCCESS
, rc
);
3489 return TEST_SUCCESS
;
3492 static memcached_return_t
ping_each_server(const memcached_st
*,
3493 memcached_server_instance_st instance
,
3496 // Test both the version that returns a code, and the one that does not.
3497 memcached_return_t rc
;
3498 if (libmemcached_util_ping(memcached_server_name(instance
),
3499 memcached_server_port(instance
), &rc
) == false)
3501 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "%s:%d %s", memcached_server_name(instance
),
3502 memcached_server_port(instance
), memcached_strerror(NULL
, rc
));
3505 if (libmemcached_util_ping(memcached_server_name(instance
),
3506 memcached_server_port(instance
), NULL
) == false)
3508 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "%s:%d", memcached_server_name(instance
), memcached_server_port(instance
));
3511 return MEMCACHED_SUCCESS
;
3514 test_return_t
libmemcached_util_ping_TEST(memcached_st
*memc
)
3516 memcached_server_fn callbacks
[1]= { ping_each_server
};
3517 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
3519 return TEST_SUCCESS
;
3524 test_return_t
hash_sanity_test (memcached_st
*memc
)
3528 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
3529 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
3530 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
3531 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
3532 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
3533 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
3534 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
3535 #ifdef HAVE_HSIEH_HASH
3536 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
3538 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
3539 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
3540 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
3542 return TEST_SUCCESS
;
3546 test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
3548 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH
));
3550 test_compare(MEMCACHED_SUCCESS
,
3551 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
3552 (uint64_t)MEMCACHED_HASH_HSIEH
));
3554 return TEST_SUCCESS
;
3557 test_return_t
murmur_avaibility_test (memcached_st
*memc
)
3559 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR
));
3561 test_compare(MEMCACHED_SUCCESS
,
3562 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
));
3564 return TEST_SUCCESS
;
3567 test_return_t
one_at_a_time_run (memcached_st
*)
3572 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3574 test_compare(one_at_a_time_values
[x
],
3575 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
));
3578 return TEST_SUCCESS
;
3581 test_return_t
md5_run (memcached_st
*)
3586 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3588 test_compare(md5_values
[x
],
3589 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
));
3592 return TEST_SUCCESS
;
3595 test_return_t
crc_run (memcached_st
*)
3600 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3602 test_compare(crc_values
[x
],
3603 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
));
3606 return TEST_SUCCESS
;
3609 test_return_t
fnv1_64_run (memcached_st
*)
3611 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64
));
3616 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3618 test_compare(fnv1_64_values
[x
],
3619 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
));
3622 return TEST_SUCCESS
;
3625 test_return_t
fnv1a_64_run (memcached_st
*)
3627 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1A_64
));
3632 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3634 test_compare(fnv1a_64_values
[x
],
3635 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
));
3638 return TEST_SUCCESS
;
3641 test_return_t
fnv1_32_run (memcached_st
*)
3646 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3648 test_compare(fnv1_32_values
[x
],
3649 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
));
3652 return TEST_SUCCESS
;
3655 test_return_t
fnv1a_32_run (memcached_st
*)
3660 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3662 test_compare(fnv1a_32_values
[x
],
3663 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
));
3666 return TEST_SUCCESS
;
3669 test_return_t
hsieh_run (memcached_st
*)
3671 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH
));
3676 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3678 test_compare(hsieh_values
[x
],
3679 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
));
3682 return TEST_SUCCESS
;
3685 test_return_t
murmur_run (memcached_st
*)
3687 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR
));
3689 #ifdef WORDS_BIGENDIAN
3690 (void)murmur_values
;
3691 return TEST_SKIPPED
;
3696 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3698 test_compare(murmur_values
[x
],
3699 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
));
3702 return TEST_SUCCESS
;
3706 test_return_t
murmur3_TEST(hashkit_st
*)
3708 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR3
));
3710 #ifdef WORDS_BIGENDIAN
3711 (void)murmur3_values
;
3712 return TEST_SKIPPED
;
3717 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3719 test_compare(murmur3_values
[x
],
3720 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR3
));
3723 return TEST_SUCCESS
;
3727 test_return_t
jenkins_run (memcached_st
*)
3732 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3734 test_compare(jenkins_values
[x
],
3735 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
));
3738 return TEST_SUCCESS
;
3741 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *)
3743 return libhashkit_md5(string
, string_length
);
3746 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *)
3748 return libhashkit_crc32(string
, string_length
);
3751 test_return_t
memcached_get_hashkit_test (memcached_st
*)
3757 memcached_st
*memc
= memcached(test_literal_param("--server=localhost:1 --server=localhost:2 --server=localhost:3 --server=localhost:4 --server=localhost5 --DISTRIBUTION=modula"));
3759 uint32_t md5_hosts
[]= {4U, 1U, 0U, 1U, 4U, 2U, 0U, 3U, 0U, 0U, 3U, 1U, 0U, 0U, 1U, 3U, 0U, 0U, 0U, 3U, 1U, 0U, 4U, 4U, 3U};
3760 uint32_t crc_hosts
[]= {2U, 4U, 1U, 0U, 2U, 4U, 4U, 4U, 1U, 2U, 3U, 4U, 3U, 4U, 1U, 3U, 3U, 2U, 0U, 0U, 0U, 1U, 2U, 4U, 0U};
3762 const hashkit_st
*kit
= memcached_get_hashkit(memc
);
3764 hashkit_clone(&new_kit
, kit
);
3765 test_compare(HASHKIT_SUCCESS
, hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
));
3767 memcached_set_hashkit(memc
, &new_kit
);
3770 Verify Setting the hash.
3772 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3776 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
3777 test_compare_got(md5_values
[x
], hash_val
, *ptr
);
3782 Now check memcached_st.
3784 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3788 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
3789 test_compare_got(md5_hosts
[x
], hash_val
, *ptr
);
3792 test_compare(HASHKIT_SUCCESS
, hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
));
3794 memcached_set_hashkit(memc
, &new_kit
);
3797 Verify Setting the hash.
3799 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3803 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
3804 test_true(crc_values
[x
] == hash_val
);
3807 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3811 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
3812 test_compare(crc_hosts
[x
], hash_val
);
3815 memcached_free(memc
);
3817 return TEST_SUCCESS
;
3821 Test case adapted from John Gorman <johngorman2@gmail.com>
3823 We are testing the error condition when we connect to a server via memcached_get()
3824 but find that the server is not available.
3826 test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*)
3830 memcached_return rc
;
3833 memcached_st
*tl_memc_h
= memcached(test_literal_param("--server=localhost:9898 --server=localhost:9899")); // This server should not exist
3835 // See if memcached is reachable.
3836 char *value
= memcached_get(tl_memc_h
,
3837 test_literal_param(__func__
),
3842 test_true(memcached_failed(rc
));
3844 memcached_free(tl_memc_h
);
3846 return TEST_SUCCESS
;
3850 We connect to a server which exists, but search for a key that does not exist.
3852 test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
3856 memcached_return rc
;
3858 // See if memcached is reachable.
3859 char *value
= memcached_get(memc
,
3860 test_literal_param(__func__
),
3865 test_compare(MEMCACHED_NOTFOUND
, rc
);
3867 return TEST_SUCCESS
;
3871 Test case adapted from John Gorman <johngorman2@gmail.com>
3873 We are testing the error condition when we connect to a server via memcached_get_by_key()
3874 but find that the server is not available.
3876 test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*)
3880 memcached_return rc
;
3883 memcached_st
*tl_memc_h
= memcached_create(NULL
);
3884 memcached_server_st
*servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
3885 memcached_server_push(tl_memc_h
, servers
);
3886 memcached_server_list_free(servers
);
3888 // See if memcached is reachable.
3889 char *value
= memcached_get_by_key(tl_memc_h
,
3890 test_literal_param(__func__
), // Key
3891 test_literal_param(__func__
), // Value
3896 test_true(memcached_failed(rc
));
3898 memcached_free(tl_memc_h
);
3900 return TEST_SUCCESS
;
3904 We connect to a server which exists, but search for a key that does not exist.
3906 test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
3910 memcached_return rc
;
3912 // See if memcached is reachable.
3913 char *value
= memcached_get_by_key(memc
,
3914 test_literal_param(__func__
), // Key
3915 test_literal_param(__func__
), // Value
3920 test_compare(MEMCACHED_NOTFOUND
, rc
);
3922 return TEST_SUCCESS
;
3925 test_return_t
regression_bug_434484(memcached_st
*memc
)
3927 test_skip(TEST_SUCCESS
, pre_binary(memc
));
3929 test_compare(MEMCACHED_NOTSTORED
,
3930 memcached_append(memc
,
3931 test_literal_param(__func__
), // Key
3932 test_literal_param(__func__
), // Value
3935 libtest::vchar_t data
;
3936 data
.resize(2048 * 1024);
3937 test_compare(MEMCACHED_E2BIG
,
3939 test_literal_param(__func__
), // Key
3940 &data
[0], data
.size(), 0, 0));
3942 return TEST_SUCCESS
;
3945 test_return_t
regression_bug_434843(memcached_st
*original_memc
)
3947 test_skip(TEST_SUCCESS
, pre_binary(original_memc
));
3949 memcached_return_t rc
;
3951 memcached_execute_fn callbacks
[]= { &callback_counter
};
3954 * I only want to hit only _one_ server so I know the number of requests I'm
3955 * sending in the pipleine to the server. Let's try to do a multiget of
3956 * 1024 (that should satisfy most users don't you think?). Future versions
3957 * will include a mget_execute function call if you need a higher number.
3959 memcached_st
*memc
= create_single_instance_memcached(original_memc
, "--BINARY-PROTOCOL");
3964 * Run two times.. the first time we should have 100% cache miss,
3965 * and the second time we should have 100% cache hits
3967 for (ptrdiff_t y
= 0; y
< 2; y
++)
3969 test_compare(MEMCACHED_SUCCESS
,
3970 memcached_mget(memc
, keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
3972 // One the first run we should get a NOT_FOUND, but on the second some data
3973 // should be returned.
3974 test_compare(y
? MEMCACHED_SUCCESS
: MEMCACHED_NOTFOUND
,
3975 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
3979 /* The first iteration should give me a 100% cache miss. verify that*/
3980 char blob
[1024]= { 0 };
3982 test_false(counter
);
3984 for (size_t x
= 0; x
< keys
.size(); ++x
)
3986 rc
= memcached_add(memc
,
3987 keys
.key_at(x
), keys
.length_at(x
),
3988 blob
, sizeof(blob
), 0, 0);
3989 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3994 /* Verify that we received all of the key/value pairs */
3995 test_compare(counter
, keys
.size());
3999 memcached_free(memc
);
4001 return TEST_SUCCESS
;
4004 test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
4006 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
4008 return regression_bug_434843(memc
);
4011 test_return_t
regression_bug_421108(memcached_st
*memc
)
4013 memcached_return_t rc
;
4014 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4015 test_compare(MEMCACHED_SUCCESS
, rc
);
4017 char *bytes_str
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
4018 test_compare(MEMCACHED_SUCCESS
, rc
);
4019 test_true(bytes_str
);
4020 char *bytes_read_str
= memcached_stat_get_value(memc
, memc_stat
,
4022 test_compare(MEMCACHED_SUCCESS
, rc
);
4023 test_true(bytes_read_str
);
4025 char *bytes_written_str
= memcached_stat_get_value(memc
, memc_stat
,
4026 "bytes_written", &rc
);
4027 test_compare(MEMCACHED_SUCCESS
, rc
);
4028 test_true(bytes_written_str
);
4030 unsigned long long bytes
= strtoull(bytes_str
, 0, 10);
4031 unsigned long long bytes_read
= strtoull(bytes_read_str
, 0, 10);
4032 unsigned long long bytes_written
= strtoull(bytes_written_str
, 0, 10);
4034 test_true(bytes
!= bytes_read
);
4035 test_true(bytes
!= bytes_written
);
4037 /* Release allocated resources */
4039 free(bytes_read_str
);
4040 free(bytes_written_str
);
4041 memcached_stat_free(NULL
, memc_stat
);
4043 return TEST_SUCCESS
;
4047 * The test case isn't obvious so I should probably document why
4048 * it works the way it does. Bug 442914 was caused by a bug
4049 * in the logic in memcached_purge (it did not handle the case
4050 * where the number of bytes sent was equal to the watermark).
4051 * In this test case, create messages so that we hit that case
4052 * and then disable noreply mode and issue a new command to
4053 * verify that it isn't stuck. If we change the format for the
4054 * delete command or the watermarks, we need to update this
4057 test_return_t
regression_bug_442914(memcached_st
*original_memc
)
4059 test_skip(original_memc
->servers
[0].type
, MEMCACHED_CONNECTION_TCP
);
4061 memcached_st
* memc
= create_single_instance_memcached(original_memc
, "--NOREPLY --TCP-NODELAY");
4063 for (uint32_t x
= 0; x
< 250; ++x
)
4066 size_t len
= (size_t)snprintf(key
, sizeof(key
), "%0250u", x
);
4067 memcached_return_t rc
= memcached_delete(memc
, key
, len
, 0);
4068 char error_buffer
[2048]= { 0 };
4069 snprintf(error_buffer
, sizeof(error_buffer
), "%s key: %s", memcached_last_error_message(memc
), key
);
4070 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, error_buffer
);
4073 // Delete, and then delete again to look for not found
4076 size_t len
= snprintf(key
, sizeof(key
), "%037u", 251U);
4077 memcached_return_t rc
= memcached_delete(memc
, key
, len
, 0);
4078 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
4080 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, false));
4081 test_compare(MEMCACHED_NOTFOUND
, memcached_delete(memc
, key
, len
, 0));
4084 memcached_free(memc
);
4086 return TEST_SUCCESS
;
4089 test_return_t
regression_bug_447342(memcached_st
*memc
)
4091 if (memcached_server_count(memc
) < 3 or pre_replication(memc
) != TEST_SUCCESS
)
4093 return TEST_SKIPPED
;
4096 test_compare(MEMCACHED_SUCCESS
,
4097 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2));
4101 for (size_t x
= 0; x
< keys
.size(); ++x
)
4103 test_compare(MEMCACHED_SUCCESS
,
4105 keys
.key_at(x
), keys
.length_at(x
), // Keys
4106 keys
.key_at(x
), keys
.length_at(x
), // Values
4111 ** We are using the quiet commands to store the replicas, so we need
4112 ** to ensure that all of them are processed before we can continue.
4113 ** In the test we go directly from storing the object to trying to
4114 ** receive the object from all of the different servers, so we
4115 ** could end up in a race condition (the memcached server hasn't yet
4116 ** processed the quiet command from the replication set when it process
4117 ** the request from the other client (created by the clone)). As a
4118 ** workaround for that we call memcached_quit to send the quit command
4119 ** to the server and wait for the response ;-) If you use the test code
4120 ** as an example for your own code, please note that you shouldn't need
4123 memcached_quit(memc
);
4125 /* Verify that all messages are stored, and we didn't stuff too much
4128 test_compare(MEMCACHED_SUCCESS
,
4129 memcached_mget(memc
,
4130 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
4132 unsigned int counter
= 0;
4133 memcached_execute_fn callbacks
[]= { &callback_counter
};
4134 test_compare(MEMCACHED_SUCCESS
,
4135 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
4137 /* Verify that we received all of the key/value pairs */
4138 test_compare(counter
, keys
.size());
4140 memcached_quit(memc
);
4142 * Don't do the following in your code. I am abusing the internal details
4143 * within the library, and this is not a supported interface.
4144 * This is to verify correct behavior in the library. Fake that two servers
4147 memcached_server_instance_st instance_one
= memcached_server_instance_by_position(memc
, 0);
4148 memcached_server_instance_st instance_two
= memcached_server_instance_by_position(memc
, 2);
4149 in_port_t port0
= instance_one
->port();
4150 in_port_t port2
= instance_two
->port();
4152 ((memcached_server_write_instance_st
)instance_one
)->port(0);
4153 ((memcached_server_write_instance_st
)instance_two
)->port(0);
4155 test_compare(MEMCACHED_SUCCESS
,
4156 memcached_mget(memc
,
4157 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
4160 test_compare(MEMCACHED_SUCCESS
,
4161 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
4162 test_compare(counter
, keys
.size());
4164 /* restore the memc handle */
4165 ((memcached_server_write_instance_st
)instance_one
)->port(port0
);
4166 ((memcached_server_write_instance_st
)instance_two
)->port(port2
);
4168 memcached_quit(memc
);
4170 /* Remove half of the objects */
4171 for (size_t x
= 0; x
< keys
.size(); ++x
)
4175 test_compare(MEMCACHED_SUCCESS
,
4176 memcached_delete(memc
, keys
.key_at(x
), keys
.length_at(x
), 0));
4180 memcached_quit(memc
);
4181 ((memcached_server_write_instance_st
)instance_one
)->port(0);
4182 ((memcached_server_write_instance_st
)instance_two
)->port(0);
4184 /* now retry the command, this time we should have cache misses */
4185 test_compare(MEMCACHED_SUCCESS
,
4186 memcached_mget(memc
,
4187 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
4190 test_compare(MEMCACHED_SUCCESS
,
4191 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
4192 test_compare(counter
, (unsigned int)(keys
.size() >> 1));
4194 /* restore the memc handle */
4195 ((memcached_server_write_instance_st
)instance_one
)->port(port0
);
4196 ((memcached_server_write_instance_st
)instance_two
)->port(port2
);
4198 return TEST_SUCCESS
;
4201 test_return_t
regression_bug_463297(memcached_st
*memc
)
4203 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_delete(memc
, "foo", 3, 1));
4205 // Since we blocked timed delete, this test is no longer valid.
4207 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4208 test_true(memc_clone
);
4209 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
4211 memcached_server_instance_st instance
=
4212 memcached_server_instance_by_position(memc_clone
, 0);
4214 if (instance
->major_version
> 1 ||
4215 (instance
->major_version
== 1 &&
4216 instance
->minor_version
> 2))
4218 /* Binary protocol doesn't support deferred delete */
4219 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
4220 test_true(bin_clone
);
4221 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1));
4222 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_delete(bin_clone
, "foo", 3, 1));
4223 memcached_free(bin_clone
);
4225 memcached_quit(memc_clone
);
4227 /* If we know the server version, deferred delete should fail
4228 * with invalid arguments */
4229 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_delete(memc_clone
, "foo", 3, 1));
4231 /* If we don't know the server version, we should get a protocol error */
4232 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
4234 /* but there is a bug in some of the memcached servers (1.4) that treats
4235 * the counter as noreply so it doesn't send the proper error message
4237 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
4239 /* And buffered mode should be disabled and we should get protocol error */
4240 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
4241 rc
= memcached_delete(memc
, "foo", 3, 1);
4242 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
4244 /* Same goes for noreply... */
4245 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1));
4246 rc
= memcached_delete(memc
, "foo", 3, 1);
4247 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
4249 /* but a normal request should go through (and be buffered) */
4250 test_compare(MEMCACHED_BUFFERED
, (rc
= memcached_delete(memc
, "foo", 3, 0)));
4251 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
4253 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0));
4254 /* unbuffered noreply should be success */
4255 test_compare(MEMCACHED_SUCCESS
, memcached_delete(memc
, "foo", 3, 0));
4256 /* unbuffered with reply should be not found... */
4257 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0));
4258 test_compare(MEMCACHED_NOTFOUND
, memcached_delete(memc
, "foo", 3, 0));
4261 memcached_free(memc_clone
);
4264 return TEST_SUCCESS
;
4268 /* Test memcached_server_get_last_disconnect
4269 * For a working server set, shall be NULL
4270 * For a set of non existing server, shall not be NULL
4272 test_return_t
test_get_last_disconnect(memcached_st
*memc
)
4274 memcached_return_t rc
;
4275 memcached_server_instance_st disconnected_server
;
4277 /* With the working set of server */
4278 const char *key
= "marmotte";
4279 const char *value
= "milka";
4281 memcached_reset_last_disconnected_server(memc
);
4282 test_false(memc
->last_disconnected_server
);
4283 rc
= memcached_set(memc
, key
, strlen(key
),
4284 value
, strlen(value
),
4285 (time_t)0, (uint32_t)0);
4286 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4288 disconnected_server
= memcached_server_get_last_disconnect(memc
);
4289 test_false(disconnected_server
);
4291 /* With a non existing server */
4293 memcached_server_st
*servers
;
4295 const char *server_list
= "localhost:9";
4297 servers
= memcached_servers_parse(server_list
);
4299 mine
= memcached_create(NULL
);
4300 rc
= memcached_server_push(mine
, servers
);
4301 test_compare(MEMCACHED_SUCCESS
, rc
);
4302 memcached_server_list_free(servers
);
4305 rc
= memcached_set(mine
, key
, strlen(key
),
4306 value
, strlen(value
),
4307 (time_t)0, (uint32_t)0);
4308 test_true(memcached_failed(rc
));
4310 disconnected_server
= memcached_server_get_last_disconnect(mine
);
4311 test_true_got(disconnected_server
, memcached_strerror(mine
, rc
));
4312 test_compare(in_port_t(9), memcached_server_port(disconnected_server
));
4313 test_false(strncmp(memcached_server_name(disconnected_server
),"localhost",9));
4315 memcached_quit(mine
);
4316 memcached_free(mine
);
4318 return TEST_SUCCESS
;
4321 test_return_t
test_multiple_get_last_disconnect(memcached_st
*)
4323 const char *server_string
= "--server=localhost:8888 --server=localhost:8889 --server=localhost:8890 --server=localhost:8891 --server=localhost:8892";
4324 char buffer
[BUFSIZ
];
4326 test_compare(MEMCACHED_SUCCESS
,
4327 libmemcached_check_configuration(server_string
, strlen(server_string
), buffer
, sizeof(buffer
)));
4329 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
4332 // We will just use the error strings as our keys
4333 uint32_t counter
= 100;
4336 for (int x
= int(MEMCACHED_SUCCESS
); x
< int(MEMCACHED_MAXIMUM_RETURN
); ++x
)
4338 const char *msg
= memcached_strerror(memc
, memcached_return_t(x
));
4339 memcached_return_t ret
= memcached_set(memc
, msg
, strlen(msg
), NULL
, 0, (time_t)0, (uint32_t)0);
4340 test_true_got((ret
== MEMCACHED_CONNECTION_FAILURE
or ret
== MEMCACHED_SERVER_TEMPORARILY_DISABLED
), memcached_last_error_message(memc
));
4342 memcached_server_instance_st disconnected_server
= memcached_server_get_last_disconnect(memc
);
4343 test_true(disconnected_server
);
4344 test_strcmp("localhost", memcached_server_name(disconnected_server
));
4345 test_true(memcached_server_port(disconnected_server
) >= 8888 and memcached_server_port(disconnected_server
) <= 8892);
4349 memcached_reset_last_disconnected_server(memc
);
4354 memcached_free(memc
);
4356 return TEST_SUCCESS
;
4359 test_return_t
test_verbosity(memcached_st
*memc
)
4361 memcached_verbosity(memc
, 3);
4363 return TEST_SUCCESS
;
4367 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
4368 const char *key
, size_t key_length
,
4369 const char *value
, size_t value_length
,
4379 return MEMCACHED_SUCCESS
;
4382 test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
4384 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
4385 test_compare(MEMCACHED_SUCCESS
, rc
);
4387 test_compare(MEMCACHED_SUCCESS
,
4388 memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
));
4390 test_compare(MEMCACHED_SUCCESS
,
4391 memcached_stat_execute(memc
, "items", stat_printer
, NULL
));
4393 test_compare(MEMCACHED_SUCCESS
,
4394 memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
));
4396 return TEST_SUCCESS
;
4400 * This test ensures that the failure counter isn't incremented during
4401 * normal termination of the memcached instance.
4403 test_return_t
wrong_failure_counter_test(memcached_st
*original_memc
)
4405 memcached_st
* memc
= create_single_instance_memcached(original_memc
, NULL
);
4407 /* Ensure that we are connected to the server by setting a value */
4408 memcached_return_t rc
= memcached_set(memc
,
4409 test_literal_param(__func__
), // Key
4410 test_literal_param(__func__
), // Value
4411 time_t(0), uint32_t(0));
4412 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
4415 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
4417 /* The test is to see that the memcached_quit doesn't increase the
4418 * the server failure conter, so let's ensure that it is zero
4419 * before sending quit
4421 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
4423 memcached_quit(memc
);
4425 /* Verify that it memcached_quit didn't increment the failure counter
4426 * Please note that this isn't bullet proof, because an error could
4429 test_zero(instance
->server_failure_counter
);
4431 memcached_free(memc
);
4433 return TEST_SUCCESS
;
4437 * This tests ensures expected disconnections (for some behavior changes
4438 * for instance) do not wrongly increase failure counter
4440 test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
4442 /* Set value to force connection to the server */
4443 const char *key
= "marmotte";
4444 const char *value
= "milka";
4446 test_compare_hint(MEMCACHED_SUCCESS
,
4447 memcached_set(memc
, key
, strlen(key
),
4448 value
, strlen(value
),
4449 (time_t)0, (uint32_t)0),
4450 memcached_last_error_message(memc
));
4453 /* put failure limit to 1 */
4454 test_compare(MEMCACHED_SUCCESS
,
4455 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, true));
4457 /* Put a retry timeout to effectively activate failure_limit effect */
4458 test_compare(MEMCACHED_SUCCESS
,
4459 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, true));
4461 /* change behavior that triggers memcached_quit()*/
4462 test_compare(MEMCACHED_SUCCESS
,
4463 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, true));
4466 /* Check if we still are connected */
4468 size_t string_length
;
4469 memcached_return rc
;
4470 char *string
= memcached_get(memc
, key
, strlen(key
),
4471 &string_length
, &flags
, &rc
);
4473 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
4477 return TEST_SUCCESS
;
4480 test_return_t
regression_996813_TEST(memcached_st
*)
4482 memcached_st
* memc
= memcached_create(NULL
);
4484 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
));
4485 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1));
4486 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1));
4487 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1));
4488 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1));
4489 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 300));
4490 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 30));
4492 // We will never connect to these servers
4493 in_port_t base_port
= 11211;
4494 for (size_t x
= 0; x
< 17; x
++)
4496 test_compare(MEMCACHED_SUCCESS
, memcached_server_add(memc
, "10.2.3.4", base_port
+x
));
4498 test_compare(6U, memcached_generate_hash(memc
, test_literal_param("SZ6hu0SHweFmpwpc0w2R")));
4499 test_compare(1U, memcached_generate_hash(memc
, test_literal_param("SQCK9eiCf53YxHWnYA.o")));
4500 test_compare(9U, memcached_generate_hash(memc
, test_literal_param("SUSDkGXuuZC9t9VhMwa.")));
4501 test_compare(0U, memcached_generate_hash(memc
, test_literal_param("SnnqnJARfaCNT679iAF_")));
4503 memcached_free(memc
);
4505 return TEST_SUCCESS
;
4510 * Test that ensures mget_execute does not end into recursive calls that finally fails
4512 test_return_t
regression_bug_490486(memcached_st
*original_memc
)
4516 return TEST_SKIPPED
; // My MAC can't handle this test
4519 test_skip(TEST_SUCCESS
, pre_binary(original_memc
));
4522 * I only want to hit _one_ server so I know the number of requests I'm
4523 * sending in the pipeline.
4525 memcached_st
*memc
= create_single_instance_memcached(original_memc
, "--BINARY-PROTOCOL --POLL-TIMEOUT=1000 --REMOVE-FAILED-SERVERS=1 --RETRY-TIMEOUT=3600");
4528 keys_st
keys(20480);
4530 /* First add all of the items.. */
4531 char blob
[1024]= { 0 };
4532 for (size_t x
= 0; x
< keys
.size(); ++x
)
4534 memcached_return rc
= memcached_set(memc
,
4535 keys
.key_at(x
), keys
.length_at(x
),
4536 blob
, sizeof(blob
), 0, 0);
4537 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
); // MEMCACHED_TIMEOUT <-- hash been observed on OSX
4542 /* Try to get all of them with a large multiget */
4544 memcached_execute_function callbacks
[]= { &callback_counter
};
4545 memcached_return_t rc
= memcached_mget_execute(memc
,
4546 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size(),
4547 callbacks
, &counter
, 1);
4548 test_compare(MEMCACHED_SUCCESS
, rc
);
4550 char* the_value
= NULL
;
4551 char the_key
[MEMCACHED_MAX_KEY
];
4552 size_t the_key_length
;
4553 size_t the_value_length
;
4557 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
4559 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
4565 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
4568 test_compare(MEMCACHED_END
, rc
);
4570 /* Verify that we got all of the items */
4571 test_compare(counter
, keys
.size());
4574 memcached_free(memc
);
4576 return TEST_SUCCESS
;
4579 test_return_t
regression_1021819_TEST(memcached_st
*original
)
4581 memcached_st
*memc
= memcached_clone(NULL
, original
);
4584 test_compare(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 2000000), MEMCACHED_SUCCESS
);
4585 test_compare(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 3000000), MEMCACHED_SUCCESS
);
4587 memcached_return_t rc
;
4590 test_literal_param(__func__
),
4593 test_compare(rc
, MEMCACHED_NOTFOUND
);
4595 memcached_free(memc
);
4597 return TEST_SUCCESS
;
4600 test_return_t
regression_bug_583031(memcached_st
*)
4602 memcached_st
*memc
= memcached_create(NULL
);
4604 test_compare(MEMCACHED_SUCCESS
, memcached_server_add(memc
, "10.2.3.4", 11211));
4606 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
4607 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
4608 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4609 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4610 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
4611 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
4613 memcached_return_t rc
;
4617 const char *value
= memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
4621 test_compare_got(MEMCACHED_TIMEOUT
, rc
, memcached_last_error_message(memc
));
4623 memcached_free(memc
);
4625 return TEST_SUCCESS
;
4628 test_return_t
regression_bug_581030(memcached_st
*)
4631 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
4632 test_false(local_stat
);
4634 memcached_stat_free(NULL
, NULL
);
4637 return TEST_SUCCESS
;
4640 #define regression_bug_655423_COUNT 6000
4641 test_return_t
regression_bug_655423(memcached_st
*memc
)
4643 memcached_st
*clone
= memcached_clone(NULL
, memc
);
4644 memc
= NULL
; // Just to make sure it is not used
4649 return TEST_SKIPPED
;
4652 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1));
4653 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1));
4654 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1));
4655 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
, 1));
4657 memset(payload
, int('x'), sizeof(payload
));
4659 keys_st
keys(regression_bug_655423_COUNT
);
4661 for (size_t x
= 0; x
< keys
.size(); x
++)
4663 test_compare(MEMCACHED_SUCCESS
, memcached_set(clone
,
4666 payload
, sizeof(payload
), 0, 0));
4669 for (size_t x
= 0; x
< keys
.size(); x
++)
4671 size_t value_length
;
4672 memcached_return_t rc
;
4673 char *value
= memcached_get(clone
,
4676 &value_length
, NULL
, &rc
);
4678 if (rc
== MEMCACHED_NOTFOUND
)
4681 test_zero(value_length
);
4685 test_compare(MEMCACHED_SUCCESS
, rc
);
4687 test_compare(100LLU, value_length
);
4691 test_compare(MEMCACHED_SUCCESS
,
4692 memcached_mget(clone
,
4693 keys
.keys_ptr(), keys
.lengths_ptr(),
4697 memcached_result_st
*result
= NULL
;
4698 while ((result
= memcached_fetch_result(clone
, result
, NULL
)))
4700 test_compare(size_t(100), memcached_result_length(result
));
4704 test_true(count
> 100); // If we don't get back atleast this, something is up
4706 memcached_free(clone
);
4708 return TEST_SUCCESS
;
4712 * Test that ensures that buffered set to not trigger problems during io_flush
4714 #define regression_bug_490520_COUNT 200480
4715 test_return_t
regression_bug_490520(memcached_st
*original_memc
)
4717 memcached_st
* memc
= create_single_instance_memcached(original_memc
, NULL
);
4719 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
,1);
4720 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
,1);
4721 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
4722 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
,1);
4723 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
4725 /* First add all of the items.. */
4726 char blob
[3333] = {0};
4727 for (uint32_t x
= 0; x
< regression_bug_490520_COUNT
; ++x
)
4730 int key_length
= snprintf(key
, sizeof(key
), "0200%u", x
);
4732 memcached_return rc
= memcached_set(memc
, key
, key_length
, blob
, sizeof(blob
), 0, 0);
4733 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_last_error_message(memc
));
4736 memcached_free(memc
);
4738 return TEST_SUCCESS
;
4741 test_return_t
regression_1009493_TEST(memcached_st
*)
4743 memcached_st
* memc
= memcached_create(NULL
);
4745 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, true));
4747 memcached_st
* clone
= memcached_clone(NULL
, memc
);
4750 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
),
4751 memcached_behavior_get(clone
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
));
4753 memcached_free(memc
);
4754 memcached_free(clone
);
4756 return TEST_SUCCESS
;
4759 test_return_t
regression_994772_TEST(memcached_st
* memc
)
4761 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1));
4763 test_compare(MEMCACHED_SUCCESS
,
4765 test_literal_param(__func__
), // Key
4766 test_literal_param(__func__
), // Value
4767 time_t(0), uint32_t(0)));
4769 const char *keys
[] = { __func__
};
4770 size_t key_length
[]= { strlen(__func__
) };
4771 test_compare(MEMCACHED_SUCCESS
,
4772 memcached_mget(memc
, keys
, key_length
, 1));
4774 memcached_return_t rc
;
4775 memcached_result_st
*results
= memcached_fetch_result(memc
, NULL
, &rc
);
4777 test_compare(MEMCACHED_SUCCESS
, rc
);
4779 test_strcmp(__func__
, memcached_result_value(results
));
4780 uint64_t cas_value
= memcached_result_cas(results
);
4781 test_true(cas_value
);
4783 char* take_value
= memcached_result_take_value(results
);
4784 test_strcmp(__func__
, take_value
);
4787 memcached_result_free(results
);
4789 // Bad cas value, sanity check
4790 test_true(cas_value
!= 9999);
4791 test_compare(MEMCACHED_END
,
4793 test_literal_param(__func__
), // Key
4794 test_literal_param(__FILE__
), // Value
4795 time_t(0), uint32_t(0), 9999));
4797 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
,
4798 "different", strlen("different"), // Key
4799 test_literal_param(__FILE__
), // Value
4800 time_t(0), uint32_t(0)));
4802 return TEST_SUCCESS
;
4805 test_return_t
regression_bug_854604(memcached_st
*)
4809 test_compare(MEMCACHED_INVALID_ARGUMENTS
, libmemcached_check_configuration(0, 0, buffer
, 0));
4811 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, 0));
4813 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, 1));
4814 test_compare(buffer
[0], 0);
4816 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, 10));
4817 test_true(strlen(buffer
));
4819 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, sizeof(buffer
)));
4820 test_true(strlen(buffer
));
4822 return TEST_SUCCESS
;
4825 static void die_message(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
4827 fprintf(stderr
, "Iteration #%u: ", it
);
4829 if (error
== MEMCACHED_ERRNO
)
4831 fprintf(stderr
, "system error %d from %s: %s\n",
4832 errno
, what
, strerror(errno
));
4836 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
4837 memcached_strerror(mc
, error
));
4841 #define TEST_CONSTANT_CREATION 200
4843 test_return_t
regression_bug_(memcached_st
*memc
)
4845 const char *remote_server
;
4848 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
4850 return TEST_SKIPPED
;
4853 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
4855 memcached_st
* mc
= memcached_create(NULL
);
4856 memcached_return rc
;
4858 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
4859 if (rc
!= MEMCACHED_SUCCESS
)
4861 die_message(mc
, rc
, "memcached_behavior_set", x
);
4864 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
4865 if (rc
!= MEMCACHED_SUCCESS
)
4867 die_message(mc
, rc
, "memcached_behavior_set", x
);
4870 rc
= memcached_server_add(mc
, remote_server
, 0);
4871 if (rc
!= MEMCACHED_SUCCESS
)
4873 die_message(mc
, rc
, "memcached_server_add", x
);
4876 const char *set_key
= "akey";
4877 const size_t set_key_len
= strlen(set_key
);
4878 const char *set_value
= "a value";
4879 const size_t set_value_len
= strlen(set_value
);
4881 if (rc
== MEMCACHED_SUCCESS
)
4885 size_t get_value_len
;
4887 uint32_t get_value_flags
;
4889 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
4890 &get_value_flags
, &rc
);
4891 if (rc
!= MEMCACHED_SUCCESS
)
4893 die_message(mc
, rc
, "memcached_get", x
);
4899 (get_value_len
!= set_value_len
4900 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
4902 fprintf(stderr
, "Values don't match?\n");
4903 rc
= MEMCACHED_FAILURE
;
4909 rc
= memcached_set(mc
,
4910 set_key
, set_key_len
,
4911 set_value
, set_value_len
,
4915 if (rc
!= MEMCACHED_SUCCESS
)
4917 die_message(mc
, rc
, "memcached_set", x
);
4924 if (rc
!= MEMCACHED_SUCCESS
)
4930 return TEST_SUCCESS
;
4933 test_return_t
kill_HUP_TEST(memcached_st
*original_memc
)
4935 memcached_st
*memc
= create_single_instance_memcached(original_memc
, 0);
4938 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
4941 test_true((pid
= libmemcached_util_getpid(memcached_server_name(instance
),
4942 memcached_server_port(instance
), NULL
)) > -1);
4945 test_compare(MEMCACHED_SUCCESS
,
4947 test_literal_param(__func__
), // Keys
4948 test_literal_param(__func__
), // Values
4950 test_true_got(kill(pid
, SIGHUP
) == 0, strerror(errno
));
4952 memcached_return_t ret
= memcached_set(memc
,
4953 test_literal_param(__func__
), // Keys
4954 test_literal_param(__func__
), // Values
4956 test_compare(ret
, MEMCACHED_CONNECTION_FAILURE
);
4958 memcached_free(memc
);
4960 return TEST_SUCCESS
;