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/replication.h"
39 #include "tests/basic.h"
40 #include "tests/error_conditions.h"
41 #include "tests/print.h"
44 #ifdef HAVE_LIBMEMCACHEDUTIL
46 #include "libmemcached/memcached_util.h"
49 #include "hash_results.h"
51 #define GLOBAL_COUNT 10000
52 #define GLOBAL2_COUNT 100
53 #define SERVERS_TO_CREATE 5
54 static uint32_t global_count
;
56 static pairs_st
*global_pairs
;
57 static const char *global_keys
[GLOBAL_COUNT
];
58 static size_t global_keys_length
[GLOBAL_COUNT
];
61 static test_return_t
pre_binary(memcached_st
*memc
);
64 static test_return_t
init_test(memcached_st
*not_used
)
69 (void)memcached_create(&memc
);
70 memcached_free(&memc
);
75 static test_return_t
server_list_null_test(memcached_st
*ptr
)
77 memcached_server_st
*server_list
;
78 memcached_return_t rc
;
81 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
82 test_true(server_list
== NULL
);
84 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
85 test_true(server_list
== NULL
);
87 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
88 test_true(server_list
== NULL
);
93 #define TEST_PORT_COUNT 7
94 in_port_t test_ports
[TEST_PORT_COUNT
];
96 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
97 const memcached_server_st
*server
,
101 size_t bigger
= *((size_t *)(context
));
103 assert(bigger
<= memcached_server_port(server
));
104 *((size_t *)(context
))= memcached_server_port(server
);
106 return MEMCACHED_SUCCESS
;
109 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
110 const memcached_server_st
*instance
,
114 FILE *stream
= (FILE *)context
;
117 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
118 memcached_server_name(instance
),
119 memcached_server_port(instance
),
120 instance
->major_version
,
121 instance
->minor_version
,
122 instance
->micro_version
);
124 return MEMCACHED_SUCCESS
;
127 static test_return_t
server_sort_test(memcached_st
*ptr
)
129 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
131 memcached_return_t rc
;
132 memcached_server_fn callbacks
[1];
133 memcached_st
*local_memc
;
136 local_memc
= memcached_create(NULL
);
137 test_true(local_memc
);
138 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
140 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
142 test_ports
[x
]= (in_port_t
)random() % 64000;
143 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
144 test_true(memcached_server_count(local_memc
) == x
+ 1);
146 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
148 test_true(rc
== MEMCACHED_SUCCESS
);
151 callbacks
[0]= server_display_function
;
152 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
155 memcached_free(local_memc
);
160 static test_return_t
server_sort2_test(memcached_st
*ptr
)
162 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
163 memcached_return_t rc
;
164 memcached_server_fn callbacks
[1];
165 memcached_st
*local_memc
;
166 memcached_server_instance_st instance
;
169 local_memc
= memcached_create(NULL
);
170 test_true(local_memc
);
171 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
172 test_true(rc
== MEMCACHED_SUCCESS
);
174 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
175 test_true(rc
== MEMCACHED_SUCCESS
);
176 instance
= memcached_server_instance_by_position(local_memc
, 0);
177 test_true(memcached_server_port(instance
) == 43043);
179 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
180 test_true(rc
== MEMCACHED_SUCCESS
);
182 instance
= memcached_server_instance_by_position(local_memc
, 0);
183 test_true(memcached_server_port(instance
) == 43042);
185 instance
= memcached_server_instance_by_position(local_memc
, 1);
186 test_true(memcached_server_port(instance
) == 43043);
188 callbacks
[0]= server_display_function
;
189 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
192 memcached_free(local_memc
);
197 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
)
199 memcached_return_t rc
;
200 memcached_st local_memc
;
202 memcached_server_st
*servers
;
203 memcached_server_fn callbacks
[1];
205 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";
208 memc
= memcached_create(&local_memc
);
210 servers
= memcached_servers_parse(server_string
);
212 rc
= memcached_server_push(memc
, servers
);
213 memcached_server_list_free(servers
);
215 callbacks
[0]= server_print_callback
;
216 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
218 memcached_free(memc
);
223 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
,
224 const memcached_server_st
*server
,
228 uint32_t x
= *((uint32_t *)(context
));
231 assert(test_ports
[x
] == server
->port
);
232 *((uint32_t *)(context
))= ++x
;
234 return MEMCACHED_SUCCESS
;
237 static test_return_t
server_unsort_test(memcached_st
*ptr
)
239 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
240 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
241 memcached_return_t rc
;
242 memcached_server_fn callbacks
[1];
243 memcached_st
*local_memc
;
246 local_memc
= memcached_create(NULL
);
247 test_true(local_memc
);
249 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
251 test_ports
[x
]= (in_port_t
)(random() % 64000);
252 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
253 test_true(memcached_server_count(local_memc
) == x
+1);
255 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
257 test_true(rc
== MEMCACHED_SUCCESS
);
260 callbacks
[0]= server_display_unsort_function
;
261 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
263 /* Now we sort old data! */
264 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
265 callbacks
[0]= server_display_function
;
266 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
269 memcached_free(local_memc
);
274 static test_return_t
allocation_test(memcached_st
*not_used
)
278 memc
= memcached_create(NULL
);
280 memcached_free(memc
);
285 static test_return_t
clone_test(memcached_st
*memc
)
289 memcached_st
*memc_clone
;
290 memc_clone
= memcached_clone(NULL
, NULL
);
291 test_true(memc_clone
);
292 memcached_free(memc_clone
);
295 /* Can we init from null? */
297 memcached_st
*memc_clone
;
298 memc_clone
= memcached_clone(NULL
, memc
);
299 test_true(memc_clone
);
302 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
303 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
304 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
305 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
308 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
309 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
310 test_true(memc_clone
->distribution
== memc
->distribution
);
311 { // Test all of the flags
312 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
313 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
314 test_true(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
315 test_true(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
316 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
317 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
318 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
319 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
320 test_true(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
321 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
322 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
323 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
324 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
325 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
326 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
328 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
329 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
330 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
331 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
332 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
333 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
334 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
335 test_true(memc_clone
->on_clone
== memc
->on_clone
);
336 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
337 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
338 test_true(memc_clone
->recv_size
== memc
->recv_size
);
339 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
340 test_true(memc_clone
->send_size
== memc
->send_size
);
341 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
342 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
343 test_true(memc_clone
->user_data
== memc
->user_data
);
345 memcached_free(memc_clone
);
348 /* Can we init from struct? */
350 memcached_st declared_clone
;
351 memcached_st
*memc_clone
;
352 memset(&declared_clone
, 0 , sizeof(memcached_st
));
353 memc_clone
= memcached_clone(&declared_clone
, NULL
);
354 test_true(memc_clone
);
355 memcached_free(memc_clone
);
358 /* Can we init from struct? */
360 memcached_st declared_clone
;
361 memcached_st
*memc_clone
;
362 memset(&declared_clone
, 0 , sizeof(memcached_st
));
363 memc_clone
= memcached_clone(&declared_clone
, memc
);
364 test_true(memc_clone
);
365 memcached_free(memc_clone
);
371 static test_return_t
userdata_test(memcached_st
*memc
)
374 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
375 test_true(memcached_get_user_data(memc
) == foo
);
376 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
381 static test_return_t
connection_test(memcached_st
*memc
)
383 memcached_return_t rc
;
385 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
386 test_true(rc
== MEMCACHED_SUCCESS
);
391 static test_return_t
error_test(memcached_st
*memc
)
393 memcached_return_t rc
;
394 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
395 982370485U, 1263635348U, 4242906218U, 3829656100U,
396 1891735253U, 334139633U, 2257084983U, 3088286104U,
397 13199785U, 2542027183U, 1097051614U, 199566778U,
398 2748246961U, 2465192557U, 1664094137U, 2405439045U,
399 1842224848U, 692413798U, 3479807801U, 919913813U,
400 4269430871U, 610793021U, 527273862U, 1437122909U,
401 2300930706U, 2943759320U, 674306647U, 2400528935U,
402 54481931U, 4186304426U, 1741088401U, 2979625118U,
403 4159057246U, 3425930182U, 2593724503U, 1868899624U,
404 1769812374U, 2302537950U, 1110330676U, 3365377466U };
406 // You have updated the memcache_error messages but not updated docs/tests.
407 test_true(MEMCACHED_MAXIMUM_RETURN
== 44);
408 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
411 const char *msg
= memcached_strerror(memc
, rc
);
412 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
413 MEMCACHED_HASH_JENKINS
);
414 if (values
[rc
] != hash_val
)
416 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
417 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
419 test_true(values
[rc
] == hash_val
);
425 static test_return_t
set_test(memcached_st
*memc
)
427 memcached_return_t rc
;
428 const char *key
= "foo";
429 const char *value
= "when we sanitize";
431 rc
= memcached_set(memc
, key
, strlen(key
),
432 value
, strlen(value
),
433 (time_t)0, (uint32_t)0);
434 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
439 static test_return_t
append_test(memcached_st
*memc
)
441 memcached_return_t rc
;
442 const char *key
= "fig";
443 const char *in_value
= "we";
444 char *out_value
= NULL
;
448 rc
= memcached_flush(memc
, 0);
449 test_true(rc
== MEMCACHED_SUCCESS
);
451 rc
= memcached_set(memc
, key
, strlen(key
),
452 in_value
, strlen(in_value
),
453 (time_t)0, (uint32_t)0);
454 test_true(rc
== MEMCACHED_SUCCESS
);
456 rc
= memcached_append(memc
, key
, strlen(key
),
457 " the", strlen(" the"),
458 (time_t)0, (uint32_t)0);
459 test_true(rc
== MEMCACHED_SUCCESS
);
461 rc
= memcached_append(memc
, key
, strlen(key
),
462 " people", strlen(" people"),
463 (time_t)0, (uint32_t)0);
464 test_true(rc
== MEMCACHED_SUCCESS
);
466 out_value
= memcached_get(memc
, key
, strlen(key
),
467 &value_length
, &flags
, &rc
);
468 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
469 test_true(strlen("we the people") == value_length
);
470 test_true(rc
== MEMCACHED_SUCCESS
);
476 static test_return_t
append_binary_test(memcached_st
*memc
)
478 memcached_return_t rc
;
479 const char *key
= "numbers";
480 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
486 rc
= memcached_flush(memc
, 0);
487 test_true(rc
== MEMCACHED_SUCCESS
);
489 rc
= memcached_set(memc
,
492 (time_t)0, (uint32_t)0);
493 test_true(rc
== MEMCACHED_SUCCESS
);
495 for (x
= 0; store_list
[x
] ; x
++)
497 rc
= memcached_append(memc
,
499 (char *)&store_list
[x
], sizeof(uint32_t),
500 (time_t)0, (uint32_t)0);
501 test_true(rc
== MEMCACHED_SUCCESS
);
504 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
505 &value_length
, &flags
, &rc
);
506 test_true((value_length
== (sizeof(uint32_t) * x
)));
507 test_true(rc
== MEMCACHED_SUCCESS
);
509 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
511 test_true(*ptr
== store_list
[x
- counter
]);
519 static test_return_t
cas2_test(memcached_st
*memc
)
521 memcached_return_t rc
;
522 const char *keys
[]= {"fudge", "son", "food"};
523 size_t key_length
[]= {5, 3, 4};
524 const char *value
= "we the people";
525 size_t value_length
= strlen("we the people");
527 memcached_result_st results_obj
;
528 memcached_result_st
*results
;
531 rc
= memcached_flush(memc
, 0);
532 test_true(rc
== MEMCACHED_SUCCESS
);
534 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
536 for (x
= 0; x
< 3; x
++)
538 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
539 keys
[x
], key_length
[x
],
540 (time_t)50, (uint32_t)9);
541 test_true(rc
== MEMCACHED_SUCCESS
);
544 rc
= memcached_mget(memc
, keys
, key_length
, 3);
546 results
= memcached_result_create(memc
, &results_obj
);
548 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
550 test_true(results
->item_cas
);
551 test_true(rc
== MEMCACHED_SUCCESS
);
552 test_true(memcached_result_cas(results
));
554 test_true(!memcmp(value
, "we the people", strlen("we the people")));
555 test_true(strlen("we the people") == value_length
);
556 test_true(rc
== MEMCACHED_SUCCESS
);
558 memcached_result_free(&results_obj
);
563 static test_return_t
cas_test(memcached_st
*memc
)
565 memcached_return_t rc
;
566 const char *key
= "fun";
567 size_t key_length
= strlen(key
);
568 const char *value
= "we the people";
569 const char* keys
[2] = { key
, NULL
};
570 size_t keylengths
[2] = { strlen(key
), 0 };
571 size_t value_length
= strlen(value
);
572 const char *value2
= "change the value";
573 size_t value2_length
= strlen(value2
);
575 memcached_result_st results_obj
;
576 memcached_result_st
*results
;
579 rc
= memcached_flush(memc
, 0);
580 test_true(rc
== MEMCACHED_SUCCESS
);
582 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
584 rc
= memcached_set(memc
, key
, strlen(key
),
585 value
, strlen(value
),
586 (time_t)0, (uint32_t)0);
587 test_true(rc
== MEMCACHED_SUCCESS
);
589 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
591 results
= memcached_result_create(memc
, &results_obj
);
593 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
595 test_true(rc
== MEMCACHED_SUCCESS
);
596 test_true(memcached_result_cas(results
));
597 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
598 test_true(strlen(memcached_result_value(results
)) == value_length
);
599 test_true(rc
== MEMCACHED_SUCCESS
);
600 uint64_t cas
= memcached_result_cas(results
);
603 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
604 test_true(rc
== MEMCACHED_END
);
605 test_true(results
== NULL
);
608 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
609 test_true(rc
== MEMCACHED_SUCCESS
);
612 * The item will have a new cas value, so try to set it again with the old
613 * value. This should fail!
615 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
616 test_true(rc
== MEMCACHED_DATA_EXISTS
);
618 memcached_result_free(&results_obj
);
623 static test_return_t
prepend_test(memcached_st
*memc
)
625 memcached_return_t rc
;
626 const char *key
= "fig";
627 const char *value
= "people";
628 char *out_value
= NULL
;
632 rc
= memcached_flush(memc
, 0);
633 test_true(rc
== MEMCACHED_SUCCESS
);
635 rc
= memcached_set(memc
, key
, strlen(key
),
636 value
, strlen(value
),
637 (time_t)0, (uint32_t)0);
638 test_true(rc
== MEMCACHED_SUCCESS
);
640 rc
= memcached_prepend(memc
, key
, strlen(key
),
641 "the ", strlen("the "),
642 (time_t)0, (uint32_t)0);
643 test_true(rc
== MEMCACHED_SUCCESS
);
645 rc
= memcached_prepend(memc
, key
, strlen(key
),
646 "we ", strlen("we "),
647 (time_t)0, (uint32_t)0);
648 test_true(rc
== MEMCACHED_SUCCESS
);
650 out_value
= memcached_get(memc
, key
, strlen(key
),
651 &value_length
, &flags
, &rc
);
652 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
653 test_true(strlen("we the people") == value_length
);
654 test_true(rc
== MEMCACHED_SUCCESS
);
661 Set the value, then quit to make sure it is flushed.
662 Come back in and test that add fails.
664 static test_return_t
add_test(memcached_st
*memc
)
666 memcached_return_t rc
;
667 const char *key
= "foo";
668 const char *value
= "when we sanitize";
669 unsigned long long setting_value
;
671 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
673 rc
= memcached_set(memc
, key
, strlen(key
),
674 value
, strlen(value
),
675 (time_t)0, (uint32_t)0);
676 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
677 memcached_quit(memc
);
678 rc
= memcached_add(memc
, key
, strlen(key
),
679 value
, strlen(value
),
680 (time_t)0, (uint32_t)0);
682 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
685 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
689 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
696 ** There was a problem of leaking filedescriptors in the initial release
697 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
698 ** systems it seems that the kernel is slow on reclaiming the resources
699 ** because the connects starts to time out (the test doesn't do much
700 ** anyway, so just loop 10 iterations)
702 static test_return_t
add_wrapper(memcached_st
*memc
)
704 unsigned int max
= 10000;
712 for (uint32_t x
= 0; x
< max
; x
++)
718 static test_return_t
replace_test(memcached_st
*memc
)
720 memcached_return_t rc
;
721 const char *key
= "foo";
722 const char *value
= "when we sanitize";
723 const char *original
= "first we insert some data";
725 rc
= memcached_set(memc
, key
, strlen(key
),
726 original
, strlen(original
),
727 (time_t)0, (uint32_t)0);
728 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
730 rc
= memcached_replace(memc
, key
, strlen(key
),
731 value
, strlen(value
),
732 (time_t)0, (uint32_t)0);
733 test_true(rc
== MEMCACHED_SUCCESS
);
738 static test_return_t
delete_test(memcached_st
*memc
)
740 memcached_return_t rc
;
741 const char *key
= "foo";
742 const char *value
= "when we sanitize";
744 rc
= memcached_set(memc
, key
, strlen(key
),
745 value
, strlen(value
),
746 (time_t)0, (uint32_t)0);
747 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
749 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
750 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
755 static test_return_t
flush_test(memcached_st
*memc
)
757 memcached_return_t rc
;
759 rc
= memcached_flush(memc
, 0);
760 test_true(rc
== MEMCACHED_SUCCESS
);
765 static memcached_return_t
server_function(const memcached_st
*ptr
,
766 const memcached_server_st
*server
,
769 (void)ptr
; (void)server
; (void)context
;
772 return MEMCACHED_SUCCESS
;
775 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
778 strcpy(context
, "foo bad");
779 memcached_server_fn callbacks
[1];
781 callbacks
[0]= server_function
;
782 memcached_server_cursor(memc
, callbacks
, context
, 1);
786 static test_return_t
bad_key_test(memcached_st
*memc
)
788 memcached_return_t rc
;
789 const char *key
= "foo bad";
791 size_t string_length
;
793 memcached_st
*memc_clone
;
795 size_t max_keylen
= 0xffff;
797 // Just skip if we are in binary mode.
798 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
801 memc_clone
= memcached_clone(NULL
, memc
);
802 test_true(memc_clone
);
804 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
805 test_true(rc
== MEMCACHED_SUCCESS
);
807 /* All keys are valid in the binary protocol (except for length) */
808 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
810 string
= memcached_get(memc_clone
, key
, strlen(key
),
811 &string_length
, &flags
, &rc
);
812 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
813 test_true(string_length
== 0);
817 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
818 test_true(rc
== MEMCACHED_SUCCESS
);
819 string
= memcached_get(memc_clone
, key
, strlen(key
),
820 &string_length
, &flags
, &rc
);
821 test_true(rc
== MEMCACHED_NOTFOUND
);
822 test_true(string_length
== 0);
825 /* Test multi key for bad keys */
826 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
827 size_t key_lengths
[] = { 7, 7, 7 };
829 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
830 test_true(rc
== MEMCACHED_SUCCESS
);
832 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
833 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
835 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
836 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
840 /* The following test should be moved to the end of this function when the
841 memcached server is updated to allow max size length of the keys in the
844 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
845 test_true(rc
== MEMCACHED_SUCCESS
);
847 char *longkey
= malloc(max_keylen
+ 1);
850 memset(longkey
, 'a', max_keylen
+ 1);
851 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
852 &string_length
, &flags
, &rc
);
853 test_true(rc
== MEMCACHED_NOTFOUND
);
854 test_true(string_length
== 0);
857 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
858 &string_length
, &flags
, &rc
);
859 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
860 test_true(string_length
== 0);
867 /* Make sure zero length keys are marked as bad */
869 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
870 test_true(rc
== MEMCACHED_SUCCESS
);
871 string
= memcached_get(memc_clone
, key
, 0,
872 &string_length
, &flags
, &rc
);
873 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
874 test_true(string_length
== 0);
877 memcached_free(memc_clone
);
882 #define READ_THROUGH_VALUE "set for me"
883 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
886 memcached_result_st
*result
)
888 (void)memc
;(void)key
;(void)key_length
;
889 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
892 static test_return_t
read_through(memcached_st
*memc
)
894 memcached_return_t rc
;
895 const char *key
= "foo";
897 size_t string_length
;
899 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
901 string
= memcached_get(memc
, key
, strlen(key
),
902 &string_length
, &flags
, &rc
);
904 test_true(rc
== MEMCACHED_NOTFOUND
);
905 test_false(string_length
);
908 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
910 test_true(rc
== MEMCACHED_SUCCESS
);
912 string
= memcached_get(memc
, key
, strlen(key
),
913 &string_length
, &flags
, &rc
);
915 test_true(rc
== MEMCACHED_SUCCESS
);
916 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
917 test_strcmp(READ_THROUGH_VALUE
, string
);
920 string
= memcached_get(memc
, key
, strlen(key
),
921 &string_length
, &flags
, &rc
);
923 test_true(rc
== MEMCACHED_SUCCESS
);
924 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
925 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
931 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
935 (void)ptr
;(void)key_length
;
938 return MEMCACHED_SUCCESS
;
941 static test_return_t
delete_through(memcached_st
*memc
)
943 memcached_trigger_delete_key_fn callback
;
944 memcached_return_t rc
;
946 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
948 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
949 test_true(rc
== MEMCACHED_SUCCESS
);
954 static test_return_t
get_test(memcached_st
*memc
)
956 memcached_return_t rc
;
957 const char *key
= "foo";
959 size_t string_length
;
962 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
963 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
965 string
= memcached_get(memc
, key
, strlen(key
),
966 &string_length
, &flags
, &rc
);
968 test_true(rc
== MEMCACHED_NOTFOUND
);
969 test_false(string_length
);
975 static test_return_t
get_test2(memcached_st
*memc
)
977 memcached_return_t rc
;
978 const char *key
= "foo";
979 const char *value
= "when we sanitize";
981 size_t string_length
;
984 rc
= memcached_set(memc
, key
, strlen(key
),
985 value
, strlen(value
),
986 (time_t)0, (uint32_t)0);
987 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
989 string
= memcached_get(memc
, key
, strlen(key
),
990 &string_length
, &flags
, &rc
);
993 test_true(rc
== MEMCACHED_SUCCESS
);
994 test_true(string_length
== strlen(value
));
995 test_true(!memcmp(string
, value
, string_length
));
1002 static test_return_t
set_test2(memcached_st
*memc
)
1004 memcached_return_t rc
;
1005 const char *key
= "foo";
1006 const char *value
= "train in the brain";
1007 size_t value_length
= strlen(value
);
1010 for (x
= 0; x
< 10; x
++)
1012 rc
= memcached_set(memc
, key
, strlen(key
),
1013 value
, value_length
,
1014 (time_t)0, (uint32_t)0);
1015 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1018 return TEST_SUCCESS
;
1021 static test_return_t
set_test3(memcached_st
*memc
)
1023 memcached_return_t rc
;
1025 size_t value_length
= 8191;
1028 value
= (char*)malloc(value_length
);
1031 for (x
= 0; x
< value_length
; x
++)
1032 value
[x
] = (char) (x
% 127);
1034 /* The dump test relies on there being at least 32 items in memcached */
1035 for (x
= 0; x
< 32; x
++)
1039 snprintf(key
, sizeof(key
), "foo%u", x
);
1041 rc
= memcached_set(memc
, key
, strlen(key
),
1042 value
, value_length
,
1043 (time_t)0, (uint32_t)0);
1044 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1049 return TEST_SUCCESS
;
1052 static test_return_t
get_test3(memcached_st
*memc
)
1054 memcached_return_t rc
;
1055 const char *key
= "foo";
1057 size_t value_length
= 8191;
1059 size_t string_length
;
1063 value
= (char*)malloc(value_length
);
1066 for (x
= 0; x
< value_length
; x
++)
1067 value
[x
] = (char) (x
% 127);
1069 rc
= memcached_set(memc
, key
, strlen(key
),
1070 value
, value_length
,
1071 (time_t)0, (uint32_t)0);
1072 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1074 string
= memcached_get(memc
, key
, strlen(key
),
1075 &string_length
, &flags
, &rc
);
1077 test_true(rc
== MEMCACHED_SUCCESS
);
1079 test_true(string_length
== value_length
);
1080 test_true(!memcmp(string
, value
, string_length
));
1085 return TEST_SUCCESS
;
1088 static test_return_t
get_test4(memcached_st
*memc
)
1090 memcached_return_t rc
;
1091 const char *key
= "foo";
1093 size_t value_length
= 8191;
1095 size_t string_length
;
1099 value
= (char*)malloc(value_length
);
1102 for (x
= 0; x
< value_length
; x
++)
1103 value
[x
] = (char) (x
% 127);
1105 rc
= memcached_set(memc
, key
, strlen(key
),
1106 value
, value_length
,
1107 (time_t)0, (uint32_t)0);
1108 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1110 for (x
= 0; x
< 10; x
++)
1112 string
= memcached_get(memc
, key
, strlen(key
),
1113 &string_length
, &flags
, &rc
);
1115 test_true(rc
== MEMCACHED_SUCCESS
);
1117 test_true(string_length
== value_length
);
1118 test_true(!memcmp(string
, value
, string_length
));
1124 return TEST_SUCCESS
;
1128 * This test verifies that memcached_read_one_response doesn't try to
1129 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1130 * responses before you execute a storage command.
1132 static test_return_t
get_test5(memcached_st
*memc
)
1135 ** Request the same key twice, to ensure that we hash to the same server
1136 ** (so that we have multiple response values queued up) ;-)
1138 const char *keys
[]= { "key", "key" };
1139 size_t lengths
[]= { 3, 3 };
1143 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1144 keys
[0], lengths
[0], 0, 0);
1145 test_true(rc
== MEMCACHED_SUCCESS
);
1146 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1148 memcached_result_st results_obj
;
1149 memcached_result_st
*results
;
1150 results
=memcached_result_create(memc
, &results_obj
);
1152 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1154 memcached_result_free(&results_obj
);
1156 /* Don't read out the second result, but issue a set instead.. */
1157 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1158 test_true(rc
== MEMCACHED_SUCCESS
);
1160 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1161 &rlen
, &flags
, &rc
);
1162 test_true(val
== NULL
);
1163 test_true(rc
== MEMCACHED_NOTFOUND
);
1164 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1165 test_true(val
!= NULL
);
1166 test_true(rc
== MEMCACHED_SUCCESS
);
1169 return TEST_SUCCESS
;
1172 static test_return_t
mget_end(memcached_st
*memc
)
1174 const char *keys
[]= { "foo", "foo2" };
1175 size_t lengths
[]= { 3, 4 };
1176 const char *values
[]= { "fjord", "41" };
1178 memcached_return_t rc
;
1181 for (int i
= 0; i
< 2; i
++)
1183 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1184 (time_t)0, (uint32_t)0);
1185 test_true(rc
== MEMCACHED_SUCCESS
);
1189 size_t string_length
;
1192 // retrieve both via mget
1193 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1194 test_true(rc
== MEMCACHED_SUCCESS
);
1196 char key
[MEMCACHED_MAX_KEY
];
1199 // this should get both
1200 for (int i
= 0; i
< 2; i
++)
1202 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1204 test_true(rc
== MEMCACHED_SUCCESS
);
1206 if (key_length
== 4)
1208 test_true(string_length
== strlen(values
[val
]));
1209 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1213 // this should indicate end
1214 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1215 test_true(rc
== MEMCACHED_END
);
1218 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1219 test_true(rc
== MEMCACHED_SUCCESS
);
1221 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1222 test_true(key_length
== lengths
[0]);
1223 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1224 test_true(string_length
== strlen(values
[0]));
1225 test_true(strncmp(values
[0], string
, string_length
) == 0);
1226 test_true(rc
== MEMCACHED_SUCCESS
);
1229 // this should indicate end
1230 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1231 test_true(rc
== MEMCACHED_END
);
1233 return TEST_SUCCESS
;
1236 /* Do not copy the style of this code, I just access hosts to testthis function */
1237 static test_return_t
stats_servername_test(memcached_st
*memc
)
1239 memcached_return_t rc
;
1240 memcached_stat_st memc_stat
;
1241 memcached_server_instance_st instance
=
1242 memcached_server_instance_by_position(memc
, 0);
1244 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1245 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1246 return TEST_SKIPPED
;
1248 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1249 memcached_server_name(instance
),
1250 memcached_server_port(instance
));
1252 return TEST_SUCCESS
;
1255 static test_return_t
increment_test(memcached_st
*memc
)
1257 uint64_t new_number
;
1258 memcached_return_t rc
;
1259 const char *key
= "number";
1260 const char *value
= "0";
1262 rc
= memcached_set(memc
, key
, strlen(key
),
1263 value
, strlen(value
),
1264 (time_t)0, (uint32_t)0);
1265 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1267 rc
= memcached_increment(memc
, key
, strlen(key
),
1269 test_true(rc
== MEMCACHED_SUCCESS
);
1270 test_true(new_number
== 1);
1272 rc
= memcached_increment(memc
, key
, strlen(key
),
1274 test_true(rc
== MEMCACHED_SUCCESS
);
1275 test_true(new_number
== 2);
1277 return TEST_SUCCESS
;
1280 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1282 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1284 uint64_t new_number
;
1285 memcached_return_t rc
;
1286 const char *key
= "number";
1287 uint64_t initial
= 0;
1289 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1290 1, initial
, 0, &new_number
);
1291 test_true(rc
== MEMCACHED_SUCCESS
);
1292 test_true(new_number
== initial
);
1294 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1295 1, initial
, 0, &new_number
);
1296 test_true(rc
== MEMCACHED_SUCCESS
);
1297 test_true(new_number
== (initial
+ 1));
1299 return TEST_SUCCESS
;
1302 static test_return_t
decrement_test(memcached_st
*memc
)
1304 uint64_t new_number
;
1305 memcached_return_t rc
;
1306 const char *key
= "number";
1307 const char *value
= "3";
1309 rc
= memcached_set(memc
, key
, strlen(key
),
1310 value
, strlen(value
),
1311 (time_t)0, (uint32_t)0);
1312 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1314 rc
= memcached_decrement(memc
, key
, strlen(key
),
1316 test_true(rc
== MEMCACHED_SUCCESS
);
1317 test_true(new_number
== 2);
1319 rc
= memcached_decrement(memc
, key
, strlen(key
),
1321 test_true(rc
== MEMCACHED_SUCCESS
);
1322 test_true(new_number
== 1);
1324 return TEST_SUCCESS
;
1327 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1329 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1331 uint64_t new_number
;
1332 memcached_return_t rc
;
1333 const char *key
= "number";
1334 uint64_t initial
= 3;
1336 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1337 1, initial
, 0, &new_number
);
1338 test_true(rc
== MEMCACHED_SUCCESS
);
1339 test_true(new_number
== initial
);
1341 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1342 1, initial
, 0, &new_number
);
1343 test_true(rc
== MEMCACHED_SUCCESS
);
1344 test_true(new_number
== (initial
- 1));
1346 return TEST_SUCCESS
;
1349 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1351 uint64_t new_number
;
1352 memcached_return_t rc
;
1353 const char *master_key
= "foo";
1354 const char *key
= "number";
1355 const char *value
= "0";
1357 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1359 value
, strlen(value
),
1360 (time_t)0, (uint32_t)0);
1361 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1363 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1365 test_true(rc
== MEMCACHED_SUCCESS
);
1366 test_true(new_number
== 1);
1368 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1370 test_true(rc
== MEMCACHED_SUCCESS
);
1371 test_true(new_number
== 2);
1373 return TEST_SUCCESS
;
1376 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1378 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1380 uint64_t new_number
;
1381 memcached_return_t rc
;
1382 const char *master_key
= "foo";
1383 const char *key
= "number";
1384 uint64_t initial
= 0;
1386 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1388 1, initial
, 0, &new_number
);
1389 test_true(rc
== MEMCACHED_SUCCESS
);
1390 test_true(new_number
== initial
);
1392 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1394 1, initial
, 0, &new_number
);
1395 test_true(rc
== MEMCACHED_SUCCESS
);
1396 test_true(new_number
== (initial
+ 1));
1398 return TEST_SUCCESS
;
1401 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1403 uint64_t new_number
;
1404 memcached_return_t rc
;
1405 const char *master_key
= "foo";
1406 const char *key
= "number";
1407 const char *value
= "3";
1409 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1411 value
, strlen(value
),
1412 (time_t)0, (uint32_t)0);
1413 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1415 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1418 test_true(rc
== MEMCACHED_SUCCESS
);
1419 test_true(new_number
== 2);
1421 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1424 test_true(rc
== MEMCACHED_SUCCESS
);
1425 test_true(new_number
== 1);
1427 return TEST_SUCCESS
;
1430 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1432 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1434 uint64_t new_number
;
1435 memcached_return_t rc
;
1436 const char *master_key
= "foo";
1437 const char *key
= "number";
1438 uint64_t initial
= 3;
1440 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1442 1, initial
, 0, &new_number
);
1443 test_true(rc
== MEMCACHED_SUCCESS
);
1444 test_true(new_number
== initial
);
1446 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1448 1, initial
, 0, &new_number
);
1449 test_true(rc
== MEMCACHED_SUCCESS
);
1450 test_true(new_number
== (initial
- 1));
1452 return TEST_SUCCESS
;
1455 static test_return_t
quit_test(memcached_st
*memc
)
1457 memcached_return_t rc
;
1458 const char *key
= "fudge";
1459 const char *value
= "sanford and sun";
1461 rc
= memcached_set(memc
, key
, strlen(key
),
1462 value
, strlen(value
),
1463 (time_t)10, (uint32_t)3);
1464 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1465 memcached_quit(memc
);
1467 rc
= memcached_set(memc
, key
, strlen(key
),
1468 value
, strlen(value
),
1469 (time_t)50, (uint32_t)9);
1470 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1472 return TEST_SUCCESS
;
1475 static test_return_t
mget_result_test(memcached_st
*memc
)
1477 memcached_return_t rc
;
1478 const char *keys
[]= {"fudge", "son", "food"};
1479 size_t key_length
[]= {5, 3, 4};
1482 memcached_result_st results_obj
;
1483 memcached_result_st
*results
;
1485 results
= memcached_result_create(memc
, &results_obj
);
1487 test_true(&results_obj
== results
);
1489 /* We need to empty the server before continueing test */
1490 rc
= memcached_flush(memc
, 0);
1491 test_true(rc
== MEMCACHED_SUCCESS
);
1493 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1494 test_true(rc
== MEMCACHED_SUCCESS
);
1496 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1501 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1502 test_true(!results
);
1503 test_true(rc
== MEMCACHED_END
);
1505 for (x
= 0; x
< 3; x
++)
1507 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1508 keys
[x
], key_length
[x
],
1509 (time_t)50, (uint32_t)9);
1510 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1513 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1514 test_true(rc
== MEMCACHED_SUCCESS
);
1516 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1519 test_true(&results_obj
== results
);
1520 test_true(rc
== MEMCACHED_SUCCESS
);
1521 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1522 test_true(!memcmp(memcached_result_key_value(results
),
1523 memcached_result_value(results
),
1524 memcached_result_length(results
)));
1527 memcached_result_free(&results_obj
);
1529 return TEST_SUCCESS
;
1532 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1534 memcached_return_t rc
;
1535 const char *keys
[]= {"fudge", "son", "food"};
1536 size_t key_length
[]= {5, 3, 4};
1539 memcached_result_st
*results
;
1541 /* We need to empty the server before continueing test */
1542 rc
= memcached_flush(memc
, 0);
1543 test_true(rc
== MEMCACHED_SUCCESS
);
1545 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1546 test_true(rc
== MEMCACHED_SUCCESS
);
1548 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1552 test_true(!results
);
1553 test_true(rc
== MEMCACHED_END
);
1555 for (x
= 0; x
< 3; x
++)
1557 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1558 keys
[x
], key_length
[x
],
1559 (time_t)50, (uint32_t)9);
1560 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1563 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1564 test_true(rc
== MEMCACHED_SUCCESS
);
1567 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1570 test_true(rc
== MEMCACHED_SUCCESS
);
1571 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1572 test_true(!memcmp(memcached_result_key_value(results
),
1573 memcached_result_value(results
),
1574 memcached_result_length(results
)));
1575 memcached_result_free(results
);
1579 return TEST_SUCCESS
;
1582 /* Count the results */
1583 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1584 memcached_result_st
*result
,
1587 (void)ptr
; (void)result
;
1588 size_t *counter
= (size_t *)context
;
1590 *counter
= *counter
+ 1;
1592 return MEMCACHED_SUCCESS
;
1595 static test_return_t
mget_result_function(memcached_st
*memc
)
1597 memcached_return_t rc
;
1598 const char *keys
[]= {"fudge", "son", "food"};
1599 size_t key_length
[]= {5, 3, 4};
1602 memcached_execute_fn callbacks
[1];
1604 /* We need to empty the server before continueing test */
1605 rc
= memcached_flush(memc
, 0);
1606 for (x
= 0; x
< 3; x
++)
1608 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1609 keys
[x
], key_length
[x
],
1610 (time_t)50, (uint32_t)9);
1611 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1614 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1615 test_true(rc
== MEMCACHED_SUCCESS
);
1617 callbacks
[0]= &callback_counter
;
1619 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1621 test_true(counter
== 3);
1623 return TEST_SUCCESS
;
1626 static test_return_t
mget_test(memcached_st
*memc
)
1628 memcached_return_t rc
;
1629 const char *keys
[]= {"fudge", "son", "food"};
1630 size_t key_length
[]= {5, 3, 4};
1634 char return_key
[MEMCACHED_MAX_KEY
];
1635 size_t return_key_length
;
1637 size_t return_value_length
;
1639 /* We need to empty the server before continueing test */
1640 rc
= memcached_flush(memc
, 0);
1641 test_true(rc
== MEMCACHED_SUCCESS
);
1643 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1644 test_true(rc
== MEMCACHED_SUCCESS
);
1646 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1647 &return_value_length
, &flags
, &rc
)) != NULL
)
1649 test_true(return_value
);
1651 test_true(!return_value
);
1652 test_true(return_value_length
== 0);
1653 test_true(rc
== MEMCACHED_END
);
1655 for (x
= 0; x
< 3; x
++)
1657 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1658 keys
[x
], key_length
[x
],
1659 (time_t)50, (uint32_t)9);
1660 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1663 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1664 test_true(rc
== MEMCACHED_SUCCESS
);
1667 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1668 &return_value_length
, &flags
, &rc
)))
1670 test_true(return_value
);
1671 test_true(rc
== MEMCACHED_SUCCESS
);
1672 test_true(return_key_length
== return_value_length
);
1673 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1678 return TEST_SUCCESS
;
1681 static test_return_t
mget_execute(memcached_st
*memc
)
1685 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1689 * I only want to hit _one_ server so I know the number of requests I'm
1690 * sending in the pipeline.
1692 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1693 memc
->number_of_hosts
= 1;
1695 size_t max_keys
= 20480;
1698 char **keys
= calloc(max_keys
, sizeof(char*));
1699 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1701 /* First add all of the items.. */
1702 char blob
[1024] = {0};
1703 memcached_return_t rc
;
1705 for (size_t x
= 0; x
< max_keys
; ++x
)
1709 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1711 test_true(keys
[x
] != NULL
);
1712 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1713 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1716 /* Try to get all of them with a large multiget */
1718 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1719 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1720 max_keys
, callbacks
, &counter
, 1);
1722 if (rc
== MEMCACHED_SUCCESS
)
1725 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1726 test_true(rc
== MEMCACHED_END
);
1728 /* Verify that we got all of the items */
1729 test_true(counter
== max_keys
);
1731 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1733 test_true(counter
== 0);
1737 test_fail("note: this test functions differently when in binary mode");
1740 /* Release all allocated resources */
1741 for (size_t x
= 0; x
< max_keys
; ++x
)
1748 memc
->number_of_hosts
= number_of_hosts
;
1749 return TEST_SUCCESS
;
1752 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1754 static test_return_t
key_setup(memcached_st
*memc
)
1758 if (pre_binary(memc
) != TEST_SUCCESS
)
1759 return TEST_SKIPPED
;
1761 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1763 return TEST_SUCCESS
;
1766 static test_return_t
key_teardown(memcached_st
*memc
)
1769 pairs_free(global_pairs
);
1771 return TEST_SUCCESS
;
1774 static test_return_t
block_add_regression(memcached_st
*memc
)
1776 /* First add all of the items.. */
1777 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1779 memcached_return_t rc
;
1780 char blob
[1024] = {0};
1782 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1783 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1786 return TEST_SUCCESS
;
1789 static test_return_t
binary_add_regression(memcached_st
*memc
)
1791 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1792 test_return_t rc
= block_add_regression(memc
);
1793 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1797 static test_return_t
get_stats_keys(memcached_st
*memc
)
1801 memcached_stat_st memc_stat
;
1802 memcached_return_t rc
;
1804 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1805 test_true(rc
== MEMCACHED_SUCCESS
);
1806 for (ptr
= stat_list
; *ptr
; ptr
++)
1811 return TEST_SUCCESS
;
1814 static test_return_t
version_string_test(memcached_st
*memc
)
1816 const char *version_string
;
1819 version_string
= memcached_lib_version();
1821 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1823 return TEST_SUCCESS
;
1826 static test_return_t
get_stats(memcached_st
*memc
)
1830 memcached_return_t rc
;
1831 memcached_stat_st
*memc_stat
;
1833 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1834 test_true(rc
== MEMCACHED_SUCCESS
);
1836 test_true(rc
== MEMCACHED_SUCCESS
);
1837 test_true(memc_stat
);
1839 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1841 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1842 test_true(rc
== MEMCACHED_SUCCESS
);
1843 for (ptr
= stat_list
; *ptr
; ptr
++);
1848 memcached_stat_free(NULL
, memc_stat
);
1850 return TEST_SUCCESS
;
1853 static test_return_t
add_host_test(memcached_st
*memc
)
1856 memcached_server_st
*servers
;
1857 memcached_return_t rc
;
1858 char servername
[]= "0.example.com";
1860 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1862 test_true(1 == memcached_server_list_count(servers
));
1864 for (x
= 2; x
< 20; x
++)
1866 char buffer
[SMALL_STRING_LEN
];
1868 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1869 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1871 test_true(rc
== MEMCACHED_SUCCESS
);
1872 test_true(x
== memcached_server_list_count(servers
));
1875 rc
= memcached_server_push(memc
, servers
);
1876 test_true(rc
== MEMCACHED_SUCCESS
);
1877 rc
= memcached_server_push(memc
, servers
);
1878 test_true(rc
== MEMCACHED_SUCCESS
);
1880 memcached_server_list_free(servers
);
1882 return TEST_SUCCESS
;
1885 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1887 (void)parent
;(void)memc_clone
;
1888 return MEMCACHED_SUCCESS
;
1891 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1894 return MEMCACHED_SUCCESS
;
1897 static test_return_t
callback_test(memcached_st
*memc
)
1899 /* Test User Data */
1903 memcached_return_t rc
;
1905 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1906 test_true(rc
== MEMCACHED_SUCCESS
);
1907 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1908 test_true(*test_ptr
== x
);
1911 /* Test Clone Callback */
1913 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1914 void *clone_cb_ptr
= *(void **)&clone_cb
;
1915 void *temp_function
= NULL
;
1916 memcached_return_t rc
;
1918 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1920 test_true(rc
== MEMCACHED_SUCCESS
);
1921 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1922 test_true(temp_function
== clone_cb_ptr
);
1925 /* Test Cleanup Callback */
1927 memcached_cleanup_fn cleanup_cb
=
1928 (memcached_cleanup_fn
)cleanup_test_callback
;
1929 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1930 void *temp_function
= NULL
;
1931 memcached_return_t rc
;
1933 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1935 test_true(rc
== MEMCACHED_SUCCESS
);
1936 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1937 test_true(temp_function
== cleanup_cb_ptr
);
1940 return TEST_SUCCESS
;
1943 /* We don't test the behavior itself, we test the switches */
1944 static test_return_t
behavior_test(memcached_st
*memc
)
1949 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1950 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1951 test_true(value
== 1);
1953 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1954 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1955 test_true(value
== 1);
1957 set
= MEMCACHED_HASH_MD5
;
1958 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1959 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1960 test_true(value
== MEMCACHED_HASH_MD5
);
1964 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1965 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1966 test_true(value
== 0);
1968 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1969 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1970 test_true(value
== 0);
1972 set
= MEMCACHED_HASH_DEFAULT
;
1973 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1974 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1975 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1977 set
= MEMCACHED_HASH_CRC
;
1978 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1979 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1980 test_true(value
== MEMCACHED_HASH_CRC
);
1982 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1983 test_true(value
> 0);
1985 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1986 test_true(value
> 0);
1988 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1989 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1990 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1992 return TEST_SUCCESS
;
1995 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1997 memcached_return_t rc
;
2001 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2002 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2004 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2006 if (rc
== MEMCACHED_SUCCESS
)
2008 test_true((bool)value
== set
);
2012 test_false((bool)value
== set
);
2015 return TEST_SUCCESS
;
2019 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2021 memcached_return_t rc
;
2025 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2026 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2028 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2030 if (rc
== MEMCACHED_SUCCESS
)
2032 test_true((bool)value
== set
);
2036 test_false((bool)value
== set
);
2039 return TEST_SUCCESS
;
2043 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2045 memcached_return_t rc
;
2049 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2050 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2052 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2054 if (rc
== MEMCACHED_SUCCESS
)
2056 test_true((bool)value
== set
);
2060 test_false((bool)value
== set
);
2063 return TEST_SUCCESS
;
2066 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2068 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2069 char return_key
[MEMCACHED_MAX_KEY
];
2070 size_t return_key_length
;
2072 size_t return_value_length
;
2077 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2078 &return_value_length
, &flags
, &rc
)))
2080 test_true(return_value
);
2081 test_true(rc
== MEMCACHED_SUCCESS
);
2083 *keys_returned
= *keys_returned
+1;
2086 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2088 return TEST_SUCCESS
;
2091 /* Test case provided by Cal Haldenbrand */
2092 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2094 unsigned int setter
= 1;
2096 unsigned long long total
= 0;
2099 char randomstuff
[6 * 1024];
2100 memcached_return_t rc
;
2102 memset(randomstuff
, 0, 6 * 1024);
2104 /* We just keep looking at the same values over and over */
2107 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2108 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2112 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2116 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2117 memset(randomstuff
, 0, 6 * 1024);
2118 test_true(size
< 6 * 1024); /* Being safe here */
2120 for (j
= 0 ; j
< size
;j
++)
2121 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2124 snprintf(key
, sizeof(key
), "%u", x
);
2125 rc
= memcached_set(memc
, key
, strlen(key
),
2126 randomstuff
, strlen(randomstuff
), 10, 0);
2127 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2128 /* If we fail, lets try again */
2129 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2130 rc
= memcached_set(memc
, key
, strlen(key
),
2131 randomstuff
, strlen(randomstuff
), 10, 0);
2132 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2135 return TEST_SUCCESS
;
2138 /* Test case provided by Cal Haldenbrand */
2139 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2141 unsigned int setter
;
2145 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2146 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2148 setter
= 20 * 1024576;
2149 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2150 setter
= 20 * 1024576;
2151 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2152 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2153 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2155 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2158 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2160 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2161 char buffer
[SMALL_STRING_LEN
];
2166 memset(buffer
, 0, SMALL_STRING_LEN
);
2168 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2169 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2170 &val_len
, &flags
, &rc
);
2171 if (rc
!= MEMCACHED_SUCCESS
)
2173 if (rc
== MEMCACHED_NOTFOUND
)
2187 return TEST_SUCCESS
;
2190 /* Do a large mget() over all the keys we think exist */
2191 #define KEY_COUNT 3000 // * 1024576
2192 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2194 memcached_return_t rc
;
2195 unsigned int setter
;
2198 size_t key_lengths
[KEY_COUNT
];
2201 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2202 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2204 setter
= 20 * 1024576;
2205 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2206 setter
= 20 * 1024576;
2207 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2208 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2209 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2212 keys
= calloc(KEY_COUNT
, sizeof(char *));
2214 for (x
= 0; x
< KEY_COUNT
; x
++)
2218 snprintf(buffer
, 30, "%u", x
);
2219 keys
[x
]= strdup(buffer
);
2220 key_lengths
[x
]= strlen(keys
[x
]);
2223 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2224 test_true(rc
== MEMCACHED_SUCCESS
);
2226 size_t keys_returned
;
2227 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2229 for (x
= 0; x
< KEY_COUNT
; x
++)
2233 return TEST_SUCCESS
;
2236 /* Make sure we behave properly if server list has no values */
2237 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2239 memcached_return_t rc
;
2240 const char *keys
[]= {"fudge", "son", "food"};
2241 size_t key_length
[]= {5, 3, 4};
2244 char return_key
[MEMCACHED_MAX_KEY
];
2245 size_t return_key_length
;
2247 size_t return_value_length
;
2249 /* Here we free everything before running a bunch of mget tests */
2250 memcached_servers_reset(memc
);
2253 /* We need to empty the server before continueing test */
2254 rc
= memcached_flush(memc
, 0);
2255 test_true(rc
== MEMCACHED_NO_SERVERS
);
2257 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2258 test_true(rc
== MEMCACHED_NO_SERVERS
);
2260 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2261 &return_value_length
, &flags
, &rc
)) != NULL
)
2263 test_true(return_value
);
2265 test_true(!return_value
);
2266 test_true(return_value_length
== 0);
2267 test_true(rc
== MEMCACHED_NO_SERVERS
);
2269 for (x
= 0; x
< 3; x
++)
2271 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2272 keys
[x
], key_length
[x
],
2273 (time_t)50, (uint32_t)9);
2274 test_true(rc
== MEMCACHED_NO_SERVERS
);
2277 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2278 test_true(rc
== MEMCACHED_NO_SERVERS
);
2281 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2282 &return_value_length
, &flags
, &rc
)))
2284 test_true(return_value
);
2285 test_true(rc
== MEMCACHED_SUCCESS
);
2286 test_true(return_key_length
== return_value_length
);
2287 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2292 return TEST_SUCCESS
;
2295 #define VALUE_SIZE_BUG5 1048064
2296 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2298 memcached_return_t rc
;
2299 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2300 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2301 char return_key
[MEMCACHED_MAX_KEY
];
2302 size_t return_key_length
;
2304 size_t value_length
;
2308 char insert_data
[VALUE_SIZE_BUG5
];
2310 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2311 insert_data
[x
]= (signed char)rand();
2313 memcached_flush(memc
, 0);
2314 value
= memcached_get(memc
, keys
[0], key_length
[0],
2315 &value_length
, &flags
, &rc
);
2316 test_true(value
== NULL
);
2317 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2320 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2321 &value_length
, &flags
, &rc
)))
2323 test_true(count
== 0);
2325 for (x
= 0; x
< 4; x
++)
2327 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2328 insert_data
, VALUE_SIZE_BUG5
,
2329 (time_t)0, (uint32_t)0);
2330 test_true(rc
== MEMCACHED_SUCCESS
);
2333 for (x
= 0; x
< 10; x
++)
2335 value
= memcached_get(memc
, keys
[0], key_length
[0],
2336 &value_length
, &flags
, &rc
);
2340 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2342 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2343 &value_length
, &flags
, &rc
)))
2348 test_true(count
== 4);
2351 return TEST_SUCCESS
;
2354 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2356 memcached_return_t rc
;
2357 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2358 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2359 char return_key
[MEMCACHED_MAX_KEY
];
2360 size_t return_key_length
;
2362 size_t value_length
;
2366 char insert_data
[VALUE_SIZE_BUG5
];
2368 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2369 insert_data
[x
]= (signed char)rand();
2371 memcached_flush(memc
, 0);
2372 value
= memcached_get(memc
, keys
[0], key_length
[0],
2373 &value_length
, &flags
, &rc
);
2374 test_true(value
== NULL
);
2375 test_true(rc
== MEMCACHED_NOTFOUND
);
2376 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2377 test_true(rc
== MEMCACHED_SUCCESS
);
2380 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2381 &value_length
, &flags
, &rc
)))
2383 test_true(count
== 0);
2384 test_true(rc
== MEMCACHED_END
);
2386 for (x
= 0; x
< 4; x
++)
2388 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2389 insert_data
, VALUE_SIZE_BUG5
,
2390 (time_t)0, (uint32_t)0);
2391 test_true(rc
== MEMCACHED_SUCCESS
);
2394 for (x
= 0; x
< 2; x
++)
2396 value
= memcached_get(memc
, keys
[0], key_length
[0],
2397 &value_length
, &flags
, &rc
);
2401 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2402 test_true(rc
== MEMCACHED_SUCCESS
);
2404 /* We test for purge of partial complete fetches */
2405 for (count
= 3; count
; count
--)
2407 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2408 &value_length
, &flags
, &rc
);
2409 test_true(rc
== MEMCACHED_SUCCESS
);
2410 test_true(!(memcmp(value
, insert_data
, value_length
)));
2411 test_true(value_length
);
2416 return TEST_SUCCESS
;
2419 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2421 memcached_return_t rc
;
2423 memcached_st
*memc_clone
;
2425 memcached_server_st
*servers
;
2426 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";
2429 servers
= memcached_servers_parse(server_list
);
2432 mine
= memcached_create(NULL
);
2433 rc
= memcached_server_push(mine
, servers
);
2434 test_true(rc
== MEMCACHED_SUCCESS
);
2435 memcached_server_list_free(servers
);
2438 memc_clone
= memcached_clone(NULL
, mine
);
2440 memcached_quit(mine
);
2441 memcached_quit(memc_clone
);
2444 memcached_free(mine
);
2445 memcached_free(memc_clone
);
2447 return TEST_SUCCESS
;
2450 /* Test flag store/retrieve */
2451 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2453 memcached_return_t rc
;
2454 const char *keys
= "036790384900";
2455 size_t key_length
= strlen(keys
);
2456 char return_key
[MEMCACHED_MAX_KEY
];
2457 size_t return_key_length
;
2459 size_t value_length
;
2462 char insert_data
[VALUE_SIZE_BUG5
];
2464 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2465 insert_data
[x
]= (signed char)rand();
2467 memcached_flush(memc
, 0);
2470 rc
= memcached_set(memc
, keys
, key_length
,
2471 insert_data
, VALUE_SIZE_BUG5
,
2473 test_true(rc
== MEMCACHED_SUCCESS
);
2476 value
= memcached_get(memc
, keys
, key_length
,
2477 &value_length
, &flags
, &rc
);
2478 test_true(flags
== 245);
2482 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2485 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2486 &value_length
, &flags
, &rc
);
2487 test_true(flags
== 245);
2492 return TEST_SUCCESS
;
2495 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2497 memcached_return_t rc
;
2498 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2499 size_t key_length
[3];
2504 char return_key
[MEMCACHED_MAX_KEY
];
2505 size_t return_key_length
;
2507 size_t return_value_length
;
2510 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2511 key_length
[1]= strlen("fudge&*@#");
2512 key_length
[2]= strlen("for^#@&$not");
2515 for (x
= 0; x
< 3; x
++)
2517 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2518 keys
[x
], key_length
[x
],
2519 (time_t)50, (uint32_t)9);
2520 test_true(rc
== MEMCACHED_SUCCESS
);
2523 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2524 test_true(rc
== MEMCACHED_SUCCESS
);
2526 /* We need to empty the server before continueing test */
2527 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2528 &return_value_length
, &flags
, &rc
)) != NULL
)
2530 test_true(return_value
);
2534 test_true(count
== 3);
2536 return TEST_SUCCESS
;
2539 /* We are testing with aggressive timeout to get failures */
2540 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2542 const char *key
= "foo";
2544 size_t value_length
= 512;
2547 memcached_return_t rc
;
2548 unsigned int set
= 1;
2549 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2552 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2553 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2555 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2558 value
= (char*)malloc(value_length
* sizeof(char));
2560 for (x
= 0; x
< value_length
; x
++)
2561 value
[x
]= (char) (x
% 127);
2563 for (x
= 1; x
<= 100000; ++x
)
2565 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2567 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2568 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2570 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2575 memcached_free(mclone
);
2577 return TEST_SUCCESS
;
2581 We are looking failures in the async protocol
2583 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2585 const char *key
= "foo";
2587 size_t value_length
= 512;
2590 memcached_return_t rc
;
2591 unsigned int set
= 1;
2593 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2595 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2596 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2598 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2601 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2603 test_true(timeout
== -1);
2605 value
= (char*)malloc(value_length
* sizeof(char));
2607 for (x
= 0; x
< value_length
; x
++)
2608 value
[x
]= (char) (x
% 127);
2610 for (x
= 1; x
<= 100000; ++x
)
2612 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2616 memcached_free(mclone
);
2618 return TEST_SUCCESS
;
2622 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2624 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2626 memcached_return_t rc
;
2628 size_t value_length
;
2630 uint64_t number_value
;
2632 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2633 &value_length
, &flags
, &rc
);
2634 test_true(value
== NULL
);
2635 test_true(rc
== MEMCACHED_NOTFOUND
);
2637 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2640 test_true(value
== NULL
);
2641 /* The binary protocol will set the key if it doesn't exist */
2642 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2644 test_true(rc
== MEMCACHED_SUCCESS
);
2648 test_true(rc
== MEMCACHED_NOTFOUND
);
2651 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2653 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2654 &value_length
, &flags
, &rc
);
2656 test_true(rc
== MEMCACHED_SUCCESS
);
2659 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2661 test_true(number_value
== 2);
2662 test_true(rc
== MEMCACHED_SUCCESS
);
2664 return TEST_SUCCESS
;
2668 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2669 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2671 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2673 char key
[] = "key34567890";
2675 memcached_return_t rc
;
2676 size_t overflowSize
;
2678 char commandFirst
[]= "set key34567890 0 0 ";
2679 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2680 size_t commandLength
;
2683 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2685 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2687 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2689 overflow
= malloc(testSize
);
2690 test_true(overflow
!= NULL
);
2692 memset(overflow
, 'x', testSize
);
2693 rc
= memcached_set(memc
, key
, strlen(key
),
2694 overflow
, testSize
, 0, 0);
2695 test_true(rc
== MEMCACHED_SUCCESS
);
2699 return TEST_SUCCESS
;
2704 Test values of many different sizes
2705 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2706 set key34567890 0 0 8169 \r\n
2707 is sent followed by buffer of size 8169, followed by 8169
2709 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2712 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2713 memcached_return_t rc
;
2714 const char *key
= "foo";
2716 size_t value_length
= 18000;
2718 size_t string_length
;
2721 size_t current_length
;
2723 value
= (char*)malloc(value_length
);
2726 for (x
= 0; x
< value_length
; x
++)
2727 value
[x
] = (char) (x
% 127);
2729 for (current_length
= 0; current_length
< value_length
; current_length
++)
2731 rc
= memcached_set(memc
, key
, strlen(key
),
2732 value
, current_length
,
2733 (time_t)0, (uint32_t)0);
2734 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2736 string
= memcached_get(memc
, key
, strlen(key
),
2737 &string_length
, &flags
, &rc
);
2739 test_true(rc
== MEMCACHED_SUCCESS
);
2740 test_true(string_length
== current_length
);
2741 test_true(!memcmp(string
, value
, string_length
));
2748 return TEST_SUCCESS
;
2752 Look for zero length value problems
2754 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2757 memcached_return_t rc
;
2758 const char *key
= "mykey";
2763 for (x
= 0; x
< 2; x
++)
2765 rc
= memcached_set(memc
, key
, strlen(key
),
2767 (time_t)0, (uint32_t)0);
2769 test_true(rc
== MEMCACHED_SUCCESS
);
2771 value
= memcached_get(memc
, key
, strlen(key
),
2772 &length
, &flags
, &rc
);
2774 test_true(rc
== MEMCACHED_SUCCESS
);
2775 test_true(value
== NULL
);
2776 test_true(length
== 0);
2777 test_true(flags
== 0);
2779 value
= memcached_get(memc
, key
, strlen(key
),
2780 &length
, &flags
, &rc
);
2782 test_true(rc
== MEMCACHED_SUCCESS
);
2783 test_true(value
== NULL
);
2784 test_true(length
== 0);
2785 test_true(flags
== 0);
2788 return TEST_SUCCESS
;
2791 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2792 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2794 memcached_return_t rc
;
2795 const char *key
= "mykey";
2800 rc
= memcached_set(memc
, key
, strlen(key
),
2802 (time_t)0, UINT32_MAX
);
2804 test_true(rc
== MEMCACHED_SUCCESS
);
2806 value
= memcached_get(memc
, key
, strlen(key
),
2807 &length
, &flags
, &rc
);
2809 test_true(rc
== MEMCACHED_SUCCESS
);
2810 test_true(value
== NULL
);
2811 test_true(length
== 0);
2812 test_true(flags
== UINT32_MAX
);
2814 return TEST_SUCCESS
;
2817 #if !defined(__sun) && !defined(__OpenBSD__)
2818 /* Check the validity of chinese key*/
2819 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2821 memcached_return_t rc
;
2822 const char *key
= "豆瓣";
2823 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2828 rc
= memcached_set(memc
, key
, strlen(key
),
2829 value
, strlen(value
),
2832 test_true(rc
== MEMCACHED_SUCCESS
);
2834 value2
= memcached_get(memc
, key
, strlen(key
),
2835 &length
, &flags
, &rc
);
2837 test_true(length
==strlen(value
));
2838 test_true(rc
== MEMCACHED_SUCCESS
);
2839 test_true(memcmp(value
, value2
, length
)==0);
2842 return TEST_SUCCESS
;
2850 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2853 const memcached_server_st
*server
;
2854 memcached_return_t res
;
2858 memc
= memcached_create(NULL
);
2859 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2860 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2862 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2864 memcached_free(memc
);
2866 return TEST_SUCCESS
;
2869 /* CAS test from Andei */
2870 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2872 memcached_return_t status
;
2873 memcached_result_st
*result
, result_obj
;
2874 const char *key
= "abc";
2875 size_t key_len
= strlen("abc");
2876 const char *value
= "foobar";
2877 size_t value_len
= strlen(value
);
2879 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2881 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2882 test_true(status
== MEMCACHED_SUCCESS
);
2884 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2885 test_true(status
== MEMCACHED_SUCCESS
);
2887 result
= memcached_result_create(memc
, &result_obj
);
2890 memcached_result_create(memc
, &result_obj
);
2891 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2894 test_true(status
== MEMCACHED_SUCCESS
);
2896 memcached_result_free(result
);
2898 return TEST_SUCCESS
;
2901 #include "ketama_test_cases.h"
2902 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2904 memcached_return_t rc
;
2907 memcached_server_st
*server_pool
;
2912 memc
= memcached_create(NULL
);
2915 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2916 test_true(rc
== MEMCACHED_SUCCESS
);
2918 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2919 test_true(value
== 1);
2921 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2922 test_true(rc
== MEMCACHED_SUCCESS
);
2924 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2925 test_true(value
== MEMCACHED_HASH_MD5
);
2927 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");
2928 memcached_server_push(memc
, server_pool
);
2930 /* verify that the server list was parsed okay. */
2931 test_true(memcached_server_count(memc
) == 8);
2932 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2933 test_true(server_pool
[0].port
== 11211);
2934 test_true(server_pool
[0].weight
== 600);
2935 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2936 test_true(server_pool
[2].port
== 11211);
2937 test_true(server_pool
[2].weight
== 200);
2938 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2939 test_true(server_pool
[7].port
== 11211);
2940 test_true(server_pool
[7].weight
== 100);
2942 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2943 * us test the boundary wraparound.
2945 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2947 /* verify the standard ketama set. */
2948 for (x
= 0; x
< 99; x
++)
2950 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2952 memcached_server_instance_st instance
=
2953 memcached_server_instance_by_position(memc
, server_idx
);
2955 const char *hostname
= memcached_server_name(instance
);
2956 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2959 memcached_server_list_free(server_pool
);
2960 memcached_free(memc
);
2962 return TEST_SUCCESS
;
2965 /* Large mget() of missing keys with binary proto
2967 * If many binary quiet commands (such as getq's in an mget) fill the output
2968 * buffer and the server chooses not to respond, memcached_flush hangs. See
2969 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2972 /* sighandler_t function that always asserts false */
2973 static void fail(int unused
)
2980 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2985 return TEST_SKIPPED
;
2987 memcached_return_t rc
;
2990 size_t* key_lengths
;
2991 void (*oldalarm
)(int);
2992 memcached_st
*memc_clone
;
2994 memc_clone
= memcached_clone(NULL
, memc
);
2995 test_true(memc_clone
);
2997 /* only binproto uses getq for mget */
2998 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3000 /* empty the cache to ensure misses (hence non-responses) */
3001 rc
= memcached_flush(memc_clone
, 0);
3002 test_true(rc
== MEMCACHED_SUCCESS
);
3004 key_lengths
= calloc(key_count
, sizeof(size_t));
3005 keys
= calloc(key_count
, sizeof(char *));
3007 for (x
= 0; x
< key_count
; x
++)
3011 snprintf(buffer
, 30, "%u", x
);
3012 keys
[x
]= strdup(buffer
);
3013 key_lengths
[x
]= strlen(keys
[x
]);
3016 oldalarm
= signal(SIGALRM
, fail
);
3019 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3020 test_true(rc
== MEMCACHED_SUCCESS
);
3023 signal(SIGALRM
, oldalarm
);
3025 size_t keys_returned
;
3026 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3028 for (x
= 0; x
< key_count
; x
++)
3033 memcached_free(memc_clone
);
3035 return TEST_SUCCESS
;
3039 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3041 test_return_t test_rc
;
3042 test_rc
= pre_binary(memc
);
3044 if (test_rc
!= TEST_SUCCESS
)
3049 /* should work as of r580 */
3050 rc
= _user_supplied_bug21(memc
, 10);
3051 test_true(rc
== TEST_SUCCESS
);
3053 /* should fail as of r580 */
3054 rc
= _user_supplied_bug21(memc
, 1000);
3055 test_true(rc
== TEST_SUCCESS
);
3057 return TEST_SUCCESS
;
3060 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3063 memcached_server_instance_st instance
;
3065 memcached_return_t rc
;
3066 memcached_st
*memc
= memcached_create(NULL
);
3069 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3070 test_true(rc
== MEMCACHED_SUCCESS
);
3072 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3073 test_true(value
== 1);
3075 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3076 test_true(rc
== MEMCACHED_SUCCESS
);
3078 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3079 test_true(value
== MEMCACHED_HASH_MD5
);
3081 /* server should be removed when in delay */
3082 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3083 test_true(rc
== MEMCACHED_SUCCESS
);
3085 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3086 test_true(value
== 1);
3088 memcached_server_st
*server_pool
;
3089 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");
3090 memcached_server_push(memc
, server_pool
);
3092 /* verify that the server list was parsed okay. */
3093 test_true(memcached_server_count(memc
) == 8);
3094 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3095 test_true(server_pool
[0].port
== 11211);
3096 test_true(server_pool
[0].weight
== 600);
3097 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3098 test_true(server_pool
[2].port
== 11211);
3099 test_true(server_pool
[2].weight
== 200);
3100 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3101 test_true(server_pool
[7].port
== 11211);
3102 test_true(server_pool
[7].weight
== 100);
3104 instance
= memcached_server_instance_by_position(memc
, 2);
3105 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3106 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3109 This would not work if there were only two hosts.
3111 for (size_t x
= 0; x
< 99; x
++)
3113 memcached_autoeject(memc
);
3114 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3115 test_true(server_idx
!= 2);
3118 /* and re-added when it's back. */
3119 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3120 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3121 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3122 memc
->distribution
);
3123 for (size_t x
= 0; x
< 99; x
++)
3125 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3126 // We re-use instance from above.
3128 memcached_server_instance_by_position(memc
, server_idx
);
3129 const char *hostname
= memcached_server_name(instance
);
3130 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3133 memcached_server_list_free(server_pool
);
3134 memcached_free(memc
);
3136 return TEST_SUCCESS
;
3139 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3143 memcached_return_t rc
;
3144 memcached_st
*memc
= memcached_create(NULL
);
3148 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3149 test_true(rc
== MEMCACHED_SUCCESS
);
3151 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3152 test_true(value
== 1);
3154 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3155 test_true(rc
== MEMCACHED_SUCCESS
);
3157 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3158 test_true(value
== MEMCACHED_HASH_MD5
);
3161 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3163 memcached_server_st
*server_pool
;
3164 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");
3165 memcached_server_push(memc
, server_pool
);
3167 // @todo this needs to be refactored to actually test something.
3170 if ((fp
= fopen("ketama_keys.txt", "w")))
3174 printf("cannot write to file ketama_keys.txt");
3175 return TEST_FAILURE
;
3178 for (int x
= 0; x
< 10000; x
++)
3181 snprintf(key
, sizeof(key
), "%d", x
);
3183 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3184 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3185 in_port_t port
= memc
->hosts
[server_idx
].port
;
3186 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3187 memcached_server_instance_st instance
=
3188 memcached_server_instance_by_position(memc
, host_index
);
3192 memcached_server_list_free(server_pool
);
3193 memcached_free(memc
);
3195 return TEST_SUCCESS
;
3199 static test_return_t
result_static(memcached_st
*memc
)
3201 memcached_result_st result
;
3202 memcached_result_st
*result_ptr
;
3204 result_ptr
= memcached_result_create(memc
, &result
);
3205 test_true(result
.options
.is_allocated
== false);
3206 test_true(memcached_is_initialized(&result
) == true);
3207 test_true(result_ptr
);
3208 test_true(result_ptr
== &result
);
3210 memcached_result_free(&result
);
3212 test_true(result
.options
.is_allocated
== false);
3213 test_true(memcached_is_initialized(&result
) == false);
3215 return TEST_SUCCESS
;
3218 static test_return_t
result_alloc(memcached_st
*memc
)
3220 memcached_result_st
*result_ptr
;
3222 result_ptr
= memcached_result_create(memc
, NULL
);
3223 test_true(result_ptr
);
3224 test_true(result_ptr
->options
.is_allocated
== true);
3225 test_true(memcached_is_initialized(result_ptr
) == true);
3226 memcached_result_free(result_ptr
);
3228 return TEST_SUCCESS
;
3231 static test_return_t
string_static_null(memcached_st
*memc
)
3233 memcached_string_st string
;
3234 memcached_string_st
*string_ptr
;
3236 string_ptr
= memcached_string_create(memc
, &string
, 0);
3237 test_true(string
.options
.is_initialized
== true);
3238 test_true(string_ptr
);
3240 /* The following two better be the same! */
3241 test_true(memcached_is_allocated(string_ptr
) == false);
3242 test_true(memcached_is_allocated(&string
) == false);
3243 test_true(&string
== string_ptr
);
3245 test_true(string
.options
.is_initialized
== true);
3246 test_true(memcached_is_initialized(&string
) == true);
3247 memcached_string_free(&string
);
3248 test_true(memcached_is_initialized(&string
) == false);
3250 return TEST_SUCCESS
;
3253 static test_return_t
string_alloc_null(memcached_st
*memc
)
3255 memcached_string_st
*string
;
3257 string
= memcached_string_create(memc
, NULL
, 0);
3259 test_true(memcached_is_allocated(string
) == true);
3260 test_true(memcached_is_initialized(string
) == true);
3261 memcached_string_free(string
);
3263 return TEST_SUCCESS
;
3266 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3268 memcached_string_st
*string
;
3270 string
= memcached_string_create(memc
, NULL
, 1024);
3272 test_true(memcached_is_allocated(string
) == true);
3273 test_true(memcached_is_initialized(string
) == true);
3274 memcached_string_free(string
);
3276 return TEST_SUCCESS
;
3279 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3281 memcached_string_st
*string
;
3283 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3284 test_true(string
== NULL
);
3286 return TEST_SUCCESS
;
3289 static test_return_t
string_alloc_append(memcached_st
*memc
)
3292 char buffer
[SMALL_STRING_LEN
];
3293 memcached_string_st
*string
;
3295 /* Ring the bell! */
3296 memset(buffer
, 6, SMALL_STRING_LEN
);
3298 string
= memcached_string_create(memc
, NULL
, 100);
3300 test_true(memcached_is_allocated(string
) == true);
3301 test_true(memcached_is_initialized(string
) == true);
3303 for (x
= 0; x
< 1024; x
++)
3305 memcached_return_t rc
;
3306 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3307 test_true(rc
== MEMCACHED_SUCCESS
);
3309 test_true(memcached_is_allocated(string
) == true);
3310 memcached_string_free(string
);
3312 return TEST_SUCCESS
;
3315 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3317 memcached_return_t rc
;
3319 char buffer
[SMALL_STRING_LEN
];
3320 memcached_string_st
*string
;
3322 /* Ring the bell! */
3323 memset(buffer
, 6, SMALL_STRING_LEN
);
3325 string
= memcached_string_create(memc
, NULL
, 100);
3327 test_true(memcached_is_allocated(string
) == true);
3328 test_true(memcached_is_initialized(string
) == true);
3330 for (x
= 0; x
< 1024; x
++)
3332 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3333 test_true(rc
== MEMCACHED_SUCCESS
);
3335 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3336 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3337 test_true(memcached_is_allocated(string
) == true);
3338 memcached_string_free(string
);
3340 return TEST_SUCCESS
;
3343 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3346 pairs_free(global_pairs
);
3348 return TEST_SUCCESS
;
3351 static test_return_t
generate_pairs(memcached_st
*memc
)
3354 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3355 global_count
= GLOBAL_COUNT
;
3357 for (size_t x
= 0; x
< global_count
; x
++)
3359 global_keys
[x
]= global_pairs
[x
].key
;
3360 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3363 return TEST_SUCCESS
;
3366 static test_return_t
generate_large_pairs(memcached_st
*memc
)
3369 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3370 global_count
= GLOBAL2_COUNT
;
3372 for (size_t x
= 0; x
< global_count
; x
++)
3374 global_keys
[x
]= global_pairs
[x
].key
;
3375 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3378 return TEST_SUCCESS
;
3381 static test_return_t
generate_data(memcached_st
*memc
)
3383 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3385 test_true(check_execute
== global_count
);
3387 return TEST_SUCCESS
;
3390 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3392 memcached_stat_st
*stat_p
;
3393 memcached_return_t rc
;
3394 uint32_t host_index
= 0;
3395 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3397 test_true(check_execute
== global_count
);
3399 //TODO: hosts used size stats
3400 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3403 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3405 /* This test was changes so that "make test" would work properlly */
3407 memcached_server_instance_st instance
=
3408 memcached_server_instance_by_position(memc
, host_index
);
3410 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3412 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3415 memcached_stat_free(NULL
, stat_p
);
3417 return TEST_SUCCESS
;
3419 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3424 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3425 generate_data(memc
);
3427 return TEST_SUCCESS
;
3430 static test_return_t
get_read_count(memcached_st
*memc
)
3432 memcached_return_t rc
;
3433 memcached_st
*memc_clone
;
3435 memc_clone
= memcached_clone(NULL
, memc
);
3436 test_true(memc_clone
);
3438 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3442 size_t return_value_length
;
3446 for (size_t x
= count
= 0; x
< global_count
; x
++)
3448 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3449 &return_value_length
, &flags
, &rc
);
3450 if (rc
== MEMCACHED_SUCCESS
)
3459 memcached_free(memc_clone
);
3461 return TEST_SUCCESS
;
3464 static test_return_t
get_read(memcached_st
*memc
)
3466 memcached_return_t rc
;
3470 size_t return_value_length
;
3473 for (size_t x
= 0; x
< global_count
; x
++)
3475 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3476 &return_value_length
, &flags
, &rc
);
3478 test_true(return_value);
3479 test_true(rc == MEMCACHED_SUCCESS);
3481 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3486 return TEST_SUCCESS
;
3489 static test_return_t
mget_read(memcached_st
*memc
)
3491 memcached_return_t rc
;
3493 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3494 return TEST_SKIPPED
;
3496 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3498 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3500 // Go fetch the keys and test to see if all of them were returned
3502 size_t keys_returned
;
3503 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3505 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3506 test_true_got(global_count
== keys_returned
, buffer
);
3510 return TEST_SUCCESS
;
3513 static test_return_t
mget_read_result(memcached_st
*memc
)
3515 memcached_return_t rc
;
3517 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3518 return TEST_SKIPPED
;
3520 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3522 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3524 /* Turn this into a help function */
3526 memcached_result_st results_obj
;
3527 memcached_result_st
*results
;
3529 results
= memcached_result_create(memc
, &results_obj
);
3531 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3534 test_true(rc
== MEMCACHED_SUCCESS
);
3537 memcached_result_free(&results_obj
);
3540 return TEST_SUCCESS
;
3543 static test_return_t
mget_read_function(memcached_st
*memc
)
3545 memcached_return_t rc
;
3547 memcached_execute_fn callbacks
[1];
3549 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3550 return TEST_SKIPPED
;
3552 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3554 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3556 callbacks
[0]= &callback_counter
;
3558 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3560 return TEST_SUCCESS
;
3563 static test_return_t
delete_generate(memcached_st
*memc
)
3565 for (size_t x
= 0; x
< global_count
; x
++)
3567 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3570 return TEST_SUCCESS
;
3573 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3578 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3580 for (size_t x
= 0; x
< global_count
; x
++)
3582 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3585 return TEST_SUCCESS
;
3588 static test_return_t
add_host_test1(memcached_st
*memc
)
3590 memcached_return_t rc
;
3591 char servername
[]= "0.example.com";
3592 memcached_server_st
*servers
;
3594 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3596 test_true(1 == memcached_server_list_count(servers
));
3598 for (size_t x
= 2; x
< 20; x
++)
3600 char buffer
[SMALL_STRING_LEN
];
3602 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3603 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3605 test_true(rc
== MEMCACHED_SUCCESS
);
3606 test_true(x
== memcached_server_list_count(servers
));
3609 rc
= memcached_server_push(memc
, servers
);
3610 test_true(rc
== MEMCACHED_SUCCESS
);
3611 rc
= memcached_server_push(memc
, servers
);
3612 test_true(rc
== MEMCACHED_SUCCESS
);
3614 memcached_server_list_free(servers
);
3616 return TEST_SUCCESS
;
3619 static test_return_t
pre_nonblock(memcached_st
*memc
)
3621 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3623 return TEST_SUCCESS
;
3626 static test_return_t
pre_cork(memcached_st
*memc
)
3628 memcached_return_t rc
;
3631 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3634 return TEST_SKIPPED
;
3637 if (rc
== MEMCACHED_SUCCESS
)
3638 return TEST_SUCCESS
;
3640 return TEST_SKIPPED
;
3643 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3650 return TEST_SKIPPED
;
3653 if (rc
!= TEST_SUCCESS
)
3656 return pre_nonblock(memc
);
3659 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3661 memcached_return_t rc
= MEMCACHED_FAILURE
;
3662 memcached_st
*memc_clone
;
3664 memc_clone
= memcached_clone(NULL
, memc
);
3665 test_true(memc_clone
);
3666 // The memcached_version needs to be done on a clone, because the server
3667 // will not toggle protocol on an connection.
3668 memcached_version(memc_clone
);
3670 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3672 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3673 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3674 test_true(rc
== MEMCACHED_SUCCESS
);
3675 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3679 return TEST_SKIPPED
;
3682 memcached_free(memc_clone
);
3684 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3687 static test_return_t
pre_murmur(memcached_st
*memc
)
3689 #ifdef HAVE_MURMUR_HASH
3690 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3691 return TEST_SUCCESS
;
3694 return TEST_SKIPPED
;
3698 static test_return_t
pre_jenkins(memcached_st
*memc
)
3700 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3702 return TEST_SUCCESS
;
3706 static test_return_t
pre_md5(memcached_st
*memc
)
3708 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3710 return TEST_SUCCESS
;
3713 static test_return_t
pre_crc(memcached_st
*memc
)
3715 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3717 return TEST_SUCCESS
;
3720 static test_return_t
pre_hsieh(memcached_st
*memc
)
3722 #ifdef HAVE_HSIEH_HASH
3723 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3724 return TEST_SUCCESS
;
3727 return TEST_SKIPPED
;
3731 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3733 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3735 return TEST_SUCCESS
;
3738 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3740 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3742 return TEST_SUCCESS
;
3745 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3747 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3749 return TEST_SUCCESS
;
3752 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3754 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3756 return TEST_SUCCESS
;
3759 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3761 memcached_return_t rc
;
3764 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3765 test_true(rc
== MEMCACHED_SUCCESS
);
3767 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3768 test_true(value
== 1);
3770 return TEST_SUCCESS
;
3773 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3775 memcached_return_t rc
;
3778 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3779 test_true(rc
== MEMCACHED_SUCCESS
);
3781 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3782 test_true(value
== 1);
3784 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3785 test_true(rc
== MEMCACHED_SUCCESS
);
3787 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3788 test_true(value
== MEMCACHED_HASH_MD5
);
3790 return TEST_SUCCESS
;
3794 @note This should be testing to see if the server really supports the binary protocol.
3796 static test_return_t
pre_binary(memcached_st
*memc
)
3798 memcached_return_t rc
= MEMCACHED_FAILURE
;
3800 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3802 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3803 test_true(rc
== MEMCACHED_SUCCESS
);
3804 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3807 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3810 static test_return_t
pre_sasl(memcached_st
*memc
)
3812 memcached_return_t rc
= MEMCACHED_FAILURE
;
3814 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3815 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3816 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3817 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3819 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3821 memcached_server_st
*servers
= memcached_servers_parse(server
);
3822 test_true(servers
!= NULL
);
3823 memcached_servers_reset(memc
);
3824 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3825 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3826 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3827 test_true(rc
== MEMCACHED_SUCCESS
);
3833 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3836 static test_return_t
pre_replication(memcached_st
*memc
)
3838 test_return_t test_rc
;
3839 test_rc
= pre_binary(memc
);
3841 if (test_rc
!= TEST_SUCCESS
)
3845 * Make sure that we store the item on all servers
3846 * (master + replicas == number of servers)
3848 memcached_return_t rc
;
3849 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3850 memcached_server_count(memc
) - 1);
3851 test_true(rc
== MEMCACHED_SUCCESS
);
3852 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3854 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3858 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3862 rc
= pre_replication(memc
);
3863 if (rc
!= TEST_SUCCESS
)
3866 rc
= pre_nonblock(memc
);
3872 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3876 #ifdef HARD_MALLOC_TESTS
3877 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3885 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3889 #ifdef HARD_MALLOC_TESTS
3890 void *ret
= malloc(size
+ 8);
3893 ret
= (void*)((caddr_t
)ret
+ 8);
3896 void *ret
= malloc(size
);
3901 memset(ret
, 0xff, size
);
3908 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *context
)
3911 #ifdef HARD_MALLOC_TESTS
3912 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3913 void *nmem
= realloc(real_ptr
, size
+ 8);
3918 ret
= (void*)((caddr_t
)nmem
+ 8);
3924 return realloc(mem
, size
);
3929 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *context
)
3932 #ifdef HARD_MALLOC_TESTS
3933 void *mem
= my_malloc(ptr
, nelem
* size
);
3936 memset(mem
, 0, nelem
* size
);
3942 return calloc(nelem
, size
);
3947 static test_return_t
set_prefix(memcached_st
*memc
)
3949 memcached_return_t rc
;
3950 const char *key
= "mine";
3953 /* Make sure be default none exists */
3954 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3955 test_true(rc
== MEMCACHED_FAILURE
);
3957 /* Test a clean set */
3958 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3959 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3961 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3963 test_true(memcmp(value
, key
, 4) == 0);
3964 test_true(rc
== MEMCACHED_SUCCESS
);
3966 /* Test that we can turn it off */
3967 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3968 test_true(rc
== MEMCACHED_SUCCESS
);
3970 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3972 test_true(rc
== MEMCACHED_FAILURE
);
3974 /* Now setup for main test */
3975 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3976 test_true(rc
== MEMCACHED_SUCCESS
);
3978 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3980 test_true(rc
== MEMCACHED_SUCCESS
);
3981 test_true(memcmp(value
, key
, 4) == 0);
3983 /* Set to Zero, and then Set to something too large */
3986 memset(long_key
, 0, 255);
3988 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3989 test_true(rc
== MEMCACHED_SUCCESS
);
3991 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3993 test_true(rc
== MEMCACHED_FAILURE
);
3994 test_true(value
== NULL
);
3996 /* Test a long key for failure */
3997 /* TODO, extend test to determine based on setting, what result should be */
3998 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3999 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4000 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
4001 test_true(rc
== MEMCACHED_SUCCESS
);
4003 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
4004 strcpy(long_key
, "This is more then the allotted number of characters");
4005 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4006 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
4008 /* Test for a bad prefix, but with a short key */
4009 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
4010 test_true(rc
== MEMCACHED_SUCCESS
);
4012 strcpy(long_key
, "dog cat");
4013 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4014 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
4017 return TEST_SUCCESS
;
4021 #ifdef MEMCACHED_ENABLE_DEPRECATED
4022 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
4024 void *test_ptr
= NULL
;
4027 memcached_malloc_fn malloc_cb
=
4028 (memcached_malloc_fn
)my_malloc
;
4029 cb_ptr
= *(void **)&malloc_cb
;
4030 memcached_return_t rc
;
4032 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
4033 test_true(rc
== MEMCACHED_SUCCESS
);
4034 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
4035 test_true(rc
== MEMCACHED_SUCCESS
);
4036 test_true(test_ptr
== cb_ptr
);
4040 memcached_realloc_fn realloc_cb
=
4041 (memcached_realloc_fn
)my_realloc
;
4042 cb_ptr
= *(void **)&realloc_cb
;
4043 memcached_return_t rc
;
4045 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
4046 test_true(rc
== MEMCACHED_SUCCESS
);
4047 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
4048 test_true(rc
== MEMCACHED_SUCCESS
);
4049 test_true(test_ptr
== cb_ptr
);
4053 memcached_free_fn free_cb
=
4054 (memcached_free_fn
)my_free
;
4055 cb_ptr
= *(void **)&free_cb
;
4056 memcached_return_t rc
;
4058 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
4059 test_true(rc
== MEMCACHED_SUCCESS
);
4060 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
4061 test_true(rc
== MEMCACHED_SUCCESS
);
4062 test_true(test_ptr
== cb_ptr
);
4065 return TEST_SUCCESS
;
4070 static test_return_t
set_memory_alloc(memcached_st
*memc
)
4072 memcached_return_t rc
;
4073 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
4074 my_realloc
, my_calloc
, NULL
);
4075 test_true(rc
== MEMCACHED_FAILURE
);
4077 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
4078 my_realloc
, my_calloc
, NULL
);
4080 memcached_malloc_fn mem_malloc
;
4081 memcached_free_fn mem_free
;
4082 memcached_realloc_fn mem_realloc
;
4083 memcached_calloc_fn mem_calloc
;
4084 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4085 &mem_realloc
, &mem_calloc
);
4087 test_true(mem_malloc
== my_malloc
);
4088 test_true(mem_realloc
== my_realloc
);
4089 test_true(mem_calloc
== my_calloc
);
4090 test_true(mem_free
== my_free
);
4092 return TEST_SUCCESS
;
4095 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4098 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4099 memcached_hash_t hash
;
4100 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4101 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4104 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4105 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4107 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4109 if (hash
!= MEMCACHED_HASH_CRC
)
4110 return TEST_SKIPPED
;
4112 return TEST_SUCCESS
;
4115 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4118 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4119 memcached_hash_t hash
;
4120 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4121 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4124 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4125 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4127 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4129 if (hash
!= MEMCACHED_HASH_HSIEH
)
4130 return TEST_SKIPPED
;
4133 return TEST_SUCCESS
;
4136 static test_return_t
enable_cas(memcached_st
*memc
)
4138 unsigned int set
= 1;
4140 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4142 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4144 return TEST_SUCCESS
;
4147 return TEST_SKIPPED
;
4150 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4152 memcached_version(memc
);
4154 memcached_server_instance_st instance
=
4155 memcached_server_instance_by_position(memc
, 0);
4157 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4158 || instance
->minor_version
> 2)
4160 return TEST_SUCCESS
;
4163 return TEST_SKIPPED
;
4166 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4168 memcached_return_t rc
;
4171 memcached_servers_reset(memc
);
4173 if (stat("/tmp/memcached.socket", &buf
))
4174 return TEST_SKIPPED
;
4176 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4178 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4181 static test_return_t
pre_nodelay(memcached_st
*memc
)
4183 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4184 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4186 return TEST_SUCCESS
;
4189 static test_return_t
pre_settimer(memcached_st
*memc
)
4191 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4192 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4194 return TEST_SUCCESS
;
4197 static test_return_t
poll_timeout(memcached_st
*memc
)
4203 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4205 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4207 test_true(timeout
== 100);
4209 return TEST_SUCCESS
;
4212 static test_return_t
noreply_test(memcached_st
*memc
)
4214 memcached_return_t ret
;
4215 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4216 test_true(ret
== MEMCACHED_SUCCESS
);
4217 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4218 test_true(ret
== MEMCACHED_SUCCESS
);
4219 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4220 test_true(ret
== MEMCACHED_SUCCESS
);
4221 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4222 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4223 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4225 for (int count
=0; count
< 5; ++count
)
4227 for (size_t x
= 0; x
< 100; ++x
)
4230 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4231 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4233 size_t len
= (size_t)check_length
;
4238 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4241 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4244 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4247 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4250 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4256 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4260 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4261 ** API and is _ONLY_ done this way to verify that the library works the
4262 ** way it is supposed to do!!!!
4265 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4267 memcached_server_instance_st instance
=
4268 memcached_server_instance_by_position(memc
, x
);
4269 no_msg
+=(int)(instance
->cursor_active
);
4272 test_true(no_msg
== 0);
4273 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4276 ** Now validate that all items was set properly!
4278 for (size_t x
= 0; x
< 100; ++x
)
4282 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4284 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4286 size_t len
= (size_t)check_length
;
4289 char* value
=memcached_get(memc
, key
, strlen(key
),
4290 &length
, &flags
, &ret
);
4291 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4294 case 0: /* FALLTHROUGH */
4295 case 1: /* FALLTHROUGH */
4297 test_true(strncmp(value
, key
, len
) == 0);
4298 test_true(len
== length
);
4301 test_true(length
== len
* 2);
4304 test_true(length
== len
* 3);
4314 /* Try setting an illegal cas value (should not return an error to
4315 * the caller (because we don't expect a return message from the server)
4317 const char* keys
[]= {"0"};
4318 size_t lengths
[]= {1};
4321 memcached_result_st results_obj
;
4322 memcached_result_st
*results
;
4323 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4324 test_true(ret
== MEMCACHED_SUCCESS
);
4326 results
= memcached_result_create(memc
, &results_obj
);
4328 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4330 test_true(ret
== MEMCACHED_SUCCESS
);
4331 uint64_t cas
= memcached_result_cas(results
);
4332 memcached_result_free(&results_obj
);
4334 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4335 test_true(ret
== MEMCACHED_SUCCESS
);
4338 * The item will have a new cas value, so try to set it again with the old
4339 * value. This should fail!
4341 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4342 test_true(ret
== MEMCACHED_SUCCESS
);
4343 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4344 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4345 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4348 return TEST_SUCCESS
;
4351 static test_return_t
analyzer_test(memcached_st
*memc
)
4353 memcached_return_t rc
;
4354 memcached_stat_st
*memc_stat
;
4355 memcached_analysis_st
*report
;
4357 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4358 test_true(rc
== MEMCACHED_SUCCESS
);
4359 test_true(memc_stat
);
4361 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4362 test_true(rc
== MEMCACHED_SUCCESS
);
4366 memcached_stat_free(NULL
, memc_stat
);
4368 return TEST_SUCCESS
;
4371 /* Count the objects */
4372 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4377 (void)ptr
; (void)key
; (void)key_length
;
4378 size_t *counter
= (size_t *)context
;
4380 *counter
= *counter
+ 1;
4382 return MEMCACHED_SUCCESS
;
4385 static test_return_t
dump_test(memcached_st
*memc
)
4387 memcached_return_t rc
;
4389 memcached_dump_fn callbacks
[1];
4390 test_return_t main_rc
;
4392 callbacks
[0]= &callback_dump_counter
;
4394 /* No support for Binary protocol yet */
4395 if (memc
->flags
.binary_protocol
)
4396 return TEST_SUCCESS
;
4398 main_rc
= set_test3(memc
);
4400 test_true (main_rc
== TEST_SUCCESS
);
4402 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4403 test_true(rc
== MEMCACHED_SUCCESS
);
4405 /* We may have more then 32 if our previous flush has not completed */
4406 test_true(counter
>= 32);
4408 return TEST_SUCCESS
;
4411 #ifdef HAVE_LIBMEMCACHEDUTIL
4412 static void* connection_release(void *arg
)
4415 memcached_pool_st
* pool
;
4420 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4424 #define POOL_SIZE 10
4425 static test_return_t
connection_pool_test(memcached_st
*memc
)
4427 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4428 test_true(pool
!= NULL
);
4429 memcached_st
*mmc
[POOL_SIZE
];
4430 memcached_return_t rc
;
4432 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4434 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4435 test_true(mmc
[x
] != NULL
);
4436 test_true(rc
== MEMCACHED_SUCCESS
);
4439 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4440 test_true(rc
== MEMCACHED_SUCCESS
);
4444 memcached_pool_st
* pool
;
4446 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4447 pthread_create(&tid
, NULL
, connection_release
, &item
);
4448 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4449 test_true(rc
== MEMCACHED_SUCCESS
);
4450 pthread_join(tid
, NULL
);
4451 test_true(mmc
[9] == item
.mmc
);
4452 const char *key
= "key";
4453 size_t keylen
= strlen(key
);
4455 // verify that I can do ops with all connections
4456 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4457 test_true(rc
== MEMCACHED_SUCCESS
);
4459 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4461 uint64_t number_value
;
4462 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4463 test_true(rc
== MEMCACHED_SUCCESS
);
4464 test_true(number_value
== (x
+1));
4468 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4470 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4474 /* verify that I can set behaviors on the pool when I don't have all
4475 * of the connections in the pool. It should however be enabled
4476 * when I push the item into the pool
4478 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4479 test_true(mmc
[0] != NULL
);
4481 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4482 test_true(rc
== MEMCACHED_SUCCESS
);
4484 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4485 test_true(mmc
[1] != NULL
);
4487 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4488 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4489 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4491 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4492 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4493 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4495 test_true(memcached_pool_destroy(pool
) == memc
);
4497 return TEST_SUCCESS
;
4500 static test_return_t
util_version_test(memcached_st
*memc
)
4504 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4505 test_true(if_successful
== true);
4507 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4509 // We expect failure
4512 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4513 fprintf(stderr
, "\nDumping Server Information\n\n");
4514 memcached_server_fn callbacks
[1];
4516 callbacks
[0]= dump_server_information
;
4517 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4518 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4520 test_true(if_successful
== false);
4522 memcached_server_instance_st instance
=
4523 memcached_server_instance_by_position(memc
, 0);
4525 memcached_version(memc
);
4527 // We only use one binary when we test, so this should be just fine.
4528 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4529 test_true(if_successful
== true);
4531 if (instance
->micro_version
> 0)
4532 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4533 else if (instance
->minor_version
> 0)
4534 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4535 else if (instance
->major_version
> 0)
4536 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4538 test_true(if_successful
== true);
4540 if (instance
->micro_version
> 0)
4541 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4542 else if (instance
->minor_version
> 0)
4543 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4544 else if (instance
->major_version
> 0)
4545 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4547 test_true(if_successful
== false);
4549 return TEST_SUCCESS
;
4552 static test_return_t
ping_test(memcached_st
*memc
)
4554 memcached_return_t rc
;
4555 memcached_server_instance_st instance
=
4556 memcached_server_instance_by_position(memc
, 0);
4558 // Test both the version that returns a code, and the one that does not.
4559 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4560 memcached_server_port(instance
), NULL
));
4562 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4563 memcached_server_port(instance
), &rc
));
4565 test_true(rc
== MEMCACHED_SUCCESS
);
4567 return TEST_SUCCESS
;
4573 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4577 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4578 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4579 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4580 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4581 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4582 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4583 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4584 #ifdef HAVE_HSIEH_HASH
4585 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4587 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4588 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4589 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4591 return TEST_SUCCESS
;
4595 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4597 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4598 #ifdef HAVE_HSIEH_HASH
4599 expected_rc
= MEMCACHED_SUCCESS
;
4601 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4602 (uint64_t)MEMCACHED_HASH_HSIEH
);
4603 test_true(rc
== expected_rc
);
4605 return TEST_SUCCESS
;
4608 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4610 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4611 #ifdef HAVE_MURMUR_HASH
4612 expected_rc
= MEMCACHED_SUCCESS
;
4614 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4615 (uint64_t)MEMCACHED_HASH_MURMUR
);
4616 test_true(rc
== expected_rc
);
4618 return TEST_SUCCESS
;
4621 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4627 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4631 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4632 test_true(one_at_a_time_values
[x
] == hash_val
);
4635 return TEST_SUCCESS
;
4638 static test_return_t
md5_run (memcached_st
*memc
)
4644 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4648 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4649 test_true(md5_values
[x
] == hash_val
);
4652 return TEST_SUCCESS
;
4655 static test_return_t
crc_run (memcached_st
*memc
)
4661 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4665 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4666 test_true(crc_values
[x
] == hash_val
);
4669 return TEST_SUCCESS
;
4672 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4678 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4682 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4683 test_true(fnv1_64_values
[x
] == hash_val
);
4686 return TEST_SUCCESS
;
4689 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4695 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4699 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4700 test_true(fnv1a_64_values
[x
] == hash_val
);
4703 return TEST_SUCCESS
;
4706 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4712 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4716 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4717 test_true(fnv1_32_values
[x
] == hash_val
);
4720 return TEST_SUCCESS
;
4723 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4729 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4733 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4734 test_true(fnv1a_32_values
[x
] == hash_val
);
4737 return TEST_SUCCESS
;
4740 static test_return_t
hsieh_run (memcached_st
*memc
)
4746 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4750 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4751 test_true(hsieh_values
[x
] == hash_val
);
4754 return TEST_SUCCESS
;
4757 static test_return_t
murmur_run (memcached_st
*memc
)
4759 #ifdef WORDS_BIGENDIAN
4760 (void)murmur_values
;
4761 return TEST_SKIPPED
;
4767 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4771 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4772 test_true(murmur_values
[x
] == hash_val
);
4775 return TEST_SUCCESS
;
4779 static test_return_t
jenkins_run (memcached_st
*memc
)
4785 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4789 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4790 test_true(jenkins_values
[x
] == hash_val
);
4793 return TEST_SUCCESS
;
4796 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4799 return libhashkit_md5(string
, string_length
);
4802 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4805 return libhashkit_crc32(string
, string_length
);
4808 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4812 const hashkit_st
*kit
;
4814 hashkit_return_t hash_rc
;
4816 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};
4817 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};
4819 kit
= memcached_get_hashkit(memc
);
4821 hashkit_clone(&new_kit
, kit
);
4822 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4823 test_true(hash_rc
== HASHKIT_SUCCESS
);
4825 memcached_set_hashkit(memc
, &new_kit
);
4828 Verify Setting the hash.
4830 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4834 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4835 test_true(md5_values
[x
] == hash_val
);
4840 Now check memcached_st.
4842 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4846 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4847 test_true(md5_hosts
[x
] == hash_val
);
4850 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4851 test_true(hash_rc
== HASHKIT_SUCCESS
);
4853 memcached_set_hashkit(memc
, &new_kit
);
4856 Verify Setting the hash.
4858 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4862 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4863 test_true(crc_values
[x
] == hash_val
);
4866 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4870 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4871 test_true(crc_hosts
[x
] == hash_val
);
4874 return TEST_SUCCESS
;
4878 Test case adapted from John Gorman <johngorman2@gmail.com>
4880 We are testing the error condition when we connect to a server via memcached_get()
4881 but find that the server is not available.
4883 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4886 memcached_st
*tl_memc_h
;
4887 memcached_server_st
*servers
;
4889 const char *key
= "MemcachedLives";
4892 memcached_return rc
;
4896 tl_memc_h
= memcached_create(NULL
);
4897 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4898 memcached_server_push(tl_memc_h
, servers
);
4899 memcached_server_list_free(servers
);
4901 // See if memcached is reachable.
4902 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4905 test_true(len
== 0);
4906 test_false(rc
== MEMCACHED_SUCCESS
);
4908 memcached_free(tl_memc_h
);
4910 return TEST_SUCCESS
;
4914 We connect to a server which exists, but search for a key that does not exist.
4916 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4918 const char *key
= "MemcachedKeyNotEXIST";
4921 memcached_return rc
;
4924 // See if memcached is reachable.
4925 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4928 test_true(len
== 0);
4929 test_true(rc
== MEMCACHED_NOTFOUND
);
4931 return TEST_SUCCESS
;
4935 Test case adapted from John Gorman <johngorman2@gmail.com>
4937 We are testing the error condition when we connect to a server via memcached_get_by_key()
4938 but find that the server is not available.
4940 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4943 memcached_st
*tl_memc_h
;
4944 memcached_server_st
*servers
;
4946 const char *key
= "MemcachedLives";
4949 memcached_return rc
;
4953 tl_memc_h
= memcached_create(NULL
);
4954 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4955 memcached_server_push(tl_memc_h
, servers
);
4956 memcached_server_list_free(servers
);
4958 // See if memcached is reachable.
4959 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4962 test_true(len
== 0);
4963 test_false(rc
== MEMCACHED_SUCCESS
);
4965 memcached_free(tl_memc_h
);
4967 return TEST_SUCCESS
;
4971 We connect to a server which exists, but search for a key that does not exist.
4973 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4975 const char *key
= "MemcachedKeyNotEXIST";
4978 memcached_return rc
;
4981 // See if memcached is reachable.
4982 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4985 test_true(len
== 0);
4986 test_true(rc
== MEMCACHED_NOTFOUND
);
4988 return TEST_SUCCESS
;
4992 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4994 memcached_return_t rc
;
4997 memcached_server_st
*server_pool
;
5002 memc
= memcached_create(NULL
);
5005 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5006 test_true(rc
== MEMCACHED_SUCCESS
);
5008 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5009 test_true(value
== 1);
5011 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5012 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5015 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");
5016 memcached_server_push(memc
, server_pool
);
5018 /* verify that the server list was parsed okay. */
5019 test_true(memcached_server_count(memc
) == 8);
5020 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5021 test_true(server_pool
[0].port
== 11211);
5022 test_true(server_pool
[0].weight
== 600);
5023 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5024 test_true(server_pool
[2].port
== 11211);
5025 test_true(server_pool
[2].weight
== 200);
5026 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5027 test_true(server_pool
[7].port
== 11211);
5028 test_true(server_pool
[7].weight
== 100);
5030 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5031 * us test the boundary wraparound.
5033 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5035 /* verify the standard ketama set. */
5036 for (x
= 0; x
< 99; x
++)
5038 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5039 memcached_server_instance_st instance
=
5040 memcached_server_instance_by_position(memc
, server_idx
);
5041 const char *hostname
= memcached_server_name(instance
);
5043 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5046 memcached_server_list_free(server_pool
);
5047 memcached_free(memc
);
5049 return TEST_SUCCESS
;
5052 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5054 memcached_return_t rc
;
5057 memcached_server_st
*server_pool
;
5062 memc
= memcached_create(NULL
);
5065 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5066 test_true(rc
== MEMCACHED_SUCCESS
);
5068 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5069 test_true(value
== 1);
5071 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5072 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5074 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");
5075 memcached_server_push(memc
, server_pool
);
5077 /* verify that the server list was parsed okay. */
5078 test_true(memcached_server_count(memc
) == 8);
5079 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5080 test_true(server_pool
[0].port
== 11211);
5081 test_true(server_pool
[0].weight
== 600);
5082 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5083 test_true(server_pool
[2].port
== 11211);
5084 test_true(server_pool
[2].weight
== 200);
5085 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5086 test_true(server_pool
[7].port
== 11211);
5087 test_true(server_pool
[7].weight
== 100);
5089 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5090 * us test the boundary wraparound.
5092 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5094 /* verify the standard ketama set. */
5095 for (x
= 0; x
< 99; x
++)
5097 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5099 memcached_server_instance_st instance
=
5100 memcached_server_instance_by_position(memc
, server_idx
);
5102 const char *hostname
= memcached_server_name(instance
);
5104 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5107 memcached_server_list_free(server_pool
);
5108 memcached_free(memc
);
5110 return TEST_SUCCESS
;
5113 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5115 test_return_t test_rc
;
5116 test_rc
= pre_binary(memc
);
5118 if (test_rc
!= TEST_SUCCESS
)
5121 memcached_return_t ret
;
5122 const char *key
= "regression_bug_434484";
5123 size_t keylen
= strlen(key
);
5125 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5126 test_true(ret
== MEMCACHED_NOTSTORED
);
5128 size_t size
= 2048 * 1024;
5129 void *data
= calloc(1, size
);
5130 test_true(data
!= NULL
);
5131 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5132 test_true(ret
== MEMCACHED_E2BIG
);
5135 return TEST_SUCCESS
;
5138 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5140 test_return_t test_rc
;
5141 test_rc
= pre_binary(memc
);
5143 if (test_rc
!= TEST_SUCCESS
)
5146 memcached_return_t rc
;
5148 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5151 * I only want to hit only _one_ server so I know the number of requests I'm
5152 * sending in the pipleine to the server. Let's try to do a multiget of
5153 * 1024 (that should satisfy most users don't you think?). Future versions
5154 * will include a mget_execute function call if you need a higher number.
5156 uint32_t number_of_hosts
= memcached_server_count(memc
);
5157 memc
->number_of_hosts
= 1;
5158 const size_t max_keys
= 1024;
5159 char **keys
= calloc(max_keys
, sizeof(char*));
5160 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5162 for (size_t x
= 0; x
< max_keys
; ++x
)
5166 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5168 test_true(keys
[x
] != NULL
);
5172 * Run two times.. the first time we should have 100% cache miss,
5173 * and the second time we should have 100% cache hits
5175 for (size_t y
= 0; y
< 2; y
++)
5177 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5178 test_true(rc
== MEMCACHED_SUCCESS
);
5179 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5183 /* The first iteration should give me a 100% cache miss. verify that*/
5184 char blob
[1024]= { 0 };
5186 test_true(counter
== 0);
5188 for (size_t x
= 0; x
< max_keys
; ++x
)
5190 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5191 blob
, sizeof(blob
), 0, 0);
5192 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5197 /* Verify that we received all of the key/value pairs */
5198 test_true(counter
== max_keys
);
5202 /* Release allocated resources */
5203 for (size_t x
= 0; x
< max_keys
; ++x
)
5210 memc
->number_of_hosts
= number_of_hosts
;
5212 return TEST_SUCCESS
;
5215 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5217 memcached_return_t rc
;
5218 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5219 test_true(rc
== MEMCACHED_SUCCESS
);
5221 return regression_bug_434843(memc
);
5224 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5226 memcached_return_t rc
;
5227 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5228 test_true(rc
== MEMCACHED_SUCCESS
);
5230 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5231 test_true(rc
== MEMCACHED_SUCCESS
);
5232 test_true(bytes
!= NULL
);
5233 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5235 test_true(rc
== MEMCACHED_SUCCESS
);
5236 test_true(bytes_read
!= NULL
);
5238 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5239 "bytes_written", &rc
);
5240 test_true(rc
== MEMCACHED_SUCCESS
);
5241 test_true(bytes_written
!= NULL
);
5243 test_true(strcmp(bytes
, bytes_read
) != 0);
5244 test_true(strcmp(bytes
, bytes_written
) != 0);
5246 /* Release allocated resources */
5249 free(bytes_written
);
5250 memcached_stat_free(NULL
, memc_stat
);
5252 return TEST_SUCCESS
;
5256 * The test case isn't obvious so I should probably document why
5257 * it works the way it does. Bug 442914 was caused by a bug
5258 * in the logic in memcached_purge (it did not handle the case
5259 * where the number of bytes sent was equal to the watermark).
5260 * In this test case, create messages so that we hit that case
5261 * and then disable noreply mode and issue a new command to
5262 * verify that it isn't stuck. If we change the format for the
5263 * delete command or the watermarks, we need to update this
5266 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5268 memcached_return_t rc
;
5269 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5270 test_true(rc
== MEMCACHED_SUCCESS
);
5271 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5273 uint32_t number_of_hosts
= memcached_server_count(memc
);
5274 memc
->number_of_hosts
= 1;
5279 for (uint32_t x
= 0; x
< 250; ++x
)
5281 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5282 rc
= memcached_delete(memc
, k
, len
, 0);
5283 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5286 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5289 rc
= memcached_delete(memc
, k
, len
, 0);
5290 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5292 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5293 test_true(rc
== MEMCACHED_SUCCESS
);
5294 rc
= memcached_delete(memc
, k
, len
, 0);
5295 test_true(rc
== MEMCACHED_NOTFOUND
);
5297 memc
->number_of_hosts
= number_of_hosts
;
5299 return TEST_SUCCESS
;
5302 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5304 memcached_server_instance_st instance_one
;
5305 memcached_server_instance_st instance_two
;
5307 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5308 return TEST_SKIPPED
;
5310 memcached_return_t rc
;
5312 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5313 test_true(rc
== MEMCACHED_SUCCESS
);
5315 const size_t max_keys
= 100;
5316 char **keys
= calloc(max_keys
, sizeof(char*));
5317 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5319 for (size_t x
= 0; x
< max_keys
; ++x
)
5323 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5325 test_true(keys
[x
] != NULL
);
5326 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5327 test_true(rc
== MEMCACHED_SUCCESS
);
5331 ** We are using the quiet commands to store the replicas, so we need
5332 ** to ensure that all of them are processed before we can continue.
5333 ** In the test we go directly from storing the object to trying to
5334 ** receive the object from all of the different servers, so we
5335 ** could end up in a race condition (the memcached server hasn't yet
5336 ** processed the quiet command from the replication set when it process
5337 ** the request from the other client (created by the clone)). As a
5338 ** workaround for that we call memcached_quit to send the quit command
5339 ** to the server and wait for the response ;-) If you use the test code
5340 ** as an example for your own code, please note that you shouldn't need
5343 memcached_quit(memc
);
5345 /* Verify that all messages are stored, and we didn't stuff too much
5348 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5349 test_true(rc
== MEMCACHED_SUCCESS
);
5352 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5353 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5354 /* Verify that we received all of the key/value pairs */
5355 test_true(counter
== max_keys
);
5357 memcached_quit(memc
);
5359 * Don't do the following in your code. I am abusing the internal details
5360 * within the library, and this is not a supported interface.
5361 * This is to verify correct behavior in the library. Fake that two servers
5364 instance_one
= memcached_server_instance_by_position(memc
, 0);
5365 instance_two
= memcached_server_instance_by_position(memc
, 2);
5366 in_port_t port0
= instance_one
->port
;
5367 in_port_t port2
= instance_two
->port
;
5369 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5370 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5372 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5373 test_true(rc
== MEMCACHED_SUCCESS
);
5376 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5377 test_true(counter
== (unsigned int)max_keys
);
5379 /* restore the memc handle */
5380 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5381 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5383 memcached_quit(memc
);
5385 /* Remove half of the objects */
5386 for (size_t x
= 0; x
< max_keys
; ++x
)
5390 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5391 test_true(rc
== MEMCACHED_SUCCESS
);
5395 memcached_quit(memc
);
5396 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5397 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5399 /* now retry the command, this time we should have cache misses */
5400 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5401 test_true(rc
== MEMCACHED_SUCCESS
);
5404 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5405 test_true(counter
== (unsigned int)(max_keys
>> 1));
5407 /* Release allocated resources */
5408 for (size_t x
= 0; x
< max_keys
; ++x
)
5415 /* restore the memc handle */
5416 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5417 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5419 return TEST_SUCCESS
;
5422 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5424 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5425 test_true(memc_clone
!= NULL
);
5426 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5428 memcached_server_instance_st instance
=
5429 memcached_server_instance_by_position(memc_clone
, 0);
5431 if (instance
->major_version
> 1 ||
5432 (instance
->major_version
== 1 &&
5433 instance
->minor_version
> 2))
5435 /* Binary protocol doesn't support deferred delete */
5436 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5437 test_true(bin_clone
!= NULL
);
5438 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5439 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5440 memcached_free(bin_clone
);
5442 memcached_quit(memc_clone
);
5444 /* If we know the server version, deferred delete should fail
5445 * with invalid arguments */
5446 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5448 /* If we don't know the server version, we should get a protocol error */
5449 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5451 /* but there is a bug in some of the memcached servers (1.4) that treats
5452 * the counter as noreply so it doesn't send the proper error message
5454 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5456 /* And buffered mode should be disabled and we should get protocol error */
5457 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5458 rc
= memcached_delete(memc
, "foo", 3, 1);
5459 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5461 /* Same goes for noreply... */
5462 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5463 rc
= memcached_delete(memc
, "foo", 3, 1);
5464 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5466 /* but a normal request should go through (and be buffered) */
5467 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5468 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5470 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5471 /* unbuffered noreply should be success */
5472 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5473 /* unbuffered with reply should be not found... */
5474 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5475 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5478 memcached_free(memc_clone
);
5479 return TEST_SUCCESS
;
5483 /* Test memcached_server_get_last_disconnect
5484 * For a working server set, shall be NULL
5485 * For a set of non existing server, shall not be NULL
5487 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5489 memcached_return_t rc
;
5490 memcached_server_instance_st disconnected_server
;
5492 /* With the working set of server */
5493 const char *key
= "marmotte";
5494 const char *value
= "milka";
5496 memcached_reset_last_disconnected_server(memc
);
5497 rc
= memcached_set(memc
, key
, strlen(key
),
5498 value
, strlen(value
),
5499 (time_t)0, (uint32_t)0);
5500 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5502 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5503 test_true(disconnected_server
== NULL
);
5505 /* With a non existing server */
5507 memcached_server_st
*servers
;
5509 const char *server_list
= "localhost:9";
5511 servers
= memcached_servers_parse(server_list
);
5513 mine
= memcached_create(NULL
);
5514 rc
= memcached_server_push(mine
, servers
);
5515 test_true(rc
== MEMCACHED_SUCCESS
);
5516 memcached_server_list_free(servers
);
5519 rc
= memcached_set(mine
, key
, strlen(key
),
5520 value
, strlen(value
),
5521 (time_t)0, (uint32_t)0);
5522 test_true(rc
!= MEMCACHED_SUCCESS
);
5524 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5525 if (disconnected_server
== NULL
)
5527 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5530 test_true(disconnected_server
!= NULL
);
5531 test_true(memcached_server_port(disconnected_server
)== 9);
5532 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5534 memcached_quit(mine
);
5535 memcached_free(mine
);
5537 return TEST_SUCCESS
;
5540 static test_return_t
test_verbosity(memcached_st
*memc
)
5542 memcached_verbosity(memc
, 3);
5544 return TEST_SUCCESS
;
5547 static test_return_t
test_server_failure(memcached_st
*memc
)
5549 memcached_st
*local_memc
;
5550 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5552 local_memc
= memcached_create(NULL
);
5554 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5555 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5557 uint32_t server_count
= memcached_server_count(local_memc
);
5559 test_true(server_count
== 1);
5561 // Disable the server
5562 instance
= memcached_server_instance_by_position(local_memc
, 0);
5563 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5565 memcached_return_t rc
;
5566 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5568 (time_t)0, (uint32_t)0);
5569 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5571 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5572 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5574 (time_t)0, (uint32_t)0);
5575 test_true(rc
== MEMCACHED_SUCCESS
);
5578 memcached_free(local_memc
);
5580 return TEST_SUCCESS
;
5583 static test_return_t
test_cull_servers(memcached_st
*memc
)
5585 uint32_t count
= memcached_server_count(memc
);
5587 // Do not do this in your code, it is not supported.
5588 memc
->servers
[1].state
.is_dead
= true;
5589 memc
->state
.is_time_for_rebuild
= true;
5591 uint32_t new_count
= memcached_server_count(memc
);
5592 test_true(count
== new_count
);
5595 test_true(count
== new_count
+ 1 );
5598 return TEST_SUCCESS
;
5602 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5603 const char *key
, size_t key_length
,
5604 const char *value
, size_t value_length
,
5614 return MEMCACHED_SUCCESS
;
5617 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5619 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5620 test_true(rc
== MEMCACHED_SUCCESS
);
5622 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5623 test_true(rc
== MEMCACHED_SUCCESS
);
5625 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5626 test_true(rc
== MEMCACHED_SUCCESS
);
5628 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5629 test_true(rc
== MEMCACHED_SUCCESS
);
5631 return TEST_SUCCESS
;
5635 * This test ensures that the failure counter isn't incremented during
5636 * normal termination of the memcached instance.
5638 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5640 memcached_return_t rc
;
5641 memcached_server_instance_st instance
;
5643 /* Set value to force connection to the server */
5644 const char *key
= "marmotte";
5645 const char *value
= "milka";
5648 * Please note that I'm abusing the internal structures in libmemcached
5649 * in a non-portable way and you shouldn't be doing this. I'm only
5650 * doing this in order to verify that the library works the way it should
5652 uint32_t number_of_hosts
= memcached_server_count(memc
);
5653 memc
->number_of_hosts
= 1;
5655 /* Ensure that we are connected to the server by setting a value */
5656 rc
= memcached_set(memc
, key
, strlen(key
),
5657 value
, strlen(value
),
5658 (time_t)0, (uint32_t)0);
5659 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5662 instance
= memcached_server_instance_by_position(memc
, 0);
5663 /* The test is to see that the memcached_quit doesn't increase the
5664 * the server failure conter, so let's ensure that it is zero
5665 * before sending quit
5667 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5669 memcached_quit(memc
);
5671 /* Verify that it memcached_quit didn't increment the failure counter
5672 * Please note that this isn't bullet proof, because an error could
5675 test_true(instance
->server_failure_counter
== 0);
5677 /* restore the instance */
5678 memc
->number_of_hosts
= number_of_hosts
;
5680 return TEST_SUCCESS
;
5684 * This tests ensures expected disconnections (for some behavior changes
5685 * for instance) do not wrongly increase failure counter
5687 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5689 memcached_return rc
;
5691 memcached_st
*memc_clone
;
5692 memc_clone
= memcached_clone(NULL
, memc
);
5693 test_true(memc_clone
);
5695 /* Set value to force connection to the server */
5696 const char *key
= "marmotte";
5697 const char *value
= "milka";
5698 char *string
= NULL
;
5699 size_t string_length
;
5702 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5703 value
, strlen(value
),
5704 (time_t)0, (uint32_t)0);
5705 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5708 /* put failure limit to 1 */
5709 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5710 assert(rc
== MEMCACHED_SUCCESS
);
5711 /* Put a retry timeout to effectively activate failure_limit effect */
5712 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5713 assert(rc
== MEMCACHED_SUCCESS
);
5714 /* change behavior that triggers memcached_quit()*/
5715 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5716 assert(rc
== MEMCACHED_SUCCESS
);
5719 /* Check if we still are connected */
5720 string
= memcached_get(memc_clone
, key
, strlen(key
),
5721 &string_length
, &flags
, &rc
);
5723 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5727 return TEST_SUCCESS
;
5734 * Test that ensures mget_execute does not end into recursive calls that finally fails
5736 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5738 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5739 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5740 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5741 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5742 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5745 return TEST_SKIPPED
; // My MAC can't handle this test
5749 * I only want to hit _one_ server so I know the number of requests I'm
5750 * sending in the pipeline.
5752 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5753 memc
->number_of_hosts
= 1;
5754 size_t max_keys
= 20480;
5757 char **keys
= calloc(max_keys
, sizeof(char*));
5758 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5760 /* First add all of the items.. */
5762 char blob
[1024]= { 0 };
5763 memcached_return rc
;
5764 for (size_t x
= 0; x
< max_keys
; ++x
)
5767 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5769 assert(keys
[x
] != NULL
);
5770 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5772 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5774 break; // We are out of business
5777 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5779 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5782 sleep(1);// We will try to sleep
5785 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5787 // We failed to send everything.
5792 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5795 /* Try to get all of them with a large multiget */
5797 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5798 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5799 (size_t)max_keys
, callbacks
, &counter
, 1);
5801 assert(rc
== MEMCACHED_SUCCESS
);
5802 char* the_value
= NULL
;
5803 char the_key
[MEMCACHED_MAX_KEY
];
5804 size_t the_key_length
;
5805 size_t the_value_length
;
5809 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5811 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5817 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5820 assert(rc
== MEMCACHED_END
);
5822 /* Verify that we got all of the items */
5823 assert(counter
== max_keys
);
5826 /* Release all allocated resources */
5827 for (size_t x
= 0; x
< max_keys
; ++x
)
5834 memc
->number_of_hosts
= number_of_hosts
;
5836 return TEST_SUCCESS
;
5839 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5843 memcached_st
*memc
= memcached_create(NULL
);
5845 memcached_server_add(memc
, "10.2.3.4", 11211);
5847 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5848 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5849 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5850 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5851 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5852 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5854 memcached_return_t rc
;
5858 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5860 test_true_got(rc
== MEMCACHED_TIMEOUT
, memcached_strerror(NULL
, rc
));
5862 memcached_free(memc
);
5864 return TEST_SUCCESS
;
5867 // Look for memory leak
5868 static test_return_t
regression_bug_728286(memcached_st
*unused
)
5871 memcached_server_st
*servers
= memcached_servers_parse("1.2.3.4:99");
5872 memcached_server_free(servers
);
5874 return TEST_SUCCESS
;
5877 static test_return_t
regression_bug_581030(memcached_st
*unused
)
5882 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5883 test_false(local_stat
);
5885 memcached_stat_free(NULL
, NULL
);
5888 return TEST_SUCCESS
;
5891 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5893 fprintf(stderr
, "Iteration #%u: ", it
);
5895 if(error
== MEMCACHED_ERRNO
)
5897 fprintf(stderr
, "system error %d from %s: %s\n",
5898 errno
, what
, strerror(errno
));
5902 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5903 memcached_strerror(mc
, error
));
5907 #define TEST_CONSTANT_CREATION 200
5909 static test_return_t
regression_bug_(memcached_st
*memc
)
5911 const char *remote_server
;
5914 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5916 return TEST_SKIPPED
;
5919 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5921 memcached_st
* mc
= memcached_create(NULL
);
5922 memcached_return rc
;
5924 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5925 if (rc
!= MEMCACHED_SUCCESS
)
5927 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5930 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5931 if (rc
!= MEMCACHED_SUCCESS
)
5933 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5936 rc
= memcached_server_add(mc
, remote_server
, 0);
5937 if (rc
!= MEMCACHED_SUCCESS
)
5939 memcached_die(mc
, rc
, "memcached_server_add", x
);
5942 const char *set_key
= "akey";
5943 const size_t set_key_len
= strlen(set_key
);
5944 const char *set_value
= "a value";
5945 const size_t set_value_len
= strlen(set_value
);
5947 if (rc
== MEMCACHED_SUCCESS
)
5951 size_t get_value_len
;
5953 uint32_t get_value_flags
;
5955 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5956 &get_value_flags
, &rc
);
5957 if (rc
!= MEMCACHED_SUCCESS
)
5959 memcached_die(mc
, rc
, "memcached_get", x
);
5965 (get_value_len
!= set_value_len
5966 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5968 fprintf(stderr
, "Values don't match?\n");
5969 rc
= MEMCACHED_FAILURE
;
5975 rc
= memcached_set(mc
,
5976 set_key
, set_key_len
,
5977 set_value
, set_value_len
,
5981 if (rc
!= MEMCACHED_SUCCESS
)
5983 memcached_die(mc
, rc
, "memcached_set", x
);
5990 if (rc
!= MEMCACHED_SUCCESS
)
5996 return TEST_SUCCESS
;
6000 * Test that the sasl authentication works. We cannot use the default
6001 * pool of servers, because that would require that all servers we want
6002 * to test supports SASL authentication, and that they use the default
6005 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6007 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6008 memcached_return_t rc
;
6010 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6011 test_true(rc
== MEMCACHED_SUCCESS
);
6012 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6013 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6014 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6015 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6016 memcached_quit(memc
);
6018 rc
= memcached_set_sasl_auth_data(memc
,
6019 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6020 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6021 test_true(rc
== MEMCACHED_SUCCESS
);
6023 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6024 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6025 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6027 memcached_quit(memc
);
6028 return TEST_SUCCESS
;
6031 return TEST_FAILURE
;
6035 /* Clean the server before beginning testing */
6037 {"util_version", 1, (test_callback_fn
)util_version_test
},
6038 {"flush", 0, (test_callback_fn
)flush_test
},
6039 {"init", 0, (test_callback_fn
)init_test
},
6040 {"allocation", 0, (test_callback_fn
)allocation_test
},
6041 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6042 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6043 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6044 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6045 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6046 {"clone_test", 0, (test_callback_fn
)clone_test
},
6047 {"connection_test", 0, (test_callback_fn
)connection_test
},
6048 {"callback_test", 0, (test_callback_fn
)callback_test
},
6049 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6050 {"error", 0, (test_callback_fn
)error_test
},
6051 {"set", 0, (test_callback_fn
)set_test
},
6052 {"set2", 0, (test_callback_fn
)set_test2
},
6053 {"set3", 0, (test_callback_fn
)set_test3
},
6054 {"dump", 1, (test_callback_fn
)dump_test
},
6055 {"add", 1, (test_callback_fn
)add_test
},
6056 {"replace", 1, (test_callback_fn
)replace_test
},
6057 {"delete", 1, (test_callback_fn
)delete_test
},
6058 {"get", 1, (test_callback_fn
)get_test
},
6059 {"get2", 0, (test_callback_fn
)get_test2
},
6060 {"get3", 0, (test_callback_fn
)get_test3
},
6061 {"get4", 0, (test_callback_fn
)get_test4
},
6062 {"partial mget", 0, (test_callback_fn
)get_test5
},
6063 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6064 {"increment", 0, (test_callback_fn
)increment_test
},
6065 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6066 {"decrement", 0, (test_callback_fn
)decrement_test
},
6067 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6068 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6069 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6070 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6071 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6072 {"quit", 0, (test_callback_fn
)quit_test
},
6073 {"mget", 1, (test_callback_fn
)mget_test
},
6074 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6075 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6076 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6077 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6078 {"mget_end", 0, (test_callback_fn
)mget_end
},
6079 {"get_stats", 0, (test_callback_fn
)get_stats
},
6080 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6081 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6082 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6083 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6084 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6085 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6086 {"read_through", 1, (test_callback_fn
)read_through
},
6087 {"delete_through", 1, (test_callback_fn
)delete_through
},
6088 {"noreply", 1, (test_callback_fn
)noreply_test
},
6089 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6090 #ifdef HAVE_LIBMEMCACHEDUTIL
6091 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6092 {"ping", 1, (test_callback_fn
)ping_test
},
6094 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6095 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6096 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6097 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6098 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
6102 test_st behavior_tests
[] ={
6103 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6104 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6105 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6106 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6110 test_st basic_tests
[] ={
6111 {"init", 1, (test_callback_fn
)basic_init_test
},
6112 {"clone", 1, (test_callback_fn
)basic_clone_test
},
6113 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
6114 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
6115 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
6116 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
6120 test_st regression_binary_vs_block
[] ={
6121 {"block add", 1, (test_callback_fn
)block_add_regression
},
6122 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6126 test_st async_tests
[] ={
6127 {"add", 1, (test_callback_fn
)add_wrapper
},
6131 test_st string_tests
[] ={
6132 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6133 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6134 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6135 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6136 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6137 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6138 {0, 0, (test_callback_fn
)0}
6141 test_st result_tests
[] ={
6142 {"result static", 0, (test_callback_fn
)result_static
},
6143 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6144 {0, 0, (test_callback_fn
)0}
6147 test_st version_1_2_3
[] ={
6148 {"append", 0, (test_callback_fn
)append_test
},
6149 {"prepend", 0, (test_callback_fn
)prepend_test
},
6150 {"cas", 0, (test_callback_fn
)cas_test
},
6151 {"cas2", 0, (test_callback_fn
)cas2_test
},
6152 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6153 {0, 0, (test_callback_fn
)0}
6156 test_st user_tests
[] ={
6157 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6158 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6159 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6160 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6161 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6162 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6163 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6164 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6165 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6166 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6167 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6168 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6169 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6170 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6171 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6172 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6173 #if !defined(__sun) && !defined(__OpenBSD__)
6175 ** It seems to be something weird with the character sets..
6176 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6177 ** guess I need to find out how this is supposed to work.. Perhaps I need
6178 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6179 ** so just disable the code for now...).
6181 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6183 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6184 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6185 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6186 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6187 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6188 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6189 {0, 0, (test_callback_fn
)0}
6192 test_st replication_tests
[]= {
6193 {"set", 1, (test_callback_fn
)replication_set_test
},
6194 {"get", 0, (test_callback_fn
)replication_get_test
},
6195 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6196 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6197 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6198 {0, 0, (test_callback_fn
)0}
6202 * The following test suite is used to verify that we don't introduce
6203 * regression bugs. If you want more information about the bug / test,
6204 * you should look in the bug report at
6205 * http://bugs.launchpad.net/libmemcached
6207 test_st regression_tests
[]= {
6208 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6209 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6210 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6211 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6212 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6213 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6214 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6215 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6216 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6217 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6218 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6219 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6220 {0, 0, (test_callback_fn
)0}
6223 test_st sasl_auth_tests
[]= {
6224 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6225 {0, 0, (test_callback_fn
)0}
6228 test_st ketama_compatibility
[]= {
6229 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6230 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6231 {0, 0, (test_callback_fn
)0}
6234 test_st generate_tests
[] ={
6235 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6236 {"generate_data", 1, (test_callback_fn
)generate_data
},
6237 {"get_read", 0, (test_callback_fn
)get_read
},
6238 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6239 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6240 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6241 {"generate_data", 1, (test_callback_fn
)generate_data
},
6242 {"mget_read", 0, (test_callback_fn
)mget_read
},
6243 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6244 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6245 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6246 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6247 {"generate_data", 1, (test_callback_fn
)generate_data
},
6248 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6249 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6250 {0, 0, (test_callback_fn
)0}
6253 test_st consistent_tests
[] ={
6254 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6255 {"generate_data", 1, (test_callback_fn
)generate_data
},
6256 {"get_read", 0, (test_callback_fn
)get_read_count
},
6257 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6258 {0, 0, (test_callback_fn
)0}
6261 test_st consistent_weighted_tests
[] ={
6262 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6263 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6264 {"get_read", 0, (test_callback_fn
)get_read_count
},
6265 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6266 {0, 0, (test_callback_fn
)0}
6269 test_st hsieh_availability
[] ={
6270 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6271 {0, 0, (test_callback_fn
)0}
6274 test_st murmur_availability
[] ={
6275 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6276 {0, 0, (test_callback_fn
)0}
6280 test_st hash_sanity
[] ={
6281 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6282 {0, 0, (test_callback_fn
)0}
6286 test_st ketama_auto_eject_hosts
[] ={
6287 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6288 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6289 {0, 0, (test_callback_fn
)0}
6292 test_st hash_tests
[] ={
6293 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6294 {"md5", 0, (test_callback_fn
)md5_run
},
6295 {"crc", 0, (test_callback_fn
)crc_run
},
6296 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6297 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6298 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6299 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6300 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6301 {"murmur", 0, (test_callback_fn
)murmur_run
},
6302 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6303 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6304 {0, 0, (test_callback_fn
)0}
6307 test_st error_conditions
[] ={
6308 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6309 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6310 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6311 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6312 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6313 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6314 {0, 0, (test_callback_fn
)0}
6318 test_st parser_tests
[] ={
6319 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6320 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6321 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6322 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6323 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6324 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6325 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6326 {"memcached_parse_configure_file", 0, (test_callback_fn
)memcached_parse_configure_file_test
},
6327 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6328 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6329 {"server", 0, (test_callback_fn
)server_test
},
6330 {"servers", 0, (test_callback_fn
)servers_test
},
6331 {0, 0, (test_callback_fn
)0}
6334 collection_st collection
[] ={
6336 {"hash_sanity", 0, 0, hash_sanity
},
6338 {"basic", 0, 0, basic_tests
},
6339 {"hsieh_availability", 0, 0, hsieh_availability
},
6340 {"murmur_availability", 0, 0, murmur_availability
},
6341 {"block", 0, 0, tests
},
6342 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6343 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6344 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6345 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6346 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6347 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6348 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6349 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6350 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6351 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6352 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6353 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6354 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6355 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6356 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6357 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6358 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6359 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6360 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6361 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6362 #ifdef MEMCACHED_ENABLE_DEPRECATED
6363 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6365 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6366 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6367 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6368 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6369 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6370 {"string", 0, 0, string_tests
},
6371 {"result", 0, 0, result_tests
},
6372 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6373 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6374 {"user", 0, 0, user_tests
},
6375 {"generate", 0, 0, generate_tests
},
6376 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6377 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6378 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6379 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6380 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6381 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6382 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6384 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6385 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6386 {"consistent_not", 0, 0, consistent_tests
},
6387 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6388 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6389 {"ketama_compat", 0, 0, ketama_compatibility
},
6390 {"test_hashes", 0, 0, hash_tests
},
6391 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6392 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6393 {"regression", 0, 0, regression_tests
},
6394 {"behaviors", 0, 0, behavior_tests
},
6395 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6396 {"error_conditions", 0, 0, error_conditions
},
6397 {"parser", 0, 0, parser_tests
},
6401 #define SERVERS_TO_CREATE 5
6403 #include "libmemcached_world.h"
6405 void get_world(world_st
*world
)
6407 world
->collections
= collection
;
6409 world
->create
= (test_callback_create_fn
)world_create
;
6410 world
->destroy
= (test_callback_fn
)world_destroy
;
6412 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6413 world
->test
.flush
= (test_callback_fn
)world_flush
;
6414 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6415 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6416 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6418 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6419 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6421 world
->runner
= &defualt_libmemcached_runner
;