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"
29 #include <libtest/server.h>
31 #include "clients/generator.h"
32 #include "clients/execute.h"
34 #define SMALL_STRING_LEN 1024
36 #include <libtest/test.h>
37 #include "tests/parser.h"
38 #include "tests/string.h"
39 #include "tests/replication.h"
40 #include "tests/basic.h"
41 #include "tests/error_conditions.h"
42 #include "tests/print.h"
45 #ifdef HAVE_LIBMEMCACHEDUTIL
47 #include "libmemcached/memcached_util.h"
50 #include "hash_results.h"
52 #define GLOBAL_COUNT 10000
53 #define GLOBAL2_COUNT 100
54 #define SERVERS_TO_CREATE 5
55 static uint32_t global_count
;
57 static pairs_st
*global_pairs
;
58 static const char *global_keys
[GLOBAL_COUNT
];
59 static size_t global_keys_length
[GLOBAL_COUNT
];
62 static test_return_t
pre_binary(memcached_st
*memc
);
65 static test_return_t
init_test(memcached_st
*not_used
)
70 (void)memcached_create(&memc
);
71 memcached_free(&memc
);
76 static test_return_t
server_list_null_test(memcached_st
*ptr
)
78 memcached_server_st
*server_list
;
79 memcached_return_t rc
;
82 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
83 test_true(server_list
== NULL
);
85 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
86 test_true(server_list
== NULL
);
88 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
89 test_true(server_list
== NULL
);
94 #define TEST_PORT_COUNT 7
95 in_port_t test_ports
[TEST_PORT_COUNT
];
97 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
98 const memcached_server_st
*server
,
102 size_t bigger
= *((size_t *)(context
));
104 assert(bigger
<= memcached_server_port(server
));
105 *((size_t *)(context
))= memcached_server_port(server
);
107 return MEMCACHED_SUCCESS
;
110 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
111 const memcached_server_st
*instance
,
115 FILE *stream
= (FILE *)context
;
118 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
119 memcached_server_name(instance
),
120 memcached_server_port(instance
),
121 instance
->major_version
,
122 instance
->minor_version
,
123 instance
->micro_version
);
125 return MEMCACHED_SUCCESS
;
128 static test_return_t
server_sort_test(memcached_st
*ptr
)
130 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
132 memcached_return_t rc
;
133 memcached_server_fn callbacks
[1];
134 memcached_st
*local_memc
;
137 local_memc
= memcached_create(NULL
);
138 test_true(local_memc
);
139 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
141 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
143 test_ports
[x
]= (in_port_t
)random() % 64000;
144 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
145 test_true(memcached_server_count(local_memc
) == x
+ 1);
147 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
149 test_true(rc
== MEMCACHED_SUCCESS
);
152 callbacks
[0]= server_display_function
;
153 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
156 memcached_free(local_memc
);
161 static test_return_t
server_sort2_test(memcached_st
*ptr
)
163 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
164 memcached_return_t rc
;
165 memcached_server_fn callbacks
[1];
166 memcached_st
*local_memc
;
167 memcached_server_instance_st instance
;
170 local_memc
= memcached_create(NULL
);
171 test_true(local_memc
);
172 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
173 test_true(rc
== MEMCACHED_SUCCESS
);
175 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
176 test_true(rc
== MEMCACHED_SUCCESS
);
177 instance
= memcached_server_instance_by_position(local_memc
, 0);
178 test_true(memcached_server_port(instance
) == 43043);
180 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
181 test_true(rc
== MEMCACHED_SUCCESS
);
183 instance
= memcached_server_instance_by_position(local_memc
, 0);
184 test_true(memcached_server_port(instance
) == 43042);
186 instance
= memcached_server_instance_by_position(local_memc
, 1);
187 test_true(memcached_server_port(instance
) == 43043);
189 callbacks
[0]= server_display_function
;
190 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
193 memcached_free(local_memc
);
198 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
)
200 memcached_return_t rc
;
201 memcached_st local_memc
;
203 memcached_server_st
*servers
;
204 memcached_server_fn callbacks
[1];
206 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";
209 memc
= memcached_create(&local_memc
);
211 servers
= memcached_servers_parse(server_string
);
213 rc
= memcached_server_push(memc
, servers
);
214 memcached_server_list_free(servers
);
216 callbacks
[0]= server_print_callback
;
217 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
219 memcached_free(memc
);
224 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
,
225 const memcached_server_st
*server
,
229 uint32_t x
= *((uint32_t *)(context
));
232 assert(test_ports
[x
] == server
->port
);
233 *((uint32_t *)(context
))= ++x
;
235 return MEMCACHED_SUCCESS
;
238 static test_return_t
server_unsort_test(memcached_st
*ptr
)
240 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
241 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
242 memcached_return_t rc
;
243 memcached_server_fn callbacks
[1];
244 memcached_st
*local_memc
;
247 local_memc
= memcached_create(NULL
);
248 test_true(local_memc
);
250 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
252 test_ports
[x
]= (in_port_t
)(random() % 64000);
253 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
254 test_true(memcached_server_count(local_memc
) == x
+1);
256 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
258 test_true(rc
== MEMCACHED_SUCCESS
);
261 callbacks
[0]= server_display_unsort_function
;
262 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
264 /* Now we sort old data! */
265 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
266 callbacks
[0]= server_display_function
;
267 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
270 memcached_free(local_memc
);
275 static test_return_t
allocation_test(memcached_st
*not_used
)
279 memc
= memcached_create(NULL
);
281 memcached_free(memc
);
286 static test_return_t
clone_test(memcached_st
*memc
)
290 memcached_st
*memc_clone
;
291 memc_clone
= memcached_clone(NULL
, NULL
);
292 test_true(memc_clone
);
293 memcached_free(memc_clone
);
296 /* Can we init from null? */
298 memcached_st
*memc_clone
;
299 memc_clone
= memcached_clone(NULL
, memc
);
300 test_true(memc_clone
);
303 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
304 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
305 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
306 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
309 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
310 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
311 test_true(memc_clone
->distribution
== memc
->distribution
);
312 { // Test all of the flags
313 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
314 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
315 test_true(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
316 test_true(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
317 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
318 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
319 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
320 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
321 test_true(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
322 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
323 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
324 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
325 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
326 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
327 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
329 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
330 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
331 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
332 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
333 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
334 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
335 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
336 test_true(memc_clone
->on_clone
== memc
->on_clone
);
337 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
338 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
339 test_true(memc_clone
->recv_size
== memc
->recv_size
);
340 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
341 test_true(memc_clone
->send_size
== memc
->send_size
);
342 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
343 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
344 test_true(memc_clone
->user_data
== memc
->user_data
);
346 memcached_free(memc_clone
);
349 /* Can we init from struct? */
351 memcached_st declared_clone
;
352 memcached_st
*memc_clone
;
353 memset(&declared_clone
, 0 , sizeof(memcached_st
));
354 memc_clone
= memcached_clone(&declared_clone
, NULL
);
355 test_true(memc_clone
);
356 memcached_free(memc_clone
);
359 /* Can we init from struct? */
361 memcached_st declared_clone
;
362 memcached_st
*memc_clone
;
363 memset(&declared_clone
, 0 , sizeof(memcached_st
));
364 memc_clone
= memcached_clone(&declared_clone
, memc
);
365 test_true(memc_clone
);
366 memcached_free(memc_clone
);
372 static test_return_t
userdata_test(memcached_st
*memc
)
375 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
376 test_true(memcached_get_user_data(memc
) == foo
);
377 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
382 static test_return_t
connection_test(memcached_st
*memc
)
384 memcached_return_t rc
;
386 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
387 test_true(rc
== MEMCACHED_SUCCESS
);
392 static test_return_t
error_test(memcached_st
*memc
)
394 memcached_return_t rc
;
395 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
396 982370485U, 1263635348U, 4242906218U, 3829656100U,
397 1891735253U, 334139633U, 2257084983U, 3088286104U,
398 13199785U, 2542027183U, 1097051614U, 199566778U,
399 2748246961U, 2465192557U, 1664094137U, 2405439045U,
400 1842224848U, 692413798U, 3479807801U, 919913813U,
401 4269430871U, 610793021U, 527273862U, 1437122909U,
402 2300930706U, 2943759320U, 674306647U, 2400528935U,
403 54481931U, 4186304426U, 1741088401U, 2979625118U,
404 4159057246U, 3425930182U, 2593724503U, 1868899624U,
405 1769812374U, 2302537950U, 1110330676U, 3365377466U,
406 1336171666U, 3365377466U };
408 // You have updated the memcache_error messages but not updated docs/tests.
409 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
412 const char *msg
= memcached_strerror(memc
, rc
);
413 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
414 MEMCACHED_HASH_JENKINS
);
415 if (values
[rc
] != hash_val
)
417 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
418 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
420 test_true(values
[rc
] == hash_val
);
422 test_true(MEMCACHED_MAXIMUM_RETURN
== 45);
427 static test_return_t
set_test(memcached_st
*memc
)
429 memcached_return_t rc
;
430 const char *key
= "foo";
431 const char *value
= "when we sanitize";
433 rc
= memcached_set(memc
, key
, strlen(key
),
434 value
, strlen(value
),
435 (time_t)0, (uint32_t)0);
436 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
441 static test_return_t
append_test(memcached_st
*memc
)
443 memcached_return_t rc
;
444 const char *key
= "fig";
445 const char *in_value
= "we";
446 char *out_value
= NULL
;
450 rc
= memcached_flush(memc
, 0);
451 test_true(rc
== MEMCACHED_SUCCESS
);
453 rc
= memcached_set(memc
, key
, strlen(key
),
454 in_value
, strlen(in_value
),
455 (time_t)0, (uint32_t)0);
456 test_true(rc
== MEMCACHED_SUCCESS
);
458 rc
= memcached_append(memc
, key
, strlen(key
),
459 " the", strlen(" the"),
460 (time_t)0, (uint32_t)0);
461 test_true(rc
== MEMCACHED_SUCCESS
);
463 rc
= memcached_append(memc
, key
, strlen(key
),
464 " people", strlen(" people"),
465 (time_t)0, (uint32_t)0);
466 test_true(rc
== MEMCACHED_SUCCESS
);
468 out_value
= memcached_get(memc
, key
, strlen(key
),
469 &value_length
, &flags
, &rc
);
470 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
471 test_true(strlen("we the people") == value_length
);
472 test_true(rc
== MEMCACHED_SUCCESS
);
478 static test_return_t
append_binary_test(memcached_st
*memc
)
480 memcached_return_t rc
;
481 const char *key
= "numbers";
482 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
488 rc
= memcached_flush(memc
, 0);
489 test_true(rc
== MEMCACHED_SUCCESS
);
491 rc
= memcached_set(memc
,
494 (time_t)0, (uint32_t)0);
495 test_true(rc
== MEMCACHED_SUCCESS
);
497 for (x
= 0; store_list
[x
] ; x
++)
499 rc
= memcached_append(memc
,
501 (char *)&store_list
[x
], sizeof(uint32_t),
502 (time_t)0, (uint32_t)0);
503 test_true(rc
== MEMCACHED_SUCCESS
);
506 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
507 &value_length
, &flags
, &rc
);
508 test_true((value_length
== (sizeof(uint32_t) * x
)));
509 test_true(rc
== MEMCACHED_SUCCESS
);
511 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
513 test_true(*ptr
== store_list
[x
- counter
]);
521 static test_return_t
cas2_test(memcached_st
*memc
)
523 memcached_return_t rc
;
524 const char *keys
[]= {"fudge", "son", "food"};
525 size_t key_length
[]= {5, 3, 4};
526 const char *value
= "we the people";
527 size_t value_length
= strlen("we the people");
529 memcached_result_st results_obj
;
530 memcached_result_st
*results
;
533 rc
= memcached_flush(memc
, 0);
534 test_true(rc
== MEMCACHED_SUCCESS
);
536 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
538 for (x
= 0; x
< 3; x
++)
540 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
541 keys
[x
], key_length
[x
],
542 (time_t)50, (uint32_t)9);
543 test_true(rc
== MEMCACHED_SUCCESS
);
546 rc
= memcached_mget(memc
, keys
, key_length
, 3);
548 results
= memcached_result_create(memc
, &results_obj
);
550 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
552 test_true(results
->item_cas
);
553 test_true(rc
== MEMCACHED_SUCCESS
);
554 test_true(memcached_result_cas(results
));
556 test_true(!memcmp(value
, "we the people", strlen("we the people")));
557 test_true(strlen("we the people") == value_length
);
558 test_true(rc
== MEMCACHED_SUCCESS
);
560 memcached_result_free(&results_obj
);
565 static test_return_t
cas_test(memcached_st
*memc
)
567 memcached_return_t rc
;
568 const char *key
= "fun";
569 size_t key_length
= strlen(key
);
570 const char *value
= "we the people";
571 const char* keys
[2] = { key
, NULL
};
572 size_t keylengths
[2] = { strlen(key
), 0 };
573 size_t value_length
= strlen(value
);
574 const char *value2
= "change the value";
575 size_t value2_length
= strlen(value2
);
577 memcached_result_st results_obj
;
578 memcached_result_st
*results
;
581 rc
= memcached_flush(memc
, 0);
582 test_true(rc
== MEMCACHED_SUCCESS
);
584 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
586 rc
= memcached_set(memc
, key
, strlen(key
),
587 value
, strlen(value
),
588 (time_t)0, (uint32_t)0);
589 test_true(rc
== MEMCACHED_SUCCESS
);
591 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
593 results
= memcached_result_create(memc
, &results_obj
);
595 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
597 test_true(rc
== MEMCACHED_SUCCESS
);
598 test_true(memcached_result_cas(results
));
599 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
600 test_true(strlen(memcached_result_value(results
)) == value_length
);
601 test_true(rc
== MEMCACHED_SUCCESS
);
602 uint64_t cas
= memcached_result_cas(results
);
605 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
606 test_true(rc
== MEMCACHED_END
);
607 test_true(results
== NULL
);
610 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
611 test_true(rc
== MEMCACHED_SUCCESS
);
614 * The item will have a new cas value, so try to set it again with the old
615 * value. This should fail!
617 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
618 test_true(rc
== MEMCACHED_DATA_EXISTS
);
620 memcached_result_free(&results_obj
);
625 static test_return_t
prepend_test(memcached_st
*memc
)
627 memcached_return_t rc
;
628 const char *key
= "fig";
629 const char *value
= "people";
630 char *out_value
= NULL
;
634 rc
= memcached_flush(memc
, 0);
635 test_true(rc
== MEMCACHED_SUCCESS
);
637 rc
= memcached_set(memc
, key
, strlen(key
),
638 value
, strlen(value
),
639 (time_t)0, (uint32_t)0);
640 test_true(rc
== MEMCACHED_SUCCESS
);
642 rc
= memcached_prepend(memc
, key
, strlen(key
),
643 "the ", strlen("the "),
644 (time_t)0, (uint32_t)0);
645 test_true(rc
== MEMCACHED_SUCCESS
);
647 rc
= memcached_prepend(memc
, key
, strlen(key
),
648 "we ", strlen("we "),
649 (time_t)0, (uint32_t)0);
650 test_true(rc
== MEMCACHED_SUCCESS
);
652 out_value
= memcached_get(memc
, key
, strlen(key
),
653 &value_length
, &flags
, &rc
);
654 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
655 test_true(strlen("we the people") == value_length
);
656 test_true(rc
== MEMCACHED_SUCCESS
);
663 Set the value, then quit to make sure it is flushed.
664 Come back in and test that add fails.
666 static test_return_t
add_test(memcached_st
*memc
)
668 memcached_return_t rc
;
669 const char *key
= "foo";
670 const char *value
= "when we sanitize";
671 unsigned long long setting_value
;
673 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
675 rc
= memcached_set(memc
, key
, strlen(key
),
676 value
, strlen(value
),
677 (time_t)0, (uint32_t)0);
678 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
679 memcached_quit(memc
);
680 rc
= memcached_add(memc
, key
, strlen(key
),
681 value
, strlen(value
),
682 (time_t)0, (uint32_t)0);
684 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
687 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
691 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
698 ** There was a problem of leaking filedescriptors in the initial release
699 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
700 ** systems it seems that the kernel is slow on reclaiming the resources
701 ** because the connects starts to time out (the test doesn't do much
702 ** anyway, so just loop 10 iterations)
704 static test_return_t
add_wrapper(memcached_st
*memc
)
706 unsigned int max
= 10000;
714 for (uint32_t x
= 0; x
< max
; x
++)
720 static test_return_t
replace_test(memcached_st
*memc
)
722 memcached_return_t rc
;
723 const char *key
= "foo";
724 const char *value
= "when we sanitize";
725 const char *original
= "first we insert some data";
727 rc
= memcached_set(memc
, key
, strlen(key
),
728 original
, strlen(original
),
729 (time_t)0, (uint32_t)0);
730 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
732 rc
= memcached_replace(memc
, key
, strlen(key
),
733 value
, strlen(value
),
734 (time_t)0, (uint32_t)0);
735 test_true(rc
== MEMCACHED_SUCCESS
);
740 static test_return_t
delete_test(memcached_st
*memc
)
742 memcached_return_t rc
;
743 const char *key
= "foo";
744 const char *value
= "when we sanitize";
746 rc
= memcached_set(memc
, key
, strlen(key
),
747 value
, strlen(value
),
748 (time_t)0, (uint32_t)0);
749 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
751 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
752 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
757 static test_return_t
flush_test(memcached_st
*memc
)
759 memcached_return_t rc
;
761 rc
= memcached_flush(memc
, 0);
762 test_true(rc
== MEMCACHED_SUCCESS
);
767 static memcached_return_t
server_function(const memcached_st
*ptr
,
768 const memcached_server_st
*server
,
771 (void)ptr
; (void)server
; (void)context
;
774 return MEMCACHED_SUCCESS
;
777 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
780 strncpy(context
, "foo bad", sizeof(context
));
781 memcached_server_fn callbacks
[1];
783 callbacks
[0]= server_function
;
784 memcached_server_cursor(memc
, callbacks
, context
, 1);
788 static test_return_t
bad_key_test(memcached_st
*memc
)
790 memcached_return_t rc
;
791 const char *key
= "foo bad";
793 size_t string_length
;
795 memcached_st
*memc_clone
;
797 size_t max_keylen
= 0xffff;
799 // Just skip if we are in binary mode.
800 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
803 memc_clone
= memcached_clone(NULL
, memc
);
804 test_true(memc_clone
);
806 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
807 test_true(rc
== MEMCACHED_SUCCESS
);
809 /* All keys are valid in the binary protocol (except for length) */
810 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
812 string
= memcached_get(memc_clone
, key
, strlen(key
),
813 &string_length
, &flags
, &rc
);
814 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
815 test_true(string_length
== 0);
819 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
820 test_true(rc
== MEMCACHED_SUCCESS
);
821 string
= memcached_get(memc_clone
, key
, strlen(key
),
822 &string_length
, &flags
, &rc
);
823 test_true(rc
== MEMCACHED_NOTFOUND
);
824 test_true(string_length
== 0);
827 /* Test multi key for bad keys */
828 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
829 size_t key_lengths
[] = { 7, 7, 7 };
831 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
832 test_true(rc
== MEMCACHED_SUCCESS
);
834 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
835 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
837 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
838 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
842 /* The following test should be moved to the end of this function when the
843 memcached server is updated to allow max size length of the keys in the
846 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
847 test_true(rc
== MEMCACHED_SUCCESS
);
849 char *longkey
= (char *)malloc(max_keylen
+ 1);
852 memset(longkey
, 'a', max_keylen
+ 1);
853 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
854 &string_length
, &flags
, &rc
);
855 test_true(rc
== MEMCACHED_NOTFOUND
);
856 test_true(string_length
== 0);
859 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
860 &string_length
, &flags
, &rc
);
861 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
862 test_true(string_length
== 0);
869 /* Make sure zero length keys are marked as bad */
871 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
872 test_true(rc
== MEMCACHED_SUCCESS
);
873 string
= memcached_get(memc_clone
, key
, 0,
874 &string_length
, &flags
, &rc
);
875 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
876 test_true(string_length
== 0);
879 memcached_free(memc_clone
);
884 #define READ_THROUGH_VALUE "set for me"
885 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
888 memcached_result_st
*result
)
890 (void)memc
;(void)key
;(void)key_length
;
891 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
894 static test_return_t
read_through(memcached_st
*memc
)
896 memcached_return_t rc
;
897 const char *key
= "foo";
899 size_t string_length
;
901 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
903 string
= memcached_get(memc
, key
, strlen(key
),
904 &string_length
, &flags
, &rc
);
906 test_true(rc
== MEMCACHED_NOTFOUND
);
907 test_false(string_length
);
910 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
912 test_true(rc
== MEMCACHED_SUCCESS
);
914 string
= memcached_get(memc
, key
, strlen(key
),
915 &string_length
, &flags
, &rc
);
917 test_true(rc
== MEMCACHED_SUCCESS
);
918 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
919 test_strcmp(READ_THROUGH_VALUE
, string
);
922 string
= memcached_get(memc
, key
, strlen(key
),
923 &string_length
, &flags
, &rc
);
925 test_true(rc
== MEMCACHED_SUCCESS
);
926 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
927 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
933 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
937 (void)ptr
;(void)key_length
;
940 return MEMCACHED_SUCCESS
;
943 static test_return_t
delete_through(memcached_st
*memc
)
945 memcached_trigger_delete_key_fn callback
;
946 memcached_return_t rc
;
948 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
950 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
951 test_true(rc
== MEMCACHED_SUCCESS
);
956 static test_return_t
get_test(memcached_st
*memc
)
958 memcached_return_t rc
;
959 const char *key
= "foo";
961 size_t string_length
;
964 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
965 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
967 string
= memcached_get(memc
, key
, strlen(key
),
968 &string_length
, &flags
, &rc
);
970 test_true(rc
== MEMCACHED_NOTFOUND
);
971 test_false(string_length
);
977 static test_return_t
get_test2(memcached_st
*memc
)
979 memcached_return_t rc
;
980 const char *key
= "foo";
981 const char *value
= "when we sanitize";
983 size_t string_length
;
986 rc
= memcached_set(memc
, key
, strlen(key
),
987 value
, strlen(value
),
988 (time_t)0, (uint32_t)0);
989 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
991 string
= memcached_get(memc
, key
, strlen(key
),
992 &string_length
, &flags
, &rc
);
995 test_true(rc
== MEMCACHED_SUCCESS
);
996 test_true(string_length
== strlen(value
));
997 test_true(!memcmp(string
, value
, string_length
));
1001 return TEST_SUCCESS
;
1004 static test_return_t
set_test2(memcached_st
*memc
)
1006 memcached_return_t rc
;
1007 const char *key
= "foo";
1008 const char *value
= "train in the brain";
1009 size_t value_length
= strlen(value
);
1012 for (x
= 0; x
< 10; x
++)
1014 rc
= memcached_set(memc
, key
, strlen(key
),
1015 value
, value_length
,
1016 (time_t)0, (uint32_t)0);
1017 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1020 return TEST_SUCCESS
;
1023 static test_return_t
set_test3(memcached_st
*memc
)
1025 memcached_return_t rc
;
1027 size_t value_length
= 8191;
1030 value
= (char*)malloc(value_length
);
1033 for (x
= 0; x
< value_length
; x
++)
1034 value
[x
] = (char) (x
% 127);
1036 /* The dump test relies on there being at least 32 items in memcached */
1037 for (x
= 0; x
< 32; x
++)
1041 snprintf(key
, sizeof(key
), "foo%u", x
);
1043 rc
= memcached_set(memc
, key
, strlen(key
),
1044 value
, value_length
,
1045 (time_t)0, (uint32_t)0);
1046 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1051 return TEST_SUCCESS
;
1054 static test_return_t
get_test3(memcached_st
*memc
)
1056 memcached_return_t rc
;
1057 const char *key
= "foo";
1059 size_t value_length
= 8191;
1061 size_t string_length
;
1065 value
= (char*)malloc(value_length
);
1068 for (x
= 0; x
< value_length
; x
++)
1069 value
[x
] = (char) (x
% 127);
1071 rc
= memcached_set(memc
, key
, strlen(key
),
1072 value
, value_length
,
1073 (time_t)0, (uint32_t)0);
1074 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1076 string
= memcached_get(memc
, key
, strlen(key
),
1077 &string_length
, &flags
, &rc
);
1079 test_true(rc
== MEMCACHED_SUCCESS
);
1081 test_true(string_length
== value_length
);
1082 test_true(!memcmp(string
, value
, string_length
));
1087 return TEST_SUCCESS
;
1090 static test_return_t
get_test4(memcached_st
*memc
)
1092 memcached_return_t rc
;
1093 const char *key
= "foo";
1095 size_t value_length
= 8191;
1097 size_t string_length
;
1101 value
= (char*)malloc(value_length
);
1104 for (x
= 0; x
< value_length
; x
++)
1105 value
[x
] = (char) (x
% 127);
1107 rc
= memcached_set(memc
, key
, strlen(key
),
1108 value
, value_length
,
1109 (time_t)0, (uint32_t)0);
1110 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1112 for (x
= 0; x
< 10; x
++)
1114 string
= memcached_get(memc
, key
, strlen(key
),
1115 &string_length
, &flags
, &rc
);
1117 test_true(rc
== MEMCACHED_SUCCESS
);
1119 test_true(string_length
== value_length
);
1120 test_true(!memcmp(string
, value
, string_length
));
1126 return TEST_SUCCESS
;
1130 * This test verifies that memcached_read_one_response doesn't try to
1131 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1132 * responses before you execute a storage command.
1134 static test_return_t
get_test5(memcached_st
*memc
)
1137 ** Request the same key twice, to ensure that we hash to the same server
1138 ** (so that we have multiple response values queued up) ;-)
1140 const char *keys
[]= { "key", "key" };
1141 size_t lengths
[]= { 3, 3 };
1145 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1146 keys
[0], lengths
[0], 0, 0);
1147 test_true(rc
== MEMCACHED_SUCCESS
);
1148 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1150 memcached_result_st results_obj
;
1151 memcached_result_st
*results
;
1152 results
=memcached_result_create(memc
, &results_obj
);
1154 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1156 memcached_result_free(&results_obj
);
1158 /* Don't read out the second result, but issue a set instead.. */
1159 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1160 test_true(rc
== MEMCACHED_SUCCESS
);
1162 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1163 &rlen
, &flags
, &rc
);
1164 test_true(val
== NULL
);
1165 test_true(rc
== MEMCACHED_NOTFOUND
);
1166 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1167 test_true(val
!= NULL
);
1168 test_true(rc
== MEMCACHED_SUCCESS
);
1171 return TEST_SUCCESS
;
1174 static test_return_t
mget_end(memcached_st
*memc
)
1176 const char *keys
[]= { "foo", "foo2" };
1177 size_t lengths
[]= { 3, 4 };
1178 const char *values
[]= { "fjord", "41" };
1180 memcached_return_t rc
;
1183 for (int i
= 0; i
< 2; i
++)
1185 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1186 (time_t)0, (uint32_t)0);
1187 test_true(rc
== MEMCACHED_SUCCESS
);
1191 size_t string_length
;
1194 // retrieve both via mget
1195 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1196 test_true(rc
== MEMCACHED_SUCCESS
);
1198 char key
[MEMCACHED_MAX_KEY
];
1201 // this should get both
1202 for (int i
= 0; i
< 2; i
++)
1204 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1206 test_true(rc
== MEMCACHED_SUCCESS
);
1208 if (key_length
== 4)
1210 test_true(string_length
== strlen(values
[val
]));
1211 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1215 // this should indicate end
1216 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1217 test_true(rc
== MEMCACHED_END
);
1220 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1221 test_true(rc
== MEMCACHED_SUCCESS
);
1223 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1224 test_true(key_length
== lengths
[0]);
1225 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1226 test_true(string_length
== strlen(values
[0]));
1227 test_true(strncmp(values
[0], string
, string_length
) == 0);
1228 test_true(rc
== MEMCACHED_SUCCESS
);
1231 // this should indicate end
1232 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1233 test_true(rc
== MEMCACHED_END
);
1235 return TEST_SUCCESS
;
1238 /* Do not copy the style of this code, I just access hosts to testthis function */
1239 static test_return_t
stats_servername_test(memcached_st
*memc
)
1241 memcached_return_t rc
;
1242 memcached_stat_st memc_stat
;
1243 memcached_server_instance_st instance
=
1244 memcached_server_instance_by_position(memc
, 0);
1246 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1247 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1248 return TEST_SKIPPED
;
1250 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1251 memcached_server_name(instance
),
1252 memcached_server_port(instance
));
1254 return TEST_SUCCESS
;
1257 static test_return_t
increment_test(memcached_st
*memc
)
1259 uint64_t new_number
;
1260 memcached_return_t rc
;
1261 const char *key
= "number";
1262 const char *value
= "0";
1264 rc
= memcached_set(memc
, key
, strlen(key
),
1265 value
, strlen(value
),
1266 (time_t)0, (uint32_t)0);
1267 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1269 rc
= memcached_increment(memc
, key
, strlen(key
),
1271 test_true(rc
== MEMCACHED_SUCCESS
);
1272 test_true(new_number
== 1);
1274 rc
= memcached_increment(memc
, key
, strlen(key
),
1276 test_true(rc
== MEMCACHED_SUCCESS
);
1277 test_true(new_number
== 2);
1279 return TEST_SUCCESS
;
1282 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1284 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1286 uint64_t new_number
;
1287 memcached_return_t rc
;
1288 const char *key
= "number";
1289 uint64_t initial
= 0;
1291 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1292 1, initial
, 0, &new_number
);
1293 test_true(rc
== MEMCACHED_SUCCESS
);
1294 test_true(new_number
== initial
);
1296 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1297 1, initial
, 0, &new_number
);
1298 test_true(rc
== MEMCACHED_SUCCESS
);
1299 test_true(new_number
== (initial
+ 1));
1301 return TEST_SUCCESS
;
1304 static test_return_t
decrement_test(memcached_st
*memc
)
1306 uint64_t new_number
;
1307 memcached_return_t rc
;
1308 const char *key
= "number";
1309 const char *value
= "3";
1311 rc
= memcached_set(memc
, key
, strlen(key
),
1312 value
, strlen(value
),
1313 (time_t)0, (uint32_t)0);
1314 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1316 rc
= memcached_decrement(memc
, key
, strlen(key
),
1318 test_true(rc
== MEMCACHED_SUCCESS
);
1319 test_true(new_number
== 2);
1321 rc
= memcached_decrement(memc
, key
, strlen(key
),
1323 test_true(rc
== MEMCACHED_SUCCESS
);
1324 test_true(new_number
== 1);
1326 return TEST_SUCCESS
;
1329 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1331 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1333 uint64_t new_number
;
1334 memcached_return_t rc
;
1335 const char *key
= "number";
1336 uint64_t initial
= 3;
1338 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1339 1, initial
, 0, &new_number
);
1340 test_true(rc
== MEMCACHED_SUCCESS
);
1341 test_true(new_number
== initial
);
1343 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1344 1, initial
, 0, &new_number
);
1345 test_true(rc
== MEMCACHED_SUCCESS
);
1346 test_true(new_number
== (initial
- 1));
1348 return TEST_SUCCESS
;
1351 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1353 uint64_t new_number
;
1354 memcached_return_t rc
;
1355 const char *master_key
= "foo";
1356 const char *key
= "number";
1357 const char *value
= "0";
1359 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1361 value
, strlen(value
),
1362 (time_t)0, (uint32_t)0);
1363 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1365 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1367 test_true(rc
== MEMCACHED_SUCCESS
);
1368 test_true(new_number
== 1);
1370 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1372 test_true(rc
== MEMCACHED_SUCCESS
);
1373 test_true(new_number
== 2);
1375 return TEST_SUCCESS
;
1378 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1380 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1382 uint64_t new_number
;
1383 memcached_return_t rc
;
1384 const char *master_key
= "foo";
1385 const char *key
= "number";
1386 uint64_t initial
= 0;
1388 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1390 1, initial
, 0, &new_number
);
1391 test_true(rc
== MEMCACHED_SUCCESS
);
1392 test_true(new_number
== initial
);
1394 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1396 1, initial
, 0, &new_number
);
1397 test_true(rc
== MEMCACHED_SUCCESS
);
1398 test_true(new_number
== (initial
+ 1));
1400 return TEST_SUCCESS
;
1403 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1405 uint64_t new_number
;
1406 memcached_return_t rc
;
1407 const char *master_key
= "foo";
1408 const char *key
= "number";
1409 const char *value
= "3";
1411 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1413 value
, strlen(value
),
1414 (time_t)0, (uint32_t)0);
1415 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1417 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1420 test_true(rc
== MEMCACHED_SUCCESS
);
1421 test_true(new_number
== 2);
1423 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1426 test_true(rc
== MEMCACHED_SUCCESS
);
1427 test_true(new_number
== 1);
1429 return TEST_SUCCESS
;
1432 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1434 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1436 uint64_t new_number
;
1437 memcached_return_t rc
;
1438 const char *master_key
= "foo";
1439 const char *key
= "number";
1440 uint64_t initial
= 3;
1442 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1444 1, initial
, 0, &new_number
);
1445 test_true(rc
== MEMCACHED_SUCCESS
);
1446 test_true(new_number
== initial
);
1448 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1450 1, initial
, 0, &new_number
);
1451 test_true(rc
== MEMCACHED_SUCCESS
);
1452 test_true(new_number
== (initial
- 1));
1454 return TEST_SUCCESS
;
1457 static test_return_t
quit_test(memcached_st
*memc
)
1459 memcached_return_t rc
;
1460 const char *key
= "fudge";
1461 const char *value
= "sanford and sun";
1463 rc
= memcached_set(memc
, key
, strlen(key
),
1464 value
, strlen(value
),
1465 (time_t)10, (uint32_t)3);
1466 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1467 memcached_quit(memc
);
1469 rc
= memcached_set(memc
, key
, strlen(key
),
1470 value
, strlen(value
),
1471 (time_t)50, (uint32_t)9);
1472 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1474 return TEST_SUCCESS
;
1477 static test_return_t
mget_result_test(memcached_st
*memc
)
1479 memcached_return_t rc
;
1480 const char *keys
[]= {"fudge", "son", "food"};
1481 size_t key_length
[]= {5, 3, 4};
1484 memcached_result_st results_obj
;
1485 memcached_result_st
*results
;
1487 results
= memcached_result_create(memc
, &results_obj
);
1489 test_true(&results_obj
== results
);
1491 /* We need to empty the server before continueing test */
1492 rc
= memcached_flush(memc
, 0);
1493 test_true(rc
== MEMCACHED_SUCCESS
);
1495 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1496 test_true(rc
== MEMCACHED_SUCCESS
);
1498 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1503 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1504 test_true(!results
);
1505 test_true(rc
== MEMCACHED_END
);
1507 for (x
= 0; x
< 3; x
++)
1509 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1510 keys
[x
], key_length
[x
],
1511 (time_t)50, (uint32_t)9);
1512 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1515 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1516 test_true(rc
== MEMCACHED_SUCCESS
);
1518 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1521 test_true(&results_obj
== results
);
1522 test_true(rc
== MEMCACHED_SUCCESS
);
1523 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1524 test_true(!memcmp(memcached_result_key_value(results
),
1525 memcached_result_value(results
),
1526 memcached_result_length(results
)));
1529 memcached_result_free(&results_obj
);
1531 return TEST_SUCCESS
;
1534 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1536 memcached_return_t rc
;
1537 const char *keys
[]= {"fudge", "son", "food"};
1538 size_t key_length
[]= {5, 3, 4};
1541 memcached_result_st
*results
;
1543 /* We need to empty the server before continueing test */
1544 rc
= memcached_flush(memc
, 0);
1545 test_true(rc
== MEMCACHED_SUCCESS
);
1547 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1548 test_true(rc
== MEMCACHED_SUCCESS
);
1550 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1554 test_true(!results
);
1555 test_true(rc
== MEMCACHED_END
);
1557 for (x
= 0; x
< 3; x
++)
1559 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1560 keys
[x
], key_length
[x
],
1561 (time_t)50, (uint32_t)9);
1562 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1565 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1566 test_true(rc
== MEMCACHED_SUCCESS
);
1569 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1572 test_true(rc
== MEMCACHED_SUCCESS
);
1573 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1574 test_true(!memcmp(memcached_result_key_value(results
),
1575 memcached_result_value(results
),
1576 memcached_result_length(results
)));
1577 memcached_result_free(results
);
1581 return TEST_SUCCESS
;
1584 /* Count the results */
1585 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1586 memcached_result_st
*result
,
1589 (void)ptr
; (void)result
;
1590 size_t *counter
= (size_t *)context
;
1592 *counter
= *counter
+ 1;
1594 return MEMCACHED_SUCCESS
;
1597 static test_return_t
mget_result_function(memcached_st
*memc
)
1599 memcached_return_t rc
;
1600 const char *keys
[]= {"fudge", "son", "food"};
1601 size_t key_length
[]= {5, 3, 4};
1604 memcached_execute_fn callbacks
[1];
1606 /* We need to empty the server before continueing test */
1607 rc
= memcached_flush(memc
, 0);
1608 for (x
= 0; x
< 3; x
++)
1610 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1611 keys
[x
], key_length
[x
],
1612 (time_t)50, (uint32_t)9);
1613 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1616 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1617 test_true(rc
== MEMCACHED_SUCCESS
);
1619 callbacks
[0]= &callback_counter
;
1621 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1623 test_true(counter
== 3);
1625 return TEST_SUCCESS
;
1628 static test_return_t
mget_test(memcached_st
*memc
)
1630 memcached_return_t rc
;
1631 const char *keys
[]= {"fudge", "son", "food"};
1632 size_t key_length
[]= {5, 3, 4};
1636 char return_key
[MEMCACHED_MAX_KEY
];
1637 size_t return_key_length
;
1639 size_t return_value_length
;
1641 /* We need to empty the server before continueing test */
1642 rc
= memcached_flush(memc
, 0);
1643 test_true(rc
== MEMCACHED_SUCCESS
);
1645 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1646 test_true(rc
== MEMCACHED_SUCCESS
);
1648 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1649 &return_value_length
, &flags
, &rc
)) != NULL
)
1651 test_true(return_value
);
1653 test_true(!return_value
);
1654 test_true(return_value_length
== 0);
1655 test_true(rc
== MEMCACHED_END
);
1657 for (x
= 0; x
< 3; x
++)
1659 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1660 keys
[x
], key_length
[x
],
1661 (time_t)50, (uint32_t)9);
1662 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1665 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1666 test_true(rc
== MEMCACHED_SUCCESS
);
1669 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1670 &return_value_length
, &flags
, &rc
)))
1672 test_true(return_value
);
1673 test_true(rc
== MEMCACHED_SUCCESS
);
1674 test_true(return_key_length
== return_value_length
);
1675 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1680 return TEST_SUCCESS
;
1683 static test_return_t
mget_execute(memcached_st
*memc
)
1687 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1691 * I only want to hit _one_ server so I know the number of requests I'm
1692 * sending in the pipeline.
1694 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1695 memc
->number_of_hosts
= 1;
1697 size_t max_keys
= 20480;
1700 char **keys
= calloc(max_keys
, sizeof(char*));
1701 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1703 /* First add all of the items.. */
1704 char blob
[1024] = {0};
1705 memcached_return_t rc
;
1707 for (size_t x
= 0; x
< max_keys
; ++x
)
1711 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1713 test_true(keys
[x
] != NULL
);
1714 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1715 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1718 /* Try to get all of them with a large multiget */
1720 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1721 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1722 max_keys
, callbacks
, &counter
, 1);
1724 if (rc
== MEMCACHED_SUCCESS
)
1727 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1728 test_true(rc
== MEMCACHED_END
);
1730 /* Verify that we got all of the items */
1731 test_true(counter
== max_keys
);
1733 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1735 test_true(counter
== 0);
1739 test_fail("note: this test functions differently when in binary mode");
1742 /* Release all allocated resources */
1743 for (size_t x
= 0; x
< max_keys
; ++x
)
1750 memc
->number_of_hosts
= number_of_hosts
;
1751 return TEST_SUCCESS
;
1754 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1756 static test_return_t
key_setup(memcached_st
*memc
)
1760 if (pre_binary(memc
) != TEST_SUCCESS
)
1761 return TEST_SKIPPED
;
1763 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1765 return TEST_SUCCESS
;
1768 static test_return_t
key_teardown(memcached_st
*memc
)
1771 pairs_free(global_pairs
);
1773 return TEST_SUCCESS
;
1776 static test_return_t
block_add_regression(memcached_st
*memc
)
1778 /* First add all of the items.. */
1779 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1781 memcached_return_t rc
;
1782 char blob
[1024] = {0};
1784 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1785 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1788 return TEST_SUCCESS
;
1791 static test_return_t
binary_add_regression(memcached_st
*memc
)
1793 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1794 test_return_t rc
= block_add_regression(memc
);
1795 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1799 static test_return_t
get_stats_keys(memcached_st
*memc
)
1803 memcached_stat_st memc_stat
;
1804 memcached_return_t rc
;
1806 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1807 test_true(rc
== MEMCACHED_SUCCESS
);
1808 for (ptr
= stat_list
; *ptr
; ptr
++)
1813 return TEST_SUCCESS
;
1816 static test_return_t
version_string_test(memcached_st
*memc
)
1818 const char *version_string
;
1821 version_string
= memcached_lib_version();
1823 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1825 return TEST_SUCCESS
;
1828 static test_return_t
get_stats(memcached_st
*memc
)
1832 memcached_return_t rc
;
1833 memcached_stat_st
*memc_stat
;
1835 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1836 test_true(rc
== MEMCACHED_SUCCESS
);
1838 test_true(rc
== MEMCACHED_SUCCESS
);
1839 test_true(memc_stat
);
1841 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1843 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1844 test_true(rc
== MEMCACHED_SUCCESS
);
1845 for (ptr
= stat_list
; *ptr
; ptr
++);
1850 memcached_stat_free(NULL
, memc_stat
);
1852 return TEST_SUCCESS
;
1855 static test_return_t
add_host_test(memcached_st
*memc
)
1858 memcached_server_st
*servers
;
1859 memcached_return_t rc
;
1860 char servername
[]= "0.example.com";
1862 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1864 test_true(1 == memcached_server_list_count(servers
));
1866 for (x
= 2; x
< 20; x
++)
1868 char buffer
[SMALL_STRING_LEN
];
1870 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1871 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1873 test_true(rc
== MEMCACHED_SUCCESS
);
1874 test_true(x
== memcached_server_list_count(servers
));
1877 rc
= memcached_server_push(memc
, servers
);
1878 test_true(rc
== MEMCACHED_SUCCESS
);
1879 rc
= memcached_server_push(memc
, servers
);
1880 test_true(rc
== MEMCACHED_SUCCESS
);
1882 memcached_server_list_free(servers
);
1884 return TEST_SUCCESS
;
1887 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1889 (void)parent
;(void)memc_clone
;
1890 return MEMCACHED_SUCCESS
;
1893 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1896 return MEMCACHED_SUCCESS
;
1899 static test_return_t
callback_test(memcached_st
*memc
)
1901 /* Test User Data */
1905 memcached_return_t rc
;
1907 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1908 test_true(rc
== MEMCACHED_SUCCESS
);
1909 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1910 test_true(*test_ptr
== x
);
1913 /* Test Clone Callback */
1915 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1916 void *clone_cb_ptr
= *(void **)&clone_cb
;
1917 void *temp_function
= NULL
;
1918 memcached_return_t rc
;
1920 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1922 test_true(rc
== MEMCACHED_SUCCESS
);
1923 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1924 test_true(temp_function
== clone_cb_ptr
);
1927 /* Test Cleanup Callback */
1929 memcached_cleanup_fn cleanup_cb
=
1930 (memcached_cleanup_fn
)cleanup_test_callback
;
1931 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1932 void *temp_function
= NULL
;
1933 memcached_return_t rc
;
1935 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1937 test_true(rc
== MEMCACHED_SUCCESS
);
1938 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1939 test_true(temp_function
== cleanup_cb_ptr
);
1942 return TEST_SUCCESS
;
1945 /* We don't test the behavior itself, we test the switches */
1946 static test_return_t
behavior_test(memcached_st
*memc
)
1951 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1952 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1953 test_true(value
== 1);
1955 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1956 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1957 test_true(value
== 1);
1959 set
= MEMCACHED_HASH_MD5
;
1960 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1961 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1962 test_true(value
== MEMCACHED_HASH_MD5
);
1966 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1967 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1968 test_true(value
== 0);
1970 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1971 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1972 test_true(value
== 0);
1974 set
= MEMCACHED_HASH_DEFAULT
;
1975 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1976 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1977 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1979 set
= MEMCACHED_HASH_CRC
;
1980 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1981 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1982 test_true(value
== MEMCACHED_HASH_CRC
);
1984 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1985 test_true(value
> 0);
1987 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1988 test_true(value
> 0);
1990 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1991 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1992 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1994 return TEST_SUCCESS
;
1997 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1999 memcached_return_t rc
;
2003 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2004 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2006 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2008 if (rc
== MEMCACHED_SUCCESS
)
2010 test_true((bool)value
== set
);
2014 test_false((bool)value
== set
);
2017 return TEST_SUCCESS
;
2021 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2023 memcached_return_t rc
;
2027 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2028 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2030 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2032 if (rc
== MEMCACHED_SUCCESS
)
2034 test_true((bool)value
== set
);
2038 test_false((bool)value
== set
);
2041 return TEST_SUCCESS
;
2045 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2047 memcached_return_t rc
;
2051 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2052 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2054 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2056 if (rc
== MEMCACHED_SUCCESS
)
2058 test_true((bool)value
== set
);
2062 test_false((bool)value
== set
);
2065 return TEST_SUCCESS
;
2068 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2070 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2071 char return_key
[MEMCACHED_MAX_KEY
];
2072 size_t return_key_length
;
2074 size_t return_value_length
;
2079 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2080 &return_value_length
, &flags
, &rc
)))
2082 test_true(return_value
);
2083 test_true(rc
== MEMCACHED_SUCCESS
);
2085 *keys_returned
= *keys_returned
+1;
2088 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2090 return TEST_SUCCESS
;
2093 /* Test case provided by Cal Haldenbrand */
2094 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2096 unsigned int setter
= 1;
2098 unsigned long long total
= 0;
2101 char randomstuff
[6 * 1024];
2102 memcached_return_t rc
;
2104 memset(randomstuff
, 0, 6 * 1024);
2106 /* We just keep looking at the same values over and over */
2109 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2110 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2114 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2118 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2119 memset(randomstuff
, 0, 6 * 1024);
2120 test_true(size
< 6 * 1024); /* Being safe here */
2122 for (j
= 0 ; j
< size
;j
++)
2123 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2126 snprintf(key
, sizeof(key
), "%u", x
);
2127 rc
= memcached_set(memc
, key
, strlen(key
),
2128 randomstuff
, strlen(randomstuff
), 10, 0);
2129 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2130 /* If we fail, lets try again */
2131 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2132 rc
= memcached_set(memc
, key
, strlen(key
),
2133 randomstuff
, strlen(randomstuff
), 10, 0);
2134 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2137 return TEST_SUCCESS
;
2140 /* Test case provided by Cal Haldenbrand */
2141 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2143 unsigned int setter
;
2147 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2148 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2150 setter
= 20 * 1024576;
2151 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2152 setter
= 20 * 1024576;
2153 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2154 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2155 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2157 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2160 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2162 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2163 char buffer
[SMALL_STRING_LEN
];
2168 memset(buffer
, 0, SMALL_STRING_LEN
);
2170 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2171 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2172 &val_len
, &flags
, &rc
);
2173 if (rc
!= MEMCACHED_SUCCESS
)
2175 if (rc
== MEMCACHED_NOTFOUND
)
2189 return TEST_SUCCESS
;
2192 /* Do a large mget() over all the keys we think exist */
2193 #define KEY_COUNT 3000 // * 1024576
2194 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2196 memcached_return_t rc
;
2197 unsigned int setter
;
2200 size_t key_lengths
[KEY_COUNT
];
2203 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2204 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2206 setter
= 20 * 1024576;
2207 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2208 setter
= 20 * 1024576;
2209 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2210 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2211 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2214 keys
= calloc(KEY_COUNT
, sizeof(char *));
2216 for (x
= 0; x
< KEY_COUNT
; x
++)
2220 snprintf(buffer
, 30, "%u", x
);
2221 keys
[x
]= strdup(buffer
);
2222 key_lengths
[x
]= strlen(keys
[x
]);
2225 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2226 test_true(rc
== MEMCACHED_SUCCESS
);
2228 size_t keys_returned
;
2229 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2231 for (x
= 0; x
< KEY_COUNT
; x
++)
2235 return TEST_SUCCESS
;
2238 /* Make sure we behave properly if server list has no values */
2239 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2241 memcached_return_t rc
;
2242 const char *keys
[]= {"fudge", "son", "food"};
2243 size_t key_length
[]= {5, 3, 4};
2246 char return_key
[MEMCACHED_MAX_KEY
];
2247 size_t return_key_length
;
2249 size_t return_value_length
;
2251 /* Here we free everything before running a bunch of mget tests */
2252 memcached_servers_reset(memc
);
2255 /* We need to empty the server before continueing test */
2256 rc
= memcached_flush(memc
, 0);
2257 test_true(rc
== MEMCACHED_NO_SERVERS
);
2259 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2260 test_true(rc
== MEMCACHED_NO_SERVERS
);
2262 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2263 &return_value_length
, &flags
, &rc
)) != NULL
)
2265 test_true(return_value
);
2267 test_true(!return_value
);
2268 test_true(return_value_length
== 0);
2269 test_true(rc
== MEMCACHED_NO_SERVERS
);
2271 for (x
= 0; x
< 3; x
++)
2273 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2274 keys
[x
], key_length
[x
],
2275 (time_t)50, (uint32_t)9);
2276 test_true(rc
== MEMCACHED_NO_SERVERS
);
2279 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2280 test_true(rc
== MEMCACHED_NO_SERVERS
);
2283 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2284 &return_value_length
, &flags
, &rc
)))
2286 test_true(return_value
);
2287 test_true(rc
== MEMCACHED_SUCCESS
);
2288 test_true(return_key_length
== return_value_length
);
2289 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2294 return TEST_SUCCESS
;
2297 #define VALUE_SIZE_BUG5 1048064
2298 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2300 memcached_return_t rc
;
2301 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2302 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2303 char return_key
[MEMCACHED_MAX_KEY
];
2304 size_t return_key_length
;
2306 size_t value_length
;
2310 char insert_data
[VALUE_SIZE_BUG5
];
2312 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2313 insert_data
[x
]= (signed char)rand();
2315 memcached_flush(memc
, 0);
2316 value
= memcached_get(memc
, keys
[0], key_length
[0],
2317 &value_length
, &flags
, &rc
);
2318 test_true(value
== NULL
);
2319 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2322 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2323 &value_length
, &flags
, &rc
)))
2325 test_true(count
== 0);
2327 for (x
= 0; x
< 4; x
++)
2329 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2330 insert_data
, VALUE_SIZE_BUG5
,
2331 (time_t)0, (uint32_t)0);
2332 test_true(rc
== MEMCACHED_SUCCESS
);
2335 for (x
= 0; x
< 10; x
++)
2337 value
= memcached_get(memc
, keys
[0], key_length
[0],
2338 &value_length
, &flags
, &rc
);
2342 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2344 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2345 &value_length
, &flags
, &rc
)))
2350 test_true(count
== 4);
2353 return TEST_SUCCESS
;
2356 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2358 memcached_return_t rc
;
2359 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2360 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2361 char return_key
[MEMCACHED_MAX_KEY
];
2362 size_t return_key_length
;
2364 size_t value_length
;
2368 char insert_data
[VALUE_SIZE_BUG5
];
2370 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2371 insert_data
[x
]= (signed char)rand();
2373 memcached_flush(memc
, 0);
2374 value
= memcached_get(memc
, keys
[0], key_length
[0],
2375 &value_length
, &flags
, &rc
);
2376 test_true(value
== NULL
);
2377 test_true(rc
== MEMCACHED_NOTFOUND
);
2378 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2379 test_true(rc
== MEMCACHED_SUCCESS
);
2382 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2383 &value_length
, &flags
, &rc
)))
2385 test_true(count
== 0);
2386 test_true(rc
== MEMCACHED_END
);
2388 for (x
= 0; x
< 4; x
++)
2390 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2391 insert_data
, VALUE_SIZE_BUG5
,
2392 (time_t)0, (uint32_t)0);
2393 test_true(rc
== MEMCACHED_SUCCESS
);
2396 for (x
= 0; x
< 2; x
++)
2398 value
= memcached_get(memc
, keys
[0], key_length
[0],
2399 &value_length
, &flags
, &rc
);
2403 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2404 test_true(rc
== MEMCACHED_SUCCESS
);
2406 /* We test for purge of partial complete fetches */
2407 for (count
= 3; count
; count
--)
2409 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2410 &value_length
, &flags
, &rc
);
2411 test_true(rc
== MEMCACHED_SUCCESS
);
2412 test_true(!(memcmp(value
, insert_data
, value_length
)));
2413 test_true(value_length
);
2418 return TEST_SUCCESS
;
2421 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2423 memcached_return_t rc
;
2425 memcached_st
*memc_clone
;
2427 memcached_server_st
*servers
;
2428 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";
2431 servers
= memcached_servers_parse(server_list
);
2434 mine
= memcached_create(NULL
);
2435 rc
= memcached_server_push(mine
, servers
);
2436 test_true(rc
== MEMCACHED_SUCCESS
);
2437 memcached_server_list_free(servers
);
2440 memc_clone
= memcached_clone(NULL
, mine
);
2442 memcached_quit(mine
);
2443 memcached_quit(memc_clone
);
2446 memcached_free(mine
);
2447 memcached_free(memc_clone
);
2449 return TEST_SUCCESS
;
2452 /* Test flag store/retrieve */
2453 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2455 memcached_return_t rc
;
2456 const char *keys
= "036790384900";
2457 size_t key_length
= strlen(keys
);
2458 char return_key
[MEMCACHED_MAX_KEY
];
2459 size_t return_key_length
;
2461 size_t value_length
;
2464 char insert_data
[VALUE_SIZE_BUG5
];
2466 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2467 insert_data
[x
]= (signed char)rand();
2469 memcached_flush(memc
, 0);
2472 rc
= memcached_set(memc
, keys
, key_length
,
2473 insert_data
, VALUE_SIZE_BUG5
,
2475 test_true(rc
== MEMCACHED_SUCCESS
);
2478 value
= memcached_get(memc
, keys
, key_length
,
2479 &value_length
, &flags
, &rc
);
2480 test_true(flags
== 245);
2484 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2487 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2488 &value_length
, &flags
, &rc
);
2489 test_true(flags
== 245);
2494 return TEST_SUCCESS
;
2497 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2499 memcached_return_t rc
;
2500 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2501 size_t key_length
[3];
2506 char return_key
[MEMCACHED_MAX_KEY
];
2507 size_t return_key_length
;
2509 size_t return_value_length
;
2512 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2513 key_length
[1]= strlen("fudge&*@#");
2514 key_length
[2]= strlen("for^#@&$not");
2517 for (x
= 0; x
< 3; x
++)
2519 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2520 keys
[x
], key_length
[x
],
2521 (time_t)50, (uint32_t)9);
2522 test_true(rc
== MEMCACHED_SUCCESS
);
2525 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2526 test_true(rc
== MEMCACHED_SUCCESS
);
2528 /* We need to empty the server before continueing test */
2529 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2530 &return_value_length
, &flags
, &rc
)) != NULL
)
2532 test_true(return_value
);
2536 test_true(count
== 3);
2538 return TEST_SUCCESS
;
2541 /* We are testing with aggressive timeout to get failures */
2542 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2544 const char *key
= "foo";
2546 size_t value_length
= 512;
2549 memcached_return_t rc
;
2550 unsigned int set
= 1;
2551 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2554 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2555 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2557 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2560 value
= (char*)malloc(value_length
* sizeof(char));
2562 for (x
= 0; x
< value_length
; x
++)
2563 value
[x
]= (char) (x
% 127);
2565 for (x
= 1; x
<= 100000; ++x
)
2567 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2569 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2570 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2572 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2577 memcached_free(mclone
);
2579 return TEST_SUCCESS
;
2583 We are looking failures in the async protocol
2585 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2587 const char *key
= "foo";
2589 size_t value_length
= 512;
2592 memcached_return_t rc
;
2593 unsigned int set
= 1;
2595 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2597 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2598 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2600 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2603 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2605 test_true(timeout
== -1);
2607 value
= (char*)malloc(value_length
* sizeof(char));
2609 for (x
= 0; x
< value_length
; x
++)
2610 value
[x
]= (char) (x
% 127);
2612 for (x
= 1; x
<= 100000; ++x
)
2614 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2618 memcached_free(mclone
);
2620 return TEST_SUCCESS
;
2624 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2626 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2628 memcached_return_t rc
;
2630 size_t value_length
;
2632 uint64_t number_value
;
2634 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2635 &value_length
, &flags
, &rc
);
2636 test_true(value
== NULL
);
2637 test_true(rc
== MEMCACHED_NOTFOUND
);
2639 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2642 test_true(value
== NULL
);
2643 /* The binary protocol will set the key if it doesn't exist */
2644 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2646 test_true(rc
== MEMCACHED_SUCCESS
);
2650 test_true(rc
== MEMCACHED_NOTFOUND
);
2653 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2655 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2656 &value_length
, &flags
, &rc
);
2658 test_true(rc
== MEMCACHED_SUCCESS
);
2661 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2663 test_true(number_value
== 2);
2664 test_true(rc
== MEMCACHED_SUCCESS
);
2666 return TEST_SUCCESS
;
2670 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2671 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2673 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2675 char key
[] = "key34567890";
2677 memcached_return_t rc
;
2678 size_t overflowSize
;
2680 char commandFirst
[]= "set key34567890 0 0 ";
2681 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2682 size_t commandLength
;
2685 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2687 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2689 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2691 overflow
= malloc(testSize
);
2692 test_true(overflow
!= NULL
);
2694 memset(overflow
, 'x', testSize
);
2695 rc
= memcached_set(memc
, key
, strlen(key
),
2696 overflow
, testSize
, 0, 0);
2697 test_true(rc
== MEMCACHED_SUCCESS
);
2701 return TEST_SUCCESS
;
2706 Test values of many different sizes
2707 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2708 set key34567890 0 0 8169 \r\n
2709 is sent followed by buffer of size 8169, followed by 8169
2711 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2714 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2715 memcached_return_t rc
;
2716 const char *key
= "foo";
2718 size_t value_length
= 18000;
2720 size_t string_length
;
2723 size_t current_length
;
2725 value
= (char*)malloc(value_length
);
2728 for (x
= 0; x
< value_length
; x
++)
2729 value
[x
] = (char) (x
% 127);
2731 for (current_length
= 0; current_length
< value_length
; current_length
++)
2733 rc
= memcached_set(memc
, key
, strlen(key
),
2734 value
, current_length
,
2735 (time_t)0, (uint32_t)0);
2736 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2738 string
= memcached_get(memc
, key
, strlen(key
),
2739 &string_length
, &flags
, &rc
);
2741 test_true(rc
== MEMCACHED_SUCCESS
);
2742 test_true(string_length
== current_length
);
2743 test_true(!memcmp(string
, value
, string_length
));
2750 return TEST_SUCCESS
;
2754 Look for zero length value problems
2756 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2759 memcached_return_t rc
;
2760 const char *key
= "mykey";
2765 for (x
= 0; x
< 2; x
++)
2767 rc
= memcached_set(memc
, key
, strlen(key
),
2769 (time_t)0, (uint32_t)0);
2771 test_true(rc
== MEMCACHED_SUCCESS
);
2773 value
= memcached_get(memc
, key
, strlen(key
),
2774 &length
, &flags
, &rc
);
2776 test_true(rc
== MEMCACHED_SUCCESS
);
2777 test_true(value
== NULL
);
2778 test_true(length
== 0);
2779 test_true(flags
== 0);
2781 value
= memcached_get(memc
, key
, strlen(key
),
2782 &length
, &flags
, &rc
);
2784 test_true(rc
== MEMCACHED_SUCCESS
);
2785 test_true(value
== NULL
);
2786 test_true(length
== 0);
2787 test_true(flags
== 0);
2790 return TEST_SUCCESS
;
2793 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2794 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2796 memcached_return_t rc
;
2797 const char *key
= "mykey";
2802 rc
= memcached_set(memc
, key
, strlen(key
),
2804 (time_t)0, UINT32_MAX
);
2806 test_true(rc
== MEMCACHED_SUCCESS
);
2808 value
= memcached_get(memc
, key
, strlen(key
),
2809 &length
, &flags
, &rc
);
2811 test_true(rc
== MEMCACHED_SUCCESS
);
2812 test_true(value
== NULL
);
2813 test_true(length
== 0);
2814 test_true(flags
== UINT32_MAX
);
2816 return TEST_SUCCESS
;
2819 #if !defined(__sun) && !defined(__OpenBSD__)
2820 /* Check the validity of chinese key*/
2821 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2823 memcached_return_t rc
;
2824 const char *key
= "豆瓣";
2825 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2830 rc
= memcached_set(memc
, key
, strlen(key
),
2831 value
, strlen(value
),
2834 test_true(rc
== MEMCACHED_SUCCESS
);
2836 value2
= memcached_get(memc
, key
, strlen(key
),
2837 &length
, &flags
, &rc
);
2839 test_true(length
==strlen(value
));
2840 test_true(rc
== MEMCACHED_SUCCESS
);
2841 test_true(memcmp(value
, value2
, length
)==0);
2844 return TEST_SUCCESS
;
2852 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2855 const memcached_server_st
*server
;
2856 memcached_return_t res
;
2860 memc
= memcached_create(NULL
);
2861 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2862 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2864 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2866 memcached_free(memc
);
2868 return TEST_SUCCESS
;
2871 /* CAS test from Andei */
2872 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2874 memcached_return_t status
;
2875 memcached_result_st
*result
, result_obj
;
2876 const char *key
= "abc";
2877 size_t key_len
= strlen("abc");
2878 const char *value
= "foobar";
2879 size_t value_len
= strlen(value
);
2881 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2883 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2884 test_true(status
== MEMCACHED_SUCCESS
);
2886 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2887 test_true(status
== MEMCACHED_SUCCESS
);
2889 result
= memcached_result_create(memc
, &result_obj
);
2892 memcached_result_create(memc
, &result_obj
);
2893 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2896 test_true(status
== MEMCACHED_SUCCESS
);
2898 memcached_result_free(result
);
2900 return TEST_SUCCESS
;
2903 #include "ketama_test_cases.h"
2904 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2906 memcached_return_t rc
;
2909 memcached_server_st
*server_pool
;
2914 memc
= memcached_create(NULL
);
2917 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2918 test_true(rc
== MEMCACHED_SUCCESS
);
2920 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2921 test_true(value
== 1);
2923 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2924 test_true(rc
== MEMCACHED_SUCCESS
);
2926 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2927 test_true(value
== MEMCACHED_HASH_MD5
);
2929 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");
2930 memcached_server_push(memc
, server_pool
);
2932 /* verify that the server list was parsed okay. */
2933 test_true(memcached_server_count(memc
) == 8);
2934 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2935 test_true(server_pool
[0].port
== 11211);
2936 test_true(server_pool
[0].weight
== 600);
2937 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2938 test_true(server_pool
[2].port
== 11211);
2939 test_true(server_pool
[2].weight
== 200);
2940 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2941 test_true(server_pool
[7].port
== 11211);
2942 test_true(server_pool
[7].weight
== 100);
2944 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2945 * us test the boundary wraparound.
2947 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2949 /* verify the standard ketama set. */
2950 for (x
= 0; x
< 99; x
++)
2952 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2954 memcached_server_instance_st instance
=
2955 memcached_server_instance_by_position(memc
, server_idx
);
2957 const char *hostname
= memcached_server_name(instance
);
2958 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2961 memcached_server_list_free(server_pool
);
2962 memcached_free(memc
);
2964 return TEST_SUCCESS
;
2967 /* Large mget() of missing keys with binary proto
2969 * If many binary quiet commands (such as getq's in an mget) fill the output
2970 * buffer and the server chooses not to respond, memcached_flush hangs. See
2971 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2974 /* sighandler_t function that always asserts false */
2975 static void fail(int unused
)
2982 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2987 return TEST_SKIPPED
;
2989 memcached_return_t rc
;
2992 size_t* key_lengths
;
2993 void (*oldalarm
)(int);
2994 memcached_st
*memc_clone
;
2996 memc_clone
= memcached_clone(NULL
, memc
);
2997 test_true(memc_clone
);
2999 /* only binproto uses getq for mget */
3000 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3002 /* empty the cache to ensure misses (hence non-responses) */
3003 rc
= memcached_flush(memc_clone
, 0);
3004 test_true(rc
== MEMCACHED_SUCCESS
);
3006 key_lengths
= calloc(key_count
, sizeof(size_t));
3007 keys
= calloc(key_count
, sizeof(char *));
3009 for (x
= 0; x
< key_count
; x
++)
3013 snprintf(buffer
, 30, "%u", x
);
3014 keys
[x
]= strdup(buffer
);
3015 key_lengths
[x
]= strlen(keys
[x
]);
3018 oldalarm
= signal(SIGALRM
, fail
);
3021 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3022 test_true(rc
== MEMCACHED_SUCCESS
);
3025 signal(SIGALRM
, oldalarm
);
3027 size_t keys_returned
;
3028 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3030 for (x
= 0; x
< key_count
; x
++)
3035 memcached_free(memc_clone
);
3037 return TEST_SUCCESS
;
3041 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3043 test_return_t test_rc
;
3044 test_rc
= pre_binary(memc
);
3046 if (test_rc
!= TEST_SUCCESS
)
3051 /* should work as of r580 */
3052 rc
= _user_supplied_bug21(memc
, 10);
3053 test_true(rc
== TEST_SUCCESS
);
3055 /* should fail as of r580 */
3056 rc
= _user_supplied_bug21(memc
, 1000);
3057 test_true(rc
== TEST_SUCCESS
);
3059 return TEST_SUCCESS
;
3062 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3065 memcached_server_instance_st instance
;
3067 memcached_return_t rc
;
3068 memcached_st
*memc
= memcached_create(NULL
);
3071 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3072 test_true(rc
== MEMCACHED_SUCCESS
);
3074 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3075 test_true(value
== 1);
3077 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3078 test_true(rc
== MEMCACHED_SUCCESS
);
3080 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3081 test_true(value
== MEMCACHED_HASH_MD5
);
3083 /* server should be removed when in delay */
3084 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3085 test_true(rc
== MEMCACHED_SUCCESS
);
3087 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3088 test_true(value
== 1);
3090 memcached_server_st
*server_pool
;
3091 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");
3092 memcached_server_push(memc
, server_pool
);
3094 /* verify that the server list was parsed okay. */
3095 test_true(memcached_server_count(memc
) == 8);
3096 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3097 test_true(server_pool
[0].port
== 11211);
3098 test_true(server_pool
[0].weight
== 600);
3099 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3100 test_true(server_pool
[2].port
== 11211);
3101 test_true(server_pool
[2].weight
== 200);
3102 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3103 test_true(server_pool
[7].port
== 11211);
3104 test_true(server_pool
[7].weight
== 100);
3106 instance
= memcached_server_instance_by_position(memc
, 2);
3107 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3108 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3111 This would not work if there were only two hosts.
3113 for (size_t x
= 0; x
< 99; x
++)
3115 memcached_autoeject(memc
);
3116 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3117 test_true(server_idx
!= 2);
3120 /* and re-added when it's back. */
3121 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3122 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3123 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3124 memc
->distribution
);
3125 for (size_t x
= 0; x
< 99; x
++)
3127 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3128 // We re-use instance from above.
3130 memcached_server_instance_by_position(memc
, server_idx
);
3131 const char *hostname
= memcached_server_name(instance
);
3132 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3135 memcached_server_list_free(server_pool
);
3136 memcached_free(memc
);
3138 return TEST_SUCCESS
;
3141 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3145 memcached_return_t rc
;
3146 memcached_st
*memc
= memcached_create(NULL
);
3150 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3151 test_true(rc
== MEMCACHED_SUCCESS
);
3153 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3154 test_true(value
== 1);
3156 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3157 test_true(rc
== MEMCACHED_SUCCESS
);
3159 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3160 test_true(value
== MEMCACHED_HASH_MD5
);
3163 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3165 memcached_server_st
*server_pool
;
3166 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");
3167 memcached_server_push(memc
, server_pool
);
3169 // @todo this needs to be refactored to actually test something.
3172 if ((fp
= fopen("ketama_keys.txt", "w")))
3176 printf("cannot write to file ketama_keys.txt");
3177 return TEST_FAILURE
;
3180 for (int x
= 0; x
< 10000; x
++)
3183 snprintf(key
, sizeof(key
), "%d", x
);
3185 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3186 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3187 in_port_t port
= memc
->hosts
[server_idx
].port
;
3188 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3189 memcached_server_instance_st instance
=
3190 memcached_server_instance_by_position(memc
, host_index
);
3194 memcached_server_list_free(server_pool
);
3195 memcached_free(memc
);
3197 return TEST_SUCCESS
;
3201 static test_return_t
result_static(memcached_st
*memc
)
3203 memcached_result_st result
;
3204 memcached_result_st
*result_ptr
;
3206 result_ptr
= memcached_result_create(memc
, &result
);
3207 test_true(result
.options
.is_allocated
== false);
3208 test_true(memcached_is_initialized(&result
) == true);
3209 test_true(result_ptr
);
3210 test_true(result_ptr
== &result
);
3212 memcached_result_free(&result
);
3214 test_true(result
.options
.is_allocated
== false);
3215 test_true(memcached_is_initialized(&result
) == false);
3217 return TEST_SUCCESS
;
3220 static test_return_t
result_alloc(memcached_st
*memc
)
3222 memcached_result_st
*result_ptr
;
3224 result_ptr
= memcached_result_create(memc
, NULL
);
3225 test_true(result_ptr
);
3226 test_true(result_ptr
->options
.is_allocated
== true);
3227 test_true(memcached_is_initialized(result_ptr
) == true);
3228 memcached_result_free(result_ptr
);
3230 return TEST_SUCCESS
;
3233 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3236 pairs_free(global_pairs
);
3238 return TEST_SUCCESS
;
3241 static test_return_t
generate_pairs(memcached_st
*memc
)
3244 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3245 global_count
= GLOBAL_COUNT
;
3247 for (size_t x
= 0; x
< global_count
; x
++)
3249 global_keys
[x
]= global_pairs
[x
].key
;
3250 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3253 return TEST_SUCCESS
;
3256 static test_return_t
generate_large_pairs(memcached_st
*memc
)
3259 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3260 global_count
= GLOBAL2_COUNT
;
3262 for (size_t x
= 0; x
< global_count
; x
++)
3264 global_keys
[x
]= global_pairs
[x
].key
;
3265 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3268 return TEST_SUCCESS
;
3271 static test_return_t
generate_data(memcached_st
*memc
)
3273 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3275 test_true(check_execute
== global_count
);
3277 return TEST_SUCCESS
;
3280 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3282 memcached_stat_st
*stat_p
;
3283 memcached_return_t rc
;
3284 uint32_t host_index
= 0;
3285 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3287 test_true(check_execute
== global_count
);
3289 //TODO: hosts used size stats
3290 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3293 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3295 /* This test was changes so that "make test" would work properlly */
3297 memcached_server_instance_st instance
=
3298 memcached_server_instance_by_position(memc
, host_index
);
3300 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3302 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3305 memcached_stat_free(NULL
, stat_p
);
3307 return TEST_SUCCESS
;
3309 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3314 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3315 generate_data(memc
);
3317 return TEST_SUCCESS
;
3320 static test_return_t
get_read_count(memcached_st
*memc
)
3322 memcached_return_t rc
;
3323 memcached_st
*memc_clone
;
3325 memc_clone
= memcached_clone(NULL
, memc
);
3326 test_true(memc_clone
);
3328 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3332 size_t return_value_length
;
3336 for (size_t x
= count
= 0; x
< global_count
; x
++)
3338 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3339 &return_value_length
, &flags
, &rc
);
3340 if (rc
== MEMCACHED_SUCCESS
)
3349 memcached_free(memc_clone
);
3351 return TEST_SUCCESS
;
3354 static test_return_t
get_read(memcached_st
*memc
)
3356 memcached_return_t rc
;
3360 size_t return_value_length
;
3363 for (size_t x
= 0; x
< global_count
; x
++)
3365 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3366 &return_value_length
, &flags
, &rc
);
3368 test_true(return_value);
3369 test_true(rc == MEMCACHED_SUCCESS);
3371 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3376 return TEST_SUCCESS
;
3379 static test_return_t
mget_read(memcached_st
*memc
)
3381 memcached_return_t rc
;
3383 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3384 return TEST_SKIPPED
;
3386 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3388 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3390 // Go fetch the keys and test to see if all of them were returned
3392 size_t keys_returned
;
3393 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3395 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3396 test_true_got(global_count
== keys_returned
, buffer
);
3400 return TEST_SUCCESS
;
3403 static test_return_t
mget_read_result(memcached_st
*memc
)
3405 memcached_return_t rc
;
3407 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3408 return TEST_SKIPPED
;
3410 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3412 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3414 /* Turn this into a help function */
3416 memcached_result_st results_obj
;
3417 memcached_result_st
*results
;
3419 results
= memcached_result_create(memc
, &results_obj
);
3421 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3424 test_true(rc
== MEMCACHED_SUCCESS
);
3427 memcached_result_free(&results_obj
);
3430 return TEST_SUCCESS
;
3433 static test_return_t
mget_read_function(memcached_st
*memc
)
3435 memcached_return_t rc
;
3437 memcached_execute_fn callbacks
[1];
3439 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3440 return TEST_SKIPPED
;
3442 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3444 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3446 callbacks
[0]= &callback_counter
;
3448 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3450 return TEST_SUCCESS
;
3453 static test_return_t
delete_generate(memcached_st
*memc
)
3455 for (size_t x
= 0; x
< global_count
; x
++)
3457 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3460 return TEST_SUCCESS
;
3463 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3468 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3470 for (size_t x
= 0; x
< global_count
; x
++)
3472 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3475 return TEST_SUCCESS
;
3478 static test_return_t
add_host_test1(memcached_st
*memc
)
3480 memcached_return_t rc
;
3481 char servername
[]= "0.example.com";
3482 memcached_server_st
*servers
;
3484 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3486 test_true(1 == memcached_server_list_count(servers
));
3488 for (size_t x
= 2; x
< 20; x
++)
3490 char buffer
[SMALL_STRING_LEN
];
3492 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3493 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3495 test_true(rc
== MEMCACHED_SUCCESS
);
3496 test_true(x
== memcached_server_list_count(servers
));
3499 rc
= memcached_server_push(memc
, servers
);
3500 test_true(rc
== MEMCACHED_SUCCESS
);
3501 rc
= memcached_server_push(memc
, servers
);
3502 test_true(rc
== MEMCACHED_SUCCESS
);
3504 memcached_server_list_free(servers
);
3506 return TEST_SUCCESS
;
3509 static test_return_t
pre_nonblock(memcached_st
*memc
)
3511 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3513 return TEST_SUCCESS
;
3516 static test_return_t
pre_cork(memcached_st
*memc
)
3518 memcached_return_t rc
;
3521 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3524 return TEST_SKIPPED
;
3527 if (rc
== MEMCACHED_SUCCESS
)
3528 return TEST_SUCCESS
;
3530 return TEST_SKIPPED
;
3533 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3540 return TEST_SKIPPED
;
3543 if (rc
!= TEST_SUCCESS
)
3546 return pre_nonblock(memc
);
3549 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3551 memcached_return_t rc
= MEMCACHED_FAILURE
;
3552 memcached_st
*memc_clone
;
3554 memc_clone
= memcached_clone(NULL
, memc
);
3555 test_true(memc_clone
);
3556 // The memcached_version needs to be done on a clone, because the server
3557 // will not toggle protocol on an connection.
3558 memcached_version(memc_clone
);
3560 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3562 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3563 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3564 test_true(rc
== MEMCACHED_SUCCESS
);
3565 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3569 return TEST_SKIPPED
;
3572 memcached_free(memc_clone
);
3574 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3577 static test_return_t
pre_murmur(memcached_st
*memc
)
3579 #ifdef HAVE_MURMUR_HASH
3580 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3581 return TEST_SUCCESS
;
3584 return TEST_SKIPPED
;
3588 static test_return_t
pre_jenkins(memcached_st
*memc
)
3590 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3592 return TEST_SUCCESS
;
3596 static test_return_t
pre_md5(memcached_st
*memc
)
3598 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3600 return TEST_SUCCESS
;
3603 static test_return_t
pre_crc(memcached_st
*memc
)
3605 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3607 return TEST_SUCCESS
;
3610 static test_return_t
pre_hsieh(memcached_st
*memc
)
3612 #ifdef HAVE_HSIEH_HASH
3613 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3614 return TEST_SUCCESS
;
3617 return TEST_SKIPPED
;
3621 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3623 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3625 return TEST_SUCCESS
;
3628 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3630 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3632 return TEST_SUCCESS
;
3635 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3637 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3639 return TEST_SUCCESS
;
3642 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3644 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3646 return TEST_SUCCESS
;
3649 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3651 memcached_return_t rc
;
3654 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3655 test_true(rc
== MEMCACHED_SUCCESS
);
3657 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3658 test_true(value
== 1);
3660 return TEST_SUCCESS
;
3663 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3665 memcached_return_t rc
;
3668 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3669 test_true(rc
== MEMCACHED_SUCCESS
);
3671 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3672 test_true(value
== 1);
3674 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3675 test_true(rc
== MEMCACHED_SUCCESS
);
3677 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3678 test_true(value
== MEMCACHED_HASH_MD5
);
3680 return TEST_SUCCESS
;
3684 @note This should be testing to see if the server really supports the binary protocol.
3686 static test_return_t
pre_binary(memcached_st
*memc
)
3688 memcached_return_t rc
= MEMCACHED_FAILURE
;
3690 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3692 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3693 test_true(rc
== MEMCACHED_SUCCESS
);
3694 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3697 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3700 static test_return_t
pre_sasl(memcached_st
*memc
)
3702 memcached_return_t rc
= MEMCACHED_FAILURE
;
3704 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3705 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3706 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3707 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3709 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3711 memcached_server_st
*servers
= memcached_servers_parse(server
);
3712 test_true(servers
!= NULL
);
3713 memcached_servers_reset(memc
);
3714 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3715 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3716 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3717 test_true(rc
== MEMCACHED_SUCCESS
);
3723 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3726 static test_return_t
pre_replication(memcached_st
*memc
)
3728 test_return_t test_rc
;
3729 test_rc
= pre_binary(memc
);
3731 if (test_rc
!= TEST_SUCCESS
)
3735 * Make sure that we store the item on all servers
3736 * (master + replicas == number of servers)
3738 memcached_return_t rc
;
3739 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3740 memcached_server_count(memc
) - 1);
3741 test_true(rc
== MEMCACHED_SUCCESS
);
3742 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3744 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3748 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3752 rc
= pre_replication(memc
);
3753 if (rc
!= TEST_SUCCESS
)
3756 rc
= pre_nonblock(memc
);
3762 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3766 #ifdef HARD_MALLOC_TESTS
3767 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3775 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3779 #ifdef HARD_MALLOC_TESTS
3780 void *ret
= malloc(size
+ 8);
3783 ret
= (void*)((caddr_t
)ret
+ 8);
3786 void *ret
= malloc(size
);
3791 memset(ret
, 0xff, size
);
3798 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *context
)
3801 #ifdef HARD_MALLOC_TESTS
3802 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3803 void *nmem
= realloc(real_ptr
, size
+ 8);
3808 ret
= (void*)((caddr_t
)nmem
+ 8);
3814 return realloc(mem
, size
);
3819 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *context
)
3822 #ifdef HARD_MALLOC_TESTS
3823 void *mem
= my_malloc(ptr
, nelem
* size
);
3826 memset(mem
, 0, nelem
* size
);
3832 return calloc(nelem
, size
);
3837 static test_return_t
set_prefix(memcached_st
*memc
)
3839 memcached_return_t rc
;
3840 const char *key
= "mine";
3843 /* Make sure be default none exists */
3844 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3845 test_true(rc
== MEMCACHED_FAILURE
);
3847 /* Test a clean set */
3848 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3849 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3851 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3853 test_true(memcmp(value
, key
, 4) == 0);
3854 test_true(rc
== MEMCACHED_SUCCESS
);
3856 /* Test that we can turn it off */
3857 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3858 test_true(rc
== MEMCACHED_SUCCESS
);
3860 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3862 test_true(rc
== MEMCACHED_FAILURE
);
3864 /* Now setup for main test */
3865 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3866 test_true(rc
== MEMCACHED_SUCCESS
);
3868 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3870 test_true(rc
== MEMCACHED_SUCCESS
);
3871 test_true(memcmp(value
, key
, 4) == 0);
3873 /* Set to Zero, and then Set to something too large */
3876 memset(long_key
, 0, 255);
3878 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3879 test_true(rc
== MEMCACHED_SUCCESS
);
3881 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3883 test_true(rc
== MEMCACHED_FAILURE
);
3884 test_true(value
== NULL
);
3886 /* Test a long key for failure */
3887 /* TODO, extend test to determine based on setting, what result should be */
3888 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3889 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3890 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3891 test_true(rc
== MEMCACHED_SUCCESS
);
3893 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3894 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3895 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3896 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3898 /* Test for a bad prefix, but with a short key */
3899 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3900 test_true(rc
== MEMCACHED_SUCCESS
);
3902 strncpy(long_key
, "dog cat", sizeof(long_key
));
3903 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3904 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3907 return TEST_SUCCESS
;
3911 #ifdef MEMCACHED_ENABLE_DEPRECATED
3912 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3914 void *test_ptr
= NULL
;
3917 memcached_malloc_fn malloc_cb
=
3918 (memcached_malloc_fn
)my_malloc
;
3919 cb_ptr
= *(void **)&malloc_cb
;
3920 memcached_return_t rc
;
3922 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3923 test_true(rc
== MEMCACHED_SUCCESS
);
3924 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3925 test_true(rc
== MEMCACHED_SUCCESS
);
3926 test_true(test_ptr
== cb_ptr
);
3930 memcached_realloc_fn realloc_cb
=
3931 (memcached_realloc_fn
)my_realloc
;
3932 cb_ptr
= *(void **)&realloc_cb
;
3933 memcached_return_t rc
;
3935 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3936 test_true(rc
== MEMCACHED_SUCCESS
);
3937 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3938 test_true(rc
== MEMCACHED_SUCCESS
);
3939 test_true(test_ptr
== cb_ptr
);
3943 memcached_free_fn free_cb
=
3944 (memcached_free_fn
)my_free
;
3945 cb_ptr
= *(void **)&free_cb
;
3946 memcached_return_t rc
;
3948 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3949 test_true(rc
== MEMCACHED_SUCCESS
);
3950 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3951 test_true(rc
== MEMCACHED_SUCCESS
);
3952 test_true(test_ptr
== cb_ptr
);
3955 return TEST_SUCCESS
;
3960 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3962 memcached_return_t rc
;
3963 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3964 my_realloc
, my_calloc
, NULL
);
3965 test_true(rc
== MEMCACHED_FAILURE
);
3967 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3968 my_realloc
, my_calloc
, NULL
);
3970 memcached_malloc_fn mem_malloc
;
3971 memcached_free_fn mem_free
;
3972 memcached_realloc_fn mem_realloc
;
3973 memcached_calloc_fn mem_calloc
;
3974 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3975 &mem_realloc
, &mem_calloc
);
3977 test_true(mem_malloc
== my_malloc
);
3978 test_true(mem_realloc
== my_realloc
);
3979 test_true(mem_calloc
== my_calloc
);
3980 test_true(mem_free
== my_free
);
3982 return TEST_SUCCESS
;
3985 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
3988 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3989 memcached_hash_t hash
;
3990 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3991 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3994 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3995 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3997 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3999 if (hash
!= MEMCACHED_HASH_CRC
)
4000 return TEST_SKIPPED
;
4002 return TEST_SUCCESS
;
4005 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4008 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4009 memcached_hash_t hash
;
4010 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4011 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4014 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4015 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4017 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4019 if (hash
!= MEMCACHED_HASH_HSIEH
)
4020 return TEST_SKIPPED
;
4023 return TEST_SUCCESS
;
4026 static test_return_t
enable_cas(memcached_st
*memc
)
4028 unsigned int set
= 1;
4030 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4032 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4034 return TEST_SUCCESS
;
4037 return TEST_SKIPPED
;
4040 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4042 memcached_version(memc
);
4044 memcached_server_instance_st instance
=
4045 memcached_server_instance_by_position(memc
, 0);
4047 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4048 || instance
->minor_version
> 2)
4050 return TEST_SUCCESS
;
4053 return TEST_SKIPPED
;
4056 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4058 memcached_return_t rc
;
4061 memcached_servers_reset(memc
);
4063 if (stat("/tmp/memcached.socket", &buf
))
4064 return TEST_SKIPPED
;
4066 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4068 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4071 static test_return_t
pre_nodelay(memcached_st
*memc
)
4073 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4074 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4076 return TEST_SUCCESS
;
4079 static test_return_t
pre_settimer(memcached_st
*memc
)
4081 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4082 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4084 return TEST_SUCCESS
;
4087 static test_return_t
poll_timeout(memcached_st
*memc
)
4093 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4095 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4097 test_true(timeout
== 100);
4099 return TEST_SUCCESS
;
4102 static test_return_t
noreply_test(memcached_st
*memc
)
4104 memcached_return_t ret
;
4105 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4106 test_true(ret
== MEMCACHED_SUCCESS
);
4107 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4108 test_true(ret
== MEMCACHED_SUCCESS
);
4109 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4110 test_true(ret
== MEMCACHED_SUCCESS
);
4111 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4112 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4113 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4115 for (int count
=0; count
< 5; ++count
)
4117 for (size_t x
= 0; x
< 100; ++x
)
4120 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4121 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4123 size_t len
= (size_t)check_length
;
4128 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4131 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4134 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4137 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4140 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4146 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4150 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4151 ** API and is _ONLY_ done this way to verify that the library works the
4152 ** way it is supposed to do!!!!
4155 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4157 memcached_server_instance_st instance
=
4158 memcached_server_instance_by_position(memc
, x
);
4159 no_msg
+=(int)(instance
->cursor_active
);
4162 test_true(no_msg
== 0);
4163 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4166 ** Now validate that all items was set properly!
4168 for (size_t x
= 0; x
< 100; ++x
)
4172 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4174 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4176 size_t len
= (size_t)check_length
;
4179 char* value
=memcached_get(memc
, key
, strlen(key
),
4180 &length
, &flags
, &ret
);
4181 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4184 case 0: /* FALLTHROUGH */
4185 case 1: /* FALLTHROUGH */
4187 test_true(strncmp(value
, key
, len
) == 0);
4188 test_true(len
== length
);
4191 test_true(length
== len
* 2);
4194 test_true(length
== len
* 3);
4204 /* Try setting an illegal cas value (should not return an error to
4205 * the caller (because we don't expect a return message from the server)
4207 const char* keys
[]= {"0"};
4208 size_t lengths
[]= {1};
4211 memcached_result_st results_obj
;
4212 memcached_result_st
*results
;
4213 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4214 test_true(ret
== MEMCACHED_SUCCESS
);
4216 results
= memcached_result_create(memc
, &results_obj
);
4218 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4220 test_true(ret
== MEMCACHED_SUCCESS
);
4221 uint64_t cas
= memcached_result_cas(results
);
4222 memcached_result_free(&results_obj
);
4224 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4225 test_true(ret
== MEMCACHED_SUCCESS
);
4228 * The item will have a new cas value, so try to set it again with the old
4229 * value. This should fail!
4231 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4232 test_true(ret
== MEMCACHED_SUCCESS
);
4233 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4234 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4235 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4238 return TEST_SUCCESS
;
4241 static test_return_t
analyzer_test(memcached_st
*memc
)
4243 memcached_return_t rc
;
4244 memcached_stat_st
*memc_stat
;
4245 memcached_analysis_st
*report
;
4247 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4248 test_true(rc
== MEMCACHED_SUCCESS
);
4249 test_true(memc_stat
);
4251 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4252 test_true(rc
== MEMCACHED_SUCCESS
);
4256 memcached_stat_free(NULL
, memc_stat
);
4258 return TEST_SUCCESS
;
4261 /* Count the objects */
4262 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4267 (void)ptr
; (void)key
; (void)key_length
;
4268 size_t *counter
= (size_t *)context
;
4270 *counter
= *counter
+ 1;
4272 return MEMCACHED_SUCCESS
;
4275 static test_return_t
dump_test(memcached_st
*memc
)
4277 memcached_return_t rc
;
4279 memcached_dump_fn callbacks
[1];
4280 test_return_t main_rc
;
4282 callbacks
[0]= &callback_dump_counter
;
4284 /* No support for Binary protocol yet */
4285 if (memc
->flags
.binary_protocol
)
4286 return TEST_SUCCESS
;
4288 main_rc
= set_test3(memc
);
4290 test_true (main_rc
== TEST_SUCCESS
);
4292 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4293 test_true(rc
== MEMCACHED_SUCCESS
);
4295 /* We may have more then 32 if our previous flush has not completed */
4296 test_true(counter
>= 32);
4298 return TEST_SUCCESS
;
4301 #ifdef HAVE_LIBMEMCACHEDUTIL
4302 static void* connection_release(void *arg
)
4305 memcached_pool_st
* pool
;
4310 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4314 #define POOL_SIZE 10
4315 static test_return_t
connection_pool_test(memcached_st
*memc
)
4317 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4318 test_true(pool
!= NULL
);
4319 memcached_st
*mmc
[POOL_SIZE
];
4320 memcached_return_t rc
;
4322 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4324 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4325 test_true(mmc
[x
] != NULL
);
4326 test_true(rc
== MEMCACHED_SUCCESS
);
4329 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4330 test_true(rc
== MEMCACHED_SUCCESS
);
4334 memcached_pool_st
* pool
;
4336 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4337 pthread_create(&tid
, NULL
, connection_release
, &item
);
4338 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4339 test_true(rc
== MEMCACHED_SUCCESS
);
4340 pthread_join(tid
, NULL
);
4341 test_true(mmc
[9] == item
.mmc
);
4342 const char *key
= "key";
4343 size_t keylen
= strlen(key
);
4345 // verify that I can do ops with all connections
4346 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4347 test_true(rc
== MEMCACHED_SUCCESS
);
4349 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4351 uint64_t number_value
;
4352 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4353 test_true(rc
== MEMCACHED_SUCCESS
);
4354 test_true(number_value
== (x
+1));
4358 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4360 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4364 /* verify that I can set behaviors on the pool when I don't have all
4365 * of the connections in the pool. It should however be enabled
4366 * when I push the item into the pool
4368 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4369 test_true(mmc
[0] != NULL
);
4371 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4372 test_true(rc
== MEMCACHED_SUCCESS
);
4374 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4375 test_true(mmc
[1] != NULL
);
4377 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4378 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4379 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4381 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4382 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4383 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4385 test_true(memcached_pool_destroy(pool
) == memc
);
4387 return TEST_SUCCESS
;
4390 static test_return_t
util_version_test(memcached_st
*memc
)
4394 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4395 test_true(if_successful
== true);
4397 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4399 // We expect failure
4402 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4403 fprintf(stderr
, "\nDumping Server Information\n\n");
4404 memcached_server_fn callbacks
[1];
4406 callbacks
[0]= dump_server_information
;
4407 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4408 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4410 test_true(if_successful
== false);
4412 memcached_server_instance_st instance
=
4413 memcached_server_instance_by_position(memc
, 0);
4415 memcached_version(memc
);
4417 // We only use one binary when we test, so this should be just fine.
4418 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4419 test_true(if_successful
== true);
4421 if (instance
->micro_version
> 0)
4422 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4423 else if (instance
->minor_version
> 0)
4424 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4425 else if (instance
->major_version
> 0)
4426 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4428 test_true(if_successful
== true);
4430 if (instance
->micro_version
> 0)
4431 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4432 else if (instance
->minor_version
> 0)
4433 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4434 else if (instance
->major_version
> 0)
4435 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4437 test_true(if_successful
== false);
4439 return TEST_SUCCESS
;
4442 static test_return_t
ping_test(memcached_st
*memc
)
4444 memcached_return_t rc
;
4445 memcached_server_instance_st instance
=
4446 memcached_server_instance_by_position(memc
, 0);
4448 // Test both the version that returns a code, and the one that does not.
4449 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4450 memcached_server_port(instance
), NULL
));
4452 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4453 memcached_server_port(instance
), &rc
));
4455 test_true(rc
== MEMCACHED_SUCCESS
);
4457 return TEST_SUCCESS
;
4463 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4467 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4468 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4469 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4470 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4471 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4472 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4473 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4474 #ifdef HAVE_HSIEH_HASH
4475 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4477 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4478 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4479 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4481 return TEST_SUCCESS
;
4485 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4487 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4488 #ifdef HAVE_HSIEH_HASH
4489 expected_rc
= MEMCACHED_SUCCESS
;
4491 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4492 (uint64_t)MEMCACHED_HASH_HSIEH
);
4493 test_true(rc
== expected_rc
);
4495 return TEST_SUCCESS
;
4498 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4500 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4501 #ifdef HAVE_MURMUR_HASH
4502 expected_rc
= MEMCACHED_SUCCESS
;
4504 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4505 (uint64_t)MEMCACHED_HASH_MURMUR
);
4506 test_true(rc
== expected_rc
);
4508 return TEST_SUCCESS
;
4511 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4517 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4521 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4522 test_true(one_at_a_time_values
[x
] == hash_val
);
4525 return TEST_SUCCESS
;
4528 static test_return_t
md5_run (memcached_st
*memc
)
4534 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4538 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4539 test_true(md5_values
[x
] == hash_val
);
4542 return TEST_SUCCESS
;
4545 static test_return_t
crc_run (memcached_st
*memc
)
4551 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4555 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4556 test_true(crc_values
[x
] == hash_val
);
4559 return TEST_SUCCESS
;
4562 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4568 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4572 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4573 test_true(fnv1_64_values
[x
] == hash_val
);
4576 return TEST_SUCCESS
;
4579 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4585 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4589 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4590 test_true(fnv1a_64_values
[x
] == hash_val
);
4593 return TEST_SUCCESS
;
4596 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4602 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4606 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4607 test_true(fnv1_32_values
[x
] == hash_val
);
4610 return TEST_SUCCESS
;
4613 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4619 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4623 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4624 test_true(fnv1a_32_values
[x
] == hash_val
);
4627 return TEST_SUCCESS
;
4630 static test_return_t
hsieh_run (memcached_st
*memc
)
4636 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4640 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4641 test_true(hsieh_values
[x
] == hash_val
);
4644 return TEST_SUCCESS
;
4647 static test_return_t
murmur_run (memcached_st
*memc
)
4649 #ifdef WORDS_BIGENDIAN
4650 (void)murmur_values
;
4651 return TEST_SKIPPED
;
4657 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4661 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4662 test_true(murmur_values
[x
] == hash_val
);
4665 return TEST_SUCCESS
;
4669 static test_return_t
jenkins_run (memcached_st
*memc
)
4675 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4679 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4680 test_true(jenkins_values
[x
] == hash_val
);
4683 return TEST_SUCCESS
;
4686 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4689 return libhashkit_md5(string
, string_length
);
4692 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4695 return libhashkit_crc32(string
, string_length
);
4698 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4702 const hashkit_st
*kit
;
4704 hashkit_return_t hash_rc
;
4706 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};
4707 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};
4709 kit
= memcached_get_hashkit(memc
);
4711 hashkit_clone(&new_kit
, kit
);
4712 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4713 test_true(hash_rc
== HASHKIT_SUCCESS
);
4715 memcached_set_hashkit(memc
, &new_kit
);
4718 Verify Setting the hash.
4720 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4724 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4725 test_true(md5_values
[x
] == hash_val
);
4730 Now check memcached_st.
4732 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4736 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4737 test_true(md5_hosts
[x
] == hash_val
);
4740 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4741 test_true(hash_rc
== HASHKIT_SUCCESS
);
4743 memcached_set_hashkit(memc
, &new_kit
);
4746 Verify Setting the hash.
4748 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4752 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4753 test_true(crc_values
[x
] == hash_val
);
4756 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4760 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4761 test_true(crc_hosts
[x
] == hash_val
);
4764 return TEST_SUCCESS
;
4768 Test case adapted from John Gorman <johngorman2@gmail.com>
4770 We are testing the error condition when we connect to a server via memcached_get()
4771 but find that the server is not available.
4773 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4776 memcached_st
*tl_memc_h
;
4777 memcached_server_st
*servers
;
4779 const char *key
= "MemcachedLives";
4782 memcached_return rc
;
4786 tl_memc_h
= memcached_create(NULL
);
4787 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4788 memcached_server_push(tl_memc_h
, servers
);
4789 memcached_server_list_free(servers
);
4791 // See if memcached is reachable.
4792 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4795 test_true(len
== 0);
4796 test_false(rc
== MEMCACHED_SUCCESS
);
4798 memcached_free(tl_memc_h
);
4800 return TEST_SUCCESS
;
4804 We connect to a server which exists, but search for a key that does not exist.
4806 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4808 const char *key
= "MemcachedKeyNotEXIST";
4811 memcached_return rc
;
4814 // See if memcached is reachable.
4815 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4818 test_true(len
== 0);
4819 test_true(rc
== MEMCACHED_NOTFOUND
);
4821 return TEST_SUCCESS
;
4825 Test case adapted from John Gorman <johngorman2@gmail.com>
4827 We are testing the error condition when we connect to a server via memcached_get_by_key()
4828 but find that the server is not available.
4830 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4833 memcached_st
*tl_memc_h
;
4834 memcached_server_st
*servers
;
4836 const char *key
= "MemcachedLives";
4839 memcached_return rc
;
4843 tl_memc_h
= memcached_create(NULL
);
4844 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4845 memcached_server_push(tl_memc_h
, servers
);
4846 memcached_server_list_free(servers
);
4848 // See if memcached is reachable.
4849 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4852 test_true(len
== 0);
4853 test_false(rc
== MEMCACHED_SUCCESS
);
4855 memcached_free(tl_memc_h
);
4857 return TEST_SUCCESS
;
4861 We connect to a server which exists, but search for a key that does not exist.
4863 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4865 const char *key
= "MemcachedKeyNotEXIST";
4868 memcached_return rc
;
4871 // See if memcached is reachable.
4872 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4875 test_true(len
== 0);
4876 test_true(rc
== MEMCACHED_NOTFOUND
);
4878 return TEST_SUCCESS
;
4882 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4884 memcached_return_t rc
;
4887 memcached_server_st
*server_pool
;
4892 memc
= memcached_create(NULL
);
4895 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4896 test_true(rc
== MEMCACHED_SUCCESS
);
4898 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4899 test_true(value
== 1);
4901 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4902 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4905 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");
4906 memcached_server_push(memc
, server_pool
);
4908 /* verify that the server list was parsed okay. */
4909 test_true(memcached_server_count(memc
) == 8);
4910 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4911 test_true(server_pool
[0].port
== 11211);
4912 test_true(server_pool
[0].weight
== 600);
4913 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4914 test_true(server_pool
[2].port
== 11211);
4915 test_true(server_pool
[2].weight
== 200);
4916 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4917 test_true(server_pool
[7].port
== 11211);
4918 test_true(server_pool
[7].weight
== 100);
4920 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4921 * us test the boundary wraparound.
4923 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4925 /* verify the standard ketama set. */
4926 for (x
= 0; x
< 99; x
++)
4928 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4929 memcached_server_instance_st instance
=
4930 memcached_server_instance_by_position(memc
, server_idx
);
4931 const char *hostname
= memcached_server_name(instance
);
4933 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4936 memcached_server_list_free(server_pool
);
4937 memcached_free(memc
);
4939 return TEST_SUCCESS
;
4942 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4944 memcached_return_t rc
;
4947 memcached_server_st
*server_pool
;
4952 memc
= memcached_create(NULL
);
4955 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4956 test_true(rc
== MEMCACHED_SUCCESS
);
4958 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4959 test_true(value
== 1);
4961 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4962 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4964 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");
4965 memcached_server_push(memc
, server_pool
);
4967 /* verify that the server list was parsed okay. */
4968 test_true(memcached_server_count(memc
) == 8);
4969 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4970 test_true(server_pool
[0].port
== 11211);
4971 test_true(server_pool
[0].weight
== 600);
4972 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4973 test_true(server_pool
[2].port
== 11211);
4974 test_true(server_pool
[2].weight
== 200);
4975 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4976 test_true(server_pool
[7].port
== 11211);
4977 test_true(server_pool
[7].weight
== 100);
4979 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4980 * us test the boundary wraparound.
4982 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4984 /* verify the standard ketama set. */
4985 for (x
= 0; x
< 99; x
++)
4987 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4989 memcached_server_instance_st instance
=
4990 memcached_server_instance_by_position(memc
, server_idx
);
4992 const char *hostname
= memcached_server_name(instance
);
4994 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
4997 memcached_server_list_free(server_pool
);
4998 memcached_free(memc
);
5000 return TEST_SUCCESS
;
5003 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5005 test_return_t test_rc
;
5006 test_rc
= pre_binary(memc
);
5008 if (test_rc
!= TEST_SUCCESS
)
5011 memcached_return_t ret
;
5012 const char *key
= "regression_bug_434484";
5013 size_t keylen
= strlen(key
);
5015 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5016 test_true(ret
== MEMCACHED_NOTSTORED
);
5018 size_t size
= 2048 * 1024;
5019 void *data
= calloc(1, size
);
5020 test_true(data
!= NULL
);
5021 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5022 test_true(ret
== MEMCACHED_E2BIG
);
5025 return TEST_SUCCESS
;
5028 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5030 test_return_t test_rc
;
5031 test_rc
= pre_binary(memc
);
5033 if (test_rc
!= TEST_SUCCESS
)
5036 memcached_return_t rc
;
5038 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5041 * I only want to hit only _one_ server so I know the number of requests I'm
5042 * sending in the pipleine to the server. Let's try to do a multiget of
5043 * 1024 (that should satisfy most users don't you think?). Future versions
5044 * will include a mget_execute function call if you need a higher number.
5046 uint32_t number_of_hosts
= memcached_server_count(memc
);
5047 memc
->number_of_hosts
= 1;
5048 const size_t max_keys
= 1024;
5049 char **keys
= calloc(max_keys
, sizeof(char*));
5050 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5052 for (size_t x
= 0; x
< max_keys
; ++x
)
5056 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5058 test_true(keys
[x
] != NULL
);
5062 * Run two times.. the first time we should have 100% cache miss,
5063 * and the second time we should have 100% cache hits
5065 for (size_t y
= 0; y
< 2; y
++)
5067 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5068 test_true(rc
== MEMCACHED_SUCCESS
);
5069 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5073 /* The first iteration should give me a 100% cache miss. verify that*/
5074 char blob
[1024]= { 0 };
5076 test_true(counter
== 0);
5078 for (size_t x
= 0; x
< max_keys
; ++x
)
5080 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5081 blob
, sizeof(blob
), 0, 0);
5082 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5087 /* Verify that we received all of the key/value pairs */
5088 test_true(counter
== max_keys
);
5092 /* Release allocated resources */
5093 for (size_t x
= 0; x
< max_keys
; ++x
)
5100 memc
->number_of_hosts
= number_of_hosts
;
5102 return TEST_SUCCESS
;
5105 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5107 memcached_return_t rc
;
5108 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5109 test_true(rc
== MEMCACHED_SUCCESS
);
5111 return regression_bug_434843(memc
);
5114 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5116 memcached_return_t rc
;
5117 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5118 test_true(rc
== MEMCACHED_SUCCESS
);
5120 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5121 test_true(rc
== MEMCACHED_SUCCESS
);
5122 test_true(bytes
!= NULL
);
5123 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5125 test_true(rc
== MEMCACHED_SUCCESS
);
5126 test_true(bytes_read
!= NULL
);
5128 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5129 "bytes_written", &rc
);
5130 test_true(rc
== MEMCACHED_SUCCESS
);
5131 test_true(bytes_written
!= NULL
);
5133 test_true(strcmp(bytes
, bytes_read
) != 0);
5134 test_true(strcmp(bytes
, bytes_written
) != 0);
5136 /* Release allocated resources */
5139 free(bytes_written
);
5140 memcached_stat_free(NULL
, memc_stat
);
5142 return TEST_SUCCESS
;
5146 * The test case isn't obvious so I should probably document why
5147 * it works the way it does. Bug 442914 was caused by a bug
5148 * in the logic in memcached_purge (it did not handle the case
5149 * where the number of bytes sent was equal to the watermark).
5150 * In this test case, create messages so that we hit that case
5151 * and then disable noreply mode and issue a new command to
5152 * verify that it isn't stuck. If we change the format for the
5153 * delete command or the watermarks, we need to update this
5156 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5158 memcached_return_t rc
;
5159 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5160 test_true(rc
== MEMCACHED_SUCCESS
);
5161 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5163 uint32_t number_of_hosts
= memcached_server_count(memc
);
5164 memc
->number_of_hosts
= 1;
5169 for (uint32_t x
= 0; x
< 250; ++x
)
5171 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5172 rc
= memcached_delete(memc
, k
, len
, 0);
5173 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5176 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5179 rc
= memcached_delete(memc
, k
, len
, 0);
5180 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5182 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5183 test_true(rc
== MEMCACHED_SUCCESS
);
5184 rc
= memcached_delete(memc
, k
, len
, 0);
5185 test_true(rc
== MEMCACHED_NOTFOUND
);
5187 memc
->number_of_hosts
= number_of_hosts
;
5189 return TEST_SUCCESS
;
5192 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5194 memcached_server_instance_st instance_one
;
5195 memcached_server_instance_st instance_two
;
5197 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5198 return TEST_SKIPPED
;
5200 memcached_return_t rc
;
5202 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5203 test_true(rc
== MEMCACHED_SUCCESS
);
5205 const size_t max_keys
= 100;
5206 char **keys
= calloc(max_keys
, sizeof(char*));
5207 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5209 for (size_t x
= 0; x
< max_keys
; ++x
)
5213 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5215 test_true(keys
[x
] != NULL
);
5216 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5217 test_true(rc
== MEMCACHED_SUCCESS
);
5221 ** We are using the quiet commands to store the replicas, so we need
5222 ** to ensure that all of them are processed before we can continue.
5223 ** In the test we go directly from storing the object to trying to
5224 ** receive the object from all of the different servers, so we
5225 ** could end up in a race condition (the memcached server hasn't yet
5226 ** processed the quiet command from the replication set when it process
5227 ** the request from the other client (created by the clone)). As a
5228 ** workaround for that we call memcached_quit to send the quit command
5229 ** to the server and wait for the response ;-) If you use the test code
5230 ** as an example for your own code, please note that you shouldn't need
5233 memcached_quit(memc
);
5235 /* Verify that all messages are stored, and we didn't stuff too much
5238 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5239 test_true(rc
== MEMCACHED_SUCCESS
);
5242 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5243 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5244 /* Verify that we received all of the key/value pairs */
5245 test_true(counter
== max_keys
);
5247 memcached_quit(memc
);
5249 * Don't do the following in your code. I am abusing the internal details
5250 * within the library, and this is not a supported interface.
5251 * This is to verify correct behavior in the library. Fake that two servers
5254 instance_one
= memcached_server_instance_by_position(memc
, 0);
5255 instance_two
= memcached_server_instance_by_position(memc
, 2);
5256 in_port_t port0
= instance_one
->port
;
5257 in_port_t port2
= instance_two
->port
;
5259 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5260 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5262 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5263 test_true(rc
== MEMCACHED_SUCCESS
);
5266 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5267 test_true(counter
== (unsigned int)max_keys
);
5269 /* restore the memc handle */
5270 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5271 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5273 memcached_quit(memc
);
5275 /* Remove half of the objects */
5276 for (size_t x
= 0; x
< max_keys
; ++x
)
5280 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5281 test_true(rc
== MEMCACHED_SUCCESS
);
5285 memcached_quit(memc
);
5286 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5287 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5289 /* now retry the command, this time we should have cache misses */
5290 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5291 test_true(rc
== MEMCACHED_SUCCESS
);
5294 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5295 test_true(counter
== (unsigned int)(max_keys
>> 1));
5297 /* Release allocated resources */
5298 for (size_t x
= 0; x
< max_keys
; ++x
)
5305 /* restore the memc handle */
5306 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5307 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5309 return TEST_SUCCESS
;
5312 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5314 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5315 test_true(memc_clone
!= NULL
);
5316 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5318 memcached_server_instance_st instance
=
5319 memcached_server_instance_by_position(memc_clone
, 0);
5321 if (instance
->major_version
> 1 ||
5322 (instance
->major_version
== 1 &&
5323 instance
->minor_version
> 2))
5325 /* Binary protocol doesn't support deferred delete */
5326 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5327 test_true(bin_clone
!= NULL
);
5328 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5329 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5330 memcached_free(bin_clone
);
5332 memcached_quit(memc_clone
);
5334 /* If we know the server version, deferred delete should fail
5335 * with invalid arguments */
5336 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5338 /* If we don't know the server version, we should get a protocol error */
5339 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5341 /* but there is a bug in some of the memcached servers (1.4) that treats
5342 * the counter as noreply so it doesn't send the proper error message
5344 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5346 /* And buffered mode should be disabled and we should get protocol error */
5347 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5348 rc
= memcached_delete(memc
, "foo", 3, 1);
5349 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5351 /* Same goes for noreply... */
5352 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5353 rc
= memcached_delete(memc
, "foo", 3, 1);
5354 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5356 /* but a normal request should go through (and be buffered) */
5357 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5358 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5360 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5361 /* unbuffered noreply should be success */
5362 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5363 /* unbuffered with reply should be not found... */
5364 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5365 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5368 memcached_free(memc_clone
);
5369 return TEST_SUCCESS
;
5373 /* Test memcached_server_get_last_disconnect
5374 * For a working server set, shall be NULL
5375 * For a set of non existing server, shall not be NULL
5377 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5379 memcached_return_t rc
;
5380 memcached_server_instance_st disconnected_server
;
5382 /* With the working set of server */
5383 const char *key
= "marmotte";
5384 const char *value
= "milka";
5386 memcached_reset_last_disconnected_server(memc
);
5387 rc
= memcached_set(memc
, key
, strlen(key
),
5388 value
, strlen(value
),
5389 (time_t)0, (uint32_t)0);
5390 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5392 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5393 test_true(disconnected_server
== NULL
);
5395 /* With a non existing server */
5397 memcached_server_st
*servers
;
5399 const char *server_list
= "localhost:9";
5401 servers
= memcached_servers_parse(server_list
);
5403 mine
= memcached_create(NULL
);
5404 rc
= memcached_server_push(mine
, servers
);
5405 test_true(rc
== MEMCACHED_SUCCESS
);
5406 memcached_server_list_free(servers
);
5409 rc
= memcached_set(mine
, key
, strlen(key
),
5410 value
, strlen(value
),
5411 (time_t)0, (uint32_t)0);
5412 test_true(rc
!= MEMCACHED_SUCCESS
);
5414 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5415 if (disconnected_server
== NULL
)
5417 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5420 test_true(disconnected_server
!= NULL
);
5421 test_true(memcached_server_port(disconnected_server
)== 9);
5422 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5424 memcached_quit(mine
);
5425 memcached_free(mine
);
5427 return TEST_SUCCESS
;
5430 static test_return_t
test_verbosity(memcached_st
*memc
)
5432 memcached_verbosity(memc
, 3);
5434 return TEST_SUCCESS
;
5437 static test_return_t
test_server_failure(memcached_st
*memc
)
5439 memcached_st
*local_memc
;
5440 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5442 local_memc
= memcached_create(NULL
);
5444 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5445 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5447 uint32_t server_count
= memcached_server_count(local_memc
);
5449 test_true(server_count
== 1);
5451 // Disable the server
5452 instance
= memcached_server_instance_by_position(local_memc
, 0);
5453 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5455 memcached_return_t rc
;
5456 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5458 (time_t)0, (uint32_t)0);
5459 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5461 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5462 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5464 (time_t)0, (uint32_t)0);
5465 test_true(rc
== MEMCACHED_SUCCESS
);
5468 memcached_free(local_memc
);
5470 return TEST_SUCCESS
;
5473 static test_return_t
test_cull_servers(memcached_st
*memc
)
5475 uint32_t count
= memcached_server_count(memc
);
5477 // Do not do this in your code, it is not supported.
5478 memc
->servers
[1].state
.is_dead
= true;
5479 memc
->state
.is_time_for_rebuild
= true;
5481 uint32_t new_count
= memcached_server_count(memc
);
5482 test_true(count
== new_count
);
5485 test_true(count
== new_count
+ 1 );
5488 return TEST_SUCCESS
;
5492 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5493 const char *key
, size_t key_length
,
5494 const char *value
, size_t value_length
,
5504 return MEMCACHED_SUCCESS
;
5507 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5509 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5510 test_true(rc
== MEMCACHED_SUCCESS
);
5512 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5513 test_true(rc
== MEMCACHED_SUCCESS
);
5515 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5516 test_true(rc
== MEMCACHED_SUCCESS
);
5518 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5519 test_true(rc
== MEMCACHED_SUCCESS
);
5521 return TEST_SUCCESS
;
5525 * This test ensures that the failure counter isn't incremented during
5526 * normal termination of the memcached instance.
5528 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5530 memcached_return_t rc
;
5531 memcached_server_instance_st instance
;
5533 /* Set value to force connection to the server */
5534 const char *key
= "marmotte";
5535 const char *value
= "milka";
5538 * Please note that I'm abusing the internal structures in libmemcached
5539 * in a non-portable way and you shouldn't be doing this. I'm only
5540 * doing this in order to verify that the library works the way it should
5542 uint32_t number_of_hosts
= memcached_server_count(memc
);
5543 memc
->number_of_hosts
= 1;
5545 /* Ensure that we are connected to the server by setting a value */
5546 rc
= memcached_set(memc
, key
, strlen(key
),
5547 value
, strlen(value
),
5548 (time_t)0, (uint32_t)0);
5549 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5552 instance
= memcached_server_instance_by_position(memc
, 0);
5553 /* The test is to see that the memcached_quit doesn't increase the
5554 * the server failure conter, so let's ensure that it is zero
5555 * before sending quit
5557 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5559 memcached_quit(memc
);
5561 /* Verify that it memcached_quit didn't increment the failure counter
5562 * Please note that this isn't bullet proof, because an error could
5565 test_true(instance
->server_failure_counter
== 0);
5567 /* restore the instance */
5568 memc
->number_of_hosts
= number_of_hosts
;
5570 return TEST_SUCCESS
;
5574 * This tests ensures expected disconnections (for some behavior changes
5575 * for instance) do not wrongly increase failure counter
5577 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5579 memcached_return rc
;
5581 memcached_st
*memc_clone
;
5582 memc_clone
= memcached_clone(NULL
, memc
);
5583 test_true(memc_clone
);
5585 /* Set value to force connection to the server */
5586 const char *key
= "marmotte";
5587 const char *value
= "milka";
5588 char *string
= NULL
;
5589 size_t string_length
;
5592 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5593 value
, strlen(value
),
5594 (time_t)0, (uint32_t)0);
5595 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5598 /* put failure limit to 1 */
5599 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5600 assert(rc
== MEMCACHED_SUCCESS
);
5602 /* Put a retry timeout to effectively activate failure_limit effect */
5603 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5604 assert(rc
== MEMCACHED_SUCCESS
);
5606 /* change behavior that triggers memcached_quit()*/
5607 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5608 assert(rc
== MEMCACHED_SUCCESS
);
5611 /* Check if we still are connected */
5612 string
= memcached_get(memc_clone
, key
, strlen(key
),
5613 &string_length
, &flags
, &rc
);
5615 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5619 return TEST_SUCCESS
;
5626 * Test that ensures mget_execute does not end into recursive calls that finally fails
5628 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5630 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5631 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5632 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5633 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5634 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5637 return TEST_SKIPPED
; // My MAC can't handle this test
5641 * I only want to hit _one_ server so I know the number of requests I'm
5642 * sending in the pipeline.
5644 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5645 memc
->number_of_hosts
= 1;
5646 size_t max_keys
= 20480;
5649 char **keys
= calloc(max_keys
, sizeof(char*));
5650 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5652 /* First add all of the items.. */
5654 char blob
[1024]= { 0 };
5655 memcached_return rc
;
5656 for (size_t x
= 0; x
< max_keys
; ++x
)
5659 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5661 assert(keys
[x
] != NULL
);
5662 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5664 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5666 break; // We are out of business
5669 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5671 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5674 sleep(1);// We will try to sleep
5677 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5679 // We failed to send everything.
5684 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5687 /* Try to get all of them with a large multiget */
5689 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5690 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5691 (size_t)max_keys
, callbacks
, &counter
, 1);
5693 assert(rc
== MEMCACHED_SUCCESS
);
5694 char* the_value
= NULL
;
5695 char the_key
[MEMCACHED_MAX_KEY
];
5696 size_t the_key_length
;
5697 size_t the_value_length
;
5701 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5703 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5709 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5712 assert(rc
== MEMCACHED_END
);
5714 /* Verify that we got all of the items */
5715 assert(counter
== max_keys
);
5718 /* Release all allocated resources */
5719 for (size_t x
= 0; x
< max_keys
; ++x
)
5726 memc
->number_of_hosts
= number_of_hosts
;
5728 return TEST_SUCCESS
;
5731 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5735 memcached_st
*memc
= memcached_create(NULL
);
5737 memcached_server_add(memc
, "10.2.3.4", 11211);
5739 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5740 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5741 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5742 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5743 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5744 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5746 memcached_return_t rc
;
5750 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5752 test_true_got(rc
== MEMCACHED_TIMEOUT
, memcached_strerror(NULL
, rc
));
5754 memcached_free(memc
);
5756 return TEST_SUCCESS
;
5759 // Look for memory leak
5760 static test_return_t
regression_bug_728286(memcached_st
*unused
)
5763 memcached_server_st
*servers
= memcached_servers_parse("1.2.3.4:99");
5764 memcached_server_free(servers
);
5766 return TEST_SUCCESS
;
5769 static test_return_t
regression_bug_581030(memcached_st
*unused
)
5774 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5775 test_false(local_stat
);
5777 memcached_stat_free(NULL
, NULL
);
5780 return TEST_SUCCESS
;
5783 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5785 fprintf(stderr
, "Iteration #%u: ", it
);
5787 if(error
== MEMCACHED_ERRNO
)
5789 fprintf(stderr
, "system error %d from %s: %s\n",
5790 errno
, what
, strerror(errno
));
5794 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5795 memcached_strerror(mc
, error
));
5799 #define TEST_CONSTANT_CREATION 200
5801 static test_return_t
regression_bug_(memcached_st
*memc
)
5803 const char *remote_server
;
5806 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5808 return TEST_SKIPPED
;
5811 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5813 memcached_st
* mc
= memcached_create(NULL
);
5814 memcached_return rc
;
5816 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5817 if (rc
!= MEMCACHED_SUCCESS
)
5819 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5822 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5823 if (rc
!= MEMCACHED_SUCCESS
)
5825 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5828 rc
= memcached_server_add(mc
, remote_server
, 0);
5829 if (rc
!= MEMCACHED_SUCCESS
)
5831 memcached_die(mc
, rc
, "memcached_server_add", x
);
5834 const char *set_key
= "akey";
5835 const size_t set_key_len
= strlen(set_key
);
5836 const char *set_value
= "a value";
5837 const size_t set_value_len
= strlen(set_value
);
5839 if (rc
== MEMCACHED_SUCCESS
)
5843 size_t get_value_len
;
5845 uint32_t get_value_flags
;
5847 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5848 &get_value_flags
, &rc
);
5849 if (rc
!= MEMCACHED_SUCCESS
)
5851 memcached_die(mc
, rc
, "memcached_get", x
);
5857 (get_value_len
!= set_value_len
5858 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5860 fprintf(stderr
, "Values don't match?\n");
5861 rc
= MEMCACHED_FAILURE
;
5867 rc
= memcached_set(mc
,
5868 set_key
, set_key_len
,
5869 set_value
, set_value_len
,
5873 if (rc
!= MEMCACHED_SUCCESS
)
5875 memcached_die(mc
, rc
, "memcached_set", x
);
5882 if (rc
!= MEMCACHED_SUCCESS
)
5888 return TEST_SUCCESS
;
5892 * Test that the sasl authentication works. We cannot use the default
5893 * pool of servers, because that would require that all servers we want
5894 * to test supports SASL authentication, and that they use the default
5897 static test_return_t
sasl_auth_test(memcached_st
*memc
)
5899 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
5900 memcached_return_t rc
;
5902 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5903 test_true(rc
== MEMCACHED_SUCCESS
);
5904 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
5905 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
5906 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
5907 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
5908 memcached_quit(memc
);
5910 rc
= memcached_set_sasl_auth_data(memc
,
5911 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
5912 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
5913 test_true(rc
== MEMCACHED_SUCCESS
);
5915 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5916 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
5917 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
5919 memcached_quit(memc
);
5920 return TEST_SUCCESS
;
5923 return TEST_FAILURE
;
5927 /* Clean the server before beginning testing */
5929 {"util_version", 1, (test_callback_fn
)util_version_test
},
5930 {"flush", 0, (test_callback_fn
)flush_test
},
5931 {"init", 0, (test_callback_fn
)init_test
},
5932 {"allocation", 0, (test_callback_fn
)allocation_test
},
5933 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5934 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5935 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5936 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5937 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5938 {"clone_test", 0, (test_callback_fn
)clone_test
},
5939 {"connection_test", 0, (test_callback_fn
)connection_test
},
5940 {"callback_test", 0, (test_callback_fn
)callback_test
},
5941 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5942 {"error", 0, (test_callback_fn
)error_test
},
5943 {"set", 0, (test_callback_fn
)set_test
},
5944 {"set2", 0, (test_callback_fn
)set_test2
},
5945 {"set3", 0, (test_callback_fn
)set_test3
},
5946 {"dump", 1, (test_callback_fn
)dump_test
},
5947 {"add", 1, (test_callback_fn
)add_test
},
5948 {"replace", 1, (test_callback_fn
)replace_test
},
5949 {"delete", 1, (test_callback_fn
)delete_test
},
5950 {"get", 1, (test_callback_fn
)get_test
},
5951 {"get2", 0, (test_callback_fn
)get_test2
},
5952 {"get3", 0, (test_callback_fn
)get_test3
},
5953 {"get4", 0, (test_callback_fn
)get_test4
},
5954 {"partial mget", 0, (test_callback_fn
)get_test5
},
5955 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5956 {"increment", 0, (test_callback_fn
)increment_test
},
5957 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5958 {"decrement", 0, (test_callback_fn
)decrement_test
},
5959 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5960 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5961 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5962 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5963 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5964 {"quit", 0, (test_callback_fn
)quit_test
},
5965 {"mget", 1, (test_callback_fn
)mget_test
},
5966 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5967 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5968 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5969 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5970 {"mget_end", 0, (test_callback_fn
)mget_end
},
5971 {"get_stats", 0, (test_callback_fn
)get_stats
},
5972 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5973 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5974 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5975 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5976 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
5977 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
5978 {"read_through", 1, (test_callback_fn
)read_through
},
5979 {"delete_through", 1, (test_callback_fn
)delete_through
},
5980 {"noreply", 1, (test_callback_fn
)noreply_test
},
5981 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
5982 #ifdef HAVE_LIBMEMCACHEDUTIL
5983 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
5984 {"ping", 1, (test_callback_fn
)ping_test
},
5986 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
5987 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
5988 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
5989 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
5990 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
5994 test_st behavior_tests
[] ={
5995 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
5996 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
5997 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
5998 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6002 test_st basic_tests
[] ={
6003 {"init", 1, (test_callback_fn
)basic_init_test
},
6004 {"clone", 1, (test_callback_fn
)basic_clone_test
},
6005 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
6006 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
6007 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
6008 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
6012 test_st regression_binary_vs_block
[] ={
6013 {"block add", 1, (test_callback_fn
)block_add_regression
},
6014 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6018 test_st async_tests
[] ={
6019 {"add", 1, (test_callback_fn
)add_wrapper
},
6023 test_st string_tests
[] ={
6024 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6025 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6026 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6027 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6028 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6029 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6030 {"string_alloc_append_multiple", 0, (test_callback_fn
)string_alloc_append_multiple
},
6031 {0, 0, (test_callback_fn
)0}
6034 test_st result_tests
[] ={
6035 {"result static", 0, (test_callback_fn
)result_static
},
6036 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6037 {0, 0, (test_callback_fn
)0}
6040 test_st version_1_2_3
[] ={
6041 {"append", 0, (test_callback_fn
)append_test
},
6042 {"prepend", 0, (test_callback_fn
)prepend_test
},
6043 {"cas", 0, (test_callback_fn
)cas_test
},
6044 {"cas2", 0, (test_callback_fn
)cas2_test
},
6045 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6046 {0, 0, (test_callback_fn
)0}
6049 test_st user_tests
[] ={
6050 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6051 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6052 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6053 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6054 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6055 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6056 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6057 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6058 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6059 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6060 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6061 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6062 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6063 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6064 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6065 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6066 #if !defined(__sun) && !defined(__OpenBSD__)
6068 ** It seems to be something weird with the character sets..
6069 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6070 ** guess I need to find out how this is supposed to work.. Perhaps I need
6071 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6072 ** so just disable the code for now...).
6074 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6076 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6077 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6078 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6079 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6080 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6081 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6082 {0, 0, (test_callback_fn
)0}
6085 test_st replication_tests
[]= {
6086 {"set", 1, (test_callback_fn
)replication_set_test
},
6087 {"get", 0, (test_callback_fn
)replication_get_test
},
6088 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6089 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6090 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6091 {0, 0, (test_callback_fn
)0}
6095 * The following test suite is used to verify that we don't introduce
6096 * regression bugs. If you want more information about the bug / test,
6097 * you should look in the bug report at
6098 * http://bugs.launchpad.net/libmemcached
6100 test_st regression_tests
[]= {
6101 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6102 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6103 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6104 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6105 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6106 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6107 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6108 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6109 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6110 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6111 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6112 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6113 {0, 0, (test_callback_fn
)0}
6116 test_st sasl_auth_tests
[]= {
6117 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6118 {0, 0, (test_callback_fn
)0}
6121 test_st ketama_compatibility
[]= {
6122 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6123 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6124 {0, 0, (test_callback_fn
)0}
6127 test_st generate_tests
[] ={
6128 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6129 {"generate_data", 1, (test_callback_fn
)generate_data
},
6130 {"get_read", 0, (test_callback_fn
)get_read
},
6131 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6132 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6133 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6134 {"generate_data", 1, (test_callback_fn
)generate_data
},
6135 {"mget_read", 0, (test_callback_fn
)mget_read
},
6136 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6137 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6138 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6139 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6140 {"generate_data", 1, (test_callback_fn
)generate_data
},
6141 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6142 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6143 {0, 0, (test_callback_fn
)0}
6146 test_st consistent_tests
[] ={
6147 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6148 {"generate_data", 1, (test_callback_fn
)generate_data
},
6149 {"get_read", 0, (test_callback_fn
)get_read_count
},
6150 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6151 {0, 0, (test_callback_fn
)0}
6154 test_st consistent_weighted_tests
[] ={
6155 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6156 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6157 {"get_read", 0, (test_callback_fn
)get_read_count
},
6158 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6159 {0, 0, (test_callback_fn
)0}
6162 test_st hsieh_availability
[] ={
6163 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6164 {0, 0, (test_callback_fn
)0}
6167 test_st murmur_availability
[] ={
6168 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6169 {0, 0, (test_callback_fn
)0}
6173 test_st hash_sanity
[] ={
6174 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6175 {0, 0, (test_callback_fn
)0}
6179 test_st ketama_auto_eject_hosts
[] ={
6180 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6181 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6182 {0, 0, (test_callback_fn
)0}
6185 test_st hash_tests
[] ={
6186 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6187 {"md5", 0, (test_callback_fn
)md5_run
},
6188 {"crc", 0, (test_callback_fn
)crc_run
},
6189 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6190 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6191 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6192 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6193 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6194 {"murmur", 0, (test_callback_fn
)murmur_run
},
6195 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6196 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6197 {0, 0, (test_callback_fn
)0}
6200 test_st error_conditions
[] ={
6201 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6202 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6203 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6204 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6205 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6206 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6207 {0, 0, (test_callback_fn
)0}
6211 test_st parser_tests
[] ={
6212 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6213 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6214 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6215 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6216 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6217 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6218 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6219 {"memcached_parse_configure_file", 0, (test_callback_fn
)memcached_parse_configure_file_test
},
6220 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6221 {"randomly generated options", 0, (test_callback_fn
)random_statement_build_test
},
6222 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6223 {"server", 0, (test_callback_fn
)server_test
},
6224 {"servers", 0, (test_callback_fn
)servers_test
},
6225 {0, 0, (test_callback_fn
)0}
6228 collection_st collection
[] ={
6230 {"hash_sanity", 0, 0, hash_sanity
},
6232 {"basic", 0, 0, basic_tests
},
6233 {"hsieh_availability", 0, 0, hsieh_availability
},
6234 {"murmur_availability", 0, 0, murmur_availability
},
6235 {"block", 0, 0, tests
},
6236 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6237 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6238 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6239 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6240 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6241 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6242 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6243 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6244 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6245 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6246 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6247 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6248 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6249 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6250 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6251 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6252 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6253 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6254 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6255 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6256 #ifdef MEMCACHED_ENABLE_DEPRECATED
6257 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6259 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6260 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6261 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6262 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6263 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6264 {"string", 0, 0, string_tests
},
6265 {"result", 0, 0, result_tests
},
6266 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6267 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6268 {"user", 0, 0, user_tests
},
6269 {"generate", 0, 0, generate_tests
},
6270 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6271 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6272 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6273 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6274 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6275 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6276 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6278 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6279 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6280 {"consistent_not", 0, 0, consistent_tests
},
6281 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6282 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6283 {"ketama_compat", 0, 0, ketama_compatibility
},
6284 {"test_hashes", 0, 0, hash_tests
},
6285 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6286 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6287 {"regression", 0, 0, regression_tests
},
6288 {"behaviors", 0, 0, behavior_tests
},
6289 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6290 {"error_conditions", 0, 0, error_conditions
},
6291 {"parser", 0, 0, parser_tests
},
6295 #define SERVERS_TO_CREATE 5
6297 #include "libmemcached_world.h"
6299 void get_world(world_st
*world
)
6301 world
->collections
= collection
;
6303 world
->create
= (test_callback_create_fn
)world_create
;
6304 world
->destroy
= (test_callback_fn
)world_destroy
;
6306 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6307 world
->test
.flush
= (test_callback_fn
)world_flush
;
6308 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6309 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6310 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6312 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6313 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6315 world
->runner
= &defualt_libmemcached_runner
;