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.
40 Sample test application.
51 #include <sys/types.h>
57 #include "libmemcached/common.h"
59 #include <libtest/server.h>
61 #include "clients/generator.h"
62 #include "clients/execute.h"
64 #define SMALL_STRING_LEN 1024
66 #include <libtest/test.h>
67 #include "tests/deprecated.h"
68 #include "tests/parser.h"
69 #include "tests/pool.h"
70 #include "tests/string.h"
71 #include "tests/replication.h"
72 #include "tests/basic.h"
73 #include "tests/error_conditions.h"
74 #include "tests/print.h"
75 #include "tests/virtual_buckets.h"
78 #ifdef HAVE_LIBMEMCACHEDUTIL
80 #include "libmemcached/memcached_util.h"
83 #include "hash_results.h"
85 #define GLOBAL_COUNT 10000
86 #define GLOBAL2_COUNT 100
87 #define SERVERS_TO_CREATE 5
88 static uint32_t global_count
;
90 static pairs_st
*global_pairs
;
91 static const char *global_keys
[GLOBAL_COUNT
];
92 static size_t global_keys_length
[GLOBAL_COUNT
];
95 static test_return_t
pre_binary(memcached_st
*memc
);
98 static test_return_t
init_test(memcached_st
*not_used
)
103 (void)memcached_create(&memc
);
104 memcached_free(&memc
);
109 #define TEST_PORT_COUNT 7
110 in_port_t test_ports
[TEST_PORT_COUNT
];
112 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
113 const memcached_server_st
*server
,
117 size_t bigger
= *((size_t *)(context
));
119 assert(bigger
<= memcached_server_port(server
));
120 *((size_t *)(context
))= memcached_server_port(server
);
122 return MEMCACHED_SUCCESS
;
125 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
126 const memcached_server_st
*instance
,
130 FILE *stream
= (FILE *)context
;
133 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
134 memcached_server_name(instance
),
135 memcached_server_port(instance
),
136 instance
->major_version
,
137 instance
->minor_version
,
138 instance
->micro_version
);
140 return MEMCACHED_SUCCESS
;
143 static test_return_t
server_sort_test(memcached_st
*ptr
)
145 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
147 memcached_return_t rc
;
148 memcached_server_fn callbacks
[1];
149 memcached_st
*local_memc
;
152 local_memc
= memcached_create(NULL
);
153 test_true(local_memc
);
154 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
156 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
158 test_ports
[x
]= (in_port_t
)random() % 64000;
159 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
160 test_true(memcached_server_count(local_memc
) == x
+ 1);
162 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
164 test_true(rc
== MEMCACHED_SUCCESS
);
167 callbacks
[0]= server_display_function
;
168 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
171 memcached_free(local_memc
);
176 static test_return_t
server_sort2_test(memcached_st
*ptr
)
178 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
179 memcached_return_t rc
;
180 memcached_server_fn callbacks
[1];
181 memcached_st
*local_memc
;
182 memcached_server_instance_st instance
;
185 local_memc
= memcached_create(NULL
);
186 test_true(local_memc
);
187 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
188 test_true(rc
== MEMCACHED_SUCCESS
);
190 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
191 test_true(rc
== MEMCACHED_SUCCESS
);
192 instance
= memcached_server_instance_by_position(local_memc
, 0);
193 test_true(memcached_server_port(instance
) == 43043);
195 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
196 test_true(rc
== MEMCACHED_SUCCESS
);
198 instance
= memcached_server_instance_by_position(local_memc
, 0);
199 test_true(memcached_server_port(instance
) == 43042);
201 instance
= memcached_server_instance_by_position(local_memc
, 1);
202 test_true(memcached_server_port(instance
) == 43043);
204 callbacks
[0]= server_display_function
;
205 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
208 memcached_free(local_memc
);
213 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
)
215 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";
218 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
221 memcached_server_fn callbacks
[1];
222 callbacks
[0]= server_print_callback
;
223 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
225 memcached_free(memc
);
230 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
,
231 const memcached_server_st
*server
,
235 uint32_t x
= *((uint32_t *)(context
));
238 if (! (test_ports
[x
] == server
->port
))
240 fprintf(stderr
, "%lu -> %lu\n", (unsigned long)test_ports
[x
], (unsigned long)server
->port
);
241 return MEMCACHED_FAILURE
;
244 *((uint32_t *)(context
))= ++x
;
246 return MEMCACHED_SUCCESS
;
249 static test_return_t
server_unsort_test(memcached_st
*ptr
)
251 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
252 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
253 memcached_return_t rc
;
254 memcached_server_fn callbacks
[1];
255 memcached_st
*local_memc
;
258 local_memc
= memcached_create(NULL
);
259 test_true(local_memc
);
261 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
263 test_ports
[x
]= (in_port_t
)(random() % 64000);
264 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
265 test_true(memcached_server_count(local_memc
) == x
+1);
267 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
269 test_true(rc
== MEMCACHED_SUCCESS
);
272 callbacks
[0]= server_display_unsort_function
;
273 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
275 /* Now we sort old data! */
276 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
277 callbacks
[0]= server_display_function
;
278 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
281 memcached_free(local_memc
);
286 static test_return_t
allocation_test(memcached_st
*not_used
)
290 memc
= memcached_create(NULL
);
292 memcached_free(memc
);
297 static test_return_t
clone_test(memcached_st
*memc
)
301 memcached_st
*memc_clone
;
302 memc_clone
= memcached_clone(NULL
, NULL
);
303 test_true(memc_clone
);
304 memcached_free(memc_clone
);
307 /* Can we init from null? */
309 memcached_st
*memc_clone
;
310 memc_clone
= memcached_clone(NULL
, memc
);
311 test_true(memc_clone
);
314 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
315 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
316 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
317 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
320 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
321 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
322 test_true(memc_clone
->distribution
== memc
->distribution
);
323 { // Test all of the flags
324 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
325 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
326 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
327 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
328 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
329 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
330 test_true(memc_clone
->ketama
.weighted
== memc
->ketama
.weighted
);
331 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
332 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
333 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
334 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
335 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
336 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
338 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
339 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
340 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
341 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
342 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
343 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
344 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
345 test_true(memc_clone
->on_clone
== memc
->on_clone
);
346 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
347 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
348 test_true(memc_clone
->recv_size
== memc
->recv_size
);
349 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
350 test_true(memc_clone
->send_size
== memc
->send_size
);
351 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
352 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
353 test_true(memc_clone
->user_data
== memc
->user_data
);
355 memcached_free(memc_clone
);
358 /* Can we init from struct? */
360 memcached_st declared_clone
;
361 memcached_st
*memc_clone
;
362 memset(&declared_clone
, 0 , sizeof(memcached_st
));
363 memc_clone
= memcached_clone(&declared_clone
, NULL
);
364 test_true(memc_clone
);
365 memcached_free(memc_clone
);
368 /* Can we init from struct? */
370 memcached_st declared_clone
;
371 memcached_st
*memc_clone
;
372 memset(&declared_clone
, 0 , sizeof(memcached_st
));
373 memc_clone
= memcached_clone(&declared_clone
, memc
);
374 test_true(memc_clone
);
375 memcached_free(memc_clone
);
381 static test_return_t
userdata_test(memcached_st
*memc
)
384 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
385 test_true(memcached_get_user_data(memc
) == foo
);
386 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
391 static test_return_t
connection_test(memcached_st
*memc
)
393 memcached_return_t rc
;
395 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
396 test_true(rc
== MEMCACHED_SUCCESS
);
401 static test_return_t
error_test(memcached_st
*memc
)
403 memcached_return_t rc
;
404 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
405 982370485U, 1263635348U, 4242906218U, 3829656100U,
406 1891735253U, 334139633U, 2257084983U, 3088286104U,
407 13199785U, 2542027183U, 1097051614U, 199566778U,
408 2748246961U, 2465192557U, 1664094137U, 2405439045U,
409 1842224848U, 692413798U, 3479807801U, 919913813U,
410 4269430871U, 610793021U, 527273862U, 1437122909U,
411 2300930706U, 2943759320U, 674306647U, 2400528935U,
412 54481931U, 4186304426U, 1741088401U, 2979625118U,
413 4159057246U, 3425930182U, 2593724503U, 1868899624U,
414 1769812374U, 2302537950U, 1110330676U, 3365377466U,
415 1336171666U, 3021258493U, 3365377466U };
417 // You have updated the memcache_error messages but not updated docs/tests.
418 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
421 const char *msg
= memcached_strerror(memc
, rc
);
422 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
423 MEMCACHED_HASH_JENKINS
);
424 if (values
[rc
] != hash_val
)
426 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
427 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
429 test_true(values
[rc
] == hash_val
);
431 test_true(MEMCACHED_MAXIMUM_RETURN
== 46);
436 static test_return_t
set_test(memcached_st
*memc
)
438 memcached_return_t rc
;
439 const char *key
= "foo";
440 const char *value
= "when we sanitize";
442 rc
= memcached_set(memc
, key
, strlen(key
),
443 value
, strlen(value
),
444 (time_t)0, (uint32_t)0);
445 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
450 static test_return_t
append_test(memcached_st
*memc
)
452 memcached_return_t rc
;
453 const char *key
= "fig";
454 const char *in_value
= "we";
455 char *out_value
= NULL
;
459 rc
= memcached_flush(memc
, 0);
460 test_true(rc
== MEMCACHED_SUCCESS
);
462 rc
= memcached_set(memc
, key
, strlen(key
),
463 in_value
, strlen(in_value
),
464 (time_t)0, (uint32_t)0);
465 test_true(rc
== MEMCACHED_SUCCESS
);
467 rc
= memcached_append(memc
, key
, strlen(key
),
468 " the", strlen(" the"),
469 (time_t)0, (uint32_t)0);
470 test_true(rc
== MEMCACHED_SUCCESS
);
472 rc
= memcached_append(memc
, key
, strlen(key
),
473 " people", strlen(" people"),
474 (time_t)0, (uint32_t)0);
475 test_true(rc
== MEMCACHED_SUCCESS
);
477 out_value
= memcached_get(memc
, key
, strlen(key
),
478 &value_length
, &flags
, &rc
);
479 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
480 test_true(strlen("we the people") == value_length
);
481 test_true(rc
== MEMCACHED_SUCCESS
);
487 static test_return_t
append_binary_test(memcached_st
*memc
)
489 memcached_return_t rc
;
490 const char *key
= "numbers";
491 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
497 rc
= memcached_flush(memc
, 0);
498 test_true(rc
== MEMCACHED_SUCCESS
);
500 rc
= memcached_set(memc
,
503 (time_t)0, (uint32_t)0);
504 test_true(rc
== MEMCACHED_SUCCESS
);
506 for (x
= 0; store_list
[x
] ; x
++)
508 rc
= memcached_append(memc
,
510 (char *)&store_list
[x
], sizeof(uint32_t),
511 (time_t)0, (uint32_t)0);
512 test_true(rc
== MEMCACHED_SUCCESS
);
515 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
516 &value_length
, &flags
, &rc
);
517 test_true((value_length
== (sizeof(uint32_t) * x
)));
518 test_true(rc
== MEMCACHED_SUCCESS
);
520 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
522 test_true(*ptr
== store_list
[x
- counter
]);
530 static test_return_t
cas2_test(memcached_st
*memc
)
532 memcached_return_t rc
;
533 const char *keys
[]= {"fudge", "son", "food"};
534 size_t key_length
[]= {5, 3, 4};
535 const char *value
= "we the people";
536 size_t value_length
= strlen("we the people");
538 memcached_result_st results_obj
;
539 memcached_result_st
*results
;
542 rc
= memcached_flush(memc
, 0);
543 test_true(rc
== MEMCACHED_SUCCESS
);
545 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
547 for (x
= 0; x
< 3; x
++)
549 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
550 keys
[x
], key_length
[x
],
551 (time_t)50, (uint32_t)9);
552 test_true(rc
== MEMCACHED_SUCCESS
);
555 rc
= memcached_mget(memc
, keys
, key_length
, 3);
557 results
= memcached_result_create(memc
, &results_obj
);
559 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
561 test_true(results
->item_cas
);
562 test_true(rc
== MEMCACHED_SUCCESS
);
563 test_true(memcached_result_cas(results
));
565 test_true(!memcmp(value
, "we the people", strlen("we the people")));
566 test_true(strlen("we the people") == value_length
);
567 test_true(rc
== MEMCACHED_SUCCESS
);
569 memcached_result_free(&results_obj
);
574 static test_return_t
cas_test(memcached_st
*memc
)
576 memcached_return_t rc
;
577 const char *key
= "fun";
578 size_t key_length
= strlen(key
);
579 const char *value
= "we the people";
580 const char* keys
[2] = { key
, NULL
};
581 size_t keylengths
[2] = { strlen(key
), 0 };
582 size_t value_length
= strlen(value
);
583 const char *value2
= "change the value";
584 size_t value2_length
= strlen(value2
);
586 memcached_result_st results_obj
;
587 memcached_result_st
*results
;
590 rc
= memcached_flush(memc
, 0);
591 test_true(rc
== MEMCACHED_SUCCESS
);
593 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
595 rc
= memcached_set(memc
, key
, strlen(key
),
596 value
, strlen(value
),
597 (time_t)0, (uint32_t)0);
598 test_true(rc
== MEMCACHED_SUCCESS
);
600 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
602 results
= memcached_result_create(memc
, &results_obj
);
604 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
606 test_true(rc
== MEMCACHED_SUCCESS
);
607 test_true(memcached_result_cas(results
));
608 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
609 test_true(strlen(memcached_result_value(results
)) == value_length
);
610 test_true(rc
== MEMCACHED_SUCCESS
);
611 uint64_t cas
= memcached_result_cas(results
);
614 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
615 test_true(rc
== MEMCACHED_END
);
616 test_true(results
== NULL
);
619 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
620 test_true(rc
== MEMCACHED_SUCCESS
);
623 * The item will have a new cas value, so try to set it again with the old
624 * value. This should fail!
626 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
627 test_true(rc
== MEMCACHED_DATA_EXISTS
);
629 memcached_result_free(&results_obj
);
634 static test_return_t
prepend_test(memcached_st
*memc
)
636 memcached_return_t rc
;
637 const char *key
= "fig";
638 const char *value
= "people";
639 char *out_value
= NULL
;
643 rc
= memcached_flush(memc
, 0);
644 test_true(rc
== MEMCACHED_SUCCESS
);
646 rc
= memcached_set(memc
, key
, strlen(key
),
647 value
, strlen(value
),
648 (time_t)0, (uint32_t)0);
649 test_true(rc
== MEMCACHED_SUCCESS
);
651 rc
= memcached_prepend(memc
, key
, strlen(key
),
652 "the ", strlen("the "),
653 (time_t)0, (uint32_t)0);
654 test_true(rc
== MEMCACHED_SUCCESS
);
656 rc
= memcached_prepend(memc
, key
, strlen(key
),
657 "we ", strlen("we "),
658 (time_t)0, (uint32_t)0);
659 test_true(rc
== MEMCACHED_SUCCESS
);
661 out_value
= memcached_get(memc
, key
, strlen(key
),
662 &value_length
, &flags
, &rc
);
663 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
664 test_true(strlen("we the people") == value_length
);
665 test_true(rc
== MEMCACHED_SUCCESS
);
672 Set the value, then quit to make sure it is flushed.
673 Come back in and test that add fails.
675 static test_return_t
add_test(memcached_st
*memc
)
677 memcached_return_t rc
;
678 const char *key
= "foo";
679 const char *value
= "when we sanitize";
680 unsigned long long setting_value
;
682 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
684 rc
= memcached_set(memc
, key
, strlen(key
),
685 value
, strlen(value
),
686 (time_t)0, (uint32_t)0);
687 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
688 memcached_quit(memc
);
689 rc
= memcached_add(memc
, key
, strlen(key
),
690 value
, strlen(value
),
691 (time_t)0, (uint32_t)0);
693 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
696 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
700 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
707 ** There was a problem of leaking filedescriptors in the initial release
708 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
709 ** systems it seems that the kernel is slow on reclaiming the resources
710 ** because the connects starts to time out (the test doesn't do much
711 ** anyway, so just loop 10 iterations)
713 static test_return_t
add_wrapper(memcached_st
*memc
)
715 unsigned int max
= 10000;
723 for (uint32_t x
= 0; x
< max
; x
++)
729 static test_return_t
replace_test(memcached_st
*memc
)
731 memcached_return_t rc
;
732 const char *key
= "foo";
733 const char *value
= "when we sanitize";
734 const char *original
= "first we insert some data";
736 rc
= memcached_set(memc
, key
, strlen(key
),
737 original
, strlen(original
),
738 (time_t)0, (uint32_t)0);
739 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
741 rc
= memcached_replace(memc
, key
, strlen(key
),
742 value
, strlen(value
),
743 (time_t)0, (uint32_t)0);
744 test_true(rc
== MEMCACHED_SUCCESS
);
749 static test_return_t
delete_test(memcached_st
*memc
)
751 memcached_return_t rc
;
752 const char *key
= "foo";
753 const char *value
= "when we sanitize";
755 rc
= memcached_set(memc
, key
, strlen(key
),
756 value
, strlen(value
),
757 (time_t)0, (uint32_t)0);
758 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
760 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
761 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
766 static test_return_t
flush_test(memcached_st
*memc
)
768 memcached_return_t rc
;
770 rc
= memcached_flush(memc
, 0);
771 test_true(rc
== MEMCACHED_SUCCESS
);
776 static memcached_return_t
server_function(const memcached_st
*ptr
,
777 const memcached_server_st
*server
,
780 (void)ptr
; (void)server
; (void)context
;
783 return MEMCACHED_SUCCESS
;
786 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
789 strncpy(context
, "foo bad", sizeof(context
));
790 memcached_server_fn callbacks
[1];
792 callbacks
[0]= server_function
;
793 memcached_server_cursor(memc
, callbacks
, context
, 1);
797 static test_return_t
bad_key_test(memcached_st
*memc
)
799 memcached_return_t rc
;
800 const char *key
= "foo bad";
802 size_t string_length
;
804 memcached_st
*memc_clone
;
806 size_t max_keylen
= 0xffff;
808 // Just skip if we are in binary mode.
809 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
812 memc_clone
= memcached_clone(NULL
, memc
);
813 test_true(memc_clone
);
815 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
816 test_true(rc
== MEMCACHED_SUCCESS
);
818 /* All keys are valid in the binary protocol (except for length) */
819 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
821 string
= memcached_get(memc_clone
, key
, strlen(key
),
822 &string_length
, &flags
, &rc
);
823 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
824 test_true(string_length
== 0);
828 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
829 test_true(rc
== MEMCACHED_SUCCESS
);
830 string
= memcached_get(memc_clone
, key
, strlen(key
),
831 &string_length
, &flags
, &rc
);
832 test_true(rc
== MEMCACHED_NOTFOUND
);
833 test_true(string_length
== 0);
836 /* Test multi key for bad keys */
837 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
838 size_t key_lengths
[] = { 7, 7, 7 };
840 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
841 test_true(rc
== MEMCACHED_SUCCESS
);
843 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
844 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
846 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
847 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
851 /* The following test should be moved to the end of this function when the
852 memcached server is updated to allow max size length of the keys in the
855 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
856 test_true(rc
== MEMCACHED_SUCCESS
);
858 char *longkey
= (char *)malloc(max_keylen
+ 1);
861 memset(longkey
, 'a', max_keylen
+ 1);
862 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
863 &string_length
, &flags
, &rc
);
864 test_true(rc
== MEMCACHED_NOTFOUND
);
865 test_true(string_length
== 0);
868 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
869 &string_length
, &flags
, &rc
);
870 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
871 test_true(string_length
== 0);
878 /* Make sure zero length keys are marked as bad */
880 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
881 test_true(rc
== MEMCACHED_SUCCESS
);
882 string
= memcached_get(memc_clone
, key
, 0,
883 &string_length
, &flags
, &rc
);
884 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
885 test_true(string_length
== 0);
888 memcached_free(memc_clone
);
893 #define READ_THROUGH_VALUE "set for me"
894 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
897 memcached_result_st
*result
)
899 (void)memc
;(void)key
;(void)key_length
;
900 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
903 static test_return_t
read_through(memcached_st
*memc
)
905 memcached_return_t rc
;
906 const char *key
= "foo";
908 size_t string_length
;
910 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
912 string
= memcached_get(memc
, key
, strlen(key
),
913 &string_length
, &flags
, &rc
);
915 test_true(rc
== MEMCACHED_NOTFOUND
);
916 test_false(string_length
);
919 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
921 test_true(rc
== MEMCACHED_SUCCESS
);
923 string
= memcached_get(memc
, key
, strlen(key
),
924 &string_length
, &flags
, &rc
);
926 test_true(rc
== MEMCACHED_SUCCESS
);
927 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
928 test_strcmp(READ_THROUGH_VALUE
, string
);
931 string
= memcached_get(memc
, key
, strlen(key
),
932 &string_length
, &flags
, &rc
);
934 test_true(rc
== MEMCACHED_SUCCESS
);
935 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
936 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
942 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
946 (void)ptr
;(void)key_length
;
949 return MEMCACHED_SUCCESS
;
952 static test_return_t
delete_through(memcached_st
*memc
)
954 memcached_trigger_delete_key_fn callback
;
955 memcached_return_t rc
;
957 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
959 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
960 test_true(rc
== MEMCACHED_SUCCESS
);
965 static test_return_t
get_test(memcached_st
*memc
)
967 memcached_return_t rc
;
968 const char *key
= "foo";
970 size_t string_length
;
973 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
974 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
976 string
= memcached_get(memc
, key
, strlen(key
),
977 &string_length
, &flags
, &rc
);
979 test_true(rc
== MEMCACHED_NOTFOUND
);
980 test_false(string_length
);
986 static test_return_t
get_test2(memcached_st
*memc
)
988 memcached_return_t rc
;
989 const char *key
= "foo";
990 const char *value
= "when we sanitize";
992 size_t string_length
;
995 rc
= memcached_set(memc
, key
, strlen(key
),
996 value
, strlen(value
),
997 (time_t)0, (uint32_t)0);
998 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1000 string
= memcached_get(memc
, key
, strlen(key
),
1001 &string_length
, &flags
, &rc
);
1004 test_true(rc
== MEMCACHED_SUCCESS
);
1005 test_true(string_length
== strlen(value
));
1006 test_true(!memcmp(string
, value
, string_length
));
1010 return TEST_SUCCESS
;
1013 static test_return_t
set_test2(memcached_st
*memc
)
1015 memcached_return_t rc
;
1016 const char *key
= "foo";
1017 const char *value
= "train in the brain";
1018 size_t value_length
= strlen(value
);
1021 for (x
= 0; x
< 10; x
++)
1023 rc
= memcached_set(memc
, key
, strlen(key
),
1024 value
, value_length
,
1025 (time_t)0, (uint32_t)0);
1026 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1029 return TEST_SUCCESS
;
1032 static test_return_t
set_test3(memcached_st
*memc
)
1034 memcached_return_t rc
;
1036 size_t value_length
= 8191;
1039 value
= (char*)malloc(value_length
);
1042 for (x
= 0; x
< value_length
; x
++)
1043 value
[x
] = (char) (x
% 127);
1045 /* The dump test relies on there being at least 32 items in memcached */
1046 for (x
= 0; x
< 32; x
++)
1050 snprintf(key
, sizeof(key
), "foo%u", x
);
1052 rc
= memcached_set(memc
, key
, strlen(key
),
1053 value
, value_length
,
1054 (time_t)0, (uint32_t)0);
1055 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1060 return TEST_SUCCESS
;
1063 static test_return_t
get_test3(memcached_st
*memc
)
1065 memcached_return_t rc
;
1066 const char *key
= "foo";
1068 size_t value_length
= 8191;
1070 size_t string_length
;
1074 value
= (char*)malloc(value_length
);
1077 for (x
= 0; x
< value_length
; x
++)
1078 value
[x
] = (char) (x
% 127);
1080 rc
= memcached_set(memc
, key
, strlen(key
),
1081 value
, value_length
,
1082 (time_t)0, (uint32_t)0);
1083 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1085 string
= memcached_get(memc
, key
, strlen(key
),
1086 &string_length
, &flags
, &rc
);
1088 test_true(rc
== MEMCACHED_SUCCESS
);
1090 test_true(string_length
== value_length
);
1091 test_true(!memcmp(string
, value
, string_length
));
1096 return TEST_SUCCESS
;
1099 static test_return_t
get_test4(memcached_st
*memc
)
1101 memcached_return_t rc
;
1102 const char *key
= "foo";
1104 size_t value_length
= 8191;
1106 size_t string_length
;
1110 value
= (char*)malloc(value_length
);
1113 for (x
= 0; x
< value_length
; x
++)
1114 value
[x
] = (char) (x
% 127);
1116 rc
= memcached_set(memc
, key
, strlen(key
),
1117 value
, value_length
,
1118 (time_t)0, (uint32_t)0);
1119 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1121 for (x
= 0; x
< 10; x
++)
1123 string
= memcached_get(memc
, key
, strlen(key
),
1124 &string_length
, &flags
, &rc
);
1126 test_true(rc
== MEMCACHED_SUCCESS
);
1128 test_true(string_length
== value_length
);
1129 test_true(!memcmp(string
, value
, string_length
));
1135 return TEST_SUCCESS
;
1139 * This test verifies that memcached_read_one_response doesn't try to
1140 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1141 * responses before you execute a storage command.
1143 static test_return_t
get_test5(memcached_st
*memc
)
1146 ** Request the same key twice, to ensure that we hash to the same server
1147 ** (so that we have multiple response values queued up) ;-)
1149 const char *keys
[]= { "key", "key" };
1150 size_t lengths
[]= { 3, 3 };
1154 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1155 keys
[0], lengths
[0], 0, 0);
1156 test_true(rc
== MEMCACHED_SUCCESS
);
1157 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1159 memcached_result_st results_obj
;
1160 memcached_result_st
*results
;
1161 results
=memcached_result_create(memc
, &results_obj
);
1163 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1165 memcached_result_free(&results_obj
);
1167 /* Don't read out the second result, but issue a set instead.. */
1168 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1169 test_true(rc
== MEMCACHED_SUCCESS
);
1171 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1172 &rlen
, &flags
, &rc
);
1173 test_true(val
== NULL
);
1174 test_true(rc
== MEMCACHED_NOTFOUND
);
1175 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1176 test_true(val
!= NULL
);
1177 test_true(rc
== MEMCACHED_SUCCESS
);
1180 return TEST_SUCCESS
;
1183 static test_return_t
mget_end(memcached_st
*memc
)
1185 const char *keys
[]= { "foo", "foo2" };
1186 size_t lengths
[]= { 3, 4 };
1187 const char *values
[]= { "fjord", "41" };
1189 memcached_return_t rc
;
1192 for (int i
= 0; i
< 2; i
++)
1194 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1195 (time_t)0, (uint32_t)0);
1196 test_true(rc
== MEMCACHED_SUCCESS
);
1200 size_t string_length
;
1203 // retrieve both via mget
1204 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1205 test_true(rc
== MEMCACHED_SUCCESS
);
1207 char key
[MEMCACHED_MAX_KEY
];
1210 // this should get both
1211 for (int i
= 0; i
< 2; i
++)
1213 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1215 test_true(rc
== MEMCACHED_SUCCESS
);
1217 if (key_length
== 4)
1219 test_true(string_length
== strlen(values
[val
]));
1220 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1224 // this should indicate end
1225 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1226 test_true(rc
== MEMCACHED_END
);
1229 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1230 test_true(rc
== MEMCACHED_SUCCESS
);
1232 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1233 test_true(key_length
== lengths
[0]);
1234 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1235 test_true(string_length
== strlen(values
[0]));
1236 test_true(strncmp(values
[0], string
, string_length
) == 0);
1237 test_true(rc
== MEMCACHED_SUCCESS
);
1240 // this should indicate end
1241 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1242 test_true(rc
== MEMCACHED_END
);
1244 return TEST_SUCCESS
;
1247 /* Do not copy the style of this code, I just access hosts to testthis function */
1248 static test_return_t
stats_servername_test(memcached_st
*memc
)
1250 memcached_return_t rc
;
1251 memcached_stat_st memc_stat
;
1252 memcached_server_instance_st instance
=
1253 memcached_server_instance_by_position(memc
, 0);
1255 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1256 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1257 return TEST_SKIPPED
;
1259 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1260 memcached_server_name(instance
),
1261 memcached_server_port(instance
));
1263 return TEST_SUCCESS
;
1266 static test_return_t
increment_test(memcached_st
*memc
)
1268 uint64_t new_number
;
1269 memcached_return_t rc
;
1270 const char *key
= "number";
1271 const char *value
= "0";
1273 rc
= memcached_set(memc
, key
, strlen(key
),
1274 value
, strlen(value
),
1275 (time_t)0, (uint32_t)0);
1276 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1278 rc
= memcached_increment(memc
, key
, strlen(key
),
1280 test_true(rc
== MEMCACHED_SUCCESS
);
1281 test_true(new_number
== 1);
1283 rc
= memcached_increment(memc
, key
, strlen(key
),
1285 test_true(rc
== MEMCACHED_SUCCESS
);
1286 test_true(new_number
== 2);
1288 return TEST_SUCCESS
;
1291 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1293 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1295 uint64_t new_number
;
1296 memcached_return_t rc
;
1297 const char *key
= "number";
1298 uint64_t initial
= 0;
1300 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1301 1, initial
, 0, &new_number
);
1302 test_true(rc
== MEMCACHED_SUCCESS
);
1303 test_true(new_number
== initial
);
1305 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1306 1, initial
, 0, &new_number
);
1307 test_true(rc
== MEMCACHED_SUCCESS
);
1308 test_true(new_number
== (initial
+ 1));
1310 return TEST_SUCCESS
;
1313 static test_return_t
decrement_test(memcached_st
*memc
)
1315 uint64_t new_number
;
1316 memcached_return_t rc
;
1317 const char *key
= "number";
1318 const char *value
= "3";
1320 rc
= memcached_set(memc
, key
, strlen(key
),
1321 value
, strlen(value
),
1322 (time_t)0, (uint32_t)0);
1323 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1325 rc
= memcached_decrement(memc
, key
, strlen(key
),
1327 test_true(rc
== MEMCACHED_SUCCESS
);
1328 test_true(new_number
== 2);
1330 rc
= memcached_decrement(memc
, key
, strlen(key
),
1332 test_true(rc
== MEMCACHED_SUCCESS
);
1333 test_true(new_number
== 1);
1335 return TEST_SUCCESS
;
1338 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1340 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1342 uint64_t new_number
;
1343 memcached_return_t rc
;
1344 const char *key
= "number";
1345 uint64_t initial
= 3;
1347 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1348 1, initial
, 0, &new_number
);
1349 test_true(rc
== MEMCACHED_SUCCESS
);
1350 test_true(new_number
== initial
);
1352 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1353 1, initial
, 0, &new_number
);
1354 test_true(rc
== MEMCACHED_SUCCESS
);
1355 test_true(new_number
== (initial
- 1));
1357 return TEST_SUCCESS
;
1360 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1362 uint64_t new_number
;
1363 memcached_return_t rc
;
1364 const char *master_key
= "foo";
1365 const char *key
= "number";
1366 const char *value
= "0";
1368 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1370 value
, strlen(value
),
1371 (time_t)0, (uint32_t)0);
1372 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1374 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1376 test_true(rc
== MEMCACHED_SUCCESS
);
1377 test_true(new_number
== 1);
1379 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1381 test_true(rc
== MEMCACHED_SUCCESS
);
1382 test_true(new_number
== 2);
1384 return TEST_SUCCESS
;
1387 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1389 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1391 uint64_t new_number
;
1392 memcached_return_t rc
;
1393 const char *master_key
= "foo";
1394 const char *key
= "number";
1395 uint64_t initial
= 0;
1397 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1399 1, initial
, 0, &new_number
);
1400 test_true(rc
== MEMCACHED_SUCCESS
);
1401 test_true(new_number
== initial
);
1403 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1405 1, initial
, 0, &new_number
);
1406 test_true(rc
== MEMCACHED_SUCCESS
);
1407 test_true(new_number
== (initial
+ 1));
1409 return TEST_SUCCESS
;
1412 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1414 uint64_t new_number
;
1415 memcached_return_t rc
;
1416 const char *master_key
= "foo";
1417 const char *key
= "number";
1418 const char *value
= "3";
1420 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1422 value
, strlen(value
),
1423 (time_t)0, (uint32_t)0);
1424 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1426 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1429 test_true(rc
== MEMCACHED_SUCCESS
);
1430 test_true(new_number
== 2);
1432 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1435 test_true(rc
== MEMCACHED_SUCCESS
);
1436 test_true(new_number
== 1);
1438 return TEST_SUCCESS
;
1441 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1443 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1445 uint64_t new_number
;
1446 memcached_return_t rc
;
1447 const char *master_key
= "foo";
1448 const char *key
= "number";
1449 uint64_t initial
= 3;
1451 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1453 1, initial
, 0, &new_number
);
1454 test_true(rc
== MEMCACHED_SUCCESS
);
1455 test_true(new_number
== initial
);
1457 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1459 1, initial
, 0, &new_number
);
1460 test_true(rc
== MEMCACHED_SUCCESS
);
1461 test_true(new_number
== (initial
- 1));
1463 return TEST_SUCCESS
;
1466 static test_return_t
quit_test(memcached_st
*memc
)
1468 memcached_return_t rc
;
1469 const char *key
= "fudge";
1470 const char *value
= "sanford and sun";
1472 rc
= memcached_set(memc
, key
, strlen(key
),
1473 value
, strlen(value
),
1474 (time_t)10, (uint32_t)3);
1475 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1476 memcached_quit(memc
);
1478 rc
= memcached_set(memc
, key
, strlen(key
),
1479 value
, strlen(value
),
1480 (time_t)50, (uint32_t)9);
1481 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1483 return TEST_SUCCESS
;
1486 static test_return_t
mget_result_test(memcached_st
*memc
)
1488 memcached_return_t rc
;
1489 const char *keys
[]= {"fudge", "son", "food"};
1490 size_t key_length
[]= {5, 3, 4};
1493 memcached_result_st results_obj
;
1494 memcached_result_st
*results
;
1496 results
= memcached_result_create(memc
, &results_obj
);
1498 test_true(&results_obj
== results
);
1500 /* We need to empty the server before continueing test */
1501 rc
= memcached_flush(memc
, 0);
1502 test_true(rc
== MEMCACHED_SUCCESS
);
1504 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1505 test_true(rc
== MEMCACHED_SUCCESS
);
1507 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1512 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1513 test_true(!results
);
1514 test_true(rc
== MEMCACHED_END
);
1516 for (x
= 0; x
< 3; x
++)
1518 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1519 keys
[x
], key_length
[x
],
1520 (time_t)50, (uint32_t)9);
1521 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1524 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1525 test_true(rc
== MEMCACHED_SUCCESS
);
1527 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1530 test_true(&results_obj
== results
);
1531 test_true(rc
== MEMCACHED_SUCCESS
);
1532 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1533 test_true(!memcmp(memcached_result_key_value(results
),
1534 memcached_result_value(results
),
1535 memcached_result_length(results
)));
1538 memcached_result_free(&results_obj
);
1540 return TEST_SUCCESS
;
1543 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1545 memcached_return_t rc
;
1546 const char *keys
[]= {"fudge", "son", "food"};
1547 size_t key_length
[]= {5, 3, 4};
1550 memcached_result_st
*results
;
1552 /* We need to empty the server before continueing test */
1553 rc
= memcached_flush(memc
, 0);
1554 test_true(rc
== MEMCACHED_SUCCESS
);
1556 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1557 test_true(rc
== MEMCACHED_SUCCESS
);
1559 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1563 test_true(!results
);
1564 test_true(rc
== MEMCACHED_END
);
1566 for (x
= 0; x
< 3; x
++)
1568 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1569 keys
[x
], key_length
[x
],
1570 (time_t)50, (uint32_t)9);
1571 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1574 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1575 test_true(rc
== MEMCACHED_SUCCESS
);
1578 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1581 test_true(rc
== MEMCACHED_SUCCESS
);
1582 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1583 test_true(!memcmp(memcached_result_key_value(results
),
1584 memcached_result_value(results
),
1585 memcached_result_length(results
)));
1586 memcached_result_free(results
);
1590 return TEST_SUCCESS
;
1593 /* Count the results */
1594 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1595 memcached_result_st
*result
,
1598 (void)ptr
; (void)result
;
1599 size_t *counter
= (size_t *)context
;
1601 *counter
= *counter
+ 1;
1603 return MEMCACHED_SUCCESS
;
1606 static test_return_t
mget_result_function(memcached_st
*memc
)
1608 memcached_return_t rc
;
1609 const char *keys
[]= {"fudge", "son", "food"};
1610 size_t key_length
[]= {5, 3, 4};
1613 memcached_execute_fn callbacks
[1];
1615 /* We need to empty the server before continueing test */
1616 rc
= memcached_flush(memc
, 0);
1617 for (x
= 0; x
< 3; x
++)
1619 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1620 keys
[x
], key_length
[x
],
1621 (time_t)50, (uint32_t)9);
1622 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1625 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1626 test_true(rc
== MEMCACHED_SUCCESS
);
1628 callbacks
[0]= &callback_counter
;
1630 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1632 test_true(counter
== 3);
1634 return TEST_SUCCESS
;
1637 static test_return_t
mget_test(memcached_st
*memc
)
1639 memcached_return_t rc
;
1640 const char *keys
[]= {"fudge", "son", "food"};
1641 size_t key_length
[]= {5, 3, 4};
1645 char return_key
[MEMCACHED_MAX_KEY
];
1646 size_t return_key_length
;
1648 size_t return_value_length
;
1650 /* We need to empty the server before continueing test */
1651 rc
= memcached_flush(memc
, 0);
1652 test_true(rc
== MEMCACHED_SUCCESS
);
1654 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1655 test_true(rc
== MEMCACHED_SUCCESS
);
1657 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1658 &return_value_length
, &flags
, &rc
)) != NULL
)
1660 test_true(return_value
);
1662 test_true(!return_value
);
1663 test_true(return_value_length
== 0);
1664 test_true(rc
== MEMCACHED_END
);
1666 for (x
= 0; x
< 3; x
++)
1668 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1669 keys
[x
], key_length
[x
],
1670 (time_t)50, (uint32_t)9);
1671 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1674 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1675 test_true(rc
== MEMCACHED_SUCCESS
);
1678 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1679 &return_value_length
, &flags
, &rc
)))
1681 test_true(return_value
);
1682 test_true(rc
== MEMCACHED_SUCCESS
);
1683 test_true(return_key_length
== return_value_length
);
1684 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1689 return TEST_SUCCESS
;
1692 static test_return_t
mget_execute(memcached_st
*memc
)
1696 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1700 * I only want to hit _one_ server so I know the number of requests I'm
1701 * sending in the pipeline.
1703 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1704 memc
->number_of_hosts
= 1;
1706 size_t max_keys
= 20480;
1709 char **keys
= calloc(max_keys
, sizeof(char*));
1710 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1712 /* First add all of the items.. */
1713 char blob
[1024] = {0};
1714 memcached_return_t rc
;
1716 for (size_t x
= 0; x
< max_keys
; ++x
)
1720 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1722 test_true(keys
[x
] != NULL
);
1723 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1724 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1727 /* Try to get all of them with a large multiget */
1729 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1730 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1731 max_keys
, callbacks
, &counter
, 1);
1733 if (rc
== MEMCACHED_SUCCESS
)
1736 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1737 test_true(rc
== MEMCACHED_END
);
1739 /* Verify that we got all of the items */
1740 test_true(counter
== max_keys
);
1742 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1744 test_true(counter
== 0);
1748 test_fail("note: this test functions differently when in binary mode");
1751 /* Release all allocated resources */
1752 for (size_t x
= 0; x
< max_keys
; ++x
)
1759 memc
->number_of_hosts
= number_of_hosts
;
1760 return TEST_SUCCESS
;
1763 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1765 static test_return_t
key_setup(memcached_st
*memc
)
1769 if (pre_binary(memc
) != TEST_SUCCESS
)
1770 return TEST_SKIPPED
;
1772 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1774 return TEST_SUCCESS
;
1777 static test_return_t
key_teardown(memcached_st
*memc
)
1780 pairs_free(global_pairs
);
1782 return TEST_SUCCESS
;
1785 static test_return_t
block_add_regression(memcached_st
*memc
)
1787 /* First add all of the items.. */
1788 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1790 memcached_return_t rc
;
1791 char blob
[1024] = {0};
1793 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1794 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1797 return TEST_SUCCESS
;
1800 static test_return_t
binary_add_regression(memcached_st
*memc
)
1802 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1803 test_return_t rc
= block_add_regression(memc
);
1804 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1808 static test_return_t
get_stats_keys(memcached_st
*memc
)
1812 memcached_stat_st memc_stat
;
1813 memcached_return_t rc
;
1815 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1816 test_true(rc
== MEMCACHED_SUCCESS
);
1817 for (ptr
= stat_list
; *ptr
; ptr
++)
1822 return TEST_SUCCESS
;
1825 static test_return_t
version_string_test(memcached_st
*memc
)
1827 const char *version_string
;
1830 version_string
= memcached_lib_version();
1832 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1834 return TEST_SUCCESS
;
1837 static test_return_t
get_stats(memcached_st
*memc
)
1841 memcached_return_t rc
;
1842 memcached_stat_st
*memc_stat
;
1844 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1845 test_true(rc
== MEMCACHED_SUCCESS
);
1847 test_true(rc
== MEMCACHED_SUCCESS
);
1848 test_true(memc_stat
);
1850 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1852 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1853 test_true(rc
== MEMCACHED_SUCCESS
);
1854 for (ptr
= stat_list
; *ptr
; ptr
++);
1859 memcached_stat_free(NULL
, memc_stat
);
1861 return TEST_SUCCESS
;
1864 static test_return_t
add_host_test(memcached_st
*memc
)
1867 memcached_server_st
*servers
;
1868 memcached_return_t rc
;
1869 char servername
[]= "0.example.com";
1871 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1873 test_true(1 == memcached_server_list_count(servers
));
1875 for (x
= 2; x
< 20; x
++)
1877 char buffer
[SMALL_STRING_LEN
];
1879 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1880 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1882 test_true(rc
== MEMCACHED_SUCCESS
);
1883 test_true(x
== memcached_server_list_count(servers
));
1886 rc
= memcached_server_push(memc
, servers
);
1887 test_true(rc
== MEMCACHED_SUCCESS
);
1888 rc
= memcached_server_push(memc
, servers
);
1889 test_true(rc
== MEMCACHED_SUCCESS
);
1891 memcached_server_list_free(servers
);
1893 return TEST_SUCCESS
;
1896 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1898 (void)parent
;(void)memc_clone
;
1899 return MEMCACHED_SUCCESS
;
1902 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1905 return MEMCACHED_SUCCESS
;
1908 static test_return_t
callback_test(memcached_st
*memc
)
1910 /* Test User Data */
1914 memcached_return_t rc
;
1916 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1917 test_true(rc
== MEMCACHED_SUCCESS
);
1918 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1919 test_true(*test_ptr
== x
);
1922 /* Test Clone Callback */
1924 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1925 void *clone_cb_ptr
= *(void **)&clone_cb
;
1926 void *temp_function
= NULL
;
1927 memcached_return_t rc
;
1929 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1931 test_true(rc
== MEMCACHED_SUCCESS
);
1932 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1933 test_true(temp_function
== clone_cb_ptr
);
1936 /* Test Cleanup Callback */
1938 memcached_cleanup_fn cleanup_cb
=
1939 (memcached_cleanup_fn
)cleanup_test_callback
;
1940 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1941 void *temp_function
= NULL
;
1942 memcached_return_t rc
;
1944 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1946 test_true(rc
== MEMCACHED_SUCCESS
);
1947 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1948 test_true(temp_function
== cleanup_cb_ptr
);
1951 return TEST_SUCCESS
;
1954 /* We don't test the behavior itself, we test the switches */
1955 static test_return_t
behavior_test(memcached_st
*memc
)
1960 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1961 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1962 test_true(value
== 1);
1964 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1965 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1966 test_true(value
== 1);
1968 set
= MEMCACHED_HASH_MD5
;
1969 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1970 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1971 test_true(value
== MEMCACHED_HASH_MD5
);
1975 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1976 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1977 test_true(value
== 0);
1979 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1980 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1981 test_true(value
== 0);
1983 set
= MEMCACHED_HASH_DEFAULT
;
1984 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1985 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1986 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1988 set
= MEMCACHED_HASH_CRC
;
1989 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1990 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1991 test_true(value
== MEMCACHED_HASH_CRC
);
1993 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1994 test_true(value
> 0);
1996 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1997 test_true(value
> 0);
1999 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
2000 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
2001 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2003 return TEST_SUCCESS
;
2006 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2008 memcached_return_t rc
;
2011 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2012 test_true(rc
== MEMCACHED_DEPRECATED
);
2014 // Platform dependent
2016 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2020 return TEST_SUCCESS
;
2024 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2026 memcached_return_t rc
;
2030 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2031 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2033 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2035 if (rc
== MEMCACHED_SUCCESS
)
2037 test_true((bool)value
== set
);
2041 test_false((bool)value
== set
);
2044 return TEST_SUCCESS
;
2048 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2050 memcached_return_t rc
;
2054 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2055 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2057 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2059 if (rc
== MEMCACHED_SUCCESS
)
2061 test_true((bool)value
== set
);
2065 test_false((bool)value
== set
);
2068 return TEST_SUCCESS
;
2071 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2073 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2074 char return_key
[MEMCACHED_MAX_KEY
];
2075 size_t return_key_length
;
2077 size_t return_value_length
;
2082 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2083 &return_value_length
, &flags
, &rc
)))
2085 test_true(return_value
);
2086 test_true(rc
== MEMCACHED_SUCCESS
);
2088 *keys_returned
= *keys_returned
+1;
2091 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2093 return TEST_SUCCESS
;
2096 /* Test case provided by Cal Haldenbrand */
2097 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2099 unsigned int setter
= 1;
2101 unsigned long long total
= 0;
2104 char randomstuff
[6 * 1024];
2105 memcached_return_t rc
;
2107 memset(randomstuff
, 0, 6 * 1024);
2109 /* We just keep looking at the same values over and over */
2112 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2113 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2117 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2121 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2122 memset(randomstuff
, 0, 6 * 1024);
2123 test_true(size
< 6 * 1024); /* Being safe here */
2125 for (j
= 0 ; j
< size
;j
++)
2126 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2129 snprintf(key
, sizeof(key
), "%u", x
);
2130 rc
= memcached_set(memc
, key
, strlen(key
),
2131 randomstuff
, strlen(randomstuff
), 10, 0);
2132 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2133 /* If we fail, lets try again */
2134 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2135 rc
= memcached_set(memc
, key
, strlen(key
),
2136 randomstuff
, strlen(randomstuff
), 10, 0);
2137 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2140 return TEST_SUCCESS
;
2143 /* Test case provided by Cal Haldenbrand */
2144 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2146 unsigned int setter
;
2150 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2151 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2153 setter
= 20 * 1024576;
2154 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2155 setter
= 20 * 1024576;
2156 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2157 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2158 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2160 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2163 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2165 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2166 char buffer
[SMALL_STRING_LEN
];
2171 memset(buffer
, 0, SMALL_STRING_LEN
);
2173 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2174 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2175 &val_len
, &flags
, &rc
);
2176 if (rc
!= MEMCACHED_SUCCESS
)
2178 if (rc
== MEMCACHED_NOTFOUND
)
2192 return TEST_SUCCESS
;
2195 /* Do a large mget() over all the keys we think exist */
2196 #define KEY_COUNT 3000 // * 1024576
2197 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2199 memcached_return_t rc
;
2200 unsigned int setter
;
2203 size_t key_lengths
[KEY_COUNT
];
2206 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2207 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2209 setter
= 20 * 1024576;
2210 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2211 setter
= 20 * 1024576;
2212 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2213 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2214 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2217 keys
= calloc(KEY_COUNT
, sizeof(char *));
2219 for (x
= 0; x
< KEY_COUNT
; x
++)
2223 snprintf(buffer
, 30, "%u", x
);
2224 keys
[x
]= strdup(buffer
);
2225 key_lengths
[x
]= strlen(keys
[x
]);
2228 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2229 test_true(rc
== MEMCACHED_SUCCESS
);
2231 size_t keys_returned
;
2232 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2234 for (x
= 0; x
< KEY_COUNT
; x
++)
2238 return TEST_SUCCESS
;
2241 /* Make sure we behave properly if server list has no values */
2242 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2244 memcached_return_t rc
;
2245 const char *keys
[]= {"fudge", "son", "food"};
2246 size_t key_length
[]= {5, 3, 4};
2249 char return_key
[MEMCACHED_MAX_KEY
];
2250 size_t return_key_length
;
2252 size_t return_value_length
;
2254 /* Here we free everything before running a bunch of mget tests */
2255 memcached_servers_reset(memc
);
2258 /* We need to empty the server before continueing test */
2259 rc
= memcached_flush(memc
, 0);
2260 test_true(rc
== MEMCACHED_NO_SERVERS
);
2262 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2263 test_true(rc
== MEMCACHED_NO_SERVERS
);
2265 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2266 &return_value_length
, &flags
, &rc
)) != NULL
)
2268 test_true(return_value
);
2270 test_true(!return_value
);
2271 test_true(return_value_length
== 0);
2272 test_true(rc
== MEMCACHED_NO_SERVERS
);
2274 for (x
= 0; x
< 3; x
++)
2276 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2277 keys
[x
], key_length
[x
],
2278 (time_t)50, (uint32_t)9);
2279 test_true(rc
== MEMCACHED_NO_SERVERS
);
2282 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2283 test_true(rc
== MEMCACHED_NO_SERVERS
);
2286 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2287 &return_value_length
, &flags
, &rc
)))
2289 test_true(return_value
);
2290 test_true(rc
== MEMCACHED_SUCCESS
);
2291 test_true(return_key_length
== return_value_length
);
2292 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2297 return TEST_SUCCESS
;
2300 #define VALUE_SIZE_BUG5 1048064
2301 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2303 memcached_return_t rc
;
2304 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2305 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2306 char return_key
[MEMCACHED_MAX_KEY
];
2307 size_t return_key_length
;
2309 size_t value_length
;
2313 char insert_data
[VALUE_SIZE_BUG5
];
2315 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2316 insert_data
[x
]= (signed char)rand();
2318 memcached_flush(memc
, 0);
2319 value
= memcached_get(memc
, keys
[0], key_length
[0],
2320 &value_length
, &flags
, &rc
);
2321 test_true(value
== NULL
);
2322 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2325 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2326 &value_length
, &flags
, &rc
)))
2328 test_true(count
== 0);
2330 for (x
= 0; x
< 4; x
++)
2332 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2333 insert_data
, VALUE_SIZE_BUG5
,
2334 (time_t)0, (uint32_t)0);
2335 test_true(rc
== MEMCACHED_SUCCESS
);
2338 for (x
= 0; x
< 10; x
++)
2340 value
= memcached_get(memc
, keys
[0], key_length
[0],
2341 &value_length
, &flags
, &rc
);
2345 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2347 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2348 &value_length
, &flags
, &rc
)))
2353 test_true(count
== 4);
2356 return TEST_SUCCESS
;
2359 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2361 memcached_return_t rc
;
2362 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2363 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2364 char return_key
[MEMCACHED_MAX_KEY
];
2365 size_t return_key_length
;
2367 size_t value_length
;
2371 char insert_data
[VALUE_SIZE_BUG5
];
2373 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2374 insert_data
[x
]= (signed char)rand();
2376 memcached_flush(memc
, 0);
2377 value
= memcached_get(memc
, keys
[0], key_length
[0],
2378 &value_length
, &flags
, &rc
);
2379 test_true(value
== NULL
);
2380 test_true(rc
== MEMCACHED_NOTFOUND
);
2381 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2382 test_true(rc
== MEMCACHED_SUCCESS
);
2385 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2386 &value_length
, &flags
, &rc
)))
2388 test_true(count
== 0);
2389 test_true(rc
== MEMCACHED_END
);
2391 for (x
= 0; x
< 4; x
++)
2393 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2394 insert_data
, VALUE_SIZE_BUG5
,
2395 (time_t)0, (uint32_t)0);
2396 test_true(rc
== MEMCACHED_SUCCESS
);
2399 for (x
= 0; x
< 2; x
++)
2401 value
= memcached_get(memc
, keys
[0], key_length
[0],
2402 &value_length
, &flags
, &rc
);
2406 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2407 test_true(rc
== MEMCACHED_SUCCESS
);
2409 /* We test for purge of partial complete fetches */
2410 for (count
= 3; count
; count
--)
2412 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2413 &value_length
, &flags
, &rc
);
2414 test_true(rc
== MEMCACHED_SUCCESS
);
2415 test_true(!(memcmp(value
, insert_data
, value_length
)));
2416 test_true(value_length
);
2421 return TEST_SUCCESS
;
2424 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2426 memcached_return_t rc
;
2428 memcached_st
*memc_clone
;
2430 memcached_server_st
*servers
;
2431 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";
2434 servers
= memcached_servers_parse(server_list
);
2437 mine
= memcached_create(NULL
);
2438 rc
= memcached_server_push(mine
, servers
);
2439 test_true(rc
== MEMCACHED_SUCCESS
);
2440 memcached_server_list_free(servers
);
2443 memc_clone
= memcached_clone(NULL
, mine
);
2445 memcached_quit(mine
);
2446 memcached_quit(memc_clone
);
2449 memcached_free(mine
);
2450 memcached_free(memc_clone
);
2452 return TEST_SUCCESS
;
2455 /* Test flag store/retrieve */
2456 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2458 memcached_return_t rc
;
2459 const char *keys
= "036790384900";
2460 size_t key_length
= strlen(keys
);
2461 char return_key
[MEMCACHED_MAX_KEY
];
2462 size_t return_key_length
;
2464 size_t value_length
;
2467 char insert_data
[VALUE_SIZE_BUG5
];
2469 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2470 insert_data
[x
]= (signed char)rand();
2472 memcached_flush(memc
, 0);
2475 rc
= memcached_set(memc
, keys
, key_length
,
2476 insert_data
, VALUE_SIZE_BUG5
,
2478 test_true(rc
== MEMCACHED_SUCCESS
);
2481 value
= memcached_get(memc
, keys
, key_length
,
2482 &value_length
, &flags
, &rc
);
2483 test_true(flags
== 245);
2487 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2490 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2491 &value_length
, &flags
, &rc
);
2492 test_true(flags
== 245);
2497 return TEST_SUCCESS
;
2500 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2502 memcached_return_t rc
;
2503 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2504 size_t key_length
[3];
2509 char return_key
[MEMCACHED_MAX_KEY
];
2510 size_t return_key_length
;
2512 size_t return_value_length
;
2515 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2516 key_length
[1]= strlen("fudge&*@#");
2517 key_length
[2]= strlen("for^#@&$not");
2520 for (x
= 0; x
< 3; x
++)
2522 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2523 keys
[x
], key_length
[x
],
2524 (time_t)50, (uint32_t)9);
2525 test_true(rc
== MEMCACHED_SUCCESS
);
2528 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2529 test_true(rc
== MEMCACHED_SUCCESS
);
2531 /* We need to empty the server before continueing test */
2532 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2533 &return_value_length
, &flags
, &rc
)) != NULL
)
2535 test_true(return_value
);
2539 test_true(count
== 3);
2541 return TEST_SUCCESS
;
2544 /* We are testing with aggressive timeout to get failures */
2545 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2547 const char *key
= "foo";
2549 size_t value_length
= 512;
2552 memcached_return_t rc
;
2553 unsigned int set
= 1;
2554 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2557 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2558 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2560 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2563 value
= (char*)malloc(value_length
* sizeof(char));
2565 for (x
= 0; x
< value_length
; x
++)
2566 value
[x
]= (char) (x
% 127);
2568 for (x
= 1; x
<= 100000; ++x
)
2570 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2572 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2573 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2575 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2580 memcached_free(mclone
);
2582 return TEST_SUCCESS
;
2586 We are looking failures in the async protocol
2588 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2590 const char *key
= "foo";
2592 size_t value_length
= 512;
2595 memcached_return_t rc
;
2596 unsigned int set
= 1;
2598 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2600 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2601 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2603 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2606 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2608 test_true(timeout
== -1);
2610 value
= (char*)malloc(value_length
* sizeof(char));
2612 for (x
= 0; x
< value_length
; x
++)
2613 value
[x
]= (char) (x
% 127);
2615 for (x
= 1; x
<= 100000; ++x
)
2617 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2621 memcached_free(mclone
);
2623 return TEST_SUCCESS
;
2627 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2629 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2631 memcached_return_t rc
;
2633 size_t value_length
;
2635 uint64_t number_value
;
2637 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2638 &value_length
, &flags
, &rc
);
2639 test_true(value
== NULL
);
2640 test_true(rc
== MEMCACHED_NOTFOUND
);
2642 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2645 test_true(value
== NULL
);
2646 /* The binary protocol will set the key if it doesn't exist */
2647 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2649 test_true(rc
== MEMCACHED_SUCCESS
);
2653 test_true(rc
== MEMCACHED_NOTFOUND
);
2656 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2658 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2659 &value_length
, &flags
, &rc
);
2661 test_true(rc
== MEMCACHED_SUCCESS
);
2664 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2666 test_true(number_value
== 2);
2667 test_true(rc
== MEMCACHED_SUCCESS
);
2669 return TEST_SUCCESS
;
2673 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2674 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2676 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2678 char key
[] = "key34567890";
2680 memcached_return_t rc
;
2681 size_t overflowSize
;
2683 char commandFirst
[]= "set key34567890 0 0 ";
2684 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2685 size_t commandLength
;
2688 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2690 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2692 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2694 overflow
= malloc(testSize
);
2695 test_true(overflow
!= NULL
);
2697 memset(overflow
, 'x', testSize
);
2698 rc
= memcached_set(memc
, key
, strlen(key
),
2699 overflow
, testSize
, 0, 0);
2700 test_true(rc
== MEMCACHED_SUCCESS
);
2704 return TEST_SUCCESS
;
2709 Test values of many different sizes
2710 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2711 set key34567890 0 0 8169 \r\n
2712 is sent followed by buffer of size 8169, followed by 8169
2714 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2717 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2718 memcached_return_t rc
;
2719 const char *key
= "foo";
2721 size_t value_length
= 18000;
2723 size_t string_length
;
2726 size_t current_length
;
2728 value
= (char*)malloc(value_length
);
2731 for (x
= 0; x
< value_length
; x
++)
2732 value
[x
] = (char) (x
% 127);
2734 for (current_length
= 0; current_length
< value_length
; current_length
++)
2736 rc
= memcached_set(memc
, key
, strlen(key
),
2737 value
, current_length
,
2738 (time_t)0, (uint32_t)0);
2739 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2741 string
= memcached_get(memc
, key
, strlen(key
),
2742 &string_length
, &flags
, &rc
);
2744 test_true(rc
== MEMCACHED_SUCCESS
);
2745 test_true(string_length
== current_length
);
2746 test_true(!memcmp(string
, value
, string_length
));
2753 return TEST_SUCCESS
;
2757 Look for zero length value problems
2759 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2762 memcached_return_t rc
;
2763 const char *key
= "mykey";
2768 for (x
= 0; x
< 2; x
++)
2770 rc
= memcached_set(memc
, key
, strlen(key
),
2772 (time_t)0, (uint32_t)0);
2774 test_true(rc
== MEMCACHED_SUCCESS
);
2776 value
= memcached_get(memc
, key
, strlen(key
),
2777 &length
, &flags
, &rc
);
2779 test_true(rc
== MEMCACHED_SUCCESS
);
2780 test_true(value
== NULL
);
2781 test_true(length
== 0);
2782 test_true(flags
== 0);
2784 value
= memcached_get(memc
, key
, strlen(key
),
2785 &length
, &flags
, &rc
);
2787 test_true(rc
== MEMCACHED_SUCCESS
);
2788 test_true(value
== NULL
);
2789 test_true(length
== 0);
2790 test_true(flags
== 0);
2793 return TEST_SUCCESS
;
2796 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2797 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2799 memcached_return_t rc
;
2800 const char *key
= "mykey";
2805 rc
= memcached_set(memc
, key
, strlen(key
),
2807 (time_t)0, UINT32_MAX
);
2809 test_true(rc
== MEMCACHED_SUCCESS
);
2811 value
= memcached_get(memc
, key
, strlen(key
),
2812 &length
, &flags
, &rc
);
2814 test_true(rc
== MEMCACHED_SUCCESS
);
2815 test_true(value
== NULL
);
2816 test_true(length
== 0);
2817 test_true(flags
== UINT32_MAX
);
2819 return TEST_SUCCESS
;
2822 #if !defined(__sun) && !defined(__OpenBSD__)
2823 /* Check the validity of chinese key*/
2824 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2826 memcached_return_t rc
;
2827 const char *key
= "豆瓣";
2828 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2833 rc
= memcached_set(memc
, key
, strlen(key
),
2834 value
, strlen(value
),
2837 test_true(rc
== MEMCACHED_SUCCESS
);
2839 value2
= memcached_get(memc
, key
, strlen(key
),
2840 &length
, &flags
, &rc
);
2842 test_true(length
==strlen(value
));
2843 test_true(rc
== MEMCACHED_SUCCESS
);
2844 test_true(memcmp(value
, value2
, length
)==0);
2847 return TEST_SUCCESS
;
2855 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2858 const memcached_server_st
*server
;
2859 memcached_return_t res
;
2863 memc
= memcached_create(NULL
);
2864 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2865 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2867 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2869 memcached_free(memc
);
2871 return TEST_SUCCESS
;
2874 /* CAS test from Andei */
2875 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2877 memcached_return_t status
;
2878 memcached_result_st
*result
, result_obj
;
2879 const char *key
= "abc";
2880 size_t key_len
= strlen("abc");
2881 const char *value
= "foobar";
2882 size_t value_len
= strlen(value
);
2884 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2886 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2887 test_true(status
== MEMCACHED_SUCCESS
);
2889 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2890 test_true(status
== MEMCACHED_SUCCESS
);
2892 result
= memcached_result_create(memc
, &result_obj
);
2895 memcached_result_create(memc
, &result_obj
);
2896 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2899 test_true(status
== MEMCACHED_SUCCESS
);
2901 memcached_result_free(result
);
2903 return TEST_SUCCESS
;
2906 #include "ketama_test_cases.h"
2907 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2909 memcached_return_t rc
;
2912 memcached_server_st
*server_pool
;
2917 memc
= memcached_create(NULL
);
2920 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2921 test_true(rc
== MEMCACHED_SUCCESS
);
2923 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2924 test_true(value
== 1);
2926 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2927 test_true(rc
== MEMCACHED_SUCCESS
);
2929 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2930 test_true(value
== MEMCACHED_HASH_MD5
);
2932 server_pool
= memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
2933 memcached_server_push(memc
, server_pool
);
2935 /* verify that the server list was parsed okay. */
2936 test_true(memcached_server_count(memc
) == 8);
2937 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2938 test_true(server_pool
[0].port
== 11211);
2939 test_true(server_pool
[0].weight
== 600);
2940 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2941 test_true(server_pool
[2].port
== 11211);
2942 test_true(server_pool
[2].weight
== 200);
2943 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2944 test_true(server_pool
[7].port
== 11211);
2945 test_true(server_pool
[7].weight
== 100);
2947 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2948 * us test the boundary wraparound.
2950 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
2952 /* verify the standard ketama set. */
2953 for (x
= 0; x
< 99; x
++)
2955 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2957 memcached_server_instance_st instance
=
2958 memcached_server_instance_by_position(memc
, server_idx
);
2960 const char *hostname
= memcached_server_name(instance
);
2961 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2964 memcached_server_list_free(server_pool
);
2965 memcached_free(memc
);
2967 return TEST_SUCCESS
;
2970 /* Large mget() of missing keys with binary proto
2972 * If many binary quiet commands (such as getq's in an mget) fill the output
2973 * buffer and the server chooses not to respond, memcached_flush hangs. See
2974 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2977 /* sighandler_t function that always asserts false */
2978 static void fail(int unused
)
2985 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2990 return TEST_SKIPPED
;
2992 memcached_return_t rc
;
2995 size_t* key_lengths
;
2996 void (*oldalarm
)(int);
2997 memcached_st
*memc_clone
;
2999 memc_clone
= memcached_clone(NULL
, memc
);
3000 test_true(memc_clone
);
3002 /* only binproto uses getq for mget */
3003 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3005 /* empty the cache to ensure misses (hence non-responses) */
3006 rc
= memcached_flush(memc_clone
, 0);
3007 test_true(rc
== MEMCACHED_SUCCESS
);
3009 key_lengths
= calloc(key_count
, sizeof(size_t));
3010 keys
= calloc(key_count
, sizeof(char *));
3012 for (x
= 0; x
< key_count
; x
++)
3016 snprintf(buffer
, 30, "%u", x
);
3017 keys
[x
]= strdup(buffer
);
3018 key_lengths
[x
]= strlen(keys
[x
]);
3021 oldalarm
= signal(SIGALRM
, fail
);
3024 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3025 test_true(rc
== MEMCACHED_SUCCESS
);
3028 signal(SIGALRM
, oldalarm
);
3030 size_t keys_returned
;
3031 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3033 for (x
= 0; x
< key_count
; x
++)
3038 memcached_free(memc_clone
);
3040 return TEST_SUCCESS
;
3044 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3046 test_return_t test_rc
;
3047 test_rc
= pre_binary(memc
);
3049 if (test_rc
!= TEST_SUCCESS
)
3054 /* should work as of r580 */
3055 rc
= _user_supplied_bug21(memc
, 10);
3056 test_true(rc
== TEST_SUCCESS
);
3058 /* should fail as of r580 */
3059 rc
= _user_supplied_bug21(memc
, 1000);
3060 test_true(rc
== TEST_SUCCESS
);
3062 return TEST_SUCCESS
;
3065 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3068 memcached_server_instance_st instance
;
3070 memcached_return_t rc
;
3071 memcached_st
*memc
= memcached_create(NULL
);
3074 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3075 test_true(rc
== MEMCACHED_SUCCESS
);
3077 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3078 test_true(value
== 1);
3080 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3081 test_true(rc
== MEMCACHED_SUCCESS
);
3083 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3084 test_true(value
== MEMCACHED_HASH_MD5
);
3086 /* server should be removed when in delay */
3087 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3088 test_true(rc
== MEMCACHED_SUCCESS
);
3090 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3091 test_true(value
== 1);
3093 memcached_server_st
*server_pool
;
3094 server_pool
= memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
3095 memcached_server_push(memc
, server_pool
);
3097 /* verify that the server list was parsed okay. */
3098 test_true(memcached_server_count(memc
) == 8);
3099 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3100 test_true(server_pool
[0].port
== 11211);
3101 test_true(server_pool
[0].weight
== 600);
3102 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3103 test_true(server_pool
[2].port
== 11211);
3104 test_true(server_pool
[2].weight
== 200);
3105 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3106 test_true(server_pool
[7].port
== 11211);
3107 test_true(server_pool
[7].weight
== 100);
3109 instance
= memcached_server_instance_by_position(memc
, 2);
3110 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3111 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3114 This would not work if there were only two hosts.
3116 for (size_t x
= 0; x
< 99; x
++)
3118 memcached_autoeject(memc
);
3119 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3120 test_true(server_idx
!= 2);
3123 /* and re-added when it's back. */
3124 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3125 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3126 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3127 memc
->distribution
);
3128 for (size_t x
= 0; x
< 99; x
++)
3130 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3131 // We re-use instance from above.
3133 memcached_server_instance_by_position(memc
, server_idx
);
3134 const char *hostname
= memcached_server_name(instance
);
3135 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3138 memcached_server_list_free(server_pool
);
3139 memcached_free(memc
);
3141 return TEST_SUCCESS
;
3144 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3148 memcached_return_t rc
;
3149 memcached_st
*memc
= memcached_create(NULL
);
3153 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3154 test_true(rc
== MEMCACHED_SUCCESS
);
3156 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3157 test_true(value
== 1);
3159 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3160 test_true(rc
== MEMCACHED_SUCCESS
);
3162 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3163 test_true(value
== MEMCACHED_HASH_MD5
);
3166 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3168 memcached_server_st
*server_pool
;
3169 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");
3170 memcached_server_push(memc
, server_pool
);
3172 // @todo this needs to be refactored to actually test something.
3175 if ((fp
= fopen("ketama_keys.txt", "w")))
3179 printf("cannot write to file ketama_keys.txt");
3180 return TEST_FAILURE
;
3183 for (int x
= 0; x
< 10000; x
++)
3186 snprintf(key
, sizeof(key
), "%d", x
);
3188 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3189 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3190 in_port_t port
= memc
->hosts
[server_idx
].port
;
3191 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3192 memcached_server_instance_st instance
=
3193 memcached_server_instance_by_position(memc
, host_index
);
3197 memcached_server_list_free(server_pool
);
3198 memcached_free(memc
);
3200 return TEST_SUCCESS
;
3204 static test_return_t
result_static(memcached_st
*memc
)
3206 memcached_result_st result
;
3207 memcached_result_st
*result_ptr
;
3209 result_ptr
= memcached_result_create(memc
, &result
);
3210 test_true(result
.options
.is_allocated
== false);
3211 test_true(memcached_is_initialized(&result
) == true);
3212 test_true(result_ptr
);
3213 test_true(result_ptr
== &result
);
3215 memcached_result_free(&result
);
3217 test_true(result
.options
.is_allocated
== false);
3218 test_true(memcached_is_initialized(&result
) == false);
3220 return TEST_SUCCESS
;
3223 static test_return_t
result_alloc(memcached_st
*memc
)
3225 memcached_result_st
*result_ptr
;
3227 result_ptr
= memcached_result_create(memc
, NULL
);
3228 test_true(result_ptr
);
3229 test_true(result_ptr
->options
.is_allocated
== true);
3230 test_true(memcached_is_initialized(result_ptr
) == true);
3231 memcached_result_free(result_ptr
);
3233 return TEST_SUCCESS
;
3236 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3239 pairs_free(global_pairs
);
3241 return TEST_SUCCESS
;
3244 static test_return_t
generate_pairs(memcached_st
*memc
)
3247 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3248 global_count
= GLOBAL_COUNT
;
3250 for (size_t x
= 0; x
< global_count
; x
++)
3252 global_keys
[x
]= global_pairs
[x
].key
;
3253 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3256 return TEST_SUCCESS
;
3259 static test_return_t
generate_large_pairs(memcached_st
*memc
)
3262 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3263 global_count
= GLOBAL2_COUNT
;
3265 for (size_t x
= 0; x
< global_count
; x
++)
3267 global_keys
[x
]= global_pairs
[x
].key
;
3268 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3271 return TEST_SUCCESS
;
3274 static test_return_t
generate_data(memcached_st
*memc
)
3276 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3278 test_true(check_execute
== global_count
);
3280 return TEST_SUCCESS
;
3283 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3285 memcached_stat_st
*stat_p
;
3286 memcached_return_t rc
;
3287 uint32_t host_index
= 0;
3288 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3290 test_true(check_execute
== global_count
);
3292 //TODO: hosts used size stats
3293 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3296 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3298 /* This test was changes so that "make test" would work properlly */
3300 memcached_server_instance_st instance
=
3301 memcached_server_instance_by_position(memc
, host_index
);
3303 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3305 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3308 memcached_stat_free(NULL
, stat_p
);
3310 return TEST_SUCCESS
;
3312 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3317 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3318 generate_data(memc
);
3320 return TEST_SUCCESS
;
3323 static test_return_t
get_read_count(memcached_st
*memc
)
3325 memcached_return_t rc
;
3326 memcached_st
*memc_clone
;
3328 memc_clone
= memcached_clone(NULL
, memc
);
3329 test_true(memc_clone
);
3331 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3335 size_t return_value_length
;
3339 for (size_t x
= count
= 0; x
< global_count
; x
++)
3341 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3342 &return_value_length
, &flags
, &rc
);
3343 if (rc
== MEMCACHED_SUCCESS
)
3352 memcached_free(memc_clone
);
3354 return TEST_SUCCESS
;
3357 static test_return_t
get_read(memcached_st
*memc
)
3359 memcached_return_t rc
;
3363 size_t return_value_length
;
3366 for (size_t x
= 0; x
< global_count
; x
++)
3368 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3369 &return_value_length
, &flags
, &rc
);
3371 test_true(return_value);
3372 test_true(rc == MEMCACHED_SUCCESS);
3374 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3379 return TEST_SUCCESS
;
3382 static test_return_t
mget_read(memcached_st
*memc
)
3384 memcached_return_t rc
;
3386 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3387 return TEST_SKIPPED
;
3389 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3391 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3393 // Go fetch the keys and test to see if all of them were returned
3395 size_t keys_returned
;
3396 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3398 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3399 test_true_got(global_count
== keys_returned
, buffer
);
3403 return TEST_SUCCESS
;
3406 static test_return_t
mget_read_result(memcached_st
*memc
)
3408 memcached_return_t rc
;
3410 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3411 return TEST_SKIPPED
;
3413 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3415 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3417 /* Turn this into a help function */
3419 memcached_result_st results_obj
;
3420 memcached_result_st
*results
;
3422 results
= memcached_result_create(memc
, &results_obj
);
3424 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3427 test_true(rc
== MEMCACHED_SUCCESS
);
3430 memcached_result_free(&results_obj
);
3433 return TEST_SUCCESS
;
3436 static test_return_t
mget_read_function(memcached_st
*memc
)
3438 memcached_return_t rc
;
3440 memcached_execute_fn callbacks
[1];
3442 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3443 return TEST_SKIPPED
;
3445 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3447 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3449 callbacks
[0]= &callback_counter
;
3451 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3453 return TEST_SUCCESS
;
3456 static test_return_t
delete_generate(memcached_st
*memc
)
3458 for (size_t x
= 0; x
< global_count
; x
++)
3460 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3463 return TEST_SUCCESS
;
3466 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3471 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3473 for (size_t x
= 0; x
< global_count
; x
++)
3475 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3478 return TEST_SUCCESS
;
3481 static test_return_t
add_host_test1(memcached_st
*memc
)
3483 memcached_return_t rc
;
3484 char servername
[]= "0.example.com";
3485 memcached_server_st
*servers
;
3487 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3489 test_true(1 == memcached_server_list_count(servers
));
3491 for (size_t x
= 2; x
< 20; x
++)
3493 char buffer
[SMALL_STRING_LEN
];
3495 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3496 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3498 test_true(rc
== MEMCACHED_SUCCESS
);
3499 test_true(x
== memcached_server_list_count(servers
));
3502 rc
= memcached_server_push(memc
, servers
);
3503 test_true(rc
== MEMCACHED_SUCCESS
);
3504 rc
= memcached_server_push(memc
, servers
);
3505 test_true(rc
== MEMCACHED_SUCCESS
);
3507 memcached_server_list_free(servers
);
3509 return TEST_SUCCESS
;
3512 static test_return_t
pre_nonblock(memcached_st
*memc
)
3514 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3516 return TEST_SUCCESS
;
3519 static test_return_t
pre_cork(memcached_st
*memc
)
3521 memcached_return_t rc
;
3524 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3527 return TEST_SKIPPED
;
3530 if (rc
== MEMCACHED_SUCCESS
)
3531 return TEST_SUCCESS
;
3533 return TEST_SKIPPED
;
3536 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3543 return TEST_SKIPPED
;
3546 if (rc
!= TEST_SUCCESS
)
3549 return pre_nonblock(memc
);
3552 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3554 memcached_return_t rc
= MEMCACHED_FAILURE
;
3555 memcached_st
*memc_clone
;
3557 memc_clone
= memcached_clone(NULL
, memc
);
3558 test_true(memc_clone
);
3559 // The memcached_version needs to be done on a clone, because the server
3560 // will not toggle protocol on an connection.
3561 memcached_version(memc_clone
);
3563 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3565 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3566 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3567 test_true(rc
== MEMCACHED_SUCCESS
);
3568 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3572 return TEST_SKIPPED
;
3575 memcached_free(memc_clone
);
3577 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3580 static test_return_t
pre_murmur(memcached_st
*memc
)
3582 #ifdef HAVE_MURMUR_HASH
3583 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3584 return TEST_SUCCESS
;
3587 return TEST_SKIPPED
;
3591 static test_return_t
pre_jenkins(memcached_st
*memc
)
3593 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3595 return TEST_SUCCESS
;
3599 static test_return_t
pre_md5(memcached_st
*memc
)
3601 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3603 return TEST_SUCCESS
;
3606 static test_return_t
pre_crc(memcached_st
*memc
)
3608 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3610 return TEST_SUCCESS
;
3613 static test_return_t
pre_hsieh(memcached_st
*memc
)
3615 #ifdef HAVE_HSIEH_HASH
3616 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3617 return TEST_SUCCESS
;
3620 return TEST_SKIPPED
;
3624 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3626 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3628 return TEST_SUCCESS
;
3631 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3633 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3635 return TEST_SUCCESS
;
3638 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3640 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3642 return TEST_SUCCESS
;
3645 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3647 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3649 return TEST_SUCCESS
;
3652 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3654 memcached_return_t rc
;
3657 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3658 test_true(rc
== MEMCACHED_SUCCESS
);
3660 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3661 test_true(value
== 1);
3663 return TEST_SUCCESS
;
3666 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3668 memcached_return_t rc
;
3671 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3672 test_true(rc
== MEMCACHED_SUCCESS
);
3674 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3675 test_true(value
== 1);
3677 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3678 test_true(rc
== MEMCACHED_SUCCESS
);
3680 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3681 test_true(value
== MEMCACHED_HASH_MD5
);
3683 return TEST_SUCCESS
;
3687 @note This should be testing to see if the server really supports the binary protocol.
3689 static test_return_t
pre_binary(memcached_st
*memc
)
3691 memcached_return_t rc
= MEMCACHED_FAILURE
;
3693 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3695 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3696 test_true(rc
== MEMCACHED_SUCCESS
);
3697 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3700 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3703 static test_return_t
pre_sasl(memcached_st
*memc
)
3705 memcached_return_t rc
= MEMCACHED_FAILURE
;
3707 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3708 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3709 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3710 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3712 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3714 memcached_server_st
*servers
= memcached_servers_parse(server
);
3715 test_true(servers
!= NULL
);
3716 memcached_servers_reset(memc
);
3717 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3718 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3719 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3720 test_true(rc
== MEMCACHED_SUCCESS
);
3726 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3729 static test_return_t
pre_replication(memcached_st
*memc
)
3731 test_return_t test_rc
;
3732 test_rc
= pre_binary(memc
);
3734 if (test_rc
!= TEST_SUCCESS
)
3738 * Make sure that we store the item on all servers
3739 * (master + replicas == number of servers)
3741 memcached_return_t rc
;
3742 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3743 memcached_server_count(memc
) - 1);
3744 test_true(rc
== MEMCACHED_SUCCESS
);
3745 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3747 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3751 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3755 rc
= pre_replication(memc
);
3756 if (rc
!= TEST_SUCCESS
)
3759 rc
= pre_nonblock(memc
);
3765 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3769 #ifdef HARD_MALLOC_TESTS
3770 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3778 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3782 #ifdef HARD_MALLOC_TESTS
3783 void *ret
= malloc(size
+ 8);
3786 ret
= (void*)((caddr_t
)ret
+ 8);
3789 void *ret
= malloc(size
);
3794 memset(ret
, 0xff, size
);
3801 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *context
)
3804 #ifdef HARD_MALLOC_TESTS
3805 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3806 void *nmem
= realloc(real_ptr
, size
+ 8);
3811 ret
= (void*)((caddr_t
)nmem
+ 8);
3817 return realloc(mem
, size
);
3822 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *context
)
3825 #ifdef HARD_MALLOC_TESTS
3826 void *mem
= my_malloc(ptr
, nelem
* size
);
3829 memset(mem
, 0, nelem
* size
);
3835 return calloc(nelem
, size
);
3840 static test_return_t
set_prefix(memcached_st
*memc
)
3842 memcached_return_t rc
;
3843 const char *key
= "mine";
3846 /* Make sure be default none exists */
3847 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3848 test_true(rc
== MEMCACHED_FAILURE
);
3850 /* Test a clean set */
3851 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3852 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3854 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3856 test_true(memcmp(value
, key
, 4) == 0);
3857 test_true(rc
== MEMCACHED_SUCCESS
);
3859 /* Test that we can turn it off */
3860 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3861 test_true(rc
== MEMCACHED_SUCCESS
);
3863 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3865 test_true(rc
== MEMCACHED_FAILURE
);
3867 /* Now setup for main test */
3868 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3869 test_true(rc
== MEMCACHED_SUCCESS
);
3871 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3873 test_true(rc
== MEMCACHED_SUCCESS
);
3874 test_true(memcmp(value
, key
, 4) == 0);
3876 /* Set to Zero, and then Set to something too large */
3879 memset(long_key
, 0, 255);
3881 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3882 test_true(rc
== MEMCACHED_SUCCESS
);
3884 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3886 test_true(rc
== MEMCACHED_FAILURE
);
3887 test_true(value
== NULL
);
3889 /* Test a long key for failure */
3890 /* TODO, extend test to determine based on setting, what result should be */
3891 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3892 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3893 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3894 test_true(rc
== MEMCACHED_SUCCESS
);
3896 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3897 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3898 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3899 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3901 /* Test for a bad prefix, but with a short key */
3902 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3903 test_true(rc
== MEMCACHED_SUCCESS
);
3905 strncpy(long_key
, "dog cat", sizeof(long_key
));
3906 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3907 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3910 return TEST_SUCCESS
;
3914 #ifdef MEMCACHED_ENABLE_DEPRECATED
3915 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3917 void *test_ptr
= NULL
;
3920 memcached_malloc_fn malloc_cb
=
3921 (memcached_malloc_fn
)my_malloc
;
3922 cb_ptr
= *(void **)&malloc_cb
;
3923 memcached_return_t rc
;
3925 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3926 test_true(rc
== MEMCACHED_SUCCESS
);
3927 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3928 test_true(rc
== MEMCACHED_SUCCESS
);
3929 test_true(test_ptr
== cb_ptr
);
3933 memcached_realloc_fn realloc_cb
=
3934 (memcached_realloc_fn
)my_realloc
;
3935 cb_ptr
= *(void **)&realloc_cb
;
3936 memcached_return_t rc
;
3938 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3939 test_true(rc
== MEMCACHED_SUCCESS
);
3940 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3941 test_true(rc
== MEMCACHED_SUCCESS
);
3942 test_true(test_ptr
== cb_ptr
);
3946 memcached_free_fn free_cb
=
3947 (memcached_free_fn
)my_free
;
3948 cb_ptr
= *(void **)&free_cb
;
3949 memcached_return_t rc
;
3951 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3952 test_true(rc
== MEMCACHED_SUCCESS
);
3953 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3954 test_true(rc
== MEMCACHED_SUCCESS
);
3955 test_true(test_ptr
== cb_ptr
);
3958 return TEST_SUCCESS
;
3963 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3965 memcached_return_t rc
;
3966 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3967 my_realloc
, my_calloc
, NULL
);
3968 test_true(rc
== MEMCACHED_FAILURE
);
3970 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3971 my_realloc
, my_calloc
, NULL
);
3973 memcached_malloc_fn mem_malloc
;
3974 memcached_free_fn mem_free
;
3975 memcached_realloc_fn mem_realloc
;
3976 memcached_calloc_fn mem_calloc
;
3977 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3978 &mem_realloc
, &mem_calloc
);
3980 test_true(mem_malloc
== my_malloc
);
3981 test_true(mem_realloc
== my_realloc
);
3982 test_true(mem_calloc
== my_calloc
);
3983 test_true(mem_free
== my_free
);
3985 return TEST_SUCCESS
;
3988 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
3991 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3992 memcached_hash_t hash
;
3993 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3994 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3997 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3998 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4000 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4002 if (hash
!= MEMCACHED_HASH_CRC
)
4003 return TEST_SKIPPED
;
4005 return TEST_SUCCESS
;
4008 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4011 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4012 memcached_hash_t hash
;
4013 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4014 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4017 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4018 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4020 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4022 if (hash
!= MEMCACHED_HASH_HSIEH
)
4023 return TEST_SKIPPED
;
4026 return TEST_SUCCESS
;
4029 static test_return_t
enable_cas(memcached_st
*memc
)
4031 unsigned int set
= 1;
4033 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4035 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4037 return TEST_SUCCESS
;
4040 return TEST_SKIPPED
;
4043 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4045 memcached_version(memc
);
4047 memcached_server_instance_st instance
=
4048 memcached_server_instance_by_position(memc
, 0);
4050 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4051 || instance
->minor_version
> 2)
4053 return TEST_SUCCESS
;
4056 return TEST_SKIPPED
;
4059 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4061 memcached_return_t rc
;
4064 memcached_servers_reset(memc
);
4066 if (stat("/tmp/memcached.socket", &buf
))
4067 return TEST_SKIPPED
;
4069 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4071 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4074 static test_return_t
pre_nodelay(memcached_st
*memc
)
4076 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4077 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4079 return TEST_SUCCESS
;
4082 static test_return_t
pre_settimer(memcached_st
*memc
)
4084 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4085 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4087 return TEST_SUCCESS
;
4090 static test_return_t
poll_timeout(memcached_st
*memc
)
4096 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4098 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4100 test_true(timeout
== 100);
4102 return TEST_SUCCESS
;
4105 static test_return_t
noreply_test(memcached_st
*memc
)
4107 memcached_return_t ret
;
4108 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4109 test_true(ret
== MEMCACHED_SUCCESS
);
4110 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4111 test_true(ret
== MEMCACHED_SUCCESS
);
4112 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4113 test_true(ret
== MEMCACHED_SUCCESS
);
4114 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4115 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4116 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4118 for (int count
=0; count
< 5; ++count
)
4120 for (size_t x
= 0; x
< 100; ++x
)
4123 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4124 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4126 size_t len
= (size_t)check_length
;
4131 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4134 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4137 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4140 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4143 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4149 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4153 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4154 ** API and is _ONLY_ done this way to verify that the library works the
4155 ** way it is supposed to do!!!!
4158 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4160 memcached_server_instance_st instance
=
4161 memcached_server_instance_by_position(memc
, x
);
4162 no_msg
+=(int)(instance
->cursor_active
);
4165 test_true(no_msg
== 0);
4166 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4169 ** Now validate that all items was set properly!
4171 for (size_t x
= 0; x
< 100; ++x
)
4175 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4177 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4179 size_t len
= (size_t)check_length
;
4182 char* value
=memcached_get(memc
, key
, strlen(key
),
4183 &length
, &flags
, &ret
);
4184 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4187 case 0: /* FALLTHROUGH */
4188 case 1: /* FALLTHROUGH */
4190 test_true(strncmp(value
, key
, len
) == 0);
4191 test_true(len
== length
);
4194 test_true(length
== len
* 2);
4197 test_true(length
== len
* 3);
4207 /* Try setting an illegal cas value (should not return an error to
4208 * the caller (because we don't expect a return message from the server)
4210 const char* keys
[]= {"0"};
4211 size_t lengths
[]= {1};
4214 memcached_result_st results_obj
;
4215 memcached_result_st
*results
;
4216 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4217 test_true(ret
== MEMCACHED_SUCCESS
);
4219 results
= memcached_result_create(memc
, &results_obj
);
4221 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4223 test_true(ret
== MEMCACHED_SUCCESS
);
4224 uint64_t cas
= memcached_result_cas(results
);
4225 memcached_result_free(&results_obj
);
4227 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4228 test_true(ret
== MEMCACHED_SUCCESS
);
4231 * The item will have a new cas value, so try to set it again with the old
4232 * value. This should fail!
4234 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4235 test_true(ret
== MEMCACHED_SUCCESS
);
4236 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4237 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4238 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4241 return TEST_SUCCESS
;
4244 static test_return_t
analyzer_test(memcached_st
*memc
)
4246 memcached_return_t rc
;
4247 memcached_stat_st
*memc_stat
;
4248 memcached_analysis_st
*report
;
4250 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4251 test_true(rc
== MEMCACHED_SUCCESS
);
4252 test_true(memc_stat
);
4254 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4255 test_true(rc
== MEMCACHED_SUCCESS
);
4259 memcached_stat_free(NULL
, memc_stat
);
4261 return TEST_SUCCESS
;
4264 /* Count the objects */
4265 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4270 (void)ptr
; (void)key
; (void)key_length
;
4271 size_t *counter
= (size_t *)context
;
4273 *counter
= *counter
+ 1;
4275 return MEMCACHED_SUCCESS
;
4278 static test_return_t
dump_test(memcached_st
*memc
)
4280 memcached_return_t rc
;
4282 memcached_dump_fn callbacks
[1];
4283 test_return_t main_rc
;
4285 callbacks
[0]= &callback_dump_counter
;
4287 /* No support for Binary protocol yet */
4288 if (memc
->flags
.binary_protocol
)
4289 return TEST_SUCCESS
;
4291 main_rc
= set_test3(memc
);
4293 test_true (main_rc
== TEST_SUCCESS
);
4295 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4296 test_true(rc
== MEMCACHED_SUCCESS
);
4298 /* We may have more then 32 if our previous flush has not completed */
4299 test_true(counter
>= 32);
4301 return TEST_SUCCESS
;
4304 #ifdef HAVE_LIBMEMCACHEDUTIL
4305 static void* connection_release(void *arg
)
4308 memcached_pool_st
* pool
;
4313 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4317 #define POOL_SIZE 10
4318 static test_return_t
connection_pool_test(memcached_st
*memc
)
4320 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4321 test_true(pool
!= NULL
);
4322 memcached_st
*mmc
[POOL_SIZE
];
4323 memcached_return_t rc
;
4325 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4327 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4328 test_true(mmc
[x
] != NULL
);
4329 test_true(rc
== MEMCACHED_SUCCESS
);
4332 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4333 test_true(rc
== MEMCACHED_SUCCESS
);
4337 memcached_pool_st
* pool
;
4339 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4340 pthread_create(&tid
, NULL
, connection_release
, &item
);
4341 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4342 test_true(rc
== MEMCACHED_SUCCESS
);
4343 pthread_join(tid
, NULL
);
4344 test_true(mmc
[9] == item
.mmc
);
4345 const char *key
= "key";
4346 size_t keylen
= strlen(key
);
4348 // verify that I can do ops with all connections
4349 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4350 test_true(rc
== MEMCACHED_SUCCESS
);
4352 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4354 uint64_t number_value
;
4355 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4356 test_true(rc
== MEMCACHED_SUCCESS
);
4357 test_true(number_value
== (x
+1));
4361 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4363 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4367 /* verify that I can set behaviors on the pool when I don't have all
4368 * of the connections in the pool. It should however be enabled
4369 * when I push the item into the pool
4371 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4372 test_true(mmc
[0] != NULL
);
4374 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4375 test_true(rc
== MEMCACHED_SUCCESS
);
4377 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4378 test_true(mmc
[1] != NULL
);
4380 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4381 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4382 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4384 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4385 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4386 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4388 test_true(memcached_pool_destroy(pool
) == memc
);
4390 return TEST_SUCCESS
;
4393 static test_return_t
util_version_test(memcached_st
*memc
)
4397 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4398 test_true(if_successful
== true);
4400 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4402 // We expect failure
4405 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4406 fprintf(stderr
, "\nDumping Server Information\n\n");
4407 memcached_server_fn callbacks
[1];
4409 callbacks
[0]= dump_server_information
;
4410 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4411 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4413 test_true(if_successful
== false);
4415 memcached_server_instance_st instance
=
4416 memcached_server_instance_by_position(memc
, 0);
4418 memcached_version(memc
);
4420 // We only use one binary when we test, so this should be just fine.
4421 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4422 test_true(if_successful
== true);
4424 if (instance
->micro_version
> 0)
4425 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4426 else if (instance
->minor_version
> 0)
4427 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4428 else if (instance
->major_version
> 0)
4429 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4431 test_true(if_successful
== true);
4433 if (instance
->micro_version
> 0)
4434 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4435 else if (instance
->minor_version
> 0)
4436 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4437 else if (instance
->major_version
> 0)
4438 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4440 test_true(if_successful
== false);
4442 return TEST_SUCCESS
;
4445 static test_return_t
ping_test(memcached_st
*memc
)
4447 memcached_return_t rc
;
4448 memcached_server_instance_st instance
=
4449 memcached_server_instance_by_position(memc
, 0);
4451 // Test both the version that returns a code, and the one that does not.
4452 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4453 memcached_server_port(instance
), NULL
));
4455 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4456 memcached_server_port(instance
), &rc
));
4458 test_true(rc
== MEMCACHED_SUCCESS
);
4460 return TEST_SUCCESS
;
4466 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4470 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4471 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4472 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4473 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4474 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4475 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4476 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4477 #ifdef HAVE_HSIEH_HASH
4478 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4480 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4481 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4482 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4484 return TEST_SUCCESS
;
4488 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4490 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4491 #ifdef HAVE_HSIEH_HASH
4492 expected_rc
= MEMCACHED_SUCCESS
;
4494 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4495 (uint64_t)MEMCACHED_HASH_HSIEH
);
4496 test_true(rc
== expected_rc
);
4498 return TEST_SUCCESS
;
4501 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4503 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4504 #ifdef HAVE_MURMUR_HASH
4505 expected_rc
= MEMCACHED_SUCCESS
;
4507 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4508 (uint64_t)MEMCACHED_HASH_MURMUR
);
4509 test_true(rc
== expected_rc
);
4511 return TEST_SUCCESS
;
4514 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4520 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4524 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4525 test_true(one_at_a_time_values
[x
] == hash_val
);
4528 return TEST_SUCCESS
;
4531 static test_return_t
md5_run (memcached_st
*memc
)
4537 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4541 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4542 test_true(md5_values
[x
] == hash_val
);
4545 return TEST_SUCCESS
;
4548 static test_return_t
crc_run (memcached_st
*memc
)
4554 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4558 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4559 test_true(crc_values
[x
] == hash_val
);
4562 return TEST_SUCCESS
;
4565 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4571 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4575 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4576 test_true(fnv1_64_values
[x
] == hash_val
);
4579 return TEST_SUCCESS
;
4582 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4588 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4592 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4593 test_true(fnv1a_64_values
[x
] == hash_val
);
4596 return TEST_SUCCESS
;
4599 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4605 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4609 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4610 test_true(fnv1_32_values
[x
] == hash_val
);
4613 return TEST_SUCCESS
;
4616 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4622 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4626 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4627 test_true(fnv1a_32_values
[x
] == hash_val
);
4630 return TEST_SUCCESS
;
4633 static test_return_t
hsieh_run (memcached_st
*memc
)
4639 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4643 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4644 test_true(hsieh_values
[x
] == hash_val
);
4647 return TEST_SUCCESS
;
4650 static test_return_t
murmur_run (memcached_st
*memc
)
4652 #ifdef WORDS_BIGENDIAN
4653 (void)murmur_values
;
4654 return TEST_SKIPPED
;
4660 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4664 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4665 test_true(murmur_values
[x
] == hash_val
);
4668 return TEST_SUCCESS
;
4672 static test_return_t
jenkins_run (memcached_st
*memc
)
4678 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4682 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4683 test_true(jenkins_values
[x
] == hash_val
);
4686 return TEST_SUCCESS
;
4689 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4692 return libhashkit_md5(string
, string_length
);
4695 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4698 return libhashkit_crc32(string
, string_length
);
4701 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4705 const hashkit_st
*kit
;
4707 hashkit_return_t hash_rc
;
4709 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};
4710 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};
4712 kit
= memcached_get_hashkit(memc
);
4714 hashkit_clone(&new_kit
, kit
);
4715 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4716 test_true(hash_rc
== HASHKIT_SUCCESS
);
4718 memcached_set_hashkit(memc
, &new_kit
);
4721 Verify Setting the hash.
4723 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4727 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4728 test_true(md5_values
[x
] == hash_val
);
4733 Now check memcached_st.
4735 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4739 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4740 test_true(md5_hosts
[x
] == hash_val
);
4743 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4744 test_true(hash_rc
== HASHKIT_SUCCESS
);
4746 memcached_set_hashkit(memc
, &new_kit
);
4749 Verify Setting the hash.
4751 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4755 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4756 test_true(crc_values
[x
] == hash_val
);
4759 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4763 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4764 test_true(crc_hosts
[x
] == hash_val
);
4767 return TEST_SUCCESS
;
4771 Test case adapted from John Gorman <johngorman2@gmail.com>
4773 We are testing the error condition when we connect to a server via memcached_get()
4774 but find that the server is not available.
4776 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4779 memcached_st
*tl_memc_h
;
4780 memcached_server_st
*servers
;
4782 const char *key
= "MemcachedLives";
4785 memcached_return rc
;
4789 tl_memc_h
= memcached_create(NULL
);
4790 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4791 memcached_server_push(tl_memc_h
, servers
);
4792 memcached_server_list_free(servers
);
4794 // See if memcached is reachable.
4795 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4798 test_true(len
== 0);
4799 test_false(rc
== MEMCACHED_SUCCESS
);
4801 memcached_free(tl_memc_h
);
4803 return TEST_SUCCESS
;
4807 We connect to a server which exists, but search for a key that does not exist.
4809 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4811 const char *key
= "MemcachedKeyNotEXIST";
4814 memcached_return rc
;
4817 // See if memcached is reachable.
4818 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4821 test_true(len
== 0);
4822 test_true(rc
== MEMCACHED_NOTFOUND
);
4824 return TEST_SUCCESS
;
4828 Test case adapted from John Gorman <johngorman2@gmail.com>
4830 We are testing the error condition when we connect to a server via memcached_get_by_key()
4831 but find that the server is not available.
4833 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4836 memcached_st
*tl_memc_h
;
4837 memcached_server_st
*servers
;
4839 const char *key
= "MemcachedLives";
4842 memcached_return rc
;
4846 tl_memc_h
= memcached_create(NULL
);
4847 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4848 memcached_server_push(tl_memc_h
, servers
);
4849 memcached_server_list_free(servers
);
4851 // See if memcached is reachable.
4852 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4855 test_true(len
== 0);
4856 test_false(rc
== MEMCACHED_SUCCESS
);
4858 memcached_free(tl_memc_h
);
4860 return TEST_SUCCESS
;
4864 We connect to a server which exists, but search for a key that does not exist.
4866 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4868 const char *key
= "MemcachedKeyNotEXIST";
4871 memcached_return rc
;
4874 // See if memcached is reachable.
4875 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4878 test_true(len
== 0);
4879 test_true(rc
== MEMCACHED_NOTFOUND
);
4881 return TEST_SUCCESS
;
4885 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4887 memcached_return_t rc
;
4890 memcached_server_st
*server_pool
;
4895 memc
= memcached_create(NULL
);
4898 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4899 test_true(rc
== MEMCACHED_SUCCESS
);
4901 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4902 test_true(value
== 1);
4904 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4905 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4908 server_pool
= memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
4909 memcached_server_push(memc
, server_pool
);
4911 /* verify that the server list was parsed okay. */
4912 test_true(memcached_server_count(memc
) == 8);
4913 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4914 test_true(server_pool
[0].port
== 11211);
4915 test_true(server_pool
[0].weight
== 600);
4916 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4917 test_true(server_pool
[2].port
== 11211);
4918 test_true(server_pool
[2].weight
== 200);
4919 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4920 test_true(server_pool
[7].port
== 11211);
4921 test_true(server_pool
[7].weight
== 100);
4923 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4924 * us test the boundary wraparound.
4926 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4928 /* verify the standard ketama set. */
4929 for (x
= 0; x
< 99; x
++)
4931 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4932 memcached_server_instance_st instance
=
4933 memcached_server_instance_by_position(memc
, server_idx
);
4934 const char *hostname
= memcached_server_name(instance
);
4936 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4939 memcached_server_list_free(server_pool
);
4940 memcached_free(memc
);
4942 return TEST_SUCCESS
;
4945 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4947 memcached_return_t rc
;
4949 memcached_server_st
*server_pool
;
4954 memc
= memcached_create(NULL
);
4957 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4958 test_true(rc
== MEMCACHED_SUCCESS
);
4960 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4961 test_true(value
== 1);
4963 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4964 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4966 server_pool
= memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
4967 assert(server_pool
);
4968 memcached_server_push(memc
, server_pool
);
4970 /* verify that the server list was parsed okay. */
4971 test_true(memcached_server_count(memc
) == 8);
4972 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4973 test_true(server_pool
[0].port
== 11211);
4974 test_true(server_pool
[0].weight
== 600);
4975 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4976 test_true(server_pool
[2].port
== 11211);
4977 test_true(server_pool
[2].weight
== 200);
4978 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4979 test_true(server_pool
[7].port
== 11211);
4980 test_true(server_pool
[7].weight
== 100);
4982 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4983 * us test the boundary wraparound.
4985 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4987 /* verify the standard ketama set. */
4988 for (uint32_t x
= 0; x
< 99; x
++)
4990 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4992 memcached_server_instance_st instance
=
4993 memcached_server_instance_by_position(memc
, server_idx
);
4995 const char *hostname
= memcached_server_name(instance
);
4997 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5000 memcached_server_list_free(server_pool
);
5001 memcached_free(memc
);
5003 return TEST_SUCCESS
;
5006 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5008 test_return_t test_rc
;
5009 test_rc
= pre_binary(memc
);
5011 if (test_rc
!= TEST_SUCCESS
)
5014 memcached_return_t ret
;
5015 const char *key
= "regression_bug_434484";
5016 size_t keylen
= strlen(key
);
5018 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5019 test_true(ret
== MEMCACHED_NOTSTORED
);
5021 size_t size
= 2048 * 1024;
5022 void *data
= calloc(1, size
);
5023 test_true(data
!= NULL
);
5024 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5025 test_true(ret
== MEMCACHED_E2BIG
);
5028 return TEST_SUCCESS
;
5031 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5033 test_return_t test_rc
;
5034 test_rc
= pre_binary(memc
);
5036 if (test_rc
!= TEST_SUCCESS
)
5039 memcached_return_t rc
;
5041 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5044 * I only want to hit only _one_ server so I know the number of requests I'm
5045 * sending in the pipleine to the server. Let's try to do a multiget of
5046 * 1024 (that should satisfy most users don't you think?). Future versions
5047 * will include a mget_execute function call if you need a higher number.
5049 uint32_t number_of_hosts
= memcached_server_count(memc
);
5050 memc
->number_of_hosts
= 1;
5051 const size_t max_keys
= 1024;
5052 char **keys
= calloc(max_keys
, sizeof(char*));
5053 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5055 for (size_t x
= 0; x
< max_keys
; ++x
)
5059 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5061 test_true(keys
[x
] != NULL
);
5065 * Run two times.. the first time we should have 100% cache miss,
5066 * and the second time we should have 100% cache hits
5068 for (size_t y
= 0; y
< 2; y
++)
5070 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5071 test_true(rc
== MEMCACHED_SUCCESS
);
5072 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5076 /* The first iteration should give me a 100% cache miss. verify that*/
5077 char blob
[1024]= { 0 };
5079 test_true(counter
== 0);
5081 for (size_t x
= 0; x
< max_keys
; ++x
)
5083 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5084 blob
, sizeof(blob
), 0, 0);
5085 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5090 /* Verify that we received all of the key/value pairs */
5091 test_true(counter
== max_keys
);
5095 /* Release allocated resources */
5096 for (size_t x
= 0; x
< max_keys
; ++x
)
5103 memc
->number_of_hosts
= number_of_hosts
;
5105 return TEST_SUCCESS
;
5108 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5110 memcached_return_t rc
;
5111 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5112 test_true(rc
== MEMCACHED_SUCCESS
);
5114 return regression_bug_434843(memc
);
5117 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5119 memcached_return_t rc
;
5120 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5121 test_true(rc
== MEMCACHED_SUCCESS
);
5123 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5124 test_true(rc
== MEMCACHED_SUCCESS
);
5125 test_true(bytes
!= NULL
);
5126 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5128 test_true(rc
== MEMCACHED_SUCCESS
);
5129 test_true(bytes_read
!= NULL
);
5131 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5132 "bytes_written", &rc
);
5133 test_true(rc
== MEMCACHED_SUCCESS
);
5134 test_true(bytes_written
!= NULL
);
5136 test_true(strcmp(bytes
, bytes_read
) != 0);
5137 test_true(strcmp(bytes
, bytes_written
) != 0);
5139 /* Release allocated resources */
5142 free(bytes_written
);
5143 memcached_stat_free(NULL
, memc_stat
);
5145 return TEST_SUCCESS
;
5149 * The test case isn't obvious so I should probably document why
5150 * it works the way it does. Bug 442914 was caused by a bug
5151 * in the logic in memcached_purge (it did not handle the case
5152 * where the number of bytes sent was equal to the watermark).
5153 * In this test case, create messages so that we hit that case
5154 * and then disable noreply mode and issue a new command to
5155 * verify that it isn't stuck. If we change the format for the
5156 * delete command or the watermarks, we need to update this
5159 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5161 memcached_return_t rc
;
5162 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5163 test_true(rc
== MEMCACHED_SUCCESS
);
5164 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5166 uint32_t number_of_hosts
= memcached_server_count(memc
);
5167 memc
->number_of_hosts
= 1;
5172 for (uint32_t x
= 0; x
< 250; ++x
)
5174 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5175 rc
= memcached_delete(memc
, k
, len
, 0);
5176 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5179 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5182 rc
= memcached_delete(memc
, k
, len
, 0);
5183 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5185 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5186 test_true(rc
== MEMCACHED_SUCCESS
);
5187 rc
= memcached_delete(memc
, k
, len
, 0);
5188 test_true(rc
== MEMCACHED_NOTFOUND
);
5190 memc
->number_of_hosts
= number_of_hosts
;
5192 return TEST_SUCCESS
;
5195 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5197 memcached_server_instance_st instance_one
;
5198 memcached_server_instance_st instance_two
;
5200 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5201 return TEST_SKIPPED
;
5203 memcached_return_t rc
;
5205 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5206 test_true(rc
== MEMCACHED_SUCCESS
);
5208 const size_t max_keys
= 100;
5209 char **keys
= calloc(max_keys
, sizeof(char*));
5210 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5212 for (size_t x
= 0; x
< max_keys
; ++x
)
5216 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5218 test_true(keys
[x
] != NULL
);
5219 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5220 test_true(rc
== MEMCACHED_SUCCESS
);
5224 ** We are using the quiet commands to store the replicas, so we need
5225 ** to ensure that all of them are processed before we can continue.
5226 ** In the test we go directly from storing the object to trying to
5227 ** receive the object from all of the different servers, so we
5228 ** could end up in a race condition (the memcached server hasn't yet
5229 ** processed the quiet command from the replication set when it process
5230 ** the request from the other client (created by the clone)). As a
5231 ** workaround for that we call memcached_quit to send the quit command
5232 ** to the server and wait for the response ;-) If you use the test code
5233 ** as an example for your own code, please note that you shouldn't need
5236 memcached_quit(memc
);
5238 /* Verify that all messages are stored, and we didn't stuff too much
5241 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5242 test_true(rc
== MEMCACHED_SUCCESS
);
5245 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5246 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5247 /* Verify that we received all of the key/value pairs */
5248 test_true(counter
== max_keys
);
5250 memcached_quit(memc
);
5252 * Don't do the following in your code. I am abusing the internal details
5253 * within the library, and this is not a supported interface.
5254 * This is to verify correct behavior in the library. Fake that two servers
5257 instance_one
= memcached_server_instance_by_position(memc
, 0);
5258 instance_two
= memcached_server_instance_by_position(memc
, 2);
5259 in_port_t port0
= instance_one
->port
;
5260 in_port_t port2
= instance_two
->port
;
5262 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5263 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5265 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5266 test_true(rc
== MEMCACHED_SUCCESS
);
5269 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5270 test_true(counter
== (unsigned int)max_keys
);
5272 /* restore the memc handle */
5273 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5274 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5276 memcached_quit(memc
);
5278 /* Remove half of the objects */
5279 for (size_t x
= 0; x
< max_keys
; ++x
)
5283 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5284 test_true(rc
== MEMCACHED_SUCCESS
);
5288 memcached_quit(memc
);
5289 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5290 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5292 /* now retry the command, this time we should have cache misses */
5293 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5294 test_true(rc
== MEMCACHED_SUCCESS
);
5297 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5298 test_true(counter
== (unsigned int)(max_keys
>> 1));
5300 /* Release allocated resources */
5301 for (size_t x
= 0; x
< max_keys
; ++x
)
5308 /* restore the memc handle */
5309 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5310 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5312 return TEST_SUCCESS
;
5315 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5317 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5318 test_true(memc_clone
!= NULL
);
5319 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5321 memcached_server_instance_st instance
=
5322 memcached_server_instance_by_position(memc_clone
, 0);
5324 if (instance
->major_version
> 1 ||
5325 (instance
->major_version
== 1 &&
5326 instance
->minor_version
> 2))
5328 /* Binary protocol doesn't support deferred delete */
5329 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5330 test_true(bin_clone
!= NULL
);
5331 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5332 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5333 memcached_free(bin_clone
);
5335 memcached_quit(memc_clone
);
5337 /* If we know the server version, deferred delete should fail
5338 * with invalid arguments */
5339 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5341 /* If we don't know the server version, we should get a protocol error */
5342 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5344 /* but there is a bug in some of the memcached servers (1.4) that treats
5345 * the counter as noreply so it doesn't send the proper error message
5347 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5349 /* And buffered mode should be disabled and we should get protocol error */
5350 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5351 rc
= memcached_delete(memc
, "foo", 3, 1);
5352 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5354 /* Same goes for noreply... */
5355 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5356 rc
= memcached_delete(memc
, "foo", 3, 1);
5357 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5359 /* but a normal request should go through (and be buffered) */
5360 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5361 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5363 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5364 /* unbuffered noreply should be success */
5365 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5366 /* unbuffered with reply should be not found... */
5367 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5368 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5371 memcached_free(memc_clone
);
5372 return TEST_SUCCESS
;
5376 /* Test memcached_server_get_last_disconnect
5377 * For a working server set, shall be NULL
5378 * For a set of non existing server, shall not be NULL
5380 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5382 memcached_return_t rc
;
5383 memcached_server_instance_st disconnected_server
;
5385 /* With the working set of server */
5386 const char *key
= "marmotte";
5387 const char *value
= "milka";
5389 memcached_reset_last_disconnected_server(memc
);
5390 rc
= memcached_set(memc
, key
, strlen(key
),
5391 value
, strlen(value
),
5392 (time_t)0, (uint32_t)0);
5393 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5395 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5396 test_true(disconnected_server
== NULL
);
5398 /* With a non existing server */
5400 memcached_server_st
*servers
;
5402 const char *server_list
= "localhost:9";
5404 servers
= memcached_servers_parse(server_list
);
5406 mine
= memcached_create(NULL
);
5407 rc
= memcached_server_push(mine
, servers
);
5408 test_true(rc
== MEMCACHED_SUCCESS
);
5409 memcached_server_list_free(servers
);
5412 rc
= memcached_set(mine
, key
, strlen(key
),
5413 value
, strlen(value
),
5414 (time_t)0, (uint32_t)0);
5415 test_true(rc
!= MEMCACHED_SUCCESS
);
5417 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5418 if (disconnected_server
== NULL
)
5420 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5423 test_true(disconnected_server
!= NULL
);
5424 test_true(memcached_server_port(disconnected_server
)== 9);
5425 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5427 memcached_quit(mine
);
5428 memcached_free(mine
);
5430 return TEST_SUCCESS
;
5433 static test_return_t
test_verbosity(memcached_st
*memc
)
5435 memcached_verbosity(memc
, 3);
5437 return TEST_SUCCESS
;
5440 static test_return_t
test_server_failure(memcached_st
*memc
)
5442 memcached_st
*local_memc
;
5443 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5445 local_memc
= memcached_create(NULL
);
5447 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5448 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5450 uint32_t server_count
= memcached_server_count(local_memc
);
5452 test_true(server_count
== 1);
5454 // Disable the server
5455 instance
= memcached_server_instance_by_position(local_memc
, 0);
5456 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5458 memcached_return_t rc
;
5459 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5461 (time_t)0, (uint32_t)0);
5462 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5464 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5465 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5467 (time_t)0, (uint32_t)0);
5468 test_true(rc
== MEMCACHED_SUCCESS
);
5471 memcached_free(local_memc
);
5473 return TEST_SUCCESS
;
5476 static test_return_t
test_cull_servers(memcached_st
*memc
)
5478 uint32_t count
= memcached_server_count(memc
);
5480 // Do not do this in your code, it is not supported.
5481 memc
->servers
[1].options
.is_dead
= true;
5482 memc
->state
.is_time_for_rebuild
= true;
5484 uint32_t new_count
= memcached_server_count(memc
);
5485 test_true(count
== new_count
);
5488 test_true(count
== new_count
+ 1 );
5491 return TEST_SUCCESS
;
5495 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5496 const char *key
, size_t key_length
,
5497 const char *value
, size_t value_length
,
5507 return MEMCACHED_SUCCESS
;
5510 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5512 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5513 test_true(rc
== MEMCACHED_SUCCESS
);
5515 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5516 test_true(rc
== MEMCACHED_SUCCESS
);
5518 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5519 test_true(rc
== MEMCACHED_SUCCESS
);
5521 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5522 test_true(rc
== MEMCACHED_SUCCESS
);
5524 return TEST_SUCCESS
;
5528 * This test ensures that the failure counter isn't incremented during
5529 * normal termination of the memcached instance.
5531 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5533 memcached_return_t rc
;
5534 memcached_server_instance_st instance
;
5536 /* Set value to force connection to the server */
5537 const char *key
= "marmotte";
5538 const char *value
= "milka";
5541 * Please note that I'm abusing the internal structures in libmemcached
5542 * in a non-portable way and you shouldn't be doing this. I'm only
5543 * doing this in order to verify that the library works the way it should
5545 uint32_t number_of_hosts
= memcached_server_count(memc
);
5546 memc
->number_of_hosts
= 1;
5548 /* Ensure that we are connected to the server by setting a value */
5549 rc
= memcached_set(memc
, key
, strlen(key
),
5550 value
, strlen(value
),
5551 (time_t)0, (uint32_t)0);
5552 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5555 instance
= memcached_server_instance_by_position(memc
, 0);
5556 /* The test is to see that the memcached_quit doesn't increase the
5557 * the server failure conter, so let's ensure that it is zero
5558 * before sending quit
5560 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5562 memcached_quit(memc
);
5564 /* Verify that it memcached_quit didn't increment the failure counter
5565 * Please note that this isn't bullet proof, because an error could
5568 test_true(instance
->server_failure_counter
== 0);
5570 /* restore the instance */
5571 memc
->number_of_hosts
= number_of_hosts
;
5573 return TEST_SUCCESS
;
5577 * This tests ensures expected disconnections (for some behavior changes
5578 * for instance) do not wrongly increase failure counter
5580 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5582 memcached_return rc
;
5584 memcached_st
*memc_clone
;
5585 memc_clone
= memcached_clone(NULL
, memc
);
5586 test_true(memc_clone
);
5588 /* Set value to force connection to the server */
5589 const char *key
= "marmotte";
5590 const char *value
= "milka";
5591 char *string
= NULL
;
5592 size_t string_length
;
5595 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5596 value
, strlen(value
),
5597 (time_t)0, (uint32_t)0);
5598 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5601 /* put failure limit to 1 */
5602 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5603 assert(rc
== MEMCACHED_SUCCESS
);
5605 /* Put a retry timeout to effectively activate failure_limit effect */
5606 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5607 assert(rc
== MEMCACHED_SUCCESS
);
5609 /* change behavior that triggers memcached_quit()*/
5610 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5611 assert(rc
== MEMCACHED_SUCCESS
);
5614 /* Check if we still are connected */
5615 string
= memcached_get(memc_clone
, key
, strlen(key
),
5616 &string_length
, &flags
, &rc
);
5618 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5621 memcached_free(memc_clone
);
5623 return TEST_SUCCESS
;
5630 * Test that ensures mget_execute does not end into recursive calls that finally fails
5632 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5634 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5635 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5636 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5637 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5638 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5641 return TEST_SKIPPED
; // My MAC can't handle this test
5645 * I only want to hit _one_ server so I know the number of requests I'm
5646 * sending in the pipeline.
5648 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5649 memc
->number_of_hosts
= 1;
5650 size_t max_keys
= 20480;
5653 char **keys
= calloc(max_keys
, sizeof(char*));
5654 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5656 /* First add all of the items.. */
5658 char blob
[1024]= { 0 };
5659 memcached_return rc
;
5660 for (size_t x
= 0; x
< max_keys
; ++x
)
5663 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5665 assert(keys
[x
] != NULL
);
5666 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5668 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5670 break; // We are out of business
5673 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5675 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5678 sleep(1);// We will try to sleep
5681 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5683 // We failed to send everything.
5688 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5691 /* Try to get all of them with a large multiget */
5693 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5694 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5695 (size_t)max_keys
, callbacks
, &counter
, 1);
5697 assert(rc
== MEMCACHED_SUCCESS
);
5698 char* the_value
= NULL
;
5699 char the_key
[MEMCACHED_MAX_KEY
];
5700 size_t the_key_length
;
5701 size_t the_value_length
;
5705 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5707 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5713 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5716 assert(rc
== MEMCACHED_END
);
5718 /* Verify that we got all of the items */
5719 assert(counter
== max_keys
);
5722 /* Release all allocated resources */
5723 for (size_t x
= 0; x
< max_keys
; ++x
)
5730 memc
->number_of_hosts
= number_of_hosts
;
5732 return TEST_SUCCESS
;
5735 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5739 memcached_st
*memc
= memcached_create(NULL
);
5741 memcached_server_add(memc
, "10.2.3.4", 11211);
5743 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5744 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5745 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5746 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5747 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5748 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5750 memcached_return_t rc
;
5754 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5756 test_true_got(rc
== MEMCACHED_TIMEOUT
|| rc
== MEMCACHED_ERRNO
|| rc
== MEMCACHED_FAILURE
, memcached_strerror(memc
, rc
));
5758 memcached_free(memc
);
5760 return TEST_SUCCESS
;
5763 static test_return_t
regression_bug_581030(memcached_st
*unused
)
5768 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5769 test_false(local_stat
);
5771 memcached_stat_free(NULL
, NULL
);
5774 return TEST_SUCCESS
;
5777 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5779 fprintf(stderr
, "Iteration #%u: ", it
);
5781 if(error
== MEMCACHED_ERRNO
)
5783 fprintf(stderr
, "system error %d from %s: %s\n",
5784 errno
, what
, strerror(errno
));
5788 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5789 memcached_strerror(mc
, error
));
5793 #define TEST_CONSTANT_CREATION 200
5795 static test_return_t
regression_bug_(memcached_st
*memc
)
5797 const char *remote_server
;
5800 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5802 return TEST_SKIPPED
;
5805 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5807 memcached_st
* mc
= memcached_create(NULL
);
5808 memcached_return rc
;
5810 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5811 if (rc
!= MEMCACHED_SUCCESS
)
5813 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5816 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5817 if (rc
!= MEMCACHED_SUCCESS
)
5819 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5822 rc
= memcached_server_add(mc
, remote_server
, 0);
5823 if (rc
!= MEMCACHED_SUCCESS
)
5825 memcached_die(mc
, rc
, "memcached_server_add", x
);
5828 const char *set_key
= "akey";
5829 const size_t set_key_len
= strlen(set_key
);
5830 const char *set_value
= "a value";
5831 const size_t set_value_len
= strlen(set_value
);
5833 if (rc
== MEMCACHED_SUCCESS
)
5837 size_t get_value_len
;
5839 uint32_t get_value_flags
;
5841 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5842 &get_value_flags
, &rc
);
5843 if (rc
!= MEMCACHED_SUCCESS
)
5845 memcached_die(mc
, rc
, "memcached_get", x
);
5851 (get_value_len
!= set_value_len
5852 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5854 fprintf(stderr
, "Values don't match?\n");
5855 rc
= MEMCACHED_FAILURE
;
5861 rc
= memcached_set(mc
,
5862 set_key
, set_key_len
,
5863 set_value
, set_value_len
,
5867 if (rc
!= MEMCACHED_SUCCESS
)
5869 memcached_die(mc
, rc
, "memcached_set", x
);
5876 if (rc
!= MEMCACHED_SUCCESS
)
5882 return TEST_SUCCESS
;
5886 * Test that the sasl authentication works. We cannot use the default
5887 * pool of servers, because that would require that all servers we want
5888 * to test supports SASL authentication, and that they use the default
5891 static test_return_t
sasl_auth_test(memcached_st
*memc
)
5893 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
5894 memcached_return_t rc
;
5896 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5897 test_true(rc
== MEMCACHED_SUCCESS
);
5898 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
5899 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
5900 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
5901 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
5902 memcached_quit(memc
);
5904 rc
= memcached_set_sasl_auth_data(memc
,
5905 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
5906 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
5907 test_true(rc
== MEMCACHED_SUCCESS
);
5909 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5910 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
5911 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
5913 memcached_quit(memc
);
5914 return TEST_SUCCESS
;
5917 return TEST_FAILURE
;
5921 /* Clean the server before beginning testing */
5923 {"util_version", 1, (test_callback_fn
)util_version_test
},
5924 {"flush", 0, (test_callback_fn
)flush_test
},
5925 {"init", 0, (test_callback_fn
)init_test
},
5926 {"allocation", 0, (test_callback_fn
)allocation_test
},
5927 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5928 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5929 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5930 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5931 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5932 {"clone_test", 0, (test_callback_fn
)clone_test
},
5933 {"connection_test", 0, (test_callback_fn
)connection_test
},
5934 {"callback_test", 0, (test_callback_fn
)callback_test
},
5935 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5936 {"error", 0, (test_callback_fn
)error_test
},
5937 {"set", 0, (test_callback_fn
)set_test
},
5938 {"set2", 0, (test_callback_fn
)set_test2
},
5939 {"set3", 0, (test_callback_fn
)set_test3
},
5940 {"dump", 1, (test_callback_fn
)dump_test
},
5941 {"add", 1, (test_callback_fn
)add_test
},
5942 {"replace", 1, (test_callback_fn
)replace_test
},
5943 {"delete", 1, (test_callback_fn
)delete_test
},
5944 {"get", 1, (test_callback_fn
)get_test
},
5945 {"get2", 0, (test_callback_fn
)get_test2
},
5946 {"get3", 0, (test_callback_fn
)get_test3
},
5947 {"get4", 0, (test_callback_fn
)get_test4
},
5948 {"partial mget", 0, (test_callback_fn
)get_test5
},
5949 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5950 {"increment", 0, (test_callback_fn
)increment_test
},
5951 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5952 {"decrement", 0, (test_callback_fn
)decrement_test
},
5953 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5954 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5955 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5956 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5957 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5958 {"quit", 0, (test_callback_fn
)quit_test
},
5959 {"mget", 1, (test_callback_fn
)mget_test
},
5960 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5961 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5962 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5963 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5964 {"mget_end", 0, (test_callback_fn
)mget_end
},
5965 {"get_stats", 0, (test_callback_fn
)get_stats
},
5966 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5967 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5968 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5969 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5970 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
5971 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
5972 {"read_through", 1, (test_callback_fn
)read_through
},
5973 {"delete_through", 1, (test_callback_fn
)delete_through
},
5974 {"noreply", 1, (test_callback_fn
)noreply_test
},
5975 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
5976 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
5977 {"memcached_pool_test", 1, (test_callback_fn
)memcached_pool_test
},
5978 {"ping", 1, (test_callback_fn
)ping_test
},
5979 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
5980 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
5981 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
5982 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
5983 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
5987 test_st behavior_tests
[] ={
5988 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
5989 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
5990 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
5991 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
5995 test_st basic_tests
[] ={
5996 {"init", 1, (test_callback_fn
)basic_init_test
},
5997 {"clone", 1, (test_callback_fn
)basic_clone_test
},
5998 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
5999 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
6000 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
6001 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
6005 test_st regression_binary_vs_block
[] ={
6006 {"block add", 1, (test_callback_fn
)block_add_regression
},
6007 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6011 test_st async_tests
[] ={
6012 {"add", 1, (test_callback_fn
)add_wrapper
},
6016 test_st string_tests
[] ={
6017 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6018 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6019 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6020 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6021 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6022 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6023 {"string_alloc_append_multiple", 0, (test_callback_fn
)string_alloc_append_multiple
},
6024 {0, 0, (test_callback_fn
)0}
6027 test_st result_tests
[] ={
6028 {"result static", 0, (test_callback_fn
)result_static
},
6029 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6030 {0, 0, (test_callback_fn
)0}
6033 test_st version_1_2_3
[] ={
6034 {"append", 0, (test_callback_fn
)append_test
},
6035 {"prepend", 0, (test_callback_fn
)prepend_test
},
6036 {"cas", 0, (test_callback_fn
)cas_test
},
6037 {"cas2", 0, (test_callback_fn
)cas2_test
},
6038 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6039 {0, 0, (test_callback_fn
)0}
6042 test_st user_tests
[] ={
6043 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6044 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6045 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6046 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6047 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6048 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6049 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6050 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6051 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6052 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6053 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6054 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6055 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6056 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6057 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6058 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6059 #if !defined(__sun) && !defined(__OpenBSD__)
6061 ** It seems to be something weird with the character sets..
6062 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6063 ** guess I need to find out how this is supposed to work.. Perhaps I need
6064 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6065 ** so just disable the code for now...).
6067 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6069 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6070 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6071 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6072 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6073 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6074 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6075 {0, 0, (test_callback_fn
)0}
6078 test_st replication_tests
[]= {
6079 {"set", 1, (test_callback_fn
)replication_set_test
},
6080 {"get", 0, (test_callback_fn
)replication_get_test
},
6081 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6082 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6083 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6084 {0, 0, (test_callback_fn
)0}
6088 * The following test suite is used to verify that we don't introduce
6089 * regression bugs. If you want more information about the bug / test,
6090 * you should look in the bug report at
6091 * http://bugs.launchpad.net/libmemcached
6093 test_st regression_tests
[]= {
6094 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6095 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6096 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6097 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6098 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6099 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6100 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6101 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6102 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6103 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6104 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6105 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6106 {0, 0, (test_callback_fn
)0}
6109 test_st sasl_auth_tests
[]= {
6110 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6111 {0, 0, (test_callback_fn
)0}
6114 test_st ketama_compatibility
[]= {
6115 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6116 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6117 {0, 0, (test_callback_fn
)0}
6120 test_st generate_tests
[] ={
6121 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6122 {"generate_data", 1, (test_callback_fn
)generate_data
},
6123 {"get_read", 0, (test_callback_fn
)get_read
},
6124 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6125 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6126 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6127 {"generate_data", 1, (test_callback_fn
)generate_data
},
6128 {"mget_read", 0, (test_callback_fn
)mget_read
},
6129 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6130 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6131 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6132 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6133 {"generate_data", 1, (test_callback_fn
)generate_data
},
6134 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6135 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6136 {0, 0, (test_callback_fn
)0}
6139 test_st consistent_tests
[] ={
6140 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6141 {"generate_data", 1, (test_callback_fn
)generate_data
},
6142 {"get_read", 0, (test_callback_fn
)get_read_count
},
6143 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6144 {0, 0, (test_callback_fn
)0}
6147 test_st consistent_weighted_tests
[] ={
6148 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6149 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6150 {"get_read", 0, (test_callback_fn
)get_read_count
},
6151 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6152 {0, 0, (test_callback_fn
)0}
6155 test_st hsieh_availability
[] ={
6156 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6157 {0, 0, (test_callback_fn
)0}
6160 test_st murmur_availability
[] ={
6161 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6162 {0, 0, (test_callback_fn
)0}
6166 test_st hash_sanity
[] ={
6167 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6168 {0, 0, (test_callback_fn
)0}
6172 test_st ketama_auto_eject_hosts
[] ={
6173 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6174 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6175 {0, 0, (test_callback_fn
)0}
6178 test_st hash_tests
[] ={
6179 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6180 {"md5", 0, (test_callback_fn
)md5_run
},
6181 {"crc", 0, (test_callback_fn
)crc_run
},
6182 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6183 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6184 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6185 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6186 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6187 {"murmur", 0, (test_callback_fn
)murmur_run
},
6188 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6189 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6190 {0, 0, (test_callback_fn
)0}
6193 test_st error_conditions
[] ={
6194 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6195 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6196 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6197 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6198 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6199 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6200 {0, 0, (test_callback_fn
)0}
6204 test_st parser_tests
[] ={
6205 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6206 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6207 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6208 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6209 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6210 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6211 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6212 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6213 {"randomly generated options", 0, (test_callback_fn
)random_statement_build_test
},
6214 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6215 {"server", 0, (test_callback_fn
)server_test
},
6216 {"bad server strings", 0, (test_callback_fn
)servers_bad_test
},
6217 {"server with weights", 0, (test_callback_fn
)server_with_weight_test
},
6218 {0, 0, (test_callback_fn
)0}
6221 test_st virtual_bucket_tests
[] ={
6222 {"basic", 0, (test_callback_fn
)virtual_back_map
},
6223 {0, 0, (test_callback_fn
)0}
6226 collection_st collection
[] ={
6228 {"hash_sanity", 0, 0, hash_sanity
},
6230 {"basic", 0, 0, basic_tests
},
6231 {"hsieh_availability", 0, 0, hsieh_availability
},
6232 {"murmur_availability", 0, 0, murmur_availability
},
6233 {"block", 0, 0, tests
},
6234 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6235 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6236 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6237 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6238 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6239 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6240 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6241 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6242 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6243 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6244 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6245 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6246 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6247 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6248 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6249 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6250 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6251 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6252 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6253 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6254 #ifdef MEMCACHED_ENABLE_DEPRECATED
6255 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6257 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6258 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6259 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6260 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6261 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6262 {"string", 0, 0, string_tests
},
6263 {"result", 0, 0, result_tests
},
6264 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6265 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6266 {"user", 0, 0, user_tests
},
6267 {"generate", 0, 0, generate_tests
},
6268 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6269 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6270 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6271 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6272 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6273 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6274 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6276 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6277 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6278 {"consistent_not", 0, 0, consistent_tests
},
6279 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6280 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6281 {"ketama_compat", 0, 0, ketama_compatibility
},
6282 {"test_hashes", 0, 0, hash_tests
},
6283 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6284 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6285 {"regression", 0, 0, regression_tests
},
6286 {"behaviors", 0, 0, behavior_tests
},
6287 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6288 {"error_conditions", 0, 0, error_conditions
},
6289 {"parser", 0, 0, parser_tests
},
6290 {"virtual buckets", 0, 0, virtual_bucket_tests
},
6294 #include "libmemcached_world.h"
6296 void get_world(world_st
*world
)
6298 world
->collections
= collection
;
6300 world
->create
= (test_callback_create_fn
)world_create
;
6301 world
->destroy
= (test_callback_fn
)world_destroy
;
6303 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6304 world
->test
.flush
= (test_callback_fn
)world_flush
;
6305 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6306 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6307 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6309 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6310 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6312 world
->runner
= &defualt_libmemcached_runner
;