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,
405 1336171666U, 3365377466U };
407 // You have updated the memcache_error messages but not updated docs/tests.
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
);
421 test_true(MEMCACHED_MAXIMUM_RETURN
== 45);
426 static test_return_t
set_test(memcached_st
*memc
)
428 memcached_return_t rc
;
429 const char *key
= "foo";
430 const char *value
= "when we sanitize";
432 rc
= memcached_set(memc
, key
, strlen(key
),
433 value
, strlen(value
),
434 (time_t)0, (uint32_t)0);
435 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
440 static test_return_t
append_test(memcached_st
*memc
)
442 memcached_return_t rc
;
443 const char *key
= "fig";
444 const char *in_value
= "we";
445 char *out_value
= NULL
;
449 rc
= memcached_flush(memc
, 0);
450 test_true(rc
== MEMCACHED_SUCCESS
);
452 rc
= memcached_set(memc
, key
, strlen(key
),
453 in_value
, strlen(in_value
),
454 (time_t)0, (uint32_t)0);
455 test_true(rc
== MEMCACHED_SUCCESS
);
457 rc
= memcached_append(memc
, key
, strlen(key
),
458 " the", strlen(" the"),
459 (time_t)0, (uint32_t)0);
460 test_true(rc
== MEMCACHED_SUCCESS
);
462 rc
= memcached_append(memc
, key
, strlen(key
),
463 " people", strlen(" people"),
464 (time_t)0, (uint32_t)0);
465 test_true(rc
== MEMCACHED_SUCCESS
);
467 out_value
= memcached_get(memc
, key
, strlen(key
),
468 &value_length
, &flags
, &rc
);
469 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
470 test_true(strlen("we the people") == value_length
);
471 test_true(rc
== MEMCACHED_SUCCESS
);
477 static test_return_t
append_binary_test(memcached_st
*memc
)
479 memcached_return_t rc
;
480 const char *key
= "numbers";
481 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
487 rc
= memcached_flush(memc
, 0);
488 test_true(rc
== MEMCACHED_SUCCESS
);
490 rc
= memcached_set(memc
,
493 (time_t)0, (uint32_t)0);
494 test_true(rc
== MEMCACHED_SUCCESS
);
496 for (x
= 0; store_list
[x
] ; x
++)
498 rc
= memcached_append(memc
,
500 (char *)&store_list
[x
], sizeof(uint32_t),
501 (time_t)0, (uint32_t)0);
502 test_true(rc
== MEMCACHED_SUCCESS
);
505 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
506 &value_length
, &flags
, &rc
);
507 test_true((value_length
== (sizeof(uint32_t) * x
)));
508 test_true(rc
== MEMCACHED_SUCCESS
);
510 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
512 test_true(*ptr
== store_list
[x
- counter
]);
520 static test_return_t
cas2_test(memcached_st
*memc
)
522 memcached_return_t rc
;
523 const char *keys
[]= {"fudge", "son", "food"};
524 size_t key_length
[]= {5, 3, 4};
525 const char *value
= "we the people";
526 size_t value_length
= strlen("we the people");
528 memcached_result_st results_obj
;
529 memcached_result_st
*results
;
532 rc
= memcached_flush(memc
, 0);
533 test_true(rc
== MEMCACHED_SUCCESS
);
535 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
537 for (x
= 0; x
< 3; x
++)
539 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
540 keys
[x
], key_length
[x
],
541 (time_t)50, (uint32_t)9);
542 test_true(rc
== MEMCACHED_SUCCESS
);
545 rc
= memcached_mget(memc
, keys
, key_length
, 3);
547 results
= memcached_result_create(memc
, &results_obj
);
549 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
551 test_true(results
->item_cas
);
552 test_true(rc
== MEMCACHED_SUCCESS
);
553 test_true(memcached_result_cas(results
));
555 test_true(!memcmp(value
, "we the people", strlen("we the people")));
556 test_true(strlen("we the people") == value_length
);
557 test_true(rc
== MEMCACHED_SUCCESS
);
559 memcached_result_free(&results_obj
);
564 static test_return_t
cas_test(memcached_st
*memc
)
566 memcached_return_t rc
;
567 const char *key
= "fun";
568 size_t key_length
= strlen(key
);
569 const char *value
= "we the people";
570 const char* keys
[2] = { key
, NULL
};
571 size_t keylengths
[2] = { strlen(key
), 0 };
572 size_t value_length
= strlen(value
);
573 const char *value2
= "change the value";
574 size_t value2_length
= strlen(value2
);
576 memcached_result_st results_obj
;
577 memcached_result_st
*results
;
580 rc
= memcached_flush(memc
, 0);
581 test_true(rc
== MEMCACHED_SUCCESS
);
583 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
585 rc
= memcached_set(memc
, key
, strlen(key
),
586 value
, strlen(value
),
587 (time_t)0, (uint32_t)0);
588 test_true(rc
== MEMCACHED_SUCCESS
);
590 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
592 results
= memcached_result_create(memc
, &results_obj
);
594 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
596 test_true(rc
== MEMCACHED_SUCCESS
);
597 test_true(memcached_result_cas(results
));
598 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
599 test_true(strlen(memcached_result_value(results
)) == value_length
);
600 test_true(rc
== MEMCACHED_SUCCESS
);
601 uint64_t cas
= memcached_result_cas(results
);
604 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
605 test_true(rc
== MEMCACHED_END
);
606 test_true(results
== NULL
);
609 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
610 test_true(rc
== MEMCACHED_SUCCESS
);
613 * The item will have a new cas value, so try to set it again with the old
614 * value. This should fail!
616 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
617 test_true(rc
== MEMCACHED_DATA_EXISTS
);
619 memcached_result_free(&results_obj
);
624 static test_return_t
prepend_test(memcached_st
*memc
)
626 memcached_return_t rc
;
627 const char *key
= "fig";
628 const char *value
= "people";
629 char *out_value
= NULL
;
633 rc
= memcached_flush(memc
, 0);
634 test_true(rc
== MEMCACHED_SUCCESS
);
636 rc
= memcached_set(memc
, key
, strlen(key
),
637 value
, strlen(value
),
638 (time_t)0, (uint32_t)0);
639 test_true(rc
== MEMCACHED_SUCCESS
);
641 rc
= memcached_prepend(memc
, key
, strlen(key
),
642 "the ", strlen("the "),
643 (time_t)0, (uint32_t)0);
644 test_true(rc
== MEMCACHED_SUCCESS
);
646 rc
= memcached_prepend(memc
, key
, strlen(key
),
647 "we ", strlen("we "),
648 (time_t)0, (uint32_t)0);
649 test_true(rc
== MEMCACHED_SUCCESS
);
651 out_value
= memcached_get(memc
, key
, strlen(key
),
652 &value_length
, &flags
, &rc
);
653 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
654 test_true(strlen("we the people") == value_length
);
655 test_true(rc
== MEMCACHED_SUCCESS
);
662 Set the value, then quit to make sure it is flushed.
663 Come back in and test that add fails.
665 static test_return_t
add_test(memcached_st
*memc
)
667 memcached_return_t rc
;
668 const char *key
= "foo";
669 const char *value
= "when we sanitize";
670 unsigned long long setting_value
;
672 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
674 rc
= memcached_set(memc
, key
, strlen(key
),
675 value
, strlen(value
),
676 (time_t)0, (uint32_t)0);
677 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
678 memcached_quit(memc
);
679 rc
= memcached_add(memc
, key
, strlen(key
),
680 value
, strlen(value
),
681 (time_t)0, (uint32_t)0);
683 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
686 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
690 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
697 ** There was a problem of leaking filedescriptors in the initial release
698 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
699 ** systems it seems that the kernel is slow on reclaiming the resources
700 ** because the connects starts to time out (the test doesn't do much
701 ** anyway, so just loop 10 iterations)
703 static test_return_t
add_wrapper(memcached_st
*memc
)
705 unsigned int max
= 10000;
713 for (uint32_t x
= 0; x
< max
; x
++)
719 static test_return_t
replace_test(memcached_st
*memc
)
721 memcached_return_t rc
;
722 const char *key
= "foo";
723 const char *value
= "when we sanitize";
724 const char *original
= "first we insert some data";
726 rc
= memcached_set(memc
, key
, strlen(key
),
727 original
, strlen(original
),
728 (time_t)0, (uint32_t)0);
729 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
731 rc
= memcached_replace(memc
, key
, strlen(key
),
732 value
, strlen(value
),
733 (time_t)0, (uint32_t)0);
734 test_true(rc
== MEMCACHED_SUCCESS
);
739 static test_return_t
delete_test(memcached_st
*memc
)
741 memcached_return_t rc
;
742 const char *key
= "foo";
743 const char *value
= "when we sanitize";
745 rc
= memcached_set(memc
, key
, strlen(key
),
746 value
, strlen(value
),
747 (time_t)0, (uint32_t)0);
748 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
750 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
751 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
756 static test_return_t
flush_test(memcached_st
*memc
)
758 memcached_return_t rc
;
760 rc
= memcached_flush(memc
, 0);
761 test_true(rc
== MEMCACHED_SUCCESS
);
766 static memcached_return_t
server_function(const memcached_st
*ptr
,
767 const memcached_server_st
*server
,
770 (void)ptr
; (void)server
; (void)context
;
773 return MEMCACHED_SUCCESS
;
776 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
779 strncpy(context
, "foo bad", sizeof(context
));
780 memcached_server_fn callbacks
[1];
782 callbacks
[0]= server_function
;
783 memcached_server_cursor(memc
, callbacks
, context
, 1);
787 static test_return_t
bad_key_test(memcached_st
*memc
)
789 memcached_return_t rc
;
790 const char *key
= "foo bad";
792 size_t string_length
;
794 memcached_st
*memc_clone
;
796 size_t max_keylen
= 0xffff;
798 // Just skip if we are in binary mode.
799 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
802 memc_clone
= memcached_clone(NULL
, memc
);
803 test_true(memc_clone
);
805 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
806 test_true(rc
== MEMCACHED_SUCCESS
);
808 /* All keys are valid in the binary protocol (except for length) */
809 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
811 string
= memcached_get(memc_clone
, key
, strlen(key
),
812 &string_length
, &flags
, &rc
);
813 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
814 test_true(string_length
== 0);
818 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
819 test_true(rc
== MEMCACHED_SUCCESS
);
820 string
= memcached_get(memc_clone
, key
, strlen(key
),
821 &string_length
, &flags
, &rc
);
822 test_true(rc
== MEMCACHED_NOTFOUND
);
823 test_true(string_length
== 0);
826 /* Test multi key for bad keys */
827 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
828 size_t key_lengths
[] = { 7, 7, 7 };
830 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
831 test_true(rc
== MEMCACHED_SUCCESS
);
833 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
834 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
836 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
837 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
841 /* The following test should be moved to the end of this function when the
842 memcached server is updated to allow max size length of the keys in the
845 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
846 test_true(rc
== MEMCACHED_SUCCESS
);
848 char *longkey
= (char *)malloc(max_keylen
+ 1);
851 memset(longkey
, 'a', max_keylen
+ 1);
852 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
853 &string_length
, &flags
, &rc
);
854 test_true(rc
== MEMCACHED_NOTFOUND
);
855 test_true(string_length
== 0);
858 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
859 &string_length
, &flags
, &rc
);
860 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
861 test_true(string_length
== 0);
868 /* Make sure zero length keys are marked as bad */
870 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
871 test_true(rc
== MEMCACHED_SUCCESS
);
872 string
= memcached_get(memc_clone
, key
, 0,
873 &string_length
, &flags
, &rc
);
874 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
875 test_true(string_length
== 0);
878 memcached_free(memc_clone
);
883 #define READ_THROUGH_VALUE "set for me"
884 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
887 memcached_result_st
*result
)
889 (void)memc
;(void)key
;(void)key_length
;
890 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
893 static test_return_t
read_through(memcached_st
*memc
)
895 memcached_return_t rc
;
896 const char *key
= "foo";
898 size_t string_length
;
900 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
902 string
= memcached_get(memc
, key
, strlen(key
),
903 &string_length
, &flags
, &rc
);
905 test_true(rc
== MEMCACHED_NOTFOUND
);
906 test_false(string_length
);
909 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
911 test_true(rc
== MEMCACHED_SUCCESS
);
913 string
= memcached_get(memc
, key
, strlen(key
),
914 &string_length
, &flags
, &rc
);
916 test_true(rc
== MEMCACHED_SUCCESS
);
917 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
918 test_strcmp(READ_THROUGH_VALUE
, string
);
921 string
= memcached_get(memc
, key
, strlen(key
),
922 &string_length
, &flags
, &rc
);
924 test_true(rc
== MEMCACHED_SUCCESS
);
925 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
926 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
932 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
936 (void)ptr
;(void)key_length
;
939 return MEMCACHED_SUCCESS
;
942 static test_return_t
delete_through(memcached_st
*memc
)
944 memcached_trigger_delete_key_fn callback
;
945 memcached_return_t rc
;
947 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
949 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
950 test_true(rc
== MEMCACHED_SUCCESS
);
955 static test_return_t
get_test(memcached_st
*memc
)
957 memcached_return_t rc
;
958 const char *key
= "foo";
960 size_t string_length
;
963 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
964 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
966 string
= memcached_get(memc
, key
, strlen(key
),
967 &string_length
, &flags
, &rc
);
969 test_true(rc
== MEMCACHED_NOTFOUND
);
970 test_false(string_length
);
976 static test_return_t
get_test2(memcached_st
*memc
)
978 memcached_return_t rc
;
979 const char *key
= "foo";
980 const char *value
= "when we sanitize";
982 size_t string_length
;
985 rc
= memcached_set(memc
, key
, strlen(key
),
986 value
, strlen(value
),
987 (time_t)0, (uint32_t)0);
988 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
990 string
= memcached_get(memc
, key
, strlen(key
),
991 &string_length
, &flags
, &rc
);
994 test_true(rc
== MEMCACHED_SUCCESS
);
995 test_true(string_length
== strlen(value
));
996 test_true(!memcmp(string
, value
, string_length
));
1000 return TEST_SUCCESS
;
1003 static test_return_t
set_test2(memcached_st
*memc
)
1005 memcached_return_t rc
;
1006 const char *key
= "foo";
1007 const char *value
= "train in the brain";
1008 size_t value_length
= strlen(value
);
1011 for (x
= 0; x
< 10; x
++)
1013 rc
= memcached_set(memc
, key
, strlen(key
),
1014 value
, value_length
,
1015 (time_t)0, (uint32_t)0);
1016 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1019 return TEST_SUCCESS
;
1022 static test_return_t
set_test3(memcached_st
*memc
)
1024 memcached_return_t rc
;
1026 size_t value_length
= 8191;
1029 value
= (char*)malloc(value_length
);
1032 for (x
= 0; x
< value_length
; x
++)
1033 value
[x
] = (char) (x
% 127);
1035 /* The dump test relies on there being at least 32 items in memcached */
1036 for (x
= 0; x
< 32; x
++)
1040 snprintf(key
, sizeof(key
), "foo%u", x
);
1042 rc
= memcached_set(memc
, key
, strlen(key
),
1043 value
, value_length
,
1044 (time_t)0, (uint32_t)0);
1045 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1050 return TEST_SUCCESS
;
1053 static test_return_t
get_test3(memcached_st
*memc
)
1055 memcached_return_t rc
;
1056 const char *key
= "foo";
1058 size_t value_length
= 8191;
1060 size_t string_length
;
1064 value
= (char*)malloc(value_length
);
1067 for (x
= 0; x
< value_length
; x
++)
1068 value
[x
] = (char) (x
% 127);
1070 rc
= memcached_set(memc
, key
, strlen(key
),
1071 value
, value_length
,
1072 (time_t)0, (uint32_t)0);
1073 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1075 string
= memcached_get(memc
, key
, strlen(key
),
1076 &string_length
, &flags
, &rc
);
1078 test_true(rc
== MEMCACHED_SUCCESS
);
1080 test_true(string_length
== value_length
);
1081 test_true(!memcmp(string
, value
, string_length
));
1086 return TEST_SUCCESS
;
1089 static test_return_t
get_test4(memcached_st
*memc
)
1091 memcached_return_t rc
;
1092 const char *key
= "foo";
1094 size_t value_length
= 8191;
1096 size_t string_length
;
1100 value
= (char*)malloc(value_length
);
1103 for (x
= 0; x
< value_length
; x
++)
1104 value
[x
] = (char) (x
% 127);
1106 rc
= memcached_set(memc
, key
, strlen(key
),
1107 value
, value_length
,
1108 (time_t)0, (uint32_t)0);
1109 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1111 for (x
= 0; x
< 10; x
++)
1113 string
= memcached_get(memc
, key
, strlen(key
),
1114 &string_length
, &flags
, &rc
);
1116 test_true(rc
== MEMCACHED_SUCCESS
);
1118 test_true(string_length
== value_length
);
1119 test_true(!memcmp(string
, value
, string_length
));
1125 return TEST_SUCCESS
;
1129 * This test verifies that memcached_read_one_response doesn't try to
1130 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1131 * responses before you execute a storage command.
1133 static test_return_t
get_test5(memcached_st
*memc
)
1136 ** Request the same key twice, to ensure that we hash to the same server
1137 ** (so that we have multiple response values queued up) ;-)
1139 const char *keys
[]= { "key", "key" };
1140 size_t lengths
[]= { 3, 3 };
1144 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1145 keys
[0], lengths
[0], 0, 0);
1146 test_true(rc
== MEMCACHED_SUCCESS
);
1147 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1149 memcached_result_st results_obj
;
1150 memcached_result_st
*results
;
1151 results
=memcached_result_create(memc
, &results_obj
);
1153 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1155 memcached_result_free(&results_obj
);
1157 /* Don't read out the second result, but issue a set instead.. */
1158 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1159 test_true(rc
== MEMCACHED_SUCCESS
);
1161 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1162 &rlen
, &flags
, &rc
);
1163 test_true(val
== NULL
);
1164 test_true(rc
== MEMCACHED_NOTFOUND
);
1165 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1166 test_true(val
!= NULL
);
1167 test_true(rc
== MEMCACHED_SUCCESS
);
1170 return TEST_SUCCESS
;
1173 static test_return_t
mget_end(memcached_st
*memc
)
1175 const char *keys
[]= { "foo", "foo2" };
1176 size_t lengths
[]= { 3, 4 };
1177 const char *values
[]= { "fjord", "41" };
1179 memcached_return_t rc
;
1182 for (int i
= 0; i
< 2; i
++)
1184 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1185 (time_t)0, (uint32_t)0);
1186 test_true(rc
== MEMCACHED_SUCCESS
);
1190 size_t string_length
;
1193 // retrieve both via mget
1194 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1195 test_true(rc
== MEMCACHED_SUCCESS
);
1197 char key
[MEMCACHED_MAX_KEY
];
1200 // this should get both
1201 for (int i
= 0; i
< 2; i
++)
1203 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1205 test_true(rc
== MEMCACHED_SUCCESS
);
1207 if (key_length
== 4)
1209 test_true(string_length
== strlen(values
[val
]));
1210 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1214 // this should indicate end
1215 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1216 test_true(rc
== MEMCACHED_END
);
1219 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1220 test_true(rc
== MEMCACHED_SUCCESS
);
1222 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1223 test_true(key_length
== lengths
[0]);
1224 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1225 test_true(string_length
== strlen(values
[0]));
1226 test_true(strncmp(values
[0], string
, string_length
) == 0);
1227 test_true(rc
== MEMCACHED_SUCCESS
);
1230 // this should indicate end
1231 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1232 test_true(rc
== MEMCACHED_END
);
1234 return TEST_SUCCESS
;
1237 /* Do not copy the style of this code, I just access hosts to testthis function */
1238 static test_return_t
stats_servername_test(memcached_st
*memc
)
1240 memcached_return_t rc
;
1241 memcached_stat_st memc_stat
;
1242 memcached_server_instance_st instance
=
1243 memcached_server_instance_by_position(memc
, 0);
1245 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1246 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1247 return TEST_SKIPPED
;
1249 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1250 memcached_server_name(instance
),
1251 memcached_server_port(instance
));
1253 return TEST_SUCCESS
;
1256 static test_return_t
increment_test(memcached_st
*memc
)
1258 uint64_t new_number
;
1259 memcached_return_t rc
;
1260 const char *key
= "number";
1261 const char *value
= "0";
1263 rc
= memcached_set(memc
, key
, strlen(key
),
1264 value
, strlen(value
),
1265 (time_t)0, (uint32_t)0);
1266 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1268 rc
= memcached_increment(memc
, key
, strlen(key
),
1270 test_true(rc
== MEMCACHED_SUCCESS
);
1271 test_true(new_number
== 1);
1273 rc
= memcached_increment(memc
, key
, strlen(key
),
1275 test_true(rc
== MEMCACHED_SUCCESS
);
1276 test_true(new_number
== 2);
1278 return TEST_SUCCESS
;
1281 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1283 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1285 uint64_t new_number
;
1286 memcached_return_t rc
;
1287 const char *key
= "number";
1288 uint64_t initial
= 0;
1290 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1291 1, initial
, 0, &new_number
);
1292 test_true(rc
== MEMCACHED_SUCCESS
);
1293 test_true(new_number
== initial
);
1295 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1296 1, initial
, 0, &new_number
);
1297 test_true(rc
== MEMCACHED_SUCCESS
);
1298 test_true(new_number
== (initial
+ 1));
1300 return TEST_SUCCESS
;
1303 static test_return_t
decrement_test(memcached_st
*memc
)
1305 uint64_t new_number
;
1306 memcached_return_t rc
;
1307 const char *key
= "number";
1308 const char *value
= "3";
1310 rc
= memcached_set(memc
, key
, strlen(key
),
1311 value
, strlen(value
),
1312 (time_t)0, (uint32_t)0);
1313 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1315 rc
= memcached_decrement(memc
, key
, strlen(key
),
1317 test_true(rc
== MEMCACHED_SUCCESS
);
1318 test_true(new_number
== 2);
1320 rc
= memcached_decrement(memc
, key
, strlen(key
),
1322 test_true(rc
== MEMCACHED_SUCCESS
);
1323 test_true(new_number
== 1);
1325 return TEST_SUCCESS
;
1328 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1330 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1332 uint64_t new_number
;
1333 memcached_return_t rc
;
1334 const char *key
= "number";
1335 uint64_t initial
= 3;
1337 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1338 1, initial
, 0, &new_number
);
1339 test_true(rc
== MEMCACHED_SUCCESS
);
1340 test_true(new_number
== initial
);
1342 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1343 1, initial
, 0, &new_number
);
1344 test_true(rc
== MEMCACHED_SUCCESS
);
1345 test_true(new_number
== (initial
- 1));
1347 return TEST_SUCCESS
;
1350 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1352 uint64_t new_number
;
1353 memcached_return_t rc
;
1354 const char *master_key
= "foo";
1355 const char *key
= "number";
1356 const char *value
= "0";
1358 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1360 value
, strlen(value
),
1361 (time_t)0, (uint32_t)0);
1362 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1364 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1366 test_true(rc
== MEMCACHED_SUCCESS
);
1367 test_true(new_number
== 1);
1369 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1371 test_true(rc
== MEMCACHED_SUCCESS
);
1372 test_true(new_number
== 2);
1374 return TEST_SUCCESS
;
1377 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1379 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1381 uint64_t new_number
;
1382 memcached_return_t rc
;
1383 const char *master_key
= "foo";
1384 const char *key
= "number";
1385 uint64_t initial
= 0;
1387 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1389 1, initial
, 0, &new_number
);
1390 test_true(rc
== MEMCACHED_SUCCESS
);
1391 test_true(new_number
== initial
);
1393 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1395 1, initial
, 0, &new_number
);
1396 test_true(rc
== MEMCACHED_SUCCESS
);
1397 test_true(new_number
== (initial
+ 1));
1399 return TEST_SUCCESS
;
1402 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1404 uint64_t new_number
;
1405 memcached_return_t rc
;
1406 const char *master_key
= "foo";
1407 const char *key
= "number";
1408 const char *value
= "3";
1410 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1412 value
, strlen(value
),
1413 (time_t)0, (uint32_t)0);
1414 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1416 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1419 test_true(rc
== MEMCACHED_SUCCESS
);
1420 test_true(new_number
== 2);
1422 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1425 test_true(rc
== MEMCACHED_SUCCESS
);
1426 test_true(new_number
== 1);
1428 return TEST_SUCCESS
;
1431 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1433 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1435 uint64_t new_number
;
1436 memcached_return_t rc
;
1437 const char *master_key
= "foo";
1438 const char *key
= "number";
1439 uint64_t initial
= 3;
1441 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1443 1, initial
, 0, &new_number
);
1444 test_true(rc
== MEMCACHED_SUCCESS
);
1445 test_true(new_number
== initial
);
1447 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1449 1, initial
, 0, &new_number
);
1450 test_true(rc
== MEMCACHED_SUCCESS
);
1451 test_true(new_number
== (initial
- 1));
1453 return TEST_SUCCESS
;
1456 static test_return_t
quit_test(memcached_st
*memc
)
1458 memcached_return_t rc
;
1459 const char *key
= "fudge";
1460 const char *value
= "sanford and sun";
1462 rc
= memcached_set(memc
, key
, strlen(key
),
1463 value
, strlen(value
),
1464 (time_t)10, (uint32_t)3);
1465 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1466 memcached_quit(memc
);
1468 rc
= memcached_set(memc
, key
, strlen(key
),
1469 value
, strlen(value
),
1470 (time_t)50, (uint32_t)9);
1471 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1473 return TEST_SUCCESS
;
1476 static test_return_t
mget_result_test(memcached_st
*memc
)
1478 memcached_return_t rc
;
1479 const char *keys
[]= {"fudge", "son", "food"};
1480 size_t key_length
[]= {5, 3, 4};
1483 memcached_result_st results_obj
;
1484 memcached_result_st
*results
;
1486 results
= memcached_result_create(memc
, &results_obj
);
1488 test_true(&results_obj
== results
);
1490 /* We need to empty the server before continueing test */
1491 rc
= memcached_flush(memc
, 0);
1492 test_true(rc
== MEMCACHED_SUCCESS
);
1494 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1495 test_true(rc
== MEMCACHED_SUCCESS
);
1497 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1502 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1503 test_true(!results
);
1504 test_true(rc
== MEMCACHED_END
);
1506 for (x
= 0; x
< 3; x
++)
1508 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1509 keys
[x
], key_length
[x
],
1510 (time_t)50, (uint32_t)9);
1511 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1514 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1515 test_true(rc
== MEMCACHED_SUCCESS
);
1517 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1520 test_true(&results_obj
== results
);
1521 test_true(rc
== MEMCACHED_SUCCESS
);
1522 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1523 test_true(!memcmp(memcached_result_key_value(results
),
1524 memcached_result_value(results
),
1525 memcached_result_length(results
)));
1528 memcached_result_free(&results_obj
);
1530 return TEST_SUCCESS
;
1533 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1535 memcached_return_t rc
;
1536 const char *keys
[]= {"fudge", "son", "food"};
1537 size_t key_length
[]= {5, 3, 4};
1540 memcached_result_st
*results
;
1542 /* We need to empty the server before continueing test */
1543 rc
= memcached_flush(memc
, 0);
1544 test_true(rc
== MEMCACHED_SUCCESS
);
1546 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1547 test_true(rc
== MEMCACHED_SUCCESS
);
1549 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1553 test_true(!results
);
1554 test_true(rc
== MEMCACHED_END
);
1556 for (x
= 0; x
< 3; x
++)
1558 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1559 keys
[x
], key_length
[x
],
1560 (time_t)50, (uint32_t)9);
1561 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1564 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1565 test_true(rc
== MEMCACHED_SUCCESS
);
1568 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1571 test_true(rc
== MEMCACHED_SUCCESS
);
1572 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1573 test_true(!memcmp(memcached_result_key_value(results
),
1574 memcached_result_value(results
),
1575 memcached_result_length(results
)));
1576 memcached_result_free(results
);
1580 return TEST_SUCCESS
;
1583 /* Count the results */
1584 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1585 memcached_result_st
*result
,
1588 (void)ptr
; (void)result
;
1589 size_t *counter
= (size_t *)context
;
1591 *counter
= *counter
+ 1;
1593 return MEMCACHED_SUCCESS
;
1596 static test_return_t
mget_result_function(memcached_st
*memc
)
1598 memcached_return_t rc
;
1599 const char *keys
[]= {"fudge", "son", "food"};
1600 size_t key_length
[]= {5, 3, 4};
1603 memcached_execute_fn callbacks
[1];
1605 /* We need to empty the server before continueing test */
1606 rc
= memcached_flush(memc
, 0);
1607 for (x
= 0; x
< 3; x
++)
1609 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1610 keys
[x
], key_length
[x
],
1611 (time_t)50, (uint32_t)9);
1612 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1615 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1616 test_true(rc
== MEMCACHED_SUCCESS
);
1618 callbacks
[0]= &callback_counter
;
1620 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1622 test_true(counter
== 3);
1624 return TEST_SUCCESS
;
1627 static test_return_t
mget_test(memcached_st
*memc
)
1629 memcached_return_t rc
;
1630 const char *keys
[]= {"fudge", "son", "food"};
1631 size_t key_length
[]= {5, 3, 4};
1635 char return_key
[MEMCACHED_MAX_KEY
];
1636 size_t return_key_length
;
1638 size_t return_value_length
;
1640 /* We need to empty the server before continueing test */
1641 rc
= memcached_flush(memc
, 0);
1642 test_true(rc
== MEMCACHED_SUCCESS
);
1644 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1645 test_true(rc
== MEMCACHED_SUCCESS
);
1647 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1648 &return_value_length
, &flags
, &rc
)) != NULL
)
1650 test_true(return_value
);
1652 test_true(!return_value
);
1653 test_true(return_value_length
== 0);
1654 test_true(rc
== MEMCACHED_END
);
1656 for (x
= 0; x
< 3; x
++)
1658 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1659 keys
[x
], key_length
[x
],
1660 (time_t)50, (uint32_t)9);
1661 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1664 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1665 test_true(rc
== MEMCACHED_SUCCESS
);
1668 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1669 &return_value_length
, &flags
, &rc
)))
1671 test_true(return_value
);
1672 test_true(rc
== MEMCACHED_SUCCESS
);
1673 test_true(return_key_length
== return_value_length
);
1674 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1679 return TEST_SUCCESS
;
1682 static test_return_t
mget_execute(memcached_st
*memc
)
1686 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1690 * I only want to hit _one_ server so I know the number of requests I'm
1691 * sending in the pipeline.
1693 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1694 memc
->number_of_hosts
= 1;
1696 size_t max_keys
= 20480;
1699 char **keys
= calloc(max_keys
, sizeof(char*));
1700 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1702 /* First add all of the items.. */
1703 char blob
[1024] = {0};
1704 memcached_return_t rc
;
1706 for (size_t x
= 0; x
< max_keys
; ++x
)
1710 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1712 test_true(keys
[x
] != NULL
);
1713 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1714 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1717 /* Try to get all of them with a large multiget */
1719 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1720 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1721 max_keys
, callbacks
, &counter
, 1);
1723 if (rc
== MEMCACHED_SUCCESS
)
1726 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1727 test_true(rc
== MEMCACHED_END
);
1729 /* Verify that we got all of the items */
1730 test_true(counter
== max_keys
);
1732 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1734 test_true(counter
== 0);
1738 test_fail("note: this test functions differently when in binary mode");
1741 /* Release all allocated resources */
1742 for (size_t x
= 0; x
< max_keys
; ++x
)
1749 memc
->number_of_hosts
= number_of_hosts
;
1750 return TEST_SUCCESS
;
1753 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1755 static test_return_t
key_setup(memcached_st
*memc
)
1759 if (pre_binary(memc
) != TEST_SUCCESS
)
1760 return TEST_SKIPPED
;
1762 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1764 return TEST_SUCCESS
;
1767 static test_return_t
key_teardown(memcached_st
*memc
)
1770 pairs_free(global_pairs
);
1772 return TEST_SUCCESS
;
1775 static test_return_t
block_add_regression(memcached_st
*memc
)
1777 /* First add all of the items.. */
1778 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1780 memcached_return_t rc
;
1781 char blob
[1024] = {0};
1783 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1784 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1787 return TEST_SUCCESS
;
1790 static test_return_t
binary_add_regression(memcached_st
*memc
)
1792 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1793 test_return_t rc
= block_add_regression(memc
);
1794 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1798 static test_return_t
get_stats_keys(memcached_st
*memc
)
1802 memcached_stat_st memc_stat
;
1803 memcached_return_t rc
;
1805 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1806 test_true(rc
== MEMCACHED_SUCCESS
);
1807 for (ptr
= stat_list
; *ptr
; ptr
++)
1812 return TEST_SUCCESS
;
1815 static test_return_t
version_string_test(memcached_st
*memc
)
1817 const char *version_string
;
1820 version_string
= memcached_lib_version();
1822 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1824 return TEST_SUCCESS
;
1827 static test_return_t
get_stats(memcached_st
*memc
)
1831 memcached_return_t rc
;
1832 memcached_stat_st
*memc_stat
;
1834 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1835 test_true(rc
== MEMCACHED_SUCCESS
);
1837 test_true(rc
== MEMCACHED_SUCCESS
);
1838 test_true(memc_stat
);
1840 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1842 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1843 test_true(rc
== MEMCACHED_SUCCESS
);
1844 for (ptr
= stat_list
; *ptr
; ptr
++);
1849 memcached_stat_free(NULL
, memc_stat
);
1851 return TEST_SUCCESS
;
1854 static test_return_t
add_host_test(memcached_st
*memc
)
1857 memcached_server_st
*servers
;
1858 memcached_return_t rc
;
1859 char servername
[]= "0.example.com";
1861 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1863 test_true(1 == memcached_server_list_count(servers
));
1865 for (x
= 2; x
< 20; x
++)
1867 char buffer
[SMALL_STRING_LEN
];
1869 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1870 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1872 test_true(rc
== MEMCACHED_SUCCESS
);
1873 test_true(x
== memcached_server_list_count(servers
));
1876 rc
= memcached_server_push(memc
, servers
);
1877 test_true(rc
== MEMCACHED_SUCCESS
);
1878 rc
= memcached_server_push(memc
, servers
);
1879 test_true(rc
== MEMCACHED_SUCCESS
);
1881 memcached_server_list_free(servers
);
1883 return TEST_SUCCESS
;
1886 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1888 (void)parent
;(void)memc_clone
;
1889 return MEMCACHED_SUCCESS
;
1892 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1895 return MEMCACHED_SUCCESS
;
1898 static test_return_t
callback_test(memcached_st
*memc
)
1900 /* Test User Data */
1904 memcached_return_t rc
;
1906 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1907 test_true(rc
== MEMCACHED_SUCCESS
);
1908 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1909 test_true(*test_ptr
== x
);
1912 /* Test Clone Callback */
1914 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1915 void *clone_cb_ptr
= *(void **)&clone_cb
;
1916 void *temp_function
= NULL
;
1917 memcached_return_t rc
;
1919 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1921 test_true(rc
== MEMCACHED_SUCCESS
);
1922 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1923 test_true(temp_function
== clone_cb_ptr
);
1926 /* Test Cleanup Callback */
1928 memcached_cleanup_fn cleanup_cb
=
1929 (memcached_cleanup_fn
)cleanup_test_callback
;
1930 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1931 void *temp_function
= NULL
;
1932 memcached_return_t rc
;
1934 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1936 test_true(rc
== MEMCACHED_SUCCESS
);
1937 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1938 test_true(temp_function
== cleanup_cb_ptr
);
1941 return TEST_SUCCESS
;
1944 /* We don't test the behavior itself, we test the switches */
1945 static test_return_t
behavior_test(memcached_st
*memc
)
1950 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1951 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1952 test_true(value
== 1);
1954 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1955 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1956 test_true(value
== 1);
1958 set
= MEMCACHED_HASH_MD5
;
1959 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1960 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1961 test_true(value
== MEMCACHED_HASH_MD5
);
1965 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1966 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1967 test_true(value
== 0);
1969 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1970 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1971 test_true(value
== 0);
1973 set
= MEMCACHED_HASH_DEFAULT
;
1974 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1975 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1976 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1978 set
= MEMCACHED_HASH_CRC
;
1979 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1980 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1981 test_true(value
== MEMCACHED_HASH_CRC
);
1983 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1984 test_true(value
> 0);
1986 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1987 test_true(value
> 0);
1989 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1990 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1991 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1993 return TEST_SUCCESS
;
1996 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1998 memcached_return_t rc
;
2002 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2003 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2005 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2007 if (rc
== MEMCACHED_SUCCESS
)
2009 test_true((bool)value
== set
);
2013 test_false((bool)value
== set
);
2016 return TEST_SUCCESS
;
2020 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2022 memcached_return_t rc
;
2026 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2027 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2029 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2031 if (rc
== MEMCACHED_SUCCESS
)
2033 test_true((bool)value
== set
);
2037 test_false((bool)value
== set
);
2040 return TEST_SUCCESS
;
2044 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2046 memcached_return_t rc
;
2050 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2051 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2053 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2055 if (rc
== MEMCACHED_SUCCESS
)
2057 test_true((bool)value
== set
);
2061 test_false((bool)value
== set
);
2064 return TEST_SUCCESS
;
2067 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2069 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2070 char return_key
[MEMCACHED_MAX_KEY
];
2071 size_t return_key_length
;
2073 size_t return_value_length
;
2078 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2079 &return_value_length
, &flags
, &rc
)))
2081 test_true(return_value
);
2082 test_true(rc
== MEMCACHED_SUCCESS
);
2084 *keys_returned
= *keys_returned
+1;
2087 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2089 return TEST_SUCCESS
;
2092 /* Test case provided by Cal Haldenbrand */
2093 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2095 unsigned int setter
= 1;
2097 unsigned long long total
= 0;
2100 char randomstuff
[6 * 1024];
2101 memcached_return_t rc
;
2103 memset(randomstuff
, 0, 6 * 1024);
2105 /* We just keep looking at the same values over and over */
2108 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2109 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2113 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2117 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2118 memset(randomstuff
, 0, 6 * 1024);
2119 test_true(size
< 6 * 1024); /* Being safe here */
2121 for (j
= 0 ; j
< size
;j
++)
2122 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2125 snprintf(key
, sizeof(key
), "%u", x
);
2126 rc
= memcached_set(memc
, key
, strlen(key
),
2127 randomstuff
, strlen(randomstuff
), 10, 0);
2128 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2129 /* If we fail, lets try again */
2130 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2131 rc
= memcached_set(memc
, key
, strlen(key
),
2132 randomstuff
, strlen(randomstuff
), 10, 0);
2133 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2136 return TEST_SUCCESS
;
2139 /* Test case provided by Cal Haldenbrand */
2140 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2142 unsigned int setter
;
2146 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2147 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2149 setter
= 20 * 1024576;
2150 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2151 setter
= 20 * 1024576;
2152 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2153 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2154 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2156 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2159 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2161 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2162 char buffer
[SMALL_STRING_LEN
];
2167 memset(buffer
, 0, SMALL_STRING_LEN
);
2169 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2170 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2171 &val_len
, &flags
, &rc
);
2172 if (rc
!= MEMCACHED_SUCCESS
)
2174 if (rc
== MEMCACHED_NOTFOUND
)
2188 return TEST_SUCCESS
;
2191 /* Do a large mget() over all the keys we think exist */
2192 #define KEY_COUNT 3000 // * 1024576
2193 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2195 memcached_return_t rc
;
2196 unsigned int setter
;
2199 size_t key_lengths
[KEY_COUNT
];
2202 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2203 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2205 setter
= 20 * 1024576;
2206 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2207 setter
= 20 * 1024576;
2208 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2209 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2210 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2213 keys
= calloc(KEY_COUNT
, sizeof(char *));
2215 for (x
= 0; x
< KEY_COUNT
; x
++)
2219 snprintf(buffer
, 30, "%u", x
);
2220 keys
[x
]= strdup(buffer
);
2221 key_lengths
[x
]= strlen(keys
[x
]);
2224 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2225 test_true(rc
== MEMCACHED_SUCCESS
);
2227 size_t keys_returned
;
2228 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2230 for (x
= 0; x
< KEY_COUNT
; x
++)
2234 return TEST_SUCCESS
;
2237 /* Make sure we behave properly if server list has no values */
2238 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2240 memcached_return_t rc
;
2241 const char *keys
[]= {"fudge", "son", "food"};
2242 size_t key_length
[]= {5, 3, 4};
2245 char return_key
[MEMCACHED_MAX_KEY
];
2246 size_t return_key_length
;
2248 size_t return_value_length
;
2250 /* Here we free everything before running a bunch of mget tests */
2251 memcached_servers_reset(memc
);
2254 /* We need to empty the server before continueing test */
2255 rc
= memcached_flush(memc
, 0);
2256 test_true(rc
== MEMCACHED_NO_SERVERS
);
2258 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2259 test_true(rc
== MEMCACHED_NO_SERVERS
);
2261 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2262 &return_value_length
, &flags
, &rc
)) != NULL
)
2264 test_true(return_value
);
2266 test_true(!return_value
);
2267 test_true(return_value_length
== 0);
2268 test_true(rc
== MEMCACHED_NO_SERVERS
);
2270 for (x
= 0; x
< 3; x
++)
2272 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2273 keys
[x
], key_length
[x
],
2274 (time_t)50, (uint32_t)9);
2275 test_true(rc
== MEMCACHED_NO_SERVERS
);
2278 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2279 test_true(rc
== MEMCACHED_NO_SERVERS
);
2282 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2283 &return_value_length
, &flags
, &rc
)))
2285 test_true(return_value
);
2286 test_true(rc
== MEMCACHED_SUCCESS
);
2287 test_true(return_key_length
== return_value_length
);
2288 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2293 return TEST_SUCCESS
;
2296 #define VALUE_SIZE_BUG5 1048064
2297 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2299 memcached_return_t rc
;
2300 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2301 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2302 char return_key
[MEMCACHED_MAX_KEY
];
2303 size_t return_key_length
;
2305 size_t value_length
;
2309 char insert_data
[VALUE_SIZE_BUG5
];
2311 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2312 insert_data
[x
]= (signed char)rand();
2314 memcached_flush(memc
, 0);
2315 value
= memcached_get(memc
, keys
[0], key_length
[0],
2316 &value_length
, &flags
, &rc
);
2317 test_true(value
== NULL
);
2318 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2321 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2322 &value_length
, &flags
, &rc
)))
2324 test_true(count
== 0);
2326 for (x
= 0; x
< 4; x
++)
2328 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2329 insert_data
, VALUE_SIZE_BUG5
,
2330 (time_t)0, (uint32_t)0);
2331 test_true(rc
== MEMCACHED_SUCCESS
);
2334 for (x
= 0; x
< 10; x
++)
2336 value
= memcached_get(memc
, keys
[0], key_length
[0],
2337 &value_length
, &flags
, &rc
);
2341 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2343 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2344 &value_length
, &flags
, &rc
)))
2349 test_true(count
== 4);
2352 return TEST_SUCCESS
;
2355 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2357 memcached_return_t rc
;
2358 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2359 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2360 char return_key
[MEMCACHED_MAX_KEY
];
2361 size_t return_key_length
;
2363 size_t value_length
;
2367 char insert_data
[VALUE_SIZE_BUG5
];
2369 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2370 insert_data
[x
]= (signed char)rand();
2372 memcached_flush(memc
, 0);
2373 value
= memcached_get(memc
, keys
[0], key_length
[0],
2374 &value_length
, &flags
, &rc
);
2375 test_true(value
== NULL
);
2376 test_true(rc
== MEMCACHED_NOTFOUND
);
2377 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2378 test_true(rc
== MEMCACHED_SUCCESS
);
2381 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2382 &value_length
, &flags
, &rc
)))
2384 test_true(count
== 0);
2385 test_true(rc
== MEMCACHED_END
);
2387 for (x
= 0; x
< 4; x
++)
2389 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2390 insert_data
, VALUE_SIZE_BUG5
,
2391 (time_t)0, (uint32_t)0);
2392 test_true(rc
== MEMCACHED_SUCCESS
);
2395 for (x
= 0; x
< 2; x
++)
2397 value
= memcached_get(memc
, keys
[0], key_length
[0],
2398 &value_length
, &flags
, &rc
);
2402 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2403 test_true(rc
== MEMCACHED_SUCCESS
);
2405 /* We test for purge of partial complete fetches */
2406 for (count
= 3; count
; count
--)
2408 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2409 &value_length
, &flags
, &rc
);
2410 test_true(rc
== MEMCACHED_SUCCESS
);
2411 test_true(!(memcmp(value
, insert_data
, value_length
)));
2412 test_true(value_length
);
2417 return TEST_SUCCESS
;
2420 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2422 memcached_return_t rc
;
2424 memcached_st
*memc_clone
;
2426 memcached_server_st
*servers
;
2427 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";
2430 servers
= memcached_servers_parse(server_list
);
2433 mine
= memcached_create(NULL
);
2434 rc
= memcached_server_push(mine
, servers
);
2435 test_true(rc
== MEMCACHED_SUCCESS
);
2436 memcached_server_list_free(servers
);
2439 memc_clone
= memcached_clone(NULL
, mine
);
2441 memcached_quit(mine
);
2442 memcached_quit(memc_clone
);
2445 memcached_free(mine
);
2446 memcached_free(memc_clone
);
2448 return TEST_SUCCESS
;
2451 /* Test flag store/retrieve */
2452 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2454 memcached_return_t rc
;
2455 const char *keys
= "036790384900";
2456 size_t key_length
= strlen(keys
);
2457 char return_key
[MEMCACHED_MAX_KEY
];
2458 size_t return_key_length
;
2460 size_t value_length
;
2463 char insert_data
[VALUE_SIZE_BUG5
];
2465 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2466 insert_data
[x
]= (signed char)rand();
2468 memcached_flush(memc
, 0);
2471 rc
= memcached_set(memc
, keys
, key_length
,
2472 insert_data
, VALUE_SIZE_BUG5
,
2474 test_true(rc
== MEMCACHED_SUCCESS
);
2477 value
= memcached_get(memc
, keys
, key_length
,
2478 &value_length
, &flags
, &rc
);
2479 test_true(flags
== 245);
2483 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2486 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2487 &value_length
, &flags
, &rc
);
2488 test_true(flags
== 245);
2493 return TEST_SUCCESS
;
2496 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2498 memcached_return_t rc
;
2499 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2500 size_t key_length
[3];
2505 char return_key
[MEMCACHED_MAX_KEY
];
2506 size_t return_key_length
;
2508 size_t return_value_length
;
2511 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2512 key_length
[1]= strlen("fudge&*@#");
2513 key_length
[2]= strlen("for^#@&$not");
2516 for (x
= 0; x
< 3; x
++)
2518 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2519 keys
[x
], key_length
[x
],
2520 (time_t)50, (uint32_t)9);
2521 test_true(rc
== MEMCACHED_SUCCESS
);
2524 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2525 test_true(rc
== MEMCACHED_SUCCESS
);
2527 /* We need to empty the server before continueing test */
2528 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2529 &return_value_length
, &flags
, &rc
)) != NULL
)
2531 test_true(return_value
);
2535 test_true(count
== 3);
2537 return TEST_SUCCESS
;
2540 /* We are testing with aggressive timeout to get failures */
2541 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2543 const char *key
= "foo";
2545 size_t value_length
= 512;
2548 memcached_return_t rc
;
2549 unsigned int set
= 1;
2550 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2553 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2554 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2556 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2559 value
= (char*)malloc(value_length
* sizeof(char));
2561 for (x
= 0; x
< value_length
; x
++)
2562 value
[x
]= (char) (x
% 127);
2564 for (x
= 1; x
<= 100000; ++x
)
2566 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2568 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2569 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2571 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2576 memcached_free(mclone
);
2578 return TEST_SUCCESS
;
2582 We are looking failures in the async protocol
2584 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2586 const char *key
= "foo";
2588 size_t value_length
= 512;
2591 memcached_return_t rc
;
2592 unsigned int set
= 1;
2594 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2596 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2597 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2599 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2602 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2604 test_true(timeout
== -1);
2606 value
= (char*)malloc(value_length
* sizeof(char));
2608 for (x
= 0; x
< value_length
; x
++)
2609 value
[x
]= (char) (x
% 127);
2611 for (x
= 1; x
<= 100000; ++x
)
2613 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2617 memcached_free(mclone
);
2619 return TEST_SUCCESS
;
2623 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2625 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2627 memcached_return_t rc
;
2629 size_t value_length
;
2631 uint64_t number_value
;
2633 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2634 &value_length
, &flags
, &rc
);
2635 test_true(value
== NULL
);
2636 test_true(rc
== MEMCACHED_NOTFOUND
);
2638 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2641 test_true(value
== NULL
);
2642 /* The binary protocol will set the key if it doesn't exist */
2643 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2645 test_true(rc
== MEMCACHED_SUCCESS
);
2649 test_true(rc
== MEMCACHED_NOTFOUND
);
2652 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2654 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2655 &value_length
, &flags
, &rc
);
2657 test_true(rc
== MEMCACHED_SUCCESS
);
2660 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2662 test_true(number_value
== 2);
2663 test_true(rc
== MEMCACHED_SUCCESS
);
2665 return TEST_SUCCESS
;
2669 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2670 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2672 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2674 char key
[] = "key34567890";
2676 memcached_return_t rc
;
2677 size_t overflowSize
;
2679 char commandFirst
[]= "set key34567890 0 0 ";
2680 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2681 size_t commandLength
;
2684 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2686 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2688 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2690 overflow
= malloc(testSize
);
2691 test_true(overflow
!= NULL
);
2693 memset(overflow
, 'x', testSize
);
2694 rc
= memcached_set(memc
, key
, strlen(key
),
2695 overflow
, testSize
, 0, 0);
2696 test_true(rc
== MEMCACHED_SUCCESS
);
2700 return TEST_SUCCESS
;
2705 Test values of many different sizes
2706 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2707 set key34567890 0 0 8169 \r\n
2708 is sent followed by buffer of size 8169, followed by 8169
2710 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2713 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2714 memcached_return_t rc
;
2715 const char *key
= "foo";
2717 size_t value_length
= 18000;
2719 size_t string_length
;
2722 size_t current_length
;
2724 value
= (char*)malloc(value_length
);
2727 for (x
= 0; x
< value_length
; x
++)
2728 value
[x
] = (char) (x
% 127);
2730 for (current_length
= 0; current_length
< value_length
; current_length
++)
2732 rc
= memcached_set(memc
, key
, strlen(key
),
2733 value
, current_length
,
2734 (time_t)0, (uint32_t)0);
2735 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2737 string
= memcached_get(memc
, key
, strlen(key
),
2738 &string_length
, &flags
, &rc
);
2740 test_true(rc
== MEMCACHED_SUCCESS
);
2741 test_true(string_length
== current_length
);
2742 test_true(!memcmp(string
, value
, string_length
));
2749 return TEST_SUCCESS
;
2753 Look for zero length value problems
2755 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2758 memcached_return_t rc
;
2759 const char *key
= "mykey";
2764 for (x
= 0; x
< 2; x
++)
2766 rc
= memcached_set(memc
, key
, strlen(key
),
2768 (time_t)0, (uint32_t)0);
2770 test_true(rc
== MEMCACHED_SUCCESS
);
2772 value
= memcached_get(memc
, key
, strlen(key
),
2773 &length
, &flags
, &rc
);
2775 test_true(rc
== MEMCACHED_SUCCESS
);
2776 test_true(value
== NULL
);
2777 test_true(length
== 0);
2778 test_true(flags
== 0);
2780 value
= memcached_get(memc
, key
, strlen(key
),
2781 &length
, &flags
, &rc
);
2783 test_true(rc
== MEMCACHED_SUCCESS
);
2784 test_true(value
== NULL
);
2785 test_true(length
== 0);
2786 test_true(flags
== 0);
2789 return TEST_SUCCESS
;
2792 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2793 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2795 memcached_return_t rc
;
2796 const char *key
= "mykey";
2801 rc
= memcached_set(memc
, key
, strlen(key
),
2803 (time_t)0, UINT32_MAX
);
2805 test_true(rc
== MEMCACHED_SUCCESS
);
2807 value
= memcached_get(memc
, key
, strlen(key
),
2808 &length
, &flags
, &rc
);
2810 test_true(rc
== MEMCACHED_SUCCESS
);
2811 test_true(value
== NULL
);
2812 test_true(length
== 0);
2813 test_true(flags
== UINT32_MAX
);
2815 return TEST_SUCCESS
;
2818 #if !defined(__sun) && !defined(__OpenBSD__)
2819 /* Check the validity of chinese key*/
2820 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2822 memcached_return_t rc
;
2823 const char *key
= "豆瓣";
2824 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2829 rc
= memcached_set(memc
, key
, strlen(key
),
2830 value
, strlen(value
),
2833 test_true(rc
== MEMCACHED_SUCCESS
);
2835 value2
= memcached_get(memc
, key
, strlen(key
),
2836 &length
, &flags
, &rc
);
2838 test_true(length
==strlen(value
));
2839 test_true(rc
== MEMCACHED_SUCCESS
);
2840 test_true(memcmp(value
, value2
, length
)==0);
2843 return TEST_SUCCESS
;
2851 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2854 const memcached_server_st
*server
;
2855 memcached_return_t res
;
2859 memc
= memcached_create(NULL
);
2860 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2861 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2863 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2865 memcached_free(memc
);
2867 return TEST_SUCCESS
;
2870 /* CAS test from Andei */
2871 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2873 memcached_return_t status
;
2874 memcached_result_st
*result
, result_obj
;
2875 const char *key
= "abc";
2876 size_t key_len
= strlen("abc");
2877 const char *value
= "foobar";
2878 size_t value_len
= strlen(value
);
2880 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2882 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2883 test_true(status
== MEMCACHED_SUCCESS
);
2885 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2886 test_true(status
== MEMCACHED_SUCCESS
);
2888 result
= memcached_result_create(memc
, &result_obj
);
2891 memcached_result_create(memc
, &result_obj
);
2892 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2895 test_true(status
== MEMCACHED_SUCCESS
);
2897 memcached_result_free(result
);
2899 return TEST_SUCCESS
;
2902 #include "ketama_test_cases.h"
2903 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2905 memcached_return_t rc
;
2908 memcached_server_st
*server_pool
;
2913 memc
= memcached_create(NULL
);
2916 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2917 test_true(rc
== MEMCACHED_SUCCESS
);
2919 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2920 test_true(value
== 1);
2922 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2923 test_true(rc
== MEMCACHED_SUCCESS
);
2925 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2926 test_true(value
== MEMCACHED_HASH_MD5
);
2928 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");
2929 memcached_server_push(memc
, server_pool
);
2931 /* verify that the server list was parsed okay. */
2932 test_true(memcached_server_count(memc
) == 8);
2933 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2934 test_true(server_pool
[0].port
== 11211);
2935 test_true(server_pool
[0].weight
== 600);
2936 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2937 test_true(server_pool
[2].port
== 11211);
2938 test_true(server_pool
[2].weight
== 200);
2939 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2940 test_true(server_pool
[7].port
== 11211);
2941 test_true(server_pool
[7].weight
== 100);
2943 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2944 * us test the boundary wraparound.
2946 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2948 /* verify the standard ketama set. */
2949 for (x
= 0; x
< 99; x
++)
2951 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2953 memcached_server_instance_st instance
=
2954 memcached_server_instance_by_position(memc
, server_idx
);
2956 const char *hostname
= memcached_server_name(instance
);
2957 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2960 memcached_server_list_free(server_pool
);
2961 memcached_free(memc
);
2963 return TEST_SUCCESS
;
2966 /* Large mget() of missing keys with binary proto
2968 * If many binary quiet commands (such as getq's in an mget) fill the output
2969 * buffer and the server chooses not to respond, memcached_flush hangs. See
2970 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2973 /* sighandler_t function that always asserts false */
2974 static void fail(int unused
)
2981 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2986 return TEST_SKIPPED
;
2988 memcached_return_t rc
;
2991 size_t* key_lengths
;
2992 void (*oldalarm
)(int);
2993 memcached_st
*memc_clone
;
2995 memc_clone
= memcached_clone(NULL
, memc
);
2996 test_true(memc_clone
);
2998 /* only binproto uses getq for mget */
2999 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3001 /* empty the cache to ensure misses (hence non-responses) */
3002 rc
= memcached_flush(memc_clone
, 0);
3003 test_true(rc
== MEMCACHED_SUCCESS
);
3005 key_lengths
= calloc(key_count
, sizeof(size_t));
3006 keys
= calloc(key_count
, sizeof(char *));
3008 for (x
= 0; x
< key_count
; x
++)
3012 snprintf(buffer
, 30, "%u", x
);
3013 keys
[x
]= strdup(buffer
);
3014 key_lengths
[x
]= strlen(keys
[x
]);
3017 oldalarm
= signal(SIGALRM
, fail
);
3020 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3021 test_true(rc
== MEMCACHED_SUCCESS
);
3024 signal(SIGALRM
, oldalarm
);
3026 size_t keys_returned
;
3027 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3029 for (x
= 0; x
< key_count
; x
++)
3034 memcached_free(memc_clone
);
3036 return TEST_SUCCESS
;
3040 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3042 test_return_t test_rc
;
3043 test_rc
= pre_binary(memc
);
3045 if (test_rc
!= TEST_SUCCESS
)
3050 /* should work as of r580 */
3051 rc
= _user_supplied_bug21(memc
, 10);
3052 test_true(rc
== TEST_SUCCESS
);
3054 /* should fail as of r580 */
3055 rc
= _user_supplied_bug21(memc
, 1000);
3056 test_true(rc
== TEST_SUCCESS
);
3058 return TEST_SUCCESS
;
3061 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3064 memcached_server_instance_st instance
;
3066 memcached_return_t rc
;
3067 memcached_st
*memc
= memcached_create(NULL
);
3070 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3071 test_true(rc
== MEMCACHED_SUCCESS
);
3073 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3074 test_true(value
== 1);
3076 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3077 test_true(rc
== MEMCACHED_SUCCESS
);
3079 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3080 test_true(value
== MEMCACHED_HASH_MD5
);
3082 /* server should be removed when in delay */
3083 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3084 test_true(rc
== MEMCACHED_SUCCESS
);
3086 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3087 test_true(value
== 1);
3089 memcached_server_st
*server_pool
;
3090 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");
3091 memcached_server_push(memc
, server_pool
);
3093 /* verify that the server list was parsed okay. */
3094 test_true(memcached_server_count(memc
) == 8);
3095 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3096 test_true(server_pool
[0].port
== 11211);
3097 test_true(server_pool
[0].weight
== 600);
3098 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3099 test_true(server_pool
[2].port
== 11211);
3100 test_true(server_pool
[2].weight
== 200);
3101 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3102 test_true(server_pool
[7].port
== 11211);
3103 test_true(server_pool
[7].weight
== 100);
3105 instance
= memcached_server_instance_by_position(memc
, 2);
3106 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3107 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3110 This would not work if there were only two hosts.
3112 for (size_t x
= 0; x
< 99; x
++)
3114 memcached_autoeject(memc
);
3115 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3116 test_true(server_idx
!= 2);
3119 /* and re-added when it's back. */
3120 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3121 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3123 memc
->distribution
);
3124 for (size_t x
= 0; x
< 99; x
++)
3126 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3127 // We re-use instance from above.
3129 memcached_server_instance_by_position(memc
, server_idx
);
3130 const char *hostname
= memcached_server_name(instance
);
3131 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3134 memcached_server_list_free(server_pool
);
3135 memcached_free(memc
);
3137 return TEST_SUCCESS
;
3140 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3144 memcached_return_t rc
;
3145 memcached_st
*memc
= memcached_create(NULL
);
3149 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3150 test_true(rc
== MEMCACHED_SUCCESS
);
3152 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3153 test_true(value
== 1);
3155 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3156 test_true(rc
== MEMCACHED_SUCCESS
);
3158 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3159 test_true(value
== MEMCACHED_HASH_MD5
);
3162 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3164 memcached_server_st
*server_pool
;
3165 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");
3166 memcached_server_push(memc
, server_pool
);
3168 // @todo this needs to be refactored to actually test something.
3171 if ((fp
= fopen("ketama_keys.txt", "w")))
3175 printf("cannot write to file ketama_keys.txt");
3176 return TEST_FAILURE
;
3179 for (int x
= 0; x
< 10000; x
++)
3182 snprintf(key
, sizeof(key
), "%d", x
);
3184 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3185 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3186 in_port_t port
= memc
->hosts
[server_idx
].port
;
3187 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3188 memcached_server_instance_st instance
=
3189 memcached_server_instance_by_position(memc
, host_index
);
3193 memcached_server_list_free(server_pool
);
3194 memcached_free(memc
);
3196 return TEST_SUCCESS
;
3200 static test_return_t
result_static(memcached_st
*memc
)
3202 memcached_result_st result
;
3203 memcached_result_st
*result_ptr
;
3205 result_ptr
= memcached_result_create(memc
, &result
);
3206 test_true(result
.options
.is_allocated
== false);
3207 test_true(memcached_is_initialized(&result
) == true);
3208 test_true(result_ptr
);
3209 test_true(result_ptr
== &result
);
3211 memcached_result_free(&result
);
3213 test_true(result
.options
.is_allocated
== false);
3214 test_true(memcached_is_initialized(&result
) == false);
3216 return TEST_SUCCESS
;
3219 static test_return_t
result_alloc(memcached_st
*memc
)
3221 memcached_result_st
*result_ptr
;
3223 result_ptr
= memcached_result_create(memc
, NULL
);
3224 test_true(result_ptr
);
3225 test_true(result_ptr
->options
.is_allocated
== true);
3226 test_true(memcached_is_initialized(result_ptr
) == true);
3227 memcached_result_free(result_ptr
);
3229 return TEST_SUCCESS
;
3232 static test_return_t
string_static_null(memcached_st
*memc
)
3234 memcached_string_st string
;
3235 memcached_string_st
*string_ptr
;
3237 string_ptr
= memcached_string_create(memc
, &string
, 0);
3238 test_true(string
.options
.is_initialized
== true);
3239 test_true(string_ptr
);
3241 /* The following two better be the same! */
3242 test_true(memcached_is_allocated(string_ptr
) == false);
3243 test_true(memcached_is_allocated(&string
) == false);
3244 test_true(&string
== string_ptr
);
3246 test_true(string
.options
.is_initialized
== true);
3247 test_true(memcached_is_initialized(&string
) == true);
3248 memcached_string_free(&string
);
3249 test_true(memcached_is_initialized(&string
) == false);
3251 return TEST_SUCCESS
;
3254 static test_return_t
string_alloc_null(memcached_st
*memc
)
3256 memcached_string_st
*string
;
3258 string
= memcached_string_create(memc
, NULL
, 0);
3260 test_true(memcached_is_allocated(string
) == true);
3261 test_true(memcached_is_initialized(string
) == true);
3262 memcached_string_free(string
);
3264 return TEST_SUCCESS
;
3267 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3269 memcached_string_st
*string
;
3271 string
= memcached_string_create(memc
, NULL
, 1024);
3273 test_true(memcached_is_allocated(string
) == true);
3274 test_true(memcached_is_initialized(string
) == true);
3275 memcached_string_free(string
);
3277 return TEST_SUCCESS
;
3280 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3282 memcached_string_st
*string
;
3284 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3285 test_true(string
== NULL
);
3287 return TEST_SUCCESS
;
3290 static test_return_t
string_alloc_append(memcached_st
*memc
)
3293 char buffer
[SMALL_STRING_LEN
];
3294 memcached_string_st
*string
;
3296 /* Ring the bell! */
3297 memset(buffer
, 6, SMALL_STRING_LEN
);
3299 string
= memcached_string_create(memc
, NULL
, 100);
3301 test_true(memcached_is_allocated(string
) == true);
3302 test_true(memcached_is_initialized(string
) == true);
3304 for (x
= 0; x
< 1024; x
++)
3306 memcached_return_t rc
;
3307 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3308 test_true(rc
== MEMCACHED_SUCCESS
);
3310 test_true(memcached_is_allocated(string
) == true);
3311 memcached_string_free(string
);
3313 return TEST_SUCCESS
;
3316 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3318 memcached_return_t rc
;
3320 char buffer
[SMALL_STRING_LEN
];
3321 memcached_string_st
*string
;
3323 /* Ring the bell! */
3324 memset(buffer
, 6, SMALL_STRING_LEN
);
3326 string
= memcached_string_create(memc
, NULL
, 100);
3328 test_true(memcached_is_allocated(string
) == true);
3329 test_true(memcached_is_initialized(string
) == true);
3331 for (x
= 0; x
< 1024; x
++)
3333 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3334 test_true(rc
== MEMCACHED_SUCCESS
);
3336 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3337 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3338 test_true(memcached_is_allocated(string
) == true);
3339 memcached_string_free(string
);
3341 return TEST_SUCCESS
;
3344 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3347 pairs_free(global_pairs
);
3349 return TEST_SUCCESS
;
3352 static test_return_t
generate_pairs(memcached_st
*memc
)
3355 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3356 global_count
= GLOBAL_COUNT
;
3358 for (size_t x
= 0; x
< global_count
; x
++)
3360 global_keys
[x
]= global_pairs
[x
].key
;
3361 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3364 return TEST_SUCCESS
;
3367 static test_return_t
generate_large_pairs(memcached_st
*memc
)
3370 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3371 global_count
= GLOBAL2_COUNT
;
3373 for (size_t x
= 0; x
< global_count
; x
++)
3375 global_keys
[x
]= global_pairs
[x
].key
;
3376 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3379 return TEST_SUCCESS
;
3382 static test_return_t
generate_data(memcached_st
*memc
)
3384 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3386 test_true(check_execute
== global_count
);
3388 return TEST_SUCCESS
;
3391 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3393 memcached_stat_st
*stat_p
;
3394 memcached_return_t rc
;
3395 uint32_t host_index
= 0;
3396 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3398 test_true(check_execute
== global_count
);
3400 //TODO: hosts used size stats
3401 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3404 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3406 /* This test was changes so that "make test" would work properlly */
3408 memcached_server_instance_st instance
=
3409 memcached_server_instance_by_position(memc
, host_index
);
3411 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3413 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3416 memcached_stat_free(NULL
, stat_p
);
3418 return TEST_SUCCESS
;
3420 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3425 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3426 generate_data(memc
);
3428 return TEST_SUCCESS
;
3431 static test_return_t
get_read_count(memcached_st
*memc
)
3433 memcached_return_t rc
;
3434 memcached_st
*memc_clone
;
3436 memc_clone
= memcached_clone(NULL
, memc
);
3437 test_true(memc_clone
);
3439 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3443 size_t return_value_length
;
3447 for (size_t x
= count
= 0; x
< global_count
; x
++)
3449 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3450 &return_value_length
, &flags
, &rc
);
3451 if (rc
== MEMCACHED_SUCCESS
)
3460 memcached_free(memc_clone
);
3462 return TEST_SUCCESS
;
3465 static test_return_t
get_read(memcached_st
*memc
)
3467 memcached_return_t rc
;
3471 size_t return_value_length
;
3474 for (size_t x
= 0; x
< global_count
; x
++)
3476 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3477 &return_value_length
, &flags
, &rc
);
3479 test_true(return_value);
3480 test_true(rc == MEMCACHED_SUCCESS);
3482 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3487 return TEST_SUCCESS
;
3490 static test_return_t
mget_read(memcached_st
*memc
)
3492 memcached_return_t rc
;
3494 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3495 return TEST_SKIPPED
;
3497 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3499 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3501 // Go fetch the keys and test to see if all of them were returned
3503 size_t keys_returned
;
3504 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3506 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3507 test_true_got(global_count
== keys_returned
, buffer
);
3511 return TEST_SUCCESS
;
3514 static test_return_t
mget_read_result(memcached_st
*memc
)
3516 memcached_return_t rc
;
3518 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3519 return TEST_SKIPPED
;
3521 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3523 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3525 /* Turn this into a help function */
3527 memcached_result_st results_obj
;
3528 memcached_result_st
*results
;
3530 results
= memcached_result_create(memc
, &results_obj
);
3532 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3535 test_true(rc
== MEMCACHED_SUCCESS
);
3538 memcached_result_free(&results_obj
);
3541 return TEST_SUCCESS
;
3544 static test_return_t
mget_read_function(memcached_st
*memc
)
3546 memcached_return_t rc
;
3548 memcached_execute_fn callbacks
[1];
3550 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3551 return TEST_SKIPPED
;
3553 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3555 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3557 callbacks
[0]= &callback_counter
;
3559 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3561 return TEST_SUCCESS
;
3564 static test_return_t
delete_generate(memcached_st
*memc
)
3566 for (size_t x
= 0; x
< global_count
; x
++)
3568 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3571 return TEST_SUCCESS
;
3574 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3579 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3581 for (size_t x
= 0; x
< global_count
; x
++)
3583 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3586 return TEST_SUCCESS
;
3589 static test_return_t
add_host_test1(memcached_st
*memc
)
3591 memcached_return_t rc
;
3592 char servername
[]= "0.example.com";
3593 memcached_server_st
*servers
;
3595 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3597 test_true(1 == memcached_server_list_count(servers
));
3599 for (size_t x
= 2; x
< 20; x
++)
3601 char buffer
[SMALL_STRING_LEN
];
3603 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3604 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3606 test_true(rc
== MEMCACHED_SUCCESS
);
3607 test_true(x
== memcached_server_list_count(servers
));
3610 rc
= memcached_server_push(memc
, servers
);
3611 test_true(rc
== MEMCACHED_SUCCESS
);
3612 rc
= memcached_server_push(memc
, servers
);
3613 test_true(rc
== MEMCACHED_SUCCESS
);
3615 memcached_server_list_free(servers
);
3617 return TEST_SUCCESS
;
3620 static test_return_t
pre_nonblock(memcached_st
*memc
)
3622 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3624 return TEST_SUCCESS
;
3627 static test_return_t
pre_cork(memcached_st
*memc
)
3629 memcached_return_t rc
;
3632 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3635 return TEST_SKIPPED
;
3638 if (rc
== MEMCACHED_SUCCESS
)
3639 return TEST_SUCCESS
;
3641 return TEST_SKIPPED
;
3644 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3651 return TEST_SKIPPED
;
3654 if (rc
!= TEST_SUCCESS
)
3657 return pre_nonblock(memc
);
3660 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3662 memcached_return_t rc
= MEMCACHED_FAILURE
;
3663 memcached_st
*memc_clone
;
3665 memc_clone
= memcached_clone(NULL
, memc
);
3666 test_true(memc_clone
);
3667 // The memcached_version needs to be done on a clone, because the server
3668 // will not toggle protocol on an connection.
3669 memcached_version(memc_clone
);
3671 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3673 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3674 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3675 test_true(rc
== MEMCACHED_SUCCESS
);
3676 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3680 return TEST_SKIPPED
;
3683 memcached_free(memc_clone
);
3685 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3688 static test_return_t
pre_murmur(memcached_st
*memc
)
3690 #ifdef HAVE_MURMUR_HASH
3691 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3692 return TEST_SUCCESS
;
3695 return TEST_SKIPPED
;
3699 static test_return_t
pre_jenkins(memcached_st
*memc
)
3701 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3703 return TEST_SUCCESS
;
3707 static test_return_t
pre_md5(memcached_st
*memc
)
3709 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3711 return TEST_SUCCESS
;
3714 static test_return_t
pre_crc(memcached_st
*memc
)
3716 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3718 return TEST_SUCCESS
;
3721 static test_return_t
pre_hsieh(memcached_st
*memc
)
3723 #ifdef HAVE_HSIEH_HASH
3724 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3725 return TEST_SUCCESS
;
3728 return TEST_SKIPPED
;
3732 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3734 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3736 return TEST_SUCCESS
;
3739 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3741 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3743 return TEST_SUCCESS
;
3746 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3748 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3750 return TEST_SUCCESS
;
3753 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3755 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3757 return TEST_SUCCESS
;
3760 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3762 memcached_return_t rc
;
3765 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3766 test_true(rc
== MEMCACHED_SUCCESS
);
3768 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3769 test_true(value
== 1);
3771 return TEST_SUCCESS
;
3774 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3776 memcached_return_t rc
;
3779 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3780 test_true(rc
== MEMCACHED_SUCCESS
);
3782 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3783 test_true(value
== 1);
3785 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3786 test_true(rc
== MEMCACHED_SUCCESS
);
3788 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3789 test_true(value
== MEMCACHED_HASH_MD5
);
3791 return TEST_SUCCESS
;
3795 @note This should be testing to see if the server really supports the binary protocol.
3797 static test_return_t
pre_binary(memcached_st
*memc
)
3799 memcached_return_t rc
= MEMCACHED_FAILURE
;
3801 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3803 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3804 test_true(rc
== MEMCACHED_SUCCESS
);
3805 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3808 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3811 static test_return_t
pre_sasl(memcached_st
*memc
)
3813 memcached_return_t rc
= MEMCACHED_FAILURE
;
3815 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3816 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3817 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3818 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3820 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3822 memcached_server_st
*servers
= memcached_servers_parse(server
);
3823 test_true(servers
!= NULL
);
3824 memcached_servers_reset(memc
);
3825 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3826 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3827 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3828 test_true(rc
== MEMCACHED_SUCCESS
);
3834 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3837 static test_return_t
pre_replication(memcached_st
*memc
)
3839 test_return_t test_rc
;
3840 test_rc
= pre_binary(memc
);
3842 if (test_rc
!= TEST_SUCCESS
)
3846 * Make sure that we store the item on all servers
3847 * (master + replicas == number of servers)
3849 memcached_return_t rc
;
3850 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3851 memcached_server_count(memc
) - 1);
3852 test_true(rc
== MEMCACHED_SUCCESS
);
3853 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3855 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3859 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3863 rc
= pre_replication(memc
);
3864 if (rc
!= TEST_SUCCESS
)
3867 rc
= pre_nonblock(memc
);
3873 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3877 #ifdef HARD_MALLOC_TESTS
3878 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3886 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3890 #ifdef HARD_MALLOC_TESTS
3891 void *ret
= malloc(size
+ 8);
3894 ret
= (void*)((caddr_t
)ret
+ 8);
3897 void *ret
= malloc(size
);
3902 memset(ret
, 0xff, size
);
3909 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *context
)
3912 #ifdef HARD_MALLOC_TESTS
3913 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3914 void *nmem
= realloc(real_ptr
, size
+ 8);
3919 ret
= (void*)((caddr_t
)nmem
+ 8);
3925 return realloc(mem
, size
);
3930 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *context
)
3933 #ifdef HARD_MALLOC_TESTS
3934 void *mem
= my_malloc(ptr
, nelem
* size
);
3937 memset(mem
, 0, nelem
* size
);
3943 return calloc(nelem
, size
);
3948 static test_return_t
set_prefix(memcached_st
*memc
)
3950 memcached_return_t rc
;
3951 const char *key
= "mine";
3954 /* Make sure be default none exists */
3955 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3956 test_true(rc
== MEMCACHED_FAILURE
);
3958 /* Test a clean set */
3959 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3960 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3962 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3964 test_true(memcmp(value
, key
, 4) == 0);
3965 test_true(rc
== MEMCACHED_SUCCESS
);
3967 /* Test that we can turn it off */
3968 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3969 test_true(rc
== MEMCACHED_SUCCESS
);
3971 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3973 test_true(rc
== MEMCACHED_FAILURE
);
3975 /* Now setup for main test */
3976 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3977 test_true(rc
== MEMCACHED_SUCCESS
);
3979 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3981 test_true(rc
== MEMCACHED_SUCCESS
);
3982 test_true(memcmp(value
, key
, 4) == 0);
3984 /* Set to Zero, and then Set to something too large */
3987 memset(long_key
, 0, 255);
3989 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3990 test_true(rc
== MEMCACHED_SUCCESS
);
3992 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3994 test_true(rc
== MEMCACHED_FAILURE
);
3995 test_true(value
== NULL
);
3997 /* Test a long key for failure */
3998 /* TODO, extend test to determine based on setting, what result should be */
3999 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
4000 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4001 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
4002 test_true(rc
== MEMCACHED_SUCCESS
);
4004 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
4005 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
4006 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4007 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
4009 /* Test for a bad prefix, but with a short key */
4010 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
4011 test_true(rc
== MEMCACHED_SUCCESS
);
4013 strncpy(long_key
, "dog cat", sizeof(long_key
));
4014 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
4015 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
4018 return TEST_SUCCESS
;
4022 #ifdef MEMCACHED_ENABLE_DEPRECATED
4023 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
4025 void *test_ptr
= NULL
;
4028 memcached_malloc_fn malloc_cb
=
4029 (memcached_malloc_fn
)my_malloc
;
4030 cb_ptr
= *(void **)&malloc_cb
;
4031 memcached_return_t rc
;
4033 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
4034 test_true(rc
== MEMCACHED_SUCCESS
);
4035 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
4036 test_true(rc
== MEMCACHED_SUCCESS
);
4037 test_true(test_ptr
== cb_ptr
);
4041 memcached_realloc_fn realloc_cb
=
4042 (memcached_realloc_fn
)my_realloc
;
4043 cb_ptr
= *(void **)&realloc_cb
;
4044 memcached_return_t rc
;
4046 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
4047 test_true(rc
== MEMCACHED_SUCCESS
);
4048 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
4049 test_true(rc
== MEMCACHED_SUCCESS
);
4050 test_true(test_ptr
== cb_ptr
);
4054 memcached_free_fn free_cb
=
4055 (memcached_free_fn
)my_free
;
4056 cb_ptr
= *(void **)&free_cb
;
4057 memcached_return_t rc
;
4059 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
4060 test_true(rc
== MEMCACHED_SUCCESS
);
4061 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
4062 test_true(rc
== MEMCACHED_SUCCESS
);
4063 test_true(test_ptr
== cb_ptr
);
4066 return TEST_SUCCESS
;
4071 static test_return_t
set_memory_alloc(memcached_st
*memc
)
4073 memcached_return_t rc
;
4074 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
4075 my_realloc
, my_calloc
, NULL
);
4076 test_true(rc
== MEMCACHED_FAILURE
);
4078 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
4079 my_realloc
, my_calloc
, NULL
);
4081 memcached_malloc_fn mem_malloc
;
4082 memcached_free_fn mem_free
;
4083 memcached_realloc_fn mem_realloc
;
4084 memcached_calloc_fn mem_calloc
;
4085 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4086 &mem_realloc
, &mem_calloc
);
4088 test_true(mem_malloc
== my_malloc
);
4089 test_true(mem_realloc
== my_realloc
);
4090 test_true(mem_calloc
== my_calloc
);
4091 test_true(mem_free
== my_free
);
4093 return TEST_SUCCESS
;
4096 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4099 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4100 memcached_hash_t hash
;
4101 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4102 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4105 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4106 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4108 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4110 if (hash
!= MEMCACHED_HASH_CRC
)
4111 return TEST_SKIPPED
;
4113 return TEST_SUCCESS
;
4116 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4119 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4120 memcached_hash_t hash
;
4121 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4122 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4125 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4126 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4128 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4130 if (hash
!= MEMCACHED_HASH_HSIEH
)
4131 return TEST_SKIPPED
;
4134 return TEST_SUCCESS
;
4137 static test_return_t
enable_cas(memcached_st
*memc
)
4139 unsigned int set
= 1;
4141 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4143 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4145 return TEST_SUCCESS
;
4148 return TEST_SKIPPED
;
4151 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4153 memcached_version(memc
);
4155 memcached_server_instance_st instance
=
4156 memcached_server_instance_by_position(memc
, 0);
4158 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4159 || instance
->minor_version
> 2)
4161 return TEST_SUCCESS
;
4164 return TEST_SKIPPED
;
4167 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4169 memcached_return_t rc
;
4172 memcached_servers_reset(memc
);
4174 if (stat("/tmp/memcached.socket", &buf
))
4175 return TEST_SKIPPED
;
4177 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4179 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4182 static test_return_t
pre_nodelay(memcached_st
*memc
)
4184 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4185 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4187 return TEST_SUCCESS
;
4190 static test_return_t
pre_settimer(memcached_st
*memc
)
4192 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4193 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4195 return TEST_SUCCESS
;
4198 static test_return_t
poll_timeout(memcached_st
*memc
)
4204 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4206 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4208 test_true(timeout
== 100);
4210 return TEST_SUCCESS
;
4213 static test_return_t
noreply_test(memcached_st
*memc
)
4215 memcached_return_t ret
;
4216 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4217 test_true(ret
== MEMCACHED_SUCCESS
);
4218 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4219 test_true(ret
== MEMCACHED_SUCCESS
);
4220 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4221 test_true(ret
== MEMCACHED_SUCCESS
);
4222 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4223 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4224 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4226 for (int count
=0; count
< 5; ++count
)
4228 for (size_t x
= 0; x
< 100; ++x
)
4231 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4232 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4234 size_t len
= (size_t)check_length
;
4239 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4242 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4245 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4248 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4251 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4257 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4261 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4262 ** API and is _ONLY_ done this way to verify that the library works the
4263 ** way it is supposed to do!!!!
4266 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4268 memcached_server_instance_st instance
=
4269 memcached_server_instance_by_position(memc
, x
);
4270 no_msg
+=(int)(instance
->cursor_active
);
4273 test_true(no_msg
== 0);
4274 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4277 ** Now validate that all items was set properly!
4279 for (size_t x
= 0; x
< 100; ++x
)
4283 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4285 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4287 size_t len
= (size_t)check_length
;
4290 char* value
=memcached_get(memc
, key
, strlen(key
),
4291 &length
, &flags
, &ret
);
4292 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4295 case 0: /* FALLTHROUGH */
4296 case 1: /* FALLTHROUGH */
4298 test_true(strncmp(value
, key
, len
) == 0);
4299 test_true(len
== length
);
4302 test_true(length
== len
* 2);
4305 test_true(length
== len
* 3);
4315 /* Try setting an illegal cas value (should not return an error to
4316 * the caller (because we don't expect a return message from the server)
4318 const char* keys
[]= {"0"};
4319 size_t lengths
[]= {1};
4322 memcached_result_st results_obj
;
4323 memcached_result_st
*results
;
4324 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4325 test_true(ret
== MEMCACHED_SUCCESS
);
4327 results
= memcached_result_create(memc
, &results_obj
);
4329 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4331 test_true(ret
== MEMCACHED_SUCCESS
);
4332 uint64_t cas
= memcached_result_cas(results
);
4333 memcached_result_free(&results_obj
);
4335 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4336 test_true(ret
== MEMCACHED_SUCCESS
);
4339 * The item will have a new cas value, so try to set it again with the old
4340 * value. This should fail!
4342 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4343 test_true(ret
== MEMCACHED_SUCCESS
);
4344 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4345 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4346 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4349 return TEST_SUCCESS
;
4352 static test_return_t
analyzer_test(memcached_st
*memc
)
4354 memcached_return_t rc
;
4355 memcached_stat_st
*memc_stat
;
4356 memcached_analysis_st
*report
;
4358 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4359 test_true(rc
== MEMCACHED_SUCCESS
);
4360 test_true(memc_stat
);
4362 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4363 test_true(rc
== MEMCACHED_SUCCESS
);
4367 memcached_stat_free(NULL
, memc_stat
);
4369 return TEST_SUCCESS
;
4372 /* Count the objects */
4373 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4378 (void)ptr
; (void)key
; (void)key_length
;
4379 size_t *counter
= (size_t *)context
;
4381 *counter
= *counter
+ 1;
4383 return MEMCACHED_SUCCESS
;
4386 static test_return_t
dump_test(memcached_st
*memc
)
4388 memcached_return_t rc
;
4390 memcached_dump_fn callbacks
[1];
4391 test_return_t main_rc
;
4393 callbacks
[0]= &callback_dump_counter
;
4395 /* No support for Binary protocol yet */
4396 if (memc
->flags
.binary_protocol
)
4397 return TEST_SUCCESS
;
4399 main_rc
= set_test3(memc
);
4401 test_true (main_rc
== TEST_SUCCESS
);
4403 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4404 test_true(rc
== MEMCACHED_SUCCESS
);
4406 /* We may have more then 32 if our previous flush has not completed */
4407 test_true(counter
>= 32);
4409 return TEST_SUCCESS
;
4412 #ifdef HAVE_LIBMEMCACHEDUTIL
4413 static void* connection_release(void *arg
)
4416 memcached_pool_st
* pool
;
4421 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4425 #define POOL_SIZE 10
4426 static test_return_t
connection_pool_test(memcached_st
*memc
)
4428 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4429 test_true(pool
!= NULL
);
4430 memcached_st
*mmc
[POOL_SIZE
];
4431 memcached_return_t rc
;
4433 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4435 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4436 test_true(mmc
[x
] != NULL
);
4437 test_true(rc
== MEMCACHED_SUCCESS
);
4440 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4441 test_true(rc
== MEMCACHED_SUCCESS
);
4445 memcached_pool_st
* pool
;
4447 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4448 pthread_create(&tid
, NULL
, connection_release
, &item
);
4449 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4450 test_true(rc
== MEMCACHED_SUCCESS
);
4451 pthread_join(tid
, NULL
);
4452 test_true(mmc
[9] == item
.mmc
);
4453 const char *key
= "key";
4454 size_t keylen
= strlen(key
);
4456 // verify that I can do ops with all connections
4457 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4458 test_true(rc
== MEMCACHED_SUCCESS
);
4460 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4462 uint64_t number_value
;
4463 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4464 test_true(rc
== MEMCACHED_SUCCESS
);
4465 test_true(number_value
== (x
+1));
4469 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4471 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4475 /* verify that I can set behaviors on the pool when I don't have all
4476 * of the connections in the pool. It should however be enabled
4477 * when I push the item into the pool
4479 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4480 test_true(mmc
[0] != NULL
);
4482 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4483 test_true(rc
== MEMCACHED_SUCCESS
);
4485 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4486 test_true(mmc
[1] != NULL
);
4488 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4489 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4490 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4492 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4493 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4494 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4496 test_true(memcached_pool_destroy(pool
) == memc
);
4498 return TEST_SUCCESS
;
4501 static test_return_t
util_version_test(memcached_st
*memc
)
4505 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4506 test_true(if_successful
== true);
4508 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4510 // We expect failure
4513 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4514 fprintf(stderr
, "\nDumping Server Information\n\n");
4515 memcached_server_fn callbacks
[1];
4517 callbacks
[0]= dump_server_information
;
4518 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4519 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4521 test_true(if_successful
== false);
4523 memcached_server_instance_st instance
=
4524 memcached_server_instance_by_position(memc
, 0);
4526 memcached_version(memc
);
4528 // We only use one binary when we test, so this should be just fine.
4529 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4530 test_true(if_successful
== true);
4532 if (instance
->micro_version
> 0)
4533 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4534 else if (instance
->minor_version
> 0)
4535 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4536 else if (instance
->major_version
> 0)
4537 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4539 test_true(if_successful
== true);
4541 if (instance
->micro_version
> 0)
4542 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4543 else if (instance
->minor_version
> 0)
4544 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4545 else if (instance
->major_version
> 0)
4546 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4548 test_true(if_successful
== false);
4550 return TEST_SUCCESS
;
4553 static test_return_t
ping_test(memcached_st
*memc
)
4555 memcached_return_t rc
;
4556 memcached_server_instance_st instance
=
4557 memcached_server_instance_by_position(memc
, 0);
4559 // Test both the version that returns a code, and the one that does not.
4560 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4561 memcached_server_port(instance
), NULL
));
4563 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4564 memcached_server_port(instance
), &rc
));
4566 test_true(rc
== MEMCACHED_SUCCESS
);
4568 return TEST_SUCCESS
;
4574 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4578 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4579 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4580 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4581 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4582 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4583 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4584 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4585 #ifdef HAVE_HSIEH_HASH
4586 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4588 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4589 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4590 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4592 return TEST_SUCCESS
;
4596 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4598 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4599 #ifdef HAVE_HSIEH_HASH
4600 expected_rc
= MEMCACHED_SUCCESS
;
4602 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4603 (uint64_t)MEMCACHED_HASH_HSIEH
);
4604 test_true(rc
== expected_rc
);
4606 return TEST_SUCCESS
;
4609 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4611 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4612 #ifdef HAVE_MURMUR_HASH
4613 expected_rc
= MEMCACHED_SUCCESS
;
4615 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4616 (uint64_t)MEMCACHED_HASH_MURMUR
);
4617 test_true(rc
== expected_rc
);
4619 return TEST_SUCCESS
;
4622 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4628 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4632 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4633 test_true(one_at_a_time_values
[x
] == hash_val
);
4636 return TEST_SUCCESS
;
4639 static test_return_t
md5_run (memcached_st
*memc
)
4645 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4649 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4650 test_true(md5_values
[x
] == hash_val
);
4653 return TEST_SUCCESS
;
4656 static test_return_t
crc_run (memcached_st
*memc
)
4662 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4666 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4667 test_true(crc_values
[x
] == hash_val
);
4670 return TEST_SUCCESS
;
4673 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4679 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4683 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4684 test_true(fnv1_64_values
[x
] == hash_val
);
4687 return TEST_SUCCESS
;
4690 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4696 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4700 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4701 test_true(fnv1a_64_values
[x
] == hash_val
);
4704 return TEST_SUCCESS
;
4707 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4713 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4717 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4718 test_true(fnv1_32_values
[x
] == hash_val
);
4721 return TEST_SUCCESS
;
4724 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4730 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4734 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4735 test_true(fnv1a_32_values
[x
] == hash_val
);
4738 return TEST_SUCCESS
;
4741 static test_return_t
hsieh_run (memcached_st
*memc
)
4747 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4751 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4752 test_true(hsieh_values
[x
] == hash_val
);
4755 return TEST_SUCCESS
;
4758 static test_return_t
murmur_run (memcached_st
*memc
)
4760 #ifdef WORDS_BIGENDIAN
4761 (void)murmur_values
;
4762 return TEST_SKIPPED
;
4768 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4772 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4773 test_true(murmur_values
[x
] == hash_val
);
4776 return TEST_SUCCESS
;
4780 static test_return_t
jenkins_run (memcached_st
*memc
)
4786 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4790 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4791 test_true(jenkins_values
[x
] == hash_val
);
4794 return TEST_SUCCESS
;
4797 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4800 return libhashkit_md5(string
, string_length
);
4803 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4806 return libhashkit_crc32(string
, string_length
);
4809 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4813 const hashkit_st
*kit
;
4815 hashkit_return_t hash_rc
;
4817 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};
4818 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};
4820 kit
= memcached_get_hashkit(memc
);
4822 hashkit_clone(&new_kit
, kit
);
4823 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4824 test_true(hash_rc
== HASHKIT_SUCCESS
);
4826 memcached_set_hashkit(memc
, &new_kit
);
4829 Verify Setting the hash.
4831 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4835 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4836 test_true(md5_values
[x
] == hash_val
);
4841 Now check memcached_st.
4843 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4847 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4848 test_true(md5_hosts
[x
] == hash_val
);
4851 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4852 test_true(hash_rc
== HASHKIT_SUCCESS
);
4854 memcached_set_hashkit(memc
, &new_kit
);
4857 Verify Setting the hash.
4859 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4863 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4864 test_true(crc_values
[x
] == hash_val
);
4867 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4871 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4872 test_true(crc_hosts
[x
] == hash_val
);
4875 return TEST_SUCCESS
;
4879 Test case adapted from John Gorman <johngorman2@gmail.com>
4881 We are testing the error condition when we connect to a server via memcached_get()
4882 but find that the server is not available.
4884 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4887 memcached_st
*tl_memc_h
;
4888 memcached_server_st
*servers
;
4890 const char *key
= "MemcachedLives";
4893 memcached_return rc
;
4897 tl_memc_h
= memcached_create(NULL
);
4898 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4899 memcached_server_push(tl_memc_h
, servers
);
4900 memcached_server_list_free(servers
);
4902 // See if memcached is reachable.
4903 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4906 test_true(len
== 0);
4907 test_false(rc
== MEMCACHED_SUCCESS
);
4909 memcached_free(tl_memc_h
);
4911 return TEST_SUCCESS
;
4915 We connect to a server which exists, but search for a key that does not exist.
4917 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4919 const char *key
= "MemcachedKeyNotEXIST";
4922 memcached_return rc
;
4925 // See if memcached is reachable.
4926 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4929 test_true(len
== 0);
4930 test_true(rc
== MEMCACHED_NOTFOUND
);
4932 return TEST_SUCCESS
;
4936 Test case adapted from John Gorman <johngorman2@gmail.com>
4938 We are testing the error condition when we connect to a server via memcached_get_by_key()
4939 but find that the server is not available.
4941 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4944 memcached_st
*tl_memc_h
;
4945 memcached_server_st
*servers
;
4947 const char *key
= "MemcachedLives";
4950 memcached_return rc
;
4954 tl_memc_h
= memcached_create(NULL
);
4955 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4956 memcached_server_push(tl_memc_h
, servers
);
4957 memcached_server_list_free(servers
);
4959 // See if memcached is reachable.
4960 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4963 test_true(len
== 0);
4964 test_false(rc
== MEMCACHED_SUCCESS
);
4966 memcached_free(tl_memc_h
);
4968 return TEST_SUCCESS
;
4972 We connect to a server which exists, but search for a key that does not exist.
4974 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4976 const char *key
= "MemcachedKeyNotEXIST";
4979 memcached_return rc
;
4982 // See if memcached is reachable.
4983 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4986 test_true(len
== 0);
4987 test_true(rc
== MEMCACHED_NOTFOUND
);
4989 return TEST_SUCCESS
;
4993 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4995 memcached_return_t rc
;
4998 memcached_server_st
*server_pool
;
5003 memc
= memcached_create(NULL
);
5006 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5007 test_true(rc
== MEMCACHED_SUCCESS
);
5009 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5010 test_true(value
== 1);
5012 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5013 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5016 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");
5017 memcached_server_push(memc
, server_pool
);
5019 /* verify that the server list was parsed okay. */
5020 test_true(memcached_server_count(memc
) == 8);
5021 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5022 test_true(server_pool
[0].port
== 11211);
5023 test_true(server_pool
[0].weight
== 600);
5024 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5025 test_true(server_pool
[2].port
== 11211);
5026 test_true(server_pool
[2].weight
== 200);
5027 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5028 test_true(server_pool
[7].port
== 11211);
5029 test_true(server_pool
[7].weight
== 100);
5031 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5032 * us test the boundary wraparound.
5034 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5036 /* verify the standard ketama set. */
5037 for (x
= 0; x
< 99; x
++)
5039 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5040 memcached_server_instance_st instance
=
5041 memcached_server_instance_by_position(memc
, server_idx
);
5042 const char *hostname
= memcached_server_name(instance
);
5044 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5047 memcached_server_list_free(server_pool
);
5048 memcached_free(memc
);
5050 return TEST_SUCCESS
;
5053 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5055 memcached_return_t rc
;
5058 memcached_server_st
*server_pool
;
5063 memc
= memcached_create(NULL
);
5066 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5067 test_true(rc
== MEMCACHED_SUCCESS
);
5069 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5070 test_true(value
== 1);
5072 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5073 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5075 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");
5076 memcached_server_push(memc
, server_pool
);
5078 /* verify that the server list was parsed okay. */
5079 test_true(memcached_server_count(memc
) == 8);
5080 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5081 test_true(server_pool
[0].port
== 11211);
5082 test_true(server_pool
[0].weight
== 600);
5083 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5084 test_true(server_pool
[2].port
== 11211);
5085 test_true(server_pool
[2].weight
== 200);
5086 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5087 test_true(server_pool
[7].port
== 11211);
5088 test_true(server_pool
[7].weight
== 100);
5090 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5091 * us test the boundary wraparound.
5093 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5095 /* verify the standard ketama set. */
5096 for (x
= 0; x
< 99; x
++)
5098 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5100 memcached_server_instance_st instance
=
5101 memcached_server_instance_by_position(memc
, server_idx
);
5103 const char *hostname
= memcached_server_name(instance
);
5105 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5108 memcached_server_list_free(server_pool
);
5109 memcached_free(memc
);
5111 return TEST_SUCCESS
;
5114 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5116 test_return_t test_rc
;
5117 test_rc
= pre_binary(memc
);
5119 if (test_rc
!= TEST_SUCCESS
)
5122 memcached_return_t ret
;
5123 const char *key
= "regression_bug_434484";
5124 size_t keylen
= strlen(key
);
5126 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5127 test_true(ret
== MEMCACHED_NOTSTORED
);
5129 size_t size
= 2048 * 1024;
5130 void *data
= calloc(1, size
);
5131 test_true(data
!= NULL
);
5132 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5133 test_true(ret
== MEMCACHED_E2BIG
);
5136 return TEST_SUCCESS
;
5139 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5141 test_return_t test_rc
;
5142 test_rc
= pre_binary(memc
);
5144 if (test_rc
!= TEST_SUCCESS
)
5147 memcached_return_t rc
;
5149 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5152 * I only want to hit only _one_ server so I know the number of requests I'm
5153 * sending in the pipleine to the server. Let's try to do a multiget of
5154 * 1024 (that should satisfy most users don't you think?). Future versions
5155 * will include a mget_execute function call if you need a higher number.
5157 uint32_t number_of_hosts
= memcached_server_count(memc
);
5158 memc
->number_of_hosts
= 1;
5159 const size_t max_keys
= 1024;
5160 char **keys
= calloc(max_keys
, sizeof(char*));
5161 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5163 for (size_t x
= 0; x
< max_keys
; ++x
)
5167 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5169 test_true(keys
[x
] != NULL
);
5173 * Run two times.. the first time we should have 100% cache miss,
5174 * and the second time we should have 100% cache hits
5176 for (size_t y
= 0; y
< 2; y
++)
5178 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5179 test_true(rc
== MEMCACHED_SUCCESS
);
5180 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5184 /* The first iteration should give me a 100% cache miss. verify that*/
5185 char blob
[1024]= { 0 };
5187 test_true(counter
== 0);
5189 for (size_t x
= 0; x
< max_keys
; ++x
)
5191 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5192 blob
, sizeof(blob
), 0, 0);
5193 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5198 /* Verify that we received all of the key/value pairs */
5199 test_true(counter
== max_keys
);
5203 /* Release allocated resources */
5204 for (size_t x
= 0; x
< max_keys
; ++x
)
5211 memc
->number_of_hosts
= number_of_hosts
;
5213 return TEST_SUCCESS
;
5216 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5218 memcached_return_t rc
;
5219 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5220 test_true(rc
== MEMCACHED_SUCCESS
);
5222 return regression_bug_434843(memc
);
5225 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5227 memcached_return_t rc
;
5228 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5229 test_true(rc
== MEMCACHED_SUCCESS
);
5231 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5232 test_true(rc
== MEMCACHED_SUCCESS
);
5233 test_true(bytes
!= NULL
);
5234 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5236 test_true(rc
== MEMCACHED_SUCCESS
);
5237 test_true(bytes_read
!= NULL
);
5239 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5240 "bytes_written", &rc
);
5241 test_true(rc
== MEMCACHED_SUCCESS
);
5242 test_true(bytes_written
!= NULL
);
5244 test_true(strcmp(bytes
, bytes_read
) != 0);
5245 test_true(strcmp(bytes
, bytes_written
) != 0);
5247 /* Release allocated resources */
5250 free(bytes_written
);
5251 memcached_stat_free(NULL
, memc_stat
);
5253 return TEST_SUCCESS
;
5257 * The test case isn't obvious so I should probably document why
5258 * it works the way it does. Bug 442914 was caused by a bug
5259 * in the logic in memcached_purge (it did not handle the case
5260 * where the number of bytes sent was equal to the watermark).
5261 * In this test case, create messages so that we hit that case
5262 * and then disable noreply mode and issue a new command to
5263 * verify that it isn't stuck. If we change the format for the
5264 * delete command or the watermarks, we need to update this
5267 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5269 memcached_return_t rc
;
5270 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5271 test_true(rc
== MEMCACHED_SUCCESS
);
5272 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5274 uint32_t number_of_hosts
= memcached_server_count(memc
);
5275 memc
->number_of_hosts
= 1;
5280 for (uint32_t x
= 0; x
< 250; ++x
)
5282 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5283 rc
= memcached_delete(memc
, k
, len
, 0);
5284 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5287 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5290 rc
= memcached_delete(memc
, k
, len
, 0);
5291 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5293 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5294 test_true(rc
== MEMCACHED_SUCCESS
);
5295 rc
= memcached_delete(memc
, k
, len
, 0);
5296 test_true(rc
== MEMCACHED_NOTFOUND
);
5298 memc
->number_of_hosts
= number_of_hosts
;
5300 return TEST_SUCCESS
;
5303 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5305 memcached_server_instance_st instance_one
;
5306 memcached_server_instance_st instance_two
;
5308 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5309 return TEST_SKIPPED
;
5311 memcached_return_t rc
;
5313 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5314 test_true(rc
== MEMCACHED_SUCCESS
);
5316 const size_t max_keys
= 100;
5317 char **keys
= calloc(max_keys
, sizeof(char*));
5318 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5320 for (size_t x
= 0; x
< max_keys
; ++x
)
5324 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5326 test_true(keys
[x
] != NULL
);
5327 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5328 test_true(rc
== MEMCACHED_SUCCESS
);
5332 ** We are using the quiet commands to store the replicas, so we need
5333 ** to ensure that all of them are processed before we can continue.
5334 ** In the test we go directly from storing the object to trying to
5335 ** receive the object from all of the different servers, so we
5336 ** could end up in a race condition (the memcached server hasn't yet
5337 ** processed the quiet command from the replication set when it process
5338 ** the request from the other client (created by the clone)). As a
5339 ** workaround for that we call memcached_quit to send the quit command
5340 ** to the server and wait for the response ;-) If you use the test code
5341 ** as an example for your own code, please note that you shouldn't need
5344 memcached_quit(memc
);
5346 /* Verify that all messages are stored, and we didn't stuff too much
5349 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5350 test_true(rc
== MEMCACHED_SUCCESS
);
5353 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5354 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5355 /* Verify that we received all of the key/value pairs */
5356 test_true(counter
== max_keys
);
5358 memcached_quit(memc
);
5360 * Don't do the following in your code. I am abusing the internal details
5361 * within the library, and this is not a supported interface.
5362 * This is to verify correct behavior in the library. Fake that two servers
5365 instance_one
= memcached_server_instance_by_position(memc
, 0);
5366 instance_two
= memcached_server_instance_by_position(memc
, 2);
5367 in_port_t port0
= instance_one
->port
;
5368 in_port_t port2
= instance_two
->port
;
5370 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5371 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5373 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5374 test_true(rc
== MEMCACHED_SUCCESS
);
5377 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5378 test_true(counter
== (unsigned int)max_keys
);
5380 /* restore the memc handle */
5381 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5382 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5384 memcached_quit(memc
);
5386 /* Remove half of the objects */
5387 for (size_t x
= 0; x
< max_keys
; ++x
)
5391 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5392 test_true(rc
== MEMCACHED_SUCCESS
);
5396 memcached_quit(memc
);
5397 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5398 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5400 /* now retry the command, this time we should have cache misses */
5401 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5402 test_true(rc
== MEMCACHED_SUCCESS
);
5405 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5406 test_true(counter
== (unsigned int)(max_keys
>> 1));
5408 /* Release allocated resources */
5409 for (size_t x
= 0; x
< max_keys
; ++x
)
5416 /* restore the memc handle */
5417 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5418 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5420 return TEST_SUCCESS
;
5423 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5425 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5426 test_true(memc_clone
!= NULL
);
5427 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5429 memcached_server_instance_st instance
=
5430 memcached_server_instance_by_position(memc_clone
, 0);
5432 if (instance
->major_version
> 1 ||
5433 (instance
->major_version
== 1 &&
5434 instance
->minor_version
> 2))
5436 /* Binary protocol doesn't support deferred delete */
5437 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5438 test_true(bin_clone
!= NULL
);
5439 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5440 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5441 memcached_free(bin_clone
);
5443 memcached_quit(memc_clone
);
5445 /* If we know the server version, deferred delete should fail
5446 * with invalid arguments */
5447 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5449 /* If we don't know the server version, we should get a protocol error */
5450 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5452 /* but there is a bug in some of the memcached servers (1.4) that treats
5453 * the counter as noreply so it doesn't send the proper error message
5455 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5457 /* And buffered mode should be disabled and we should get protocol error */
5458 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5459 rc
= memcached_delete(memc
, "foo", 3, 1);
5460 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5462 /* Same goes for noreply... */
5463 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5464 rc
= memcached_delete(memc
, "foo", 3, 1);
5465 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5467 /* but a normal request should go through (and be buffered) */
5468 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5469 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5471 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5472 /* unbuffered noreply should be success */
5473 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5474 /* unbuffered with reply should be not found... */
5475 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5476 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5479 memcached_free(memc_clone
);
5480 return TEST_SUCCESS
;
5484 /* Test memcached_server_get_last_disconnect
5485 * For a working server set, shall be NULL
5486 * For a set of non existing server, shall not be NULL
5488 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5490 memcached_return_t rc
;
5491 memcached_server_instance_st disconnected_server
;
5493 /* With the working set of server */
5494 const char *key
= "marmotte";
5495 const char *value
= "milka";
5497 memcached_reset_last_disconnected_server(memc
);
5498 rc
= memcached_set(memc
, key
, strlen(key
),
5499 value
, strlen(value
),
5500 (time_t)0, (uint32_t)0);
5501 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5503 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5504 test_true(disconnected_server
== NULL
);
5506 /* With a non existing server */
5508 memcached_server_st
*servers
;
5510 const char *server_list
= "localhost:9";
5512 servers
= memcached_servers_parse(server_list
);
5514 mine
= memcached_create(NULL
);
5515 rc
= memcached_server_push(mine
, servers
);
5516 test_true(rc
== MEMCACHED_SUCCESS
);
5517 memcached_server_list_free(servers
);
5520 rc
= memcached_set(mine
, key
, strlen(key
),
5521 value
, strlen(value
),
5522 (time_t)0, (uint32_t)0);
5523 test_true(rc
!= MEMCACHED_SUCCESS
);
5525 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5526 if (disconnected_server
== NULL
)
5528 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5531 test_true(disconnected_server
!= NULL
);
5532 test_true(memcached_server_port(disconnected_server
)== 9);
5533 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5535 memcached_quit(mine
);
5536 memcached_free(mine
);
5538 return TEST_SUCCESS
;
5541 static test_return_t
test_verbosity(memcached_st
*memc
)
5543 memcached_verbosity(memc
, 3);
5545 return TEST_SUCCESS
;
5548 static test_return_t
test_server_failure(memcached_st
*memc
)
5550 memcached_st
*local_memc
;
5551 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5553 local_memc
= memcached_create(NULL
);
5555 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5556 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5558 uint32_t server_count
= memcached_server_count(local_memc
);
5560 test_true(server_count
== 1);
5562 // Disable the server
5563 instance
= memcached_server_instance_by_position(local_memc
, 0);
5564 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5566 memcached_return_t rc
;
5567 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5569 (time_t)0, (uint32_t)0);
5570 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5572 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5573 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5575 (time_t)0, (uint32_t)0);
5576 test_true(rc
== MEMCACHED_SUCCESS
);
5579 memcached_free(local_memc
);
5581 return TEST_SUCCESS
;
5584 static test_return_t
test_cull_servers(memcached_st
*memc
)
5586 uint32_t count
= memcached_server_count(memc
);
5588 // Do not do this in your code, it is not supported.
5589 memc
->servers
[1].state
.is_dead
= true;
5590 memc
->state
.is_time_for_rebuild
= true;
5592 uint32_t new_count
= memcached_server_count(memc
);
5593 test_true(count
== new_count
);
5596 test_true(count
== new_count
+ 1 );
5599 return TEST_SUCCESS
;
5603 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5604 const char *key
, size_t key_length
,
5605 const char *value
, size_t value_length
,
5615 return MEMCACHED_SUCCESS
;
5618 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5620 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5621 test_true(rc
== MEMCACHED_SUCCESS
);
5623 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5624 test_true(rc
== MEMCACHED_SUCCESS
);
5626 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5627 test_true(rc
== MEMCACHED_SUCCESS
);
5629 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5630 test_true(rc
== MEMCACHED_SUCCESS
);
5632 return TEST_SUCCESS
;
5636 * This test ensures that the failure counter isn't incremented during
5637 * normal termination of the memcached instance.
5639 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5641 memcached_return_t rc
;
5642 memcached_server_instance_st instance
;
5644 /* Set value to force connection to the server */
5645 const char *key
= "marmotte";
5646 const char *value
= "milka";
5649 * Please note that I'm abusing the internal structures in libmemcached
5650 * in a non-portable way and you shouldn't be doing this. I'm only
5651 * doing this in order to verify that the library works the way it should
5653 uint32_t number_of_hosts
= memcached_server_count(memc
);
5654 memc
->number_of_hosts
= 1;
5656 /* Ensure that we are connected to the server by setting a value */
5657 rc
= memcached_set(memc
, key
, strlen(key
),
5658 value
, strlen(value
),
5659 (time_t)0, (uint32_t)0);
5660 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5663 instance
= memcached_server_instance_by_position(memc
, 0);
5664 /* The test is to see that the memcached_quit doesn't increase the
5665 * the server failure conter, so let's ensure that it is zero
5666 * before sending quit
5668 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5670 memcached_quit(memc
);
5672 /* Verify that it memcached_quit didn't increment the failure counter
5673 * Please note that this isn't bullet proof, because an error could
5676 test_true(instance
->server_failure_counter
== 0);
5678 /* restore the instance */
5679 memc
->number_of_hosts
= number_of_hosts
;
5681 return TEST_SUCCESS
;
5685 * This tests ensures expected disconnections (for some behavior changes
5686 * for instance) do not wrongly increase failure counter
5688 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5690 memcached_return rc
;
5692 memcached_st
*memc_clone
;
5693 memc_clone
= memcached_clone(NULL
, memc
);
5694 test_true(memc_clone
);
5696 /* Set value to force connection to the server */
5697 const char *key
= "marmotte";
5698 const char *value
= "milka";
5699 char *string
= NULL
;
5700 size_t string_length
;
5703 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5704 value
, strlen(value
),
5705 (time_t)0, (uint32_t)0);
5706 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5709 /* put failure limit to 1 */
5710 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5711 assert(rc
== MEMCACHED_SUCCESS
);
5713 /* Put a retry timeout to effectively activate failure_limit effect */
5714 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5715 assert(rc
== MEMCACHED_SUCCESS
);
5717 /* change behavior that triggers memcached_quit()*/
5718 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5719 assert(rc
== MEMCACHED_SUCCESS
);
5722 /* Check if we still are connected */
5723 string
= memcached_get(memc_clone
, key
, strlen(key
),
5724 &string_length
, &flags
, &rc
);
5726 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5730 return TEST_SUCCESS
;
5737 * Test that ensures mget_execute does not end into recursive calls that finally fails
5739 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5741 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5742 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5743 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5744 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5745 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5748 return TEST_SKIPPED
; // My MAC can't handle this test
5752 * I only want to hit _one_ server so I know the number of requests I'm
5753 * sending in the pipeline.
5755 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5756 memc
->number_of_hosts
= 1;
5757 size_t max_keys
= 20480;
5760 char **keys
= calloc(max_keys
, sizeof(char*));
5761 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5763 /* First add all of the items.. */
5765 char blob
[1024]= { 0 };
5766 memcached_return rc
;
5767 for (size_t x
= 0; x
< max_keys
; ++x
)
5770 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5772 assert(keys
[x
] != NULL
);
5773 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5775 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5777 break; // We are out of business
5780 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5782 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5785 sleep(1);// We will try to sleep
5788 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5790 // We failed to send everything.
5795 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5798 /* Try to get all of them with a large multiget */
5800 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5801 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5802 (size_t)max_keys
, callbacks
, &counter
, 1);
5804 assert(rc
== MEMCACHED_SUCCESS
);
5805 char* the_value
= NULL
;
5806 char the_key
[MEMCACHED_MAX_KEY
];
5807 size_t the_key_length
;
5808 size_t the_value_length
;
5812 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5814 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5820 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5823 assert(rc
== MEMCACHED_END
);
5825 /* Verify that we got all of the items */
5826 assert(counter
== max_keys
);
5829 /* Release all allocated resources */
5830 for (size_t x
= 0; x
< max_keys
; ++x
)
5837 memc
->number_of_hosts
= number_of_hosts
;
5839 return TEST_SUCCESS
;
5842 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5846 memcached_st
*memc
= memcached_create(NULL
);
5848 memcached_server_add(memc
, "10.2.3.4", 11211);
5850 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5851 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5852 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5853 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5854 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5855 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5857 memcached_return_t rc
;
5861 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5863 test_true_got(rc
== MEMCACHED_TIMEOUT
, memcached_strerror(NULL
, rc
));
5865 memcached_free(memc
);
5867 return TEST_SUCCESS
;
5870 // Look for memory leak
5871 static test_return_t
regression_bug_728286(memcached_st
*unused
)
5874 memcached_server_st
*servers
= memcached_servers_parse("1.2.3.4:99");
5875 memcached_server_free(servers
);
5877 return TEST_SUCCESS
;
5880 static test_return_t
regression_bug_581030(memcached_st
*unused
)
5885 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5886 test_false(local_stat
);
5888 memcached_stat_free(NULL
, NULL
);
5891 return TEST_SUCCESS
;
5894 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5896 fprintf(stderr
, "Iteration #%u: ", it
);
5898 if(error
== MEMCACHED_ERRNO
)
5900 fprintf(stderr
, "system error %d from %s: %s\n",
5901 errno
, what
, strerror(errno
));
5905 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5906 memcached_strerror(mc
, error
));
5910 #define TEST_CONSTANT_CREATION 200
5912 static test_return_t
regression_bug_(memcached_st
*memc
)
5914 const char *remote_server
;
5917 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5919 return TEST_SKIPPED
;
5922 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5924 memcached_st
* mc
= memcached_create(NULL
);
5925 memcached_return rc
;
5927 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5928 if (rc
!= MEMCACHED_SUCCESS
)
5930 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5933 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5934 if (rc
!= MEMCACHED_SUCCESS
)
5936 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5939 rc
= memcached_server_add(mc
, remote_server
, 0);
5940 if (rc
!= MEMCACHED_SUCCESS
)
5942 memcached_die(mc
, rc
, "memcached_server_add", x
);
5945 const char *set_key
= "akey";
5946 const size_t set_key_len
= strlen(set_key
);
5947 const char *set_value
= "a value";
5948 const size_t set_value_len
= strlen(set_value
);
5950 if (rc
== MEMCACHED_SUCCESS
)
5954 size_t get_value_len
;
5956 uint32_t get_value_flags
;
5958 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5959 &get_value_flags
, &rc
);
5960 if (rc
!= MEMCACHED_SUCCESS
)
5962 memcached_die(mc
, rc
, "memcached_get", x
);
5968 (get_value_len
!= set_value_len
5969 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5971 fprintf(stderr
, "Values don't match?\n");
5972 rc
= MEMCACHED_FAILURE
;
5978 rc
= memcached_set(mc
,
5979 set_key
, set_key_len
,
5980 set_value
, set_value_len
,
5984 if (rc
!= MEMCACHED_SUCCESS
)
5986 memcached_die(mc
, rc
, "memcached_set", x
);
5993 if (rc
!= MEMCACHED_SUCCESS
)
5999 return TEST_SUCCESS
;
6003 * Test that the sasl authentication works. We cannot use the default
6004 * pool of servers, because that would require that all servers we want
6005 * to test supports SASL authentication, and that they use the default
6008 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6010 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6011 memcached_return_t rc
;
6013 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6014 test_true(rc
== MEMCACHED_SUCCESS
);
6015 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6016 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6017 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6018 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6019 memcached_quit(memc
);
6021 rc
= memcached_set_sasl_auth_data(memc
,
6022 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6023 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6024 test_true(rc
== MEMCACHED_SUCCESS
);
6026 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6027 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6028 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6030 memcached_quit(memc
);
6031 return TEST_SUCCESS
;
6034 return TEST_FAILURE
;
6038 /* Clean the server before beginning testing */
6040 {"util_version", 1, (test_callback_fn
)util_version_test
},
6041 {"flush", 0, (test_callback_fn
)flush_test
},
6042 {"init", 0, (test_callback_fn
)init_test
},
6043 {"allocation", 0, (test_callback_fn
)allocation_test
},
6044 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6045 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6046 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6047 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6048 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6049 {"clone_test", 0, (test_callback_fn
)clone_test
},
6050 {"connection_test", 0, (test_callback_fn
)connection_test
},
6051 {"callback_test", 0, (test_callback_fn
)callback_test
},
6052 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6053 {"error", 0, (test_callback_fn
)error_test
},
6054 {"set", 0, (test_callback_fn
)set_test
},
6055 {"set2", 0, (test_callback_fn
)set_test2
},
6056 {"set3", 0, (test_callback_fn
)set_test3
},
6057 {"dump", 1, (test_callback_fn
)dump_test
},
6058 {"add", 1, (test_callback_fn
)add_test
},
6059 {"replace", 1, (test_callback_fn
)replace_test
},
6060 {"delete", 1, (test_callback_fn
)delete_test
},
6061 {"get", 1, (test_callback_fn
)get_test
},
6062 {"get2", 0, (test_callback_fn
)get_test2
},
6063 {"get3", 0, (test_callback_fn
)get_test3
},
6064 {"get4", 0, (test_callback_fn
)get_test4
},
6065 {"partial mget", 0, (test_callback_fn
)get_test5
},
6066 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6067 {"increment", 0, (test_callback_fn
)increment_test
},
6068 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6069 {"decrement", 0, (test_callback_fn
)decrement_test
},
6070 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6071 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6072 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6073 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6074 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6075 {"quit", 0, (test_callback_fn
)quit_test
},
6076 {"mget", 1, (test_callback_fn
)mget_test
},
6077 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6078 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6079 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6080 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6081 {"mget_end", 0, (test_callback_fn
)mget_end
},
6082 {"get_stats", 0, (test_callback_fn
)get_stats
},
6083 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6084 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6085 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6086 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6087 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6088 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6089 {"read_through", 1, (test_callback_fn
)read_through
},
6090 {"delete_through", 1, (test_callback_fn
)delete_through
},
6091 {"noreply", 1, (test_callback_fn
)noreply_test
},
6092 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6093 #ifdef HAVE_LIBMEMCACHEDUTIL
6094 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6095 {"ping", 1, (test_callback_fn
)ping_test
},
6097 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6098 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6099 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6100 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6101 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
6105 test_st behavior_tests
[] ={
6106 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6107 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6108 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6109 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6113 test_st basic_tests
[] ={
6114 {"init", 1, (test_callback_fn
)basic_init_test
},
6115 {"clone", 1, (test_callback_fn
)basic_clone_test
},
6116 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
6117 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
6118 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
6119 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
6123 test_st regression_binary_vs_block
[] ={
6124 {"block add", 1, (test_callback_fn
)block_add_regression
},
6125 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6129 test_st async_tests
[] ={
6130 {"add", 1, (test_callback_fn
)add_wrapper
},
6134 test_st string_tests
[] ={
6135 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6136 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6137 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6138 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6139 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6140 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6141 {0, 0, (test_callback_fn
)0}
6144 test_st result_tests
[] ={
6145 {"result static", 0, (test_callback_fn
)result_static
},
6146 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6147 {0, 0, (test_callback_fn
)0}
6150 test_st version_1_2_3
[] ={
6151 {"append", 0, (test_callback_fn
)append_test
},
6152 {"prepend", 0, (test_callback_fn
)prepend_test
},
6153 {"cas", 0, (test_callback_fn
)cas_test
},
6154 {"cas2", 0, (test_callback_fn
)cas2_test
},
6155 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6156 {0, 0, (test_callback_fn
)0}
6159 test_st user_tests
[] ={
6160 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6161 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6162 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6163 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6164 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6165 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6166 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6167 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6168 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6169 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6170 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6171 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6172 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6173 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6174 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6175 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6176 #if !defined(__sun) && !defined(__OpenBSD__)
6178 ** It seems to be something weird with the character sets..
6179 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6180 ** guess I need to find out how this is supposed to work.. Perhaps I need
6181 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6182 ** so just disable the code for now...).
6184 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6186 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6187 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6188 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6189 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6190 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6191 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6192 {0, 0, (test_callback_fn
)0}
6195 test_st replication_tests
[]= {
6196 {"set", 1, (test_callback_fn
)replication_set_test
},
6197 {"get", 0, (test_callback_fn
)replication_get_test
},
6198 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6199 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6200 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6201 {0, 0, (test_callback_fn
)0}
6205 * The following test suite is used to verify that we don't introduce
6206 * regression bugs. If you want more information about the bug / test,
6207 * you should look in the bug report at
6208 * http://bugs.launchpad.net/libmemcached
6210 test_st regression_tests
[]= {
6211 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6212 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6213 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6214 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6215 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6216 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6217 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6218 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6219 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6220 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6221 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6222 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6223 {0, 0, (test_callback_fn
)0}
6226 test_st sasl_auth_tests
[]= {
6227 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6228 {0, 0, (test_callback_fn
)0}
6231 test_st ketama_compatibility
[]= {
6232 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6233 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6234 {0, 0, (test_callback_fn
)0}
6237 test_st generate_tests
[] ={
6238 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6239 {"generate_data", 1, (test_callback_fn
)generate_data
},
6240 {"get_read", 0, (test_callback_fn
)get_read
},
6241 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6242 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6243 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6244 {"generate_data", 1, (test_callback_fn
)generate_data
},
6245 {"mget_read", 0, (test_callback_fn
)mget_read
},
6246 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6247 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6248 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6249 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6250 {"generate_data", 1, (test_callback_fn
)generate_data
},
6251 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6252 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6253 {0, 0, (test_callback_fn
)0}
6256 test_st consistent_tests
[] ={
6257 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6258 {"generate_data", 1, (test_callback_fn
)generate_data
},
6259 {"get_read", 0, (test_callback_fn
)get_read_count
},
6260 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6261 {0, 0, (test_callback_fn
)0}
6264 test_st consistent_weighted_tests
[] ={
6265 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6266 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6267 {"get_read", 0, (test_callback_fn
)get_read_count
},
6268 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6269 {0, 0, (test_callback_fn
)0}
6272 test_st hsieh_availability
[] ={
6273 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6274 {0, 0, (test_callback_fn
)0}
6277 test_st murmur_availability
[] ={
6278 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6279 {0, 0, (test_callback_fn
)0}
6283 test_st hash_sanity
[] ={
6284 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6285 {0, 0, (test_callback_fn
)0}
6289 test_st ketama_auto_eject_hosts
[] ={
6290 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6291 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6292 {0, 0, (test_callback_fn
)0}
6295 test_st hash_tests
[] ={
6296 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6297 {"md5", 0, (test_callback_fn
)md5_run
},
6298 {"crc", 0, (test_callback_fn
)crc_run
},
6299 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6300 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6301 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6302 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6303 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6304 {"murmur", 0, (test_callback_fn
)murmur_run
},
6305 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6306 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6307 {0, 0, (test_callback_fn
)0}
6310 test_st error_conditions
[] ={
6311 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6312 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6313 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6314 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6315 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6316 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6317 {0, 0, (test_callback_fn
)0}
6321 test_st parser_tests
[] ={
6322 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6323 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6324 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6325 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6326 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6327 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6328 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6329 {"memcached_parse_configure_file", 0, (test_callback_fn
)memcached_parse_configure_file_test
},
6330 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6331 {"randomly generated options", 0, (test_callback_fn
)random_statement_build_test
},
6332 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6333 {"server", 0, (test_callback_fn
)server_test
},
6334 {"servers", 0, (test_callback_fn
)servers_test
},
6335 {0, 0, (test_callback_fn
)0}
6338 collection_st collection
[] ={
6340 {"hash_sanity", 0, 0, hash_sanity
},
6342 {"basic", 0, 0, basic_tests
},
6343 {"hsieh_availability", 0, 0, hsieh_availability
},
6344 {"murmur_availability", 0, 0, murmur_availability
},
6345 {"block", 0, 0, tests
},
6346 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6347 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6348 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6349 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6350 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6351 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6352 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6353 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6354 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6355 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6356 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6357 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6358 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6359 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6360 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6361 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6362 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6363 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6364 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6365 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6366 #ifdef MEMCACHED_ENABLE_DEPRECATED
6367 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6369 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6370 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6371 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6372 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6373 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6374 {"string", 0, 0, string_tests
},
6375 {"result", 0, 0, result_tests
},
6376 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6377 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6378 {"user", 0, 0, user_tests
},
6379 {"generate", 0, 0, generate_tests
},
6380 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6381 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6382 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6383 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6384 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6385 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6386 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6388 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6389 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6390 {"consistent_not", 0, 0, consistent_tests
},
6391 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6392 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6393 {"ketama_compat", 0, 0, ketama_compatibility
},
6394 {"test_hashes", 0, 0, hash_tests
},
6395 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6396 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6397 {"regression", 0, 0, regression_tests
},
6398 {"behaviors", 0, 0, behavior_tests
},
6399 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6400 {"error_conditions", 0, 0, error_conditions
},
6401 {"parser", 0, 0, parser_tests
},
6405 #define SERVERS_TO_CREATE 5
6407 #include "libmemcached_world.h"
6409 void get_world(world_st
*world
)
6411 world
->collections
= collection
;
6413 world
->create
= (test_callback_create_fn
)world_create
;
6414 world
->destroy
= (test_callback_fn
)world_destroy
;
6416 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6417 world
->test
.flush
= (test_callback_fn
)world_flush
;
6418 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6419 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6420 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6422 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6423 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6425 world
->runner
= &defualt_libmemcached_runner
;