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.
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>
59 #include <semaphore.h>
63 #include <sys/types.h>
68 #include <libtest/server.h>
70 #include "clients/generator.h"
72 #define SMALL_STRING_LEN 1024
74 #include <libtest/test.hpp>
76 using namespace libtest
;
78 #include <libmemcached/util.h>
80 #include "tests/hash_results.h"
82 #include "tests/libmemcached-1.0/callback_counter.h"
83 #include "tests/libmemcached-1.0/fetch_all_results.h"
84 #include "tests/libmemcached-1.0/mem_functions.h"
85 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
86 #include "tests/print.h"
87 #include "tests/debug.h"
89 #define UUID_STRING_MAXLENGTH 36
95 init(arg
, UUID_STRING_MAXLENGTH
);
98 keys_st(size_t arg
, size_t padding
)
103 void init(size_t arg
, size_t padding
)
105 _lengths
.resize(arg
);
108 for (size_t x
= 0; x
< _keys
.size(); x
++)
110 libtest::vchar_t key_buffer
;
111 key_buffer
.resize(padding
+1);
112 memset(&key_buffer
[0], 'x', padding
);
116 #if defined(HAVE_LIBUUID) && HAVE_LIBUUID
120 uuid_unparse(out
, &key_buffer
[0]);
121 _keys
[x
]= strdup(&key_buffer
[0]);
122 (_keys
[x
])[UUID_STRING_MAXLENGTH
]= 'x';
125 else // We just use a number and pad the string if UUID is not available
127 char int_buffer
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
128 int key_length
= snprintf(int_buffer
, sizeof(int_buffer
), "%u", uint32_t(x
));
129 memcpy(&key_buffer
[0], int_buffer
, key_length
);
130 _keys
[x
]= strdup(&key_buffer
[0]);
132 _lengths
[x
]= padding
;
138 for (libtest::vchar_ptr_t::iterator iter
= _keys
.begin();
146 libtest::vchar_ptr_t::iterator
begin()
148 return _keys
.begin();
151 libtest::vchar_ptr_t::iterator
end()
161 std::vector
<size_t>& lengths()
166 libtest::vchar_ptr_t
& keys()
171 size_t* lengths_ptr()
181 char* key_at(size_t arg
)
186 size_t length_at(size_t arg
)
188 return _lengths
[arg
];
192 libtest::vchar_ptr_t _keys
;
193 std::vector
<size_t> _lengths
;
196 static memcached_return_t
return_value_based_on_buffering(memcached_st
*memc
)
198 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
))
200 return MEMCACHED_BUFFERED
;
203 return MEMCACHED_SUCCESS
;
206 static memcached_st
* create_single_instance_memcached(const memcached_st
*original_memc
, const char *options
)
209 * I only want to hit _one_ server so I know the number of requests I'm
210 * sending in the pipeline.
212 memcached_server_instance_st instance
= memcached_server_instance_by_position(original_memc
, 0);
214 char server_string
[1024];
215 int server_string_length
;
216 if (instance
->type
== MEMCACHED_CONNECTION_UNIX_SOCKET
)
220 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--SOCKET=\"%s\" %s",
221 memcached_server_name(instance
), options
);
225 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--SOCKET=\"%s\"",
226 memcached_server_name(instance
));
233 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--server=%s:%d %s",
234 memcached_server_name(instance
), int(memcached_server_port(instance
)),
239 server_string_length
= snprintf(server_string
, sizeof(server_string
), "--server=%s:%d",
240 memcached_server_name(instance
), int(memcached_server_port(instance
)));
244 if (server_string_length
<= 0)
250 if (memcached_failed(libmemcached_check_configuration(server_string
, server_string_length
, buffer
, sizeof(buffer
))))
252 Error
<< "Failed to parse (" << server_string
<< ") " << buffer
;
256 return memcached(server_string
, server_string_length
);
260 test_return_t
init_test(memcached_st
*not_used
)
265 (void)memcached_create(&memc
);
266 memcached_free(&memc
);
271 #define TEST_PORT_COUNT 7
272 in_port_t test_ports
[TEST_PORT_COUNT
];
274 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
275 const memcached_server_st
*server
,
279 size_t bigger
= *((size_t *)(context
));
281 assert(bigger
<= memcached_server_port(server
));
282 *((size_t *)(context
))= memcached_server_port(server
);
284 return MEMCACHED_SUCCESS
;
287 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
288 const memcached_server_st
*instance
,
292 FILE *stream
= (FILE *)context
;
295 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
296 memcached_server_name(instance
),
297 memcached_server_port(instance
),
298 instance
->major_version
,
299 instance
->minor_version
,
300 instance
->micro_version
);
302 return MEMCACHED_SUCCESS
;
305 test_return_t
server_sort_test(memcached_st
*ptr
)
307 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
309 memcached_return_t rc
;
310 memcached_server_fn callbacks
[1];
311 memcached_st
*local_memc
;
314 local_memc
= memcached_create(NULL
);
315 test_true(local_memc
);
316 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
318 for (uint32_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
320 test_ports
[x
]= (in_port_t
)random() % 64000;
321 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
322 test_compare(memcached_server_count(local_memc
), x
+1);
324 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
326 test_compare(MEMCACHED_SUCCESS
, rc
);
329 callbacks
[0]= server_display_function
;
330 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
333 memcached_free(local_memc
);
338 test_return_t
server_sort2_test(memcached_st
*ptr
)
340 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
341 memcached_server_fn callbacks
[1];
342 memcached_st
*local_memc
;
343 memcached_server_instance_st instance
;
346 local_memc
= memcached_create(NULL
);
347 test_true(local_memc
);
348 test_compare(MEMCACHED_SUCCESS
,
349 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1));
351 test_compare(MEMCACHED_SUCCESS
,
352 memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0));
353 instance
= memcached_server_instance_by_position(local_memc
, 0);
354 test_compare(in_port_t(43043), memcached_server_port(instance
));
356 test_compare(MEMCACHED_SUCCESS
,
357 memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0));
359 instance
= memcached_server_instance_by_position(local_memc
, 0);
360 test_compare(in_port_t(43042), memcached_server_port(instance
));
362 instance
= memcached_server_instance_by_position(local_memc
, 1);
363 test_compare(in_port_t(43043), memcached_server_port(instance
));
365 callbacks
[0]= server_display_function
;
366 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
369 memcached_free(local_memc
);
374 test_return_t
memcached_server_remove_test(memcached_st
*)
376 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";
379 test_compare(MEMCACHED_SUCCESS
,
380 libmemcached_check_configuration(server_string
, strlen(server_string
), buffer
, sizeof(buffer
)));
381 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
384 memcached_server_fn callbacks
[1];
385 callbacks
[0]= server_print_callback
;
386 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
388 memcached_free(memc
);
393 static memcached_return_t
server_display_unsort_function(const memcached_st
*,
394 const memcached_server_st
*server
,
398 uint32_t x
= *((uint32_t *)(context
));
400 if (! (test_ports
[x
] == server
->port
))
402 fprintf(stderr
, "%lu -> %lu\n", (unsigned long)test_ports
[x
], (unsigned long)server
->port
);
403 return MEMCACHED_FAILURE
;
406 *((uint32_t *)(context
))= ++x
;
408 return MEMCACHED_SUCCESS
;
411 test_return_t
server_unsort_test(memcached_st
*ptr
)
413 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
414 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
415 memcached_server_fn callbacks
[1];
416 memcached_st
*local_memc
;
419 local_memc
= memcached_create(NULL
);
420 test_true(local_memc
);
422 for (uint32_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
424 test_ports
[x
]= (in_port_t
)(random() % 64000);
425 test_compare(MEMCACHED_SUCCESS
,
426 memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0));
427 test_compare(memcached_server_count(local_memc
), x
+1);
429 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
433 callbacks
[0]= server_display_unsort_function
;
434 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
436 /* Now we sort old data! */
437 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
438 callbacks
[0]= server_display_function
;
439 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
442 memcached_free(local_memc
);
447 test_return_t
allocation_test(memcached_st
*not_used
)
451 memc
= memcached_create(NULL
);
453 memcached_free(memc
);
458 test_return_t
clone_test(memcached_st
*memc
)
462 memcached_st
*memc_clone
;
463 memc_clone
= memcached_clone(NULL
, NULL
);
464 test_true(memc_clone
);
465 memcached_free(memc_clone
);
468 /* Can we init from null? */
470 memcached_st
*memc_clone
;
471 memc_clone
= memcached_clone(NULL
, memc
);
472 test_true(memc_clone
);
475 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
476 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
477 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
478 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
481 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
482 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
483 test_true(memc_clone
->distribution
== memc
->distribution
);
484 { // Test all of the flags
485 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
486 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
487 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
488 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
489 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
490 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
491 test_true(memc_clone
->ketama
.weighted
== memc
->ketama
.weighted
);
492 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
493 test_true(memc_clone
->flags
.hash_with_namespace
== memc
->flags
.hash_with_namespace
);
494 test_true(memc_clone
->flags
.reply
== memc
->flags
.reply
);
495 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
496 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
497 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
499 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
500 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
501 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
502 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
503 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
504 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
505 test_true(memc_clone
->on_clone
== memc
->on_clone
);
506 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
507 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
508 test_true(memc_clone
->recv_size
== memc
->recv_size
);
509 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
510 test_true(memc_clone
->send_size
== memc
->send_size
);
511 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
512 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
513 test_true(memc_clone
->user_data
== memc
->user_data
);
515 memcached_free(memc_clone
);
518 /* Can we init from struct? */
520 memcached_st declared_clone
;
521 memcached_st
*memc_clone
;
522 memset(&declared_clone
, 0 , sizeof(memcached_st
));
523 memc_clone
= memcached_clone(&declared_clone
, NULL
);
524 test_true(memc_clone
);
525 memcached_free(memc_clone
);
528 /* Can we init from struct? */
530 memcached_st declared_clone
;
531 memcached_st
*memc_clone
;
532 memset(&declared_clone
, 0 , sizeof(memcached_st
));
533 memc_clone
= memcached_clone(&declared_clone
, memc
);
534 test_true(memc_clone
);
535 memcached_free(memc_clone
);
541 test_return_t
userdata_test(memcached_st
*memc
)
544 test_false(memcached_set_user_data(memc
, foo
));
545 test_true(memcached_get_user_data(memc
) == foo
);
546 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
551 test_return_t
connection_test(memcached_st
*memc
)
553 test_compare(MEMCACHED_SUCCESS
,
554 memcached_server_add_with_weight(memc
, "localhost", 0, 0));
559 test_return_t
libmemcached_string_behavior_test(memcached_st
*)
561 for (int x
= MEMCACHED_BEHAVIOR_NO_BLOCK
; x
< int(MEMCACHED_BEHAVIOR_MAX
); ++x
)
563 test_true(libmemcached_string_behavior(memcached_behavior_t(x
)));
565 test_compare(37, int(MEMCACHED_BEHAVIOR_MAX
));
570 test_return_t
libmemcached_string_distribution_test(memcached_st
*)
572 for (int x
= MEMCACHED_DISTRIBUTION_MODULA
; x
< int(MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
); ++x
)
574 test_true(libmemcached_string_distribution(memcached_server_distribution_t(x
)));
576 test_compare(7, int(MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
));
581 test_return_t
memcached_return_t_TEST(memcached_st
*memc
)
583 uint32_t values
[] = { 851992627U, 2337886783U, 4109241422U, 4001849190U,
584 982370485U, 1263635348U, 4242906218U, 3829656100U,
585 1891735253U, 334139633U, 2257084983U, 3351789013U,
586 13199785U, 2542027183U, 1097051614U, 199566778U,
587 2748246961U, 2465192557U, 1664094137U, 2405439045U,
588 1842224848U, 692413798U, 3479807801U, 919913813U,
589 4269430871U, 610793021U, 527273862U, 1437122909U,
590 2300930706U, 2943759320U, 674306647U, 2400528935U,
591 54481931U, 4186304426U, 1741088401U, 2979625118U,
592 4159057246U, 3425930182U, 2593724503U, 1868899624U,
593 1769812374U, 2302537950U, 1110330676U, 3365377466U,
594 1336171666U, 3021258493U, 2334992265U, 3861994737U,
595 3582734124U, 3365377466U };
597 // You have updated the memcache_error messages but not updated docs/tests.
598 for (int rc
= int(MEMCACHED_SUCCESS
); rc
< int(MEMCACHED_MAXIMUM_RETURN
); ++rc
)
601 const char *msg
= memcached_strerror(memc
, memcached_return_t(rc
));
602 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
603 MEMCACHED_HASH_JENKINS
);
604 if (values
[rc
] != hash_val
)
606 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the memcached_return_t_TEST\n");
607 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, memcached_return_t(rc
)), hash_val
);
609 test_compare(values
[rc
], hash_val
);
611 test_compare(49, int(MEMCACHED_MAXIMUM_RETURN
));
616 test_return_t
set_test(memcached_st
*memc
)
618 memcached_return_t rc
= memcached_set(memc
,
619 test_literal_param("foo"),
620 test_literal_param("when we sanitize"),
621 time_t(0), (uint32_t)0);
622 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
627 test_return_t
append_test(memcached_st
*memc
)
629 memcached_return_t rc
;
630 const char *in_value
= "we";
634 test_compare(MEMCACHED_SUCCESS
,
635 memcached_flush(memc
, 0));
637 test_compare(MEMCACHED_SUCCESS
,
639 test_literal_param(__func__
),
640 in_value
, strlen(in_value
),
641 time_t(0), uint32_t(0)));
643 test_compare(MEMCACHED_SUCCESS
,
644 memcached_append(memc
,
645 test_literal_param(__func__
),
646 " the", strlen(" the"),
647 time_t(0), uint32_t(0)));
649 test_compare(MEMCACHED_SUCCESS
,
650 memcached_append(memc
,
651 test_literal_param(__func__
),
652 " people", strlen(" people"),
653 time_t(0), uint32_t(0)));
655 char *out_value
= memcached_get(memc
,
656 test_literal_param(__func__
),
657 &value_length
, &flags
, &rc
);
658 test_memcmp(out_value
, "we the people", strlen("we the people"));
659 test_compare(strlen("we the people"), value_length
);
660 test_compare(MEMCACHED_SUCCESS
, rc
);
666 test_return_t
append_binary_test(memcached_st
*memc
)
668 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
670 test_compare(MEMCACHED_SUCCESS
,
671 memcached_flush(memc
, 0));
673 test_compare(MEMCACHED_SUCCESS
,
675 test_literal_param(__func__
),
677 time_t(0), uint32_t(0)));
680 for (uint32_t x
= 0; store_list
[x
] ; x
++)
682 test_compare(MEMCACHED_SUCCESS
,
683 memcached_append(memc
,
684 test_literal_param(__func__
),
685 (char *)&store_list
[x
], sizeof(uint32_t),
686 time_t(0), uint32_t(0)));
692 memcached_return_t rc
;
693 uint32_t *value
= (uint32_t *)memcached_get(memc
,
694 test_literal_param(__func__
),
695 &value_length
, &flags
, &rc
);
696 test_compare(value_length
, sizeof(uint32_t) * count
);
697 test_compare(MEMCACHED_SUCCESS
, rc
);
699 for (uint32_t counter
= count
, *ptr
= value
; counter
; counter
--)
701 test_compare(*ptr
, store_list
[count
- counter
]);
709 test_return_t
memcached_mget_mixed_memcached_get_TEST(memcached_st
*memc
)
713 for (libtest::vchar_ptr_t::iterator iter
= keys
.begin();
717 test_compare(MEMCACHED_SUCCESS
,
721 time_t(0), uint32_t(0)));
724 for (ptrdiff_t loop
= 0; loop
< 20; loop
++)
728 test_compare(MEMCACHED_SUCCESS
,
729 memcached_mget(memc
, keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
731 memcached_result_st
*results
= memcached_result_create(memc
, NULL
);
734 size_t result_count
= 0;
735 memcached_return_t rc
;
736 while (memcached_fetch_result(memc
, results
, &rc
))
740 test_compare(keys
.size(), result_count
);
744 int which_key
= random() %keys
.size();
747 memcached_return_t rc
;
748 char *out_value
= memcached_get(memc
, keys
.key_at(which_key
), keys
.length_at(which_key
),
749 &value_length
, &flags
, &rc
);
750 test_compare(MEMCACHED_SUCCESS
, rc
);
751 test_null(out_value
);
752 test_zero(value_length
);
760 test_return_t
cas2_test(memcached_st
*memc
)
762 const char *keys
[]= {"fudge", "son", "food"};
763 size_t key_length
[]= {5, 3, 4};
764 const char *value
= "we the people";
765 size_t value_length
= strlen("we the people");
767 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0));
769 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
771 for (uint32_t x
= 0; x
< 3; x
++)
773 test_compare(MEMCACHED_SUCCESS
,
774 memcached_set(memc
, keys
[x
], key_length
[x
],
775 keys
[x
], key_length
[x
],
776 time_t(50), uint32_t(9)));
779 test_compare(MEMCACHED_SUCCESS
,
780 memcached_mget(memc
, keys
, key_length
, 3));
782 memcached_result_st
*results
= memcached_result_create(memc
, NULL
);
785 memcached_return_t rc
;
786 results
= memcached_fetch_result(memc
, results
, &rc
);
788 test_true(results
->item_cas
);
789 test_compare(MEMCACHED_SUCCESS
, rc
);
790 test_true(memcached_result_cas(results
));
792 test_memcmp(value
, "we the people", strlen("we the people"));
793 test_compare(strlen("we the people"), value_length
);
794 test_compare(MEMCACHED_SUCCESS
, rc
);
796 memcached_result_free(results
);
801 test_return_t
cas_test(memcached_st
*memc
)
803 const char* keys
[2] = { __func__
, NULL
};
804 size_t keylengths
[2] = { strlen(__func__
), 0 };
806 memcached_result_st results_obj
;
808 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0));
810 test_skip(true, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
812 test_compare(MEMCACHED_SUCCESS
,
814 test_literal_param(__func__
),
815 test_literal_param("we the people"),
816 (time_t)0, (uint32_t)0));
818 test_compare(MEMCACHED_SUCCESS
,
819 memcached_mget(memc
, keys
, keylengths
, 1));
821 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
824 memcached_return_t rc
;
825 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
827 test_compare(MEMCACHED_SUCCESS
, rc
);
828 test_true(memcached_result_cas(results
));
829 test_memcmp("we the people", memcached_result_value(results
), test_literal_param_size("we the people"));
830 test_compare(test_literal_param_size("we the people"),
831 strlen(memcached_result_value(results
)));
833 uint64_t cas
= memcached_result_cas(results
);
836 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
837 test_true(rc
== MEMCACHED_END
);
838 test_true(results
== NULL
);
841 test_compare(MEMCACHED_SUCCESS
,
843 test_literal_param(__func__
),
844 test_literal_param("change the value"),
848 * The item will have a new cas value, so try to set it again with the old
849 * value. This should fail!
851 test_compare(MEMCACHED_DATA_EXISTS
,
853 test_literal_param(__func__
),
854 test_literal_param("change the value"),
857 memcached_result_free(&results_obj
);
863 test_return_t
prepend_test(memcached_st
*memc
)
865 const char *key
= "fig";
866 const char *value
= "people";
868 test_compare(MEMCACHED_SUCCESS
,
869 memcached_flush(memc
, 0));
871 test_compare(MEMCACHED_SUCCESS
,
872 memcached_set(memc
, key
, strlen(key
),
873 value
, strlen(value
),
874 time_t(0), uint32_t(0)));
876 test_compare(MEMCACHED_SUCCESS
,
877 memcached_prepend(memc
, key
, strlen(key
),
878 "the ", strlen("the "),
879 time_t(0), uint32_t(0)));
881 test_compare(MEMCACHED_SUCCESS
,
882 memcached_prepend(memc
, key
, strlen(key
),
883 "we ", strlen("we "),
884 time_t(0), uint32_t(0)));
888 memcached_return_t rc
;
889 char *out_value
= memcached_get(memc
, key
, strlen(key
),
890 &value_length
, &flags
, &rc
);
891 test_memcmp(out_value
, "we the people", strlen("we the people"));
892 test_compare(strlen("we the people"), value_length
);
893 test_compare(MEMCACHED_SUCCESS
, rc
);
900 Set the value, then quit to make sure it is flushed.
901 Come back in and test that add fails.
903 test_return_t
add_test(memcached_st
*memc
)
905 test_compare_hint(return_value_based_on_buffering(memc
),
907 test_literal_param(__func__
),
908 test_literal_param("when we sanitize"),
909 time_t(0), uint32_t(0)),
910 memcached_last_error_message(memc
));
912 memcached_quit(memc
);
914 test_compare_hint(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_DATA_EXISTS
: MEMCACHED_NOTSTORED
,
916 test_literal_param(__func__
),
917 test_literal_param("try something else"),
918 time_t(0), uint32_t(0)),
919 memcached_last_error_message(memc
));
925 ** There was a problem of leaking filedescriptors in the initial release
926 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
927 ** systems it seems that the kernel is slow on reclaiming the resources
928 ** because the connects starts to time out (the test doesn't do much
929 ** anyway, so just loop 10 iterations)
931 test_return_t
add_wrapper(memcached_st
*memc
)
933 unsigned int max
= 10000;
941 for (uint32_t x
= 0; x
< max
; x
++)
947 test_return_t
replace_test(memcached_st
*memc
)
949 test_compare(return_value_based_on_buffering(memc
),
951 test_literal_param(__func__
),
952 test_literal_param("when we sanitize"),
953 time_t(0), uint32_t(0)));
955 test_compare(MEMCACHED_SUCCESS
,
956 memcached_replace(memc
,
957 test_literal_param(__func__
),
958 test_literal_param("first we insert some data"),
959 time_t(0), uint32_t(0)));
964 test_return_t
delete_test(memcached_st
*memc
)
966 test_compare(return_value_based_on_buffering(memc
),
968 test_literal_param(__func__
),
969 test_literal_param("when we sanitize"),
970 time_t(0), uint32_t(0)));
972 test_compare_hint(return_value_based_on_buffering(memc
),
973 memcached_delete(memc
,
974 test_literal_param(__func__
),
976 memcached_last_error_message(memc
));
981 test_return_t
flush_test(memcached_st
*memc
)
983 uint64_t query_id
= memcached_query_id(memc
);
984 test_compare(MEMCACHED_SUCCESS
,
985 memcached_flush(memc
, 0));
986 test_compare(query_id
+1, memcached_query_id(memc
));
991 static memcached_return_t
server_function(const memcached_st
*,
992 const memcached_server_st
*,
996 return MEMCACHED_SUCCESS
;
999 test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
1002 strncpy(context
, "foo bad", sizeof(context
));
1003 memcached_server_fn callbacks
[1];
1005 callbacks
[0]= server_function
;
1006 memcached_server_cursor(memc
, callbacks
, context
, 1);
1007 return TEST_SUCCESS
;
1010 test_return_t
bad_key_test(memcached_st
*memc
)
1012 memcached_return_t rc
;
1013 const char *key
= "foo bad";
1016 uint64_t query_id
= memcached_query_id(memc
);
1018 // Just skip if we are in binary mode.
1019 test_skip(false, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1021 test_compare(query_id
, memcached_query_id(memc
)); // We should not increase the query_id for memcached_behavior_get()
1023 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
1024 test_true(memc_clone
);
1026 query_id
= memcached_query_id(memc_clone
);
1027 test_compare(MEMCACHED_SUCCESS
,
1028 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, true));
1029 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
1031 /* All keys are valid in the binary protocol (except for length) */
1032 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == false)
1034 uint64_t before_query_id
= memcached_query_id(memc_clone
);
1036 size_t string_length
;
1037 char *string
= memcached_get(memc_clone
, key
, strlen(key
),
1038 &string_length
, &flags
, &rc
);
1039 test_compare(MEMCACHED_BAD_KEY_PROVIDED
, rc
);
1040 test_zero(string_length
);
1043 test_compare(before_query_id
+1, memcached_query_id(memc_clone
));
1045 query_id
= memcached_query_id(memc_clone
);
1046 test_compare(MEMCACHED_SUCCESS
,
1047 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, false));
1048 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
1050 size_t string_length
;
1051 char *string
= memcached_get(memc_clone
, key
, strlen(key
),
1052 &string_length
, &flags
, &rc
);
1053 test_compare_got(MEMCACHED_NOTFOUND
, rc
, memcached_strerror(NULL
, rc
));
1054 test_zero(string_length
);
1058 /* Test multi key for bad keys */
1059 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
1060 size_t key_lengths
[] = { 7, 7, 7 };
1061 query_id
= memcached_query_id(memc_clone
);
1062 test_compare(MEMCACHED_SUCCESS
,
1063 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, true));
1064 test_compare(query_id
, memcached_query_id(memc_clone
));
1066 query_id
= memcached_query_id(memc_clone
);
1067 test_compare(MEMCACHED_BAD_KEY_PROVIDED
,
1068 memcached_mget(memc_clone
, keys
, key_lengths
, 3));
1069 test_compare(query_id
+1, memcached_query_id(memc_clone
));
1071 query_id
= memcached_query_id(memc_clone
);
1072 // Grouping keys are not required to follow normal key behaviors
1073 test_compare(MEMCACHED_SUCCESS
,
1074 memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1));
1075 test_compare(query_id
+1, memcached_query_id(memc_clone
));
1077 /* The following test should be moved to the end of this function when the
1078 memcached server is updated to allow max size length of the keys in the
1081 test_compare(MEMCACHED_SUCCESS
,
1082 memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_NAMESPACE
, NULL
));
1084 libtest::vchar_t longkey
;
1086 libtest::vchar_t::iterator it
= longkey
.begin();
1087 longkey
.insert(it
, MEMCACHED_MAX_KEY
, 'a');
1090 test_compare(longkey
.size(), size_t(MEMCACHED_MAX_KEY
));
1092 size_t string_length
;
1094 test_null(memcached_get(memc_clone
, &longkey
[0], longkey
.size() -1, &string_length
, &flags
, &rc
));
1095 test_compare(MEMCACHED_NOTFOUND
, rc
);
1096 test_zero(string_length
);
1098 test_null(memcached_get(memc_clone
, &longkey
[0], longkey
.size(), &string_length
, &flags
, &rc
));
1099 test_compare(MEMCACHED_BAD_KEY_PROVIDED
, rc
);
1100 test_zero(string_length
);
1104 /* Make sure zero length keys are marked as bad */
1106 test_compare(MEMCACHED_SUCCESS
,
1107 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, true));
1108 size_t string_length
;
1109 char *string
= memcached_get(memc_clone
, key
, 0,
1110 &string_length
, &flags
, &rc
);
1111 test_compare(MEMCACHED_BAD_KEY_PROVIDED
, rc
);
1112 test_zero(string_length
);
1116 memcached_free(memc_clone
);
1118 return TEST_SUCCESS
;
1121 #define READ_THROUGH_VALUE "set for me"
1122 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
1125 memcached_result_st
*result
)
1127 (void)memc
;(void)key
;(void)key_length
;
1128 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
1131 #ifndef __INTEL_COMPILER
1132 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
1135 test_return_t
read_through(memcached_st
*memc
)
1137 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
1139 size_t string_length
;
1141 memcached_return_t rc
;
1142 char *string
= memcached_get(memc
,
1143 test_literal_param(__func__
),
1144 &string_length
, &flags
, &rc
);
1146 test_compare(MEMCACHED_NOTFOUND
, rc
);
1147 test_false(string_length
);
1150 test_compare(MEMCACHED_SUCCESS
,
1151 memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, *(void **)&cb
));
1153 string
= memcached_get(memc
,
1154 test_literal_param(__func__
),
1155 &string_length
, &flags
, &rc
);
1157 test_compare(MEMCACHED_SUCCESS
, rc
);
1158 test_compare(string_length
, sizeof(READ_THROUGH_VALUE
) -1);
1159 test_true(string
[sizeof(READ_THROUGH_VALUE
) -1] == 0);
1160 test_strcmp(READ_THROUGH_VALUE
, string
);
1163 string
= memcached_get(memc
,
1164 test_literal_param(__func__
),
1165 &string_length
, &flags
, &rc
);
1167 test_compare(MEMCACHED_SUCCESS
, rc
);
1169 test_compare(string_length
, sizeof(READ_THROUGH_VALUE
) -1);
1170 test_true(string
[sizeof(READ_THROUGH_VALUE
) -1] == 0);
1171 test_strcmp(READ_THROUGH_VALUE
, string
);
1174 return TEST_SUCCESS
;
1177 test_return_t
get_test(memcached_st
*memc
)
1179 memcached_return_t rc
;
1181 size_t string_length
;
1184 uint64_t query_id
= memcached_query_id(memc
);
1185 rc
= memcached_delete(memc
,
1186 test_literal_param(__func__
),
1188 test_true_got(rc
== MEMCACHED_BUFFERED
or rc
== MEMCACHED_NOTFOUND
, memcached_last_error_message(memc
));
1189 test_compare(query_id
+1, memcached_query_id(memc
));
1191 string
= memcached_get(memc
,
1192 test_literal_param(__func__
),
1193 &string_length
, &flags
, &rc
);
1195 test_compare_got(MEMCACHED_NOTFOUND
, rc
, memcached_strerror(NULL
, rc
));
1196 test_false(string_length
);
1199 return TEST_SUCCESS
;
1202 test_return_t
get_test2(memcached_st
*memc
)
1204 const char *value
= "when we sanitize";
1206 uint64_t query_id
= memcached_query_id(memc
);
1207 test_compare(return_value_based_on_buffering(memc
),
1209 test_literal_param(__func__
),
1210 value
, strlen(value
),
1211 time_t(0), uint32_t(0)));
1212 test_compare(query_id
+1, memcached_query_id(memc
));
1214 query_id
= memcached_query_id(memc
);
1215 test_true(query_id
);
1218 size_t string_length
;
1219 memcached_return_t rc
;
1220 char *string
= memcached_get(memc
,
1221 test_literal_param(__func__
),
1222 &string_length
, &flags
, &rc
);
1223 test_compare(query_id
+1, memcached_query_id(memc
));
1225 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
1226 test_compare_got(MEMCACHED_SUCCESS
, memcached_last_error(memc
), memcached_last_error_message(memc
));
1228 test_compare(strlen(value
), string_length
);
1229 test_memcmp(string
, value
, string_length
);
1233 return TEST_SUCCESS
;
1236 test_return_t
set_test2(memcached_st
*memc
)
1238 for (uint32_t x
= 0; x
< 10; x
++)
1240 test_compare(return_value_based_on_buffering(memc
),
1242 test_literal_param("foo"),
1243 test_literal_param("train in the brain"),
1244 time_t(0), uint32_t(0)));
1247 return TEST_SUCCESS
;
1250 test_return_t
set_test3(memcached_st
*memc
)
1252 size_t value_length
= 8191;
1254 libtest::vchar_t value
;
1255 value
.reserve(value_length
);
1256 for (uint32_t x
= 0; x
< value_length
; x
++)
1258 value
.push_back(char(x
% 127));
1261 /* The dump test relies on there being at least 32 items in memcached */
1262 for (uint32_t x
= 0; x
< 32; x
++)
1266 snprintf(key
, sizeof(key
), "foo%u", x
);
1268 uint64_t query_id
= memcached_query_id(memc
);
1269 test_compare_hint(return_value_based_on_buffering(memc
),
1270 memcached_set(memc
, key
, strlen(key
),
1271 &value
[0], value
.size(),
1272 time_t(0), uint32_t(0)),
1273 memcached_last_error_message(memc
));
1274 test_compare(query_id
+1, memcached_query_id(memc
));
1277 return TEST_SUCCESS
;
1280 test_return_t
get_test3(memcached_st
*memc
)
1282 size_t value_length
= 8191;
1284 libtest::vchar_t value
;
1285 value
.reserve(value_length
);
1286 for (uint32_t x
= 0; x
< value_length
; x
++)
1288 value
.push_back(char(x
% 127));
1291 test_compare_hint(return_value_based_on_buffering(memc
),
1293 test_literal_param(__func__
),
1294 &value
[0], value
.size(),
1295 time_t(0), uint32_t(0)),
1296 memcached_last_error_message(memc
));
1298 size_t string_length
;
1300 memcached_return_t rc
;
1301 char *string
= memcached_get(memc
,
1302 test_literal_param(__func__
),
1303 &string_length
, &flags
, &rc
);
1305 test_compare(MEMCACHED_SUCCESS
, rc
);
1307 test_compare(value
.size(), string_length
);
1308 test_memcmp(string
, &value
[0], string_length
);
1312 return TEST_SUCCESS
;
1315 test_return_t
get_test4(memcached_st
*memc
)
1317 size_t value_length
= 8191;
1319 libtest::vchar_t value
;
1320 value
.reserve(value_length
);
1321 for (uint32_t x
= 0; x
< value_length
; x
++)
1323 value
.push_back(char(x
% 127));
1326 test_compare_hint(return_value_based_on_buffering(memc
),
1328 test_literal_param(__func__
),
1329 &value
[0], value
.size(),
1330 time_t(0), uint32_t(0)),
1331 memcached_last_error_message(memc
));
1333 for (uint32_t x
= 0; x
< 10; x
++)
1336 size_t string_length
;
1337 memcached_return_t rc
;
1338 char *string
= memcached_get(memc
,
1339 test_literal_param(__func__
),
1340 &string_length
, &flags
, &rc
);
1342 test_compare(MEMCACHED_SUCCESS
, rc
);
1344 test_compare(value
.size(), string_length
);
1345 test_memcmp(string
, &value
[0], string_length
);
1349 return TEST_SUCCESS
;
1353 * This test verifies that memcached_read_one_response doesn't try to
1354 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1355 * responses before you execute a storage command.
1357 test_return_t
get_test5(memcached_st
*memc
)
1360 ** Request the same key twice, to ensure that we hash to the same server
1361 ** (so that we have multiple response values queued up) ;-)
1363 const char *keys
[]= { "key", "key" };
1364 size_t lengths
[]= { 3, 3 };
1368 test_compare_hint(return_value_based_on_buffering(memc
),
1369 memcached_set(memc
, keys
[0], lengths
[0],
1370 keys
[0], lengths
[0],
1371 time_t(0), uint32_t(0)),
1372 memcached_last_error_message(memc
));
1373 test_compare(MEMCACHED_SUCCESS
, memcached_mget(memc
, keys
, lengths
, test_array_length(keys
)));
1375 memcached_result_st results_obj
;
1376 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
1379 memcached_return_t rc
;
1380 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
1383 memcached_result_free(&results_obj
);
1385 /* Don't read out the second result, but issue a set instead.. */
1386 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0));
1388 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1389 &rlen
, &flags
, &rc
);
1391 test_compare(MEMCACHED_NOTFOUND
, rc
);
1392 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1394 test_compare(MEMCACHED_SUCCESS
, rc
);
1397 return TEST_SUCCESS
;
1400 test_return_t
mget_end(memcached_st
*memc
)
1402 const char *keys
[]= { "foo", "foo2" };
1403 size_t lengths
[]= { 3, 4 };
1404 const char *values
[]= { "fjord", "41" };
1407 for (size_t x
= 0; x
< test_array_length(keys
); x
++)
1409 test_compare(MEMCACHED_SUCCESS
,
1411 keys
[x
], lengths
[x
],
1412 values
[x
], strlen(values
[x
]),
1413 time_t(0), uint32_t(0)));
1417 size_t string_length
;
1420 // retrieve both via mget
1421 test_compare(MEMCACHED_SUCCESS
,
1422 memcached_mget(memc
,
1424 test_array_length(keys
)));
1426 char key
[MEMCACHED_MAX_KEY
];
1428 memcached_return_t rc
;
1430 // this should get both
1431 for (size_t x
= 0; x
< test_array_length(keys
); x
++)
1433 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1435 test_compare(MEMCACHED_SUCCESS
, rc
);
1437 if (key_length
== 4)
1442 test_compare(string_length
, strlen(values
[val
]));
1443 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1447 // this should indicate end
1448 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1449 test_compare(MEMCACHED_END
, rc
);
1453 test_compare(MEMCACHED_SUCCESS
,
1454 memcached_mget(memc
, keys
, lengths
, 1));
1456 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1457 test_compare(key_length
, lengths
[0]);
1458 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1459 test_compare(string_length
, strlen(values
[0]));
1460 test_true(strncmp(values
[0], string
, string_length
) == 0);
1461 test_compare(MEMCACHED_SUCCESS
, rc
);
1464 // this should indicate end
1465 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1466 test_compare(MEMCACHED_END
, rc
);
1469 return TEST_SUCCESS
;
1472 /* Do not copy the style of this code, I just access hosts to testthis function */
1473 test_return_t
stats_servername_test(memcached_st
*memc
)
1475 memcached_stat_st memc_stat
;
1476 memcached_server_instance_st instance
=
1477 memcached_server_instance_by_position(memc
, 0);
1479 if (LIBMEMCACHED_WITH_SASL_SUPPORT
and memcached_get_sasl_callbacks(memc
))
1481 return TEST_SKIPPED
;
1484 test_compare(MEMCACHED_SUCCESS
, memcached_stat_servername(&memc_stat
, NULL
,
1485 memcached_server_name(instance
),
1486 memcached_server_port(instance
)));
1488 return TEST_SUCCESS
;
1491 test_return_t
increment_test(memcached_st
*memc
)
1493 uint64_t new_number
;
1495 test_compare(MEMCACHED_SUCCESS
,
1497 test_literal_param("number"),
1498 test_literal_param("0"),
1499 (time_t)0, (uint32_t)0));
1501 test_compare(MEMCACHED_SUCCESS
,
1502 memcached_increment(memc
, test_literal_param("number"), 1, &new_number
));
1503 test_compare(uint64_t(1), new_number
);
1505 test_compare(MEMCACHED_SUCCESS
,
1506 memcached_increment(memc
, test_literal_param("number"), 1, &new_number
));
1507 test_compare(uint64_t(2), new_number
);
1509 return TEST_SUCCESS
;
1512 test_return_t
increment_with_initial_test(memcached_st
*memc
)
1514 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1516 uint64_t new_number
;
1517 uint64_t initial
= 0;
1519 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1521 test_compare(MEMCACHED_SUCCESS
,
1522 memcached_increment_with_initial(memc
, test_literal_param("number"), 1, initial
, 0, &new_number
));
1523 test_compare(new_number
, initial
);
1525 test_compare(MEMCACHED_SUCCESS
,
1526 memcached_increment_with_initial(memc
, test_literal_param("number"), 1, initial
, 0, &new_number
));
1527 test_compare(new_number
, (initial
+1));
1529 return TEST_SUCCESS
;
1532 test_return_t
decrement_test(memcached_st
*memc
)
1534 test_compare(return_value_based_on_buffering(memc
),
1536 test_literal_param(__func__
),
1537 test_literal_param("3"),
1538 time_t(0), uint32_t(0)));
1539 // Make sure we flush the value we just set
1540 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1542 uint64_t new_number
;
1543 test_compare(MEMCACHED_SUCCESS
,
1544 memcached_decrement(memc
,
1545 test_literal_param(__func__
),
1547 test_compare(uint64_t(2), new_number
);
1549 test_compare(MEMCACHED_SUCCESS
,
1550 memcached_decrement(memc
,
1551 test_literal_param(__func__
),
1553 test_compare(uint64_t(1), new_number
);
1555 return TEST_SUCCESS
;
1558 test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1560 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1562 uint64_t initial
= 3;
1564 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1566 uint64_t new_number
;
1567 test_compare(MEMCACHED_SUCCESS
,
1568 memcached_decrement_with_initial(memc
,
1569 test_literal_param(__func__
),
1572 test_compare(new_number
, initial
);
1574 test_compare(MEMCACHED_SUCCESS
,
1575 memcached_decrement_with_initial(memc
,
1576 test_literal_param(__func__
),
1579 test_compare(new_number
, (initial
- 1));
1581 return TEST_SUCCESS
;
1584 test_return_t
increment_by_key_test(memcached_st
*memc
)
1586 const char *master_key
= "foo";
1587 const char *key
= "number";
1588 const char *value
= "0";
1590 test_compare(return_value_based_on_buffering(memc
),
1591 memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1593 value
, strlen(value
),
1594 time_t(0), uint32_t(0)));
1596 // Make sure we flush the value we just set
1597 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1599 uint64_t new_number
;
1600 test_compare(MEMCACHED_SUCCESS
,
1601 memcached_increment_by_key(memc
, master_key
, strlen(master_key
),
1602 key
, strlen(key
), 1, &new_number
));
1603 test_compare(uint64_t(1), new_number
);
1605 test_compare(MEMCACHED_SUCCESS
,
1606 memcached_increment_by_key(memc
, master_key
, strlen(master_key
),
1607 key
, strlen(key
), 1, &new_number
));
1608 test_compare(uint64_t(2), new_number
);
1610 return TEST_SUCCESS
;
1613 test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1615 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1617 uint64_t new_number
;
1618 const char *master_key
= "foo";
1619 const char *key
= "number";
1620 uint64_t initial
= 0;
1622 test_compare(MEMCACHED_SUCCESS
,
1623 memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1625 1, initial
, 0, &new_number
));
1626 test_compare(new_number
, initial
);
1628 test_compare(MEMCACHED_SUCCESS
,
1629 memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1631 1, initial
, 0, &new_number
));
1632 test_compare(new_number
, (initial
+1));
1634 return TEST_SUCCESS
;
1637 test_return_t
decrement_by_key_test(memcached_st
*memc
)
1639 uint64_t new_number
;
1640 const char *value
= "3";
1642 test_compare(return_value_based_on_buffering(memc
),
1643 memcached_set_by_key(memc
,
1644 test_literal_param("foo"),
1645 test_literal_param("number"),
1646 value
, strlen(value
),
1647 (time_t)0, (uint32_t)0));
1649 test_compare(MEMCACHED_SUCCESS
,
1650 memcached_decrement_by_key(memc
,
1651 test_literal_param("foo"),
1652 test_literal_param("number"),
1654 test_compare(uint64_t(2), new_number
);
1656 test_compare(MEMCACHED_SUCCESS
,
1657 memcached_decrement_by_key(memc
,
1658 test_literal_param("foo"),
1659 test_literal_param("number"),
1661 test_compare(uint64_t(1), new_number
);
1663 return TEST_SUCCESS
;
1666 test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1668 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1670 uint64_t new_number
;
1671 uint64_t initial
= 3;
1673 test_compare(MEMCACHED_SUCCESS
,
1674 memcached_decrement_with_initial_by_key(memc
,
1675 test_literal_param("foo"),
1676 test_literal_param("number"),
1677 1, initial
, 0, &new_number
));
1678 test_compare(new_number
, initial
);
1680 test_compare(MEMCACHED_SUCCESS
,
1681 memcached_decrement_with_initial_by_key(memc
,
1682 test_literal_param("foo"),
1683 test_literal_param("number"),
1684 1, initial
, 0, &new_number
));
1685 test_compare(new_number
, (initial
- 1));
1687 return TEST_SUCCESS
;
1689 test_return_t
binary_increment_with_prefix_test(memcached_st
*memc
)
1691 test_skip(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1693 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)"namespace:"));
1695 test_compare(return_value_based_on_buffering(memc
),
1697 test_literal_param("number"),
1698 test_literal_param("0"),
1699 (time_t)0, (uint32_t)0));
1701 uint64_t new_number
;
1702 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
,
1703 test_literal_param("number"),
1705 test_compare(uint64_t(1), new_number
);
1707 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
,
1708 test_literal_param("number"),
1710 test_compare(uint64_t(2), new_number
);
1712 return TEST_SUCCESS
;
1715 test_return_t
quit_test(memcached_st
*memc
)
1717 const char *value
= "sanford and sun";
1719 test_compare(return_value_based_on_buffering(memc
),
1721 test_literal_param(__func__
),
1722 value
, strlen(value
),
1723 (time_t)10, (uint32_t)3));
1724 memcached_quit(memc
);
1726 test_compare(return_value_based_on_buffering(memc
),
1728 test_literal_param(__func__
),
1729 value
, strlen(value
),
1730 (time_t)50, (uint32_t)9));
1732 return TEST_SUCCESS
;
1735 test_return_t
mget_result_test(memcached_st
*memc
)
1737 const char *keys
[]= {"fudge", "son", "food"};
1738 size_t key_length
[]= {5, 3, 4};
1740 memcached_result_st results_obj
;
1741 memcached_result_st
*results
;
1743 results
= memcached_result_create(memc
, &results_obj
);
1745 test_true(&results_obj
== results
);
1747 /* We need to empty the server before continueing test */
1748 test_compare(MEMCACHED_SUCCESS
,
1749 memcached_flush(memc
, 0));
1751 test_compare(MEMCACHED_SUCCESS
,
1752 memcached_mget(memc
, keys
, key_length
, 3));
1754 memcached_return_t rc
;
1755 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1760 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
))) { test_true(false); /* We should never see a value returned */ };
1761 test_false(results
);
1762 test_compare_got(MEMCACHED_NOTFOUND
, rc
, memcached_strerror(NULL
, rc
));
1764 for (uint32_t x
= 0; x
< 3; x
++)
1766 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1767 keys
[x
], key_length
[x
],
1768 (time_t)50, (uint32_t)9);
1769 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
1772 test_compare(MEMCACHED_SUCCESS
,
1773 memcached_mget(memc
, keys
, key_length
, 3));
1775 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1778 test_true(&results_obj
== results
);
1779 test_compare(MEMCACHED_SUCCESS
, rc
);
1780 test_memcmp(memcached_result_key_value(results
),
1781 memcached_result_value(results
),
1782 memcached_result_length(results
));
1783 test_compare(memcached_result_key_length(results
), memcached_result_length(results
));
1786 memcached_result_free(&results_obj
);
1788 return TEST_SUCCESS
;
1791 test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1793 const char *keys
[]= {"fudge", "son", "food"};
1794 size_t key_length
[]= {5, 3, 4};
1796 memcached_result_st
*results
;
1798 /* We need to empty the server before continueing test */
1799 test_compare(MEMCACHED_SUCCESS
,
1800 memcached_flush(memc
, 0));
1802 test_compare(MEMCACHED_SUCCESS
,
1803 memcached_mget(memc
, keys
, key_length
, 3));
1805 memcached_return_t rc
;
1806 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1810 test_false(results
);
1811 test_compare_got(MEMCACHED_NOTFOUND
, rc
, memcached_strerror(NULL
, rc
));
1813 for (uint32_t x
= 0; x
< 3; x
++)
1815 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1816 keys
[x
], key_length
[x
],
1817 (time_t)50, (uint32_t)9);
1818 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
1821 test_compare(MEMCACHED_SUCCESS
,
1822 memcached_mget(memc
, keys
, key_length
, 3));
1825 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1828 test_compare(MEMCACHED_SUCCESS
, rc
);
1829 test_compare(memcached_result_key_length(results
), memcached_result_length(results
));
1830 test_memcmp(memcached_result_key_value(results
),
1831 memcached_result_value(results
),
1832 memcached_result_length(results
));
1833 memcached_result_free(results
);
1837 return TEST_SUCCESS
;
1840 test_return_t
mget_result_function(memcached_st
*memc
)
1842 const char *keys
[]= {"fudge", "son", "food"};
1843 size_t key_length
[]= {5, 3, 4};
1845 memcached_execute_fn callbacks
[1];
1847 for (uint32_t x
= 0; x
< 3; x
++)
1849 test_compare(return_value_based_on_buffering(memc
),
1850 memcached_set(memc
, keys
[x
], key_length
[x
],
1851 keys
[x
], key_length
[x
],
1852 time_t(50), uint32_t(9)));
1854 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
1855 memcached_quit(memc
);
1857 test_compare(MEMCACHED_SUCCESS
,
1858 memcached_mget(memc
, keys
, key_length
, 3));
1860 callbacks
[0]= &callback_counter
;
1863 test_compare(MEMCACHED_SUCCESS
,
1864 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
1866 test_compare(size_t(3), counter
);
1868 return TEST_SUCCESS
;
1871 test_return_t
mget_test(memcached_st
*memc
)
1873 const char *keys
[]= {"fudge", "son", "food"};
1874 size_t key_length
[]= {5, 3, 4};
1876 char return_key
[MEMCACHED_MAX_KEY
];
1877 size_t return_key_length
;
1879 size_t return_value_length
;
1881 test_compare(MEMCACHED_SUCCESS
,
1882 memcached_mget(memc
, keys
, key_length
, 3));
1885 memcached_return_t rc
;
1886 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1887 &return_value_length
, &flags
, &rc
)))
1889 test_true(return_value
);
1891 test_false(return_value
);
1892 test_zero(return_value_length
);
1893 test_compare(MEMCACHED_NOTFOUND
, rc
);
1895 for (uint32_t x
= 0; x
< 3; x
++)
1897 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1898 keys
[x
], key_length
[x
],
1899 (time_t)50, (uint32_t)9);
1900 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
1902 test_compare(MEMCACHED_SUCCESS
,
1903 memcached_mget(memc
, keys
, key_length
, 3));
1906 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1907 &return_value_length
, &flags
, &rc
)))
1909 test_true(return_value
);
1910 test_compare(MEMCACHED_SUCCESS
, rc
);
1911 if (not memc
->_namespace
)
1913 test_compare(return_key_length
, return_value_length
);
1914 test_memcmp(return_value
, return_key
, return_value_length
);
1920 return TEST_SUCCESS
;
1923 test_return_t
mget_execute(memcached_st
*original_memc
)
1925 test_skip(true, memcached_behavior_get(original_memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
1927 memcached_st
*memc
= create_single_instance_memcached(original_memc
, "--BINARY-PROTOCOL");
1930 keys_st
keys(20480);
1932 /* First add all of the items.. */
1933 char blob
[1024] = {0};
1935 for (size_t x
= 0; x
< keys
.size(); ++x
)
1937 uint64_t query_id
= memcached_query_id(memc
);
1938 memcached_return_t rc
= memcached_add(memc
,
1939 keys
.key_at(x
), keys
.length_at(x
),
1942 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
,
1943 memcached_strerror(NULL
, rc
));
1944 test_compare(query_id
+1, memcached_query_id(memc
));
1947 /* Try to get all of them with a large multiget */
1949 memcached_execute_fn callbacks
[]= { &callback_counter
};
1950 test_compare(MEMCACHED_SUCCESS
,
1951 memcached_mget_execute(memc
,
1952 keys
.keys_ptr(), keys
.lengths_ptr(),
1953 keys
.size(), callbacks
, &counter
, 1));
1956 uint64_t query_id
= memcached_query_id(memc
);
1957 test_compare(MEMCACHED_SUCCESS
,
1958 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
1959 test_compare(query_id
, memcached_query_id(memc
));
1961 /* Verify that we got all of the items */
1962 test_compare(keys
.size(), counter
);
1965 memcached_free(memc
);
1967 return TEST_SUCCESS
;
1970 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1971 static pairs_st
*global_pairs
;
1973 test_return_t
key_setup(memcached_st
*memc
)
1975 test_skip(TEST_SUCCESS
, pre_binary(memc
));
1977 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1979 return TEST_SUCCESS
;
1982 test_return_t
key_teardown(memcached_st
*)
1984 pairs_free(global_pairs
);
1986 return TEST_SUCCESS
;
1989 test_return_t
block_add_regression(memcached_st
*memc
)
1991 /* First add all of the items.. */
1992 for (ptrdiff_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1994 char blob
[1024] = {0};
1996 memcached_return_t rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1997 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE
, memcached_strerror(NULL
, rc
));
2000 return TEST_SUCCESS
;
2003 test_return_t
binary_add_regression(memcached_st
*memc
)
2005 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
2006 test_return_t rc
= block_add_regression(memc
);
2011 test_return_t
get_stats_keys(memcached_st
*memc
)
2015 memcached_stat_st memc_stat
;
2016 memcached_return_t rc
;
2018 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
2019 test_compare(MEMCACHED_SUCCESS
, rc
);
2020 for (ptr
= stat_list
; *ptr
; ptr
++)
2025 return TEST_SUCCESS
;
2028 test_return_t
version_string_test(memcached_st
*)
2030 test_strcmp(LIBMEMCACHED_VERSION_STRING
, memcached_lib_version());
2032 return TEST_SUCCESS
;
2035 test_return_t
get_stats(memcached_st
*memc
)
2037 memcached_return_t rc
;
2039 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
2040 test_compare(MEMCACHED_SUCCESS
, rc
);
2041 test_true(memc_stat
);
2043 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
2045 char **stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
2046 test_compare(MEMCACHED_SUCCESS
, rc
);
2047 for (char **ptr
= stat_list
; *ptr
; ptr
++) {};
2052 memcached_stat_free(NULL
, memc_stat
);
2054 return TEST_SUCCESS
;
2057 test_return_t
add_host_test(memcached_st
*memc
)
2059 char servername
[]= "0.example.com";
2061 memcached_return_t rc
;
2062 memcached_server_st
*servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2063 test_compare(1U, memcached_server_list_count(servers
));
2065 for (unsigned int x
= 2; x
< 20; x
++)
2067 char buffer
[SMALL_STRING_LEN
];
2069 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2070 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2072 test_compare(MEMCACHED_SUCCESS
, rc
);
2073 test_compare(x
, memcached_server_list_count(servers
));
2076 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
2077 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
2079 memcached_server_list_free(servers
);
2081 return TEST_SUCCESS
;
2084 test_return_t
memcached_fetch_result_NOT_FOUND(memcached_st
*memc
)
2086 memcached_return_t rc
;
2088 const char *key
= "not_found";
2089 size_t key_length
= test_literal_param_size("not_found");
2091 test_compare(MEMCACHED_SUCCESS
,
2092 memcached_mget(memc
, &key
, &key_length
, 1));
2094 memcached_result_st
*result
= memcached_fetch_result(memc
, NULL
, &rc
);
2096 test_compare_got(MEMCACHED_NOTFOUND
, rc
, memcached_strerror(NULL
, rc
));
2098 memcached_result_free(result
);
2100 return TEST_SUCCESS
;
2103 static memcached_return_t
clone_test_callback(memcached_st
*, memcached_st
*)
2105 return MEMCACHED_SUCCESS
;
2108 static memcached_return_t
cleanup_test_callback(memcached_st
*)
2110 return MEMCACHED_SUCCESS
;
2113 test_return_t
callback_test(memcached_st
*memc
)
2115 /* Test User Data */
2119 memcached_return_t rc
;
2121 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
));
2122 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
2123 test_true(*test_ptr
== x
);
2126 /* Test Clone Callback */
2128 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
2129 void *clone_cb_ptr
= *(void **)&clone_cb
;
2130 void *temp_function
= NULL
;
2132 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_cb_ptr
));
2133 memcached_return_t rc
;
2134 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
2135 test_true(temp_function
== clone_cb_ptr
);
2136 test_compare(MEMCACHED_SUCCESS
, rc
);
2139 /* Test Cleanup Callback */
2141 memcached_cleanup_fn cleanup_cb
= (memcached_cleanup_fn
)cleanup_test_callback
;
2142 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
2143 void *temp_function
= NULL
;
2144 memcached_return_t rc
;
2146 test_compare(MEMCACHED_SUCCESS
, memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_cb_ptr
));
2147 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
2148 test_true(temp_function
== cleanup_cb_ptr
);
2151 return TEST_SUCCESS
;
2154 /* We don't test the behavior itself, we test the switches */
2155 test_return_t
behavior_test(memcached_st
*memc
)
2157 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
2158 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
));
2160 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
2161 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
));
2163 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, MEMCACHED_HASH_MD5
);
2164 test_compare(uint64_t(MEMCACHED_HASH_MD5
), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
));
2166 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2167 test_zero(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
));
2169 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2170 test_zero(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
));
2172 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, MEMCACHED_HASH_DEFAULT
);
2173 test_compare(uint64_t(MEMCACHED_HASH_DEFAULT
), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
));
2175 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, MEMCACHED_HASH_CRC
);
2176 test_compare(uint64_t(MEMCACHED_HASH_CRC
), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
));
2178 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
));
2180 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
));
2182 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
2183 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+1);
2184 test_compare((value
+1), memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2186 return TEST_SUCCESS
;
2189 test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2191 test_compare(MEMCACHED_DEPRECATED
,
2192 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, true));
2194 // Platform dependent
2196 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2200 return TEST_SUCCESS
;
2204 test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2206 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, true);
2207 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2209 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2211 if (memcached_success(rc
))
2220 return TEST_SUCCESS
;
2224 test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2226 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, true);
2227 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2229 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2231 if (memcached_success(rc
))
2240 return TEST_SUCCESS
;
2243 /* Make sure we behave properly if server list has no values */
2244 test_return_t
user_supplied_bug4(memcached_st
*memc
)
2246 const char *keys
[]= {"fudge", "son", "food"};
2247 size_t key_length
[]= {5, 3, 4};
2249 /* Here we free everything before running a bunch of mget tests */
2250 memcached_servers_reset(memc
);
2253 /* We need to empty the server before continueing test */
2254 test_compare(MEMCACHED_NO_SERVERS
,
2255 memcached_flush(memc
, 0));
2257 test_compare(MEMCACHED_NO_SERVERS
,
2258 memcached_mget(memc
, keys
, key_length
, 3));
2261 unsigned int keys_returned
;
2262 memcached_return_t rc
;
2263 test_compare(TEST_SUCCESS
, fetch_all_results(memc
, keys_returned
, rc
));
2264 test_compare(MEMCACHED_NOTFOUND
, rc
);
2265 test_zero(keys_returned
);
2268 for (uint32_t x
= 0; x
< 3; x
++)
2270 test_compare(MEMCACHED_NO_SERVERS
,
2271 memcached_set(memc
, keys
[x
], key_length
[x
],
2272 keys
[x
], key_length
[x
],
2273 (time_t)50, (uint32_t)9));
2276 test_compare(MEMCACHED_NO_SERVERS
,
2277 memcached_mget(memc
, keys
, key_length
, 3));
2281 char return_key
[MEMCACHED_MAX_KEY
];
2282 memcached_return_t rc
;
2283 size_t return_key_length
;
2284 size_t return_value_length
;
2287 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2288 &return_value_length
, &flags
, &rc
)))
2290 test_true(return_value
);
2291 test_compare(MEMCACHED_SUCCESS
, rc
);
2292 test_true(return_key_length
== return_value_length
);
2293 test_memcmp(return_value
, return_key
, return_value_length
);
2299 return TEST_SUCCESS
;
2302 #define VALUE_SIZE_BUG5 1048064
2303 test_return_t
user_supplied_bug5(memcached_st
*memc
)
2305 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2306 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2308 size_t value_length
;
2310 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2312 for (uint32_t x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2314 insert_data
[x
]= (signed char)rand();
2317 test_compare(MEMCACHED_SUCCESS
,
2318 memcached_flush(memc
, 0));
2320 memcached_return_t rc
;
2321 test_null(memcached_get(memc
, keys
[0], key_length
[0], &value_length
, &flags
, &rc
));
2322 test_compare(MEMCACHED_SUCCESS
,
2323 memcached_mget(memc
, keys
, key_length
, 4));
2326 test_compare(TEST_SUCCESS
, fetch_all_results(memc
, count
, rc
));
2327 test_compare(MEMCACHED_NOTFOUND
, rc
);
2330 for (uint32_t x
= 0; x
< 4; x
++)
2332 test_compare(MEMCACHED_SUCCESS
,
2333 memcached_set(memc
, keys
[x
], key_length
[x
],
2334 insert_data
, VALUE_SIZE_BUG5
,
2335 (time_t)0, (uint32_t)0));
2338 for (uint32_t x
= 0; x
< 10; x
++)
2340 value
= memcached_get(memc
, keys
[0], key_length
[0],
2341 &value_length
, &flags
, &rc
);
2342 test_compare(rc
, MEMCACHED_SUCCESS
);
2346 test_compare(MEMCACHED_SUCCESS
,
2347 memcached_mget(memc
, keys
, key_length
, 4));
2349 test_compare(TEST_SUCCESS
, fetch_all_results(memc
, count
));
2350 test_compare(4U, count
);
2352 delete [] insert_data
;
2354 return TEST_SUCCESS
;
2357 test_return_t
user_supplied_bug6(memcached_st
*memc
)
2359 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2360 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2361 char return_key
[MEMCACHED_MAX_KEY
];
2362 size_t return_key_length
;
2364 size_t value_length
;
2366 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2368 for (uint32_t x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2370 insert_data
[x
]= (signed char)rand();
2373 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0));
2375 test_compare(TEST_SUCCESS
, confirm_keys_dont_exist(memc
, keys
, test_array_length(keys
)));
2377 // We will now confirm that memcached_mget() returns success, but we will
2378 // then check to make sure that no actual keys are returned.
2379 test_compare(MEMCACHED_SUCCESS
,
2380 memcached_mget(memc
, keys
, key_length
, 4));
2382 memcached_return_t rc
;
2384 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2385 &value_length
, &flags
, &rc
)))
2390 test_compare_got(MEMCACHED_NOTFOUND
, rc
, memcached_strerror(NULL
, rc
));
2392 for (uint32_t x
= 0; x
< test_array_length(keys
); x
++)
2394 test_compare(MEMCACHED_SUCCESS
,
2395 memcached_set(memc
, keys
[x
], key_length
[x
],
2396 insert_data
, VALUE_SIZE_BUG5
,
2397 (time_t)0, (uint32_t)0));
2399 test_compare(TEST_SUCCESS
, confirm_keys_exist(memc
, keys
, test_array_length(keys
)));
2401 for (uint32_t x
= 0; x
< 2; x
++)
2403 value
= memcached_get(memc
, keys
[0], key_length
[0],
2404 &value_length
, &flags
, &rc
);
2408 test_compare(MEMCACHED_SUCCESS
,
2409 memcached_mget(memc
, keys
, key_length
, 4));
2410 /* We test for purge of partial complete fetches */
2411 for (count
= 3; count
; count
--)
2413 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2414 &value_length
, &flags
, &rc
);
2415 test_compare(MEMCACHED_SUCCESS
, rc
);
2416 test_memcmp(value
, insert_data
, value_length
);
2417 test_true(value_length
);
2421 delete [] insert_data
;
2423 return TEST_SUCCESS
;
2426 test_return_t
user_supplied_bug8(memcached_st
*)
2428 memcached_return_t rc
;
2430 memcached_st
*memc_clone
;
2432 memcached_server_st
*servers
;
2433 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";
2435 servers
= memcached_servers_parse(server_list
);
2438 mine
= memcached_create(NULL
);
2439 rc
= memcached_server_push(mine
, servers
);
2440 test_compare(MEMCACHED_SUCCESS
, rc
);
2441 memcached_server_list_free(servers
);
2444 memc_clone
= memcached_clone(NULL
, mine
);
2446 memcached_quit(mine
);
2447 memcached_quit(memc_clone
);
2450 memcached_free(mine
);
2451 memcached_free(memc_clone
);
2453 return TEST_SUCCESS
;
2456 /* Test flag store/retrieve */
2457 test_return_t
user_supplied_bug7(memcached_st
*memc
)
2459 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2460 test_true(insert_data
);
2462 for (size_t x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2464 insert_data
[x
]= (signed char)rand();
2467 memcached_flush(memc
, 0);
2469 const char *keys
= "036790384900";
2470 size_t key_length
= strlen(keys
);
2471 test_compare_hint(MEMCACHED_SUCCESS
, memcached_set(memc
, keys
, key_length
,
2472 insert_data
, VALUE_SIZE_BUG5
,
2474 memcached_last_error_message(memc
));
2476 memcached_return_t rc
;
2477 size_t value_length
;
2479 char *value
= memcached_get(memc
, keys
, key_length
,
2480 &value_length
, &flags
, &rc
);
2481 test_compare(245U, flags
);
2485 test_compare(MEMCACHED_SUCCESS
, memcached_mget(memc
, &keys
, &key_length
, 1));
2487 char return_key
[MEMCACHED_MAX_KEY
];
2488 size_t return_key_length
;
2490 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2491 &value_length
, &flags
, &rc
);
2492 test_compare(uint32_t(245), flags
);
2495 delete [] insert_data
;
2498 return TEST_SUCCESS
;
2501 test_return_t
user_supplied_bug9(memcached_st
*memc
)
2503 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2504 size_t key_length
[3];
2508 char return_key
[MEMCACHED_MAX_KEY
];
2509 size_t return_key_length
;
2511 size_t return_value_length
;
2514 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2515 key_length
[1]= strlen("fudge&*@#");
2516 key_length
[2]= strlen("for^#@&$not");
2519 for (unsigned int x
= 0; x
< 3; x
++)
2521 memcached_return_t rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2522 keys
[x
], key_length
[x
],
2523 (time_t)50, (uint32_t)9);
2524 test_compare(MEMCACHED_SUCCESS
, rc
);
2527 memcached_return_t rc
= memcached_mget(memc
, keys
, key_length
, 3);
2528 test_compare(MEMCACHED_SUCCESS
, rc
);
2530 /* We need to empty the server before continueing test */
2531 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2532 &return_value_length
, &flags
, &rc
)) != NULL
)
2534 test_true(return_value
);
2538 test_compare(3U, count
);
2540 return TEST_SUCCESS
;
2543 /* We are testing with aggressive timeout to get failures */
2544 test_return_t
user_supplied_bug10(memcached_st
*memc
)
2546 test_skip(memc
->servers
[0].type
, MEMCACHED_CONNECTION_TCP
);
2548 size_t value_length
= 512;
2549 unsigned int set
= 1;
2550 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2552 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2553 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2554 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, uint64_t(0));
2556 libtest::vchar_t value
;
2557 value
.reserve(value_length
);
2558 for (uint32_t x
= 0; x
< value_length
; x
++)
2560 value
.push_back(char(x
% 127));
2563 for (unsigned int x
= 1; x
<= 100000; ++x
)
2565 memcached_return_t rc
= memcached_set(mclone
,
2566 test_literal_param("foo"),
2567 &value
[0], value
.size(),
2570 test_true_got((rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_WRITE_FAILURE
or rc
== MEMCACHED_BUFFERED
or rc
== MEMCACHED_TIMEOUT
or rc
== MEMCACHED_CONNECTION_FAILURE
2571 or rc
== MEMCACHED_SERVER_TEMPORARILY_DISABLED
),
2572 memcached_strerror(NULL
, rc
));
2574 if (rc
== MEMCACHED_WRITE_FAILURE
or rc
== MEMCACHED_TIMEOUT
)
2580 memcached_free(mclone
);
2582 return TEST_SUCCESS
;
2586 We are looking failures in the async protocol
2588 test_return_t
user_supplied_bug11(memcached_st
*memc
)
2590 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2592 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, true);
2593 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, true);
2594 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, size_t(-1));
2596 test_compare(-1, int32_t(memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
)));
2599 libtest::vchar_t value
;
2601 for (unsigned int x
= 0; x
< 512; x
++)
2603 value
.push_back(char(x
% 127));
2606 for (unsigned int x
= 1; x
<= 100000; ++x
)
2608 memcached_return_t rc
= memcached_set(mclone
, test_literal_param("foo"), &value
[0], value
.size(), 0, 0);
2612 memcached_free(mclone
);
2614 return TEST_SUCCESS
;
2618 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2620 test_return_t
user_supplied_bug12(memcached_st
*memc
)
2622 memcached_return_t rc
;
2624 size_t value_length
;
2626 uint64_t number_value
;
2628 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2629 &value_length
, &flags
, &rc
);
2631 test_compare(MEMCACHED_NOTFOUND
, rc
);
2633 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2636 /* The binary protocol will set the key if it doesn't exist */
2637 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2639 test_compare(MEMCACHED_SUCCESS
, rc
);
2643 test_compare(MEMCACHED_NOTFOUND
, rc
);
2646 test_compare(MEMCACHED_SUCCESS
,
2647 memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0));
2649 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"), &value_length
, &flags
, &rc
);
2653 test_compare(MEMCACHED_SUCCESS
,
2654 memcached_increment(memc
, "autoincrement", strlen("autoincrement"), 1, &number_value
));
2655 test_compare(2UL, number_value
);
2657 return TEST_SUCCESS
;
2661 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2662 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2664 test_return_t
user_supplied_bug13(memcached_st
*memc
)
2666 char key
[] = "key34567890";
2668 char commandFirst
[]= "set key34567890 0 0 ";
2669 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2670 size_t commandLength
;
2672 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2674 size_t overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2676 for (size_t testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2678 char *overflow
= new (std::nothrow
) char[testSize
];
2679 test_true(overflow
);
2681 memset(overflow
, 'x', testSize
);
2682 test_compare(MEMCACHED_SUCCESS
,
2683 memcached_set(memc
, key
, strlen(key
),
2684 overflow
, testSize
, 0, 0));
2688 return TEST_SUCCESS
;
2693 Test values of many different sizes
2694 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2695 set key34567890 0 0 8169 \r\n
2696 is sent followed by buffer of size 8169, followed by 8169
2698 test_return_t
user_supplied_bug14(memcached_st
*memc
)
2700 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, true);
2702 libtest::vchar_t value
;
2703 value
.reserve(18000);
2704 for (ptrdiff_t x
= 0; x
< 18000; x
++)
2706 value
.push_back((char) (x
% 127));
2709 for (size_t current_length
= 0; current_length
< value
.size(); current_length
++)
2711 memcached_return_t rc
= memcached_set(memc
, test_literal_param("foo"),
2712 &value
[0], current_length
,
2713 (time_t)0, (uint32_t)0);
2714 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
2716 size_t string_length
;
2718 char *string
= memcached_get(memc
, test_literal_param("foo"),
2719 &string_length
, &flags
, &rc
);
2721 test_compare(MEMCACHED_SUCCESS
, rc
);
2722 test_compare(string_length
, current_length
);
2723 test_memcmp(string
, &value
[0], string_length
);
2728 return TEST_SUCCESS
;
2732 Look for zero length value problems
2734 test_return_t
user_supplied_bug15(memcached_st
*memc
)
2736 for (uint32_t x
= 0; x
< 2; x
++)
2738 memcached_return_t rc
= memcached_set(memc
, test_literal_param("mykey"),
2740 (time_t)0, (uint32_t)0);
2742 test_compare(MEMCACHED_SUCCESS
, rc
);
2746 char *value
= memcached_get(memc
, test_literal_param("mykey"),
2747 &length
, &flags
, &rc
);
2749 test_compare(MEMCACHED_SUCCESS
, rc
);
2754 value
= memcached_get(memc
, test_literal_param("mykey"),
2755 &length
, &flags
, &rc
);
2757 test_compare(MEMCACHED_SUCCESS
, rc
);
2763 return TEST_SUCCESS
;
2766 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2767 test_return_t
user_supplied_bug16(memcached_st
*memc
)
2769 test_compare_hint(MEMCACHED_SUCCESS
, memcached_set(memc
, test_literal_param("mykey"),
2771 (time_t)0, UINT32_MAX
),
2772 memcached_last_error_message(memc
));
2777 memcached_return_t rc
;
2778 char *value
= memcached_get(memc
, test_literal_param("mykey"),
2779 &length
, &flags
, &rc
);
2781 test_compare(MEMCACHED_SUCCESS
, rc
);
2784 test_compare(flags
, UINT32_MAX
);
2786 return TEST_SUCCESS
;
2789 #if !defined(__sun) && !defined(__OpenBSD__)
2790 /* Check the validity of chinese key*/
2791 test_return_t
user_supplied_bug17(memcached_st
*memc
)
2793 const char *key
= "豆瓣";
2794 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2795 memcached_return_t rc
= memcached_set(memc
, key
, strlen(key
),
2796 value
, strlen(value
),
2799 test_compare(MEMCACHED_SUCCESS
, rc
);
2803 char *value2
= memcached_get(memc
, key
, strlen(key
),
2804 &length
, &flags
, &rc
);
2806 test_true(length
==strlen(value
));
2807 test_compare(MEMCACHED_SUCCESS
, rc
);
2808 test_memcmp(value
, value2
, length
);
2811 return TEST_SUCCESS
;
2819 test_return_t
user_supplied_bug19(memcached_st
*)
2821 memcached_return_t res
;
2823 memcached_st
*memc
= memcached(test_literal_param("--server=localhost:11311/?100 --server=localhost:11312/?100"));
2825 const memcached_server_st
*server
= memcached_server_by_key(memc
, "a", 1, &res
);
2828 memcached_free(memc
);
2830 return TEST_SUCCESS
;
2833 /* CAS test from Andei */
2834 test_return_t
user_supplied_bug20(memcached_st
*memc
)
2836 const char *key
= "abc";
2837 size_t key_len
= strlen("abc");
2839 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
2841 test_compare(MEMCACHED_SUCCESS
,
2843 test_literal_param("abc"),
2844 test_literal_param("foobar"),
2845 (time_t)0, (uint32_t)0));
2847 test_compare(MEMCACHED_SUCCESS
,
2848 memcached_mget(memc
, &key
, &key_len
, 1));
2850 memcached_result_st result_obj
;
2851 memcached_result_st
*result
= memcached_result_create(memc
, &result_obj
);
2854 memcached_result_create(memc
, &result_obj
);
2855 memcached_return_t status
;
2856 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2859 test_compare(MEMCACHED_SUCCESS
, status
);
2861 memcached_result_free(result
);
2863 return TEST_SUCCESS
;
2866 /* Large mget() of missing keys with binary proto
2868 * If many binary quiet commands (such as getq's in an mget) fill the output
2869 * buffer and the server chooses not to respond, memcached_flush hangs. See
2870 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2873 /* sighandler_t function that always asserts false */
2874 static void fail(int)
2880 test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2885 return TEST_SKIPPED
;
2887 void (*oldalarm
)(int);
2889 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
2890 test_true(memc_clone
);
2892 /* only binproto uses getq for mget */
2893 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
2895 /* empty the cache to ensure misses (hence non-responses) */
2896 test_compare(MEMCACHED_SUCCESS
, memcached_flush(memc_clone
, 0));
2898 keys_st
keys(key_count
);
2900 oldalarm
= signal(SIGALRM
, fail
);
2903 test_compare_got(MEMCACHED_SUCCESS
,
2904 memcached_mget(memc_clone
, keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()),
2905 memcached_last_error_message(memc_clone
));
2908 signal(SIGALRM
, oldalarm
);
2910 memcached_return_t rc
;
2912 char return_key
[MEMCACHED_MAX_KEY
];
2913 size_t return_key_length
;
2915 size_t return_value_length
;
2916 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2917 &return_value_length
, &flags
, &rc
)))
2919 test_false(return_value
); // There are no keys to fetch, so the value should never be returned
2921 test_compare(MEMCACHED_NOTFOUND
, rc
);
2922 test_zero(return_value_length
);
2923 test_zero(return_key_length
);
2924 test_false(return_key
[0]);
2925 test_false(return_value
);
2927 memcached_free(memc_clone
);
2929 return TEST_SUCCESS
;
2933 test_return_t
user_supplied_bug21(memcached_st
*memc
)
2935 test_skip(TEST_SUCCESS
, pre_binary(memc
));
2937 /* should work as of r580 */
2938 test_compare(TEST_SUCCESS
,
2939 _user_supplied_bug21(memc
, 10));
2941 /* should fail as of r580 */
2942 test_compare(TEST_SUCCESS
,
2943 _user_supplied_bug21(memc
, 1000));
2945 return TEST_SUCCESS
;
2948 test_return_t
output_ketama_weighted_keys(memcached_st
*)
2950 memcached_st
*memc
= memcached_create(NULL
);
2954 test_compare(MEMCACHED_SUCCESS
,
2955 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, true));
2957 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2958 test_compare(value
, uint64_t(1));
2960 test_compare(MEMCACHED_SUCCESS
,
2961 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
));
2963 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2964 test_true(value
== MEMCACHED_HASH_MD5
);
2967 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
2969 memcached_server_st
*server_pool
;
2970 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");
2971 memcached_server_push(memc
, server_pool
);
2973 // @todo this needs to be refactored to actually test something.
2976 if ((fp
= fopen("ketama_keys.txt", "w")))
2980 printf("cannot write to file ketama_keys.txt");
2981 return TEST_FAILURE
;
2984 for (int x
= 0; x
< 10000; x
++)
2987 snprintf(key
, sizeof(key
), "%d", x
);
2989 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
2990 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2991 in_port_t port
= memc
->hosts
[server_idx
].port
;
2992 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
2993 memcached_server_instance_st instance
=
2994 memcached_server_instance_by_position(memc
, host_index
);
2998 memcached_server_list_free(server_pool
);
2999 memcached_free(memc
);
3001 return TEST_SUCCESS
;
3005 test_return_t
result_static(memcached_st
*memc
)
3007 memcached_result_st result
;
3008 memcached_result_st
*result_ptr
= memcached_result_create(memc
, &result
);
3009 test_false(result
.options
.is_allocated
);
3010 test_true(memcached_is_initialized(&result
));
3011 test_true(result_ptr
);
3012 test_true(result_ptr
== &result
);
3014 memcached_result_free(&result
);
3016 test_false(result
.options
.is_allocated
);
3017 test_false(memcached_is_initialized(&result
));
3019 return TEST_SUCCESS
;
3022 test_return_t
result_alloc(memcached_st
*memc
)
3024 memcached_result_st
*result_ptr
= memcached_result_create(memc
, NULL
);
3025 test_true(result_ptr
);
3026 test_true(result_ptr
->options
.is_allocated
);
3027 test_true(memcached_is_initialized(result_ptr
));
3028 memcached_result_free(result_ptr
);
3030 return TEST_SUCCESS
;
3034 test_return_t
add_host_test1(memcached_st
*memc
)
3036 memcached_return_t rc
;
3037 char servername
[]= "0.example.com";
3039 memcached_server_st
*servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3041 test_compare(1U, memcached_server_list_count(servers
));
3043 for (uint32_t x
= 2; x
< 20; x
++)
3045 char buffer
[SMALL_STRING_LEN
];
3047 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3048 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3050 test_compare(MEMCACHED_SUCCESS
, rc
);
3051 test_compare(x
, memcached_server_list_count(servers
));
3054 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
3055 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
3057 memcached_server_list_free(servers
);
3059 return TEST_SUCCESS
;
3063 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3067 #ifdef HARD_MALLOC_TESTS
3068 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3076 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3080 #ifdef HARD_MALLOC_TESTS
3081 void *ret
= malloc(size
+ 8);
3084 ret
= (void*)((caddr_t
)ret
+ 8);
3087 void *ret
= malloc(size
);
3092 memset(ret
, 0xff, size
);
3099 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *)
3101 #ifdef HARD_MALLOC_TESTS
3102 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3103 void *nmem
= realloc(real_ptr
, size
+ 8);
3108 ret
= (void*)((caddr_t
)nmem
+ 8);
3114 return realloc(mem
, size
);
3119 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *)
3121 #ifdef HARD_MALLOC_TESTS
3122 void *mem
= my_malloc(ptr
, nelem
* size
);
3125 memset(mem
, 0, nelem
* size
);
3131 return calloc(nelem
, size
);
3135 test_return_t
selection_of_namespace_tests(memcached_st
*memc
)
3137 memcached_return_t rc
;
3138 const char *key
= "mine";
3141 /* Make sure be default none exists */
3142 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3144 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3146 /* Test a clean set */
3147 test_compare(MEMCACHED_SUCCESS
,
3148 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, (void *)key
));
3150 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3152 test_memcmp(value
, key
, 4);
3153 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3155 /* Test that we can turn it off */
3156 test_compare(MEMCACHED_SUCCESS
,
3157 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, NULL
));
3159 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3161 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3163 /* Now setup for main test */
3164 test_compare(MEMCACHED_SUCCESS
,
3165 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, (void *)key
));
3167 value
= (char *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3169 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3170 test_memcmp(value
, key
, 4);
3172 /* Set to Zero, and then Set to something too large */
3175 memset(long_key
, 0, 255);
3177 test_compare(MEMCACHED_SUCCESS
,
3178 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, NULL
));
3180 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3182 test_compare(MEMCACHED_SUCCESS
, rc
);
3184 /* Test a long key for failure */
3185 /* TODO, extend test to determine based on setting, what result should be */
3186 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3187 test_compare(MEMCACHED_SUCCESS
,
3188 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, long_key
));
3190 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3191 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3192 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_SUCCESS
: MEMCACHED_BAD_KEY_PROVIDED
,
3193 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, long_key
));
3195 /* Test for a bad prefix, but with a short key */
3196 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_INVALID_ARGUMENTS
: MEMCACHED_SUCCESS
,
3197 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1));
3199 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) ? MEMCACHED_SUCCESS
: MEMCACHED_BAD_KEY_PROVIDED
,
3200 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, "dog cat"));
3203 return TEST_SUCCESS
;
3206 test_return_t
set_namespace(memcached_st
*memc
)
3208 memcached_return_t rc
;
3209 const char *key
= "mine";
3212 // Make sure we default to a null namespace
3213 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3215 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3217 /* Test a clean set */
3218 test_compare(MEMCACHED_SUCCESS
,
3219 memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, (void *)key
));
3221 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_NAMESPACE
, &rc
);
3223 test_memcmp(value
, key
, 4);
3224 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
3226 return TEST_SUCCESS
;
3229 test_return_t
set_namespace_and_binary(memcached_st
*memc
)
3231 test_return_if(pre_binary(memc
));
3232 test_return_if(set_namespace(memc
));
3234 return TEST_SUCCESS
;
3237 #ifdef MEMCACHED_ENABLE_DEPRECATED
3238 test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3240 void *test_ptr
= NULL
;
3243 memcached_malloc_fn malloc_cb
= (memcached_malloc_fn
)my_malloc
;
3244 cb_ptr
= *(void **)&malloc_cb
;
3245 memcached_return_t rc
;
3247 test_compare(MEMCACHED_SUCCESS
,
3248 memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
));
3249 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3250 test_compare(MEMCACHED_SUCCESS
, rc
);
3251 test_true(test_ptr
== cb_ptr
);
3255 memcached_realloc_fn realloc_cb
=
3256 (memcached_realloc_fn
)my_realloc
;
3257 cb_ptr
= *(void **)&realloc_cb
;
3258 memcached_return_t rc
;
3260 test_compare(MEMCACHED_SUCCESS
,
3261 memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
));
3262 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3263 test_compare(MEMCACHED_SUCCESS
, rc
);
3264 test_true(test_ptr
== cb_ptr
);
3268 memcached_free_fn free_cb
=
3269 (memcached_free_fn
)my_free
;
3270 cb_ptr
= *(void **)&free_cb
;
3271 memcached_return_t rc
;
3273 test_compare(MEMCACHED_SUCCESS
,
3274 memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
));
3275 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3276 test_compare(MEMCACHED_SUCCESS
, rc
);
3277 test_true(test_ptr
== cb_ptr
);
3280 return TEST_SUCCESS
;
3285 test_return_t
set_memory_alloc(memcached_st
*memc
)
3287 test_compare(MEMCACHED_INVALID_ARGUMENTS
,
3288 memcached_set_memory_allocators(memc
, NULL
, my_free
,
3289 my_realloc
, my_calloc
, NULL
));
3291 test_compare(MEMCACHED_SUCCESS
,
3292 memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3293 my_realloc
, my_calloc
, NULL
));
3295 memcached_malloc_fn mem_malloc
;
3296 memcached_free_fn mem_free
;
3297 memcached_realloc_fn mem_realloc
;
3298 memcached_calloc_fn mem_calloc
;
3299 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3300 &mem_realloc
, &mem_calloc
);
3302 test_true(mem_malloc
== my_malloc
);
3303 test_true(mem_realloc
== my_realloc
);
3304 test_true(mem_calloc
== my_calloc
);
3305 test_true(mem_free
== my_free
);
3307 return TEST_SUCCESS
;
3310 test_return_t
enable_consistent_crc(memcached_st
*memc
)
3313 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3314 memcached_hash_t hash
;
3315 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3316 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3319 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3320 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3322 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3324 if (hash
!= MEMCACHED_HASH_CRC
)
3325 return TEST_SKIPPED
;
3327 return TEST_SUCCESS
;
3330 test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
3333 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3334 memcached_hash_t hash
;
3335 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3336 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
3341 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3342 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3344 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3346 if (hash
!= MEMCACHED_HASH_HSIEH
)
3347 return TEST_SKIPPED
;
3350 return TEST_SUCCESS
;
3353 test_return_t
enable_cas(memcached_st
*memc
)
3355 unsigned int set
= 1;
3357 if (libmemcached_util_version_check(memc
, 1, 2, 4))
3359 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3361 return TEST_SUCCESS
;
3364 return TEST_SKIPPED
;
3367 test_return_t
check_for_1_2_3(memcached_st
*memc
)
3369 memcached_version(memc
);
3371 memcached_server_instance_st instance
=
3372 memcached_server_instance_by_position(memc
, 0);
3374 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
3375 or instance
->minor_version
> 2)
3377 return TEST_SUCCESS
;
3380 return TEST_SKIPPED
;
3383 test_return_t
MEMCACHED_BEHAVIOR_POLL_TIMEOUT_test(memcached_st
*memc
)
3385 const uint64_t timeout
= 100; // Not using, just checking that it sets
3387 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3389 test_compare(timeout
, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
));
3391 return TEST_SUCCESS
;
3394 test_return_t
noreply_test(memcached_st
*memc
)
3396 test_compare(MEMCACHED_SUCCESS
,
3397 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, true));
3398 test_compare(MEMCACHED_SUCCESS
,
3399 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
3400 test_compare(MEMCACHED_SUCCESS
,
3401 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, true));
3402 test_compare(1LLU, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
));
3403 test_compare(1LLU, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
));
3404 test_compare(1LLU, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
));
3406 memcached_return_t ret
;
3407 for (int count
= 0; count
< 5; ++count
)
3409 for (size_t x
= 0; x
< 100; ++x
)
3411 char key
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
3412 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
3413 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
3415 size_t len
= (size_t)check_length
;
3420 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3423 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3426 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3429 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3432 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3438 test_true_got(ret
== MEMCACHED_SUCCESS
or ret
== MEMCACHED_BUFFERED
,
3439 memcached_strerror(NULL
, ret
));
3443 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3444 ** API and is _ONLY_ done this way to verify that the library works the
3445 ** way it is supposed to do!!!!
3449 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
3451 memcached_server_instance_st instance
=
3452 memcached_server_instance_by_position(memc
, x
);
3453 no_msg
+=(int)(instance
->cursor_active
);
3456 test_true(no_msg
== 0);
3458 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
3461 ** Now validate that all items was set properly!
3463 for (size_t x
= 0; x
< 100; ++x
)
3467 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
3469 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
3471 size_t len
= (size_t)check_length
;
3474 char* value
=memcached_get(memc
, key
, strlen(key
),
3475 &length
, &flags
, &ret
);
3476 test_true_got(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
, memcached_strerror(NULL
, ret
));
3479 case 0: /* FALLTHROUGH */
3480 case 1: /* FALLTHROUGH */
3482 test_true(strncmp(value
, key
, len
) == 0);
3483 test_true(len
== length
);
3486 test_true(length
== len
* 2);
3489 test_true(length
== len
* 3);
3499 /* Try setting an illegal cas value (should not return an error to
3500 * the caller (because we don't expect a return message from the server)
3502 const char* keys
[]= {"0"};
3503 size_t lengths
[]= {1};
3506 memcached_result_st results_obj
;
3507 memcached_result_st
*results
;
3508 test_compare(MEMCACHED_SUCCESS
,
3509 memcached_mget(memc
, keys
, lengths
, 1));
3511 results
= memcached_result_create(memc
, &results_obj
);
3513 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3515 test_compare(MEMCACHED_SUCCESS
, ret
);
3516 uint64_t cas
= memcached_result_cas(results
);
3517 memcached_result_free(&results_obj
);
3519 test_compare(MEMCACHED_SUCCESS
,
3520 memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
));
3523 * The item will have a new cas value, so try to set it again with the old
3524 * value. This should fail!
3526 test_compare(MEMCACHED_SUCCESS
,
3527 memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
));
3528 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3529 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3530 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3533 return TEST_SUCCESS
;
3536 test_return_t
analyzer_test(memcached_st
*memc
)
3538 memcached_analysis_st
*report
;
3539 memcached_return_t rc
;
3541 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3542 test_compare(MEMCACHED_SUCCESS
, rc
);
3543 test_true(memc_stat
);
3545 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3546 test_compare(MEMCACHED_SUCCESS
, rc
);
3550 memcached_stat_free(NULL
, memc_stat
);
3552 return TEST_SUCCESS
;
3555 test_return_t
util_version_test(memcached_st
*memc
)
3557 test_compare_hint(MEMCACHED_SUCCESS
, memcached_version(memc
), memcached_last_error_message(memc
));
3558 test_true(libmemcached_util_version_check(memc
, 0, 0, 0));
3560 bool if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
3562 // We expect failure
3565 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
3566 fprintf(stderr
, "\nDumping Server Information\n\n");
3567 memcached_server_fn callbacks
[1];
3569 callbacks
[0]= dump_server_information
;
3570 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
3571 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
3573 test_true(if_successful
== false);
3575 memcached_server_instance_st instance
=
3576 memcached_server_instance_by_position(memc
, 0);
3578 memcached_version(memc
);
3580 // We only use one binary when we test, so this should be just fine.
3581 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
3582 test_true(if_successful
== true);
3584 if (instance
->micro_version
> 0)
3586 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
3588 else if (instance
->minor_version
> 0)
3590 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
3592 else if (instance
->major_version
> 0)
3594 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
3597 test_true(if_successful
== true);
3599 if (instance
->micro_version
> 0)
3601 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
3603 else if (instance
->minor_version
> 0)
3605 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
3607 else if (instance
->major_version
> 0)
3609 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
3612 test_true(if_successful
== false);
3614 return TEST_SUCCESS
;
3617 test_return_t
getpid_connection_failure_test(memcached_st
*memc
)
3619 test_skip(memc
->servers
[0].type
, MEMCACHED_CONNECTION_TCP
);
3620 memcached_return_t rc
;
3621 memcached_server_instance_st instance
=
3622 memcached_server_instance_by_position(memc
, 0);
3624 // Test both the version that returns a code, and the one that does not.
3625 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3626 memcached_server_port(instance
) -1, NULL
) == -1);
3628 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3629 memcached_server_port(instance
) -1, &rc
) == -1);
3630 test_compare_got(MEMCACHED_CONNECTION_FAILURE
, rc
, memcached_strerror(memc
, rc
));
3632 return TEST_SUCCESS
;
3636 test_return_t
getpid_test(memcached_st
*memc
)
3638 memcached_return_t rc
;
3639 memcached_server_instance_st instance
=
3640 memcached_server_instance_by_position(memc
, 0);
3642 // Test both the version that returns a code, and the one that does not.
3643 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3644 memcached_server_port(instance
), NULL
) > -1);
3646 test_true(libmemcached_util_getpid(memcached_server_name(instance
),
3647 memcached_server_port(instance
), &rc
) > -1);
3648 test_compare(MEMCACHED_SUCCESS
, rc
);
3650 return TEST_SUCCESS
;
3653 test_return_t
ping_test(memcached_st
*memc
)
3655 memcached_return_t rc
;
3656 memcached_server_instance_st instance
=
3657 memcached_server_instance_by_position(memc
, 0);
3659 // Test both the version that returns a code, and the one that does not.
3660 test_true(libmemcached_util_ping(memcached_server_name(instance
),
3661 memcached_server_port(instance
), NULL
));
3663 test_true(libmemcached_util_ping(memcached_server_name(instance
),
3664 memcached_server_port(instance
), &rc
));
3666 test_compare(MEMCACHED_SUCCESS
, rc
);
3668 return TEST_SUCCESS
;
3673 test_return_t
hash_sanity_test (memcached_st
*memc
)
3677 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
3678 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
3679 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
3680 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
3681 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
3682 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
3683 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
3684 #ifdef HAVE_HSIEH_HASH
3685 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
3687 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
3688 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
3689 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
3691 return TEST_SUCCESS
;
3695 test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
3697 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH
));
3699 test_compare(MEMCACHED_SUCCESS
,
3700 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
3701 (uint64_t)MEMCACHED_HASH_HSIEH
));
3703 return TEST_SUCCESS
;
3706 test_return_t
murmur_avaibility_test (memcached_st
*memc
)
3708 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR
));
3710 test_compare(MEMCACHED_SUCCESS
,
3711 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
));
3713 return TEST_SUCCESS
;
3716 test_return_t
one_at_a_time_run (memcached_st
*)
3721 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3723 test_compare(one_at_a_time_values
[x
],
3724 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
));
3727 return TEST_SUCCESS
;
3730 test_return_t
md5_run (memcached_st
*)
3735 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3737 test_compare(md5_values
[x
],
3738 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
));
3741 return TEST_SUCCESS
;
3744 test_return_t
crc_run (memcached_st
*)
3749 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3751 test_compare(crc_values
[x
],
3752 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
));
3755 return TEST_SUCCESS
;
3758 test_return_t
fnv1_64_run (memcached_st
*)
3760 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64
));
3765 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3767 test_compare(fnv1_64_values
[x
],
3768 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
));
3771 return TEST_SUCCESS
;
3774 test_return_t
fnv1a_64_run (memcached_st
*)
3776 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1A_64
));
3781 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3783 test_compare(fnv1a_64_values
[x
],
3784 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
));
3787 return TEST_SUCCESS
;
3790 test_return_t
fnv1_32_run (memcached_st
*)
3795 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3797 test_compare(fnv1_32_values
[x
],
3798 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
));
3801 return TEST_SUCCESS
;
3804 test_return_t
fnv1a_32_run (memcached_st
*)
3809 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3811 test_compare(fnv1a_32_values
[x
],
3812 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
));
3815 return TEST_SUCCESS
;
3818 test_return_t
hsieh_run (memcached_st
*)
3820 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH
));
3825 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3827 test_compare(hsieh_values
[x
],
3828 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
));
3831 return TEST_SUCCESS
;
3834 test_return_t
murmur_run (memcached_st
*)
3836 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR
));
3838 #ifdef WORDS_BIGENDIAN
3839 (void)murmur_values
;
3840 return TEST_SKIPPED
;
3845 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3847 test_compare(murmur_values
[x
],
3848 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
));
3851 return TEST_SUCCESS
;
3855 test_return_t
jenkins_run (memcached_st
*)
3860 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3862 test_compare(jenkins_values
[x
],
3863 memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
));
3866 return TEST_SUCCESS
;
3869 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *)
3871 return libhashkit_md5(string
, string_length
);
3874 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *)
3876 return libhashkit_crc32(string
, string_length
);
3879 test_return_t
memcached_get_hashkit_test (memcached_st
*)
3885 memcached_st
*memc
= memcached(test_literal_param("--server=localhost:1 --server=localhost:2 --server=localhost:3 --server=localhost:4 --server=localhost5 --DISTRIBUTION=modula"));
3887 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};
3888 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};
3890 const hashkit_st
*kit
= memcached_get_hashkit(memc
);
3892 hashkit_clone(&new_kit
, kit
);
3893 test_compare(HASHKIT_SUCCESS
, hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
));
3895 memcached_set_hashkit(memc
, &new_kit
);
3898 Verify Setting the hash.
3900 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3904 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
3905 test_compare_got(md5_values
[x
], hash_val
, *ptr
);
3910 Now check memcached_st.
3912 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3916 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
3917 test_compare_got(md5_hosts
[x
], hash_val
, *ptr
);
3920 test_compare(HASHKIT_SUCCESS
, hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
));
3922 memcached_set_hashkit(memc
, &new_kit
);
3925 Verify Setting the hash.
3927 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3931 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
3932 test_true(crc_values
[x
] == hash_val
);
3935 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
3939 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
3940 test_compare(crc_hosts
[x
], hash_val
);
3943 memcached_free(memc
);
3945 return TEST_SUCCESS
;
3949 Test case adapted from John Gorman <johngorman2@gmail.com>
3951 We are testing the error condition when we connect to a server via memcached_get()
3952 but find that the server is not available.
3954 test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*)
3958 memcached_return rc
;
3961 memcached_st
*tl_memc_h
= memcached(test_literal_param("--server=localhost:9898 --server=localhost:9899")); // This server should not exist
3963 // See if memcached is reachable.
3964 char *value
= memcached_get(tl_memc_h
,
3965 test_literal_param(__func__
),
3970 test_true(memcached_failed(rc
));
3972 memcached_free(tl_memc_h
);
3974 return TEST_SUCCESS
;
3978 We connect to a server which exists, but search for a key that does not exist.
3980 test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
3984 memcached_return rc
;
3986 // See if memcached is reachable.
3987 char *value
= memcached_get(memc
,
3988 test_literal_param(__func__
),
3993 test_compare(MEMCACHED_NOTFOUND
, rc
);
3995 return TEST_SUCCESS
;
3999 Test case adapted from John Gorman <johngorman2@gmail.com>
4001 We are testing the error condition when we connect to a server via memcached_get_by_key()
4002 but find that the server is not available.
4004 test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*)
4008 memcached_return rc
;
4011 memcached_st
*tl_memc_h
= memcached_create(NULL
);
4012 memcached_server_st
*servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4013 memcached_server_push(tl_memc_h
, servers
);
4014 memcached_server_list_free(servers
);
4016 // See if memcached is reachable.
4017 char *value
= memcached_get_by_key(tl_memc_h
,
4018 test_literal_param(__func__
), // Key
4019 test_literal_param(__func__
), // Value
4024 test_true(memcached_failed(rc
));
4026 memcached_free(tl_memc_h
);
4028 return TEST_SUCCESS
;
4032 We connect to a server which exists, but search for a key that does not exist.
4034 test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4038 memcached_return rc
;
4040 // See if memcached is reachable.
4041 char *value
= memcached_get_by_key(memc
,
4042 test_literal_param(__func__
), // Key
4043 test_literal_param(__func__
), // Value
4048 test_compare(MEMCACHED_NOTFOUND
, rc
);
4050 return TEST_SUCCESS
;
4053 test_return_t
regression_bug_434484(memcached_st
*memc
)
4055 test_skip(TEST_SUCCESS
, pre_binary(memc
));
4057 test_compare(MEMCACHED_NOTSTORED
,
4058 memcached_append(memc
,
4059 test_literal_param(__func__
), // Key
4060 test_literal_param(__func__
), // Value
4063 libtest::vchar_t data
;
4064 data
.resize(2048 * 1024);
4065 test_compare(MEMCACHED_E2BIG
,
4067 test_literal_param(__func__
), // Key
4068 &data
[0], data
.size(), 0, 0));
4070 return TEST_SUCCESS
;
4073 test_return_t
regression_bug_434843(memcached_st
*original_memc
)
4075 test_skip(TEST_SUCCESS
, pre_binary(original_memc
));
4077 memcached_return_t rc
;
4079 memcached_execute_fn callbacks
[]= { &callback_counter
};
4082 * I only want to hit only _one_ server so I know the number of requests I'm
4083 * sending in the pipleine to the server. Let's try to do a multiget of
4084 * 1024 (that should satisfy most users don't you think?). Future versions
4085 * will include a mget_execute function call if you need a higher number.
4087 memcached_st
*memc
= create_single_instance_memcached(original_memc
, "--BINARY-PROTOCOL");
4092 * Run two times.. the first time we should have 100% cache miss,
4093 * and the second time we should have 100% cache hits
4095 for (ptrdiff_t y
= 0; y
< 2; y
++)
4097 test_compare(MEMCACHED_SUCCESS
,
4098 memcached_mget(memc
, keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
4100 // One the first run we should get a NOT_FOUND, but on the second some data
4101 // should be returned.
4102 test_compare(y
? MEMCACHED_SUCCESS
: MEMCACHED_NOTFOUND
,
4103 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
4107 /* The first iteration should give me a 100% cache miss. verify that*/
4108 char blob
[1024]= { 0 };
4110 test_false(counter
);
4112 for (size_t x
= 0; x
< keys
.size(); ++x
)
4114 rc
= memcached_add(memc
,
4115 keys
.key_at(x
), keys
.length_at(x
),
4116 blob
, sizeof(blob
), 0, 0);
4117 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4122 /* Verify that we received all of the key/value pairs */
4123 test_compare(counter
, keys
.size());
4127 memcached_free(memc
);
4129 return TEST_SUCCESS
;
4132 test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
4134 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
4136 return regression_bug_434843(memc
);
4139 test_return_t
regression_bug_421108(memcached_st
*memc
)
4141 memcached_return_t rc
;
4142 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4143 test_compare(MEMCACHED_SUCCESS
, rc
);
4145 char *bytes_str
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
4146 test_compare(MEMCACHED_SUCCESS
, rc
);
4147 test_true(bytes_str
);
4148 char *bytes_read_str
= memcached_stat_get_value(memc
, memc_stat
,
4150 test_compare(MEMCACHED_SUCCESS
, rc
);
4151 test_true(bytes_read_str
);
4153 char *bytes_written_str
= memcached_stat_get_value(memc
, memc_stat
,
4154 "bytes_written", &rc
);
4155 test_compare(MEMCACHED_SUCCESS
, rc
);
4156 test_true(bytes_written_str
);
4158 unsigned long long bytes
= strtoull(bytes_str
, 0, 10);
4159 unsigned long long bytes_read
= strtoull(bytes_read_str
, 0, 10);
4160 unsigned long long bytes_written
= strtoull(bytes_written_str
, 0, 10);
4162 test_true(bytes
!= bytes_read
);
4163 test_true(bytes
!= bytes_written
);
4165 /* Release allocated resources */
4167 free(bytes_read_str
);
4168 free(bytes_written_str
);
4169 memcached_stat_free(NULL
, memc_stat
);
4171 return TEST_SUCCESS
;
4175 * The test case isn't obvious so I should probably document why
4176 * it works the way it does. Bug 442914 was caused by a bug
4177 * in the logic in memcached_purge (it did not handle the case
4178 * where the number of bytes sent was equal to the watermark).
4179 * In this test case, create messages so that we hit that case
4180 * and then disable noreply mode and issue a new command to
4181 * verify that it isn't stuck. If we change the format for the
4182 * delete command or the watermarks, we need to update this
4185 test_return_t
regression_bug_442914(memcached_st
*memc
)
4187 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1));
4188 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
4190 uint32_t number_of_hosts
= memcached_server_count(memc
);
4191 memc
->number_of_hosts
= 1;
4193 for (uint32_t x
= 0; x
< 250; ++x
)
4196 size_t len
= (size_t)snprintf(key
, sizeof(key
), "%0250u", x
);
4197 memcached_return_t rc
= memcached_delete(memc
, key
, len
, 0);
4198 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_last_error_message(memc
));
4201 // Delete, and then delete again to look for not found
4204 size_t len
= snprintf(key
, sizeof(key
), "%037u", 251U);
4205 memcached_return_t rc
= memcached_delete(memc
, key
, len
, 0);
4206 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4208 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0));
4209 test_compare(MEMCACHED_NOTFOUND
, memcached_delete(memc
, key
, len
, 0));
4212 memc
->number_of_hosts
= number_of_hosts
;
4214 return TEST_SUCCESS
;
4217 test_return_t
regression_bug_447342(memcached_st
*memc
)
4219 if (memcached_server_count(memc
) < 3 or pre_replication(memc
) != TEST_SUCCESS
)
4221 return TEST_SKIPPED
;
4224 test_compare(MEMCACHED_SUCCESS
,
4225 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2));
4229 for (size_t x
= 0; x
< keys
.size(); ++x
)
4231 test_compare(MEMCACHED_SUCCESS
,
4233 keys
.key_at(x
), keys
.length_at(x
), // Keys
4234 keys
.key_at(x
), keys
.length_at(x
), // Values
4239 ** We are using the quiet commands to store the replicas, so we need
4240 ** to ensure that all of them are processed before we can continue.
4241 ** In the test we go directly from storing the object to trying to
4242 ** receive the object from all of the different servers, so we
4243 ** could end up in a race condition (the memcached server hasn't yet
4244 ** processed the quiet command from the replication set when it process
4245 ** the request from the other client (created by the clone)). As a
4246 ** workaround for that we call memcached_quit to send the quit command
4247 ** to the server and wait for the response ;-) If you use the test code
4248 ** as an example for your own code, please note that you shouldn't need
4251 memcached_quit(memc
);
4253 /* Verify that all messages are stored, and we didn't stuff too much
4256 test_compare(MEMCACHED_SUCCESS
,
4257 memcached_mget(memc
,
4258 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
4260 unsigned int counter
= 0;
4261 memcached_execute_fn callbacks
[]= { &callback_counter
};
4262 test_compare(MEMCACHED_SUCCESS
,
4263 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
4265 /* Verify that we received all of the key/value pairs */
4266 test_compare(counter
, keys
.size());
4268 memcached_quit(memc
);
4270 * Don't do the following in your code. I am abusing the internal details
4271 * within the library, and this is not a supported interface.
4272 * This is to verify correct behavior in the library. Fake that two servers
4275 memcached_server_instance_st instance_one
= memcached_server_instance_by_position(memc
, 0);
4276 memcached_server_instance_st instance_two
= memcached_server_instance_by_position(memc
, 2);
4277 in_port_t port0
= instance_one
->port
;
4278 in_port_t port2
= instance_two
->port
;
4280 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
4281 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
4283 test_compare(MEMCACHED_SUCCESS
,
4284 memcached_mget(memc
,
4285 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
4288 test_compare(MEMCACHED_SUCCESS
,
4289 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
4290 test_compare(counter
, keys
.size());
4292 /* restore the memc handle */
4293 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
4294 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
4296 memcached_quit(memc
);
4298 /* Remove half of the objects */
4299 for (size_t x
= 0; x
< keys
.size(); ++x
)
4303 test_compare(MEMCACHED_SUCCESS
,
4304 memcached_delete(memc
, keys
.key_at(x
), keys
.length_at(x
), 0));
4308 memcached_quit(memc
);
4309 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
4310 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
4312 /* now retry the command, this time we should have cache misses */
4313 test_compare(MEMCACHED_SUCCESS
,
4314 memcached_mget(memc
,
4315 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size()));
4318 test_compare(MEMCACHED_SUCCESS
,
4319 memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1));
4320 test_compare(counter
, (unsigned int)(keys
.size() >> 1));
4322 /* restore the memc handle */
4323 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
4324 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
4326 return TEST_SUCCESS
;
4329 test_return_t
regression_bug_463297(memcached_st
*memc
)
4331 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_delete(memc
, "foo", 3, 1));
4333 // Since we blocked timed delete, this test is no longer valid.
4335 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4336 test_true(memc_clone
);
4337 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
4339 memcached_server_instance_st instance
=
4340 memcached_server_instance_by_position(memc_clone
, 0);
4342 if (instance
->major_version
> 1 ||
4343 (instance
->major_version
== 1 &&
4344 instance
->minor_version
> 2))
4346 /* Binary protocol doesn't support deferred delete */
4347 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
4348 test_true(bin_clone
);
4349 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1));
4350 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_delete(bin_clone
, "foo", 3, 1));
4351 memcached_free(bin_clone
);
4353 memcached_quit(memc_clone
);
4355 /* If we know the server version, deferred delete should fail
4356 * with invalid arguments */
4357 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_delete(memc_clone
, "foo", 3, 1));
4359 /* If we don't know the server version, we should get a protocol error */
4360 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
4362 /* but there is a bug in some of the memcached servers (1.4) that treats
4363 * the counter as noreply so it doesn't send the proper error message
4365 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
4367 /* And buffered mode should be disabled and we should get protocol error */
4368 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
4369 rc
= memcached_delete(memc
, "foo", 3, 1);
4370 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
4372 /* Same goes for noreply... */
4373 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1));
4374 rc
= memcached_delete(memc
, "foo", 3, 1);
4375 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
4377 /* but a normal request should go through (and be buffered) */
4378 test_compare(MEMCACHED_BUFFERED
, (rc
= memcached_delete(memc
, "foo", 3, 0)));
4379 test_compare(MEMCACHED_SUCCESS
, memcached_flush_buffers(memc
));
4381 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0));
4382 /* unbuffered noreply should be success */
4383 test_compare(MEMCACHED_SUCCESS
, memcached_delete(memc
, "foo", 3, 0));
4384 /* unbuffered with reply should be not found... */
4385 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0));
4386 test_compare(MEMCACHED_NOTFOUND
, memcached_delete(memc
, "foo", 3, 0));
4389 memcached_free(memc_clone
);
4392 return TEST_SUCCESS
;
4396 /* Test memcached_server_get_last_disconnect
4397 * For a working server set, shall be NULL
4398 * For a set of non existing server, shall not be NULL
4400 test_return_t
test_get_last_disconnect(memcached_st
*memc
)
4402 memcached_return_t rc
;
4403 memcached_server_instance_st disconnected_server
;
4405 /* With the working set of server */
4406 const char *key
= "marmotte";
4407 const char *value
= "milka";
4409 memcached_reset_last_disconnected_server(memc
);
4410 test_false(memc
->last_disconnected_server
);
4411 rc
= memcached_set(memc
, key
, strlen(key
),
4412 value
, strlen(value
),
4413 (time_t)0, (uint32_t)0);
4414 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4416 disconnected_server
= memcached_server_get_last_disconnect(memc
);
4417 test_false(disconnected_server
);
4419 /* With a non existing server */
4421 memcached_server_st
*servers
;
4423 const char *server_list
= "localhost:9";
4425 servers
= memcached_servers_parse(server_list
);
4427 mine
= memcached_create(NULL
);
4428 rc
= memcached_server_push(mine
, servers
);
4429 test_compare(MEMCACHED_SUCCESS
, rc
);
4430 memcached_server_list_free(servers
);
4433 rc
= memcached_set(mine
, key
, strlen(key
),
4434 value
, strlen(value
),
4435 (time_t)0, (uint32_t)0);
4436 test_true(memcached_failed(rc
));
4438 disconnected_server
= memcached_server_get_last_disconnect(mine
);
4439 test_true_got(disconnected_server
, memcached_strerror(mine
, rc
));
4440 test_compare(in_port_t(9), memcached_server_port(disconnected_server
));
4441 test_false(strncmp(memcached_server_name(disconnected_server
),"localhost",9));
4443 memcached_quit(mine
);
4444 memcached_free(mine
);
4446 return TEST_SUCCESS
;
4449 test_return_t
test_multiple_get_last_disconnect(memcached_st
*)
4451 const char *server_string
= "--server=localhost:8888 --server=localhost:8889 --server=localhost:8890 --server=localhost:8891 --server=localhost:8892";
4452 char buffer
[BUFSIZ
];
4454 test_compare(MEMCACHED_SUCCESS
,
4455 libmemcached_check_configuration(server_string
, strlen(server_string
), buffer
, sizeof(buffer
)));
4457 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
4460 // We will just use the error strings as our keys
4461 uint32_t counter
= 100;
4464 for (int x
= int(MEMCACHED_SUCCESS
); x
< int(MEMCACHED_MAXIMUM_RETURN
); ++x
)
4466 const char *msg
= memcached_strerror(memc
, memcached_return_t(x
));
4467 memcached_return_t ret
= memcached_set(memc
, msg
, strlen(msg
), NULL
, 0, (time_t)0, (uint32_t)0);
4468 test_true_got((ret
== MEMCACHED_CONNECTION_FAILURE
or ret
== MEMCACHED_SERVER_TEMPORARILY_DISABLED
), memcached_last_error_message(memc
));
4470 memcached_server_instance_st disconnected_server
= memcached_server_get_last_disconnect(memc
);
4471 test_true(disconnected_server
);
4472 test_strcmp("localhost", memcached_server_name(disconnected_server
));
4473 test_true(memcached_server_port(disconnected_server
) >= 8888 and memcached_server_port(disconnected_server
) <= 8892);
4477 memcached_reset_last_disconnected_server(memc
);
4482 memcached_free(memc
);
4484 return TEST_SUCCESS
;
4487 test_return_t
test_verbosity(memcached_st
*memc
)
4489 memcached_verbosity(memc
, 3);
4491 return TEST_SUCCESS
;
4495 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
4496 const char *key
, size_t key_length
,
4497 const char *value
, size_t value_length
,
4507 return MEMCACHED_SUCCESS
;
4510 test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
4512 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
4513 test_compare(MEMCACHED_SUCCESS
, rc
);
4515 test_compare(MEMCACHED_SUCCESS
,
4516 memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
));
4518 test_compare(MEMCACHED_SUCCESS
,
4519 memcached_stat_execute(memc
, "items", stat_printer
, NULL
));
4521 test_compare(MEMCACHED_SUCCESS
,
4522 memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
));
4524 return TEST_SUCCESS
;
4528 * This test ensures that the failure counter isn't incremented during
4529 * normal termination of the memcached instance.
4531 test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
4533 memcached_return_t rc
;
4534 memcached_server_instance_st instance
;
4536 /* Set value to force connection to the server */
4537 const char *key
= "marmotte";
4538 const char *value
= "milka";
4541 * Please note that I'm abusing the internal structures in libmemcached
4542 * in a non-portable way and you shouldn't be doing this. I'm only
4543 * doing this in order to verify that the library works the way it should
4545 uint32_t number_of_hosts
= memcached_server_count(memc
);
4546 memc
->number_of_hosts
= 1;
4548 /* Ensure that we are connected to the server by setting a value */
4549 rc
= memcached_set(memc
, key
, strlen(key
),
4550 value
, strlen(value
),
4551 (time_t)0, (uint32_t)0);
4552 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
4555 instance
= memcached_server_instance_by_position(memc
, 0);
4556 /* The test is to see that the memcached_quit doesn't increase the
4557 * the server failure conter, so let's ensure that it is zero
4558 * before sending quit
4560 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
4562 memcached_quit(memc
);
4564 /* Verify that it memcached_quit didn't increment the failure counter
4565 * Please note that this isn't bullet proof, because an error could
4568 test_zero(instance
->server_failure_counter
);
4570 /* restore the instance */
4571 memc
->number_of_hosts
= number_of_hosts
;
4573 return TEST_SUCCESS
;
4577 * This tests ensures expected disconnections (for some behavior changes
4578 * for instance) do not wrongly increase failure counter
4580 test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
4582 /* Set value to force connection to the server */
4583 const char *key
= "marmotte";
4584 const char *value
= "milka";
4586 test_compare_hint(MEMCACHED_SUCCESS
,
4587 memcached_set(memc
, key
, strlen(key
),
4588 value
, strlen(value
),
4589 (time_t)0, (uint32_t)0),
4590 memcached_last_error_message(memc
));
4593 /* put failure limit to 1 */
4594 test_compare(MEMCACHED_SUCCESS
,
4595 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, true));
4597 /* Put a retry timeout to effectively activate failure_limit effect */
4598 test_compare(MEMCACHED_SUCCESS
,
4599 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, true));
4601 /* change behavior that triggers memcached_quit()*/
4602 test_compare(MEMCACHED_SUCCESS
,
4603 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, true));
4606 /* Check if we still are connected */
4608 size_t string_length
;
4609 memcached_return rc
;
4610 char *string
= memcached_get(memc
, key
, strlen(key
),
4611 &string_length
, &flags
, &rc
);
4613 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
4617 return TEST_SUCCESS
;
4622 * Test that ensures mget_execute does not end into recursive calls that finally fails
4624 test_return_t
regression_bug_490486(memcached_st
*original_memc
)
4628 return TEST_SKIPPED
; // My MAC can't handle this test
4631 test_skip(TEST_SUCCESS
, pre_binary(original_memc
));
4634 * I only want to hit _one_ server so I know the number of requests I'm
4635 * sending in the pipeline.
4637 memcached_st
*memc
= create_single_instance_memcached(original_memc
, "--BINARY-PROTOCOL --POLL-TIMEOUT=1000 --REMOVE-FAILED-SERVERS=1 --RETRY-TIMEOUT=3600");
4640 keys_st
keys(20480);
4642 /* First add all of the items.. */
4643 char blob
[1024]= { 0 };
4644 for (size_t x
= 0; x
< keys
.size(); ++x
)
4646 memcached_return rc
= memcached_set(memc
,
4647 keys
.key_at(x
), keys
.length_at(x
),
4648 blob
, sizeof(blob
), 0, 0);
4649 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
); // MEMCACHED_TIMEOUT <-- hash been observed on OSX
4654 /* Try to get all of them with a large multiget */
4656 memcached_execute_function callbacks
[]= { &callback_counter
};
4657 memcached_return_t rc
= memcached_mget_execute(memc
,
4658 keys
.keys_ptr(), keys
.lengths_ptr(), keys
.size(),
4659 callbacks
, &counter
, 1);
4660 test_compare(MEMCACHED_SUCCESS
, rc
);
4662 char* the_value
= NULL
;
4663 char the_key
[MEMCACHED_MAX_KEY
];
4664 size_t the_key_length
;
4665 size_t the_value_length
;
4669 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
4671 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
4677 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
4680 test_compare(MEMCACHED_END
, rc
);
4682 /* Verify that we got all of the items */
4683 test_compare(counter
, keys
.size());
4686 memcached_free(memc
);
4688 return TEST_SUCCESS
;
4691 test_return_t
regression_bug_583031(memcached_st
*)
4693 memcached_st
*memc
= memcached_create(NULL
);
4695 test_compare(MEMCACHED_SUCCESS
, memcached_server_add(memc
, "10.2.3.4", 11211));
4697 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
4698 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
4699 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4700 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4701 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
4702 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
4704 memcached_return_t rc
;
4708 const char *value
= memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
4712 test_compare_got(MEMCACHED_TIMEOUT
, rc
, memcached_last_error_message(memc
));
4714 memcached_free(memc
);
4716 return TEST_SUCCESS
;
4719 test_return_t
regression_bug_581030(memcached_st
*)
4722 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
4723 test_false(local_stat
);
4725 memcached_stat_free(NULL
, NULL
);
4728 return TEST_SUCCESS
;
4731 #define regression_bug_655423_COUNT 6000
4732 test_return_t
regression_bug_655423(memcached_st
*memc
)
4734 memcached_st
*clone
= memcached_clone(NULL
, memc
);
4735 memc
= NULL
; // Just to make sure it is not used
4740 return TEST_SKIPPED
;
4743 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1));
4744 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1));
4745 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1));
4746 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH
, 1));
4748 memset(payload
, int('x'), sizeof(payload
));
4750 keys_st
keys(regression_bug_655423_COUNT
);
4752 for (size_t x
= 0; x
< keys
.size(); x
++)
4754 test_compare(MEMCACHED_SUCCESS
, memcached_set(clone
,
4757 payload
, sizeof(payload
), 0, 0));
4760 for (size_t x
= 0; x
< keys
.size(); x
++)
4762 size_t value_length
;
4763 memcached_return_t rc
;
4764 char *value
= memcached_get(clone
,
4767 &value_length
, NULL
, &rc
);
4769 if (rc
== MEMCACHED_NOTFOUND
)
4772 test_zero(value_length
);
4776 test_compare(MEMCACHED_SUCCESS
, rc
);
4778 test_compare(100LLU, value_length
);
4782 test_compare(MEMCACHED_SUCCESS
,
4783 memcached_mget(clone
,
4784 keys
.keys_ptr(), keys
.lengths_ptr(),
4788 memcached_result_st
*result
= NULL
;
4789 while ((result
= memcached_fetch_result(clone
, result
, NULL
)))
4791 test_compare(size_t(100), memcached_result_length(result
));
4795 test_true(count
> 100); // If we don't get back atleast this, something is up
4797 memcached_free(clone
);
4799 return TEST_SUCCESS
;
4803 * Test that ensures that buffered set to not trigger problems during io_flush
4805 #define regression_bug_490520_COUNT 200480
4806 test_return_t
regression_bug_490520(memcached_st
*memc
)
4808 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
,1);
4809 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
,1);
4810 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
4811 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
,1);
4812 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
4814 memc
->number_of_hosts
= 1;
4816 /* First add all of the items.. */
4817 char blob
[3333] = {0};
4818 for (uint32_t x
= 0; x
< regression_bug_490520_COUNT
; ++x
)
4821 int key_length
= snprintf(key
, sizeof(key
), "0200%u", x
);
4823 memcached_return rc
= memcached_set(memc
, key
, key_length
, blob
, sizeof(blob
), 0, 0);
4824 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_last_error_message(memc
));
4827 return TEST_SUCCESS
;
4831 test_return_t
regression_bug_854604(memcached_st
*)
4835 test_compare(MEMCACHED_INVALID_ARGUMENTS
, libmemcached_check_configuration(0, 0, buffer
, 0));
4837 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, 0));
4839 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, 1));
4840 test_compare(buffer
[0], 0);
4842 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, 10));
4843 test_true(strlen(buffer
));
4845 test_compare(MEMCACHED_PARSE_ERROR
, libmemcached_check_configuration(test_literal_param("syntax error"), buffer
, sizeof(buffer
)));
4846 test_true(strlen(buffer
));
4848 return TEST_SUCCESS
;
4851 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
4853 fprintf(stderr
, "Iteration #%u: ", it
);
4855 if (error
== MEMCACHED_ERRNO
)
4857 fprintf(stderr
, "system error %d from %s: %s\n",
4858 errno
, what
, strerror(errno
));
4862 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
4863 memcached_strerror(mc
, error
));
4867 #define TEST_CONSTANT_CREATION 200
4869 test_return_t
regression_bug_(memcached_st
*memc
)
4871 const char *remote_server
;
4874 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
4876 return TEST_SKIPPED
;
4879 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
4881 memcached_st
* mc
= memcached_create(NULL
);
4882 memcached_return rc
;
4884 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
4885 if (rc
!= MEMCACHED_SUCCESS
)
4887 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
4890 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
4891 if (rc
!= MEMCACHED_SUCCESS
)
4893 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
4896 rc
= memcached_server_add(mc
, remote_server
, 0);
4897 if (rc
!= MEMCACHED_SUCCESS
)
4899 memcached_die(mc
, rc
, "memcached_server_add", x
);
4902 const char *set_key
= "akey";
4903 const size_t set_key_len
= strlen(set_key
);
4904 const char *set_value
= "a value";
4905 const size_t set_value_len
= strlen(set_value
);
4907 if (rc
== MEMCACHED_SUCCESS
)
4911 size_t get_value_len
;
4913 uint32_t get_value_flags
;
4915 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
4916 &get_value_flags
, &rc
);
4917 if (rc
!= MEMCACHED_SUCCESS
)
4919 memcached_die(mc
, rc
, "memcached_get", x
);
4925 (get_value_len
!= set_value_len
4926 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
4928 fprintf(stderr
, "Values don't match?\n");
4929 rc
= MEMCACHED_FAILURE
;
4935 rc
= memcached_set(mc
,
4936 set_key
, set_key_len
,
4937 set_value
, set_value_len
,
4941 if (rc
!= MEMCACHED_SUCCESS
)
4943 memcached_die(mc
, rc
, "memcached_set", x
);
4950 if (rc
!= MEMCACHED_SUCCESS
)
4956 return TEST_SUCCESS
;