1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
21 #include <sys/types.h>
27 #include "libmemcached/common.h"
30 #include "clients/generator.h"
31 #include "clients/execute.h"
33 #define SMALL_STRING_LEN 1024
38 #ifdef HAVE_LIBMEMCACHEDUTIL
40 #include "libmemcached/memcached_util.h"
43 #include "hash_results.h"
45 #define GLOBAL_COUNT 10000
46 #define GLOBAL2_COUNT 100
47 #define SERVERS_TO_CREATE 5
48 static uint32_t global_count
;
50 static pairs_st
*global_pairs
;
51 static const char *global_keys
[GLOBAL_COUNT
];
52 static size_t global_keys_length
[GLOBAL_COUNT
];
55 static test_return_t
pre_binary(memcached_st
*memc
);
58 static test_return_t
init_test(memcached_st
*not_used
)
63 (void)memcached_create(&memc
);
64 memcached_free(&memc
);
69 static test_return_t
server_list_null_test(memcached_st
*ptr
)
71 memcached_server_st
*server_list
;
72 memcached_return_t rc
;
75 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
76 test_true(server_list
== NULL
);
78 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
79 test_true(server_list
== NULL
);
81 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
82 test_true(server_list
== NULL
);
87 #define TEST_PORT_COUNT 7
88 in_port_t test_ports
[TEST_PORT_COUNT
];
90 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
91 const memcached_server_st
*server
,
95 size_t bigger
= *((size_t *)(context
));
97 assert(bigger
<= memcached_server_port(server
));
98 *((size_t *)(context
))= memcached_server_port(server
);
100 return MEMCACHED_SUCCESS
;
103 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
104 const memcached_server_st
*instance
,
108 FILE *stream
= (FILE *)context
;
111 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
112 memcached_server_name(instance
),
113 memcached_server_port(instance
),
114 instance
->major_version
,
115 instance
->minor_version
,
116 instance
->micro_version
);
118 return MEMCACHED_SUCCESS
;
121 static test_return_t
server_sort_test(memcached_st
*ptr
)
123 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
125 memcached_return_t rc
;
126 memcached_server_fn callbacks
[1];
127 memcached_st
*local_memc
;
130 local_memc
= memcached_create(NULL
);
131 test_true(local_memc
);
132 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
134 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
136 test_ports
[x
]= (in_port_t
)random() % 64000;
137 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
138 test_true(memcached_server_count(local_memc
) == x
+ 1);
140 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
142 test_true(rc
== MEMCACHED_SUCCESS
);
145 callbacks
[0]= server_display_function
;
146 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
149 memcached_free(local_memc
);
154 static test_return_t
server_sort2_test(memcached_st
*ptr
)
156 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
157 memcached_return_t rc
;
158 memcached_server_fn callbacks
[1];
159 memcached_st
*local_memc
;
160 memcached_server_instance_st instance
;
163 local_memc
= memcached_create(NULL
);
164 test_true(local_memc
);
165 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
166 test_true(rc
== MEMCACHED_SUCCESS
);
168 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
169 test_true(rc
== MEMCACHED_SUCCESS
);
170 instance
= memcached_server_instance_by_position(local_memc
, 0);
171 test_true(memcached_server_port(instance
) == 43043);
173 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
174 test_true(rc
== MEMCACHED_SUCCESS
);
176 instance
= memcached_server_instance_by_position(local_memc
, 0);
177 test_true(memcached_server_port(instance
) == 43042);
179 instance
= memcached_server_instance_by_position(local_memc
, 1);
180 test_true(memcached_server_port(instance
) == 43043);
182 callbacks
[0]= server_display_function
;
183 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
186 memcached_free(local_memc
);
191 static memcached_return_t
server_print_callback(const memcached_st
*ptr
,
192 const memcached_server_st
*server
,
195 (void)server
; // Just in case we aren't printing.
200 fprintf(stderr
, "%s(%d)", memcached_server_name(server
), memcached_server_port(server
));
203 return MEMCACHED_SUCCESS
;
206 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
)
208 memcached_return_t rc
;
209 memcached_st local_memc
;
211 memcached_server_st
*servers
;
212 memcached_server_fn callbacks
[1];
214 const char *server_string
= "localhost:4444, localhost:4445, localhost:4446, localhost:4447, localhost, 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";
217 memc
= memcached_create(&local_memc
);
219 servers
= memcached_servers_parse(server_string
);
221 rc
= memcached_server_push(memc
, servers
);
222 memcached_server_list_free(servers
);
224 callbacks
[0]= server_print_callback
;
225 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
227 memcached_free(memc
);
232 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
,
233 const memcached_server_st
*server
,
237 uint32_t x
= *((uint32_t *)(context
));
240 assert(test_ports
[x
] == server
->port
);
241 *((uint32_t *)(context
))= ++x
;
243 return MEMCACHED_SUCCESS
;
246 static test_return_t
server_unsort_test(memcached_st
*ptr
)
248 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
249 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
250 memcached_return_t rc
;
251 memcached_server_fn callbacks
[1];
252 memcached_st
*local_memc
;
255 local_memc
= memcached_create(NULL
);
256 test_true(local_memc
);
258 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
260 test_ports
[x
]= (in_port_t
)(random() % 64000);
261 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
262 test_true(memcached_server_count(local_memc
) == x
+1);
264 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
266 test_true(rc
== MEMCACHED_SUCCESS
);
269 callbacks
[0]= server_display_unsort_function
;
270 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
272 /* Now we sort old data! */
273 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
274 callbacks
[0]= server_display_function
;
275 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
278 memcached_free(local_memc
);
283 static test_return_t
allocation_test(memcached_st
*not_used
)
287 memc
= memcached_create(NULL
);
289 memcached_free(memc
);
294 static test_return_t
clone_test(memcached_st
*memc
)
298 memcached_st
*memc_clone
;
299 memc_clone
= memcached_clone(NULL
, NULL
);
300 test_true(memc_clone
);
301 memcached_free(memc_clone
);
304 /* Can we init from null? */
306 memcached_st
*memc_clone
;
307 memc_clone
= memcached_clone(NULL
, memc
);
308 test_true(memc_clone
);
311 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
312 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
313 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
314 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
317 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
318 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
319 test_true(memc_clone
->distribution
== memc
->distribution
);
320 { // Test all of the flags
321 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
322 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
323 test_true(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
324 test_true(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
325 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
326 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
327 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
328 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
329 test_true(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
330 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
331 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
332 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
333 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
334 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
335 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
337 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
338 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
339 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
340 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
341 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
342 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
343 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
344 test_true(memc_clone
->on_clone
== memc
->on_clone
);
345 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
346 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
347 test_true(memc_clone
->recv_size
== memc
->recv_size
);
348 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
349 test_true(memc_clone
->send_size
== memc
->send_size
);
350 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
351 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
352 test_true(memc_clone
->user_data
== memc
->user_data
);
354 memcached_free(memc_clone
);
357 /* Can we init from struct? */
359 memcached_st declared_clone
;
360 memcached_st
*memc_clone
;
361 memset(&declared_clone
, 0 , sizeof(memcached_st
));
362 memc_clone
= memcached_clone(&declared_clone
, NULL
);
363 test_true(memc_clone
);
364 memcached_free(memc_clone
);
367 /* Can we init from struct? */
369 memcached_st declared_clone
;
370 memcached_st
*memc_clone
;
371 memset(&declared_clone
, 0 , sizeof(memcached_st
));
372 memc_clone
= memcached_clone(&declared_clone
, memc
);
373 test_true(memc_clone
);
374 memcached_free(memc_clone
);
380 static test_return_t
userdata_test(memcached_st
*memc
)
383 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
384 test_true(memcached_get_user_data(memc
) == foo
);
385 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
390 static test_return_t
connection_test(memcached_st
*memc
)
392 memcached_return_t rc
;
394 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
395 test_true(rc
== MEMCACHED_SUCCESS
);
400 static test_return_t
error_test(memcached_st
*memc
)
402 memcached_return_t rc
;
403 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
404 982370485U, 1263635348U, 4242906218U, 3829656100U,
405 1891735253U, 334139633U, 2257084983U, 3088286104U,
406 13199785U, 2542027183U, 1097051614U, 199566778U,
407 2748246961U, 2465192557U, 1664094137U, 2405439045U,
408 1842224848U, 692413798U, 3479807801U, 919913813U,
409 4269430871U, 610793021U, 527273862U, 1437122909U,
410 2300930706U, 2943759320U, 674306647U, 2400528935U,
411 54481931U, 4186304426U, 1741088401U, 2979625118U,
412 4159057246U, 3425930182U, 2593724503U, 1868899624U,
413 1769812374U, 2302537950U, 1110330676U };
415 // You have updated the memcache_error messages but not updated docs/tests.
416 test_true(MEMCACHED_MAXIMUM_RETURN
== 43);
417 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
420 const char *msg
= memcached_strerror(memc
, rc
);
421 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
422 MEMCACHED_HASH_JENKINS
);
423 if (values
[rc
] != hash_val
)
425 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
426 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
428 test_true(values
[rc
] == hash_val
);
434 static test_return_t
set_test(memcached_st
*memc
)
436 memcached_return_t rc
;
437 const char *key
= "foo";
438 const char *value
= "when we sanitize";
440 rc
= memcached_set(memc
, key
, strlen(key
),
441 value
, strlen(value
),
442 (time_t)0, (uint32_t)0);
443 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
448 static test_return_t
append_test(memcached_st
*memc
)
450 memcached_return_t rc
;
451 const char *key
= "fig";
452 const char *in_value
= "we";
453 char *out_value
= NULL
;
457 rc
= memcached_flush(memc
, 0);
458 test_true(rc
== MEMCACHED_SUCCESS
);
460 rc
= memcached_set(memc
, key
, strlen(key
),
461 in_value
, strlen(in_value
),
462 (time_t)0, (uint32_t)0);
463 test_true(rc
== MEMCACHED_SUCCESS
);
465 rc
= memcached_append(memc
, key
, strlen(key
),
466 " the", strlen(" the"),
467 (time_t)0, (uint32_t)0);
468 test_true(rc
== MEMCACHED_SUCCESS
);
470 rc
= memcached_append(memc
, key
, strlen(key
),
471 " people", strlen(" people"),
472 (time_t)0, (uint32_t)0);
473 test_true(rc
== MEMCACHED_SUCCESS
);
475 out_value
= memcached_get(memc
, key
, strlen(key
),
476 &value_length
, &flags
, &rc
);
477 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
478 test_true(strlen("we the people") == value_length
);
479 test_true(rc
== MEMCACHED_SUCCESS
);
485 static test_return_t
append_binary_test(memcached_st
*memc
)
487 memcached_return_t rc
;
488 const char *key
= "numbers";
489 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
495 rc
= memcached_flush(memc
, 0);
496 test_true(rc
== MEMCACHED_SUCCESS
);
498 rc
= memcached_set(memc
,
501 (time_t)0, (uint32_t)0);
502 test_true(rc
== MEMCACHED_SUCCESS
);
504 for (x
= 0; store_list
[x
] ; x
++)
506 rc
= memcached_append(memc
,
508 (char *)&store_list
[x
], sizeof(uint32_t),
509 (time_t)0, (uint32_t)0);
510 test_true(rc
== MEMCACHED_SUCCESS
);
513 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
514 &value_length
, &flags
, &rc
);
515 test_true((value_length
== (sizeof(uint32_t) * x
)));
516 test_true(rc
== MEMCACHED_SUCCESS
);
518 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
520 test_true(*ptr
== store_list
[x
- counter
]);
528 static test_return_t
cas2_test(memcached_st
*memc
)
530 memcached_return_t rc
;
531 const char *keys
[]= {"fudge", "son", "food"};
532 size_t key_length
[]= {5, 3, 4};
533 const char *value
= "we the people";
534 size_t value_length
= strlen("we the people");
536 memcached_result_st results_obj
;
537 memcached_result_st
*results
;
540 rc
= memcached_flush(memc
, 0);
541 test_true(rc
== MEMCACHED_SUCCESS
);
543 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
545 for (x
= 0; x
< 3; x
++)
547 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
548 keys
[x
], key_length
[x
],
549 (time_t)50, (uint32_t)9);
550 test_true(rc
== MEMCACHED_SUCCESS
);
553 rc
= memcached_mget(memc
, keys
, key_length
, 3);
555 results
= memcached_result_create(memc
, &results_obj
);
557 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
559 test_true(results
->item_cas
);
560 test_true(rc
== MEMCACHED_SUCCESS
);
561 test_true(memcached_result_cas(results
));
563 test_true(!memcmp(value
, "we the people", strlen("we the people")));
564 test_true(strlen("we the people") == value_length
);
565 test_true(rc
== MEMCACHED_SUCCESS
);
567 memcached_result_free(&results_obj
);
572 static test_return_t
cas_test(memcached_st
*memc
)
574 memcached_return_t rc
;
575 const char *key
= "fun";
576 size_t key_length
= strlen(key
);
577 const char *value
= "we the people";
578 const char* keys
[2] = { key
, NULL
};
579 size_t keylengths
[2] = { strlen(key
), 0 };
580 size_t value_length
= strlen(value
);
581 const char *value2
= "change the value";
582 size_t value2_length
= strlen(value2
);
584 memcached_result_st results_obj
;
585 memcached_result_st
*results
;
588 rc
= memcached_flush(memc
, 0);
589 test_true(rc
== MEMCACHED_SUCCESS
);
591 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
593 rc
= memcached_set(memc
, key
, strlen(key
),
594 value
, strlen(value
),
595 (time_t)0, (uint32_t)0);
596 test_true(rc
== MEMCACHED_SUCCESS
);
598 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
600 results
= memcached_result_create(memc
, &results_obj
);
602 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
604 test_true(rc
== MEMCACHED_SUCCESS
);
605 test_true(memcached_result_cas(results
));
606 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
607 test_true(strlen(memcached_result_value(results
)) == value_length
);
608 test_true(rc
== MEMCACHED_SUCCESS
);
609 uint64_t cas
= memcached_result_cas(results
);
612 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
613 test_true(rc
== MEMCACHED_END
);
614 test_true(results
== NULL
);
617 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
618 test_true(rc
== MEMCACHED_SUCCESS
);
621 * The item will have a new cas value, so try to set it again with the old
622 * value. This should fail!
624 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
625 test_true(rc
== MEMCACHED_DATA_EXISTS
);
627 memcached_result_free(&results_obj
);
632 static test_return_t
prepend_test(memcached_st
*memc
)
634 memcached_return_t rc
;
635 const char *key
= "fig";
636 const char *value
= "people";
637 char *out_value
= NULL
;
641 rc
= memcached_flush(memc
, 0);
642 test_true(rc
== MEMCACHED_SUCCESS
);
644 rc
= memcached_set(memc
, key
, strlen(key
),
645 value
, strlen(value
),
646 (time_t)0, (uint32_t)0);
647 test_true(rc
== MEMCACHED_SUCCESS
);
649 rc
= memcached_prepend(memc
, key
, strlen(key
),
650 "the ", strlen("the "),
651 (time_t)0, (uint32_t)0);
652 test_true(rc
== MEMCACHED_SUCCESS
);
654 rc
= memcached_prepend(memc
, key
, strlen(key
),
655 "we ", strlen("we "),
656 (time_t)0, (uint32_t)0);
657 test_true(rc
== MEMCACHED_SUCCESS
);
659 out_value
= memcached_get(memc
, key
, strlen(key
),
660 &value_length
, &flags
, &rc
);
661 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
662 test_true(strlen("we the people") == value_length
);
663 test_true(rc
== MEMCACHED_SUCCESS
);
670 Set the value, then quit to make sure it is flushed.
671 Come back in and test that add fails.
673 static test_return_t
add_test(memcached_st
*memc
)
675 memcached_return_t rc
;
676 const char *key
= "foo";
677 const char *value
= "when we sanitize";
678 unsigned long long setting_value
;
680 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
682 rc
= memcached_set(memc
, key
, strlen(key
),
683 value
, strlen(value
),
684 (time_t)0, (uint32_t)0);
685 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
686 memcached_quit(memc
);
687 rc
= memcached_add(memc
, key
, strlen(key
),
688 value
, strlen(value
),
689 (time_t)0, (uint32_t)0);
691 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
694 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
698 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
705 ** There was a problem of leaking filedescriptors in the initial release
706 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
707 ** systems it seems that the kernel is slow on reclaiming the resources
708 ** because the connects starts to time out (the test doesn't do much
709 ** anyway, so just loop 10 iterations)
711 static test_return_t
add_wrapper(memcached_st
*memc
)
713 unsigned int max
= 10000;
721 for (uint32_t x
= 0; x
< max
; x
++)
727 static test_return_t
replace_test(memcached_st
*memc
)
729 memcached_return_t rc
;
730 const char *key
= "foo";
731 const char *value
= "when we sanitize";
732 const char *original
= "first we insert some data";
734 rc
= memcached_set(memc
, key
, strlen(key
),
735 original
, strlen(original
),
736 (time_t)0, (uint32_t)0);
737 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
739 rc
= memcached_replace(memc
, key
, strlen(key
),
740 value
, strlen(value
),
741 (time_t)0, (uint32_t)0);
742 test_true(rc
== MEMCACHED_SUCCESS
);
747 static test_return_t
delete_test(memcached_st
*memc
)
749 memcached_return_t rc
;
750 const char *key
= "foo";
751 const char *value
= "when we sanitize";
753 rc
= memcached_set(memc
, key
, strlen(key
),
754 value
, strlen(value
),
755 (time_t)0, (uint32_t)0);
756 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
758 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
759 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
764 static test_return_t
flush_test(memcached_st
*memc
)
766 memcached_return_t rc
;
768 rc
= memcached_flush(memc
, 0);
769 test_true(rc
== MEMCACHED_SUCCESS
);
774 static memcached_return_t
server_function(const memcached_st
*ptr
,
775 const memcached_server_st
*server
,
778 (void)ptr
; (void)server
; (void)context
;
781 return MEMCACHED_SUCCESS
;
784 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
787 strcpy(context
, "foo bad");
788 memcached_server_fn callbacks
[1];
790 callbacks
[0]= server_function
;
791 memcached_server_cursor(memc
, callbacks
, context
, 1);
795 static test_return_t
bad_key_test(memcached_st
*memc
)
797 memcached_return_t rc
;
798 const char *key
= "foo bad";
800 size_t string_length
;
802 memcached_st
*memc_clone
;
804 size_t max_keylen
= 0xffff;
806 // Just skip if we are in binary mode.
807 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
810 memc_clone
= memcached_clone(NULL
, memc
);
811 test_true(memc_clone
);
813 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
814 test_true(rc
== MEMCACHED_SUCCESS
);
816 /* All keys are valid in the binary protocol (except for length) */
817 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
819 string
= memcached_get(memc_clone
, key
, strlen(key
),
820 &string_length
, &flags
, &rc
);
821 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
822 test_true(string_length
== 0);
826 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
827 test_true(rc
== MEMCACHED_SUCCESS
);
828 string
= memcached_get(memc_clone
, key
, strlen(key
),
829 &string_length
, &flags
, &rc
);
830 test_true(rc
== MEMCACHED_NOTFOUND
);
831 test_true(string_length
== 0);
834 /* Test multi key for bad keys */
835 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
836 size_t key_lengths
[] = { 7, 7, 7 };
838 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
839 test_true(rc
== MEMCACHED_SUCCESS
);
841 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
842 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
844 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
845 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
849 /* The following test should be moved to the end of this function when the
850 memcached server is updated to allow max size length of the keys in the
853 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
854 test_true(rc
== MEMCACHED_SUCCESS
);
856 char *longkey
= malloc(max_keylen
+ 1);
859 memset(longkey
, 'a', max_keylen
+ 1);
860 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
861 &string_length
, &flags
, &rc
);
862 test_true(rc
== MEMCACHED_NOTFOUND
);
863 test_true(string_length
== 0);
866 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
867 &string_length
, &flags
, &rc
);
868 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
869 test_true(string_length
== 0);
876 /* Make sure zero length keys are marked as bad */
878 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
879 test_true(rc
== MEMCACHED_SUCCESS
);
880 string
= memcached_get(memc_clone
, key
, 0,
881 &string_length
, &flags
, &rc
);
882 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
883 test_true(string_length
== 0);
886 memcached_free(memc_clone
);
891 #define READ_THROUGH_VALUE "set for me"
892 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
895 memcached_result_st
*result
)
897 (void)memc
;(void)key
;(void)key_length
;
898 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
901 static test_return_t
read_through(memcached_st
*memc
)
903 memcached_return_t rc
;
904 const char *key
= "foo";
906 size_t string_length
;
908 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
910 string
= memcached_get(memc
, key
, strlen(key
),
911 &string_length
, &flags
, &rc
);
913 test_true(rc
== MEMCACHED_NOTFOUND
);
914 test_false(string_length
);
917 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
919 test_true(rc
== MEMCACHED_SUCCESS
);
921 string
= memcached_get(memc
, key
, strlen(key
),
922 &string_length
, &flags
, &rc
);
924 test_true(rc
== MEMCACHED_SUCCESS
);
925 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
926 test_strcmp(READ_THROUGH_VALUE
, string
);
929 string
= memcached_get(memc
, key
, strlen(key
),
930 &string_length
, &flags
, &rc
);
932 test_true(rc
== MEMCACHED_SUCCESS
);
933 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
934 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
940 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
944 (void)ptr
;(void)key_length
;
947 return MEMCACHED_SUCCESS
;
950 static test_return_t
delete_through(memcached_st
*memc
)
952 memcached_trigger_delete_key_fn callback
;
953 memcached_return_t rc
;
955 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
957 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
958 test_true(rc
== MEMCACHED_SUCCESS
);
963 static test_return_t
get_test(memcached_st
*memc
)
965 memcached_return_t rc
;
966 const char *key
= "foo";
968 size_t string_length
;
971 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
972 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
974 string
= memcached_get(memc
, key
, strlen(key
),
975 &string_length
, &flags
, &rc
);
977 test_true(rc
== MEMCACHED_NOTFOUND
);
978 test_false(string_length
);
984 static test_return_t
get_test2(memcached_st
*memc
)
986 memcached_return_t rc
;
987 const char *key
= "foo";
988 const char *value
= "when we sanitize";
990 size_t string_length
;
993 rc
= memcached_set(memc
, key
, strlen(key
),
994 value
, strlen(value
),
995 (time_t)0, (uint32_t)0);
996 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
998 string
= memcached_get(memc
, key
, strlen(key
),
999 &string_length
, &flags
, &rc
);
1002 test_true(rc
== MEMCACHED_SUCCESS
);
1003 test_true(string_length
== strlen(value
));
1004 test_true(!memcmp(string
, value
, string_length
));
1008 return TEST_SUCCESS
;
1011 static test_return_t
set_test2(memcached_st
*memc
)
1013 memcached_return_t rc
;
1014 const char *key
= "foo";
1015 const char *value
= "train in the brain";
1016 size_t value_length
= strlen(value
);
1019 for (x
= 0; x
< 10; x
++)
1021 rc
= memcached_set(memc
, key
, strlen(key
),
1022 value
, value_length
,
1023 (time_t)0, (uint32_t)0);
1024 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1027 return TEST_SUCCESS
;
1030 static test_return_t
set_test3(memcached_st
*memc
)
1032 memcached_return_t rc
;
1034 size_t value_length
= 8191;
1037 value
= (char*)malloc(value_length
);
1040 for (x
= 0; x
< value_length
; x
++)
1041 value
[x
] = (char) (x
% 127);
1043 /* The dump test relies on there being at least 32 items in memcached */
1044 for (x
= 0; x
< 32; x
++)
1048 snprintf(key
, sizeof(key
), "foo%u", x
);
1050 rc
= memcached_set(memc
, key
, strlen(key
),
1051 value
, value_length
,
1052 (time_t)0, (uint32_t)0);
1053 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1058 return TEST_SUCCESS
;
1061 static test_return_t
get_test3(memcached_st
*memc
)
1063 memcached_return_t rc
;
1064 const char *key
= "foo";
1066 size_t value_length
= 8191;
1068 size_t string_length
;
1072 value
= (char*)malloc(value_length
);
1075 for (x
= 0; x
< value_length
; x
++)
1076 value
[x
] = (char) (x
% 127);
1078 rc
= memcached_set(memc
, key
, strlen(key
),
1079 value
, value_length
,
1080 (time_t)0, (uint32_t)0);
1081 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1083 string
= memcached_get(memc
, key
, strlen(key
),
1084 &string_length
, &flags
, &rc
);
1086 test_true(rc
== MEMCACHED_SUCCESS
);
1088 test_true(string_length
== value_length
);
1089 test_true(!memcmp(string
, value
, string_length
));
1094 return TEST_SUCCESS
;
1097 static test_return_t
get_test4(memcached_st
*memc
)
1099 memcached_return_t rc
;
1100 const char *key
= "foo";
1102 size_t value_length
= 8191;
1104 size_t string_length
;
1108 value
= (char*)malloc(value_length
);
1111 for (x
= 0; x
< value_length
; x
++)
1112 value
[x
] = (char) (x
% 127);
1114 rc
= memcached_set(memc
, key
, strlen(key
),
1115 value
, value_length
,
1116 (time_t)0, (uint32_t)0);
1117 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1119 for (x
= 0; x
< 10; x
++)
1121 string
= memcached_get(memc
, key
, strlen(key
),
1122 &string_length
, &flags
, &rc
);
1124 test_true(rc
== MEMCACHED_SUCCESS
);
1126 test_true(string_length
== value_length
);
1127 test_true(!memcmp(string
, value
, string_length
));
1133 return TEST_SUCCESS
;
1137 * This test verifies that memcached_read_one_response doesn't try to
1138 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1139 * responses before you execute a storage command.
1141 static test_return_t
get_test5(memcached_st
*memc
)
1144 ** Request the same key twice, to ensure that we hash to the same server
1145 ** (so that we have multiple response values queued up) ;-)
1147 const char *keys
[]= { "key", "key" };
1148 size_t lengths
[]= { 3, 3 };
1152 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1153 keys
[0], lengths
[0], 0, 0);
1154 test_true(rc
== MEMCACHED_SUCCESS
);
1155 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1157 memcached_result_st results_obj
;
1158 memcached_result_st
*results
;
1159 results
=memcached_result_create(memc
, &results_obj
);
1161 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1163 memcached_result_free(&results_obj
);
1165 /* Don't read out the second result, but issue a set instead.. */
1166 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1167 test_true(rc
== MEMCACHED_SUCCESS
);
1169 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1170 &rlen
, &flags
, &rc
);
1171 test_true(val
== NULL
);
1172 test_true(rc
== MEMCACHED_NOTFOUND
);
1173 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1174 test_true(val
!= NULL
);
1175 test_true(rc
== MEMCACHED_SUCCESS
);
1178 return TEST_SUCCESS
;
1181 static test_return_t
mget_end(memcached_st
*memc
)
1183 const char *keys
[]= { "foo", "foo2" };
1184 size_t lengths
[]= { 3, 4 };
1185 const char *values
[]= { "fjord", "41" };
1187 memcached_return_t rc
;
1190 for (int i
= 0; i
< 2; i
++)
1192 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1193 (time_t)0, (uint32_t)0);
1194 test_true(rc
== MEMCACHED_SUCCESS
);
1198 size_t string_length
;
1201 // retrieve both via mget
1202 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1203 test_true(rc
== MEMCACHED_SUCCESS
);
1205 char key
[MEMCACHED_MAX_KEY
];
1208 // this should get both
1209 for (int i
= 0; i
< 2; i
++)
1211 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1213 test_true(rc
== MEMCACHED_SUCCESS
);
1215 if (key_length
== 4)
1217 test_true(string_length
== strlen(values
[val
]));
1218 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1222 // this should indicate end
1223 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1224 test_true(rc
== MEMCACHED_END
);
1227 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1228 test_true(rc
== MEMCACHED_SUCCESS
);
1230 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1231 test_true(key_length
== lengths
[0]);
1232 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1233 test_true(string_length
== strlen(values
[0]));
1234 test_true(strncmp(values
[0], string
, string_length
) == 0);
1235 test_true(rc
== MEMCACHED_SUCCESS
);
1238 // this should indicate end
1239 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1240 test_true(rc
== MEMCACHED_END
);
1242 return TEST_SUCCESS
;
1245 /* Do not copy the style of this code, I just access hosts to testthis function */
1246 static test_return_t
stats_servername_test(memcached_st
*memc
)
1248 memcached_return_t rc
;
1249 memcached_stat_st memc_stat
;
1250 memcached_server_instance_st instance
=
1251 memcached_server_instance_by_position(memc
, 0);
1253 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1254 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1255 return TEST_SKIPPED
;
1257 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1258 memcached_server_name(instance
),
1259 memcached_server_port(instance
));
1261 return TEST_SUCCESS
;
1264 static test_return_t
increment_test(memcached_st
*memc
)
1266 uint64_t new_number
;
1267 memcached_return_t rc
;
1268 const char *key
= "number";
1269 const char *value
= "0";
1271 rc
= memcached_set(memc
, key
, strlen(key
),
1272 value
, strlen(value
),
1273 (time_t)0, (uint32_t)0);
1274 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1276 rc
= memcached_increment(memc
, key
, strlen(key
),
1278 test_true(rc
== MEMCACHED_SUCCESS
);
1279 test_true(new_number
== 1);
1281 rc
= memcached_increment(memc
, key
, strlen(key
),
1283 test_true(rc
== MEMCACHED_SUCCESS
);
1284 test_true(new_number
== 2);
1286 return TEST_SUCCESS
;
1289 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1291 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1293 uint64_t new_number
;
1294 memcached_return_t rc
;
1295 const char *key
= "number";
1296 uint64_t initial
= 0;
1298 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1299 1, initial
, 0, &new_number
);
1300 test_true(rc
== MEMCACHED_SUCCESS
);
1301 test_true(new_number
== initial
);
1303 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1304 1, initial
, 0, &new_number
);
1305 test_true(rc
== MEMCACHED_SUCCESS
);
1306 test_true(new_number
== (initial
+ 1));
1308 return TEST_SUCCESS
;
1311 static test_return_t
decrement_test(memcached_st
*memc
)
1313 uint64_t new_number
;
1314 memcached_return_t rc
;
1315 const char *key
= "number";
1316 const char *value
= "3";
1318 rc
= memcached_set(memc
, key
, strlen(key
),
1319 value
, strlen(value
),
1320 (time_t)0, (uint32_t)0);
1321 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1323 rc
= memcached_decrement(memc
, key
, strlen(key
),
1325 test_true(rc
== MEMCACHED_SUCCESS
);
1326 test_true(new_number
== 2);
1328 rc
= memcached_decrement(memc
, key
, strlen(key
),
1330 test_true(rc
== MEMCACHED_SUCCESS
);
1331 test_true(new_number
== 1);
1333 return TEST_SUCCESS
;
1336 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1338 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1340 uint64_t new_number
;
1341 memcached_return_t rc
;
1342 const char *key
= "number";
1343 uint64_t initial
= 3;
1345 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1346 1, initial
, 0, &new_number
);
1347 test_true(rc
== MEMCACHED_SUCCESS
);
1348 test_true(new_number
== initial
);
1350 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1351 1, initial
, 0, &new_number
);
1352 test_true(rc
== MEMCACHED_SUCCESS
);
1353 test_true(new_number
== (initial
- 1));
1355 return TEST_SUCCESS
;
1358 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1360 uint64_t new_number
;
1361 memcached_return_t rc
;
1362 const char *master_key
= "foo";
1363 const char *key
= "number";
1364 const char *value
= "0";
1366 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1368 value
, strlen(value
),
1369 (time_t)0, (uint32_t)0);
1370 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1372 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1374 test_true(rc
== MEMCACHED_SUCCESS
);
1375 test_true(new_number
== 1);
1377 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1379 test_true(rc
== MEMCACHED_SUCCESS
);
1380 test_true(new_number
== 2);
1382 return TEST_SUCCESS
;
1385 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1387 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1389 uint64_t new_number
;
1390 memcached_return_t rc
;
1391 const char *master_key
= "foo";
1392 const char *key
= "number";
1393 uint64_t initial
= 0;
1395 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1397 1, initial
, 0, &new_number
);
1398 test_true(rc
== MEMCACHED_SUCCESS
);
1399 test_true(new_number
== initial
);
1401 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1403 1, initial
, 0, &new_number
);
1404 test_true(rc
== MEMCACHED_SUCCESS
);
1405 test_true(new_number
== (initial
+ 1));
1407 return TEST_SUCCESS
;
1410 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1412 uint64_t new_number
;
1413 memcached_return_t rc
;
1414 const char *master_key
= "foo";
1415 const char *key
= "number";
1416 const char *value
= "3";
1418 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1420 value
, strlen(value
),
1421 (time_t)0, (uint32_t)0);
1422 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1424 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1427 test_true(rc
== MEMCACHED_SUCCESS
);
1428 test_true(new_number
== 2);
1430 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1433 test_true(rc
== MEMCACHED_SUCCESS
);
1434 test_true(new_number
== 1);
1436 return TEST_SUCCESS
;
1439 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1441 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1443 uint64_t new_number
;
1444 memcached_return_t rc
;
1445 const char *master_key
= "foo";
1446 const char *key
= "number";
1447 uint64_t initial
= 3;
1449 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1451 1, initial
, 0, &new_number
);
1452 test_true(rc
== MEMCACHED_SUCCESS
);
1453 test_true(new_number
== initial
);
1455 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1457 1, initial
, 0, &new_number
);
1458 test_true(rc
== MEMCACHED_SUCCESS
);
1459 test_true(new_number
== (initial
- 1));
1461 return TEST_SUCCESS
;
1464 static test_return_t
quit_test(memcached_st
*memc
)
1466 memcached_return_t rc
;
1467 const char *key
= "fudge";
1468 const char *value
= "sanford and sun";
1470 rc
= memcached_set(memc
, key
, strlen(key
),
1471 value
, strlen(value
),
1472 (time_t)10, (uint32_t)3);
1473 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1474 memcached_quit(memc
);
1476 rc
= memcached_set(memc
, key
, strlen(key
),
1477 value
, strlen(value
),
1478 (time_t)50, (uint32_t)9);
1479 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1481 return TEST_SUCCESS
;
1484 static test_return_t
mget_result_test(memcached_st
*memc
)
1486 memcached_return_t rc
;
1487 const char *keys
[]= {"fudge", "son", "food"};
1488 size_t key_length
[]= {5, 3, 4};
1491 memcached_result_st results_obj
;
1492 memcached_result_st
*results
;
1494 results
= memcached_result_create(memc
, &results_obj
);
1496 test_true(&results_obj
== results
);
1498 /* We need to empty the server before continueing test */
1499 rc
= memcached_flush(memc
, 0);
1500 test_true(rc
== MEMCACHED_SUCCESS
);
1502 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1503 test_true(rc
== MEMCACHED_SUCCESS
);
1505 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1510 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1511 test_true(!results
);
1512 test_true(rc
== MEMCACHED_END
);
1514 for (x
= 0; x
< 3; x
++)
1516 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1517 keys
[x
], key_length
[x
],
1518 (time_t)50, (uint32_t)9);
1519 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1522 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1523 test_true(rc
== MEMCACHED_SUCCESS
);
1525 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1528 test_true(&results_obj
== results
);
1529 test_true(rc
== MEMCACHED_SUCCESS
);
1530 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1531 test_true(!memcmp(memcached_result_key_value(results
),
1532 memcached_result_value(results
),
1533 memcached_result_length(results
)));
1536 memcached_result_free(&results_obj
);
1538 return TEST_SUCCESS
;
1541 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1543 memcached_return_t rc
;
1544 const char *keys
[]= {"fudge", "son", "food"};
1545 size_t key_length
[]= {5, 3, 4};
1548 memcached_result_st
*results
;
1550 /* We need to empty the server before continueing test */
1551 rc
= memcached_flush(memc
, 0);
1552 test_true(rc
== MEMCACHED_SUCCESS
);
1554 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1555 test_true(rc
== MEMCACHED_SUCCESS
);
1557 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1561 test_true(!results
);
1562 test_true(rc
== MEMCACHED_END
);
1564 for (x
= 0; x
< 3; x
++)
1566 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1567 keys
[x
], key_length
[x
],
1568 (time_t)50, (uint32_t)9);
1569 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1572 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1573 test_true(rc
== MEMCACHED_SUCCESS
);
1576 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1579 test_true(rc
== MEMCACHED_SUCCESS
);
1580 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1581 test_true(!memcmp(memcached_result_key_value(results
),
1582 memcached_result_value(results
),
1583 memcached_result_length(results
)));
1584 memcached_result_free(results
);
1588 return TEST_SUCCESS
;
1591 /* Count the results */
1592 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1593 memcached_result_st
*result
,
1596 (void)ptr
; (void)result
;
1597 size_t *counter
= (size_t *)context
;
1599 *counter
= *counter
+ 1;
1601 return MEMCACHED_SUCCESS
;
1604 static test_return_t
mget_result_function(memcached_st
*memc
)
1606 memcached_return_t rc
;
1607 const char *keys
[]= {"fudge", "son", "food"};
1608 size_t key_length
[]= {5, 3, 4};
1611 memcached_execute_fn callbacks
[1];
1613 /* We need to empty the server before continueing test */
1614 rc
= memcached_flush(memc
, 0);
1615 for (x
= 0; x
< 3; x
++)
1617 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1618 keys
[x
], key_length
[x
],
1619 (time_t)50, (uint32_t)9);
1620 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1623 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1624 test_true(rc
== MEMCACHED_SUCCESS
);
1626 callbacks
[0]= &callback_counter
;
1628 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1630 test_true(counter
== 3);
1632 return TEST_SUCCESS
;
1635 static test_return_t
mget_test(memcached_st
*memc
)
1637 memcached_return_t rc
;
1638 const char *keys
[]= {"fudge", "son", "food"};
1639 size_t key_length
[]= {5, 3, 4};
1643 char return_key
[MEMCACHED_MAX_KEY
];
1644 size_t return_key_length
;
1646 size_t return_value_length
;
1648 /* We need to empty the server before continueing test */
1649 rc
= memcached_flush(memc
, 0);
1650 test_true(rc
== MEMCACHED_SUCCESS
);
1652 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1653 test_true(rc
== MEMCACHED_SUCCESS
);
1655 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1656 &return_value_length
, &flags
, &rc
)) != NULL
)
1658 test_true(return_value
);
1660 test_true(!return_value
);
1661 test_true(return_value_length
== 0);
1662 test_true(rc
== MEMCACHED_END
);
1664 for (x
= 0; x
< 3; x
++)
1666 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1667 keys
[x
], key_length
[x
],
1668 (time_t)50, (uint32_t)9);
1669 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1672 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1673 test_true(rc
== MEMCACHED_SUCCESS
);
1676 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1677 &return_value_length
, &flags
, &rc
)))
1679 test_true(return_value
);
1680 test_true(rc
== MEMCACHED_SUCCESS
);
1681 test_true(return_key_length
== return_value_length
);
1682 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1687 return TEST_SUCCESS
;
1690 static test_return_t
mget_execute(memcached_st
*memc
)
1694 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1698 * I only want to hit _one_ server so I know the number of requests I'm
1699 * sending in the pipeline.
1701 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1702 memc
->number_of_hosts
= 1;
1704 size_t max_keys
= 20480;
1707 char **keys
= calloc(max_keys
, sizeof(char*));
1708 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1710 /* First add all of the items.. */
1711 char blob
[1024] = {0};
1712 memcached_return_t rc
;
1714 for (size_t x
= 0; x
< max_keys
; ++x
)
1718 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1720 test_true(keys
[x
] != NULL
);
1721 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1722 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1725 /* Try to get all of them with a large multiget */
1727 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1728 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1729 max_keys
, callbacks
, &counter
, 1);
1731 if (rc
== MEMCACHED_SUCCESS
)
1734 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1735 test_true(rc
== MEMCACHED_END
);
1737 /* Verify that we got all of the items */
1738 test_true(counter
== max_keys
);
1740 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1742 test_true(counter
== 0);
1746 test_fail("note: this test functions differently when in binary mode");
1749 /* Release all allocated resources */
1750 for (size_t x
= 0; x
< max_keys
; ++x
)
1757 memc
->number_of_hosts
= number_of_hosts
;
1758 return TEST_SUCCESS
;
1761 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1763 static test_return_t
key_setup(memcached_st
*memc
)
1767 if (pre_binary(memc
) != TEST_SUCCESS
)
1768 return TEST_SKIPPED
;
1770 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1772 return TEST_SUCCESS
;
1775 static test_return_t
key_teardown(memcached_st
*memc
)
1778 pairs_free(global_pairs
);
1780 return TEST_SUCCESS
;
1783 static test_return_t
block_add_regression(memcached_st
*memc
)
1785 /* First add all of the items.. */
1786 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1788 memcached_return_t rc
;
1789 char blob
[1024] = {0};
1791 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1792 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1795 return TEST_SUCCESS
;
1798 static test_return_t
binary_add_regression(memcached_st
*memc
)
1800 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1801 test_return_t rc
= block_add_regression(memc
);
1802 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1806 static test_return_t
get_stats_keys(memcached_st
*memc
)
1810 memcached_stat_st memc_stat
;
1811 memcached_return_t rc
;
1813 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1814 test_true(rc
== MEMCACHED_SUCCESS
);
1815 for (ptr
= stat_list
; *ptr
; ptr
++)
1820 return TEST_SUCCESS
;
1823 static test_return_t
version_string_test(memcached_st
*memc
)
1825 const char *version_string
;
1828 version_string
= memcached_lib_version();
1830 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1832 return TEST_SUCCESS
;
1835 static test_return_t
get_stats(memcached_st
*memc
)
1839 memcached_return_t rc
;
1840 memcached_stat_st
*memc_stat
;
1842 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1843 test_true(rc
== MEMCACHED_SUCCESS
);
1845 test_true(rc
== MEMCACHED_SUCCESS
);
1846 test_true(memc_stat
);
1848 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1850 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1851 test_true(rc
== MEMCACHED_SUCCESS
);
1852 for (ptr
= stat_list
; *ptr
; ptr
++);
1857 memcached_stat_free(NULL
, memc_stat
);
1859 return TEST_SUCCESS
;
1862 static test_return_t
add_host_test(memcached_st
*memc
)
1865 memcached_server_st
*servers
;
1866 memcached_return_t rc
;
1867 char servername
[]= "0.example.com";
1869 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1871 test_true(1 == memcached_server_list_count(servers
));
1873 for (x
= 2; x
< 20; x
++)
1875 char buffer
[SMALL_STRING_LEN
];
1877 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1878 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1880 test_true(rc
== MEMCACHED_SUCCESS
);
1881 test_true(x
== memcached_server_list_count(servers
));
1884 rc
= memcached_server_push(memc
, servers
);
1885 test_true(rc
== MEMCACHED_SUCCESS
);
1886 rc
= memcached_server_push(memc
, servers
);
1887 test_true(rc
== MEMCACHED_SUCCESS
);
1889 memcached_server_list_free(servers
);
1891 return TEST_SUCCESS
;
1894 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1896 (void)parent
;(void)memc_clone
;
1897 return MEMCACHED_SUCCESS
;
1900 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1903 return MEMCACHED_SUCCESS
;
1906 static test_return_t
callback_test(memcached_st
*memc
)
1908 /* Test User Data */
1912 memcached_return_t rc
;
1914 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1915 test_true(rc
== MEMCACHED_SUCCESS
);
1916 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1917 test_true(*test_ptr
== x
);
1920 /* Test Clone Callback */
1922 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1923 void *clone_cb_ptr
= *(void **)&clone_cb
;
1924 void *temp_function
= NULL
;
1925 memcached_return_t rc
;
1927 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1929 test_true(rc
== MEMCACHED_SUCCESS
);
1930 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1931 test_true(temp_function
== clone_cb_ptr
);
1934 /* Test Cleanup Callback */
1936 memcached_cleanup_fn cleanup_cb
=
1937 (memcached_cleanup_fn
)cleanup_test_callback
;
1938 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1939 void *temp_function
= NULL
;
1940 memcached_return_t rc
;
1942 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1944 test_true(rc
== MEMCACHED_SUCCESS
);
1945 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1946 test_true(temp_function
== cleanup_cb_ptr
);
1949 return TEST_SUCCESS
;
1952 /* We don't test the behavior itself, we test the switches */
1953 static test_return_t
behavior_test(memcached_st
*memc
)
1958 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1959 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1960 test_true(value
== 1);
1962 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1963 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1964 test_true(value
== 1);
1966 set
= MEMCACHED_HASH_MD5
;
1967 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1968 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1969 test_true(value
== MEMCACHED_HASH_MD5
);
1973 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1974 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1975 test_true(value
== 0);
1977 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1978 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1979 test_true(value
== 0);
1981 set
= MEMCACHED_HASH_DEFAULT
;
1982 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1983 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1984 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1986 set
= MEMCACHED_HASH_CRC
;
1987 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1988 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1989 test_true(value
== MEMCACHED_HASH_CRC
);
1991 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1992 test_true(value
> 0);
1994 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1995 test_true(value
> 0);
1997 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1998 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1999 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2001 return TEST_SUCCESS
;
2004 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2006 memcached_return_t rc
;
2010 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2011 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2013 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2015 if (rc
== MEMCACHED_SUCCESS
)
2017 test_true((bool)value
== set
);
2021 test_false((bool)value
== set
);
2024 return TEST_SUCCESS
;
2028 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2030 memcached_return_t rc
;
2034 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2035 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2037 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2039 if (rc
== MEMCACHED_SUCCESS
)
2041 test_true((bool)value
== set
);
2045 test_false((bool)value
== set
);
2048 return TEST_SUCCESS
;
2052 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2054 memcached_return_t rc
;
2058 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2059 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2061 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2063 if (rc
== MEMCACHED_SUCCESS
)
2065 test_true((bool)value
== set
);
2069 test_false((bool)value
== set
);
2072 return TEST_SUCCESS
;
2075 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2077 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2078 char return_key
[MEMCACHED_MAX_KEY
];
2079 size_t return_key_length
;
2081 size_t return_value_length
;
2086 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2087 &return_value_length
, &flags
, &rc
)))
2089 test_true(return_value
);
2090 test_true(rc
== MEMCACHED_SUCCESS
);
2092 *keys_returned
= *keys_returned
+1;
2095 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2097 return TEST_SUCCESS
;
2100 /* Test case provided by Cal Haldenbrand */
2101 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2103 unsigned int setter
= 1;
2105 unsigned long long total
= 0;
2108 char randomstuff
[6 * 1024];
2109 memcached_return_t rc
;
2111 memset(randomstuff
, 0, 6 * 1024);
2113 /* We just keep looking at the same values over and over */
2116 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2117 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2121 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2125 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2126 memset(randomstuff
, 0, 6 * 1024);
2127 test_true(size
< 6 * 1024); /* Being safe here */
2129 for (j
= 0 ; j
< size
;j
++)
2130 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2133 snprintf(key
, sizeof(key
), "%u", x
);
2134 rc
= memcached_set(memc
, key
, strlen(key
),
2135 randomstuff
, strlen(randomstuff
), 10, 0);
2136 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2137 /* If we fail, lets try again */
2138 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2139 rc
= memcached_set(memc
, key
, strlen(key
),
2140 randomstuff
, strlen(randomstuff
), 10, 0);
2141 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2144 return TEST_SUCCESS
;
2147 /* Test case provided by Cal Haldenbrand */
2148 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2150 unsigned int setter
;
2154 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2155 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2157 setter
= 20 * 1024576;
2158 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2159 setter
= 20 * 1024576;
2160 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2161 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2162 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2164 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2167 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2169 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2170 char buffer
[SMALL_STRING_LEN
];
2175 memset(buffer
, 0, SMALL_STRING_LEN
);
2177 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2178 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2179 &val_len
, &flags
, &rc
);
2180 if (rc
!= MEMCACHED_SUCCESS
)
2182 if (rc
== MEMCACHED_NOTFOUND
)
2196 return TEST_SUCCESS
;
2199 /* Do a large mget() over all the keys we think exist */
2200 #define KEY_COUNT 3000 // * 1024576
2201 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2203 memcached_return_t rc
;
2204 unsigned int setter
;
2207 size_t key_lengths
[KEY_COUNT
];
2210 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2211 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2213 setter
= 20 * 1024576;
2214 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2215 setter
= 20 * 1024576;
2216 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2217 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2218 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2221 keys
= calloc(KEY_COUNT
, sizeof(char *));
2223 for (x
= 0; x
< KEY_COUNT
; x
++)
2227 snprintf(buffer
, 30, "%u", x
);
2228 keys
[x
]= strdup(buffer
);
2229 key_lengths
[x
]= strlen(keys
[x
]);
2232 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2233 test_true(rc
== MEMCACHED_SUCCESS
);
2235 size_t keys_returned
;
2236 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2238 for (x
= 0; x
< KEY_COUNT
; x
++)
2242 return TEST_SUCCESS
;
2245 /* Make sure we behave properly if server list has no values */
2246 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2248 memcached_return_t rc
;
2249 const char *keys
[]= {"fudge", "son", "food"};
2250 size_t key_length
[]= {5, 3, 4};
2253 char return_key
[MEMCACHED_MAX_KEY
];
2254 size_t return_key_length
;
2256 size_t return_value_length
;
2258 /* Here we free everything before running a bunch of mget tests */
2259 memcached_servers_reset(memc
);
2262 /* We need to empty the server before continueing test */
2263 rc
= memcached_flush(memc
, 0);
2264 test_true(rc
== MEMCACHED_NO_SERVERS
);
2266 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2267 test_true(rc
== MEMCACHED_NO_SERVERS
);
2269 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2270 &return_value_length
, &flags
, &rc
)) != NULL
)
2272 test_true(return_value
);
2274 test_true(!return_value
);
2275 test_true(return_value_length
== 0);
2276 test_true(rc
== MEMCACHED_NO_SERVERS
);
2278 for (x
= 0; x
< 3; x
++)
2280 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2281 keys
[x
], key_length
[x
],
2282 (time_t)50, (uint32_t)9);
2283 test_true(rc
== MEMCACHED_NO_SERVERS
);
2286 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2287 test_true(rc
== MEMCACHED_NO_SERVERS
);
2290 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2291 &return_value_length
, &flags
, &rc
)))
2293 test_true(return_value
);
2294 test_true(rc
== MEMCACHED_SUCCESS
);
2295 test_true(return_key_length
== return_value_length
);
2296 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2301 return TEST_SUCCESS
;
2304 #define VALUE_SIZE_BUG5 1048064
2305 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2307 memcached_return_t rc
;
2308 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2309 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2310 char return_key
[MEMCACHED_MAX_KEY
];
2311 size_t return_key_length
;
2313 size_t value_length
;
2317 char insert_data
[VALUE_SIZE_BUG5
];
2319 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2320 insert_data
[x
]= (signed char)rand();
2322 memcached_flush(memc
, 0);
2323 value
= memcached_get(memc
, keys
[0], key_length
[0],
2324 &value_length
, &flags
, &rc
);
2325 test_true(value
== NULL
);
2326 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2329 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2330 &value_length
, &flags
, &rc
)))
2332 test_true(count
== 0);
2334 for (x
= 0; x
< 4; x
++)
2336 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2337 insert_data
, VALUE_SIZE_BUG5
,
2338 (time_t)0, (uint32_t)0);
2339 test_true(rc
== MEMCACHED_SUCCESS
);
2342 for (x
= 0; x
< 10; x
++)
2344 value
= memcached_get(memc
, keys
[0], key_length
[0],
2345 &value_length
, &flags
, &rc
);
2349 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2351 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2352 &value_length
, &flags
, &rc
)))
2357 test_true(count
== 4);
2360 return TEST_SUCCESS
;
2363 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2365 memcached_return_t rc
;
2366 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2367 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2368 char return_key
[MEMCACHED_MAX_KEY
];
2369 size_t return_key_length
;
2371 size_t value_length
;
2375 char insert_data
[VALUE_SIZE_BUG5
];
2377 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2378 insert_data
[x
]= (signed char)rand();
2380 memcached_flush(memc
, 0);
2381 value
= memcached_get(memc
, keys
[0], key_length
[0],
2382 &value_length
, &flags
, &rc
);
2383 test_true(value
== NULL
);
2384 test_true(rc
== MEMCACHED_NOTFOUND
);
2385 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2386 test_true(rc
== MEMCACHED_SUCCESS
);
2389 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2390 &value_length
, &flags
, &rc
)))
2392 test_true(count
== 0);
2393 test_true(rc
== MEMCACHED_END
);
2395 for (x
= 0; x
< 4; x
++)
2397 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2398 insert_data
, VALUE_SIZE_BUG5
,
2399 (time_t)0, (uint32_t)0);
2400 test_true(rc
== MEMCACHED_SUCCESS
);
2403 for (x
= 0; x
< 2; x
++)
2405 value
= memcached_get(memc
, keys
[0], key_length
[0],
2406 &value_length
, &flags
, &rc
);
2410 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2411 test_true(rc
== MEMCACHED_SUCCESS
);
2413 /* We test for purge of partial complete fetches */
2414 for (count
= 3; count
; count
--)
2416 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2417 &value_length
, &flags
, &rc
);
2418 test_true(rc
== MEMCACHED_SUCCESS
);
2419 test_true(!(memcmp(value
, insert_data
, value_length
)));
2420 test_true(value_length
);
2425 return TEST_SUCCESS
;
2428 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2430 memcached_return_t rc
;
2432 memcached_st
*memc_clone
;
2434 memcached_server_st
*servers
;
2435 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";
2438 servers
= memcached_servers_parse(server_list
);
2441 mine
= memcached_create(NULL
);
2442 rc
= memcached_server_push(mine
, servers
);
2443 test_true(rc
== MEMCACHED_SUCCESS
);
2444 memcached_server_list_free(servers
);
2447 memc_clone
= memcached_clone(NULL
, mine
);
2449 memcached_quit(mine
);
2450 memcached_quit(memc_clone
);
2453 memcached_free(mine
);
2454 memcached_free(memc_clone
);
2456 return TEST_SUCCESS
;
2459 /* Test flag store/retrieve */
2460 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2462 memcached_return_t rc
;
2463 const char *keys
= "036790384900";
2464 size_t key_length
= strlen(keys
);
2465 char return_key
[MEMCACHED_MAX_KEY
];
2466 size_t return_key_length
;
2468 size_t value_length
;
2471 char insert_data
[VALUE_SIZE_BUG5
];
2473 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2474 insert_data
[x
]= (signed char)rand();
2476 memcached_flush(memc
, 0);
2479 rc
= memcached_set(memc
, keys
, key_length
,
2480 insert_data
, VALUE_SIZE_BUG5
,
2482 test_true(rc
== MEMCACHED_SUCCESS
);
2485 value
= memcached_get(memc
, keys
, key_length
,
2486 &value_length
, &flags
, &rc
);
2487 test_true(flags
== 245);
2491 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2494 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2495 &value_length
, &flags
, &rc
);
2496 test_true(flags
== 245);
2501 return TEST_SUCCESS
;
2504 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2506 memcached_return_t rc
;
2507 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2508 size_t key_length
[3];
2513 char return_key
[MEMCACHED_MAX_KEY
];
2514 size_t return_key_length
;
2516 size_t return_value_length
;
2519 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2520 key_length
[1]= strlen("fudge&*@#");
2521 key_length
[2]= strlen("for^#@&$not");
2524 for (x
= 0; x
< 3; x
++)
2526 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2527 keys
[x
], key_length
[x
],
2528 (time_t)50, (uint32_t)9);
2529 test_true(rc
== MEMCACHED_SUCCESS
);
2532 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2533 test_true(rc
== MEMCACHED_SUCCESS
);
2535 /* We need to empty the server before continueing test */
2536 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2537 &return_value_length
, &flags
, &rc
)) != NULL
)
2539 test_true(return_value
);
2543 test_true(count
== 3);
2545 return TEST_SUCCESS
;
2548 /* We are testing with aggressive timeout to get failures */
2549 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2551 const char *key
= "foo";
2553 size_t value_length
= 512;
2556 memcached_return_t rc
;
2557 unsigned int set
= 1;
2558 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2561 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2562 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2564 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2567 value
= (char*)malloc(value_length
* sizeof(char));
2569 for (x
= 0; x
< value_length
; x
++)
2570 value
[x
]= (char) (x
% 127);
2572 for (x
= 1; x
<= 100000; ++x
)
2574 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2576 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2577 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2579 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2584 memcached_free(mclone
);
2586 return TEST_SUCCESS
;
2590 We are looking failures in the async protocol
2592 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2594 const char *key
= "foo";
2596 size_t value_length
= 512;
2599 memcached_return_t rc
;
2600 unsigned int set
= 1;
2602 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2604 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2605 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2607 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2610 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2612 test_true(timeout
== -1);
2614 value
= (char*)malloc(value_length
* sizeof(char));
2616 for (x
= 0; x
< value_length
; x
++)
2617 value
[x
]= (char) (x
% 127);
2619 for (x
= 1; x
<= 100000; ++x
)
2621 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2625 memcached_free(mclone
);
2627 return TEST_SUCCESS
;
2631 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2633 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2635 memcached_return_t rc
;
2637 size_t value_length
;
2639 uint64_t number_value
;
2641 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2642 &value_length
, &flags
, &rc
);
2643 test_true(value
== NULL
);
2644 test_true(rc
== MEMCACHED_NOTFOUND
);
2646 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2649 test_true(value
== NULL
);
2650 /* The binary protocol will set the key if it doesn't exist */
2651 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2653 test_true(rc
== MEMCACHED_SUCCESS
);
2657 test_true(rc
== MEMCACHED_NOTFOUND
);
2660 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2662 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2663 &value_length
, &flags
, &rc
);
2665 test_true(rc
== MEMCACHED_SUCCESS
);
2668 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2670 test_true(number_value
== 2);
2671 test_true(rc
== MEMCACHED_SUCCESS
);
2673 return TEST_SUCCESS
;
2677 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2678 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2680 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2682 char key
[] = "key34567890";
2684 memcached_return_t rc
;
2685 size_t overflowSize
;
2687 char commandFirst
[]= "set key34567890 0 0 ";
2688 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2689 size_t commandLength
;
2692 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2694 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2696 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2698 overflow
= malloc(testSize
);
2699 test_true(overflow
!= NULL
);
2701 memset(overflow
, 'x', testSize
);
2702 rc
= memcached_set(memc
, key
, strlen(key
),
2703 overflow
, testSize
, 0, 0);
2704 test_true(rc
== MEMCACHED_SUCCESS
);
2708 return TEST_SUCCESS
;
2713 Test values of many different sizes
2714 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2715 set key34567890 0 0 8169 \r\n
2716 is sent followed by buffer of size 8169, followed by 8169
2718 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2721 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2722 memcached_return_t rc
;
2723 const char *key
= "foo";
2725 size_t value_length
= 18000;
2727 size_t string_length
;
2730 size_t current_length
;
2732 value
= (char*)malloc(value_length
);
2735 for (x
= 0; x
< value_length
; x
++)
2736 value
[x
] = (char) (x
% 127);
2738 for (current_length
= 0; current_length
< value_length
; current_length
++)
2740 rc
= memcached_set(memc
, key
, strlen(key
),
2741 value
, current_length
,
2742 (time_t)0, (uint32_t)0);
2743 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2745 string
= memcached_get(memc
, key
, strlen(key
),
2746 &string_length
, &flags
, &rc
);
2748 test_true(rc
== MEMCACHED_SUCCESS
);
2749 test_true(string_length
== current_length
);
2750 test_true(!memcmp(string
, value
, string_length
));
2757 return TEST_SUCCESS
;
2761 Look for zero length value problems
2763 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2766 memcached_return_t rc
;
2767 const char *key
= "mykey";
2772 for (x
= 0; x
< 2; x
++)
2774 rc
= memcached_set(memc
, key
, strlen(key
),
2776 (time_t)0, (uint32_t)0);
2778 test_true(rc
== MEMCACHED_SUCCESS
);
2780 value
= memcached_get(memc
, key
, strlen(key
),
2781 &length
, &flags
, &rc
);
2783 test_true(rc
== MEMCACHED_SUCCESS
);
2784 test_true(value
== NULL
);
2785 test_true(length
== 0);
2786 test_true(flags
== 0);
2788 value
= memcached_get(memc
, key
, strlen(key
),
2789 &length
, &flags
, &rc
);
2791 test_true(rc
== MEMCACHED_SUCCESS
);
2792 test_true(value
== NULL
);
2793 test_true(length
== 0);
2794 test_true(flags
== 0);
2797 return TEST_SUCCESS
;
2800 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2801 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2803 memcached_return_t rc
;
2804 const char *key
= "mykey";
2809 rc
= memcached_set(memc
, key
, strlen(key
),
2811 (time_t)0, UINT32_MAX
);
2813 test_true(rc
== MEMCACHED_SUCCESS
);
2815 value
= memcached_get(memc
, key
, strlen(key
),
2816 &length
, &flags
, &rc
);
2818 test_true(rc
== MEMCACHED_SUCCESS
);
2819 test_true(value
== NULL
);
2820 test_true(length
== 0);
2821 test_true(flags
== UINT32_MAX
);
2823 return TEST_SUCCESS
;
2826 #if !defined(__sun) && !defined(__OpenBSD__)
2827 /* Check the validity of chinese key*/
2828 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2830 memcached_return_t rc
;
2831 const char *key
= "豆瓣";
2832 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2837 rc
= memcached_set(memc
, key
, strlen(key
),
2838 value
, strlen(value
),
2841 test_true(rc
== MEMCACHED_SUCCESS
);
2843 value2
= memcached_get(memc
, key
, strlen(key
),
2844 &length
, &flags
, &rc
);
2846 test_true(length
==strlen(value
));
2847 test_true(rc
== MEMCACHED_SUCCESS
);
2848 test_true(memcmp(value
, value2
, length
)==0);
2851 return TEST_SUCCESS
;
2859 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2862 const memcached_server_st
*server
;
2863 memcached_return_t res
;
2867 memc
= memcached_create(NULL
);
2868 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2869 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2871 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2873 memcached_free(memc
);
2875 return TEST_SUCCESS
;
2878 /* CAS test from Andei */
2879 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2881 memcached_return_t status
;
2882 memcached_result_st
*result
, result_obj
;
2883 const char *key
= "abc";
2884 size_t key_len
= strlen("abc");
2885 const char *value
= "foobar";
2886 size_t value_len
= strlen(value
);
2888 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2890 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2891 test_true(status
== MEMCACHED_SUCCESS
);
2893 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2894 test_true(status
== MEMCACHED_SUCCESS
);
2896 result
= memcached_result_create(memc
, &result_obj
);
2899 memcached_result_create(memc
, &result_obj
);
2900 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2903 test_true(status
== MEMCACHED_SUCCESS
);
2905 memcached_result_free(result
);
2907 return TEST_SUCCESS
;
2910 #include "ketama_test_cases.h"
2911 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2913 memcached_return_t rc
;
2916 memcached_server_st
*server_pool
;
2921 memc
= memcached_create(NULL
);
2924 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2925 test_true(rc
== MEMCACHED_SUCCESS
);
2927 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2928 test_true(value
== 1);
2930 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2931 test_true(rc
== MEMCACHED_SUCCESS
);
2933 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2934 test_true(value
== MEMCACHED_HASH_MD5
);
2936 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");
2937 memcached_server_push(memc
, server_pool
);
2939 /* verify that the server list was parsed okay. */
2940 test_true(memcached_server_count(memc
) == 8);
2941 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2942 test_true(server_pool
[0].port
== 11211);
2943 test_true(server_pool
[0].weight
== 600);
2944 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2945 test_true(server_pool
[2].port
== 11211);
2946 test_true(server_pool
[2].weight
== 200);
2947 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2948 test_true(server_pool
[7].port
== 11211);
2949 test_true(server_pool
[7].weight
== 100);
2951 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2952 * us test the boundary wraparound.
2954 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2956 /* verify the standard ketama set. */
2957 for (x
= 0; x
< 99; x
++)
2959 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2961 memcached_server_instance_st instance
=
2962 memcached_server_instance_by_position(memc
, server_idx
);
2964 const char *hostname
= memcached_server_name(instance
);
2965 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2968 memcached_server_list_free(server_pool
);
2969 memcached_free(memc
);
2971 return TEST_SUCCESS
;
2974 /* Large mget() of missing keys with binary proto
2976 * If many binary quiet commands (such as getq's in an mget) fill the output
2977 * buffer and the server chooses not to respond, memcached_flush hangs. See
2978 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2981 /* sighandler_t function that always asserts false */
2982 static void fail(int unused
)
2989 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2994 return TEST_SKIPPED
;
2996 memcached_return_t rc
;
2999 size_t* key_lengths
;
3000 void (*oldalarm
)(int);
3001 memcached_st
*memc_clone
;
3003 memc_clone
= memcached_clone(NULL
, memc
);
3004 test_true(memc_clone
);
3006 /* only binproto uses getq for mget */
3007 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3009 /* empty the cache to ensure misses (hence non-responses) */
3010 rc
= memcached_flush(memc_clone
, 0);
3011 test_true(rc
== MEMCACHED_SUCCESS
);
3013 key_lengths
= calloc(key_count
, sizeof(size_t));
3014 keys
= calloc(key_count
, sizeof(char *));
3016 for (x
= 0; x
< key_count
; x
++)
3020 snprintf(buffer
, 30, "%u", x
);
3021 keys
[x
]= strdup(buffer
);
3022 key_lengths
[x
]= strlen(keys
[x
]);
3025 oldalarm
= signal(SIGALRM
, fail
);
3028 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3029 test_true(rc
== MEMCACHED_SUCCESS
);
3032 signal(SIGALRM
, oldalarm
);
3034 size_t keys_returned
;
3035 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3037 for (x
= 0; x
< key_count
; x
++)
3042 memcached_free(memc_clone
);
3044 return TEST_SUCCESS
;
3048 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3050 test_return_t test_rc
;
3051 test_rc
= pre_binary(memc
);
3053 if (test_rc
!= TEST_SUCCESS
)
3058 /* should work as of r580 */
3059 rc
= _user_supplied_bug21(memc
, 10);
3060 test_true(rc
== TEST_SUCCESS
);
3062 /* should fail as of r580 */
3063 rc
= _user_supplied_bug21(memc
, 1000);
3064 test_true(rc
== TEST_SUCCESS
);
3066 return TEST_SUCCESS
;
3069 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3072 memcached_server_instance_st instance
;
3074 memcached_return_t rc
;
3075 memcached_st
*memc
= memcached_create(NULL
);
3078 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3079 test_true(rc
== MEMCACHED_SUCCESS
);
3081 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3082 test_true(value
== 1);
3084 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3085 test_true(rc
== MEMCACHED_SUCCESS
);
3087 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3088 test_true(value
== MEMCACHED_HASH_MD5
);
3090 /* server should be removed when in delay */
3091 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3092 test_true(rc
== MEMCACHED_SUCCESS
);
3094 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3095 test_true(value
== 1);
3097 memcached_server_st
*server_pool
;
3098 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");
3099 memcached_server_push(memc
, server_pool
);
3101 /* verify that the server list was parsed okay. */
3102 test_true(memcached_server_count(memc
) == 8);
3103 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3104 test_true(server_pool
[0].port
== 11211);
3105 test_true(server_pool
[0].weight
== 600);
3106 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3107 test_true(server_pool
[2].port
== 11211);
3108 test_true(server_pool
[2].weight
== 200);
3109 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3110 test_true(server_pool
[7].port
== 11211);
3111 test_true(server_pool
[7].weight
== 100);
3113 instance
= memcached_server_instance_by_position(memc
, 2);
3114 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3115 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3118 This would not work if there were only two hosts.
3120 for (size_t x
= 0; x
< 99; x
++)
3122 memcached_autoeject(memc
);
3123 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3124 test_true(server_idx
!= 2);
3127 /* and re-added when it's back. */
3128 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3129 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3130 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3131 memc
->distribution
);
3132 for (size_t x
= 0; x
< 99; x
++)
3134 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3135 // We re-use instance from above.
3137 memcached_server_instance_by_position(memc
, server_idx
);
3138 const char *hostname
= memcached_server_name(instance
);
3139 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3142 memcached_server_list_free(server_pool
);
3143 memcached_free(memc
);
3145 return TEST_SUCCESS
;
3148 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3152 memcached_return_t rc
;
3153 memcached_st
*memc
= memcached_create(NULL
);
3157 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3158 test_true(rc
== MEMCACHED_SUCCESS
);
3160 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3161 test_true(value
== 1);
3163 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3164 test_true(rc
== MEMCACHED_SUCCESS
);
3166 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3167 test_true(value
== MEMCACHED_HASH_MD5
);
3170 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3172 memcached_server_st
*server_pool
;
3173 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");
3174 memcached_server_push(memc
, server_pool
);
3176 // @todo this needs to be refactored to actually test something.
3179 if ((fp
= fopen("ketama_keys.txt", "w")))
3183 printf("cannot write to file ketama_keys.txt");
3184 return TEST_FAILURE
;
3187 for (int x
= 0; x
< 10000; x
++)
3190 snprintf(key
, sizeof(key
), "%d", x
);
3192 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3193 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3194 in_port_t port
= memc
->hosts
[server_idx
].port
;
3195 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3196 memcached_server_instance_st instance
=
3197 memcached_server_instance_by_position(memc
, host_index
);
3201 memcached_server_list_free(server_pool
);
3202 memcached_free(memc
);
3204 return TEST_SUCCESS
;
3208 static test_return_t
result_static(memcached_st
*memc
)
3210 memcached_result_st result
;
3211 memcached_result_st
*result_ptr
;
3213 result_ptr
= memcached_result_create(memc
, &result
);
3214 test_true(result
.options
.is_allocated
== false);
3215 test_true(memcached_is_initialized(&result
) == true);
3216 test_true(result_ptr
);
3217 test_true(result_ptr
== &result
);
3219 memcached_result_free(&result
);
3221 test_true(result
.options
.is_allocated
== false);
3222 test_true(memcached_is_initialized(&result
) == false);
3224 return TEST_SUCCESS
;
3227 static test_return_t
result_alloc(memcached_st
*memc
)
3229 memcached_result_st
*result_ptr
;
3231 result_ptr
= memcached_result_create(memc
, NULL
);
3232 test_true(result_ptr
);
3233 test_true(result_ptr
->options
.is_allocated
== true);
3234 test_true(memcached_is_initialized(result_ptr
) == true);
3235 memcached_result_free(result_ptr
);
3237 return TEST_SUCCESS
;
3240 static test_return_t
string_static_null(memcached_st
*memc
)
3242 memcached_string_st string
;
3243 memcached_string_st
*string_ptr
;
3245 string_ptr
= memcached_string_create(memc
, &string
, 0);
3246 test_true(string
.options
.is_initialized
== true);
3247 test_true(string_ptr
);
3249 /* The following two better be the same! */
3250 test_true(memcached_is_allocated(string_ptr
) == false);
3251 test_true(memcached_is_allocated(&string
) == false);
3252 test_true(&string
== string_ptr
);
3254 test_true(string
.options
.is_initialized
== true);
3255 test_true(memcached_is_initialized(&string
) == true);
3256 memcached_string_free(&string
);
3257 test_true(memcached_is_initialized(&string
) == false);
3259 return TEST_SUCCESS
;
3262 static test_return_t
string_alloc_null(memcached_st
*memc
)
3264 memcached_string_st
*string
;
3266 string
= memcached_string_create(memc
, NULL
, 0);
3268 test_true(memcached_is_allocated(string
) == true);
3269 test_true(memcached_is_initialized(string
) == true);
3270 memcached_string_free(string
);
3272 return TEST_SUCCESS
;
3275 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3277 memcached_string_st
*string
;
3279 string
= memcached_string_create(memc
, NULL
, 1024);
3281 test_true(memcached_is_allocated(string
) == true);
3282 test_true(memcached_is_initialized(string
) == true);
3283 memcached_string_free(string
);
3285 return TEST_SUCCESS
;
3288 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3290 memcached_string_st
*string
;
3292 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3293 test_true(string
== NULL
);
3295 return TEST_SUCCESS
;
3298 static test_return_t
string_alloc_append(memcached_st
*memc
)
3301 char buffer
[SMALL_STRING_LEN
];
3302 memcached_string_st
*string
;
3304 /* Ring the bell! */
3305 memset(buffer
, 6, SMALL_STRING_LEN
);
3307 string
= memcached_string_create(memc
, NULL
, 100);
3309 test_true(memcached_is_allocated(string
) == true);
3310 test_true(memcached_is_initialized(string
) == true);
3312 for (x
= 0; x
< 1024; x
++)
3314 memcached_return_t rc
;
3315 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3316 test_true(rc
== MEMCACHED_SUCCESS
);
3318 test_true(memcached_is_allocated(string
) == true);
3319 memcached_string_free(string
);
3321 return TEST_SUCCESS
;
3324 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3326 memcached_return_t rc
;
3328 char buffer
[SMALL_STRING_LEN
];
3329 memcached_string_st
*string
;
3331 /* Ring the bell! */
3332 memset(buffer
, 6, SMALL_STRING_LEN
);
3334 string
= memcached_string_create(memc
, NULL
, 100);
3336 test_true(memcached_is_allocated(string
) == true);
3337 test_true(memcached_is_initialized(string
) == true);
3339 for (x
= 0; x
< 1024; x
++)
3341 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3342 test_true(rc
== MEMCACHED_SUCCESS
);
3344 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3345 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3346 test_true(memcached_is_allocated(string
) == true);
3347 memcached_string_free(string
);
3349 return TEST_SUCCESS
;
3352 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3355 pairs_free(global_pairs
);
3357 return TEST_SUCCESS
;
3360 static test_return_t
generate_pairs(memcached_st
*memc
)
3363 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3364 global_count
= GLOBAL_COUNT
;
3366 for (size_t x
= 0; x
< global_count
; x
++)
3368 global_keys
[x
]= global_pairs
[x
].key
;
3369 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3372 return TEST_SUCCESS
;
3375 static test_return_t
generate_large_pairs(memcached_st
*memc
)
3378 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3379 global_count
= GLOBAL2_COUNT
;
3381 for (size_t x
= 0; x
< global_count
; x
++)
3383 global_keys
[x
]= global_pairs
[x
].key
;
3384 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3387 return TEST_SUCCESS
;
3390 static test_return_t
generate_data(memcached_st
*memc
)
3392 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3394 test_true(check_execute
== global_count
);
3396 return TEST_SUCCESS
;
3399 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3401 memcached_stat_st
*stat_p
;
3402 memcached_return_t rc
;
3403 uint32_t host_index
= 0;
3404 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3406 test_true(check_execute
== global_count
);
3408 //TODO: hosts used size stats
3409 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3412 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3414 /* This test was changes so that "make test" would work properlly */
3416 memcached_server_instance_st instance
=
3417 memcached_server_instance_by_position(memc
, host_index
);
3419 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3421 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3424 memcached_stat_free(NULL
, stat_p
);
3426 return TEST_SUCCESS
;
3428 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3433 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3434 generate_data(memc
);
3436 return TEST_SUCCESS
;
3439 static test_return_t
get_read_count(memcached_st
*memc
)
3441 memcached_return_t rc
;
3442 memcached_st
*memc_clone
;
3444 memc_clone
= memcached_clone(NULL
, memc
);
3445 test_true(memc_clone
);
3447 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3451 size_t return_value_length
;
3455 for (size_t x
= count
= 0; x
< global_count
; x
++)
3457 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3458 &return_value_length
, &flags
, &rc
);
3459 if (rc
== MEMCACHED_SUCCESS
)
3468 memcached_free(memc_clone
);
3470 return TEST_SUCCESS
;
3473 static test_return_t
get_read(memcached_st
*memc
)
3475 memcached_return_t rc
;
3479 size_t return_value_length
;
3482 for (size_t x
= 0; x
< global_count
; x
++)
3484 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3485 &return_value_length
, &flags
, &rc
);
3487 test_true(return_value);
3488 test_true(rc == MEMCACHED_SUCCESS);
3490 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3495 return TEST_SUCCESS
;
3498 static test_return_t
mget_read(memcached_st
*memc
)
3500 memcached_return_t rc
;
3502 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3503 return TEST_SKIPPED
;
3505 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3507 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3509 // Go fetch the keys and test to see if all of them were returned
3511 size_t keys_returned
;
3512 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3514 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3515 test_true_got(global_count
== keys_returned
, buffer
);
3519 return TEST_SUCCESS
;
3522 static test_return_t
mget_read_result(memcached_st
*memc
)
3524 memcached_return_t rc
;
3526 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3527 return TEST_SKIPPED
;
3529 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3531 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3533 /* Turn this into a help function */
3535 memcached_result_st results_obj
;
3536 memcached_result_st
*results
;
3538 results
= memcached_result_create(memc
, &results_obj
);
3540 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3543 test_true(rc
== MEMCACHED_SUCCESS
);
3546 memcached_result_free(&results_obj
);
3549 return TEST_SUCCESS
;
3552 static test_return_t
mget_read_function(memcached_st
*memc
)
3554 memcached_return_t rc
;
3556 memcached_execute_fn callbacks
[1];
3558 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3559 return TEST_SKIPPED
;
3561 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3563 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3565 callbacks
[0]= &callback_counter
;
3567 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3569 return TEST_SUCCESS
;
3572 static test_return_t
delete_generate(memcached_st
*memc
)
3574 for (size_t x
= 0; x
< global_count
; x
++)
3576 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3579 return TEST_SUCCESS
;
3582 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3587 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3589 for (size_t x
= 0; x
< global_count
; x
++)
3591 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3594 return TEST_SUCCESS
;
3597 static test_return_t
add_host_test1(memcached_st
*memc
)
3599 memcached_return_t rc
;
3600 char servername
[]= "0.example.com";
3601 memcached_server_st
*servers
;
3603 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3605 test_true(1 == memcached_server_list_count(servers
));
3607 for (size_t x
= 2; x
< 20; x
++)
3609 char buffer
[SMALL_STRING_LEN
];
3611 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3612 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3614 test_true(rc
== MEMCACHED_SUCCESS
);
3615 test_true(x
== memcached_server_list_count(servers
));
3618 rc
= memcached_server_push(memc
, servers
);
3619 test_true(rc
== MEMCACHED_SUCCESS
);
3620 rc
= memcached_server_push(memc
, servers
);
3621 test_true(rc
== MEMCACHED_SUCCESS
);
3623 memcached_server_list_free(servers
);
3625 return TEST_SUCCESS
;
3628 static test_return_t
pre_nonblock(memcached_st
*memc
)
3630 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3632 return TEST_SUCCESS
;
3635 static test_return_t
pre_cork(memcached_st
*memc
)
3637 memcached_return_t rc
;
3640 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3643 return TEST_SKIPPED
;
3646 if (rc
== MEMCACHED_SUCCESS
)
3647 return TEST_SUCCESS
;
3649 return TEST_SKIPPED
;
3652 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3659 return TEST_SKIPPED
;
3662 if (rc
!= TEST_SUCCESS
)
3665 return pre_nonblock(memc
);
3668 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3670 memcached_return_t rc
= MEMCACHED_FAILURE
;
3671 memcached_st
*memc_clone
;
3673 memc_clone
= memcached_clone(NULL
, memc
);
3674 test_true(memc_clone
);
3675 // The memcached_version needs to be done on a clone, because the server
3676 // will not toggle protocol on an connection.
3677 memcached_version(memc_clone
);
3679 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3681 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3682 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3683 test_true(rc
== MEMCACHED_SUCCESS
);
3684 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3688 return TEST_SKIPPED
;
3691 memcached_free(memc_clone
);
3693 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3696 static test_return_t
pre_murmur(memcached_st
*memc
)
3698 #ifdef HAVE_MURMUR_HASH
3699 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3700 return TEST_SUCCESS
;
3703 return TEST_SKIPPED
;
3707 static test_return_t
pre_jenkins(memcached_st
*memc
)
3709 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3711 return TEST_SUCCESS
;
3715 static test_return_t
pre_md5(memcached_st
*memc
)
3717 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3719 return TEST_SUCCESS
;
3722 static test_return_t
pre_crc(memcached_st
*memc
)
3724 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3726 return TEST_SUCCESS
;
3729 static test_return_t
pre_hsieh(memcached_st
*memc
)
3731 #ifdef HAVE_HSIEH_HASH
3732 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3733 return TEST_SUCCESS
;
3736 return TEST_SKIPPED
;
3740 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3742 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3744 return TEST_SUCCESS
;
3747 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3749 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3751 return TEST_SUCCESS
;
3754 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3756 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3758 return TEST_SUCCESS
;
3761 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3763 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3765 return TEST_SUCCESS
;
3768 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3770 memcached_return_t rc
;
3773 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3774 test_true(rc
== MEMCACHED_SUCCESS
);
3776 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3777 test_true(value
== 1);
3779 return TEST_SUCCESS
;
3782 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3784 memcached_return_t rc
;
3787 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3788 test_true(rc
== MEMCACHED_SUCCESS
);
3790 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3791 test_true(value
== 1);
3793 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3794 test_true(rc
== MEMCACHED_SUCCESS
);
3796 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3797 test_true(value
== MEMCACHED_HASH_MD5
);
3799 return TEST_SUCCESS
;
3803 @note This should be testing to see if the server really supports the binary protocol.
3805 static test_return_t
pre_binary(memcached_st
*memc
)
3807 memcached_return_t rc
= MEMCACHED_FAILURE
;
3809 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3811 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3812 test_true(rc
== MEMCACHED_SUCCESS
);
3813 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3816 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3819 static test_return_t
pre_sasl(memcached_st
*memc
)
3821 memcached_return_t rc
= MEMCACHED_FAILURE
;
3823 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3824 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3825 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3826 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3828 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3830 memcached_server_st
*servers
= memcached_servers_parse(server
);
3831 test_true(servers
!= NULL
);
3832 memcached_servers_reset(memc
);
3833 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3834 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3835 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3836 test_true(rc
== MEMCACHED_SUCCESS
);
3842 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3845 static test_return_t
pre_replication(memcached_st
*memc
)
3847 test_return_t test_rc
;
3848 test_rc
= pre_binary(memc
);
3850 if (test_rc
!= TEST_SUCCESS
)
3854 * Make sure that we store the item on all servers
3855 * (master + replicas == number of servers)
3857 memcached_return_t rc
;
3858 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3859 memcached_server_count(memc
) - 1);
3860 test_true(rc
== MEMCACHED_SUCCESS
);
3861 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3863 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3867 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3871 rc
= pre_replication(memc
);
3872 if (rc
!= TEST_SUCCESS
)
3875 rc
= pre_nonblock(memc
);
3881 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3885 #ifdef HARD_MALLOC_TESTS
3886 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3894 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3898 #ifdef HARD_MALLOC_TESTS
3899 void *ret
= malloc(size
+ 8);
3902 ret
= (void*)((caddr_t
)ret
+ 8);
3905 void *ret
= malloc(size
);
3910 memset(ret
, 0xff, size
);
3917 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *context
)
3920 #ifdef HARD_MALLOC_TESTS
3921 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3922 void *nmem
= realloc(real_ptr
, size
+ 8);
3927 ret
= (void*)((caddr_t
)nmem
+ 8);
3933 return realloc(mem
, size
);
3938 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *context
)
3941 #ifdef HARD_MALLOC_TESTS
3942 void *mem
= my_malloc(ptr
, nelem
* size
);
3945 memset(mem
, 0, nelem
* size
);
3951 return calloc(nelem
, size
);
3956 static test_return_t
set_prefix(memcached_st
*memc
)
3958 memcached_return_t rc
;
3959 const char *key
= "mine";
3962 /* Make sure be default none exists */
3963 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3964 test_true(rc
== MEMCACHED_FAILURE
);
3966 /* Test a clean set */
3967 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3968 test_true(rc
== MEMCACHED_SUCCESS
);
3970 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3971 test_true(memcmp(value
, key
, 4) == 0);
3972 test_true(rc
== MEMCACHED_SUCCESS
);
3974 /* Test that we can turn it off */
3975 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3976 test_true(rc
== MEMCACHED_SUCCESS
);
3978 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3979 test_true(rc
== MEMCACHED_FAILURE
);
3981 /* Now setup for main test */
3982 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3983 test_true(rc
== MEMCACHED_SUCCESS
);
3985 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3986 test_true(rc
== MEMCACHED_SUCCESS
);
3987 test_true(memcmp(value
, key
, 4) == 0);
3989 /* Set to Zero, and then Set to something too large */
3992 memset(long_key
, 0, 255);
3994 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3995 test_true(rc
== MEMCACHED_SUCCESS
);
3997 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3998 test_true(rc
== MEMCACHED_FAILURE
);
3999 test_true(value
== NULL
);
4001 /* Test a long key for failure */
4002 /* TODO, extend test to determine based on setting, what result should be */
4003 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
4004 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4005 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
4006 test_true(rc
== MEMCACHED_SUCCESS
);
4008 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
4009 strcpy(long_key
, "This is more then the allotted number of characters");
4010 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4011 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
4013 /* Test for a bad prefix, but with a short key */
4014 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
4015 test_true(rc
== MEMCACHED_SUCCESS
);
4017 strcpy(long_key
, "dog cat");
4018 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4019 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
4022 return TEST_SUCCESS
;
4026 #ifdef MEMCACHED_ENABLE_DEPRECATED
4027 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
4029 void *test_ptr
= NULL
;
4032 memcached_malloc_fn malloc_cb
=
4033 (memcached_malloc_fn
)my_malloc
;
4034 cb_ptr
= *(void **)&malloc_cb
;
4035 memcached_return_t rc
;
4037 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
4038 test_true(rc
== MEMCACHED_SUCCESS
);
4039 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
4040 test_true(rc
== MEMCACHED_SUCCESS
);
4041 test_true(test_ptr
== cb_ptr
);
4045 memcached_realloc_fn realloc_cb
=
4046 (memcached_realloc_fn
)my_realloc
;
4047 cb_ptr
= *(void **)&realloc_cb
;
4048 memcached_return_t rc
;
4050 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
4051 test_true(rc
== MEMCACHED_SUCCESS
);
4052 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
4053 test_true(rc
== MEMCACHED_SUCCESS
);
4054 test_true(test_ptr
== cb_ptr
);
4058 memcached_free_fn free_cb
=
4059 (memcached_free_fn
)my_free
;
4060 cb_ptr
= *(void **)&free_cb
;
4061 memcached_return_t rc
;
4063 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
4064 test_true(rc
== MEMCACHED_SUCCESS
);
4065 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
4066 test_true(rc
== MEMCACHED_SUCCESS
);
4067 test_true(test_ptr
== cb_ptr
);
4070 return TEST_SUCCESS
;
4075 static test_return_t
set_memory_alloc(memcached_st
*memc
)
4077 memcached_return_t rc
;
4078 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
4079 my_realloc
, my_calloc
, NULL
);
4080 test_true(rc
== MEMCACHED_FAILURE
);
4082 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
4083 my_realloc
, my_calloc
, NULL
);
4085 memcached_malloc_fn mem_malloc
;
4086 memcached_free_fn mem_free
;
4087 memcached_realloc_fn mem_realloc
;
4088 memcached_calloc_fn mem_calloc
;
4089 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4090 &mem_realloc
, &mem_calloc
);
4092 test_true(mem_malloc
== my_malloc
);
4093 test_true(mem_realloc
== my_realloc
);
4094 test_true(mem_calloc
== my_calloc
);
4095 test_true(mem_free
== my_free
);
4097 return TEST_SUCCESS
;
4100 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4103 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4104 memcached_hash_t hash
;
4105 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4106 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4109 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4110 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4112 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4114 if (hash
!= MEMCACHED_HASH_CRC
)
4115 return TEST_SKIPPED
;
4117 return TEST_SUCCESS
;
4120 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4123 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4124 memcached_hash_t hash
;
4125 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4126 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4129 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4130 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4132 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4134 if (hash
!= MEMCACHED_HASH_HSIEH
)
4135 return TEST_SKIPPED
;
4138 return TEST_SUCCESS
;
4141 static test_return_t
enable_cas(memcached_st
*memc
)
4143 unsigned int set
= 1;
4145 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4147 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4149 return TEST_SUCCESS
;
4152 return TEST_SKIPPED
;
4155 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4157 memcached_version(memc
);
4159 memcached_server_instance_st instance
=
4160 memcached_server_instance_by_position(memc
, 0);
4162 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4163 || instance
->minor_version
> 2)
4165 return TEST_SUCCESS
;
4168 return TEST_SKIPPED
;
4171 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4173 memcached_return_t rc
;
4176 memcached_servers_reset(memc
);
4178 if (stat("/tmp/memcached.socket", &buf
))
4179 return TEST_SKIPPED
;
4181 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4183 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4186 static test_return_t
pre_nodelay(memcached_st
*memc
)
4188 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4189 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4191 return TEST_SUCCESS
;
4194 static test_return_t
pre_settimer(memcached_st
*memc
)
4196 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4197 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4199 return TEST_SUCCESS
;
4202 static test_return_t
poll_timeout(memcached_st
*memc
)
4208 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4210 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4212 test_true(timeout
== 100);
4214 return TEST_SUCCESS
;
4217 static test_return_t
noreply_test(memcached_st
*memc
)
4219 memcached_return_t ret
;
4220 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4221 test_true(ret
== MEMCACHED_SUCCESS
);
4222 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4223 test_true(ret
== MEMCACHED_SUCCESS
);
4224 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4225 test_true(ret
== MEMCACHED_SUCCESS
);
4226 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4227 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4228 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4230 for (int count
=0; count
< 5; ++count
)
4232 for (size_t x
= 0; x
< 100; ++x
)
4235 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4236 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4238 size_t len
= (size_t)check_length
;
4243 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4246 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4249 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4252 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4255 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4261 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4265 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4266 ** API and is _ONLY_ done this way to verify that the library works the
4267 ** way it is supposed to do!!!!
4270 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4272 memcached_server_instance_st instance
=
4273 memcached_server_instance_by_position(memc
, x
);
4274 no_msg
+=(int)(instance
->cursor_active
);
4277 test_true(no_msg
== 0);
4278 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4281 ** Now validate that all items was set properly!
4283 for (size_t x
= 0; x
< 100; ++x
)
4287 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4289 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4291 size_t len
= (size_t)check_length
;
4294 char* value
=memcached_get(memc
, key
, strlen(key
),
4295 &length
, &flags
, &ret
);
4296 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4299 case 0: /* FALLTHROUGH */
4300 case 1: /* FALLTHROUGH */
4302 test_true(strncmp(value
, key
, len
) == 0);
4303 test_true(len
== length
);
4306 test_true(length
== len
* 2);
4309 test_true(length
== len
* 3);
4319 /* Try setting an illegal cas value (should not return an error to
4320 * the caller (because we don't expect a return message from the server)
4322 const char* keys
[]= {"0"};
4323 size_t lengths
[]= {1};
4326 memcached_result_st results_obj
;
4327 memcached_result_st
*results
;
4328 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4329 test_true(ret
== MEMCACHED_SUCCESS
);
4331 results
= memcached_result_create(memc
, &results_obj
);
4333 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4335 test_true(ret
== MEMCACHED_SUCCESS
);
4336 uint64_t cas
= memcached_result_cas(results
);
4337 memcached_result_free(&results_obj
);
4339 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4340 test_true(ret
== MEMCACHED_SUCCESS
);
4343 * The item will have a new cas value, so try to set it again with the old
4344 * value. This should fail!
4346 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4347 test_true(ret
== MEMCACHED_SUCCESS
);
4348 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4349 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4350 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4353 return TEST_SUCCESS
;
4356 static test_return_t
analyzer_test(memcached_st
*memc
)
4358 memcached_return_t rc
;
4359 memcached_stat_st
*memc_stat
;
4360 memcached_analysis_st
*report
;
4362 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4363 test_true(rc
== MEMCACHED_SUCCESS
);
4364 test_true(memc_stat
);
4366 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4367 test_true(rc
== MEMCACHED_SUCCESS
);
4371 memcached_stat_free(NULL
, memc_stat
);
4373 return TEST_SUCCESS
;
4376 /* Count the objects */
4377 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4382 (void)ptr
; (void)key
; (void)key_length
;
4383 size_t *counter
= (size_t *)context
;
4385 *counter
= *counter
+ 1;
4387 return MEMCACHED_SUCCESS
;
4390 static test_return_t
dump_test(memcached_st
*memc
)
4392 memcached_return_t rc
;
4394 memcached_dump_fn callbacks
[1];
4395 test_return_t main_rc
;
4397 callbacks
[0]= &callback_dump_counter
;
4399 /* No support for Binary protocol yet */
4400 if (memc
->flags
.binary_protocol
)
4401 return TEST_SUCCESS
;
4403 main_rc
= set_test3(memc
);
4405 test_true (main_rc
== TEST_SUCCESS
);
4407 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4408 test_true(rc
== MEMCACHED_SUCCESS
);
4410 /* We may have more then 32 if our previous flush has not completed */
4411 test_true(counter
>= 32);
4413 return TEST_SUCCESS
;
4416 #ifdef HAVE_LIBMEMCACHEDUTIL
4417 static void* connection_release(void *arg
)
4420 memcached_pool_st
* pool
;
4425 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4429 #define POOL_SIZE 10
4430 static test_return_t
connection_pool_test(memcached_st
*memc
)
4432 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4433 test_true(pool
!= NULL
);
4434 memcached_st
*mmc
[POOL_SIZE
];
4435 memcached_return_t rc
;
4437 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4439 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4440 test_true(mmc
[x
] != NULL
);
4441 test_true(rc
== MEMCACHED_SUCCESS
);
4444 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4445 test_true(rc
== MEMCACHED_SUCCESS
);
4449 memcached_pool_st
* pool
;
4451 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4452 pthread_create(&tid
, NULL
, connection_release
, &item
);
4453 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4454 test_true(rc
== MEMCACHED_SUCCESS
);
4455 pthread_join(tid
, NULL
);
4456 test_true(mmc
[9] == item
.mmc
);
4457 const char *key
= "key";
4458 size_t keylen
= strlen(key
);
4460 // verify that I can do ops with all connections
4461 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4462 test_true(rc
== MEMCACHED_SUCCESS
);
4464 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4466 uint64_t number_value
;
4467 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4468 test_true(rc
== MEMCACHED_SUCCESS
);
4469 test_true(number_value
== (x
+1));
4473 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4475 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4479 /* verify that I can set behaviors on the pool when I don't have all
4480 * of the connections in the pool. It should however be enabled
4481 * when I push the item into the pool
4483 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4484 test_true(mmc
[0] != NULL
);
4486 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4487 test_true(rc
== MEMCACHED_SUCCESS
);
4489 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4490 test_true(mmc
[1] != NULL
);
4492 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4493 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4494 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4496 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4497 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4498 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4500 test_true(memcached_pool_destroy(pool
) == memc
);
4502 return TEST_SUCCESS
;
4505 static test_return_t
util_version_test(memcached_st
*memc
)
4509 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4510 test_true(if_successful
== true);
4512 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4514 // We expect failure
4517 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4518 fprintf(stderr
, "\nDumping Server Information\n\n");
4519 memcached_server_fn callbacks
[1];
4521 callbacks
[0]= dump_server_information
;
4522 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4523 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4525 test_true(if_successful
== false);
4527 memcached_server_instance_st instance
=
4528 memcached_server_instance_by_position(memc
, 0);
4530 memcached_version(memc
);
4532 // We only use one binary when we test, so this should be just fine.
4533 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4534 test_true(if_successful
== true);
4536 if (instance
->micro_version
> 0)
4537 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4538 else if (instance
->minor_version
> 0)
4539 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4540 else if (instance
->major_version
> 0)
4541 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4543 test_true(if_successful
== true);
4545 if (instance
->micro_version
> 0)
4546 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4547 else if (instance
->minor_version
> 0)
4548 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4549 else if (instance
->major_version
> 0)
4550 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4552 test_true(if_successful
== false);
4554 return TEST_SUCCESS
;
4557 static test_return_t
ping_test(memcached_st
*memc
)
4559 memcached_return_t rc
;
4560 memcached_server_instance_st instance
=
4561 memcached_server_instance_by_position(memc
, 0);
4563 // Test both the version that returns a code, and the one that does not.
4564 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4565 memcached_server_port(instance
), NULL
));
4567 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4568 memcached_server_port(instance
), &rc
));
4570 test_true(rc
== MEMCACHED_SUCCESS
);
4572 return TEST_SUCCESS
;
4576 static test_return_t
replication_set_test(memcached_st
*memc
)
4578 memcached_return_t rc
;
4579 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4580 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4582 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4583 test_true(rc
== MEMCACHED_SUCCESS
);
4586 ** We are using the quiet commands to store the replicas, so we need
4587 ** to ensure that all of them are processed before we can continue.
4588 ** In the test we go directly from storing the object to trying to
4589 ** receive the object from all of the different servers, so we
4590 ** could end up in a race condition (the memcached server hasn't yet
4591 ** processed the quiet command from the replication set when it process
4592 ** the request from the other client (created by the clone)). As a
4593 ** workaround for that we call memcached_quit to send the quit command
4594 ** to the server and wait for the response ;-) If you use the test code
4595 ** as an example for your own code, please note that you shouldn't need
4598 memcached_quit(memc
);
4601 ** "bubba" should now be stored on all of our servers. We don't have an
4602 ** easy to use API to address each individual server, so I'll just iterate
4603 ** through a bunch of "master keys" and I should most likely hit all of the
4606 for (int x
= 'a'; x
<= 'z'; ++x
)
4608 char key
[2]= { [0]= (char)x
};
4611 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4613 test_true(rc
== MEMCACHED_SUCCESS
);
4614 test_true(val
!= NULL
);
4618 memcached_free(memc_clone
);
4620 return TEST_SUCCESS
;
4623 static test_return_t
replication_get_test(memcached_st
*memc
)
4625 memcached_return_t rc
;
4628 * Don't do the following in your code. I am abusing the internal details
4629 * within the library, and this is not a supported interface.
4630 * This is to verify correct behavior in the library
4632 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4634 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4635 memcached_server_instance_st instance
=
4636 memcached_server_instance_by_position(memc_clone
, host
);
4638 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4640 for (int x
= 'a'; x
<= 'z'; ++x
)
4642 char key
[2]= { [0]= (char)x
};
4645 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4647 test_true(rc
== MEMCACHED_SUCCESS
);
4648 test_true(val
!= NULL
);
4652 memcached_free(memc_clone
);
4655 return TEST_SUCCESS
;
4658 static test_return_t
replication_mget_test(memcached_st
*memc
)
4660 memcached_return_t rc
;
4661 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4662 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4664 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4665 size_t len
[]= { 5, 4, 4, 4 };
4667 for (size_t x
= 0; x
< 4; ++x
)
4669 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4670 test_true(rc
== MEMCACHED_SUCCESS
);
4674 ** We are using the quiet commands to store the replicas, so we need
4675 ** to ensure that all of them are processed before we can continue.
4676 ** In the test we go directly from storing the object to trying to
4677 ** receive the object from all of the different servers, so we
4678 ** could end up in a race condition (the memcached server hasn't yet
4679 ** processed the quiet command from the replication set when it process
4680 ** the request from the other client (created by the clone)). As a
4681 ** workaround for that we call memcached_quit to send the quit command
4682 ** to the server and wait for the response ;-) If you use the test code
4683 ** as an example for your own code, please note that you shouldn't need
4686 memcached_quit(memc
);
4689 * Don't do the following in your code. I am abusing the internal details
4690 * within the library, and this is not a supported interface.
4691 * This is to verify correct behavior in the library
4693 memcached_result_st result_obj
;
4694 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4696 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4697 memcached_server_instance_st instance
=
4698 memcached_server_instance_by_position(new_clone
, host
);
4699 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4701 for (int x
= 'a'; x
<= 'z'; ++x
)
4703 char key
[2]= { [0]= (char)x
, [1]= 0 };
4705 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4706 test_true(rc
== MEMCACHED_SUCCESS
);
4708 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4712 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4716 test_true(hits
== 4);
4717 memcached_result_free(&result_obj
);
4720 memcached_free(new_clone
);
4723 memcached_free(memc_clone
);
4725 return TEST_SUCCESS
;
4728 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4730 memcached_result_st result_obj
;
4731 memcached_return_t rc
;
4732 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4733 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4734 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4736 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4737 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4739 for (size_t x
= 0; x
< 7; ++x
)
4741 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4742 test_true(rc
== MEMCACHED_SUCCESS
);
4745 memcached_quit(memc
);
4747 for (size_t x
= 0; x
< 7; ++x
)
4749 const char key
[2]= { [0]= (const char)x
};
4751 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4752 test_true(rc
== MEMCACHED_SUCCESS
);
4754 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4758 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4762 test_true(hits
== 7);
4763 memcached_result_free(&result_obj
);
4765 memcached_free(memc_clone
);
4766 return TEST_SUCCESS
;
4769 static test_return_t
replication_delete_test(memcached_st
*memc
)
4771 memcached_return_t rc
;
4772 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4773 /* Delete the items from all of the servers except 1 */
4774 uint64_t repl
= memcached_behavior_get(memc
,
4775 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4776 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4778 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4779 size_t len
[]= { 5, 4, 4, 4 };
4781 for (size_t x
= 0; x
< 4; ++x
)
4783 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4784 test_true(rc
== MEMCACHED_SUCCESS
);
4788 * Don't do the following in your code. I am abusing the internal details
4789 * within the library, and this is not a supported interface.
4790 * This is to verify correct behavior in the library
4792 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4793 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4795 memcached_server_instance_st instance
=
4796 memcached_server_instance_by_position(memc_clone
, x
);
4798 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4799 if (++hash
== memc_clone
->number_of_hosts
)
4803 memcached_result_st result_obj
;
4804 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4806 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4808 const char key
[2]= { [0]= (const char)x
};
4810 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4811 test_true(rc
== MEMCACHED_SUCCESS
);
4813 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4817 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4821 test_true(hits
== 4);
4822 memcached_result_free(&result_obj
);
4825 memcached_free(memc_clone
);
4827 return TEST_SUCCESS
;
4831 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4835 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4836 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4837 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4838 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4839 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4840 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4841 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4842 #ifdef HAVE_HSIEH_HASH
4843 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4845 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4846 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4847 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4849 return TEST_SUCCESS
;
4853 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4855 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4856 #ifdef HAVE_HSIEH_HASH
4857 expected_rc
= MEMCACHED_SUCCESS
;
4859 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4860 (uint64_t)MEMCACHED_HASH_HSIEH
);
4861 test_true(rc
== expected_rc
);
4863 return TEST_SUCCESS
;
4866 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4868 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4869 #ifdef HAVE_MURMUR_HASH
4870 expected_rc
= MEMCACHED_SUCCESS
;
4872 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4873 (uint64_t)MEMCACHED_HASH_MURMUR
);
4874 test_true(rc
== expected_rc
);
4876 return TEST_SUCCESS
;
4879 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4885 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4889 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4890 test_true(one_at_a_time_values
[x
] == hash_val
);
4893 return TEST_SUCCESS
;
4896 static test_return_t
md5_run (memcached_st
*memc
)
4902 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4906 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4907 test_true(md5_values
[x
] == hash_val
);
4910 return TEST_SUCCESS
;
4913 static test_return_t
crc_run (memcached_st
*memc
)
4919 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4923 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4924 test_true(crc_values
[x
] == hash_val
);
4927 return TEST_SUCCESS
;
4930 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4936 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4940 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4941 test_true(fnv1_64_values
[x
] == hash_val
);
4944 return TEST_SUCCESS
;
4947 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4953 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4957 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4958 test_true(fnv1a_64_values
[x
] == hash_val
);
4961 return TEST_SUCCESS
;
4964 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4970 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4974 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4975 test_true(fnv1_32_values
[x
] == hash_val
);
4978 return TEST_SUCCESS
;
4981 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4987 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4991 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4992 test_true(fnv1a_32_values
[x
] == hash_val
);
4995 return TEST_SUCCESS
;
4998 static test_return_t
hsieh_run (memcached_st
*memc
)
5004 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5008 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
5009 test_true(hsieh_values
[x
] == hash_val
);
5012 return TEST_SUCCESS
;
5015 static test_return_t
murmur_run (memcached_st
*memc
)
5017 #ifdef WORDS_BIGENDIAN
5018 (void)murmur_values
;
5019 return TEST_SKIPPED
;
5025 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5029 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
5030 test_true(murmur_values
[x
] == hash_val
);
5033 return TEST_SUCCESS
;
5037 static test_return_t
jenkins_run (memcached_st
*memc
)
5043 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5047 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
5048 test_true(jenkins_values
[x
] == hash_val
);
5051 return TEST_SUCCESS
;
5054 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
5057 return libhashkit_md5(string
, string_length
);
5060 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
5063 return libhashkit_crc32(string
, string_length
);
5066 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
5070 const hashkit_st
*kit
;
5072 hashkit_return_t hash_rc
;
5074 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};
5075 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};
5077 kit
= memcached_get_hashkit(memc
);
5079 hashkit_clone(&new_kit
, kit
);
5080 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
5081 test_true(hash_rc
== HASHKIT_SUCCESS
);
5083 memcached_set_hashkit(memc
, &new_kit
);
5086 Verify Setting the hash.
5088 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5092 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
5093 test_true(md5_values
[x
] == hash_val
);
5098 Now check memcached_st.
5100 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5104 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
5105 test_true(md5_hosts
[x
] == hash_val
);
5108 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
5109 test_true(hash_rc
== HASHKIT_SUCCESS
);
5111 memcached_set_hashkit(memc
, &new_kit
);
5114 Verify Setting the hash.
5116 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5120 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
5121 test_true(crc_values
[x
] == hash_val
);
5124 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5128 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
5129 test_true(crc_hosts
[x
] == hash_val
);
5132 return TEST_SUCCESS
;
5136 Test case adapted from John Gorman <johngorman2@gmail.com>
5138 We are testing the error condition when we connect to a server via memcached_get()
5139 but find that the server is not available.
5141 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
5144 memcached_st
*tl_memc_h
;
5145 memcached_server_st
*servers
;
5147 const char *key
= "MemcachedLives";
5150 memcached_return rc
;
5154 tl_memc_h
= memcached_create(NULL
);
5155 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5156 memcached_server_push(tl_memc_h
, servers
);
5157 memcached_server_list_free(servers
);
5159 // See if memcached is reachable.
5160 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
5163 test_true(len
== 0);
5164 test_false(rc
== MEMCACHED_SUCCESS
);
5166 memcached_free(tl_memc_h
);
5168 return TEST_SUCCESS
;
5172 We connect to a server which exists, but search for a key that does not exist.
5174 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5176 const char *key
= "MemcachedKeyNotEXIST";
5179 memcached_return rc
;
5182 // See if memcached is reachable.
5183 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
5186 test_true(len
== 0);
5187 test_true(rc
== MEMCACHED_NOTFOUND
);
5189 return TEST_SUCCESS
;
5193 Test case adapted from John Gorman <johngorman2@gmail.com>
5195 We are testing the error condition when we connect to a server via memcached_get_by_key()
5196 but find that the server is not available.
5198 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
5201 memcached_st
*tl_memc_h
;
5202 memcached_server_st
*servers
;
5204 const char *key
= "MemcachedLives";
5207 memcached_return rc
;
5211 tl_memc_h
= memcached_create(NULL
);
5212 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5213 memcached_server_push(tl_memc_h
, servers
);
5214 memcached_server_list_free(servers
);
5216 // See if memcached is reachable.
5217 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5220 test_true(len
== 0);
5221 test_false(rc
== MEMCACHED_SUCCESS
);
5223 memcached_free(tl_memc_h
);
5225 return TEST_SUCCESS
;
5229 We connect to a server which exists, but search for a key that does not exist.
5231 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5233 const char *key
= "MemcachedKeyNotEXIST";
5236 memcached_return rc
;
5239 // See if memcached is reachable.
5240 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5243 test_true(len
== 0);
5244 test_true(rc
== MEMCACHED_NOTFOUND
);
5246 return TEST_SUCCESS
;
5250 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
5252 memcached_return_t rc
;
5255 memcached_server_st
*server_pool
;
5260 memc
= memcached_create(NULL
);
5263 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5264 test_true(rc
== MEMCACHED_SUCCESS
);
5266 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5267 test_true(value
== 1);
5269 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5270 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5273 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");
5274 memcached_server_push(memc
, server_pool
);
5276 /* verify that the server list was parsed okay. */
5277 test_true(memcached_server_count(memc
) == 8);
5278 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5279 test_true(server_pool
[0].port
== 11211);
5280 test_true(server_pool
[0].weight
== 600);
5281 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5282 test_true(server_pool
[2].port
== 11211);
5283 test_true(server_pool
[2].weight
== 200);
5284 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5285 test_true(server_pool
[7].port
== 11211);
5286 test_true(server_pool
[7].weight
== 100);
5288 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5289 * us test the boundary wraparound.
5291 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5293 /* verify the standard ketama set. */
5294 for (x
= 0; x
< 99; x
++)
5296 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5297 memcached_server_instance_st instance
=
5298 memcached_server_instance_by_position(memc
, server_idx
);
5299 const char *hostname
= memcached_server_name(instance
);
5301 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5304 memcached_server_list_free(server_pool
);
5305 memcached_free(memc
);
5307 return TEST_SUCCESS
;
5310 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5312 memcached_return_t rc
;
5315 memcached_server_st
*server_pool
;
5320 memc
= memcached_create(NULL
);
5323 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5324 test_true(rc
== MEMCACHED_SUCCESS
);
5326 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5327 test_true(value
== 1);
5329 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5330 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5332 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");
5333 memcached_server_push(memc
, server_pool
);
5335 /* verify that the server list was parsed okay. */
5336 test_true(memcached_server_count(memc
) == 8);
5337 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5338 test_true(server_pool
[0].port
== 11211);
5339 test_true(server_pool
[0].weight
== 600);
5340 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5341 test_true(server_pool
[2].port
== 11211);
5342 test_true(server_pool
[2].weight
== 200);
5343 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5344 test_true(server_pool
[7].port
== 11211);
5345 test_true(server_pool
[7].weight
== 100);
5347 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5348 * us test the boundary wraparound.
5350 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5352 /* verify the standard ketama set. */
5353 for (x
= 0; x
< 99; x
++)
5355 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5357 memcached_server_instance_st instance
=
5358 memcached_server_instance_by_position(memc
, server_idx
);
5360 const char *hostname
= memcached_server_name(instance
);
5362 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5365 memcached_server_list_free(server_pool
);
5366 memcached_free(memc
);
5368 return TEST_SUCCESS
;
5371 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5373 test_return_t test_rc
;
5374 test_rc
= pre_binary(memc
);
5376 if (test_rc
!= TEST_SUCCESS
)
5379 memcached_return_t ret
;
5380 const char *key
= "regression_bug_434484";
5381 size_t keylen
= strlen(key
);
5383 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5384 test_true(ret
== MEMCACHED_NOTSTORED
);
5386 size_t size
= 2048 * 1024;
5387 void *data
= calloc(1, size
);
5388 test_true(data
!= NULL
);
5389 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5390 test_true(ret
== MEMCACHED_E2BIG
);
5393 return TEST_SUCCESS
;
5396 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5398 test_return_t test_rc
;
5399 test_rc
= pre_binary(memc
);
5401 if (test_rc
!= TEST_SUCCESS
)
5404 memcached_return_t rc
;
5406 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5409 * I only want to hit only _one_ server so I know the number of requests I'm
5410 * sending in the pipleine to the server. Let's try to do a multiget of
5411 * 1024 (that should satisfy most users don't you think?). Future versions
5412 * will include a mget_execute function call if you need a higher number.
5414 uint32_t number_of_hosts
= memcached_server_count(memc
);
5415 memc
->number_of_hosts
= 1;
5416 const size_t max_keys
= 1024;
5417 char **keys
= calloc(max_keys
, sizeof(char*));
5418 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5420 for (size_t x
= 0; x
< max_keys
; ++x
)
5424 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5426 test_true(keys
[x
] != NULL
);
5430 * Run two times.. the first time we should have 100% cache miss,
5431 * and the second time we should have 100% cache hits
5433 for (size_t y
= 0; y
< 2; y
++)
5435 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5436 test_true(rc
== MEMCACHED_SUCCESS
);
5437 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5441 /* The first iteration should give me a 100% cache miss. verify that*/
5442 char blob
[1024]= { 0 };
5444 test_true(counter
== 0);
5446 for (size_t x
= 0; x
< max_keys
; ++x
)
5448 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5449 blob
, sizeof(blob
), 0, 0);
5450 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5455 /* Verify that we received all of the key/value pairs */
5456 test_true(counter
== max_keys
);
5460 /* Release allocated resources */
5461 for (size_t x
= 0; x
< max_keys
; ++x
)
5468 memc
->number_of_hosts
= number_of_hosts
;
5470 return TEST_SUCCESS
;
5473 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5475 memcached_return_t rc
;
5476 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5477 test_true(rc
== MEMCACHED_SUCCESS
);
5479 return regression_bug_434843(memc
);
5482 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5484 memcached_return_t rc
;
5485 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5486 test_true(rc
== MEMCACHED_SUCCESS
);
5488 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5489 test_true(rc
== MEMCACHED_SUCCESS
);
5490 test_true(bytes
!= NULL
);
5491 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5493 test_true(rc
== MEMCACHED_SUCCESS
);
5494 test_true(bytes_read
!= NULL
);
5496 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5497 "bytes_written", &rc
);
5498 test_true(rc
== MEMCACHED_SUCCESS
);
5499 test_true(bytes_written
!= NULL
);
5501 test_true(strcmp(bytes
, bytes_read
) != 0);
5502 test_true(strcmp(bytes
, bytes_written
) != 0);
5504 /* Release allocated resources */
5507 free(bytes_written
);
5508 memcached_stat_free(NULL
, memc_stat
);
5510 return TEST_SUCCESS
;
5514 * The test case isn't obvious so I should probably document why
5515 * it works the way it does. Bug 442914 was caused by a bug
5516 * in the logic in memcached_purge (it did not handle the case
5517 * where the number of bytes sent was equal to the watermark).
5518 * In this test case, create messages so that we hit that case
5519 * and then disable noreply mode and issue a new command to
5520 * verify that it isn't stuck. If we change the format for the
5521 * delete command or the watermarks, we need to update this
5524 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5526 memcached_return_t rc
;
5527 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5528 test_true(rc
== MEMCACHED_SUCCESS
);
5529 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5531 uint32_t number_of_hosts
= memcached_server_count(memc
);
5532 memc
->number_of_hosts
= 1;
5537 for (uint32_t x
= 0; x
< 250; ++x
)
5539 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5540 rc
= memcached_delete(memc
, k
, len
, 0);
5541 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5544 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5547 rc
= memcached_delete(memc
, k
, len
, 0);
5548 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5550 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5551 test_true(rc
== MEMCACHED_SUCCESS
);
5552 rc
= memcached_delete(memc
, k
, len
, 0);
5553 test_true(rc
== MEMCACHED_NOTFOUND
);
5555 memc
->number_of_hosts
= number_of_hosts
;
5557 return TEST_SUCCESS
;
5560 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5562 memcached_server_instance_st instance_one
;
5563 memcached_server_instance_st instance_two
;
5565 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5566 return TEST_SKIPPED
;
5568 memcached_return_t rc
;
5570 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5571 test_true(rc
== MEMCACHED_SUCCESS
);
5573 const size_t max_keys
= 100;
5574 char **keys
= calloc(max_keys
, sizeof(char*));
5575 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5577 for (size_t x
= 0; x
< max_keys
; ++x
)
5581 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5583 test_true(keys
[x
] != NULL
);
5584 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5585 test_true(rc
== MEMCACHED_SUCCESS
);
5589 ** We are using the quiet commands to store the replicas, so we need
5590 ** to ensure that all of them are processed before we can continue.
5591 ** In the test we go directly from storing the object to trying to
5592 ** receive the object from all of the different servers, so we
5593 ** could end up in a race condition (the memcached server hasn't yet
5594 ** processed the quiet command from the replication set when it process
5595 ** the request from the other client (created by the clone)). As a
5596 ** workaround for that we call memcached_quit to send the quit command
5597 ** to the server and wait for the response ;-) If you use the test code
5598 ** as an example for your own code, please note that you shouldn't need
5601 memcached_quit(memc
);
5603 /* Verify that all messages are stored, and we didn't stuff too much
5606 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5607 test_true(rc
== MEMCACHED_SUCCESS
);
5610 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5611 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5612 /* Verify that we received all of the key/value pairs */
5613 test_true(counter
== max_keys
);
5615 memcached_quit(memc
);
5617 * Don't do the following in your code. I am abusing the internal details
5618 * within the library, and this is not a supported interface.
5619 * This is to verify correct behavior in the library. Fake that two servers
5622 instance_one
= memcached_server_instance_by_position(memc
, 0);
5623 instance_two
= memcached_server_instance_by_position(memc
, 2);
5624 in_port_t port0
= instance_one
->port
;
5625 in_port_t port2
= instance_two
->port
;
5627 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5628 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5630 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5631 test_true(rc
== MEMCACHED_SUCCESS
);
5634 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5635 test_true(counter
== (unsigned int)max_keys
);
5637 /* restore the memc handle */
5638 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5639 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5641 memcached_quit(memc
);
5643 /* Remove half of the objects */
5644 for (size_t x
= 0; x
< max_keys
; ++x
)
5648 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5649 test_true(rc
== MEMCACHED_SUCCESS
);
5653 memcached_quit(memc
);
5654 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5655 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5657 /* now retry the command, this time we should have cache misses */
5658 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5659 test_true(rc
== MEMCACHED_SUCCESS
);
5662 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5663 test_true(counter
== (unsigned int)(max_keys
>> 1));
5665 /* Release allocated resources */
5666 for (size_t x
= 0; x
< max_keys
; ++x
)
5673 /* restore the memc handle */
5674 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5675 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5677 return TEST_SUCCESS
;
5680 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5682 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5683 test_true(memc_clone
!= NULL
);
5684 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5686 memcached_server_instance_st instance
=
5687 memcached_server_instance_by_position(memc_clone
, 0);
5689 if (instance
->major_version
> 1 ||
5690 (instance
->major_version
== 1 &&
5691 instance
->minor_version
> 2))
5693 /* Binary protocol doesn't support deferred delete */
5694 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5695 test_true(bin_clone
!= NULL
);
5696 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5697 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5698 memcached_free(bin_clone
);
5700 memcached_quit(memc_clone
);
5702 /* If we know the server version, deferred delete should fail
5703 * with invalid arguments */
5704 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5706 /* If we don't know the server version, we should get a protocol error */
5707 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5709 /* but there is a bug in some of the memcached servers (1.4) that treats
5710 * the counter as noreply so it doesn't send the proper error message
5712 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5714 /* And buffered mode should be disabled and we should get protocol error */
5715 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5716 rc
= memcached_delete(memc
, "foo", 3, 1);
5717 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5719 /* Same goes for noreply... */
5720 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5721 rc
= memcached_delete(memc
, "foo", 3, 1);
5722 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5724 /* but a normal request should go through (and be buffered) */
5725 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5726 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5728 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5729 /* unbuffered noreply should be success */
5730 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5731 /* unbuffered with reply should be not found... */
5732 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5733 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5736 memcached_free(memc_clone
);
5737 return TEST_SUCCESS
;
5741 /* Test memcached_server_get_last_disconnect
5742 * For a working server set, shall be NULL
5743 * For a set of non existing server, shall not be NULL
5745 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5747 memcached_return_t rc
;
5748 memcached_server_instance_st disconnected_server
;
5750 /* With the working set of server */
5751 const char *key
= "marmotte";
5752 const char *value
= "milka";
5754 memcached_reset_last_disconnected_server(memc
);
5755 rc
= memcached_set(memc
, key
, strlen(key
),
5756 value
, strlen(value
),
5757 (time_t)0, (uint32_t)0);
5758 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5760 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5761 test_true(disconnected_server
== NULL
);
5763 /* With a non existing server */
5765 memcached_server_st
*servers
;
5767 const char *server_list
= "localhost:9";
5769 servers
= memcached_servers_parse(server_list
);
5771 mine
= memcached_create(NULL
);
5772 rc
= memcached_server_push(mine
, servers
);
5773 test_true(rc
== MEMCACHED_SUCCESS
);
5774 memcached_server_list_free(servers
);
5777 rc
= memcached_set(mine
, key
, strlen(key
),
5778 value
, strlen(value
),
5779 (time_t)0, (uint32_t)0);
5780 test_true(rc
!= MEMCACHED_SUCCESS
);
5782 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5783 if (disconnected_server
== NULL
)
5785 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5788 test_true(disconnected_server
!= NULL
);
5789 test_true(memcached_server_port(disconnected_server
)== 9);
5790 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5792 memcached_quit(mine
);
5793 memcached_free(mine
);
5795 return TEST_SUCCESS
;
5798 static test_return_t
test_verbosity(memcached_st
*memc
)
5800 memcached_verbosity(memc
, 3);
5802 return TEST_SUCCESS
;
5805 static test_return_t
test_server_failure(memcached_st
*memc
)
5807 memcached_st
*local_memc
;
5808 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5810 local_memc
= memcached_create(NULL
);
5812 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5813 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5815 uint32_t server_count
= memcached_server_count(local_memc
);
5817 test_true(server_count
== 1);
5819 // Disable the server
5820 instance
= memcached_server_instance_by_position(local_memc
, 0);
5821 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5823 memcached_return_t rc
;
5824 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5826 (time_t)0, (uint32_t)0);
5827 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5829 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5830 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5832 (time_t)0, (uint32_t)0);
5833 test_true(rc
== MEMCACHED_SUCCESS
);
5836 memcached_free(local_memc
);
5838 return TEST_SUCCESS
;
5841 static test_return_t
test_cull_servers(memcached_st
*memc
)
5843 uint32_t count
= memcached_server_count(memc
);
5845 // Do not do this in your code, it is not supported.
5846 memc
->servers
[1].state
.is_dead
= true;
5847 memc
->state
.is_time_for_rebuild
= true;
5849 uint32_t new_count
= memcached_server_count(memc
);
5850 test_true(count
== new_count
);
5853 test_true(count
== new_count
+ 1 );
5856 return TEST_SUCCESS
;
5860 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5861 const char *key
, size_t key_length
,
5862 const char *value
, size_t value_length
,
5872 return MEMCACHED_SUCCESS
;
5875 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5877 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5878 test_true(rc
== MEMCACHED_SUCCESS
);
5880 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5881 test_true(rc
== MEMCACHED_SUCCESS
);
5883 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5884 test_true(rc
== MEMCACHED_SUCCESS
);
5886 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5887 test_true(rc
== MEMCACHED_SUCCESS
);
5889 return TEST_SUCCESS
;
5893 * This test ensures that the failure counter isn't incremented during
5894 * normal termination of the memcached instance.
5896 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5898 memcached_return_t rc
;
5899 memcached_server_instance_st instance
;
5901 /* Set value to force connection to the server */
5902 const char *key
= "marmotte";
5903 const char *value
= "milka";
5906 * Please note that I'm abusing the internal structures in libmemcached
5907 * in a non-portable way and you shouldn't be doing this. I'm only
5908 * doing this in order to verify that the library works the way it should
5910 uint32_t number_of_hosts
= memcached_server_count(memc
);
5911 memc
->number_of_hosts
= 1;
5913 /* Ensure that we are connected to the server by setting a value */
5914 rc
= memcached_set(memc
, key
, strlen(key
),
5915 value
, strlen(value
),
5916 (time_t)0, (uint32_t)0);
5917 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5920 instance
= memcached_server_instance_by_position(memc
, 0);
5921 /* The test is to see that the memcached_quit doesn't increase the
5922 * the server failure conter, so let's ensure that it is zero
5923 * before sending quit
5925 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5927 memcached_quit(memc
);
5929 /* Verify that it memcached_quit didn't increment the failure counter
5930 * Please note that this isn't bullet proof, because an error could
5933 test_true(instance
->server_failure_counter
== 0);
5935 /* restore the instance */
5936 memc
->number_of_hosts
= number_of_hosts
;
5938 return TEST_SUCCESS
;
5942 * This tests ensures expected disconnections (for some behavior changes
5943 * for instance) do not wrongly increase failure counter
5945 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5947 memcached_return rc
;
5949 memcached_st
*memc_clone
;
5950 memc_clone
= memcached_clone(NULL
, memc
);
5951 test_true(memc_clone
);
5953 /* Set value to force connection to the server */
5954 const char *key
= "marmotte";
5955 const char *value
= "milka";
5956 char *string
= NULL
;
5957 size_t string_length
;
5960 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5961 value
, strlen(value
),
5962 (time_t)0, (uint32_t)0);
5963 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5966 /* put failure limit to 1 */
5967 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5968 assert(rc
== MEMCACHED_SUCCESS
);
5969 /* Put a retry timeout to effectively activate failure_limit effect */
5970 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5971 assert(rc
== MEMCACHED_SUCCESS
);
5972 /* change behavior that triggers memcached_quit()*/
5973 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5974 assert(rc
== MEMCACHED_SUCCESS
);
5977 /* Check if we still are connected */
5978 string
= memcached_get(memc_clone
, key
, strlen(key
),
5979 &string_length
, &flags
, &rc
);
5981 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5985 return TEST_SUCCESS
;
5992 * Test that ensures mget_execute does not end into recursive calls that finally fails
5994 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5996 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5997 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5998 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5999 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
6000 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
6003 return TEST_SKIPPED
; // My MAC can't handle this test
6007 * I only want to hit _one_ server so I know the number of requests I'm
6008 * sending in the pipeline.
6010 uint32_t number_of_hosts
= memc
->number_of_hosts
;
6011 memc
->number_of_hosts
= 1;
6012 size_t max_keys
= 20480;
6015 char **keys
= calloc(max_keys
, sizeof(char*));
6016 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
6018 /* First add all of the items.. */
6020 char blob
[1024]= { 0 };
6021 memcached_return rc
;
6022 for (size_t x
= 0; x
< max_keys
; ++x
)
6025 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
6027 assert(keys
[x
] != NULL
);
6028 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
6030 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
6032 break; // We are out of business
6035 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
6037 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
6040 sleep(1);// We will try to sleep
6043 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
6045 // We failed to send everything.
6050 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
6053 /* Try to get all of them with a large multiget */
6055 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
6056 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
6057 (size_t)max_keys
, callbacks
, &counter
, 1);
6059 assert(rc
== MEMCACHED_SUCCESS
);
6060 char* the_value
= NULL
;
6061 char the_key
[MEMCACHED_MAX_KEY
];
6062 size_t the_key_length
;
6063 size_t the_value_length
;
6067 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
6069 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
6075 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
6078 assert(rc
== MEMCACHED_END
);
6080 /* Verify that we got all of the items */
6081 assert(counter
== max_keys
);
6084 /* Release all allocated resources */
6085 for (size_t x
= 0; x
< max_keys
; ++x
)
6092 memc
->number_of_hosts
= number_of_hosts
;
6094 return TEST_SUCCESS
;
6097 static test_return_t
regression_bug_583031(memcached_st
*unused
)
6101 memcached_st
*memc
= memcached_create(NULL
);
6103 memcached_server_add(memc
, "10.2.3.4", 11211);
6105 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
6106 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
6107 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
6108 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
6109 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
6110 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
6112 memcached_return_t rc
;
6116 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
6118 test_true_got(rc
== MEMCACHED_TIMEOUT
, memcached_strerror(NULL
, rc
));
6120 memcached_free(memc
);
6122 return TEST_SUCCESS
;
6125 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
6127 fprintf(stderr
, "Iteration #%u: ", it
);
6129 if(error
== MEMCACHED_ERRNO
)
6131 fprintf(stderr
, "system error %d from %s: %s\n",
6132 errno
, what
, strerror(errno
));
6136 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
6137 memcached_strerror(mc
, error
));
6141 #define TEST_CONSTANT_CREATION 200
6143 static test_return_t
regression_bug_(memcached_st
*memc
)
6145 const char *remote_server
;
6148 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
6150 return TEST_SKIPPED
;
6153 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
6155 memcached_st
* mc
= memcached_create(NULL
);
6156 memcached_return rc
;
6158 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
6159 if (rc
!= MEMCACHED_SUCCESS
)
6161 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
6164 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
6165 if (rc
!= MEMCACHED_SUCCESS
)
6167 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
6170 rc
= memcached_server_add(mc
, remote_server
, 0);
6171 if (rc
!= MEMCACHED_SUCCESS
)
6173 memcached_die(mc
, rc
, "memcached_server_add", x
);
6176 const char *set_key
= "akey";
6177 const size_t set_key_len
= strlen(set_key
);
6178 const char *set_value
= "a value";
6179 const size_t set_value_len
= strlen(set_value
);
6181 if (rc
== MEMCACHED_SUCCESS
)
6185 size_t get_value_len
;
6187 uint32_t get_value_flags
;
6189 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
6190 &get_value_flags
, &rc
);
6191 if (rc
!= MEMCACHED_SUCCESS
)
6193 memcached_die(mc
, rc
, "memcached_get", x
);
6199 (get_value_len
!= set_value_len
6200 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
6202 fprintf(stderr
, "Values don't match?\n");
6203 rc
= MEMCACHED_FAILURE
;
6209 rc
= memcached_set(mc
,
6210 set_key
, set_key_len
,
6211 set_value
, set_value_len
,
6215 if (rc
!= MEMCACHED_SUCCESS
)
6217 memcached_die(mc
, rc
, "memcached_set", x
);
6224 if (rc
!= MEMCACHED_SUCCESS
)
6230 return TEST_SUCCESS
;
6234 * Test that the sasl authentication works. We cannot use the default
6235 * pool of servers, because that would require that all servers we want
6236 * to test supports SASL authentication, and that they use the default
6239 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6241 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6242 memcached_return_t rc
;
6244 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6245 test_true(rc
== MEMCACHED_SUCCESS
);
6246 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6247 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6248 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6249 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6250 memcached_quit(memc
);
6252 rc
= memcached_set_sasl_auth_data(memc
,
6253 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6254 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6255 test_true(rc
== MEMCACHED_SUCCESS
);
6257 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6258 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6259 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6261 memcached_quit(memc
);
6262 return TEST_SUCCESS
;
6265 return TEST_FAILURE
;
6269 /* Clean the server before beginning testing */
6271 {"util_version", 1, (test_callback_fn
)util_version_test
},
6272 {"flush", 0, (test_callback_fn
)flush_test
},
6273 {"init", 0, (test_callback_fn
)init_test
},
6274 {"allocation", 0, (test_callback_fn
)allocation_test
},
6275 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6276 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6277 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6278 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6279 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6280 {"clone_test", 0, (test_callback_fn
)clone_test
},
6281 {"connection_test", 0, (test_callback_fn
)connection_test
},
6282 {"callback_test", 0, (test_callback_fn
)callback_test
},
6283 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6284 {"error", 0, (test_callback_fn
)error_test
},
6285 {"set", 0, (test_callback_fn
)set_test
},
6286 {"set2", 0, (test_callback_fn
)set_test2
},
6287 {"set3", 0, (test_callback_fn
)set_test3
},
6288 {"dump", 1, (test_callback_fn
)dump_test
},
6289 {"add", 1, (test_callback_fn
)add_test
},
6290 {"replace", 1, (test_callback_fn
)replace_test
},
6291 {"delete", 1, (test_callback_fn
)delete_test
},
6292 {"get", 1, (test_callback_fn
)get_test
},
6293 {"get2", 0, (test_callback_fn
)get_test2
},
6294 {"get3", 0, (test_callback_fn
)get_test3
},
6295 {"get4", 0, (test_callback_fn
)get_test4
},
6296 {"partial mget", 0, (test_callback_fn
)get_test5
},
6297 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6298 {"increment", 0, (test_callback_fn
)increment_test
},
6299 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6300 {"decrement", 0, (test_callback_fn
)decrement_test
},
6301 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6302 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6303 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6304 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6305 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6306 {"quit", 0, (test_callback_fn
)quit_test
},
6307 {"mget", 1, (test_callback_fn
)mget_test
},
6308 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6309 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6310 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6311 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6312 {"mget_end", 0, (test_callback_fn
)mget_end
},
6313 {"get_stats", 0, (test_callback_fn
)get_stats
},
6314 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6315 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6316 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6317 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6318 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6319 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6320 {"read_through", 1, (test_callback_fn
)read_through
},
6321 {"delete_through", 1, (test_callback_fn
)delete_through
},
6322 {"noreply", 1, (test_callback_fn
)noreply_test
},
6323 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6324 #ifdef HAVE_LIBMEMCACHEDUTIL
6325 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6326 {"ping", 1, (test_callback_fn
)ping_test
},
6328 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6329 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6330 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6331 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6332 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
6336 test_st behavior_tests
[] ={
6337 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6338 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6339 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6340 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6344 test_st regression_binary_vs_block
[] ={
6345 {"block add", 1, (test_callback_fn
)block_add_regression
},
6346 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6350 test_st async_tests
[] ={
6351 {"add", 1, (test_callback_fn
)add_wrapper
},
6355 test_st string_tests
[] ={
6356 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6357 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6358 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6359 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6360 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6361 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6362 {0, 0, (test_callback_fn
)0}
6365 test_st result_tests
[] ={
6366 {"result static", 0, (test_callback_fn
)result_static
},
6367 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6368 {0, 0, (test_callback_fn
)0}
6371 test_st version_1_2_3
[] ={
6372 {"append", 0, (test_callback_fn
)append_test
},
6373 {"prepend", 0, (test_callback_fn
)prepend_test
},
6374 {"cas", 0, (test_callback_fn
)cas_test
},
6375 {"cas2", 0, (test_callback_fn
)cas2_test
},
6376 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6377 {0, 0, (test_callback_fn
)0}
6380 test_st user_tests
[] ={
6381 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6382 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6383 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6384 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6385 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6386 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6387 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6388 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6389 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6390 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6391 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6392 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6393 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6394 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6395 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6396 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6397 #if !defined(__sun) && !defined(__OpenBSD__)
6399 ** It seems to be something weird with the character sets..
6400 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6401 ** guess I need to find out how this is supposed to work.. Perhaps I need
6402 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6403 ** so just disable the code for now...).
6405 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6407 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6408 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6409 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6410 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6411 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6412 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6413 {0, 0, (test_callback_fn
)0}
6416 test_st replication_tests
[]= {
6417 {"set", 1, (test_callback_fn
)replication_set_test
},
6418 {"get", 0, (test_callback_fn
)replication_get_test
},
6419 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6420 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6421 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6422 {0, 0, (test_callback_fn
)0}
6426 * The following test suite is used to verify that we don't introduce
6427 * regression bugs. If you want more information about the bug / test,
6428 * you should look in the bug report at
6429 * http://bugs.launchpad.net/libmemcached
6431 test_st regression_tests
[]= {
6432 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6433 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6434 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6435 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6436 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6437 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6438 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6439 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6440 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6441 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6442 {0, 0, (test_callback_fn
)0}
6445 test_st sasl_auth_tests
[]= {
6446 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6447 {0, 0, (test_callback_fn
)0}
6450 test_st ketama_compatibility
[]= {
6451 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6452 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6453 {0, 0, (test_callback_fn
)0}
6456 test_st generate_tests
[] ={
6457 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6458 {"generate_data", 1, (test_callback_fn
)generate_data
},
6459 {"get_read", 0, (test_callback_fn
)get_read
},
6460 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6461 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6462 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6463 {"generate_data", 1, (test_callback_fn
)generate_data
},
6464 {"mget_read", 0, (test_callback_fn
)mget_read
},
6465 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6466 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6467 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6468 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6469 {"generate_data", 1, (test_callback_fn
)generate_data
},
6470 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6471 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6472 {0, 0, (test_callback_fn
)0}
6475 test_st consistent_tests
[] ={
6476 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6477 {"generate_data", 1, (test_callback_fn
)generate_data
},
6478 {"get_read", 0, (test_callback_fn
)get_read_count
},
6479 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6480 {0, 0, (test_callback_fn
)0}
6483 test_st consistent_weighted_tests
[] ={
6484 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6485 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6486 {"get_read", 0, (test_callback_fn
)get_read_count
},
6487 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6488 {0, 0, (test_callback_fn
)0}
6491 test_st hsieh_availability
[] ={
6492 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6493 {0, 0, (test_callback_fn
)0}
6496 test_st murmur_availability
[] ={
6497 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6498 {0, 0, (test_callback_fn
)0}
6502 test_st hash_sanity
[] ={
6503 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6504 {0, 0, (test_callback_fn
)0}
6508 test_st ketama_auto_eject_hosts
[] ={
6509 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6510 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6511 {0, 0, (test_callback_fn
)0}
6514 test_st hash_tests
[] ={
6515 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6516 {"md5", 0, (test_callback_fn
)md5_run
},
6517 {"crc", 0, (test_callback_fn
)crc_run
},
6518 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6519 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6520 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6521 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6522 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6523 {"murmur", 0, (test_callback_fn
)murmur_run
},
6524 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6525 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6526 {0, 0, (test_callback_fn
)0}
6529 test_st error_conditions
[] ={
6530 {"memcached_get_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6531 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6532 {"memcached_get_by_key_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6533 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6534 {0, 0, (test_callback_fn
)0}
6537 collection_st collection
[] ={
6539 {"hash_sanity", 0, 0, hash_sanity
},
6541 {"hsieh_availability", 0, 0, hsieh_availability
},
6542 {"murmur_availability", 0, 0, murmur_availability
},
6543 {"block", 0, 0, tests
},
6544 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6545 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6546 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6547 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6548 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6549 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6550 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6551 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6552 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6553 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6554 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6555 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6556 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6557 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6558 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6559 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6560 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6561 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6562 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6563 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6564 #ifdef MEMCACHED_ENABLE_DEPRECATED
6565 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6567 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6568 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6569 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6570 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6571 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6572 {"string", 0, 0, string_tests
},
6573 {"result", 0, 0, result_tests
},
6574 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6575 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6576 {"user", 0, 0, user_tests
},
6577 {"generate", 0, 0, generate_tests
},
6578 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6579 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6580 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6581 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6582 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6583 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6584 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6586 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6587 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6588 {"consistent_not", 0, 0, consistent_tests
},
6589 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6590 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6591 {"ketama_compat", 0, 0, ketama_compatibility
},
6592 {"test_hashes", 0, 0, hash_tests
},
6593 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6594 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6595 {"regression", 0, 0, regression_tests
},
6596 {"behaviors", 0, 0, behavior_tests
},
6597 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6598 {"error_conditions", 0, 0, error_conditions
},
6602 #define SERVERS_TO_CREATE 5
6604 #include "libmemcached_world.h"
6606 void get_world(world_st
*world
)
6608 world
->collections
= collection
;
6610 world
->create
= (test_callback_create_fn
)world_create
;
6611 world
->destroy
= (test_callback_fn
)world_destroy
;
6613 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6614 world
->test
.flush
= (test_callback_fn
)world_flush
;
6615 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6616 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6617 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6619 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6620 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6622 world
->runner
= &defualt_libmemcached_runner
;