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 uint64_t query_id
= memcached_query_id(memc
);
771 rc
= memcached_flush(memc
, 0);
772 test_compare(rc
, MEMCACHED_SUCCESS
);
773 test_compare(query_id
+1, memcached_query_id(memc
));
778 static memcached_return_t
server_function(const memcached_st
*ptr
,
779 const memcached_server_st
*server
,
782 (void)ptr
; (void)server
; (void)context
;
785 return MEMCACHED_SUCCESS
;
788 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
791 strncpy(context
, "foo bad", sizeof(context
));
792 memcached_server_fn callbacks
[1];
794 callbacks
[0]= server_function
;
795 memcached_server_cursor(memc
, callbacks
, context
, 1);
799 static test_return_t
bad_key_test(memcached_st
*memc
)
801 memcached_return_t rc
;
802 const char *key
= "foo bad";
804 size_t string_length
;
806 memcached_st
*memc_clone
;
808 size_t max_keylen
= 0xffff;
810 // Just skip if we are in binary mode.
811 uint64_t query_id
= memcached_query_id(memc
);
812 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
814 test_compare(query_id
, memcached_query_id(memc
)); // We should not increase the query_id for memcached_behavior_get()
816 memc_clone
= memcached_clone(NULL
, memc
);
817 test_true(memc_clone
);
819 query_id
= memcached_query_id(memc_clone
);
820 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
821 test_true(rc
== MEMCACHED_SUCCESS
);
822 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
824 /* All keys are valid in the binary protocol (except for length) */
825 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
827 query_id
= memcached_query_id(memc_clone
);
828 string
= memcached_get(memc_clone
, key
, strlen(key
),
829 &string_length
, &flags
, &rc
);
830 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
831 test_true(string_length
== 0);
835 query_id
= memcached_query_id(memc_clone
);
836 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
837 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
838 test_true(rc
== MEMCACHED_SUCCESS
);
839 string
= memcached_get(memc_clone
, key
, strlen(key
),
840 &string_length
, &flags
, &rc
);
841 test_true(rc
== MEMCACHED_NOTFOUND
);
842 test_true(string_length
== 0);
845 /* Test multi key for bad keys */
846 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
847 size_t key_lengths
[] = { 7, 7, 7 };
849 query_id
= memcached_query_id(memc_clone
);
850 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
851 test_true(rc
== MEMCACHED_SUCCESS
);
852 test_compare(query_id
, memcached_query_id(memc_clone
));
854 query_id
= memcached_query_id(memc_clone
);
855 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
856 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
857 test_compare(query_id
+1, memcached_query_id(memc_clone
));
859 query_id
= memcached_query_id(memc_clone
);
860 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
861 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
862 test_compare(query_id
+1, memcached_query_id(memc_clone
));
866 /* The following test should be moved to the end of this function when the
867 memcached server is updated to allow max size length of the keys in the
870 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
871 test_true(rc
== MEMCACHED_SUCCESS
);
873 char *longkey
= (char *)malloc(max_keylen
+ 1);
876 memset(longkey
, 'a', max_keylen
+ 1);
877 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
878 &string_length
, &flags
, &rc
);
879 test_true(rc
== MEMCACHED_NOTFOUND
);
880 test_true(string_length
== 0);
883 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
884 &string_length
, &flags
, &rc
);
885 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
886 test_true(string_length
== 0);
893 /* Make sure zero length keys are marked as bad */
895 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
896 test_true(rc
== MEMCACHED_SUCCESS
);
897 string
= memcached_get(memc_clone
, key
, 0,
898 &string_length
, &flags
, &rc
);
899 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
900 test_true(string_length
== 0);
903 memcached_free(memc_clone
);
908 #define READ_THROUGH_VALUE "set for me"
909 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
912 memcached_result_st
*result
)
914 (void)memc
;(void)key
;(void)key_length
;
915 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
918 static test_return_t
read_through(memcached_st
*memc
)
920 memcached_return_t rc
;
921 const char *key
= "foo";
923 size_t string_length
;
925 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
927 string
= memcached_get(memc
, key
, strlen(key
),
928 &string_length
, &flags
, &rc
);
930 test_true(rc
== MEMCACHED_NOTFOUND
);
931 test_false(string_length
);
934 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
936 test_true(rc
== MEMCACHED_SUCCESS
);
938 string
= memcached_get(memc
, key
, strlen(key
),
939 &string_length
, &flags
, &rc
);
941 test_true(rc
== MEMCACHED_SUCCESS
);
942 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
943 test_strcmp(READ_THROUGH_VALUE
, string
);
946 string
= memcached_get(memc
, key
, strlen(key
),
947 &string_length
, &flags
, &rc
);
949 test_true(rc
== MEMCACHED_SUCCESS
);
950 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
951 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
957 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
961 (void)ptr
;(void)key_length
;
964 return MEMCACHED_SUCCESS
;
967 static test_return_t
delete_through(memcached_st
*memc
)
969 memcached_trigger_delete_key_fn callback
;
970 memcached_return_t rc
;
972 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
974 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
975 test_true(rc
== MEMCACHED_SUCCESS
);
980 static test_return_t
get_test(memcached_st
*memc
)
982 memcached_return_t rc
;
983 const char *key
= "foo";
985 size_t string_length
;
988 uint64_t query_id
= memcached_query_id(memc
);
989 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
990 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
991 test_compare(query_id
+1, memcached_query_id(memc
));
993 string
= memcached_get(memc
, key
, strlen(key
),
994 &string_length
, &flags
, &rc
);
996 test_true(rc
== MEMCACHED_NOTFOUND
);
997 test_false(string_length
);
1000 return TEST_SUCCESS
;
1003 static test_return_t
get_test2(memcached_st
*memc
)
1005 memcached_return_t rc
;
1006 const char *key
= "foo";
1007 const char *value
= "when we sanitize";
1009 size_t string_length
;
1012 uint64_t query_id
= memcached_query_id(memc
);
1013 rc
= memcached_set(memc
, key
, strlen(key
),
1014 value
, strlen(value
),
1015 (time_t)0, (uint32_t)0);
1016 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1017 test_compare(query_id
+1, memcached_query_id(memc
));
1019 query_id
= memcached_query_id(memc
);
1020 string
= memcached_get(memc
, key
, strlen(key
),
1021 &string_length
, &flags
, &rc
);
1022 test_compare(query_id
+1, memcached_query_id(memc
));
1025 test_true(rc
== MEMCACHED_SUCCESS
);
1026 test_true(string_length
== strlen(value
));
1027 test_memcmp(string
, value
, string_length
);
1031 return TEST_SUCCESS
;
1034 static test_return_t
set_test2(memcached_st
*memc
)
1036 memcached_return_t rc
;
1037 const char *key
= "foo";
1038 const char *value
= "train in the brain";
1039 size_t value_length
= strlen(value
);
1042 for (x
= 0; x
< 10; x
++)
1044 rc
= memcached_set(memc
, key
, strlen(key
),
1045 value
, value_length
,
1046 (time_t)0, (uint32_t)0);
1047 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1050 return TEST_SUCCESS
;
1053 static test_return_t
set_test3(memcached_st
*memc
)
1055 memcached_return_t rc
;
1057 size_t value_length
= 8191;
1059 value
= (char*)malloc(value_length
);
1062 for (uint32_t x
= 0; x
< value_length
; x
++)
1063 value
[x
] = (char) (x
% 127);
1065 /* The dump test relies on there being at least 32 items in memcached */
1066 for (uint32_t x
= 0; x
< 32; x
++)
1070 snprintf(key
, sizeof(key
), "foo%u", x
);
1072 uint64_t query_id
= memcached_query_id(memc
);
1073 rc
= memcached_set(memc
, key
, strlen(key
),
1074 value
, value_length
,
1075 (time_t)0, (uint32_t)0);
1076 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1077 test_compare(query_id
+1, memcached_query_id(memc
));
1082 return TEST_SUCCESS
;
1085 static test_return_t
get_test3(memcached_st
*memc
)
1087 memcached_return_t rc
;
1088 const char *key
= "foo";
1090 size_t value_length
= 8191;
1092 size_t string_length
;
1096 value
= (char*)malloc(value_length
);
1099 for (x
= 0; x
< value_length
; x
++)
1100 value
[x
] = (char) (x
% 127);
1102 rc
= memcached_set(memc
, key
, strlen(key
),
1103 value
, value_length
,
1104 (time_t)0, (uint32_t)0);
1105 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1107 string
= memcached_get(memc
, key
, strlen(key
),
1108 &string_length
, &flags
, &rc
);
1110 test_true(rc
== MEMCACHED_SUCCESS
);
1112 test_true(string_length
== value_length
);
1113 test_true(!memcmp(string
, value
, string_length
));
1118 return TEST_SUCCESS
;
1121 static test_return_t
get_test4(memcached_st
*memc
)
1123 memcached_return_t rc
;
1124 const char *key
= "foo";
1126 size_t value_length
= 8191;
1128 size_t string_length
;
1132 value
= (char*)malloc(value_length
);
1135 for (x
= 0; x
< value_length
; x
++)
1136 value
[x
] = (char) (x
% 127);
1138 rc
= memcached_set(memc
, key
, strlen(key
),
1139 value
, value_length
,
1140 (time_t)0, (uint32_t)0);
1141 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1143 for (x
= 0; x
< 10; x
++)
1145 string
= memcached_get(memc
, key
, strlen(key
),
1146 &string_length
, &flags
, &rc
);
1148 test_true(rc
== MEMCACHED_SUCCESS
);
1150 test_true(string_length
== value_length
);
1151 test_true(!memcmp(string
, value
, string_length
));
1157 return TEST_SUCCESS
;
1161 * This test verifies that memcached_read_one_response doesn't try to
1162 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1163 * responses before you execute a storage command.
1165 static test_return_t
get_test5(memcached_st
*memc
)
1168 ** Request the same key twice, to ensure that we hash to the same server
1169 ** (so that we have multiple response values queued up) ;-)
1171 const char *keys
[]= { "key", "key" };
1172 size_t lengths
[]= { 3, 3 };
1176 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1177 keys
[0], lengths
[0], 0, 0);
1178 test_true(rc
== MEMCACHED_SUCCESS
);
1179 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1181 memcached_result_st results_obj
;
1182 memcached_result_st
*results
;
1183 results
=memcached_result_create(memc
, &results_obj
);
1185 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1187 memcached_result_free(&results_obj
);
1189 /* Don't read out the second result, but issue a set instead.. */
1190 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1191 test_true(rc
== MEMCACHED_SUCCESS
);
1193 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1194 &rlen
, &flags
, &rc
);
1195 test_true(val
== NULL
);
1196 test_true(rc
== MEMCACHED_NOTFOUND
);
1197 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1198 test_true(val
!= NULL
);
1199 test_true(rc
== MEMCACHED_SUCCESS
);
1202 return TEST_SUCCESS
;
1205 static test_return_t
mget_end(memcached_st
*memc
)
1207 const char *keys
[]= { "foo", "foo2" };
1208 size_t lengths
[]= { 3, 4 };
1209 const char *values
[]= { "fjord", "41" };
1211 memcached_return_t rc
;
1214 for (int i
= 0; i
< 2; i
++)
1216 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1217 (time_t)0, (uint32_t)0);
1218 test_true(rc
== MEMCACHED_SUCCESS
);
1222 size_t string_length
;
1225 // retrieve both via mget
1226 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1227 test_true(rc
== MEMCACHED_SUCCESS
);
1229 char key
[MEMCACHED_MAX_KEY
];
1232 // this should get both
1233 for (int i
= 0; i
< 2; i
++)
1235 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1237 test_true(rc
== MEMCACHED_SUCCESS
);
1239 if (key_length
== 4)
1241 test_true(string_length
== strlen(values
[val
]));
1242 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1246 // this should indicate end
1247 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1248 test_true(rc
== MEMCACHED_END
);
1251 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1252 test_true(rc
== MEMCACHED_SUCCESS
);
1254 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1255 test_true(key_length
== lengths
[0]);
1256 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1257 test_true(string_length
== strlen(values
[0]));
1258 test_true(strncmp(values
[0], string
, string_length
) == 0);
1259 test_true(rc
== MEMCACHED_SUCCESS
);
1262 // this should indicate end
1263 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1264 test_true(rc
== MEMCACHED_END
);
1266 return TEST_SUCCESS
;
1269 /* Do not copy the style of this code, I just access hosts to testthis function */
1270 static test_return_t
stats_servername_test(memcached_st
*memc
)
1272 memcached_return_t rc
;
1273 memcached_stat_st memc_stat
;
1274 memcached_server_instance_st instance
=
1275 memcached_server_instance_by_position(memc
, 0);
1277 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1278 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1279 return TEST_SKIPPED
;
1281 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1282 memcached_server_name(instance
),
1283 memcached_server_port(instance
));
1285 return TEST_SUCCESS
;
1288 static test_return_t
increment_test(memcached_st
*memc
)
1290 uint64_t new_number
;
1291 memcached_return_t rc
;
1292 const char *key
= "number";
1293 const char *value
= "0";
1295 rc
= memcached_set(memc
, key
, strlen(key
),
1296 value
, strlen(value
),
1297 (time_t)0, (uint32_t)0);
1298 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1300 rc
= memcached_increment(memc
, key
, strlen(key
),
1302 test_true(rc
== MEMCACHED_SUCCESS
);
1303 test_true(new_number
== 1);
1305 rc
= memcached_increment(memc
, key
, strlen(key
),
1307 test_true(rc
== MEMCACHED_SUCCESS
);
1308 test_true(new_number
== 2);
1310 return TEST_SUCCESS
;
1313 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1315 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1317 uint64_t new_number
;
1318 memcached_return_t rc
;
1319 const char *key
= "number";
1320 uint64_t initial
= 0;
1322 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1323 1, initial
, 0, &new_number
);
1324 test_true(rc
== MEMCACHED_SUCCESS
);
1325 test_true(new_number
== initial
);
1327 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1328 1, initial
, 0, &new_number
);
1329 test_true(rc
== MEMCACHED_SUCCESS
);
1330 test_true(new_number
== (initial
+ 1));
1332 return TEST_SUCCESS
;
1335 static test_return_t
decrement_test(memcached_st
*memc
)
1337 uint64_t new_number
;
1338 memcached_return_t rc
;
1339 const char *key
= "number";
1340 const char *value
= "3";
1342 rc
= memcached_set(memc
, key
, strlen(key
),
1343 value
, strlen(value
),
1344 (time_t)0, (uint32_t)0);
1345 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1347 rc
= memcached_decrement(memc
, key
, strlen(key
),
1349 test_true(rc
== MEMCACHED_SUCCESS
);
1350 test_true(new_number
== 2);
1352 rc
= memcached_decrement(memc
, key
, strlen(key
),
1354 test_true(rc
== MEMCACHED_SUCCESS
);
1355 test_true(new_number
== 1);
1357 return TEST_SUCCESS
;
1360 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1362 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1364 uint64_t new_number
;
1365 memcached_return_t rc
;
1366 const char *key
= "number";
1367 uint64_t initial
= 3;
1369 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1370 1, initial
, 0, &new_number
);
1371 test_true(rc
== MEMCACHED_SUCCESS
);
1372 test_true(new_number
== initial
);
1374 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1375 1, initial
, 0, &new_number
);
1376 test_true(rc
== MEMCACHED_SUCCESS
);
1377 test_true(new_number
== (initial
- 1));
1379 return TEST_SUCCESS
;
1382 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1384 uint64_t new_number
;
1385 memcached_return_t rc
;
1386 const char *master_key
= "foo";
1387 const char *key
= "number";
1388 const char *value
= "0";
1390 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1392 value
, strlen(value
),
1393 (time_t)0, (uint32_t)0);
1394 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1396 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1398 test_true(rc
== MEMCACHED_SUCCESS
);
1399 test_true(new_number
== 1);
1401 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1403 test_true(rc
== MEMCACHED_SUCCESS
);
1404 test_true(new_number
== 2);
1406 return TEST_SUCCESS
;
1409 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1411 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1413 uint64_t new_number
;
1414 memcached_return_t rc
;
1415 const char *master_key
= "foo";
1416 const char *key
= "number";
1417 uint64_t initial
= 0;
1419 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1421 1, initial
, 0, &new_number
);
1422 test_true(rc
== MEMCACHED_SUCCESS
);
1423 test_true(new_number
== initial
);
1425 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1427 1, initial
, 0, &new_number
);
1428 test_true(rc
== MEMCACHED_SUCCESS
);
1429 test_true(new_number
== (initial
+ 1));
1431 return TEST_SUCCESS
;
1434 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1436 uint64_t new_number
;
1437 memcached_return_t rc
;
1438 const char *master_key
= "foo";
1439 const char *key
= "number";
1440 const char *value
= "3";
1442 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1444 value
, strlen(value
),
1445 (time_t)0, (uint32_t)0);
1446 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1448 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1451 test_true(rc
== MEMCACHED_SUCCESS
);
1452 test_true(new_number
== 2);
1454 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1457 test_true(rc
== MEMCACHED_SUCCESS
);
1458 test_true(new_number
== 1);
1460 return TEST_SUCCESS
;
1463 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1465 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1467 uint64_t new_number
;
1468 memcached_return_t rc
;
1469 const char *master_key
= "foo";
1470 const char *key
= "number";
1471 uint64_t initial
= 3;
1473 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1475 1, initial
, 0, &new_number
);
1476 test_true(rc
== MEMCACHED_SUCCESS
);
1477 test_true(new_number
== initial
);
1479 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1481 1, initial
, 0, &new_number
);
1482 test_true(rc
== MEMCACHED_SUCCESS
);
1483 test_true(new_number
== (initial
- 1));
1485 return TEST_SUCCESS
;
1488 static test_return_t
quit_test(memcached_st
*memc
)
1490 memcached_return_t rc
;
1491 const char *key
= "fudge";
1492 const char *value
= "sanford and sun";
1494 rc
= memcached_set(memc
, key
, strlen(key
),
1495 value
, strlen(value
),
1496 (time_t)10, (uint32_t)3);
1497 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1498 memcached_quit(memc
);
1500 rc
= memcached_set(memc
, key
, strlen(key
),
1501 value
, strlen(value
),
1502 (time_t)50, (uint32_t)9);
1503 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1505 return TEST_SUCCESS
;
1508 static test_return_t
mget_result_test(memcached_st
*memc
)
1510 memcached_return_t rc
;
1511 const char *keys
[]= {"fudge", "son", "food"};
1512 size_t key_length
[]= {5, 3, 4};
1515 memcached_result_st results_obj
;
1516 memcached_result_st
*results
;
1518 results
= memcached_result_create(memc
, &results_obj
);
1520 test_true(&results_obj
== results
);
1522 /* We need to empty the server before continueing test */
1523 rc
= memcached_flush(memc
, 0);
1524 test_true(rc
== MEMCACHED_SUCCESS
);
1526 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1527 test_true(rc
== MEMCACHED_SUCCESS
);
1529 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1534 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1535 test_true(!results
);
1536 test_true(rc
== MEMCACHED_END
);
1538 for (x
= 0; x
< 3; x
++)
1540 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1541 keys
[x
], key_length
[x
],
1542 (time_t)50, (uint32_t)9);
1543 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1546 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1547 test_true(rc
== MEMCACHED_SUCCESS
);
1549 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1552 test_true(&results_obj
== results
);
1553 test_true(rc
== MEMCACHED_SUCCESS
);
1554 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1555 test_true(!memcmp(memcached_result_key_value(results
),
1556 memcached_result_value(results
),
1557 memcached_result_length(results
)));
1560 memcached_result_free(&results_obj
);
1562 return TEST_SUCCESS
;
1565 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1567 memcached_return_t rc
;
1568 const char *keys
[]= {"fudge", "son", "food"};
1569 size_t key_length
[]= {5, 3, 4};
1572 memcached_result_st
*results
;
1574 /* We need to empty the server before continueing test */
1575 rc
= memcached_flush(memc
, 0);
1576 test_true(rc
== MEMCACHED_SUCCESS
);
1578 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1579 test_true(rc
== MEMCACHED_SUCCESS
);
1581 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1585 test_true(!results
);
1586 test_true(rc
== MEMCACHED_END
);
1588 for (x
= 0; x
< 3; x
++)
1590 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1591 keys
[x
], key_length
[x
],
1592 (time_t)50, (uint32_t)9);
1593 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1596 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1597 test_true(rc
== MEMCACHED_SUCCESS
);
1600 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1603 test_true(rc
== MEMCACHED_SUCCESS
);
1604 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1605 test_true(!memcmp(memcached_result_key_value(results
),
1606 memcached_result_value(results
),
1607 memcached_result_length(results
)));
1608 memcached_result_free(results
);
1612 return TEST_SUCCESS
;
1615 /* Count the results */
1616 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1617 memcached_result_st
*result
,
1620 (void)ptr
; (void)result
;
1621 size_t *counter
= (size_t *)context
;
1623 *counter
= *counter
+ 1;
1625 return MEMCACHED_SUCCESS
;
1628 static test_return_t
mget_result_function(memcached_st
*memc
)
1630 memcached_return_t rc
;
1631 const char *keys
[]= {"fudge", "son", "food"};
1632 size_t key_length
[]= {5, 3, 4};
1635 memcached_execute_fn callbacks
[1];
1637 /* We need to empty the server before continueing test */
1638 rc
= memcached_flush(memc
, 0);
1639 for (x
= 0; x
< 3; x
++)
1641 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1642 keys
[x
], key_length
[x
],
1643 (time_t)50, (uint32_t)9);
1644 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1647 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1648 test_true(rc
== MEMCACHED_SUCCESS
);
1650 callbacks
[0]= &callback_counter
;
1652 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1654 test_true(counter
== 3);
1656 return TEST_SUCCESS
;
1659 static test_return_t
mget_test(memcached_st
*memc
)
1661 memcached_return_t rc
;
1662 const char *keys
[]= {"fudge", "son", "food"};
1663 size_t key_length
[]= {5, 3, 4};
1667 char return_key
[MEMCACHED_MAX_KEY
];
1668 size_t return_key_length
;
1670 size_t return_value_length
;
1672 /* We need to empty the server before continueing test */
1673 rc
= memcached_flush(memc
, 0);
1674 test_true(rc
== MEMCACHED_SUCCESS
);
1676 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1677 test_true(rc
== MEMCACHED_SUCCESS
);
1679 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1680 &return_value_length
, &flags
, &rc
)) != NULL
)
1682 test_true(return_value
);
1684 test_true(!return_value
);
1685 test_true(return_value_length
== 0);
1686 test_true(rc
== MEMCACHED_END
);
1688 for (x
= 0; x
< 3; x
++)
1690 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1691 keys
[x
], key_length
[x
],
1692 (time_t)50, (uint32_t)9);
1693 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1696 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1697 test_true(rc
== MEMCACHED_SUCCESS
);
1700 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1701 &return_value_length
, &flags
, &rc
)))
1703 test_true(return_value
);
1704 test_true(rc
== MEMCACHED_SUCCESS
);
1705 test_true(return_key_length
== return_value_length
);
1706 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1711 return TEST_SUCCESS
;
1714 static test_return_t
mget_execute(memcached_st
*memc
)
1718 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1722 * I only want to hit _one_ server so I know the number of requests I'm
1723 * sending in the pipeline.
1725 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1726 memc
->number_of_hosts
= 1;
1728 size_t max_keys
= 20480;
1731 char **keys
= calloc(max_keys
, sizeof(char*));
1732 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1734 /* First add all of the items.. */
1735 char blob
[1024] = {0};
1736 memcached_return_t rc
;
1738 for (size_t x
= 0; x
< max_keys
; ++x
)
1742 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1744 test_true(keys
[x
] != NULL
);
1745 uint64_t query_id
= memcached_query_id(memc
);
1746 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1747 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1748 test_compare(query_id
+1, memcached_query_id(memc
));
1751 /* Try to get all of them with a large multiget */
1753 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1754 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1755 max_keys
, callbacks
, &counter
, 1);
1757 if (rc
== MEMCACHED_SUCCESS
)
1760 uint64_t query_id
= memcached_query_id(memc
);
1761 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1762 test_true(rc
== MEMCACHED_END
);
1763 test_compare(query_id
, memcached_query_id(memc
));
1765 /* Verify that we got all of the items */
1766 test_true(counter
== max_keys
);
1768 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1770 test_true(counter
== 0);
1774 test_fail("note: this test functions differently when in binary mode");
1777 /* Release all allocated resources */
1778 for (size_t x
= 0; x
< max_keys
; ++x
)
1785 memc
->number_of_hosts
= number_of_hosts
;
1786 return TEST_SUCCESS
;
1789 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1791 static test_return_t
key_setup(memcached_st
*memc
)
1795 if (pre_binary(memc
) != TEST_SUCCESS
)
1796 return TEST_SKIPPED
;
1798 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1800 return TEST_SUCCESS
;
1803 static test_return_t
key_teardown(memcached_st
*memc
)
1806 pairs_free(global_pairs
);
1808 return TEST_SUCCESS
;
1811 static test_return_t
block_add_regression(memcached_st
*memc
)
1813 /* First add all of the items.. */
1814 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1816 memcached_return_t rc
;
1817 char blob
[1024] = {0};
1819 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1820 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1823 return TEST_SUCCESS
;
1826 static test_return_t
binary_add_regression(memcached_st
*memc
)
1828 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1829 test_return_t rc
= block_add_regression(memc
);
1830 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1834 static test_return_t
get_stats_keys(memcached_st
*memc
)
1838 memcached_stat_st memc_stat
;
1839 memcached_return_t rc
;
1841 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1842 test_true(rc
== MEMCACHED_SUCCESS
);
1843 for (ptr
= stat_list
; *ptr
; ptr
++)
1848 return TEST_SUCCESS
;
1851 static test_return_t
version_string_test(memcached_st
*memc
)
1853 const char *version_string
;
1856 version_string
= memcached_lib_version();
1858 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1860 return TEST_SUCCESS
;
1863 static test_return_t
get_stats(memcached_st
*memc
)
1867 memcached_return_t rc
;
1868 memcached_stat_st
*memc_stat
;
1870 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1871 test_true(rc
== MEMCACHED_SUCCESS
);
1873 test_true(rc
== MEMCACHED_SUCCESS
);
1874 test_true(memc_stat
);
1876 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1878 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1879 test_true(rc
== MEMCACHED_SUCCESS
);
1880 for (ptr
= stat_list
; *ptr
; ptr
++);
1885 memcached_stat_free(NULL
, memc_stat
);
1887 return TEST_SUCCESS
;
1890 static test_return_t
add_host_test(memcached_st
*memc
)
1893 memcached_server_st
*servers
;
1894 memcached_return_t rc
;
1895 char servername
[]= "0.example.com";
1897 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1899 test_true(1 == memcached_server_list_count(servers
));
1901 for (x
= 2; x
< 20; x
++)
1903 char buffer
[SMALL_STRING_LEN
];
1905 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1906 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1908 test_true(rc
== MEMCACHED_SUCCESS
);
1909 test_true(x
== memcached_server_list_count(servers
));
1912 rc
= memcached_server_push(memc
, servers
);
1913 test_true(rc
== MEMCACHED_SUCCESS
);
1914 rc
= memcached_server_push(memc
, servers
);
1915 test_true(rc
== MEMCACHED_SUCCESS
);
1917 memcached_server_list_free(servers
);
1919 return TEST_SUCCESS
;
1922 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1924 (void)parent
;(void)memc_clone
;
1925 return MEMCACHED_SUCCESS
;
1928 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1931 return MEMCACHED_SUCCESS
;
1934 static test_return_t
callback_test(memcached_st
*memc
)
1936 /* Test User Data */
1940 memcached_return_t rc
;
1942 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1943 test_true(rc
== MEMCACHED_SUCCESS
);
1944 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1945 test_true(*test_ptr
== x
);
1948 /* Test Clone Callback */
1950 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1951 void *clone_cb_ptr
= *(void **)&clone_cb
;
1952 void *temp_function
= NULL
;
1953 memcached_return_t rc
;
1955 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1957 test_true(rc
== MEMCACHED_SUCCESS
);
1958 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1959 test_true(temp_function
== clone_cb_ptr
);
1962 /* Test Cleanup Callback */
1964 memcached_cleanup_fn cleanup_cb
=
1965 (memcached_cleanup_fn
)cleanup_test_callback
;
1966 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1967 void *temp_function
= NULL
;
1968 memcached_return_t rc
;
1970 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1972 test_true(rc
== MEMCACHED_SUCCESS
);
1973 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1974 test_true(temp_function
== cleanup_cb_ptr
);
1977 return TEST_SUCCESS
;
1980 /* We don't test the behavior itself, we test the switches */
1981 static test_return_t
behavior_test(memcached_st
*memc
)
1986 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1987 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1988 test_true(value
== 1);
1990 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1991 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1992 test_true(value
== 1);
1994 set
= MEMCACHED_HASH_MD5
;
1995 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1996 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1997 test_true(value
== MEMCACHED_HASH_MD5
);
2001 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2002 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
2003 test_true(value
== 0);
2005 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2006 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
2007 test_true(value
== 0);
2009 set
= MEMCACHED_HASH_DEFAULT
;
2010 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2011 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2012 test_true(value
== MEMCACHED_HASH_DEFAULT
);
2014 set
= MEMCACHED_HASH_CRC
;
2015 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2016 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2017 test_true(value
== MEMCACHED_HASH_CRC
);
2019 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2020 test_true(value
> 0);
2022 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2023 test_true(value
> 0);
2025 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
2026 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
2027 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2029 return TEST_SUCCESS
;
2032 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2034 memcached_return_t rc
;
2037 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2038 test_true(rc
== MEMCACHED_DEPRECATED
);
2040 // Platform dependent
2042 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2046 return TEST_SUCCESS
;
2050 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2052 memcached_return_t rc
;
2056 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2057 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2059 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2061 if (rc
== MEMCACHED_SUCCESS
)
2063 test_true((bool)value
== set
);
2067 test_false((bool)value
== set
);
2070 return TEST_SUCCESS
;
2074 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2076 memcached_return_t rc
;
2080 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2081 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2083 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2085 if (rc
== MEMCACHED_SUCCESS
)
2087 test_true((bool)value
== set
);
2091 test_false((bool)value
== set
);
2094 return TEST_SUCCESS
;
2097 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2099 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2100 char return_key
[MEMCACHED_MAX_KEY
];
2101 size_t return_key_length
;
2103 size_t return_value_length
;
2108 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2109 &return_value_length
, &flags
, &rc
)))
2111 test_true(return_value
);
2112 test_true(rc
== MEMCACHED_SUCCESS
);
2114 *keys_returned
= *keys_returned
+1;
2117 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2119 return TEST_SUCCESS
;
2122 /* Test case provided by Cal Haldenbrand */
2123 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2125 unsigned int setter
= 1;
2127 unsigned long long total
= 0;
2130 char randomstuff
[6 * 1024];
2131 memcached_return_t rc
;
2133 memset(randomstuff
, 0, 6 * 1024);
2135 /* We just keep looking at the same values over and over */
2138 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2139 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2143 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2147 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2148 memset(randomstuff
, 0, 6 * 1024);
2149 test_true(size
< 6 * 1024); /* Being safe here */
2151 for (j
= 0 ; j
< size
;j
++)
2152 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2155 snprintf(key
, sizeof(key
), "%u", x
);
2156 rc
= memcached_set(memc
, key
, strlen(key
),
2157 randomstuff
, strlen(randomstuff
), 10, 0);
2158 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2159 /* If we fail, lets try again */
2160 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2161 rc
= memcached_set(memc
, key
, strlen(key
),
2162 randomstuff
, strlen(randomstuff
), 10, 0);
2163 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2166 return TEST_SUCCESS
;
2169 /* Test case provided by Cal Haldenbrand */
2170 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2172 unsigned int setter
;
2176 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2177 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2179 setter
= 20 * 1024576;
2180 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2181 setter
= 20 * 1024576;
2182 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2183 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2184 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2186 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2189 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2191 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2192 char buffer
[SMALL_STRING_LEN
];
2197 memset(buffer
, 0, SMALL_STRING_LEN
);
2199 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2200 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2201 &val_len
, &flags
, &rc
);
2202 if (rc
!= MEMCACHED_SUCCESS
)
2204 if (rc
== MEMCACHED_NOTFOUND
)
2218 return TEST_SUCCESS
;
2221 /* Do a large mget() over all the keys we think exist */
2222 #define KEY_COUNT 3000 // * 1024576
2223 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2225 memcached_return_t rc
;
2226 unsigned int setter
;
2229 size_t key_lengths
[KEY_COUNT
];
2232 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2233 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2235 setter
= 20 * 1024576;
2236 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2237 setter
= 20 * 1024576;
2238 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2239 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2240 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2243 keys
= calloc(KEY_COUNT
, sizeof(char *));
2245 for (x
= 0; x
< KEY_COUNT
; x
++)
2249 snprintf(buffer
, 30, "%u", x
);
2250 keys
[x
]= strdup(buffer
);
2251 key_lengths
[x
]= strlen(keys
[x
]);
2254 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2255 test_true(rc
== MEMCACHED_SUCCESS
);
2257 size_t keys_returned
;
2258 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2260 for (x
= 0; x
< KEY_COUNT
; x
++)
2264 return TEST_SUCCESS
;
2267 /* Make sure we behave properly if server list has no values */
2268 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2270 memcached_return_t rc
;
2271 const char *keys
[]= {"fudge", "son", "food"};
2272 size_t key_length
[]= {5, 3, 4};
2275 char return_key
[MEMCACHED_MAX_KEY
];
2276 size_t return_key_length
;
2278 size_t return_value_length
;
2280 /* Here we free everything before running a bunch of mget tests */
2281 memcached_servers_reset(memc
);
2284 /* We need to empty the server before continueing test */
2285 rc
= memcached_flush(memc
, 0);
2286 test_compare(rc
, MEMCACHED_NO_SERVERS
);
2288 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2289 test_true(rc
== MEMCACHED_NO_SERVERS
);
2291 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2292 &return_value_length
, &flags
, &rc
)) != NULL
)
2294 test_true(return_value
);
2296 test_false(return_value
);
2297 test_true(return_value_length
== 0);
2298 test_true(rc
== MEMCACHED_NO_SERVERS
);
2300 for (x
= 0; x
< 3; x
++)
2302 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2303 keys
[x
], key_length
[x
],
2304 (time_t)50, (uint32_t)9);
2305 test_true(rc
== MEMCACHED_NO_SERVERS
);
2308 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2309 test_true(rc
== MEMCACHED_NO_SERVERS
);
2312 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2313 &return_value_length
, &flags
, &rc
)))
2315 test_true(return_value
);
2316 test_true(rc
== MEMCACHED_SUCCESS
);
2317 test_true(return_key_length
== return_value_length
);
2318 test_memcmp(return_value
, return_key
, return_value_length
);
2323 return TEST_SUCCESS
;
2326 #define VALUE_SIZE_BUG5 1048064
2327 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2329 memcached_return_t rc
;
2330 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2331 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2332 char return_key
[MEMCACHED_MAX_KEY
];
2333 size_t return_key_length
;
2335 size_t value_length
;
2339 char insert_data
[VALUE_SIZE_BUG5
];
2341 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2342 insert_data
[x
]= (signed char)rand();
2344 memcached_flush(memc
, 0);
2345 value
= memcached_get(memc
, keys
[0], key_length
[0],
2346 &value_length
, &flags
, &rc
);
2347 test_true(value
== NULL
);
2348 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2351 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2352 &value_length
, &flags
, &rc
)))
2354 test_true(count
== 0);
2356 for (x
= 0; x
< 4; x
++)
2358 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2359 insert_data
, VALUE_SIZE_BUG5
,
2360 (time_t)0, (uint32_t)0);
2361 test_true(rc
== MEMCACHED_SUCCESS
);
2364 for (x
= 0; x
< 10; x
++)
2366 value
= memcached_get(memc
, keys
[0], key_length
[0],
2367 &value_length
, &flags
, &rc
);
2371 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2373 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2374 &value_length
, &flags
, &rc
)))
2379 test_true(count
== 4);
2382 return TEST_SUCCESS
;
2385 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2387 memcached_return_t rc
;
2388 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2389 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2390 char return_key
[MEMCACHED_MAX_KEY
];
2391 size_t return_key_length
;
2393 size_t value_length
;
2397 char insert_data
[VALUE_SIZE_BUG5
];
2399 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2400 insert_data
[x
]= (signed char)rand();
2402 memcached_flush(memc
, 0);
2403 value
= memcached_get(memc
, keys
[0], key_length
[0],
2404 &value_length
, &flags
, &rc
);
2405 test_true(value
== NULL
);
2406 test_true(rc
== MEMCACHED_NOTFOUND
);
2407 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2408 test_true(rc
== MEMCACHED_SUCCESS
);
2411 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2412 &value_length
, &flags
, &rc
)))
2414 test_true(count
== 0);
2415 test_true(rc
== MEMCACHED_END
);
2417 for (x
= 0; x
< 4; x
++)
2419 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2420 insert_data
, VALUE_SIZE_BUG5
,
2421 (time_t)0, (uint32_t)0);
2422 test_true(rc
== MEMCACHED_SUCCESS
);
2425 for (x
= 0; x
< 2; x
++)
2427 value
= memcached_get(memc
, keys
[0], key_length
[0],
2428 &value_length
, &flags
, &rc
);
2432 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2433 test_true(rc
== MEMCACHED_SUCCESS
);
2435 /* We test for purge of partial complete fetches */
2436 for (count
= 3; count
; count
--)
2438 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2439 &value_length
, &flags
, &rc
);
2440 test_true(rc
== MEMCACHED_SUCCESS
);
2441 test_true(!(memcmp(value
, insert_data
, value_length
)));
2442 test_true(value_length
);
2447 return TEST_SUCCESS
;
2450 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2452 memcached_return_t rc
;
2454 memcached_st
*memc_clone
;
2456 memcached_server_st
*servers
;
2457 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";
2460 servers
= memcached_servers_parse(server_list
);
2463 mine
= memcached_create(NULL
);
2464 rc
= memcached_server_push(mine
, servers
);
2465 test_true(rc
== MEMCACHED_SUCCESS
);
2466 memcached_server_list_free(servers
);
2469 memc_clone
= memcached_clone(NULL
, mine
);
2471 memcached_quit(mine
);
2472 memcached_quit(memc_clone
);
2475 memcached_free(mine
);
2476 memcached_free(memc_clone
);
2478 return TEST_SUCCESS
;
2481 /* Test flag store/retrieve */
2482 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2484 memcached_return_t rc
;
2485 const char *keys
= "036790384900";
2486 size_t key_length
= strlen(keys
);
2487 char return_key
[MEMCACHED_MAX_KEY
];
2488 size_t return_key_length
;
2490 size_t value_length
;
2493 char insert_data
[VALUE_SIZE_BUG5
];
2495 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2496 insert_data
[x
]= (signed char)rand();
2498 memcached_flush(memc
, 0);
2501 rc
= memcached_set(memc
, keys
, key_length
,
2502 insert_data
, VALUE_SIZE_BUG5
,
2504 test_true(rc
== MEMCACHED_SUCCESS
);
2507 value
= memcached_get(memc
, keys
, key_length
,
2508 &value_length
, &flags
, &rc
);
2509 test_true(flags
== 245);
2513 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2516 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2517 &value_length
, &flags
, &rc
);
2518 test_true(flags
== 245);
2523 return TEST_SUCCESS
;
2526 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2528 memcached_return_t rc
;
2529 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2530 size_t key_length
[3];
2535 char return_key
[MEMCACHED_MAX_KEY
];
2536 size_t return_key_length
;
2538 size_t return_value_length
;
2541 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2542 key_length
[1]= strlen("fudge&*@#");
2543 key_length
[2]= strlen("for^#@&$not");
2546 for (x
= 0; x
< 3; x
++)
2548 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2549 keys
[x
], key_length
[x
],
2550 (time_t)50, (uint32_t)9);
2551 test_true(rc
== MEMCACHED_SUCCESS
);
2554 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2555 test_true(rc
== MEMCACHED_SUCCESS
);
2557 /* We need to empty the server before continueing test */
2558 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2559 &return_value_length
, &flags
, &rc
)) != NULL
)
2561 test_true(return_value
);
2565 test_true(count
== 3);
2567 return TEST_SUCCESS
;
2570 /* We are testing with aggressive timeout to get failures */
2571 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2573 const char *key
= "foo";
2575 size_t value_length
= 512;
2578 memcached_return_t rc
;
2579 unsigned int set
= 1;
2580 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2583 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2584 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2586 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2589 value
= (char*)malloc(value_length
* sizeof(char));
2591 for (x
= 0; x
< value_length
; x
++)
2592 value
[x
]= (char) (x
% 127);
2594 for (x
= 1; x
<= 100000; ++x
)
2596 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2598 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2599 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2601 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2606 memcached_free(mclone
);
2608 return TEST_SUCCESS
;
2612 We are looking failures in the async protocol
2614 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2616 const char *key
= "foo";
2618 size_t value_length
= 512;
2621 memcached_return_t rc
;
2622 unsigned int set
= 1;
2624 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2626 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2627 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2629 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2632 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2634 test_true(timeout
== -1);
2636 value
= (char*)malloc(value_length
* sizeof(char));
2638 for (x
= 0; x
< value_length
; x
++)
2639 value
[x
]= (char) (x
% 127);
2641 for (x
= 1; x
<= 100000; ++x
)
2643 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2647 memcached_free(mclone
);
2649 return TEST_SUCCESS
;
2653 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2655 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2657 memcached_return_t rc
;
2659 size_t value_length
;
2661 uint64_t number_value
;
2663 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2664 &value_length
, &flags
, &rc
);
2665 test_true(value
== NULL
);
2666 test_true(rc
== MEMCACHED_NOTFOUND
);
2668 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2671 test_true(value
== NULL
);
2672 /* The binary protocol will set the key if it doesn't exist */
2673 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2675 test_true(rc
== MEMCACHED_SUCCESS
);
2679 test_true(rc
== MEMCACHED_NOTFOUND
);
2682 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2684 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2685 &value_length
, &flags
, &rc
);
2687 test_true(rc
== MEMCACHED_SUCCESS
);
2690 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2692 test_true(number_value
== 2);
2693 test_true(rc
== MEMCACHED_SUCCESS
);
2695 return TEST_SUCCESS
;
2699 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2700 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2702 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2704 char key
[] = "key34567890";
2706 memcached_return_t rc
;
2707 size_t overflowSize
;
2709 char commandFirst
[]= "set key34567890 0 0 ";
2710 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2711 size_t commandLength
;
2714 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2716 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2718 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2720 overflow
= malloc(testSize
);
2721 test_true(overflow
!= NULL
);
2723 memset(overflow
, 'x', testSize
);
2724 rc
= memcached_set(memc
, key
, strlen(key
),
2725 overflow
, testSize
, 0, 0);
2726 test_true(rc
== MEMCACHED_SUCCESS
);
2730 return TEST_SUCCESS
;
2735 Test values of many different sizes
2736 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2737 set key34567890 0 0 8169 \r\n
2738 is sent followed by buffer of size 8169, followed by 8169
2740 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2743 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2744 memcached_return_t rc
;
2745 const char *key
= "foo";
2747 size_t value_length
= 18000;
2749 size_t string_length
;
2752 size_t current_length
;
2754 value
= (char*)malloc(value_length
);
2757 for (x
= 0; x
< value_length
; x
++)
2758 value
[x
] = (char) (x
% 127);
2760 for (current_length
= 0; current_length
< value_length
; current_length
++)
2762 rc
= memcached_set(memc
, key
, strlen(key
),
2763 value
, current_length
,
2764 (time_t)0, (uint32_t)0);
2765 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2767 string
= memcached_get(memc
, key
, strlen(key
),
2768 &string_length
, &flags
, &rc
);
2770 test_true(rc
== MEMCACHED_SUCCESS
);
2771 test_true(string_length
== current_length
);
2772 test_true(!memcmp(string
, value
, string_length
));
2779 return TEST_SUCCESS
;
2783 Look for zero length value problems
2785 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2788 memcached_return_t rc
;
2789 const char *key
= "mykey";
2794 for (x
= 0; x
< 2; x
++)
2796 rc
= memcached_set(memc
, key
, strlen(key
),
2798 (time_t)0, (uint32_t)0);
2800 test_true(rc
== MEMCACHED_SUCCESS
);
2802 value
= memcached_get(memc
, key
, strlen(key
),
2803 &length
, &flags
, &rc
);
2805 test_true(rc
== MEMCACHED_SUCCESS
);
2806 test_true(value
== NULL
);
2807 test_true(length
== 0);
2808 test_true(flags
== 0);
2810 value
= memcached_get(memc
, key
, strlen(key
),
2811 &length
, &flags
, &rc
);
2813 test_true(rc
== MEMCACHED_SUCCESS
);
2814 test_true(value
== NULL
);
2815 test_true(length
== 0);
2816 test_true(flags
== 0);
2819 return TEST_SUCCESS
;
2822 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2823 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2825 memcached_return_t rc
;
2826 const char *key
= "mykey";
2831 rc
= memcached_set(memc
, key
, strlen(key
),
2833 (time_t)0, UINT32_MAX
);
2835 test_true(rc
== MEMCACHED_SUCCESS
);
2837 value
= memcached_get(memc
, key
, strlen(key
),
2838 &length
, &flags
, &rc
);
2840 test_true(rc
== MEMCACHED_SUCCESS
);
2841 test_true(value
== NULL
);
2842 test_true(length
== 0);
2843 test_true(flags
== UINT32_MAX
);
2845 return TEST_SUCCESS
;
2848 #if !defined(__sun) && !defined(__OpenBSD__)
2849 /* Check the validity of chinese key*/
2850 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2852 memcached_return_t rc
;
2853 const char *key
= "豆瓣";
2854 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2859 rc
= memcached_set(memc
, key
, strlen(key
),
2860 value
, strlen(value
),
2863 test_true(rc
== MEMCACHED_SUCCESS
);
2865 value2
= memcached_get(memc
, key
, strlen(key
),
2866 &length
, &flags
, &rc
);
2868 test_true(length
==strlen(value
));
2869 test_true(rc
== MEMCACHED_SUCCESS
);
2870 test_true(memcmp(value
, value2
, length
)==0);
2873 return TEST_SUCCESS
;
2881 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2884 const memcached_server_st
*server
;
2885 memcached_return_t res
;
2889 memc
= memcached_create(NULL
);
2890 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2891 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2893 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2895 memcached_free(memc
);
2897 return TEST_SUCCESS
;
2900 /* CAS test from Andei */
2901 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2903 memcached_return_t status
;
2904 memcached_result_st
*result
, result_obj
;
2905 const char *key
= "abc";
2906 size_t key_len
= strlen("abc");
2907 const char *value
= "foobar";
2908 size_t value_len
= strlen(value
);
2910 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2912 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2913 test_true(status
== MEMCACHED_SUCCESS
);
2915 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2916 test_true(status
== MEMCACHED_SUCCESS
);
2918 result
= memcached_result_create(memc
, &result_obj
);
2921 memcached_result_create(memc
, &result_obj
);
2922 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2925 test_true(status
== MEMCACHED_SUCCESS
);
2927 memcached_result_free(result
);
2929 return TEST_SUCCESS
;
2932 #include "ketama_test_cases.h"
2933 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2935 memcached_return_t rc
;
2938 memcached_server_st
*server_pool
;
2943 memc
= memcached_create(NULL
);
2946 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2947 test_true(rc
== MEMCACHED_SUCCESS
);
2949 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2950 test_true(value
== 1);
2952 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2953 test_true(rc
== MEMCACHED_SUCCESS
);
2955 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2956 test_true(value
== MEMCACHED_HASH_MD5
);
2958 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");
2959 memcached_server_push(memc
, server_pool
);
2961 /* verify that the server list was parsed okay. */
2962 test_true(memcached_server_count(memc
) == 8);
2963 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2964 test_true(server_pool
[0].port
== 11211);
2965 test_true(server_pool
[0].weight
== 600);
2966 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2967 test_true(server_pool
[2].port
== 11211);
2968 test_true(server_pool
[2].weight
== 200);
2969 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2970 test_true(server_pool
[7].port
== 11211);
2971 test_true(server_pool
[7].weight
== 100);
2973 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2974 * us test the boundary wraparound.
2976 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
2978 /* verify the standard ketama set. */
2979 for (x
= 0; x
< 99; x
++)
2981 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2983 memcached_server_instance_st instance
=
2984 memcached_server_instance_by_position(memc
, server_idx
);
2986 const char *hostname
= memcached_server_name(instance
);
2987 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2990 memcached_server_list_free(server_pool
);
2991 memcached_free(memc
);
2993 return TEST_SUCCESS
;
2996 /* Large mget() of missing keys with binary proto
2998 * If many binary quiet commands (such as getq's in an mget) fill the output
2999 * buffer and the server chooses not to respond, memcached_flush hangs. See
3000 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
3003 /* sighandler_t function that always asserts false */
3004 static void fail(int unused
)
3011 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
3016 return TEST_SKIPPED
;
3018 memcached_return_t rc
;
3021 size_t* key_lengths
;
3022 void (*oldalarm
)(int);
3023 memcached_st
*memc_clone
;
3025 memc_clone
= memcached_clone(NULL
, memc
);
3026 test_true(memc_clone
);
3028 /* only binproto uses getq for mget */
3029 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3031 /* empty the cache to ensure misses (hence non-responses) */
3032 rc
= memcached_flush(memc_clone
, 0);
3033 test_true(rc
== MEMCACHED_SUCCESS
);
3035 key_lengths
= calloc(key_count
, sizeof(size_t));
3036 keys
= calloc(key_count
, sizeof(char *));
3038 for (x
= 0; x
< key_count
; x
++)
3042 snprintf(buffer
, 30, "%u", x
);
3043 keys
[x
]= strdup(buffer
);
3044 key_lengths
[x
]= strlen(keys
[x
]);
3047 oldalarm
= signal(SIGALRM
, fail
);
3050 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3051 test_true(rc
== MEMCACHED_SUCCESS
);
3054 signal(SIGALRM
, oldalarm
);
3056 size_t keys_returned
;
3057 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3059 for (x
= 0; x
< key_count
; x
++)
3064 memcached_free(memc_clone
);
3066 return TEST_SUCCESS
;
3070 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3072 test_return_t test_rc
;
3073 test_rc
= pre_binary(memc
);
3075 if (test_rc
!= TEST_SUCCESS
)
3080 /* should work as of r580 */
3081 rc
= _user_supplied_bug21(memc
, 10);
3082 test_true(rc
== TEST_SUCCESS
);
3084 /* should fail as of r580 */
3085 rc
= _user_supplied_bug21(memc
, 1000);
3086 test_true(rc
== TEST_SUCCESS
);
3088 return TEST_SUCCESS
;
3091 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3094 memcached_server_instance_st instance
;
3096 memcached_return_t rc
;
3097 memcached_st
*memc
= memcached_create(NULL
);
3100 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3101 test_true(rc
== MEMCACHED_SUCCESS
);
3103 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3104 test_true(value
== 1);
3106 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3107 test_true(rc
== MEMCACHED_SUCCESS
);
3109 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3110 test_true(value
== MEMCACHED_HASH_MD5
);
3112 /* server should be removed when in delay */
3113 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3114 test_true(rc
== MEMCACHED_SUCCESS
);
3116 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3117 test_true(value
== 1);
3119 memcached_server_st
*server_pool
;
3120 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");
3121 memcached_server_push(memc
, server_pool
);
3123 /* verify that the server list was parsed okay. */
3124 test_true(memcached_server_count(memc
) == 8);
3125 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3126 test_true(server_pool
[0].port
== 11211);
3127 test_true(server_pool
[0].weight
== 600);
3128 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3129 test_true(server_pool
[2].port
== 11211);
3130 test_true(server_pool
[2].weight
== 200);
3131 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3132 test_true(server_pool
[7].port
== 11211);
3133 test_true(server_pool
[7].weight
== 100);
3135 instance
= memcached_server_instance_by_position(memc
, 2);
3136 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3137 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3140 This would not work if there were only two hosts.
3142 for (size_t x
= 0; x
< 99; x
++)
3144 memcached_autoeject(memc
);
3145 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3146 test_true(server_idx
!= 2);
3149 /* and re-added when it's back. */
3150 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3151 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3152 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3153 memc
->distribution
);
3154 for (size_t x
= 0; x
< 99; x
++)
3156 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3157 // We re-use instance from above.
3159 memcached_server_instance_by_position(memc
, server_idx
);
3160 const char *hostname
= memcached_server_name(instance
);
3161 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3164 memcached_server_list_free(server_pool
);
3165 memcached_free(memc
);
3167 return TEST_SUCCESS
;
3170 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3174 memcached_return_t rc
;
3175 memcached_st
*memc
= memcached_create(NULL
);
3179 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3180 test_true(rc
== MEMCACHED_SUCCESS
);
3182 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3183 test_true(value
== 1);
3185 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3186 test_true(rc
== MEMCACHED_SUCCESS
);
3188 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3189 test_true(value
== MEMCACHED_HASH_MD5
);
3192 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3194 memcached_server_st
*server_pool
;
3195 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");
3196 memcached_server_push(memc
, server_pool
);
3198 // @todo this needs to be refactored to actually test something.
3201 if ((fp
= fopen("ketama_keys.txt", "w")))
3205 printf("cannot write to file ketama_keys.txt");
3206 return TEST_FAILURE
;
3209 for (int x
= 0; x
< 10000; x
++)
3212 snprintf(key
, sizeof(key
), "%d", x
);
3214 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3215 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3216 in_port_t port
= memc
->hosts
[server_idx
].port
;
3217 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3218 memcached_server_instance_st instance
=
3219 memcached_server_instance_by_position(memc
, host_index
);
3223 memcached_server_list_free(server_pool
);
3224 memcached_free(memc
);
3226 return TEST_SUCCESS
;
3230 static test_return_t
result_static(memcached_st
*memc
)
3232 memcached_result_st result
;
3233 memcached_result_st
*result_ptr
;
3235 result_ptr
= memcached_result_create(memc
, &result
);
3236 test_true(result
.options
.is_allocated
== false);
3237 test_true(memcached_is_initialized(&result
) == true);
3238 test_true(result_ptr
);
3239 test_true(result_ptr
== &result
);
3241 memcached_result_free(&result
);
3243 test_true(result
.options
.is_allocated
== false);
3244 test_true(memcached_is_initialized(&result
) == false);
3246 return TEST_SUCCESS
;
3249 static test_return_t
result_alloc(memcached_st
*memc
)
3251 memcached_result_st
*result_ptr
;
3253 result_ptr
= memcached_result_create(memc
, NULL
);
3254 test_true(result_ptr
);
3255 test_true(result_ptr
->options
.is_allocated
== true);
3256 test_true(memcached_is_initialized(result_ptr
) == true);
3257 memcached_result_free(result_ptr
);
3259 return TEST_SUCCESS
;
3262 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3265 pairs_free(global_pairs
);
3267 return TEST_SUCCESS
;
3270 static test_return_t
generate_pairs(memcached_st
*memc
)
3273 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3274 global_count
= GLOBAL_COUNT
;
3276 for (size_t x
= 0; x
< global_count
; x
++)
3278 global_keys
[x
]= global_pairs
[x
].key
;
3279 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3282 return TEST_SUCCESS
;
3285 static test_return_t
generate_large_pairs(memcached_st
*memc
)
3288 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3289 global_count
= GLOBAL2_COUNT
;
3291 for (size_t x
= 0; x
< global_count
; x
++)
3293 global_keys
[x
]= global_pairs
[x
].key
;
3294 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3297 return TEST_SUCCESS
;
3300 static test_return_t
generate_data(memcached_st
*memc
)
3302 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3304 test_true(check_execute
== global_count
);
3306 return TEST_SUCCESS
;
3309 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3311 memcached_stat_st
*stat_p
;
3312 memcached_return_t rc
;
3313 uint32_t host_index
= 0;
3314 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3316 test_true(check_execute
== global_count
);
3318 //TODO: hosts used size stats
3319 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3322 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3324 /* This test was changes so that "make test" would work properlly */
3326 memcached_server_instance_st instance
=
3327 memcached_server_instance_by_position(memc
, host_index
);
3329 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3331 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3334 memcached_stat_free(NULL
, stat_p
);
3336 return TEST_SUCCESS
;
3338 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3343 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3344 generate_data(memc
);
3346 return TEST_SUCCESS
;
3349 static test_return_t
get_read_count(memcached_st
*memc
)
3351 memcached_return_t rc
;
3352 memcached_st
*memc_clone
;
3354 memc_clone
= memcached_clone(NULL
, memc
);
3355 test_true(memc_clone
);
3357 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3361 size_t return_value_length
;
3365 for (size_t x
= count
= 0; x
< global_count
; x
++)
3367 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3368 &return_value_length
, &flags
, &rc
);
3369 if (rc
== MEMCACHED_SUCCESS
)
3378 memcached_free(memc_clone
);
3380 return TEST_SUCCESS
;
3383 static test_return_t
get_read(memcached_st
*memc
)
3385 memcached_return_t rc
;
3389 size_t return_value_length
;
3392 for (size_t x
= 0; x
< global_count
; x
++)
3394 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3395 &return_value_length
, &flags
, &rc
);
3397 test_true(return_value);
3398 test_true(rc == MEMCACHED_SUCCESS);
3400 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3405 return TEST_SUCCESS
;
3408 static test_return_t
mget_read(memcached_st
*memc
)
3410 memcached_return_t rc
;
3412 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3413 return TEST_SKIPPED
;
3415 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3417 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3419 // Go fetch the keys and test to see if all of them were returned
3421 size_t keys_returned
;
3422 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3424 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3425 test_true_got(global_count
== keys_returned
, buffer
);
3429 return TEST_SUCCESS
;
3432 static test_return_t
mget_read_result(memcached_st
*memc
)
3434 memcached_return_t rc
;
3436 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3437 return TEST_SKIPPED
;
3439 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3441 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3443 /* Turn this into a help function */
3445 memcached_result_st results_obj
;
3446 memcached_result_st
*results
;
3448 results
= memcached_result_create(memc
, &results_obj
);
3450 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3453 test_true(rc
== MEMCACHED_SUCCESS
);
3456 memcached_result_free(&results_obj
);
3459 return TEST_SUCCESS
;
3462 static test_return_t
mget_read_function(memcached_st
*memc
)
3464 memcached_return_t rc
;
3466 memcached_execute_fn callbacks
[1];
3468 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3469 return TEST_SKIPPED
;
3471 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3473 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3475 callbacks
[0]= &callback_counter
;
3477 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3479 return TEST_SUCCESS
;
3482 static test_return_t
delete_generate(memcached_st
*memc
)
3484 for (size_t x
= 0; x
< global_count
; x
++)
3486 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3489 return TEST_SUCCESS
;
3492 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3497 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3499 for (size_t x
= 0; x
< global_count
; x
++)
3501 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3504 return TEST_SUCCESS
;
3507 static test_return_t
add_host_test1(memcached_st
*memc
)
3509 memcached_return_t rc
;
3510 char servername
[]= "0.example.com";
3511 memcached_server_st
*servers
;
3513 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3515 test_true(1 == memcached_server_list_count(servers
));
3517 for (size_t x
= 2; x
< 20; x
++)
3519 char buffer
[SMALL_STRING_LEN
];
3521 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3522 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3524 test_true(rc
== MEMCACHED_SUCCESS
);
3525 test_true(x
== memcached_server_list_count(servers
));
3528 rc
= memcached_server_push(memc
, servers
);
3529 test_true(rc
== MEMCACHED_SUCCESS
);
3530 rc
= memcached_server_push(memc
, servers
);
3531 test_true(rc
== MEMCACHED_SUCCESS
);
3533 memcached_server_list_free(servers
);
3535 return TEST_SUCCESS
;
3538 static test_return_t
pre_nonblock(memcached_st
*memc
)
3540 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3542 return TEST_SUCCESS
;
3545 static test_return_t
pre_cork(memcached_st
*memc
)
3547 memcached_return_t rc
;
3550 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3553 return TEST_SKIPPED
;
3556 if (rc
== MEMCACHED_SUCCESS
)
3557 return TEST_SUCCESS
;
3559 return TEST_SKIPPED
;
3562 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3569 return TEST_SKIPPED
;
3572 if (rc
!= TEST_SUCCESS
)
3575 return pre_nonblock(memc
);
3578 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3580 memcached_return_t rc
= MEMCACHED_FAILURE
;
3581 memcached_st
*memc_clone
;
3583 memc_clone
= memcached_clone(NULL
, memc
);
3584 test_true(memc_clone
);
3585 // The memcached_version needs to be done on a clone, because the server
3586 // will not toggle protocol on an connection.
3587 memcached_version(memc_clone
);
3589 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3591 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3592 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3593 test_true(rc
== MEMCACHED_SUCCESS
);
3594 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3598 return TEST_SKIPPED
;
3601 memcached_free(memc_clone
);
3603 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3606 static test_return_t
pre_murmur(memcached_st
*memc
)
3608 #ifdef HAVE_MURMUR_HASH
3609 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3610 return TEST_SUCCESS
;
3613 return TEST_SKIPPED
;
3617 static test_return_t
pre_jenkins(memcached_st
*memc
)
3619 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3621 return TEST_SUCCESS
;
3625 static test_return_t
pre_md5(memcached_st
*memc
)
3627 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3629 return TEST_SUCCESS
;
3632 static test_return_t
pre_crc(memcached_st
*memc
)
3634 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3636 return TEST_SUCCESS
;
3639 static test_return_t
pre_hsieh(memcached_st
*memc
)
3641 #ifdef HAVE_HSIEH_HASH
3642 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3643 return TEST_SUCCESS
;
3646 return TEST_SKIPPED
;
3650 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3652 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3654 return TEST_SUCCESS
;
3657 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3659 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3661 return TEST_SUCCESS
;
3664 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3666 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3668 return TEST_SUCCESS
;
3671 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3673 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3675 return TEST_SUCCESS
;
3678 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3680 memcached_return_t rc
;
3683 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3684 test_true(rc
== MEMCACHED_SUCCESS
);
3686 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3687 test_true(value
== 1);
3689 return TEST_SUCCESS
;
3692 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3694 memcached_return_t rc
;
3697 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3698 test_true(rc
== MEMCACHED_SUCCESS
);
3700 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3701 test_true(value
== 1);
3703 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3704 test_true(rc
== MEMCACHED_SUCCESS
);
3706 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3707 test_true(value
== MEMCACHED_HASH_MD5
);
3709 return TEST_SUCCESS
;
3713 @note This should be testing to see if the server really supports the binary protocol.
3715 static test_return_t
pre_binary(memcached_st
*memc
)
3717 memcached_return_t rc
= MEMCACHED_FAILURE
;
3719 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3721 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3722 test_true(rc
== MEMCACHED_SUCCESS
);
3723 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3726 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3729 static test_return_t
pre_sasl(memcached_st
*memc
)
3731 memcached_return_t rc
= MEMCACHED_FAILURE
;
3733 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3734 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3735 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3736 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3738 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3740 memcached_server_st
*servers
= memcached_servers_parse(server
);
3741 test_true(servers
!= NULL
);
3742 memcached_servers_reset(memc
);
3743 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3744 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3745 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3746 test_true(rc
== MEMCACHED_SUCCESS
);
3752 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3755 static test_return_t
pre_replication(memcached_st
*memc
)
3757 test_return_t test_rc
;
3758 test_rc
= pre_binary(memc
);
3760 if (test_rc
!= TEST_SUCCESS
)
3764 * Make sure that we store the item on all servers
3765 * (master + replicas == number of servers)
3767 memcached_return_t rc
;
3768 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3769 memcached_server_count(memc
) - 1);
3770 test_true(rc
== MEMCACHED_SUCCESS
);
3771 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3773 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3777 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3781 rc
= pre_replication(memc
);
3782 if (rc
!= TEST_SUCCESS
)
3785 rc
= pre_nonblock(memc
);
3791 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3795 #ifdef HARD_MALLOC_TESTS
3796 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3804 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3808 #ifdef HARD_MALLOC_TESTS
3809 void *ret
= malloc(size
+ 8);
3812 ret
= (void*)((caddr_t
)ret
+ 8);
3815 void *ret
= malloc(size
);
3820 memset(ret
, 0xff, size
);
3827 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *context
)
3830 #ifdef HARD_MALLOC_TESTS
3831 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3832 void *nmem
= realloc(real_ptr
, size
+ 8);
3837 ret
= (void*)((caddr_t
)nmem
+ 8);
3843 return realloc(mem
, size
);
3848 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *context
)
3851 #ifdef HARD_MALLOC_TESTS
3852 void *mem
= my_malloc(ptr
, nelem
* size
);
3855 memset(mem
, 0, nelem
* size
);
3861 return calloc(nelem
, size
);
3866 static test_return_t
set_prefix(memcached_st
*memc
)
3868 memcached_return_t rc
;
3869 const char *key
= "mine";
3872 /* Make sure be default none exists */
3873 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3874 test_true(rc
== MEMCACHED_FAILURE
);
3876 /* Test a clean set */
3877 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3878 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3880 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3882 test_true(memcmp(value
, key
, 4) == 0);
3883 test_true(rc
== MEMCACHED_SUCCESS
);
3885 /* Test that we can turn it off */
3886 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3887 test_true(rc
== MEMCACHED_SUCCESS
);
3889 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3891 test_true(rc
== MEMCACHED_FAILURE
);
3893 /* Now setup for main test */
3894 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3895 test_true(rc
== MEMCACHED_SUCCESS
);
3897 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3899 test_true(rc
== MEMCACHED_SUCCESS
);
3900 test_true(memcmp(value
, key
, 4) == 0);
3902 /* Set to Zero, and then Set to something too large */
3905 memset(long_key
, 0, 255);
3907 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3908 test_true(rc
== MEMCACHED_SUCCESS
);
3910 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3912 test_true(rc
== MEMCACHED_FAILURE
);
3913 test_true(value
== NULL
);
3915 /* Test a long key for failure */
3916 /* TODO, extend test to determine based on setting, what result should be */
3917 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3918 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3919 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3920 test_true(rc
== MEMCACHED_SUCCESS
);
3922 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3923 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3924 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3925 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3927 /* Test for a bad prefix, but with a short key */
3928 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3929 test_true(rc
== MEMCACHED_SUCCESS
);
3931 strncpy(long_key
, "dog cat", sizeof(long_key
));
3932 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3933 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3936 return TEST_SUCCESS
;
3940 #ifdef MEMCACHED_ENABLE_DEPRECATED
3941 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3943 void *test_ptr
= NULL
;
3946 memcached_malloc_fn malloc_cb
=
3947 (memcached_malloc_fn
)my_malloc
;
3948 cb_ptr
= *(void **)&malloc_cb
;
3949 memcached_return_t rc
;
3951 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3952 test_true(rc
== MEMCACHED_SUCCESS
);
3953 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3954 test_true(rc
== MEMCACHED_SUCCESS
);
3955 test_true(test_ptr
== cb_ptr
);
3959 memcached_realloc_fn realloc_cb
=
3960 (memcached_realloc_fn
)my_realloc
;
3961 cb_ptr
= *(void **)&realloc_cb
;
3962 memcached_return_t rc
;
3964 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3965 test_true(rc
== MEMCACHED_SUCCESS
);
3966 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3967 test_true(rc
== MEMCACHED_SUCCESS
);
3968 test_true(test_ptr
== cb_ptr
);
3972 memcached_free_fn free_cb
=
3973 (memcached_free_fn
)my_free
;
3974 cb_ptr
= *(void **)&free_cb
;
3975 memcached_return_t rc
;
3977 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3978 test_true(rc
== MEMCACHED_SUCCESS
);
3979 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3980 test_true(rc
== MEMCACHED_SUCCESS
);
3981 test_true(test_ptr
== cb_ptr
);
3984 return TEST_SUCCESS
;
3989 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3991 memcached_return_t rc
;
3992 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3993 my_realloc
, my_calloc
, NULL
);
3994 test_true(rc
== MEMCACHED_FAILURE
);
3996 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3997 my_realloc
, my_calloc
, NULL
);
3999 memcached_malloc_fn mem_malloc
;
4000 memcached_free_fn mem_free
;
4001 memcached_realloc_fn mem_realloc
;
4002 memcached_calloc_fn mem_calloc
;
4003 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4004 &mem_realloc
, &mem_calloc
);
4006 test_true(mem_malloc
== my_malloc
);
4007 test_true(mem_realloc
== my_realloc
);
4008 test_true(mem_calloc
== my_calloc
);
4009 test_true(mem_free
== my_free
);
4011 return TEST_SUCCESS
;
4014 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4017 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4018 memcached_hash_t hash
;
4019 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4020 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4023 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4024 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4026 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4028 if (hash
!= MEMCACHED_HASH_CRC
)
4029 return TEST_SKIPPED
;
4031 return TEST_SUCCESS
;
4034 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4037 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4038 memcached_hash_t hash
;
4039 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4040 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4043 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4044 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4046 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4048 if (hash
!= MEMCACHED_HASH_HSIEH
)
4049 return TEST_SKIPPED
;
4052 return TEST_SUCCESS
;
4055 static test_return_t
enable_cas(memcached_st
*memc
)
4057 unsigned int set
= 1;
4059 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4061 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4063 return TEST_SUCCESS
;
4066 return TEST_SKIPPED
;
4069 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4071 memcached_version(memc
);
4073 memcached_server_instance_st instance
=
4074 memcached_server_instance_by_position(memc
, 0);
4076 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4077 || instance
->minor_version
> 2)
4079 return TEST_SUCCESS
;
4082 return TEST_SKIPPED
;
4085 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4087 memcached_return_t rc
;
4090 memcached_servers_reset(memc
);
4092 if (stat("/tmp/memcached.socket", &buf
))
4093 return TEST_SKIPPED
;
4095 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4097 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4100 static test_return_t
pre_nodelay(memcached_st
*memc
)
4102 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4103 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4105 return TEST_SUCCESS
;
4108 static test_return_t
pre_settimer(memcached_st
*memc
)
4110 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4111 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4113 return TEST_SUCCESS
;
4116 static test_return_t
poll_timeout(memcached_st
*memc
)
4122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4124 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4126 test_true(timeout
== 100);
4128 return TEST_SUCCESS
;
4131 static test_return_t
noreply_test(memcached_st
*memc
)
4133 memcached_return_t ret
;
4134 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4135 test_true(ret
== MEMCACHED_SUCCESS
);
4136 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4137 test_true(ret
== MEMCACHED_SUCCESS
);
4138 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4139 test_true(ret
== MEMCACHED_SUCCESS
);
4140 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4141 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4142 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4144 for (int count
=0; count
< 5; ++count
)
4146 for (size_t x
= 0; x
< 100; ++x
)
4149 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4150 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4152 size_t len
= (size_t)check_length
;
4157 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4160 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4163 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4166 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4169 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4175 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4179 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4180 ** API and is _ONLY_ done this way to verify that the library works the
4181 ** way it is supposed to do!!!!
4184 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4186 memcached_server_instance_st instance
=
4187 memcached_server_instance_by_position(memc
, x
);
4188 no_msg
+=(int)(instance
->cursor_active
);
4191 test_true(no_msg
== 0);
4192 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4195 ** Now validate that all items was set properly!
4197 for (size_t x
= 0; x
< 100; ++x
)
4201 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4203 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4205 size_t len
= (size_t)check_length
;
4208 char* value
=memcached_get(memc
, key
, strlen(key
),
4209 &length
, &flags
, &ret
);
4210 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4213 case 0: /* FALLTHROUGH */
4214 case 1: /* FALLTHROUGH */
4216 test_true(strncmp(value
, key
, len
) == 0);
4217 test_true(len
== length
);
4220 test_true(length
== len
* 2);
4223 test_true(length
== len
* 3);
4233 /* Try setting an illegal cas value (should not return an error to
4234 * the caller (because we don't expect a return message from the server)
4236 const char* keys
[]= {"0"};
4237 size_t lengths
[]= {1};
4240 memcached_result_st results_obj
;
4241 memcached_result_st
*results
;
4242 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4243 test_true(ret
== MEMCACHED_SUCCESS
);
4245 results
= memcached_result_create(memc
, &results_obj
);
4247 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4249 test_true(ret
== MEMCACHED_SUCCESS
);
4250 uint64_t cas
= memcached_result_cas(results
);
4251 memcached_result_free(&results_obj
);
4253 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4254 test_true(ret
== MEMCACHED_SUCCESS
);
4257 * The item will have a new cas value, so try to set it again with the old
4258 * value. This should fail!
4260 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4261 test_true(ret
== MEMCACHED_SUCCESS
);
4262 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4263 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4264 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4267 return TEST_SUCCESS
;
4270 static test_return_t
analyzer_test(memcached_st
*memc
)
4272 memcached_return_t rc
;
4273 memcached_stat_st
*memc_stat
;
4274 memcached_analysis_st
*report
;
4276 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4277 test_true(rc
== MEMCACHED_SUCCESS
);
4278 test_true(memc_stat
);
4280 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4281 test_true(rc
== MEMCACHED_SUCCESS
);
4285 memcached_stat_free(NULL
, memc_stat
);
4287 return TEST_SUCCESS
;
4290 /* Count the objects */
4291 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4296 (void)ptr
; (void)key
; (void)key_length
;
4297 size_t *counter
= (size_t *)context
;
4299 *counter
= *counter
+ 1;
4301 return MEMCACHED_SUCCESS
;
4304 static test_return_t
dump_test(memcached_st
*memc
)
4306 memcached_return_t rc
;
4308 memcached_dump_fn callbacks
[1];
4309 test_return_t main_rc
;
4311 callbacks
[0]= &callback_dump_counter
;
4313 /* No support for Binary protocol yet */
4314 if (memc
->flags
.binary_protocol
)
4315 return TEST_SUCCESS
;
4317 main_rc
= set_test3(memc
);
4319 test_true (main_rc
== TEST_SUCCESS
);
4321 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4322 test_true(rc
== MEMCACHED_SUCCESS
);
4324 /* We may have more then 32 if our previous flush has not completed */
4325 test_true(counter
>= 32);
4327 return TEST_SUCCESS
;
4330 #ifdef HAVE_LIBMEMCACHEDUTIL
4331 static void* connection_release(void *arg
)
4334 memcached_pool_st
* pool
;
4339 // Release all of the memc we are holding
4340 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4344 #define POOL_SIZE 10
4345 static test_return_t
connection_pool_test(memcached_st
*memc
)
4347 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4348 test_true(pool
!= NULL
);
4349 memcached_st
*mmc
[POOL_SIZE
];
4350 memcached_return_t rc
;
4352 // Fill up our array that we will store the memc that are in the pool
4353 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4355 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4356 test_true(mmc
[x
] != NULL
);
4357 test_true(rc
== MEMCACHED_SUCCESS
);
4360 // All memc should be gone
4361 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4362 test_true(rc
== MEMCACHED_SUCCESS
);
4366 memcached_pool_st
* pool
;
4368 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4370 pthread_create(&tid
, NULL
, connection_release
, &item
);
4371 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4372 test_true(rc
== MEMCACHED_SUCCESS
);
4373 pthread_join(tid
, NULL
);
4375 const char *key
= "key";
4376 size_t keylen
= strlen(key
);
4378 // verify that I can do ops with all connections
4379 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4380 test_true(rc
== MEMCACHED_SUCCESS
);
4382 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4384 uint64_t number_value
;
4385 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4386 test_true(rc
== MEMCACHED_SUCCESS
);
4387 test_true(number_value
== (x
+1));
4391 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4393 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4397 /* verify that I can set behaviors on the pool when I don't have all
4398 * of the connections in the pool. It should however be enabled
4399 * when I push the item into the pool
4401 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4402 test_true(mmc
[0] != NULL
);
4404 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4405 test_true(rc
== MEMCACHED_SUCCESS
);
4407 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4408 test_true(mmc
[1] != NULL
);
4410 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4411 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4412 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4414 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4415 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4416 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4418 test_true(memcached_pool_destroy(pool
) == memc
);
4420 return TEST_SUCCESS
;
4423 static test_return_t
util_version_test(memcached_st
*memc
)
4427 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4428 test_true(if_successful
== true);
4430 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4432 // We expect failure
4435 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4436 fprintf(stderr
, "\nDumping Server Information\n\n");
4437 memcached_server_fn callbacks
[1];
4439 callbacks
[0]= dump_server_information
;
4440 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4441 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4443 test_true(if_successful
== false);
4445 memcached_server_instance_st instance
=
4446 memcached_server_instance_by_position(memc
, 0);
4448 memcached_version(memc
);
4450 // We only use one binary when we test, so this should be just fine.
4451 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4452 test_true(if_successful
== true);
4454 if (instance
->micro_version
> 0)
4455 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4456 else if (instance
->minor_version
> 0)
4457 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4458 else if (instance
->major_version
> 0)
4459 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4461 test_true(if_successful
== true);
4463 if (instance
->micro_version
> 0)
4464 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4465 else if (instance
->minor_version
> 0)
4466 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4467 else if (instance
->major_version
> 0)
4468 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4470 test_true(if_successful
== false);
4472 return TEST_SUCCESS
;
4475 static test_return_t
ping_test(memcached_st
*memc
)
4477 memcached_return_t rc
;
4478 memcached_server_instance_st instance
=
4479 memcached_server_instance_by_position(memc
, 0);
4481 // Test both the version that returns a code, and the one that does not.
4482 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4483 memcached_server_port(instance
), NULL
));
4485 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4486 memcached_server_port(instance
), &rc
));
4488 test_true(rc
== MEMCACHED_SUCCESS
);
4490 return TEST_SUCCESS
;
4496 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4500 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4501 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4502 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4503 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4504 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4505 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4506 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4507 #ifdef HAVE_HSIEH_HASH
4508 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4510 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4511 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4512 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4514 return TEST_SUCCESS
;
4518 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4520 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4521 #ifdef HAVE_HSIEH_HASH
4522 expected_rc
= MEMCACHED_SUCCESS
;
4524 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4525 (uint64_t)MEMCACHED_HASH_HSIEH
);
4526 test_true(rc
== expected_rc
);
4528 return TEST_SUCCESS
;
4531 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4533 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4534 #ifdef HAVE_MURMUR_HASH
4535 expected_rc
= MEMCACHED_SUCCESS
;
4537 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4538 (uint64_t)MEMCACHED_HASH_MURMUR
);
4539 test_true(rc
== expected_rc
);
4541 return TEST_SUCCESS
;
4544 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4550 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4554 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4555 test_true(one_at_a_time_values
[x
] == hash_val
);
4558 return TEST_SUCCESS
;
4561 static test_return_t
md5_run (memcached_st
*memc
)
4567 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4571 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4572 test_true(md5_values
[x
] == hash_val
);
4575 return TEST_SUCCESS
;
4578 static test_return_t
crc_run (memcached_st
*memc
)
4584 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4588 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4589 test_true(crc_values
[x
] == hash_val
);
4592 return TEST_SUCCESS
;
4595 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4601 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4605 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4606 test_true(fnv1_64_values
[x
] == hash_val
);
4609 return TEST_SUCCESS
;
4612 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4618 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4622 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4623 test_true(fnv1a_64_values
[x
] == hash_val
);
4626 return TEST_SUCCESS
;
4629 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4635 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4639 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4640 test_true(fnv1_32_values
[x
] == hash_val
);
4643 return TEST_SUCCESS
;
4646 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4652 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4656 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4657 test_true(fnv1a_32_values
[x
] == hash_val
);
4660 return TEST_SUCCESS
;
4663 static test_return_t
hsieh_run (memcached_st
*memc
)
4669 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4673 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4674 test_true(hsieh_values
[x
] == hash_val
);
4677 return TEST_SUCCESS
;
4680 static test_return_t
murmur_run (memcached_st
*memc
)
4682 #ifdef WORDS_BIGENDIAN
4683 (void)murmur_values
;
4684 return TEST_SKIPPED
;
4690 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4694 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4695 test_true(murmur_values
[x
] == hash_val
);
4698 return TEST_SUCCESS
;
4702 static test_return_t
jenkins_run (memcached_st
*memc
)
4708 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4712 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4713 test_true(jenkins_values
[x
] == hash_val
);
4716 return TEST_SUCCESS
;
4719 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4722 return libhashkit_md5(string
, string_length
);
4725 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4728 return libhashkit_crc32(string
, string_length
);
4731 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4735 const hashkit_st
*kit
;
4737 hashkit_return_t hash_rc
;
4739 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};
4740 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};
4742 kit
= memcached_get_hashkit(memc
);
4744 hashkit_clone(&new_kit
, kit
);
4745 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4746 test_true(hash_rc
== HASHKIT_SUCCESS
);
4748 memcached_set_hashkit(memc
, &new_kit
);
4751 Verify Setting the hash.
4753 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4757 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4758 test_true(md5_values
[x
] == hash_val
);
4763 Now check memcached_st.
4765 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4769 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4770 test_true(md5_hosts
[x
] == hash_val
);
4773 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4774 test_true(hash_rc
== HASHKIT_SUCCESS
);
4776 memcached_set_hashkit(memc
, &new_kit
);
4779 Verify Setting the hash.
4781 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4785 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4786 test_true(crc_values
[x
] == hash_val
);
4789 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4793 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4794 test_true(crc_hosts
[x
] == hash_val
);
4797 return TEST_SUCCESS
;
4801 Test case adapted from John Gorman <johngorman2@gmail.com>
4803 We are testing the error condition when we connect to a server via memcached_get()
4804 but find that the server is not available.
4806 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4809 memcached_st
*tl_memc_h
;
4810 memcached_server_st
*servers
;
4812 const char *key
= "MemcachedLives";
4815 memcached_return rc
;
4819 tl_memc_h
= memcached_create(NULL
);
4820 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4821 memcached_server_push(tl_memc_h
, servers
);
4822 memcached_server_list_free(servers
);
4824 // See if memcached is reachable.
4825 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4828 test_true(len
== 0);
4829 test_false(rc
== MEMCACHED_SUCCESS
);
4831 memcached_free(tl_memc_h
);
4833 return TEST_SUCCESS
;
4837 We connect to a server which exists, but search for a key that does not exist.
4839 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4841 const char *key
= "MemcachedKeyNotEXIST";
4844 memcached_return rc
;
4847 // See if memcached is reachable.
4848 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4851 test_true(len
== 0);
4852 test_true(rc
== MEMCACHED_NOTFOUND
);
4854 return TEST_SUCCESS
;
4858 Test case adapted from John Gorman <johngorman2@gmail.com>
4860 We are testing the error condition when we connect to a server via memcached_get_by_key()
4861 but find that the server is not available.
4863 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4866 memcached_st
*tl_memc_h
;
4867 memcached_server_st
*servers
;
4869 const char *key
= "MemcachedLives";
4872 memcached_return rc
;
4876 tl_memc_h
= memcached_create(NULL
);
4877 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4878 memcached_server_push(tl_memc_h
, servers
);
4879 memcached_server_list_free(servers
);
4881 // See if memcached is reachable.
4882 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4885 test_true(len
== 0);
4886 test_false(rc
== MEMCACHED_SUCCESS
);
4888 memcached_free(tl_memc_h
);
4890 return TEST_SUCCESS
;
4894 We connect to a server which exists, but search for a key that does not exist.
4896 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4898 const char *key
= "MemcachedKeyNotEXIST";
4901 memcached_return rc
;
4904 // See if memcached is reachable.
4905 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4908 test_true(len
== 0);
4909 test_true(rc
== MEMCACHED_NOTFOUND
);
4911 return TEST_SUCCESS
;
4915 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4917 memcached_return_t rc
;
4920 memcached_server_st
*server_pool
;
4925 memc
= memcached_create(NULL
);
4928 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4929 test_true(rc
== MEMCACHED_SUCCESS
);
4931 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4932 test_true(value
== 1);
4934 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4935 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4938 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");
4939 memcached_server_push(memc
, server_pool
);
4941 /* verify that the server list was parsed okay. */
4942 test_true(memcached_server_count(memc
) == 8);
4943 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4944 test_true(server_pool
[0].port
== 11211);
4945 test_true(server_pool
[0].weight
== 600);
4946 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4947 test_true(server_pool
[2].port
== 11211);
4948 test_true(server_pool
[2].weight
== 200);
4949 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4950 test_true(server_pool
[7].port
== 11211);
4951 test_true(server_pool
[7].weight
== 100);
4953 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4954 * us test the boundary wraparound.
4956 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4958 /* verify the standard ketama set. */
4959 for (x
= 0; x
< 99; x
++)
4961 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4962 memcached_server_instance_st instance
=
4963 memcached_server_instance_by_position(memc
, server_idx
);
4964 const char *hostname
= memcached_server_name(instance
);
4966 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4969 memcached_server_list_free(server_pool
);
4970 memcached_free(memc
);
4972 return TEST_SUCCESS
;
4975 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4977 memcached_return_t rc
;
4979 memcached_server_st
*server_pool
;
4984 memc
= memcached_create(NULL
);
4987 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4988 test_true(rc
== MEMCACHED_SUCCESS
);
4990 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4991 test_true(value
== 1);
4993 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4994 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4996 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");
4997 assert(server_pool
);
4998 memcached_server_push(memc
, server_pool
);
5000 /* verify that the server list was parsed okay. */
5001 test_true(memcached_server_count(memc
) == 8);
5002 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5003 test_true(server_pool
[0].port
== 11211);
5004 test_true(server_pool
[0].weight
== 600);
5005 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5006 test_true(server_pool
[2].port
== 11211);
5007 test_true(server_pool
[2].weight
== 200);
5008 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5009 test_true(server_pool
[7].port
== 11211);
5010 test_true(server_pool
[7].weight
== 100);
5012 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5013 * us test the boundary wraparound.
5015 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
5017 /* verify the standard ketama set. */
5018 for (uint32_t x
= 0; x
< 99; x
++)
5020 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5022 memcached_server_instance_st instance
=
5023 memcached_server_instance_by_position(memc
, server_idx
);
5025 const char *hostname
= memcached_server_name(instance
);
5027 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5030 memcached_server_list_free(server_pool
);
5031 memcached_free(memc
);
5033 return TEST_SUCCESS
;
5036 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5038 test_return_t test_rc
;
5039 test_rc
= pre_binary(memc
);
5041 if (test_rc
!= TEST_SUCCESS
)
5044 memcached_return_t ret
;
5045 const char *key
= "regression_bug_434484";
5046 size_t keylen
= strlen(key
);
5048 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5049 test_true(ret
== MEMCACHED_NOTSTORED
);
5051 size_t size
= 2048 * 1024;
5052 void *data
= calloc(1, size
);
5053 test_true(data
!= NULL
);
5054 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5055 test_true(ret
== MEMCACHED_E2BIG
);
5058 return TEST_SUCCESS
;
5061 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5063 test_return_t test_rc
;
5064 test_rc
= pre_binary(memc
);
5066 if (test_rc
!= TEST_SUCCESS
)
5069 memcached_return_t rc
;
5071 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5074 * I only want to hit only _one_ server so I know the number of requests I'm
5075 * sending in the pipleine to the server. Let's try to do a multiget of
5076 * 1024 (that should satisfy most users don't you think?). Future versions
5077 * will include a mget_execute function call if you need a higher number.
5079 uint32_t number_of_hosts
= memcached_server_count(memc
);
5080 memc
->number_of_hosts
= 1;
5081 const size_t max_keys
= 1024;
5082 char **keys
= calloc(max_keys
, sizeof(char*));
5083 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5085 for (size_t x
= 0; x
< max_keys
; ++x
)
5089 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5091 test_true(keys
[x
] != NULL
);
5095 * Run two times.. the first time we should have 100% cache miss,
5096 * and the second time we should have 100% cache hits
5098 for (size_t y
= 0; y
< 2; y
++)
5100 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5101 test_true(rc
== MEMCACHED_SUCCESS
);
5102 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5106 /* The first iteration should give me a 100% cache miss. verify that*/
5107 char blob
[1024]= { 0 };
5109 test_true(counter
== 0);
5111 for (size_t x
= 0; x
< max_keys
; ++x
)
5113 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5114 blob
, sizeof(blob
), 0, 0);
5115 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5120 /* Verify that we received all of the key/value pairs */
5121 test_true(counter
== max_keys
);
5125 /* Release allocated resources */
5126 for (size_t x
= 0; x
< max_keys
; ++x
)
5133 memc
->number_of_hosts
= number_of_hosts
;
5135 return TEST_SUCCESS
;
5138 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5140 memcached_return_t rc
;
5141 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5142 test_true(rc
== MEMCACHED_SUCCESS
);
5144 return regression_bug_434843(memc
);
5147 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5149 memcached_return_t rc
;
5150 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5151 test_true(rc
== MEMCACHED_SUCCESS
);
5153 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5154 test_true(rc
== MEMCACHED_SUCCESS
);
5155 test_true(bytes
!= NULL
);
5156 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5158 test_true(rc
== MEMCACHED_SUCCESS
);
5159 test_true(bytes_read
!= NULL
);
5161 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5162 "bytes_written", &rc
);
5163 test_true(rc
== MEMCACHED_SUCCESS
);
5164 test_true(bytes_written
!= NULL
);
5166 test_true(strcmp(bytes
, bytes_read
) != 0);
5167 test_true(strcmp(bytes
, bytes_written
) != 0);
5169 /* Release allocated resources */
5172 free(bytes_written
);
5173 memcached_stat_free(NULL
, memc_stat
);
5175 return TEST_SUCCESS
;
5179 * The test case isn't obvious so I should probably document why
5180 * it works the way it does. Bug 442914 was caused by a bug
5181 * in the logic in memcached_purge (it did not handle the case
5182 * where the number of bytes sent was equal to the watermark).
5183 * In this test case, create messages so that we hit that case
5184 * and then disable noreply mode and issue a new command to
5185 * verify that it isn't stuck. If we change the format for the
5186 * delete command or the watermarks, we need to update this
5189 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5191 memcached_return_t rc
;
5192 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5193 test_true(rc
== MEMCACHED_SUCCESS
);
5194 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5196 uint32_t number_of_hosts
= memcached_server_count(memc
);
5197 memc
->number_of_hosts
= 1;
5202 for (uint32_t x
= 0; x
< 250; ++x
)
5204 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5205 rc
= memcached_delete(memc
, k
, len
, 0);
5206 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5209 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5212 rc
= memcached_delete(memc
, k
, len
, 0);
5213 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5215 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5216 test_true(rc
== MEMCACHED_SUCCESS
);
5217 rc
= memcached_delete(memc
, k
, len
, 0);
5218 test_true(rc
== MEMCACHED_NOTFOUND
);
5220 memc
->number_of_hosts
= number_of_hosts
;
5222 return TEST_SUCCESS
;
5225 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5227 memcached_server_instance_st instance_one
;
5228 memcached_server_instance_st instance_two
;
5230 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5231 return TEST_SKIPPED
;
5233 memcached_return_t rc
;
5235 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5236 test_true(rc
== MEMCACHED_SUCCESS
);
5238 const size_t max_keys
= 100;
5239 char **keys
= calloc(max_keys
, sizeof(char*));
5240 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5242 for (size_t x
= 0; x
< max_keys
; ++x
)
5246 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5248 test_true(keys
[x
] != NULL
);
5249 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5250 test_true(rc
== MEMCACHED_SUCCESS
);
5254 ** We are using the quiet commands to store the replicas, so we need
5255 ** to ensure that all of them are processed before we can continue.
5256 ** In the test we go directly from storing the object to trying to
5257 ** receive the object from all of the different servers, so we
5258 ** could end up in a race condition (the memcached server hasn't yet
5259 ** processed the quiet command from the replication set when it process
5260 ** the request from the other client (created by the clone)). As a
5261 ** workaround for that we call memcached_quit to send the quit command
5262 ** to the server and wait for the response ;-) If you use the test code
5263 ** as an example for your own code, please note that you shouldn't need
5266 memcached_quit(memc
);
5268 /* Verify that all messages are stored, and we didn't stuff too much
5271 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5272 test_true(rc
== MEMCACHED_SUCCESS
);
5275 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5276 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5277 /* Verify that we received all of the key/value pairs */
5278 test_true(counter
== max_keys
);
5280 memcached_quit(memc
);
5282 * Don't do the following in your code. I am abusing the internal details
5283 * within the library, and this is not a supported interface.
5284 * This is to verify correct behavior in the library. Fake that two servers
5287 instance_one
= memcached_server_instance_by_position(memc
, 0);
5288 instance_two
= memcached_server_instance_by_position(memc
, 2);
5289 in_port_t port0
= instance_one
->port
;
5290 in_port_t port2
= instance_two
->port
;
5292 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5293 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5295 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5296 test_true(rc
== MEMCACHED_SUCCESS
);
5299 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5300 test_true(counter
== (unsigned int)max_keys
);
5302 /* restore the memc handle */
5303 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5304 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5306 memcached_quit(memc
);
5308 /* Remove half of the objects */
5309 for (size_t x
= 0; x
< max_keys
; ++x
)
5313 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5314 test_true(rc
== MEMCACHED_SUCCESS
);
5318 memcached_quit(memc
);
5319 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5320 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5322 /* now retry the command, this time we should have cache misses */
5323 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5324 test_true(rc
== MEMCACHED_SUCCESS
);
5327 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5328 test_true(counter
== (unsigned int)(max_keys
>> 1));
5330 /* Release allocated resources */
5331 for (size_t x
= 0; x
< max_keys
; ++x
)
5338 /* restore the memc handle */
5339 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5340 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5342 return TEST_SUCCESS
;
5345 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5347 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5348 test_true(memc_clone
!= NULL
);
5349 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5351 memcached_server_instance_st instance
=
5352 memcached_server_instance_by_position(memc_clone
, 0);
5354 if (instance
->major_version
> 1 ||
5355 (instance
->major_version
== 1 &&
5356 instance
->minor_version
> 2))
5358 /* Binary protocol doesn't support deferred delete */
5359 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5360 test_true(bin_clone
!= NULL
);
5361 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5362 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5363 memcached_free(bin_clone
);
5365 memcached_quit(memc_clone
);
5367 /* If we know the server version, deferred delete should fail
5368 * with invalid arguments */
5369 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5371 /* If we don't know the server version, we should get a protocol error */
5372 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5374 /* but there is a bug in some of the memcached servers (1.4) that treats
5375 * the counter as noreply so it doesn't send the proper error message
5377 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5379 /* And buffered mode should be disabled and we should get protocol error */
5380 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5381 rc
= memcached_delete(memc
, "foo", 3, 1);
5382 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5384 /* Same goes for noreply... */
5385 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5386 rc
= memcached_delete(memc
, "foo", 3, 1);
5387 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5389 /* but a normal request should go through (and be buffered) */
5390 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5391 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5393 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5394 /* unbuffered noreply should be success */
5395 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5396 /* unbuffered with reply should be not found... */
5397 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5398 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5401 memcached_free(memc_clone
);
5402 return TEST_SUCCESS
;
5406 /* Test memcached_server_get_last_disconnect
5407 * For a working server set, shall be NULL
5408 * For a set of non existing server, shall not be NULL
5410 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5412 memcached_return_t rc
;
5413 memcached_server_instance_st disconnected_server
;
5415 /* With the working set of server */
5416 const char *key
= "marmotte";
5417 const char *value
= "milka";
5419 memcached_reset_last_disconnected_server(memc
);
5420 rc
= memcached_set(memc
, key
, strlen(key
),
5421 value
, strlen(value
),
5422 (time_t)0, (uint32_t)0);
5423 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5425 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5426 test_true(disconnected_server
== NULL
);
5428 /* With a non existing server */
5430 memcached_server_st
*servers
;
5432 const char *server_list
= "localhost:9";
5434 servers
= memcached_servers_parse(server_list
);
5436 mine
= memcached_create(NULL
);
5437 rc
= memcached_server_push(mine
, servers
);
5438 test_true(rc
== MEMCACHED_SUCCESS
);
5439 memcached_server_list_free(servers
);
5442 rc
= memcached_set(mine
, key
, strlen(key
),
5443 value
, strlen(value
),
5444 (time_t)0, (uint32_t)0);
5445 test_true(rc
!= MEMCACHED_SUCCESS
);
5447 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5448 if (disconnected_server
== NULL
)
5450 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5453 test_true(disconnected_server
!= NULL
);
5454 test_true(memcached_server_port(disconnected_server
)== 9);
5455 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5457 memcached_quit(mine
);
5458 memcached_free(mine
);
5460 return TEST_SUCCESS
;
5463 static test_return_t
test_verbosity(memcached_st
*memc
)
5465 memcached_verbosity(memc
, 3);
5467 return TEST_SUCCESS
;
5470 static test_return_t
test_server_failure(memcached_st
*memc
)
5472 memcached_st
*local_memc
;
5473 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5475 local_memc
= memcached_create(NULL
);
5477 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5478 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5480 uint32_t server_count
= memcached_server_count(local_memc
);
5482 test_true(server_count
== 1);
5484 // Disable the server
5485 instance
= memcached_server_instance_by_position(local_memc
, 0);
5486 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5488 memcached_return_t rc
;
5489 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5491 (time_t)0, (uint32_t)0);
5492 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5494 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5495 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5497 (time_t)0, (uint32_t)0);
5498 test_true(rc
== MEMCACHED_SUCCESS
);
5501 memcached_free(local_memc
);
5503 return TEST_SUCCESS
;
5506 static test_return_t
test_cull_servers(memcached_st
*memc
)
5508 uint32_t count
= memcached_server_count(memc
);
5510 // Do not do this in your code, it is not supported.
5511 memc
->servers
[1].options
.is_dead
= true;
5512 memc
->state
.is_time_for_rebuild
= true;
5514 uint32_t new_count
= memcached_server_count(memc
);
5515 test_true(count
== new_count
);
5518 test_true(count
== new_count
+ 1 );
5521 return TEST_SUCCESS
;
5525 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5526 const char *key
, size_t key_length
,
5527 const char *value
, size_t value_length
,
5537 return MEMCACHED_SUCCESS
;
5540 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5542 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5543 test_true(rc
== MEMCACHED_SUCCESS
);
5545 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5546 test_true(rc
== MEMCACHED_SUCCESS
);
5548 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5549 test_true(rc
== MEMCACHED_SUCCESS
);
5551 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5552 test_true(rc
== MEMCACHED_SUCCESS
);
5554 return TEST_SUCCESS
;
5558 * This test ensures that the failure counter isn't incremented during
5559 * normal termination of the memcached instance.
5561 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5563 memcached_return_t rc
;
5564 memcached_server_instance_st instance
;
5566 /* Set value to force connection to the server */
5567 const char *key
= "marmotte";
5568 const char *value
= "milka";
5571 * Please note that I'm abusing the internal structures in libmemcached
5572 * in a non-portable way and you shouldn't be doing this. I'm only
5573 * doing this in order to verify that the library works the way it should
5575 uint32_t number_of_hosts
= memcached_server_count(memc
);
5576 memc
->number_of_hosts
= 1;
5578 /* Ensure that we are connected to the server by setting a value */
5579 rc
= memcached_set(memc
, key
, strlen(key
),
5580 value
, strlen(value
),
5581 (time_t)0, (uint32_t)0);
5582 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5585 instance
= memcached_server_instance_by_position(memc
, 0);
5586 /* The test is to see that the memcached_quit doesn't increase the
5587 * the server failure conter, so let's ensure that it is zero
5588 * before sending quit
5590 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5592 memcached_quit(memc
);
5594 /* Verify that it memcached_quit didn't increment the failure counter
5595 * Please note that this isn't bullet proof, because an error could
5598 test_true(instance
->server_failure_counter
== 0);
5600 /* restore the instance */
5601 memc
->number_of_hosts
= number_of_hosts
;
5603 return TEST_SUCCESS
;
5607 * This tests ensures expected disconnections (for some behavior changes
5608 * for instance) do not wrongly increase failure counter
5610 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5612 memcached_return rc
;
5614 memcached_st
*memc_clone
;
5615 memc_clone
= memcached_clone(NULL
, memc
);
5616 test_true(memc_clone
);
5618 /* Set value to force connection to the server */
5619 const char *key
= "marmotte";
5620 const char *value
= "milka";
5621 char *string
= NULL
;
5622 size_t string_length
;
5625 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5626 value
, strlen(value
),
5627 (time_t)0, (uint32_t)0);
5628 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5631 /* put failure limit to 1 */
5632 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5633 assert(rc
== MEMCACHED_SUCCESS
);
5635 /* Put a retry timeout to effectively activate failure_limit effect */
5636 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5637 assert(rc
== MEMCACHED_SUCCESS
);
5639 /* change behavior that triggers memcached_quit()*/
5640 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5641 assert(rc
== MEMCACHED_SUCCESS
);
5644 /* Check if we still are connected */
5645 string
= memcached_get(memc_clone
, key
, strlen(key
),
5646 &string_length
, &flags
, &rc
);
5648 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5651 memcached_free(memc_clone
);
5653 return TEST_SUCCESS
;
5660 * Test that ensures mget_execute does not end into recursive calls that finally fails
5662 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5664 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5665 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5666 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5667 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5668 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5671 return TEST_SKIPPED
; // My MAC can't handle this test
5675 * I only want to hit _one_ server so I know the number of requests I'm
5676 * sending in the pipeline.
5678 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5679 memc
->number_of_hosts
= 1;
5680 size_t max_keys
= 20480;
5683 char **keys
= calloc(max_keys
, sizeof(char*));
5684 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5686 /* First add all of the items.. */
5688 char blob
[1024]= { 0 };
5689 memcached_return rc
;
5690 for (size_t x
= 0; x
< max_keys
; ++x
)
5693 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5695 assert(keys
[x
] != NULL
);
5696 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5698 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5700 break; // We are out of business
5703 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5705 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5708 sleep(1);// We will try to sleep
5711 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5713 // We failed to send everything.
5718 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5721 /* Try to get all of them with a large multiget */
5723 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5724 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5725 (size_t)max_keys
, callbacks
, &counter
, 1);
5727 assert(rc
== MEMCACHED_SUCCESS
);
5728 char* the_value
= NULL
;
5729 char the_key
[MEMCACHED_MAX_KEY
];
5730 size_t the_key_length
;
5731 size_t the_value_length
;
5735 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5737 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5743 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5746 assert(rc
== MEMCACHED_END
);
5748 /* Verify that we got all of the items */
5749 assert(counter
== max_keys
);
5752 /* Release all allocated resources */
5753 for (size_t x
= 0; x
< max_keys
; ++x
)
5760 memc
->number_of_hosts
= number_of_hosts
;
5762 return TEST_SUCCESS
;
5765 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5769 memcached_st
*memc
= memcached_create(NULL
);
5771 memcached_server_add(memc
, "10.2.3.4", 11211);
5773 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5774 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5775 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5776 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5777 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5778 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5780 memcached_return_t rc
;
5784 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5786 test_true_got(rc
== MEMCACHED_TIMEOUT
|| rc
== MEMCACHED_ERRNO
|| rc
== MEMCACHED_FAILURE
, memcached_strerror(memc
, rc
));
5788 memcached_free(memc
);
5790 return TEST_SUCCESS
;
5793 static test_return_t
regression_bug_581030(memcached_st
*unused
)
5798 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5799 test_false(local_stat
);
5801 memcached_stat_free(NULL
, NULL
);
5804 return TEST_SUCCESS
;
5807 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5809 fprintf(stderr
, "Iteration #%u: ", it
);
5811 if(error
== MEMCACHED_ERRNO
)
5813 fprintf(stderr
, "system error %d from %s: %s\n",
5814 errno
, what
, strerror(errno
));
5818 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5819 memcached_strerror(mc
, error
));
5823 #define TEST_CONSTANT_CREATION 200
5825 static test_return_t
regression_bug_(memcached_st
*memc
)
5827 const char *remote_server
;
5830 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5832 return TEST_SKIPPED
;
5835 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5837 memcached_st
* mc
= memcached_create(NULL
);
5838 memcached_return rc
;
5840 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5841 if (rc
!= MEMCACHED_SUCCESS
)
5843 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5846 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5847 if (rc
!= MEMCACHED_SUCCESS
)
5849 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5852 rc
= memcached_server_add(mc
, remote_server
, 0);
5853 if (rc
!= MEMCACHED_SUCCESS
)
5855 memcached_die(mc
, rc
, "memcached_server_add", x
);
5858 const char *set_key
= "akey";
5859 const size_t set_key_len
= strlen(set_key
);
5860 const char *set_value
= "a value";
5861 const size_t set_value_len
= strlen(set_value
);
5863 if (rc
== MEMCACHED_SUCCESS
)
5867 size_t get_value_len
;
5869 uint32_t get_value_flags
;
5871 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5872 &get_value_flags
, &rc
);
5873 if (rc
!= MEMCACHED_SUCCESS
)
5875 memcached_die(mc
, rc
, "memcached_get", x
);
5881 (get_value_len
!= set_value_len
5882 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5884 fprintf(stderr
, "Values don't match?\n");
5885 rc
= MEMCACHED_FAILURE
;
5891 rc
= memcached_set(mc
,
5892 set_key
, set_key_len
,
5893 set_value
, set_value_len
,
5897 if (rc
!= MEMCACHED_SUCCESS
)
5899 memcached_die(mc
, rc
, "memcached_set", x
);
5906 if (rc
!= MEMCACHED_SUCCESS
)
5912 return TEST_SUCCESS
;
5916 * Test that the sasl authentication works. We cannot use the default
5917 * pool of servers, because that would require that all servers we want
5918 * to test supports SASL authentication, and that they use the default
5921 static test_return_t
sasl_auth_test(memcached_st
*memc
)
5923 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
5924 memcached_return_t rc
;
5926 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5927 test_true(rc
== MEMCACHED_SUCCESS
);
5928 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
5929 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
5930 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
5931 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
5932 memcached_quit(memc
);
5934 rc
= memcached_set_sasl_auth_data(memc
,
5935 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
5936 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
5937 test_true(rc
== MEMCACHED_SUCCESS
);
5939 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5940 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
5941 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
5943 memcached_quit(memc
);
5944 return TEST_SUCCESS
;
5947 return TEST_FAILURE
;
5951 /* Clean the server before beginning testing */
5953 {"util_version", 1, (test_callback_fn
)util_version_test
},
5954 {"flush", 0, (test_callback_fn
)flush_test
},
5955 {"init", 0, (test_callback_fn
)init_test
},
5956 {"allocation", 0, (test_callback_fn
)allocation_test
},
5957 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5958 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5959 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5960 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5961 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5962 {"clone_test", 0, (test_callback_fn
)clone_test
},
5963 {"connection_test", 0, (test_callback_fn
)connection_test
},
5964 {"callback_test", 0, (test_callback_fn
)callback_test
},
5965 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5966 {"error", 0, (test_callback_fn
)error_test
},
5967 {"set", 0, (test_callback_fn
)set_test
},
5968 {"set2", 0, (test_callback_fn
)set_test2
},
5969 {"set3", 0, (test_callback_fn
)set_test3
},
5970 {"dump", 1, (test_callback_fn
)dump_test
},
5971 {"add", 1, (test_callback_fn
)add_test
},
5972 {"replace", 1, (test_callback_fn
)replace_test
},
5973 {"delete", 1, (test_callback_fn
)delete_test
},
5974 {"get", 1, (test_callback_fn
)get_test
},
5975 {"get2", 0, (test_callback_fn
)get_test2
},
5976 {"get3", 0, (test_callback_fn
)get_test3
},
5977 {"get4", 0, (test_callback_fn
)get_test4
},
5978 {"partial mget", 0, (test_callback_fn
)get_test5
},
5979 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5980 {"increment", 0, (test_callback_fn
)increment_test
},
5981 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5982 {"decrement", 0, (test_callback_fn
)decrement_test
},
5983 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5984 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5985 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5986 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5987 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5988 {"quit", 0, (test_callback_fn
)quit_test
},
5989 {"mget", 1, (test_callback_fn
)mget_test
},
5990 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5991 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5992 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5993 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5994 {"mget_end", 0, (test_callback_fn
)mget_end
},
5995 {"get_stats", 0, (test_callback_fn
)get_stats
},
5996 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5997 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5998 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5999 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6000 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6001 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6002 {"read_through", 1, (test_callback_fn
)read_through
},
6003 {"delete_through", 1, (test_callback_fn
)delete_through
},
6004 {"noreply", 1, (test_callback_fn
)noreply_test
},
6005 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6006 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6007 {"memcached_pool_test", 1, (test_callback_fn
)memcached_pool_test
},
6008 {"ping", 1, (test_callback_fn
)ping_test
},
6009 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6010 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6011 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6012 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6013 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
6017 test_st behavior_tests
[] ={
6018 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6019 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6020 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6021 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6025 test_st basic_tests
[] ={
6026 {"init", 1, (test_callback_fn
)basic_init_test
},
6027 {"clone", 1, (test_callback_fn
)basic_clone_test
},
6028 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
6029 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
6030 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
6031 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
6035 test_st regression_binary_vs_block
[] ={
6036 {"block add", 1, (test_callback_fn
)block_add_regression
},
6037 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6041 test_st async_tests
[] ={
6042 {"add", 1, (test_callback_fn
)add_wrapper
},
6046 test_st string_tests
[] ={
6047 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6048 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6049 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6050 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6051 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6052 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6053 {"string_alloc_append_multiple", 0, (test_callback_fn
)string_alloc_append_multiple
},
6054 {0, 0, (test_callback_fn
)0}
6057 test_st result_tests
[] ={
6058 {"result static", 0, (test_callback_fn
)result_static
},
6059 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6060 {0, 0, (test_callback_fn
)0}
6063 test_st version_1_2_3
[] ={
6064 {"append", 0, (test_callback_fn
)append_test
},
6065 {"prepend", 0, (test_callback_fn
)prepend_test
},
6066 {"cas", 0, (test_callback_fn
)cas_test
},
6067 {"cas2", 0, (test_callback_fn
)cas2_test
},
6068 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6069 {0, 0, (test_callback_fn
)0}
6072 test_st user_tests
[] ={
6073 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6074 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6075 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6076 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6077 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6078 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6079 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6080 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6081 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6082 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6083 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6084 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6085 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6086 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6087 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6088 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6089 #if !defined(__sun) && !defined(__OpenBSD__)
6091 ** It seems to be something weird with the character sets..
6092 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6093 ** guess I need to find out how this is supposed to work.. Perhaps I need
6094 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6095 ** so just disable the code for now...).
6097 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6099 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6100 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6101 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6102 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6103 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6104 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6105 {0, 0, (test_callback_fn
)0}
6108 test_st replication_tests
[]= {
6109 {"set", 1, (test_callback_fn
)replication_set_test
},
6110 {"get", 0, (test_callback_fn
)replication_get_test
},
6111 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6112 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6113 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6114 {0, 0, (test_callback_fn
)0}
6118 * The following test suite is used to verify that we don't introduce
6119 * regression bugs. If you want more information about the bug / test,
6120 * you should look in the bug report at
6121 * http://bugs.launchpad.net/libmemcached
6123 test_st regression_tests
[]= {
6124 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6125 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6126 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6127 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6128 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6129 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6130 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6131 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6132 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6133 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6134 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6135 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6136 {0, 0, (test_callback_fn
)0}
6139 test_st sasl_auth_tests
[]= {
6140 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6141 {0, 0, (test_callback_fn
)0}
6144 test_st ketama_compatibility
[]= {
6145 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6146 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6147 {0, 0, (test_callback_fn
)0}
6150 test_st generate_tests
[] ={
6151 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6152 {"generate_data", 1, (test_callback_fn
)generate_data
},
6153 {"get_read", 0, (test_callback_fn
)get_read
},
6154 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6155 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6156 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6157 {"generate_data", 1, (test_callback_fn
)generate_data
},
6158 {"mget_read", 0, (test_callback_fn
)mget_read
},
6159 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6160 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6161 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6162 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6163 {"generate_data", 1, (test_callback_fn
)generate_data
},
6164 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6165 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6166 {0, 0, (test_callback_fn
)0}
6169 test_st consistent_tests
[] ={
6170 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6171 {"generate_data", 1, (test_callback_fn
)generate_data
},
6172 {"get_read", 0, (test_callback_fn
)get_read_count
},
6173 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6174 {0, 0, (test_callback_fn
)0}
6177 test_st consistent_weighted_tests
[] ={
6178 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6179 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6180 {"get_read", 0, (test_callback_fn
)get_read_count
},
6181 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6182 {0, 0, (test_callback_fn
)0}
6185 test_st hsieh_availability
[] ={
6186 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6187 {0, 0, (test_callback_fn
)0}
6190 test_st murmur_availability
[] ={
6191 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6192 {0, 0, (test_callback_fn
)0}
6196 test_st hash_sanity
[] ={
6197 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6198 {0, 0, (test_callback_fn
)0}
6202 test_st ketama_auto_eject_hosts
[] ={
6203 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6204 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6205 {0, 0, (test_callback_fn
)0}
6208 test_st hash_tests
[] ={
6209 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6210 {"md5", 0, (test_callback_fn
)md5_run
},
6211 {"crc", 0, (test_callback_fn
)crc_run
},
6212 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6213 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6214 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6215 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6216 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6217 {"murmur", 0, (test_callback_fn
)murmur_run
},
6218 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6219 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6220 {0, 0, (test_callback_fn
)0}
6223 test_st error_conditions
[] ={
6224 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6225 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6226 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6227 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6228 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6229 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6230 {0, 0, (test_callback_fn
)0}
6234 test_st parser_tests
[] ={
6235 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6236 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6237 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6238 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6239 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6240 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6241 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6242 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6243 {"randomly generated options", 0, (test_callback_fn
)random_statement_build_test
},
6244 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6245 {"server", 0, (test_callback_fn
)server_test
},
6246 {"bad server strings", 0, (test_callback_fn
)servers_bad_test
},
6247 {"server with weights", 0, (test_callback_fn
)server_with_weight_test
},
6248 {0, 0, (test_callback_fn
)0}
6251 test_st virtual_bucket_tests
[] ={
6252 {"basic", 0, (test_callback_fn
)virtual_back_map
},
6253 {0, 0, (test_callback_fn
)0}
6256 collection_st collection
[] ={
6258 {"hash_sanity", 0, 0, hash_sanity
},
6260 {"basic", 0, 0, basic_tests
},
6261 {"hsieh_availability", 0, 0, hsieh_availability
},
6262 {"murmur_availability", 0, 0, murmur_availability
},
6263 {"block", 0, 0, tests
},
6264 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6265 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6266 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6267 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6268 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6269 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6270 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6271 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6272 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6273 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6274 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6275 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6276 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6277 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6278 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6279 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6280 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6281 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6282 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6283 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6284 #ifdef MEMCACHED_ENABLE_DEPRECATED
6285 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6287 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6288 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6289 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6290 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6291 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6292 {"string", 0, 0, string_tests
},
6293 {"result", 0, 0, result_tests
},
6294 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6295 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6296 {"user", 0, 0, user_tests
},
6297 {"generate", 0, 0, generate_tests
},
6298 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6299 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6300 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6301 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6302 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6303 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6304 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6306 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6307 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6308 {"consistent_not", 0, 0, consistent_tests
},
6309 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6310 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6311 {"ketama_compat", 0, 0, ketama_compatibility
},
6312 {"test_hashes", 0, 0, hash_tests
},
6313 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6314 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6315 {"regression", 0, 0, regression_tests
},
6316 {"behaviors", 0, 0, behavior_tests
},
6317 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6318 {"error_conditions", 0, 0, error_conditions
},
6319 {"parser", 0, 0, parser_tests
},
6320 {"virtual buckets", 0, 0, virtual_bucket_tests
},
6324 #include "tests/libmemcached_world.h"
6326 void get_world(world_st
*world
)
6328 world
->collections
= collection
;
6330 world
->create
= (test_callback_create_fn
)world_create
;
6331 world
->destroy
= (test_callback_fn
)world_destroy
;
6333 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6334 world
->test
.flush
= (test_callback_fn
)world_flush
;
6335 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6336 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6337 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6339 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6340 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6342 world
->runner
= &defualt_libmemcached_runner
;