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.
13 #include "libmemcached/common.h"
20 #include <sys/types.h>
26 #include "clients/generator.h"
27 #include "clients/execute.h"
30 #define INT64_MAX LONG_MAX
33 #define INT32_MAX INT_MAX
39 #ifdef HAVE_LIBMEMCACHEDUTIL
41 #include "libmemcached/memcached_util.h"
44 #include "hash_results.h"
46 #define GLOBAL_COUNT 10000
47 #define GLOBAL2_COUNT 100
48 #define SERVERS_TO_CREATE 5
49 static uint32_t global_count
;
51 static pairs_st
*global_pairs
;
52 static const char *global_keys
[GLOBAL_COUNT
];
53 static size_t global_keys_length
[GLOBAL_COUNT
];
55 static test_return_t
init_test(memcached_st
*not_used
__attribute__((unused
)))
59 (void)memcached_create(&memc
);
60 memcached_free(&memc
);
65 static test_return_t
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
67 memcached_server_st
*server_list
;
68 memcached_return_t rc
;
70 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
71 test_true(server_list
== NULL
);
73 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
74 test_true(server_list
== NULL
);
76 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
77 test_true(server_list
== NULL
);
82 #define TEST_PORT_COUNT 7
83 in_port_t test_ports
[TEST_PORT_COUNT
];
85 static memcached_return_t
server_display_function(const memcached_st
*ptr
__attribute__((unused
)),
86 const memcached_server_st
*server
,
90 size_t bigger
= *((size_t *)(context
));
91 assert(bigger
<= memcached_server_port(server
));
92 *((size_t *)(context
))= memcached_server_port(server
);
94 return MEMCACHED_SUCCESS
;
97 static test_return_t
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
99 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
101 memcached_return_t rc
;
102 memcached_server_fn callbacks
[1];
103 memcached_st
*local_memc
;
105 local_memc
= memcached_create(NULL
);
106 test_true(local_memc
);
107 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
109 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
111 test_ports
[x
]= (in_port_t
)random() % 64000;
112 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
113 test_true(memcached_server_count(local_memc
) == x
+ 1);
115 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
117 test_true(rc
== MEMCACHED_SUCCESS
);
120 callbacks
[0]= server_display_function
;
121 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
124 memcached_free(local_memc
);
129 static test_return_t
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
131 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
132 memcached_return_t rc
;
133 memcached_server_fn callbacks
[1];
134 memcached_st
*local_memc
;
135 memcached_server_instance_st instance
;
137 local_memc
= memcached_create(NULL
);
138 test_true(local_memc
);
139 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
140 test_true(rc
== MEMCACHED_SUCCESS
);
142 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
143 test_true(rc
== MEMCACHED_SUCCESS
);
144 instance
= memcached_server_instance_by_position(local_memc
, 0);
145 test_true(memcached_server_port(instance
) == 43043);
147 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
148 test_true(rc
== MEMCACHED_SUCCESS
);
150 instance
= memcached_server_instance_by_position(local_memc
, 0);
151 test_true(memcached_server_port(instance
) == 43042);
153 instance
= memcached_server_instance_by_position(local_memc
, 1);
154 test_true(memcached_server_port(instance
) == 43043);
156 callbacks
[0]= server_display_function
;
157 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
160 memcached_free(local_memc
);
165 static memcached_return_t
server_print_callback(const memcached_st
*ptr
__attribute__((unused
)),
166 const memcached_server_st
*server
,
167 void *context
__attribute__((unused
)))
169 (void)server
; // Just in case we aren't printing.
172 fprintf(stderr
, "%s(%d)", memcached_server_name(server
), memcached_server_port(server
));
175 return MEMCACHED_SUCCESS
;
178 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
__attribute__((unused
)))
180 memcached_return_t rc
;
181 memcached_st local_memc
;
183 memcached_server_st
*servers
;
184 memcached_server_fn callbacks
[1];
186 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";
188 memc
= memcached_create(&local_memc
);
190 servers
= memcached_servers_parse(server_string
);
192 rc
= memcached_server_push(memc
, servers
);
194 callbacks
[0]= server_print_callback
;
195 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
197 memcached_free(memc
);
202 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
__attribute__((unused
)),
203 const memcached_server_st
*server
,
207 uint32_t x
= *((uint32_t *)(context
));
209 assert(test_ports
[x
] == server
->port
);
210 *((uint32_t *)(context
))= ++x
;
212 return MEMCACHED_SUCCESS
;
215 static test_return_t
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
217 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
218 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
219 memcached_return_t rc
;
220 memcached_server_fn callbacks
[1];
221 memcached_st
*local_memc
;
223 local_memc
= memcached_create(NULL
);
224 test_true(local_memc
);
226 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
228 test_ports
[x
]= (in_port_t
)(random() % 64000);
229 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
230 test_true(memcached_server_count(local_memc
) == x
+1);
232 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
234 test_true(rc
== MEMCACHED_SUCCESS
);
237 callbacks
[0]= server_display_unsort_function
;
238 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
240 /* Now we sort old data! */
241 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
242 callbacks
[0]= server_display_function
;
243 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
246 memcached_free(local_memc
);
251 static test_return_t
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
254 memc
= memcached_create(NULL
);
256 memcached_free(memc
);
261 static test_return_t
clone_test(memcached_st
*memc
)
265 memcached_st
*memc_clone
;
266 memc_clone
= memcached_clone(NULL
, NULL
);
267 test_true(memc_clone
);
268 memcached_free(memc_clone
);
271 /* Can we init from null? */
273 memcached_st
*memc_clone
;
274 memc_clone
= memcached_clone(NULL
, memc
);
275 test_true(memc_clone
);
278 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
279 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
280 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
281 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
284 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
285 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
286 test_true(memc_clone
->distribution
== memc
->distribution
);
287 { // Test all of the flags
288 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
289 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
290 test_true(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
291 test_true(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
292 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
293 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
294 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
295 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
296 test_true(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
297 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
298 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
299 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
300 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
301 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
302 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
304 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
305 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
306 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
307 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
308 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
309 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
310 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
311 test_true(memc_clone
->on_clone
== memc
->on_clone
);
312 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
313 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
314 test_true(memc_clone
->recv_size
== memc
->recv_size
);
315 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
316 test_true(memc_clone
->send_size
== memc
->send_size
);
317 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
318 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
319 test_true(memc_clone
->user_data
== memc
->user_data
);
321 memcached_free(memc_clone
);
324 /* Can we init from struct? */
326 memcached_st declared_clone
;
327 memcached_st
*memc_clone
;
328 memset(&declared_clone
, 0 , sizeof(memcached_st
));
329 memc_clone
= memcached_clone(&declared_clone
, NULL
);
330 test_true(memc_clone
);
331 memcached_free(memc_clone
);
334 /* Can we init from struct? */
336 memcached_st declared_clone
;
337 memcached_st
*memc_clone
;
338 memset(&declared_clone
, 0 , sizeof(memcached_st
));
339 memc_clone
= memcached_clone(&declared_clone
, memc
);
340 test_true(memc_clone
);
341 memcached_free(memc_clone
);
347 static test_return_t
userdata_test(memcached_st
*memc
)
350 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
351 test_true(memcached_get_user_data(memc
) == foo
);
352 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
357 static test_return_t
connection_test(memcached_st
*memc
)
359 memcached_return_t rc
;
361 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
362 test_true(rc
== MEMCACHED_SUCCESS
);
367 static test_return_t
error_test(memcached_st
*memc
)
369 memcached_return_t rc
;
370 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
371 982370485U, 1263635348U, 4242906218U, 3829656100U,
372 1891735253U, 334139633U, 2257084983U, 3088286104U,
373 13199785U, 2542027183U, 1097051614U, 199566778U,
374 2748246961U, 2465192557U, 1664094137U, 2405439045U,
375 1842224848U, 692413798U, 3479807801U, 919913813U,
376 4269430871U, 610793021U, 527273862U, 1437122909U,
377 2300930706U, 2943759320U, 674306647U, 2400528935U,
378 54481931U, 4186304426U, 1741088401U, 2979625118U,
379 4159057246U, 3425930182U, 2593724503U, 1868899624U,
380 1769812374U, 2302537950U, 1110330676U };
382 // You have updated the memcache_error messages but not updated docs/tests.
383 test_true(MEMCACHED_MAXIMUM_RETURN
== 43);
384 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
387 const char *msg
= memcached_strerror(memc
, rc
);
388 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
389 MEMCACHED_HASH_JENKINS
);
390 if (values
[rc
] != hash_val
)
392 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
393 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
395 test_true(values
[rc
] == hash_val
);
401 static test_return_t
set_test(memcached_st
*memc
)
403 memcached_return_t rc
;
404 const char *key
= "foo";
405 const char *value
= "when we sanitize";
407 rc
= memcached_set(memc
, key
, strlen(key
),
408 value
, strlen(value
),
409 (time_t)0, (uint32_t)0);
410 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
415 static test_return_t
append_test(memcached_st
*memc
)
417 memcached_return_t rc
;
418 const char *key
= "fig";
419 const char *in_value
= "we";
420 char *out_value
= NULL
;
424 rc
= memcached_flush(memc
, 0);
425 test_true(rc
== MEMCACHED_SUCCESS
);
427 rc
= memcached_set(memc
, key
, strlen(key
),
428 in_value
, strlen(in_value
),
429 (time_t)0, (uint32_t)0);
430 test_true(rc
== MEMCACHED_SUCCESS
);
432 rc
= memcached_append(memc
, key
, strlen(key
),
433 " the", strlen(" the"),
434 (time_t)0, (uint32_t)0);
435 test_true(rc
== MEMCACHED_SUCCESS
);
437 rc
= memcached_append(memc
, key
, strlen(key
),
438 " people", strlen(" people"),
439 (time_t)0, (uint32_t)0);
440 test_true(rc
== MEMCACHED_SUCCESS
);
442 out_value
= memcached_get(memc
, key
, strlen(key
),
443 &value_length
, &flags
, &rc
);
444 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
445 test_true(strlen("we the people") == value_length
);
446 test_true(rc
== MEMCACHED_SUCCESS
);
452 static test_return_t
append_binary_test(memcached_st
*memc
)
454 memcached_return_t rc
;
455 const char *key
= "numbers";
456 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
462 rc
= memcached_flush(memc
, 0);
463 test_true(rc
== MEMCACHED_SUCCESS
);
465 rc
= memcached_set(memc
,
468 (time_t)0, (uint32_t)0);
469 test_true(rc
== MEMCACHED_SUCCESS
);
471 for (x
= 0; store_list
[x
] ; x
++)
473 rc
= memcached_append(memc
,
475 (char *)&store_list
[x
], sizeof(uint32_t),
476 (time_t)0, (uint32_t)0);
477 test_true(rc
== MEMCACHED_SUCCESS
);
480 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
481 &value_length
, &flags
, &rc
);
482 test_true((value_length
== (sizeof(uint32_t) * x
)));
483 test_true(rc
== MEMCACHED_SUCCESS
);
485 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
487 test_true(*ptr
== store_list
[x
- counter
]);
495 static test_return_t
cas2_test(memcached_st
*memc
)
497 memcached_return_t rc
;
498 const char *keys
[]= {"fudge", "son", "food"};
499 size_t key_length
[]= {5, 3, 4};
500 const char *value
= "we the people";
501 size_t value_length
= strlen("we the people");
503 memcached_result_st results_obj
;
504 memcached_result_st
*results
;
507 rc
= memcached_flush(memc
, 0);
508 test_true(rc
== MEMCACHED_SUCCESS
);
510 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
512 for (x
= 0; x
< 3; x
++)
514 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
515 keys
[x
], key_length
[x
],
516 (time_t)50, (uint32_t)9);
517 test_true(rc
== MEMCACHED_SUCCESS
);
520 rc
= memcached_mget(memc
, keys
, key_length
, 3);
522 results
= memcached_result_create(memc
, &results_obj
);
524 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
526 test_true(results
->item_cas
);
527 test_true(rc
== MEMCACHED_SUCCESS
);
528 test_true(memcached_result_cas(results
));
530 test_true(!memcmp(value
, "we the people", strlen("we the people")));
531 test_true(strlen("we the people") == value_length
);
532 test_true(rc
== MEMCACHED_SUCCESS
);
534 memcached_result_free(&results_obj
);
539 static test_return_t
cas_test(memcached_st
*memc
)
541 memcached_return_t rc
;
542 const char *key
= "fun";
543 size_t key_length
= strlen(key
);
544 const char *value
= "we the people";
545 const char* keys
[2] = { key
, NULL
};
546 size_t keylengths
[2] = { strlen(key
), 0 };
547 size_t value_length
= strlen(value
);
548 const char *value2
= "change the value";
549 size_t value2_length
= strlen(value2
);
551 memcached_result_st results_obj
;
552 memcached_result_st
*results
;
555 rc
= memcached_flush(memc
, 0);
556 test_true(rc
== MEMCACHED_SUCCESS
);
558 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
560 rc
= memcached_set(memc
, key
, strlen(key
),
561 value
, strlen(value
),
562 (time_t)0, (uint32_t)0);
563 test_true(rc
== MEMCACHED_SUCCESS
);
565 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
567 results
= memcached_result_create(memc
, &results_obj
);
569 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
571 test_true(rc
== MEMCACHED_SUCCESS
);
572 test_true(memcached_result_cas(results
));
573 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
574 test_true(strlen(memcached_result_value(results
)) == value_length
);
575 test_true(rc
== MEMCACHED_SUCCESS
);
576 uint64_t cas
= memcached_result_cas(results
);
579 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
580 test_true(rc
== MEMCACHED_END
);
581 test_true(results
== NULL
);
584 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
585 test_true(rc
== MEMCACHED_SUCCESS
);
588 * The item will have a new cas value, so try to set it again with the old
589 * value. This should fail!
591 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
592 test_true(rc
== MEMCACHED_DATA_EXISTS
);
594 memcached_result_free(&results_obj
);
599 static test_return_t
prepend_test(memcached_st
*memc
)
601 memcached_return_t rc
;
602 const char *key
= "fig";
603 const char *value
= "people";
604 char *out_value
= NULL
;
608 rc
= memcached_flush(memc
, 0);
609 test_true(rc
== MEMCACHED_SUCCESS
);
611 rc
= memcached_set(memc
, key
, strlen(key
),
612 value
, strlen(value
),
613 (time_t)0, (uint32_t)0);
614 test_true(rc
== MEMCACHED_SUCCESS
);
616 rc
= memcached_prepend(memc
, key
, strlen(key
),
617 "the ", strlen("the "),
618 (time_t)0, (uint32_t)0);
619 test_true(rc
== MEMCACHED_SUCCESS
);
621 rc
= memcached_prepend(memc
, key
, strlen(key
),
622 "we ", strlen("we "),
623 (time_t)0, (uint32_t)0);
624 test_true(rc
== MEMCACHED_SUCCESS
);
626 out_value
= memcached_get(memc
, key
, strlen(key
),
627 &value_length
, &flags
, &rc
);
628 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
629 test_true(strlen("we the people") == value_length
);
630 test_true(rc
== MEMCACHED_SUCCESS
);
637 Set the value, then quit to make sure it is flushed.
638 Come back in and test that add fails.
640 static test_return_t
add_test(memcached_st
*memc
)
642 memcached_return_t rc
;
643 const char *key
= "foo";
644 const char *value
= "when we sanitize";
645 unsigned long long setting_value
;
647 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
649 rc
= memcached_set(memc
, key
, strlen(key
),
650 value
, strlen(value
),
651 (time_t)0, (uint32_t)0);
652 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
653 memcached_quit(memc
);
654 rc
= memcached_add(memc
, key
, strlen(key
),
655 value
, strlen(value
),
656 (time_t)0, (uint32_t)0);
658 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
661 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
665 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
672 ** There was a problem of leaking filedescriptors in the initial release
673 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
674 ** systems it seems that the kernel is slow on reclaiming the resources
675 ** because the connects starts to time out (the test doesn't do much
676 ** anyway, so just loop 10 iterations)
678 static test_return_t
add_wrapper(memcached_st
*memc
)
681 unsigned int max
= 10000;
689 for (x
= 0; x
< max
; x
++)
695 static test_return_t
replace_test(memcached_st
*memc
)
697 memcached_return_t rc
;
698 const char *key
= "foo";
699 const char *value
= "when we sanitize";
700 const char *original
= "first we insert some data";
702 rc
= memcached_set(memc
, key
, strlen(key
),
703 original
, strlen(original
),
704 (time_t)0, (uint32_t)0);
705 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
707 rc
= memcached_replace(memc
, key
, strlen(key
),
708 value
, strlen(value
),
709 (time_t)0, (uint32_t)0);
710 test_true(rc
== MEMCACHED_SUCCESS
);
715 static test_return_t
delete_test(memcached_st
*memc
)
717 memcached_return_t rc
;
718 const char *key
= "foo";
719 const char *value
= "when we sanitize";
721 rc
= memcached_set(memc
, key
, strlen(key
),
722 value
, strlen(value
),
723 (time_t)0, (uint32_t)0);
724 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
726 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
727 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
732 static test_return_t
flush_test(memcached_st
*memc
)
734 memcached_return_t rc
;
736 rc
= memcached_flush(memc
, 0);
737 test_true(rc
== MEMCACHED_SUCCESS
);
742 static memcached_return_t
server_function(const memcached_st
*ptr
__attribute__((unused
)),
743 const memcached_server_st
*server
__attribute__((unused
)),
744 void *context
__attribute__((unused
)))
748 return MEMCACHED_SUCCESS
;
751 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
754 strcpy(context
, "foo bad");
755 memcached_server_fn callbacks
[1];
757 callbacks
[0]= server_function
;
758 memcached_server_cursor(memc
, callbacks
, context
, 1);
762 static test_return_t
bad_key_test(memcached_st
*memc
)
764 memcached_return_t rc
;
765 const char *key
= "foo bad";
767 size_t string_length
;
769 memcached_st
*memc_clone
;
771 size_t max_keylen
= 0xffff;
773 // Just skip if we are in binary mode.
774 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
777 memc_clone
= memcached_clone(NULL
, memc
);
778 test_true(memc_clone
);
780 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
781 test_true(rc
== MEMCACHED_SUCCESS
);
783 /* All keys are valid in the binary protocol (except for length) */
784 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
786 string
= memcached_get(memc_clone
, key
, strlen(key
),
787 &string_length
, &flags
, &rc
);
788 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
789 test_true(string_length
== 0);
793 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
794 test_true(rc
== MEMCACHED_SUCCESS
);
795 string
= memcached_get(memc_clone
, key
, strlen(key
),
796 &string_length
, &flags
, &rc
);
797 test_true(rc
== MEMCACHED_NOTFOUND
);
798 test_true(string_length
== 0);
801 /* Test multi key for bad keys */
802 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
803 size_t key_lengths
[] = { 7, 7, 7 };
805 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
806 test_true(rc
== MEMCACHED_SUCCESS
);
808 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
809 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
811 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
812 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
816 /* The following test should be moved to the end of this function when the
817 memcached server is updated to allow max size length of the keys in the
820 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
821 test_true(rc
== MEMCACHED_SUCCESS
);
823 char *longkey
= malloc(max_keylen
+ 1);
826 memset(longkey
, 'a', max_keylen
+ 1);
827 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
828 &string_length
, &flags
, &rc
);
829 test_true(rc
== MEMCACHED_NOTFOUND
);
830 test_true(string_length
== 0);
833 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
834 &string_length
, &flags
, &rc
);
835 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
836 test_true(string_length
== 0);
843 /* Make sure zero length keys are marked as bad */
845 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
846 test_true(rc
== MEMCACHED_SUCCESS
);
847 string
= memcached_get(memc_clone
, key
, 0,
848 &string_length
, &flags
, &rc
);
849 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
850 test_true(string_length
== 0);
853 memcached_free(memc_clone
);
858 #define READ_THROUGH_VALUE "set for me"
859 static memcached_return_t
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
860 char *key
__attribute__((unused
)),
861 size_t key_length
__attribute__((unused
)),
862 memcached_result_st
*result
)
865 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
868 static test_return_t
read_through(memcached_st
*memc
)
870 memcached_return_t rc
;
871 const char *key
= "foo";
873 size_t string_length
;
875 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
877 string
= memcached_get(memc
, key
, strlen(key
),
878 &string_length
, &flags
, &rc
);
880 test_true(rc
== MEMCACHED_NOTFOUND
);
881 test_false(string_length
);
884 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
886 test_true(rc
== MEMCACHED_SUCCESS
);
888 string
= memcached_get(memc
, key
, strlen(key
),
889 &string_length
, &flags
, &rc
);
891 test_true(rc
== MEMCACHED_SUCCESS
);
892 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
893 test_strcmp(READ_THROUGH_VALUE
, string
);
896 string
= memcached_get(memc
, key
, strlen(key
),
897 &string_length
, &flags
, &rc
);
899 test_true(rc
== MEMCACHED_SUCCESS
);
900 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
901 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
907 static memcached_return_t
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
909 size_t key_length
__attribute__((unused
)))
913 return MEMCACHED_SUCCESS
;
916 static test_return_t
delete_through(memcached_st
*memc
)
918 memcached_trigger_delete_key_fn callback
;
919 memcached_return_t rc
;
921 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
923 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
924 test_true(rc
== MEMCACHED_SUCCESS
);
929 static test_return_t
get_test(memcached_st
*memc
)
931 memcached_return_t rc
;
932 const char *key
= "foo";
934 size_t string_length
;
937 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
938 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
940 string
= memcached_get(memc
, key
, strlen(key
),
941 &string_length
, &flags
, &rc
);
943 test_true(rc
== MEMCACHED_NOTFOUND
);
944 test_false(string_length
);
950 static test_return_t
get_test2(memcached_st
*memc
)
952 memcached_return_t rc
;
953 const char *key
= "foo";
954 const char *value
= "when we sanitize";
956 size_t string_length
;
959 rc
= memcached_set(memc
, key
, strlen(key
),
960 value
, strlen(value
),
961 (time_t)0, (uint32_t)0);
962 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
964 string
= memcached_get(memc
, key
, strlen(key
),
965 &string_length
, &flags
, &rc
);
968 test_true(rc
== MEMCACHED_SUCCESS
);
969 test_true(string_length
== strlen(value
));
970 test_true(!memcmp(string
, value
, string_length
));
977 static test_return_t
set_test2(memcached_st
*memc
)
979 memcached_return_t rc
;
980 const char *key
= "foo";
981 const char *value
= "train in the brain";
982 size_t value_length
= strlen(value
);
985 for (x
= 0; x
< 10; x
++)
987 rc
= memcached_set(memc
, key
, strlen(key
),
989 (time_t)0, (uint32_t)0);
990 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
996 static test_return_t
set_test3(memcached_st
*memc
)
998 memcached_return_t rc
;
1000 size_t value_length
= 8191;
1003 value
= (char*)malloc(value_length
);
1006 for (x
= 0; x
< value_length
; x
++)
1007 value
[x
] = (char) (x
% 127);
1009 /* The dump test relies on there being at least 32 items in memcached */
1010 for (x
= 0; x
< 32; x
++)
1014 sprintf(key
, "foo%u", x
);
1016 rc
= memcached_set(memc
, key
, strlen(key
),
1017 value
, value_length
,
1018 (time_t)0, (uint32_t)0);
1019 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1024 return TEST_SUCCESS
;
1027 static test_return_t
get_test3(memcached_st
*memc
)
1029 memcached_return_t rc
;
1030 const char *key
= "foo";
1032 size_t value_length
= 8191;
1034 size_t string_length
;
1038 value
= (char*)malloc(value_length
);
1041 for (x
= 0; x
< value_length
; x
++)
1042 value
[x
] = (char) (x
% 127);
1044 rc
= memcached_set(memc
, key
, strlen(key
),
1045 value
, value_length
,
1046 (time_t)0, (uint32_t)0);
1047 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1049 string
= memcached_get(memc
, key
, strlen(key
),
1050 &string_length
, &flags
, &rc
);
1052 test_true(rc
== MEMCACHED_SUCCESS
);
1054 test_true(string_length
== value_length
);
1055 test_true(!memcmp(string
, value
, string_length
));
1060 return TEST_SUCCESS
;
1063 static test_return_t
get_test4(memcached_st
*memc
)
1065 memcached_return_t rc
;
1066 const char *key
= "foo";
1068 size_t value_length
= 8191;
1070 size_t string_length
;
1074 value
= (char*)malloc(value_length
);
1077 for (x
= 0; x
< value_length
; x
++)
1078 value
[x
] = (char) (x
% 127);
1080 rc
= memcached_set(memc
, key
, strlen(key
),
1081 value
, value_length
,
1082 (time_t)0, (uint32_t)0);
1083 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1085 for (x
= 0; x
< 10; x
++)
1087 string
= memcached_get(memc
, key
, strlen(key
),
1088 &string_length
, &flags
, &rc
);
1090 test_true(rc
== MEMCACHED_SUCCESS
);
1092 test_true(string_length
== value_length
);
1093 test_true(!memcmp(string
, value
, string_length
));
1099 return TEST_SUCCESS
;
1103 * This test verifies that memcached_read_one_response doesn't try to
1104 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1105 * responses before you execute a storage command.
1107 static test_return_t
get_test5(memcached_st
*memc
)
1110 ** Request the same key twice, to ensure that we hash to the same server
1111 ** (so that we have multiple response values queued up) ;-)
1113 const char *keys
[]= { "key", "key" };
1114 size_t lengths
[]= { 3, 3 };
1118 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1119 keys
[0], lengths
[0], 0, 0);
1120 test_true(rc
== MEMCACHED_SUCCESS
);
1121 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1123 memcached_result_st results_obj
;
1124 memcached_result_st
*results
;
1125 results
=memcached_result_create(memc
, &results_obj
);
1127 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1129 memcached_result_free(&results_obj
);
1131 /* Don't read out the second result, but issue a set instead.. */
1132 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1133 test_true(rc
== MEMCACHED_SUCCESS
);
1135 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1136 &rlen
, &flags
, &rc
);
1137 test_true(val
== NULL
);
1138 test_true(rc
== MEMCACHED_NOTFOUND
);
1139 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1140 test_true(val
!= NULL
);
1141 test_true(rc
== MEMCACHED_SUCCESS
);
1144 return TEST_SUCCESS
;
1147 static test_return_t
mget_end(memcached_st
*memc
)
1149 const char *keys
[]= { "foo", "foo2" };
1150 size_t lengths
[]= { 3, 4 };
1151 const char *values
[]= { "fjord", "41" };
1153 memcached_return_t rc
;
1156 for (int i
= 0; i
< 2; i
++)
1158 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1159 (time_t)0, (uint32_t)0);
1160 test_true(rc
== MEMCACHED_SUCCESS
);
1164 size_t string_length
;
1167 // retrieve both via mget
1168 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1169 test_true(rc
== MEMCACHED_SUCCESS
);
1171 char key
[MEMCACHED_MAX_KEY
];
1174 // this should get both
1175 for (int i
= 0; i
< 2; i
++)
1177 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1179 test_true(rc
== MEMCACHED_SUCCESS
);
1181 if (key_length
== 4)
1183 test_true(string_length
== strlen(values
[val
]));
1184 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1188 // this should indicate end
1189 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1190 test_true(rc
== MEMCACHED_END
);
1193 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1194 test_true(rc
== MEMCACHED_SUCCESS
);
1196 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1197 test_true(key_length
== lengths
[0]);
1198 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1199 test_true(string_length
== strlen(values
[0]));
1200 test_true(strncmp(values
[0], string
, string_length
) == 0);
1201 test_true(rc
== MEMCACHED_SUCCESS
);
1204 // this should indicate end
1205 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1206 test_true(rc
== MEMCACHED_END
);
1208 return TEST_SUCCESS
;
1211 /* Do not copy the style of this code, I just access hosts to testthis function */
1212 static test_return_t
stats_servername_test(memcached_st
*memc
)
1214 memcached_return_t rc
;
1215 memcached_stat_st memc_stat
;
1216 memcached_server_instance_st instance
=
1217 memcached_server_instance_by_position(memc
, 0);
1219 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1220 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1221 return TEST_SKIPPED
;
1223 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1224 memcached_server_name(instance
),
1225 memcached_server_port(instance
));
1227 return TEST_SUCCESS
;
1230 static test_return_t
increment_test(memcached_st
*memc
)
1232 uint64_t new_number
;
1233 memcached_return_t rc
;
1234 const char *key
= "number";
1235 const char *value
= "0";
1237 rc
= memcached_set(memc
, key
, strlen(key
),
1238 value
, strlen(value
),
1239 (time_t)0, (uint32_t)0);
1240 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1242 rc
= memcached_increment(memc
, key
, strlen(key
),
1244 test_true(rc
== MEMCACHED_SUCCESS
);
1245 test_true(new_number
== 1);
1247 rc
= memcached_increment(memc
, key
, strlen(key
),
1249 test_true(rc
== MEMCACHED_SUCCESS
);
1250 test_true(new_number
== 2);
1252 return TEST_SUCCESS
;
1255 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1257 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1259 uint64_t new_number
;
1260 memcached_return_t rc
;
1261 const char *key
= "number";
1262 uint64_t initial
= 0;
1264 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1265 1, initial
, 0, &new_number
);
1266 test_true(rc
== MEMCACHED_SUCCESS
);
1267 test_true(new_number
== initial
);
1269 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1270 1, initial
, 0, &new_number
);
1271 test_true(rc
== MEMCACHED_SUCCESS
);
1272 test_true(new_number
== (initial
+ 1));
1274 return TEST_SUCCESS
;
1277 static test_return_t
decrement_test(memcached_st
*memc
)
1279 uint64_t new_number
;
1280 memcached_return_t rc
;
1281 const char *key
= "number";
1282 const char *value
= "3";
1284 rc
= memcached_set(memc
, key
, strlen(key
),
1285 value
, strlen(value
),
1286 (time_t)0, (uint32_t)0);
1287 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1289 rc
= memcached_decrement(memc
, key
, strlen(key
),
1291 test_true(rc
== MEMCACHED_SUCCESS
);
1292 test_true(new_number
== 2);
1294 rc
= memcached_decrement(memc
, key
, strlen(key
),
1296 test_true(rc
== MEMCACHED_SUCCESS
);
1297 test_true(new_number
== 1);
1299 return TEST_SUCCESS
;
1302 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1304 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1306 uint64_t new_number
;
1307 memcached_return_t rc
;
1308 const char *key
= "number";
1309 uint64_t initial
= 3;
1311 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1312 1, initial
, 0, &new_number
);
1313 test_true(rc
== MEMCACHED_SUCCESS
);
1314 test_true(new_number
== initial
);
1316 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1317 1, initial
, 0, &new_number
);
1318 test_true(rc
== MEMCACHED_SUCCESS
);
1319 test_true(new_number
== (initial
- 1));
1321 return TEST_SUCCESS
;
1324 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1326 uint64_t new_number
;
1327 memcached_return_t rc
;
1328 const char *master_key
= "foo";
1329 const char *key
= "number";
1330 const char *value
= "0";
1332 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1334 value
, strlen(value
),
1335 (time_t)0, (uint32_t)0);
1336 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1338 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1340 test_true(rc
== MEMCACHED_SUCCESS
);
1341 test_true(new_number
== 1);
1343 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1345 test_true(rc
== MEMCACHED_SUCCESS
);
1346 test_true(new_number
== 2);
1348 return TEST_SUCCESS
;
1351 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1353 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1355 uint64_t new_number
;
1356 memcached_return_t rc
;
1357 const char *master_key
= "foo";
1358 const char *key
= "number";
1359 uint64_t initial
= 0;
1361 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1363 1, initial
, 0, &new_number
);
1364 test_true(rc
== MEMCACHED_SUCCESS
);
1365 test_true(new_number
== initial
);
1367 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1369 1, initial
, 0, &new_number
);
1370 test_true(rc
== MEMCACHED_SUCCESS
);
1371 test_true(new_number
== (initial
+ 1));
1373 return TEST_SUCCESS
;
1376 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1378 uint64_t new_number
;
1379 memcached_return_t rc
;
1380 const char *master_key
= "foo";
1381 const char *key
= "number";
1382 const char *value
= "3";
1384 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1386 value
, strlen(value
),
1387 (time_t)0, (uint32_t)0);
1388 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1390 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1393 test_true(rc
== MEMCACHED_SUCCESS
);
1394 test_true(new_number
== 2);
1396 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1399 test_true(rc
== MEMCACHED_SUCCESS
);
1400 test_true(new_number
== 1);
1402 return TEST_SUCCESS
;
1405 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1407 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1409 uint64_t new_number
;
1410 memcached_return_t rc
;
1411 const char *master_key
= "foo";
1412 const char *key
= "number";
1413 uint64_t initial
= 3;
1415 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1417 1, initial
, 0, &new_number
);
1418 test_true(rc
== MEMCACHED_SUCCESS
);
1419 test_true(new_number
== initial
);
1421 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1423 1, initial
, 0, &new_number
);
1424 test_true(rc
== MEMCACHED_SUCCESS
);
1425 test_true(new_number
== (initial
- 1));
1427 return TEST_SUCCESS
;
1430 static test_return_t
quit_test(memcached_st
*memc
)
1432 memcached_return_t rc
;
1433 const char *key
= "fudge";
1434 const char *value
= "sanford and sun";
1436 rc
= memcached_set(memc
, key
, strlen(key
),
1437 value
, strlen(value
),
1438 (time_t)10, (uint32_t)3);
1439 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1440 memcached_quit(memc
);
1442 rc
= memcached_set(memc
, key
, strlen(key
),
1443 value
, strlen(value
),
1444 (time_t)50, (uint32_t)9);
1445 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1447 return TEST_SUCCESS
;
1450 static test_return_t
mget_result_test(memcached_st
*memc
)
1452 memcached_return_t rc
;
1453 const char *keys
[]= {"fudge", "son", "food"};
1454 size_t key_length
[]= {5, 3, 4};
1457 memcached_result_st results_obj
;
1458 memcached_result_st
*results
;
1460 results
= memcached_result_create(memc
, &results_obj
);
1462 test_true(&results_obj
== results
);
1464 /* We need to empty the server before continueing test */
1465 rc
= memcached_flush(memc
, 0);
1466 test_true(rc
== MEMCACHED_SUCCESS
);
1468 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1469 test_true(rc
== MEMCACHED_SUCCESS
);
1471 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1476 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1477 test_true(!results
);
1478 test_true(rc
== MEMCACHED_END
);
1480 for (x
= 0; x
< 3; x
++)
1482 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1483 keys
[x
], key_length
[x
],
1484 (time_t)50, (uint32_t)9);
1485 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1488 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1489 test_true(rc
== MEMCACHED_SUCCESS
);
1491 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1494 test_true(&results_obj
== results
);
1495 test_true(rc
== MEMCACHED_SUCCESS
);
1496 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1497 test_true(!memcmp(memcached_result_key_value(results
),
1498 memcached_result_value(results
),
1499 memcached_result_length(results
)));
1502 memcached_result_free(&results_obj
);
1504 return TEST_SUCCESS
;
1507 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1509 memcached_return_t rc
;
1510 const char *keys
[]= {"fudge", "son", "food"};
1511 size_t key_length
[]= {5, 3, 4};
1514 memcached_result_st
*results
;
1516 /* We need to empty the server before continueing test */
1517 rc
= memcached_flush(memc
, 0);
1518 test_true(rc
== MEMCACHED_SUCCESS
);
1520 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1521 test_true(rc
== MEMCACHED_SUCCESS
);
1523 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1527 test_true(!results
);
1528 test_true(rc
== MEMCACHED_END
);
1530 for (x
= 0; x
< 3; x
++)
1532 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1533 keys
[x
], key_length
[x
],
1534 (time_t)50, (uint32_t)9);
1535 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1538 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1539 test_true(rc
== MEMCACHED_SUCCESS
);
1542 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1545 test_true(rc
== MEMCACHED_SUCCESS
);
1546 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1547 test_true(!memcmp(memcached_result_key_value(results
),
1548 memcached_result_value(results
),
1549 memcached_result_length(results
)));
1550 memcached_result_free(results
);
1554 return TEST_SUCCESS
;
1557 /* Count the results */
1558 static memcached_return_t
callback_counter(const memcached_st
*ptr
__attribute__((unused
)),
1559 memcached_result_st
*result
__attribute__((unused
)),
1562 size_t *counter
= (size_t *)context
;
1564 *counter
= *counter
+ 1;
1566 return MEMCACHED_SUCCESS
;
1569 static test_return_t
mget_result_function(memcached_st
*memc
)
1571 memcached_return_t rc
;
1572 const char *keys
[]= {"fudge", "son", "food"};
1573 size_t key_length
[]= {5, 3, 4};
1576 memcached_execute_fn callbacks
[1];
1578 /* We need to empty the server before continueing test */
1579 rc
= memcached_flush(memc
, 0);
1580 for (x
= 0; x
< 3; x
++)
1582 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1583 keys
[x
], key_length
[x
],
1584 (time_t)50, (uint32_t)9);
1585 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1588 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1589 test_true(rc
== MEMCACHED_SUCCESS
);
1591 callbacks
[0]= &callback_counter
;
1593 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1595 test_true(counter
== 3);
1597 return TEST_SUCCESS
;
1600 static test_return_t
mget_test(memcached_st
*memc
)
1602 memcached_return_t rc
;
1603 const char *keys
[]= {"fudge", "son", "food"};
1604 size_t key_length
[]= {5, 3, 4};
1608 char return_key
[MEMCACHED_MAX_KEY
];
1609 size_t return_key_length
;
1611 size_t return_value_length
;
1613 /* We need to empty the server before continueing test */
1614 rc
= memcached_flush(memc
, 0);
1615 test_true(rc
== MEMCACHED_SUCCESS
);
1617 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1618 test_true(rc
== MEMCACHED_SUCCESS
);
1620 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1621 &return_value_length
, &flags
, &rc
)) != NULL
)
1623 test_true(return_value
);
1625 test_true(!return_value
);
1626 test_true(return_value_length
== 0);
1627 test_true(rc
== MEMCACHED_END
);
1629 for (x
= 0; x
< 3; x
++)
1631 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1632 keys
[x
], key_length
[x
],
1633 (time_t)50, (uint32_t)9);
1634 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1637 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1638 test_true(rc
== MEMCACHED_SUCCESS
);
1641 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1642 &return_value_length
, &flags
, &rc
)))
1644 test_true(return_value
);
1645 test_true(rc
== MEMCACHED_SUCCESS
);
1646 test_true(return_key_length
== return_value_length
);
1647 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1652 return TEST_SUCCESS
;
1655 static test_return_t
mget_execute(memcached_st
*memc
)
1659 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1663 * I only want to hit _one_ server so I know the number of requests I'm
1664 * sending in the pipeline.
1666 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1667 memc
->number_of_hosts
= 1;
1669 size_t max_keys
= binary
? 20480 : 1;
1672 char **keys
= calloc(max_keys
, sizeof(char*));
1673 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1675 /* First add all of the items.. */
1676 char blob
[1024] = {0};
1677 memcached_return_t rc
;
1678 for (size_t x
= 0; x
< max_keys
; ++x
)
1682 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
1684 test_true(keys
[x
] != NULL
);
1685 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1686 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1689 /* Try to get all of them with a large multiget */
1691 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1692 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1693 max_keys
, callbacks
, &counter
, 1);
1697 test_true(rc
== MEMCACHED_SUCCESS
);
1699 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1700 test_true(rc
== MEMCACHED_END
);
1702 /* Verify that we got all of the items */
1703 test_true(counter
== max_keys
);
1707 test_true(rc
== MEMCACHED_NOT_SUPPORTED
);
1708 test_true(counter
== 0);
1711 /* Release all allocated resources */
1712 for (size_t x
= 0; x
< max_keys
; ++x
)
1719 memc
->number_of_hosts
= number_of_hosts
;
1720 return TEST_SUCCESS
;
1723 static test_return_t
get_stats_keys(memcached_st
*memc
)
1727 memcached_stat_st memc_stat
;
1728 memcached_return_t rc
;
1730 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1731 test_true(rc
== MEMCACHED_SUCCESS
);
1732 for (ptr
= stat_list
; *ptr
; ptr
++)
1737 return TEST_SUCCESS
;
1740 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1742 const char *version_string
;
1744 version_string
= memcached_lib_version();
1746 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1748 return TEST_SUCCESS
;
1751 static test_return_t
get_stats(memcached_st
*memc
)
1755 memcached_return_t rc
;
1756 memcached_stat_st
*memc_stat
;
1758 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1759 test_true(rc
== MEMCACHED_SUCCESS
);
1761 test_true(rc
== MEMCACHED_SUCCESS
);
1762 test_true(memc_stat
);
1764 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1766 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1767 test_true(rc
== MEMCACHED_SUCCESS
);
1768 for (ptr
= stat_list
; *ptr
; ptr
++);
1773 memcached_stat_free(NULL
, memc_stat
);
1775 return TEST_SUCCESS
;
1778 static test_return_t
add_host_test(memcached_st
*memc
)
1781 memcached_server_st
*servers
;
1782 memcached_return_t rc
;
1783 char servername
[]= "0.example.com";
1785 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1787 test_true(1 == memcached_server_list_count(servers
));
1789 for (x
= 2; x
< 20; x
++)
1791 char buffer
[SMALL_STRING_LEN
];
1793 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1794 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1796 test_true(rc
== MEMCACHED_SUCCESS
);
1797 test_true(x
== memcached_server_list_count(servers
));
1800 rc
= memcached_server_push(memc
, servers
);
1801 test_true(rc
== MEMCACHED_SUCCESS
);
1802 rc
= memcached_server_push(memc
, servers
);
1803 test_true(rc
== MEMCACHED_SUCCESS
);
1805 memcached_server_list_free(servers
);
1807 return TEST_SUCCESS
;
1810 static memcached_return_t
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1812 return MEMCACHED_SUCCESS
;
1815 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1817 return MEMCACHED_SUCCESS
;
1820 static test_return_t
callback_test(memcached_st
*memc
)
1822 /* Test User Data */
1826 memcached_return_t rc
;
1828 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1829 test_true(rc
== MEMCACHED_SUCCESS
);
1830 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1831 test_true(*test_ptr
== x
);
1834 /* Test Clone Callback */
1836 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1837 void *clone_cb_ptr
= *(void **)&clone_cb
;
1838 void *temp_function
= NULL
;
1839 memcached_return_t rc
;
1841 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1843 test_true(rc
== MEMCACHED_SUCCESS
);
1844 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1845 test_true(temp_function
== clone_cb_ptr
);
1848 /* Test Cleanup Callback */
1850 memcached_cleanup_fn cleanup_cb
=
1851 (memcached_cleanup_fn
)cleanup_test_callback
;
1852 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1853 void *temp_function
= NULL
;
1854 memcached_return_t rc
;
1856 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1858 test_true(rc
== MEMCACHED_SUCCESS
);
1859 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1860 test_true(temp_function
== cleanup_cb_ptr
);
1863 return TEST_SUCCESS
;
1866 /* We don't test the behavior itself, we test the switches */
1867 static test_return_t
behavior_test(memcached_st
*memc
)
1872 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1873 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1874 test_true(value
== 1);
1876 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1877 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1878 test_true(value
== 1);
1880 set
= MEMCACHED_HASH_MD5
;
1881 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1882 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1883 test_true(value
== MEMCACHED_HASH_MD5
);
1887 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1888 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1889 test_true(value
== 0);
1891 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1892 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1893 test_true(value
== 0);
1895 set
= MEMCACHED_HASH_DEFAULT
;
1896 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1897 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1898 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1900 set
= MEMCACHED_HASH_CRC
;
1901 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1902 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1903 test_true(value
== MEMCACHED_HASH_CRC
);
1905 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1906 test_true(value
> 0);
1908 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1909 test_true(value
> 0);
1911 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1912 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1913 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1915 return TEST_SUCCESS
;
1918 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1920 memcached_return_t rc
;
1924 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
1925 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
1927 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
1929 if (rc
== MEMCACHED_SUCCESS
)
1931 test_true((bool)value
== set
);
1935 test_false((bool)value
== set
);
1938 return TEST_SUCCESS
;
1941 static test_return_t
fetch_all_results(memcached_st
*memc
)
1943 memcached_return_t rc
= MEMCACHED_SUCCESS
;
1944 char return_key
[MEMCACHED_MAX_KEY
];
1945 size_t return_key_length
;
1947 size_t return_value_length
;
1950 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1951 &return_value_length
, &flags
, &rc
)))
1953 test_true(return_value
);
1954 test_true(rc
== MEMCACHED_SUCCESS
);
1958 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
1961 /* Test case provided by Cal Haldenbrand */
1962 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
1964 unsigned int setter
= 1;
1966 unsigned long long total
= 0;
1969 char randomstuff
[6 * 1024];
1970 memcached_return_t rc
;
1972 memset(randomstuff
, 0, 6 * 1024);
1974 /* We just keep looking at the same values over and over */
1977 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1978 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1982 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
1986 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1987 memset(randomstuff
, 0, 6 * 1024);
1988 test_true(size
< 6 * 1024); /* Being safe here */
1990 for (j
= 0 ; j
< size
;j
++)
1991 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1994 snprintf(key
, sizeof(key
), "%u", x
);
1995 rc
= memcached_set(memc
, key
, strlen(key
),
1996 randomstuff
, strlen(randomstuff
), 10, 0);
1997 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1998 /* If we fail, lets try again */
1999 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2000 rc
= memcached_set(memc
, key
, strlen(key
),
2001 randomstuff
, strlen(randomstuff
), 10, 0);
2002 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2005 return TEST_SUCCESS
;
2008 /* Test case provided by Cal Haldenbrand */
2009 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2011 unsigned int setter
;
2015 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2016 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2018 setter
= 20 * 1024576;
2019 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2020 setter
= 20 * 1024576;
2021 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2022 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2023 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2025 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2028 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2030 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2031 char buffer
[SMALL_STRING_LEN
];
2036 memset(buffer
, 0, SMALL_STRING_LEN
);
2038 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2039 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2040 &val_len
, &flags
, &rc
);
2041 if (rc
!= MEMCACHED_SUCCESS
)
2043 if (rc
== MEMCACHED_NOTFOUND
)
2057 return TEST_SUCCESS
;
2060 /* Do a large mget() over all the keys we think exist */
2061 #define KEY_COUNT 3000 // * 1024576
2062 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2064 memcached_return_t rc
;
2065 unsigned int setter
;
2068 size_t key_lengths
[KEY_COUNT
];
2071 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2072 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2074 setter
= 20 * 1024576;
2075 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2076 setter
= 20 * 1024576;
2077 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2078 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2079 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2082 keys
= calloc(KEY_COUNT
, sizeof(char *));
2084 for (x
= 0; x
< KEY_COUNT
; x
++)
2088 snprintf(buffer
, 30, "%u", x
);
2089 keys
[x
]= strdup(buffer
);
2090 key_lengths
[x
]= strlen(keys
[x
]);
2093 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2094 test_true(rc
== MEMCACHED_SUCCESS
);
2096 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2098 for (x
= 0; x
< KEY_COUNT
; x
++)
2102 return TEST_SUCCESS
;
2105 /* Make sure we behave properly if server list has no values */
2106 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2108 memcached_return_t rc
;
2109 const char *keys
[]= {"fudge", "son", "food"};
2110 size_t key_length
[]= {5, 3, 4};
2113 char return_key
[MEMCACHED_MAX_KEY
];
2114 size_t return_key_length
;
2116 size_t return_value_length
;
2118 /* Here we free everything before running a bunch of mget tests */
2119 memcached_servers_reset(memc
);
2122 /* We need to empty the server before continueing test */
2123 rc
= memcached_flush(memc
, 0);
2124 test_true(rc
== MEMCACHED_NO_SERVERS
);
2126 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2127 test_true(rc
== MEMCACHED_NO_SERVERS
);
2129 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2130 &return_value_length
, &flags
, &rc
)) != NULL
)
2132 test_true(return_value
);
2134 test_true(!return_value
);
2135 test_true(return_value_length
== 0);
2136 test_true(rc
== MEMCACHED_NO_SERVERS
);
2138 for (x
= 0; x
< 3; x
++)
2140 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2141 keys
[x
], key_length
[x
],
2142 (time_t)50, (uint32_t)9);
2143 test_true(rc
== MEMCACHED_NO_SERVERS
);
2146 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2147 test_true(rc
== MEMCACHED_NO_SERVERS
);
2150 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2151 &return_value_length
, &flags
, &rc
)))
2153 test_true(return_value
);
2154 test_true(rc
== MEMCACHED_SUCCESS
);
2155 test_true(return_key_length
== return_value_length
);
2156 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2161 return TEST_SUCCESS
;
2164 #define VALUE_SIZE_BUG5 1048064
2165 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2167 memcached_return_t rc
;
2168 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2169 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2170 char return_key
[MEMCACHED_MAX_KEY
];
2171 size_t return_key_length
;
2173 size_t value_length
;
2177 char insert_data
[VALUE_SIZE_BUG5
];
2179 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2180 insert_data
[x
]= (signed char)rand();
2182 memcached_flush(memc
, 0);
2183 value
= memcached_get(memc
, keys
[0], key_length
[0],
2184 &value_length
, &flags
, &rc
);
2185 test_true(value
== NULL
);
2186 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2189 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2190 &value_length
, &flags
, &rc
)))
2192 test_true(count
== 0);
2194 for (x
= 0; x
< 4; x
++)
2196 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2197 insert_data
, VALUE_SIZE_BUG5
,
2198 (time_t)0, (uint32_t)0);
2199 test_true(rc
== MEMCACHED_SUCCESS
);
2202 for (x
= 0; x
< 10; x
++)
2204 value
= memcached_get(memc
, keys
[0], key_length
[0],
2205 &value_length
, &flags
, &rc
);
2209 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2211 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2212 &value_length
, &flags
, &rc
)))
2217 test_true(count
== 4);
2220 return TEST_SUCCESS
;
2223 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2225 memcached_return_t rc
;
2226 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2227 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2228 char return_key
[MEMCACHED_MAX_KEY
];
2229 size_t return_key_length
;
2231 size_t value_length
;
2235 char insert_data
[VALUE_SIZE_BUG5
];
2237 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2238 insert_data
[x
]= (signed char)rand();
2240 memcached_flush(memc
, 0);
2241 value
= memcached_get(memc
, keys
[0], key_length
[0],
2242 &value_length
, &flags
, &rc
);
2243 test_true(value
== NULL
);
2244 test_true(rc
== MEMCACHED_NOTFOUND
);
2245 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2246 test_true(rc
== MEMCACHED_SUCCESS
);
2249 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2250 &value_length
, &flags
, &rc
)))
2252 test_true(count
== 0);
2253 test_true(rc
== MEMCACHED_END
);
2255 for (x
= 0; x
< 4; x
++)
2257 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2258 insert_data
, VALUE_SIZE_BUG5
,
2259 (time_t)0, (uint32_t)0);
2260 test_true(rc
== MEMCACHED_SUCCESS
);
2263 for (x
= 0; x
< 2; x
++)
2265 value
= memcached_get(memc
, keys
[0], key_length
[0],
2266 &value_length
, &flags
, &rc
);
2270 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2271 test_true(rc
== MEMCACHED_SUCCESS
);
2273 /* We test for purge of partial complete fetches */
2274 for (count
= 3; count
; count
--)
2276 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2277 &value_length
, &flags
, &rc
);
2278 test_true(rc
== MEMCACHED_SUCCESS
);
2279 test_true(!(memcmp(value
, insert_data
, value_length
)));
2280 test_true(value_length
);
2285 return TEST_SUCCESS
;
2288 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2290 memcached_return_t rc
;
2292 memcached_st
*memc_clone
;
2294 memcached_server_st
*servers
;
2295 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";
2297 servers
= memcached_servers_parse(server_list
);
2300 mine
= memcached_create(NULL
);
2301 rc
= memcached_server_push(mine
, servers
);
2302 test_true(rc
== MEMCACHED_SUCCESS
);
2303 memcached_server_list_free(servers
);
2306 memc_clone
= memcached_clone(NULL
, mine
);
2308 memcached_quit(mine
);
2309 memcached_quit(memc_clone
);
2312 memcached_free(mine
);
2313 memcached_free(memc_clone
);
2315 return TEST_SUCCESS
;
2318 /* Test flag store/retrieve */
2319 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2321 memcached_return_t rc
;
2322 const char *keys
= "036790384900";
2323 size_t key_length
= strlen(keys
);
2324 char return_key
[MEMCACHED_MAX_KEY
];
2325 size_t return_key_length
;
2327 size_t value_length
;
2330 char insert_data
[VALUE_SIZE_BUG5
];
2332 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2333 insert_data
[x
]= (signed char)rand();
2335 memcached_flush(memc
, 0);
2338 rc
= memcached_set(memc
, keys
, key_length
,
2339 insert_data
, VALUE_SIZE_BUG5
,
2341 test_true(rc
== MEMCACHED_SUCCESS
);
2344 value
= memcached_get(memc
, keys
, key_length
,
2345 &value_length
, &flags
, &rc
);
2346 test_true(flags
== 245);
2350 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2353 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2354 &value_length
, &flags
, &rc
);
2355 test_true(flags
== 245);
2360 return TEST_SUCCESS
;
2363 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2365 memcached_return_t rc
;
2366 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2367 size_t key_length
[3];
2372 char return_key
[MEMCACHED_MAX_KEY
];
2373 size_t return_key_length
;
2375 size_t return_value_length
;
2378 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2379 key_length
[1]= strlen("fudge&*@#");
2380 key_length
[2]= strlen("for^#@&$not");
2383 for (x
= 0; x
< 3; x
++)
2385 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2386 keys
[x
], key_length
[x
],
2387 (time_t)50, (uint32_t)9);
2388 test_true(rc
== MEMCACHED_SUCCESS
);
2391 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2392 test_true(rc
== MEMCACHED_SUCCESS
);
2394 /* We need to empty the server before continueing test */
2395 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2396 &return_value_length
, &flags
, &rc
)) != NULL
)
2398 test_true(return_value
);
2402 test_true(count
== 3);
2404 return TEST_SUCCESS
;
2407 /* We are testing with aggressive timeout to get failures */
2408 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2410 const char *key
= "foo";
2412 size_t value_length
= 512;
2415 memcached_return_t rc
;
2416 unsigned int set
= 1;
2417 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2420 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2421 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2423 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2426 value
= (char*)malloc(value_length
* sizeof(char));
2428 for (x
= 0; x
< value_length
; x
++)
2429 value
[x
]= (char) (x
% 127);
2431 for (x
= 1; x
<= 100000; ++x
)
2433 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2435 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2436 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2438 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2443 memcached_free(mclone
);
2445 return TEST_SUCCESS
;
2449 We are looking failures in the async protocol
2451 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2453 const char *key
= "foo";
2455 size_t value_length
= 512;
2458 memcached_return_t rc
;
2459 unsigned int set
= 1;
2461 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2463 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2464 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2466 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2469 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2471 test_true(timeout
== -1);
2473 value
= (char*)malloc(value_length
* sizeof(char));
2475 for (x
= 0; x
< value_length
; x
++)
2476 value
[x
]= (char) (x
% 127);
2478 for (x
= 1; x
<= 100000; ++x
)
2480 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2484 memcached_free(mclone
);
2486 return TEST_SUCCESS
;
2490 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2492 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2494 memcached_return_t rc
;
2496 size_t value_length
;
2498 uint64_t number_value
;
2500 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2501 &value_length
, &flags
, &rc
);
2502 test_true(value
== NULL
);
2503 test_true(rc
== MEMCACHED_NOTFOUND
);
2505 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2508 test_true(value
== NULL
);
2509 /* The binary protocol will set the key if it doesn't exist */
2510 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2512 test_true(rc
== MEMCACHED_SUCCESS
);
2516 test_true(rc
== MEMCACHED_NOTFOUND
);
2519 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2521 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2522 &value_length
, &flags
, &rc
);
2524 test_true(rc
== MEMCACHED_SUCCESS
);
2527 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2529 test_true(number_value
== 2);
2530 test_true(rc
== MEMCACHED_SUCCESS
);
2532 return TEST_SUCCESS
;
2536 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2537 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2539 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2541 char key
[] = "key34567890";
2543 memcached_return_t rc
;
2544 size_t overflowSize
;
2546 char commandFirst
[]= "set key34567890 0 0 ";
2547 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2548 size_t commandLength
;
2551 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2553 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2555 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2557 overflow
= malloc(testSize
);
2558 test_true(overflow
!= NULL
);
2560 memset(overflow
, 'x', testSize
);
2561 rc
= memcached_set(memc
, key
, strlen(key
),
2562 overflow
, testSize
, 0, 0);
2563 test_true(rc
== MEMCACHED_SUCCESS
);
2567 return TEST_SUCCESS
;
2572 Test values of many different sizes
2573 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2574 set key34567890 0 0 8169 \r\n
2575 is sent followed by buffer of size 8169, followed by 8169
2577 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2580 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2581 memcached_return_t rc
;
2582 const char *key
= "foo";
2584 size_t value_length
= 18000;
2586 size_t string_length
;
2589 size_t current_length
;
2591 value
= (char*)malloc(value_length
);
2594 for (x
= 0; x
< value_length
; x
++)
2595 value
[x
] = (char) (x
% 127);
2597 for (current_length
= 0; current_length
< value_length
; current_length
++)
2599 rc
= memcached_set(memc
, key
, strlen(key
),
2600 value
, current_length
,
2601 (time_t)0, (uint32_t)0);
2602 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2604 string
= memcached_get(memc
, key
, strlen(key
),
2605 &string_length
, &flags
, &rc
);
2607 test_true(rc
== MEMCACHED_SUCCESS
);
2608 test_true(string_length
== current_length
);
2609 test_true(!memcmp(string
, value
, string_length
));
2616 return TEST_SUCCESS
;
2620 Look for zero length value problems
2622 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2625 memcached_return_t rc
;
2626 const char *key
= "mykey";
2631 for (x
= 0; x
< 2; x
++)
2633 rc
= memcached_set(memc
, key
, strlen(key
),
2635 (time_t)0, (uint32_t)0);
2637 test_true(rc
== MEMCACHED_SUCCESS
);
2639 value
= memcached_get(memc
, key
, strlen(key
),
2640 &length
, &flags
, &rc
);
2642 test_true(rc
== MEMCACHED_SUCCESS
);
2643 test_true(value
== NULL
);
2644 test_true(length
== 0);
2645 test_true(flags
== 0);
2647 value
= memcached_get(memc
, key
, strlen(key
),
2648 &length
, &flags
, &rc
);
2650 test_true(rc
== MEMCACHED_SUCCESS
);
2651 test_true(value
== NULL
);
2652 test_true(length
== 0);
2653 test_true(flags
== 0);
2656 return TEST_SUCCESS
;
2659 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2660 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2662 memcached_return_t rc
;
2663 const char *key
= "mykey";
2668 rc
= memcached_set(memc
, key
, strlen(key
),
2670 (time_t)0, UINT32_MAX
);
2672 test_true(rc
== MEMCACHED_SUCCESS
);
2674 value
= memcached_get(memc
, key
, strlen(key
),
2675 &length
, &flags
, &rc
);
2677 test_true(rc
== MEMCACHED_SUCCESS
);
2678 test_true(value
== NULL
);
2679 test_true(length
== 0);
2680 test_true(flags
== UINT32_MAX
);
2682 return TEST_SUCCESS
;
2686 /* Check the validity of chinese key*/
2687 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2689 memcached_return_t rc
;
2690 const char *key
= "豆瓣";
2691 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2696 rc
= memcached_set(memc
, key
, strlen(key
),
2697 value
, strlen(value
),
2700 test_true(rc
== MEMCACHED_SUCCESS
);
2702 value2
= memcached_get(memc
, key
, strlen(key
),
2703 &length
, &flags
, &rc
);
2705 test_true(length
==strlen(value
));
2706 test_true(rc
== MEMCACHED_SUCCESS
);
2707 test_true(memcmp(value
, value2
, length
)==0);
2710 return TEST_SUCCESS
;
2718 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2721 const memcached_server_st
*server
;
2722 memcached_return_t res
;
2726 memc
= memcached_create(NULL
);
2727 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2728 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2730 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2732 return TEST_SUCCESS
;
2735 /* CAS test from Andei */
2736 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2738 memcached_return_t status
;
2739 memcached_result_st
*result
, result_obj
;
2740 const char *key
= "abc";
2741 size_t key_len
= strlen("abc");
2742 const char *value
= "foobar";
2743 size_t value_len
= strlen(value
);
2745 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2747 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2748 test_true(status
== MEMCACHED_SUCCESS
);
2750 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2751 test_true(status
== MEMCACHED_SUCCESS
);
2753 result
= memcached_result_create(memc
, &result_obj
);
2756 memcached_result_create(memc
, &result_obj
);
2757 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2760 test_true(status
== MEMCACHED_SUCCESS
);
2762 memcached_result_free(result
);
2764 return TEST_SUCCESS
;
2767 #include "ketama_test_cases.h"
2768 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2770 memcached_return_t rc
;
2773 memcached_server_st
*server_pool
;
2778 memc
= memcached_create(NULL
);
2781 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2782 test_true(rc
== MEMCACHED_SUCCESS
);
2784 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2785 test_true(value
== 1);
2787 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2788 test_true(rc
== MEMCACHED_SUCCESS
);
2790 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2791 test_true(value
== MEMCACHED_HASH_MD5
);
2793 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");
2794 memcached_server_push(memc
, server_pool
);
2796 /* verify that the server list was parsed okay. */
2797 test_true(memcached_server_count(memc
) == 8);
2798 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2799 test_true(server_pool
[0].port
== 11211);
2800 test_true(server_pool
[0].weight
== 600);
2801 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2802 test_true(server_pool
[2].port
== 11211);
2803 test_true(server_pool
[2].weight
== 200);
2804 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2805 test_true(server_pool
[7].port
== 11211);
2806 test_true(server_pool
[7].weight
== 100);
2808 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2809 * us test the boundary wraparound.
2811 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2813 /* verify the standard ketama set. */
2814 for (x
= 0; x
< 99; x
++)
2816 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2818 memcached_server_instance_st instance
=
2819 memcached_server_instance_by_position(memc
, server_idx
);
2821 const char *hostname
= memcached_server_name(instance
);
2822 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2825 memcached_server_list_free(server_pool
);
2826 memcached_free(memc
);
2828 return TEST_SUCCESS
;
2831 /* Large mget() of missing keys with binary proto
2833 * If many binary quiet commands (such as getq's in an mget) fill the output
2834 * buffer and the server chooses not to respond, memcached_flush hangs. See
2835 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2838 /* sighandler_t function that always asserts false */
2839 static void fail(int unused
__attribute__((unused
)))
2845 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2847 memcached_return_t rc
;
2850 size_t* key_lengths
;
2851 void (*oldalarm
)(int);
2852 memcached_st
*memc_clone
;
2854 memc_clone
= memcached_clone(NULL
, memc
);
2855 test_true(memc_clone
);
2857 /* only binproto uses getq for mget */
2858 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2860 /* empty the cache to ensure misses (hence non-responses) */
2861 rc
= memcached_flush(memc_clone
, 0);
2862 test_true(rc
== MEMCACHED_SUCCESS
);
2864 key_lengths
= calloc(key_count
, sizeof(size_t));
2865 keys
= calloc(key_count
, sizeof(char *));
2867 for (x
= 0; x
< key_count
; x
++)
2871 snprintf(buffer
, 30, "%u", x
);
2872 keys
[x
]= strdup(buffer
);
2873 key_lengths
[x
]= strlen(keys
[x
]);
2876 oldalarm
= signal(SIGALRM
, fail
);
2879 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2880 test_true(rc
== MEMCACHED_SUCCESS
);
2883 signal(SIGALRM
, oldalarm
);
2885 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2887 for (x
= 0; x
< key_count
; x
++)
2892 memcached_free(memc_clone
);
2894 return TEST_SUCCESS
;
2897 static test_return_t
pre_binary(memcached_st
*memc
);
2899 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
2901 test_return_t test_rc
;
2902 test_rc
= pre_binary(memc
);
2904 if (test_rc
!= TEST_SUCCESS
)
2909 /* should work as of r580 */
2910 rc
= _user_supplied_bug21(memc
, 10);
2911 test_true(rc
== TEST_SUCCESS
);
2913 /* should fail as of r580 */
2914 rc
= _user_supplied_bug21(memc
, 1000);
2915 test_true(rc
== TEST_SUCCESS
);
2917 return TEST_SUCCESS
;
2920 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
2923 memcached_server_instance_st instance
;
2925 memcached_return_t rc
;
2926 memcached_st
*memc
= memcached_create(NULL
);
2929 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2930 test_true(rc
== MEMCACHED_SUCCESS
);
2932 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2933 test_true(value
== 1);
2935 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2936 test_true(rc
== MEMCACHED_SUCCESS
);
2938 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2939 test_true(value
== MEMCACHED_HASH_MD5
);
2941 /* server should be removed when in delay */
2942 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2943 test_true(rc
== MEMCACHED_SUCCESS
);
2945 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2946 test_true(value
== 1);
2948 memcached_server_st
*server_pool
;
2949 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");
2950 memcached_server_push(memc
, server_pool
);
2952 /* verify that the server list was parsed okay. */
2953 test_true(memcached_server_count(memc
) == 8);
2954 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2955 test_true(server_pool
[0].port
== 11211);
2956 test_true(server_pool
[0].weight
== 600);
2957 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2958 test_true(server_pool
[2].port
== 11211);
2959 test_true(server_pool
[2].weight
== 200);
2960 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2961 test_true(server_pool
[7].port
== 11211);
2962 test_true(server_pool
[7].weight
== 100);
2964 instance
= memcached_server_instance_by_position(memc
, 2);
2965 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
2966 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2969 This would not work if there were only two hosts.
2971 for (size_t x
= 0; x
< 99; x
++)
2973 memcached_autoeject(memc
);
2974 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2975 test_true(server_idx
!= 2);
2978 /* and re-added when it's back. */
2979 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
2980 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2981 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
2982 memc
->distribution
);
2983 for (size_t x
= 0; x
< 99; x
++)
2985 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2986 // We re-use instance from above.
2988 memcached_server_instance_by_position(memc
, server_idx
);
2989 const char *hostname
= memcached_server_name(instance
);
2990 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
2993 memcached_server_list_free(server_pool
);
2994 memcached_free(memc
);
2996 return TEST_SUCCESS
;
2999 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3003 memcached_return_t rc
;
3004 memcached_st
*memc
= memcached_create(NULL
);
3008 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3009 test_true(rc
== MEMCACHED_SUCCESS
);
3011 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3012 test_true(value
== 1);
3014 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3015 test_true(rc
== MEMCACHED_SUCCESS
);
3017 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3018 test_true(value
== MEMCACHED_HASH_MD5
);
3021 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3023 memcached_server_st
*server_pool
;
3024 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");
3025 memcached_server_push(memc
, server_pool
);
3027 // @todo this needs to be refactored to actually test something.
3030 if ((fp
= fopen("ketama_keys.txt", "w")))
3034 printf("cannot write to file ketama_keys.txt");
3035 return TEST_FAILURE
;
3038 for (int x
= 0; x
< 10000; x
++)
3041 sprintf(key
, "%d", x
);
3043 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3044 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3045 in_port_t port
= memc
->hosts
[server_idx
].port
;
3046 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3050 memcached_server_list_free(server_pool
);
3051 memcached_free(memc
);
3053 return TEST_SUCCESS
;
3057 static test_return_t
result_static(memcached_st
*memc
)
3059 memcached_result_st result
;
3060 memcached_result_st
*result_ptr
;
3062 result_ptr
= memcached_result_create(memc
, &result
);
3063 test_true(result
.options
.is_allocated
== false);
3064 test_true(memcached_is_initialized(&result
) == true);
3065 test_true(result_ptr
);
3066 test_true(result_ptr
== &result
);
3068 memcached_result_free(&result
);
3070 test_true(result
.options
.is_allocated
== false);
3071 test_true(memcached_is_initialized(&result
) == false);
3073 return TEST_SUCCESS
;
3076 static test_return_t
result_alloc(memcached_st
*memc
)
3078 memcached_result_st
*result_ptr
;
3080 result_ptr
= memcached_result_create(memc
, NULL
);
3081 test_true(result_ptr
);
3082 test_true(result_ptr
->options
.is_allocated
== true);
3083 test_true(memcached_is_initialized(result_ptr
) == true);
3084 memcached_result_free(result_ptr
);
3086 return TEST_SUCCESS
;
3089 static test_return_t
string_static_null(memcached_st
*memc
)
3091 memcached_string_st string
;
3092 memcached_string_st
*string_ptr
;
3094 string_ptr
= memcached_string_create(memc
, &string
, 0);
3095 test_true(string
.options
.is_initialized
== true);
3096 test_true(string_ptr
);
3098 /* The following two better be the same! */
3099 test_true(memcached_is_allocated(string_ptr
) == false);
3100 test_true(memcached_is_allocated(&string
) == false);
3101 test_true(&string
== string_ptr
);
3103 test_true(string
.options
.is_initialized
== true);
3104 test_true(memcached_is_initialized(&string
) == true);
3105 memcached_string_free(&string
);
3106 test_true(memcached_is_initialized(&string
) == false);
3108 return TEST_SUCCESS
;
3111 static test_return_t
string_alloc_null(memcached_st
*memc
)
3113 memcached_string_st
*string
;
3115 string
= memcached_string_create(memc
, NULL
, 0);
3117 test_true(memcached_is_allocated(string
) == true);
3118 test_true(memcached_is_initialized(string
) == true);
3119 memcached_string_free(string
);
3121 return TEST_SUCCESS
;
3124 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3126 memcached_string_st
*string
;
3128 string
= memcached_string_create(memc
, NULL
, 1024);
3130 test_true(memcached_is_allocated(string
) == true);
3131 test_true(memcached_is_initialized(string
) == true);
3132 memcached_string_free(string
);
3134 return TEST_SUCCESS
;
3137 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3139 memcached_string_st
*string
;
3141 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3142 test_true(string
== NULL
);
3144 return TEST_SUCCESS
;
3147 static test_return_t
string_alloc_append(memcached_st
*memc
)
3150 char buffer
[SMALL_STRING_LEN
];
3151 memcached_string_st
*string
;
3153 /* Ring the bell! */
3154 memset(buffer
, 6, SMALL_STRING_LEN
);
3156 string
= memcached_string_create(memc
, NULL
, 100);
3158 test_true(memcached_is_allocated(string
) == true);
3159 test_true(memcached_is_initialized(string
) == true);
3161 for (x
= 0; x
< 1024; x
++)
3163 memcached_return_t rc
;
3164 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3165 test_true(rc
== MEMCACHED_SUCCESS
);
3167 test_true(memcached_is_allocated(string
) == true);
3168 memcached_string_free(string
);
3170 return TEST_SUCCESS
;
3173 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3175 memcached_return_t rc
;
3177 char buffer
[SMALL_STRING_LEN
];
3178 memcached_string_st
*string
;
3180 /* Ring the bell! */
3181 memset(buffer
, 6, SMALL_STRING_LEN
);
3183 string
= memcached_string_create(memc
, NULL
, 100);
3185 test_true(memcached_is_allocated(string
) == true);
3186 test_true(memcached_is_initialized(string
) == true);
3188 for (x
= 0; x
< 1024; x
++)
3190 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3191 test_true(rc
== MEMCACHED_SUCCESS
);
3193 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3194 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3195 test_true(memcached_is_allocated(string
) == true);
3196 memcached_string_free(string
);
3198 return TEST_SUCCESS
;
3201 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3203 pairs_free(global_pairs
);
3205 return TEST_SUCCESS
;
3208 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3210 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3211 global_count
= GLOBAL_COUNT
;
3213 for (size_t x
= 0; x
< global_count
; x
++)
3215 global_keys
[x
]= global_pairs
[x
].key
;
3216 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3219 return TEST_SUCCESS
;
3222 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3224 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3225 global_count
= GLOBAL2_COUNT
;
3227 for (size_t x
= 0; x
< global_count
; x
++)
3229 global_keys
[x
]= global_pairs
[x
].key
;
3230 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3233 return TEST_SUCCESS
;
3236 static test_return_t
generate_data(memcached_st
*memc
)
3238 execute_set(memc
, global_pairs
, global_count
);
3240 return TEST_SUCCESS
;
3243 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3245 memcached_stat_st
*stat_p
;
3246 memcached_return_t rc
;
3247 uint32_t host_index
= 0;
3248 execute_set(memc
, global_pairs
, global_count
);
3250 //TODO: hosts used size stats
3251 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3254 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3256 /* This test was changes so that "make test" would work properlly */
3258 memcached_server_instance_st instance
=
3259 memcached_server_instance_by_position(memc
, host_index
);
3261 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3263 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3266 memcached_stat_free(NULL
, stat_p
);
3268 return TEST_SUCCESS
;
3270 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3275 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3276 generate_data(memc
);
3278 return TEST_SUCCESS
;
3281 static test_return_t
get_read_count(memcached_st
*memc
)
3283 memcached_return_t rc
;
3284 memcached_st
*memc_clone
;
3286 memc_clone
= memcached_clone(NULL
, memc
);
3287 test_true(memc_clone
);
3289 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3293 size_t return_value_length
;
3297 for (size_t x
= count
= 0; x
< global_count
; x
++)
3299 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3300 &return_value_length
, &flags
, &rc
);
3301 if (rc
== MEMCACHED_SUCCESS
)
3310 memcached_free(memc_clone
);
3312 return TEST_SUCCESS
;
3315 static test_return_t
get_read(memcached_st
*memc
)
3317 memcached_return_t rc
;
3321 size_t return_value_length
;
3324 for (size_t x
= 0; x
< global_count
; x
++)
3326 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3327 &return_value_length
, &flags
, &rc
);
3329 test_true(return_value);
3330 test_true(rc == MEMCACHED_SUCCESS);
3332 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3337 return TEST_SUCCESS
;
3340 static test_return_t
mget_read(memcached_st
*memc
)
3342 memcached_return_t rc
;
3344 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3345 test_true(rc
== MEMCACHED_SUCCESS
);
3346 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
3348 return TEST_SUCCESS
;
3351 static test_return_t
mget_read_result(memcached_st
*memc
)
3353 memcached_return_t rc
;
3355 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3356 test_true(rc
== MEMCACHED_SUCCESS
);
3357 /* Turn this into a help function */
3359 memcached_result_st results_obj
;
3360 memcached_result_st
*results
;
3362 results
= memcached_result_create(memc
, &results_obj
);
3364 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3367 test_true(rc
== MEMCACHED_SUCCESS
);
3370 memcached_result_free(&results_obj
);
3373 return TEST_SUCCESS
;
3376 static test_return_t
mget_read_function(memcached_st
*memc
)
3378 memcached_return_t rc
;
3380 memcached_execute_fn callbacks
[1];
3382 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3383 test_true(rc
== MEMCACHED_SUCCESS
);
3385 callbacks
[0]= &callback_counter
;
3387 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3389 return TEST_SUCCESS
;
3392 static test_return_t
delete_generate(memcached_st
*memc
)
3394 for (size_t x
= 0; x
< global_count
; x
++)
3396 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3399 return TEST_SUCCESS
;
3402 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3407 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3409 for (size_t x
= 0; x
< global_count
; x
++)
3411 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3414 return TEST_SUCCESS
;
3417 static test_return_t
add_host_test1(memcached_st
*memc
)
3419 memcached_return_t rc
;
3420 char servername
[]= "0.example.com";
3421 memcached_server_st
*servers
;
3423 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3425 test_true(1 == memcached_server_list_count(servers
));
3427 for (size_t x
= 2; x
< 20; x
++)
3429 char buffer
[SMALL_STRING_LEN
];
3431 snprintf(buffer
, SMALL_STRING_LEN
, "%zu.example.com", 400+x
);
3432 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3434 test_true(rc
== MEMCACHED_SUCCESS
);
3435 test_true(x
== memcached_server_list_count(servers
));
3438 rc
= memcached_server_push(memc
, servers
);
3439 test_true(rc
== MEMCACHED_SUCCESS
);
3440 rc
= memcached_server_push(memc
, servers
);
3441 test_true(rc
== MEMCACHED_SUCCESS
);
3443 memcached_server_list_free(servers
);
3445 return TEST_SUCCESS
;
3448 static test_return_t
pre_nonblock(memcached_st
*memc
)
3450 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3452 return TEST_SUCCESS
;
3455 static test_return_t
pre_cork(memcached_st
*memc
)
3457 memcached_return_t rc
;
3460 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3463 return TEST_SKIPPED
;
3466 if (rc
== MEMCACHED_SUCCESS
)
3467 return TEST_SUCCESS
;
3469 return TEST_SKIPPED
;
3472 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3479 return TEST_SKIPPED
;
3482 if (rc
!= TEST_SUCCESS
)
3485 return pre_nonblock(memc
);
3488 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3490 memcached_return_t rc
= MEMCACHED_FAILURE
;
3491 memcached_st
*memc_clone
;
3492 memcached_server_instance_st instance
;
3494 memc_clone
= memcached_clone(NULL
, memc
);
3495 test_true(memc_clone
);
3496 // The memcached_version needs to be done on a clone, because the server
3497 // will not toggle protocol on an connection.
3498 memcached_version(memc_clone
);
3500 instance
= memcached_server_instance_by_position(memc_clone
, 0);
3502 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
3504 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3505 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3506 test_true(rc
== MEMCACHED_SUCCESS
);
3507 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3511 return TEST_SKIPPED
;
3514 memcached_free(memc_clone
);
3516 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3519 static test_return_t
pre_murmur(memcached_st
*memc
)
3521 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3523 return TEST_SUCCESS
;
3526 static test_return_t
pre_jenkins(memcached_st
*memc
)
3528 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3530 return TEST_SUCCESS
;
3534 static test_return_t
pre_md5(memcached_st
*memc
)
3536 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3538 return TEST_SUCCESS
;
3541 static test_return_t
pre_crc(memcached_st
*memc
)
3543 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3545 return TEST_SUCCESS
;
3548 static test_return_t
pre_hsieh(memcached_st
*memc
)
3550 #ifdef HAVE_HSIEH_HASH
3551 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3552 return TEST_SUCCESS
;
3555 return TEST_SKIPPED
;
3559 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3561 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3563 return TEST_SUCCESS
;
3566 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3568 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3570 return TEST_SUCCESS
;
3573 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3575 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3577 return TEST_SUCCESS
;
3580 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3582 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3584 return TEST_SUCCESS
;
3587 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3589 memcached_return_t rc
;
3592 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3593 test_true(rc
== MEMCACHED_SUCCESS
);
3595 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3596 test_true(value
== 1);
3598 return TEST_SUCCESS
;
3601 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3603 memcached_return_t rc
;
3606 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3607 test_true(rc
== MEMCACHED_SUCCESS
);
3609 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3610 test_true(value
== 1);
3612 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3613 test_true(rc
== MEMCACHED_SUCCESS
);
3615 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3616 test_true(value
== MEMCACHED_HASH_MD5
);
3618 return TEST_SUCCESS
;
3622 @note This should be testing to see if the server really supports the binary protocol.
3624 static test_return_t
pre_binary(memcached_st
*memc
)
3626 memcached_return_t rc
= MEMCACHED_FAILURE
;
3627 memcached_st
*memc_clone
;
3628 memcached_server_instance_st instance
;
3630 memc_clone
= memcached_clone(NULL
, memc
);
3631 test_true(memc_clone
);
3632 // The memcached_version needs to be done on a clone, because the server
3633 // will not toggle protocol on an connection.
3634 memcached_version(memc_clone
);
3636 instance
= memcached_server_instance_by_position(memc_clone
, 0);
3638 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
3640 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3641 test_true(rc
== MEMCACHED_SUCCESS
);
3642 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3645 memcached_free(memc_clone
);
3647 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3650 static test_return_t
pre_sasl(memcached_st
*memc
)
3652 memcached_return_t rc
= MEMCACHED_FAILURE
;
3654 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3655 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3656 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3657 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3659 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3661 memcached_server_st
*servers
= memcached_servers_parse(server
);
3662 test_true(servers
!= NULL
);
3663 memcached_servers_reset(memc
);
3664 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3665 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3666 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3667 test_true(rc
== MEMCACHED_SUCCESS
);
3673 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3676 static test_return_t
pre_replication(memcached_st
*memc
)
3678 test_return_t test_rc
;
3679 test_rc
= pre_binary(memc
);
3681 if (test_rc
!= TEST_SUCCESS
)
3685 * Make sure that we store the item on all servers
3686 * (master + replicas == number of servers)
3688 memcached_return_t rc
;
3689 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3690 memcached_server_count(memc
) - 1);
3691 test_true(rc
== MEMCACHED_SUCCESS
);
3692 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3694 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3698 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3702 rc
= pre_replication(memc
);
3703 if (rc
!= TEST_SUCCESS
)
3706 rc
= pre_nonblock(memc
);
3712 static void my_free(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, void *context
)
3715 #ifdef HARD_MALLOC_TESTS
3716 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3724 static void *my_malloc(const memcached_st
*ptr
__attribute__((unused
)), const size_t size
, void *context
)
3727 #ifdef HARD_MALLOC_TESTS
3728 void *ret
= malloc(size
+ 8);
3731 ret
= (void*)((caddr_t
)ret
+ 8);
3734 void *ret
= malloc(size
);
3739 memset(ret
, 0xff, size
);
3746 static void *my_realloc(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
, void *context
)
3749 #ifdef HARD_MALLOC_TESTS
3750 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3751 void *nmem
= realloc(real_ptr
, size
+ 8);
3756 ret
= (void*)((caddr_t
)nmem
+ 8);
3761 return realloc(mem
, size
);
3766 static void *my_calloc(const memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
, void *context
)
3769 #ifdef HARD_MALLOC_TESTS
3770 void *mem
= my_malloc(ptr
, nelem
* size
);
3773 memset(mem
, 0, nelem
* size
);
3778 return calloc(nelem
, size
);
3783 static test_return_t
set_prefix(memcached_st
*memc
)
3785 memcached_return_t rc
;
3786 const char *key
= "mine";
3789 /* Make sure be default none exists */
3790 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3791 test_true(rc
== MEMCACHED_FAILURE
);
3793 /* Test a clean set */
3794 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3795 test_true(rc
== MEMCACHED_SUCCESS
);
3797 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3798 test_true(memcmp(value
, key
, 4) == 0);
3799 test_true(rc
== MEMCACHED_SUCCESS
);
3801 /* Test that we can turn it off */
3802 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3803 test_true(rc
== MEMCACHED_SUCCESS
);
3805 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3806 test_true(rc
== MEMCACHED_FAILURE
);
3808 /* Now setup for main test */
3809 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3810 test_true(rc
== MEMCACHED_SUCCESS
);
3812 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3813 test_true(rc
== MEMCACHED_SUCCESS
);
3814 test_true(memcmp(value
, key
, 4) == 0);
3816 /* Set to Zero, and then Set to something too large */
3819 memset(long_key
, 0, 255);
3821 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3822 test_true(rc
== MEMCACHED_SUCCESS
);
3824 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3825 test_true(rc
== MEMCACHED_FAILURE
);
3826 test_true(value
== NULL
);
3828 /* Test a long key for failure */
3829 /* TODO, extend test to determine based on setting, what result should be */
3830 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3831 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3832 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3833 test_true(rc
== MEMCACHED_SUCCESS
);
3835 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3836 strcpy(long_key
, "This is more then the allotted number of characters");
3837 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3838 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3840 /* Test for a bad prefix, but with a short key */
3841 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3842 test_true(rc
== MEMCACHED_SUCCESS
);
3844 strcpy(long_key
, "dog cat");
3845 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3846 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3849 return TEST_SUCCESS
;
3853 #ifdef MEMCACHED_ENABLE_DEPRECATED
3854 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3856 void *test_ptr
= NULL
;
3859 memcached_malloc_fn malloc_cb
=
3860 (memcached_malloc_fn
)my_malloc
;
3861 cb_ptr
= *(void **)&malloc_cb
;
3862 memcached_return_t rc
;
3864 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3865 test_true(rc
== MEMCACHED_SUCCESS
);
3866 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3867 test_true(rc
== MEMCACHED_SUCCESS
);
3868 test_true(test_ptr
== cb_ptr
);
3872 memcached_realloc_fn realloc_cb
=
3873 (memcached_realloc_fn
)my_realloc
;
3874 cb_ptr
= *(void **)&realloc_cb
;
3875 memcached_return_t rc
;
3877 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3878 test_true(rc
== MEMCACHED_SUCCESS
);
3879 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3880 test_true(rc
== MEMCACHED_SUCCESS
);
3881 test_true(test_ptr
== cb_ptr
);
3885 memcached_free_fn free_cb
=
3886 (memcached_free_fn
)my_free
;
3887 cb_ptr
= *(void **)&free_cb
;
3888 memcached_return_t rc
;
3890 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3891 test_true(rc
== MEMCACHED_SUCCESS
);
3892 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3893 test_true(rc
== MEMCACHED_SUCCESS
);
3894 test_true(test_ptr
== cb_ptr
);
3897 return TEST_SUCCESS
;
3902 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3904 memcached_return_t rc
;
3905 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3906 my_realloc
, my_calloc
, NULL
);
3907 test_true(rc
== MEMCACHED_FAILURE
);
3909 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3910 my_realloc
, my_calloc
, NULL
);
3912 memcached_malloc_fn mem_malloc
;
3913 memcached_free_fn mem_free
;
3914 memcached_realloc_fn mem_realloc
;
3915 memcached_calloc_fn mem_calloc
;
3916 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3917 &mem_realloc
, &mem_calloc
);
3919 test_true(mem_malloc
== my_malloc
);
3920 test_true(mem_realloc
== my_realloc
);
3921 test_true(mem_calloc
== my_calloc
);
3922 test_true(mem_free
== my_free
);
3924 return TEST_SUCCESS
;
3927 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
3930 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3931 memcached_hash_t hash
;
3932 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3933 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3936 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3937 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3939 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3941 if (hash
!= MEMCACHED_HASH_CRC
)
3942 return TEST_SKIPPED
;
3944 return TEST_SUCCESS
;
3947 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
3950 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3951 memcached_hash_t hash
;
3952 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3953 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
3956 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3957 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3959 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3961 if (hash
!= MEMCACHED_HASH_HSIEH
)
3962 return TEST_SKIPPED
;
3965 return TEST_SUCCESS
;
3968 static test_return_t
enable_cas(memcached_st
*memc
)
3970 unsigned int set
= 1;
3972 memcached_server_instance_st instance
=
3973 memcached_server_instance_by_position(memc
, 0);
3975 memcached_version(memc
);
3977 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
3978 || instance
->minor_version
> 2)
3980 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3982 return TEST_SUCCESS
;
3985 return TEST_SKIPPED
;
3988 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
3990 memcached_version(memc
);
3992 memcached_server_instance_st instance
=
3993 memcached_server_instance_by_position(memc
, 0);
3995 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
3996 || instance
->minor_version
> 2)
3997 return TEST_SUCCESS
;
3999 return TEST_SKIPPED
;
4002 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4004 memcached_return_t rc
;
4007 memcached_servers_reset(memc
);
4009 if (stat("/tmp/memcached.socket", &buf
))
4010 return TEST_SKIPPED
;
4012 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4014 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4017 static test_return_t
pre_nodelay(memcached_st
*memc
)
4019 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4020 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4022 return TEST_SUCCESS
;
4025 static test_return_t
pre_settimer(memcached_st
*memc
)
4027 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4028 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4030 return TEST_SUCCESS
;
4033 static test_return_t
poll_timeout(memcached_st
*memc
)
4039 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4041 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4043 test_true(timeout
== 100);
4045 return TEST_SUCCESS
;
4048 static test_return_t
noreply_test(memcached_st
*memc
)
4050 memcached_return_t ret
;
4051 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4052 test_true(ret
== MEMCACHED_SUCCESS
);
4053 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4054 test_true(ret
== MEMCACHED_SUCCESS
);
4055 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4056 test_true(ret
== MEMCACHED_SUCCESS
);
4057 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4058 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4059 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4061 for (int count
=0; count
< 5; ++count
)
4063 for (size_t x
= 0; x
< 100; ++x
)
4066 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4070 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4073 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4076 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4079 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4082 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4088 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4092 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4093 ** API and is _ONLY_ done this way to verify that the library works the
4094 ** way it is supposed to do!!!!
4097 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4099 memcached_server_instance_st instance
=
4100 memcached_server_instance_by_position(memc
, x
);
4101 no_msg
+=(int)(instance
->cursor_active
);
4104 test_true(no_msg
== 0);
4105 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4108 ** Now validate that all items was set properly!
4110 for (size_t x
= 0; x
< 100; ++x
)
4114 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4117 char* value
=memcached_get(memc
, key
, strlen(key
),
4118 &length
, &flags
, &ret
);
4119 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4122 case 0: /* FALLTHROUGH */
4123 case 1: /* FALLTHROUGH */
4125 test_true(strncmp(value
, key
, len
) == 0);
4126 test_true(len
== length
);
4129 test_true(length
== len
* 2);
4132 test_true(length
== len
* 3);
4142 /* Try setting an illegal cas value (should not return an error to
4143 * the caller (because we don't expect a return message from the server)
4145 const char* keys
[]= {"0"};
4146 size_t lengths
[]= {1};
4149 memcached_result_st results_obj
;
4150 memcached_result_st
*results
;
4151 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4152 test_true(ret
== MEMCACHED_SUCCESS
);
4154 results
= memcached_result_create(memc
, &results_obj
);
4156 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4158 test_true(ret
== MEMCACHED_SUCCESS
);
4159 uint64_t cas
= memcached_result_cas(results
);
4160 memcached_result_free(&results_obj
);
4162 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4163 test_true(ret
== MEMCACHED_SUCCESS
);
4166 * The item will have a new cas value, so try to set it again with the old
4167 * value. This should fail!
4169 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4170 test_true(ret
== MEMCACHED_SUCCESS
);
4171 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4172 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4173 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4176 return TEST_SUCCESS
;
4179 static test_return_t
analyzer_test(memcached_st
*memc
)
4181 memcached_return_t rc
;
4182 memcached_stat_st
*memc_stat
;
4183 memcached_analysis_st
*report
;
4185 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4186 test_true(rc
== MEMCACHED_SUCCESS
);
4187 test_true(memc_stat
);
4189 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4190 test_true(rc
== MEMCACHED_SUCCESS
);
4194 memcached_stat_free(NULL
, memc_stat
);
4196 return TEST_SUCCESS
;
4199 /* Count the objects */
4200 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
__attribute__((unused
)),
4201 const char *key
__attribute__((unused
)),
4202 size_t key_length
__attribute__((unused
)),
4205 size_t *counter
= (size_t *)context
;
4207 *counter
= *counter
+ 1;
4209 return MEMCACHED_SUCCESS
;
4212 static test_return_t
dump_test(memcached_st
*memc
)
4214 memcached_return_t rc
;
4216 memcached_dump_fn callbacks
[1];
4217 test_return_t main_rc
;
4219 callbacks
[0]= &callback_dump_counter
;
4221 /* No support for Binary protocol yet */
4222 if (memc
->flags
.binary_protocol
)
4223 return TEST_SUCCESS
;
4225 main_rc
= set_test3(memc
);
4227 test_true (main_rc
== TEST_SUCCESS
);
4229 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4230 test_true(rc
== MEMCACHED_SUCCESS
);
4232 /* We may have more then 32 if our previous flush has not completed */
4233 test_true(counter
>= 32);
4235 return TEST_SUCCESS
;
4238 #ifdef HAVE_LIBMEMCACHEDUTIL
4239 static void* connection_release(void *arg
)
4242 memcached_pool_st
* pool
;
4247 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4251 static test_return_t
connection_pool_test(memcached_st
*memc
)
4253 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
4254 test_true(pool
!= NULL
);
4255 memcached_st
* mmc
[10];
4256 memcached_return_t rc
;
4258 for (size_t x
= 0; x
< 10; ++x
)
4260 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4261 test_true(mmc
[x
] != NULL
);
4262 test_true(rc
== MEMCACHED_SUCCESS
);
4265 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4266 test_true(rc
== MEMCACHED_SUCCESS
);
4270 memcached_pool_st
* pool
;
4272 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4273 pthread_create(&tid
, NULL
, connection_release
, &item
);
4274 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4275 test_true(rc
== MEMCACHED_SUCCESS
);
4276 pthread_join(tid
, NULL
);
4277 test_true(mmc
[9] == item
.mmc
);
4278 const char *key
= "key";
4279 size_t keylen
= strlen(key
);
4281 // verify that I can do ops with all connections
4282 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4283 test_true(rc
== MEMCACHED_SUCCESS
);
4285 for (size_t x
= 0; x
< 10; ++x
)
4287 uint64_t number_value
;
4288 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4289 test_true(rc
== MEMCACHED_SUCCESS
);
4290 test_true(number_value
== (x
+1));
4294 for (size_t x
= 0; x
< 10; ++x
)
4296 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4300 /* verify that I can set behaviors on the pool when I don't have all
4301 * of the connections in the pool. It should however be enabled
4302 * when I push the item into the pool
4304 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4305 test_true(mmc
[0] != NULL
);
4307 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4308 test_true(rc
== MEMCACHED_SUCCESS
);
4310 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4311 test_true(mmc
[1] != NULL
);
4313 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4314 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4315 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4317 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4318 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4319 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4322 test_true(memcached_pool_destroy(pool
) == memc
);
4323 return TEST_SUCCESS
;
4327 static test_return_t
replication_set_test(memcached_st
*memc
)
4329 memcached_return_t rc
;
4330 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4331 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4333 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4334 test_true(rc
== MEMCACHED_SUCCESS
);
4337 ** We are using the quiet commands to store the replicas, so we need
4338 ** to ensure that all of them are processed before we can continue.
4339 ** In the test we go directly from storing the object to trying to
4340 ** receive the object from all of the different servers, so we
4341 ** could end up in a race condition (the memcached server hasn't yet
4342 ** processed the quiet command from the replication set when it process
4343 ** the request from the other client (created by the clone)). As a
4344 ** workaround for that we call memcached_quit to send the quit command
4345 ** to the server and wait for the response ;-) If you use the test code
4346 ** as an example for your own code, please note that you shouldn't need
4349 memcached_quit(memc
);
4352 ** "bubba" should now be stored on all of our servers. We don't have an
4353 ** easy to use API to address each individual server, so I'll just iterate
4354 ** through a bunch of "master keys" and I should most likely hit all of the
4357 for (int x
= 'a'; x
<= 'z'; ++x
)
4359 char key
[2]= { [0]= (char)x
};
4362 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4364 test_true(rc
== MEMCACHED_SUCCESS
);
4365 test_true(val
!= NULL
);
4369 memcached_free(memc_clone
);
4371 return TEST_SUCCESS
;
4374 static test_return_t
replication_get_test(memcached_st
*memc
)
4376 memcached_return_t rc
;
4379 * Don't do the following in your code. I am abusing the internal details
4380 * within the library, and this is not a supported interface.
4381 * This is to verify correct behavior in the library
4383 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4385 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4386 memcached_server_instance_st instance
=
4387 memcached_server_instance_by_position(memc_clone
, host
);
4389 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4391 for (int x
= 'a'; x
<= 'z'; ++x
)
4393 char key
[2]= { [0]= (char)x
};
4396 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4398 test_true(rc
== MEMCACHED_SUCCESS
);
4399 test_true(val
!= NULL
);
4403 memcached_free(memc_clone
);
4406 return TEST_SUCCESS
;
4409 static test_return_t
replication_mget_test(memcached_st
*memc
)
4411 memcached_return_t rc
;
4412 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4413 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4415 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4416 size_t len
[]= { 5, 4, 4, 4 };
4418 for (size_t x
= 0; x
< 4; ++x
)
4420 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4421 test_true(rc
== MEMCACHED_SUCCESS
);
4425 ** We are using the quiet commands to store the replicas, so we need
4426 ** to ensure that all of them are processed before we can continue.
4427 ** In the test we go directly from storing the object to trying to
4428 ** receive the object from all of the different servers, so we
4429 ** could end up in a race condition (the memcached server hasn't yet
4430 ** processed the quiet command from the replication set when it process
4431 ** the request from the other client (created by the clone)). As a
4432 ** workaround for that we call memcached_quit to send the quit command
4433 ** to the server and wait for the response ;-) If you use the test code
4434 ** as an example for your own code, please note that you shouldn't need
4437 memcached_quit(memc
);
4440 * Don't do the following in your code. I am abusing the internal details
4441 * within the library, and this is not a supported interface.
4442 * This is to verify correct behavior in the library
4444 memcached_result_st result_obj
;
4445 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4447 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4448 memcached_server_instance_st instance
=
4449 memcached_server_instance_by_position(new_clone
, host
);
4450 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4452 for (int x
= 'a'; x
<= 'z'; ++x
)
4454 char key
[2]= { [0]= (char)x
, [1]= 0 };
4456 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4457 test_true(rc
== MEMCACHED_SUCCESS
);
4459 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4463 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4467 test_true(hits
== 4);
4468 memcached_result_free(&result_obj
);
4471 memcached_free(new_clone
);
4474 memcached_free(memc_clone
);
4476 return TEST_SUCCESS
;
4479 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4481 memcached_result_st result_obj
;
4482 memcached_return_t rc
;
4483 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4484 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4485 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4487 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4488 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4490 for (int x
=0; x
< 7; ++x
)
4492 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4493 test_true(rc
== MEMCACHED_SUCCESS
);
4496 memcached_quit(memc
);
4498 for (size_t x
= 0; x
< 7; ++x
)
4500 const char key
[2]= { [0]= (const char)x
};
4502 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4503 test_true(rc
== MEMCACHED_SUCCESS
);
4505 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4509 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4513 test_true(hits
== 7);
4514 memcached_result_free(&result_obj
);
4516 memcached_free(memc_clone
);
4517 return TEST_SUCCESS
;
4520 static test_return_t
replication_delete_test(memcached_st
*memc
)
4522 memcached_return_t rc
;
4523 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4524 /* Delete the items from all of the servers except 1 */
4525 uint64_t repl
= memcached_behavior_get(memc
,
4526 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4527 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4529 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4530 size_t len
[]= { 5, 4, 4, 4 };
4532 for (size_t x
= 0; x
< 4; ++x
)
4534 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4535 test_true(rc
== MEMCACHED_SUCCESS
);
4539 * Don't do the following in your code. I am abusing the internal details
4540 * within the library, and this is not a supported interface.
4541 * This is to verify correct behavior in the library
4543 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4544 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4546 memcached_server_instance_st instance
=
4547 memcached_server_instance_by_position(memc_clone
, x
);
4549 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4550 if (++hash
== memc_clone
->number_of_hosts
)
4554 memcached_result_st result_obj
;
4555 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4557 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4559 const char key
[2]= { [0]= (const char)x
};
4561 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4562 test_true(rc
== MEMCACHED_SUCCESS
);
4564 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4568 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4572 test_true(hits
== 4);
4573 memcached_result_free(&result_obj
);
4576 memcached_free(memc_clone
);
4578 return TEST_SUCCESS
;
4582 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4586 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4587 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4588 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4589 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4590 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4591 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4592 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4593 #ifdef HAVE_HSIEH_HASH
4594 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4596 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4597 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4598 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4600 return TEST_SUCCESS
;
4604 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4606 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4607 #ifdef HAVE_HSIEH_HASH
4608 expected_rc
= MEMCACHED_SUCCESS
;
4610 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4611 (uint64_t)MEMCACHED_HASH_HSIEH
);
4612 test_true(rc
== expected_rc
);
4614 return TEST_SUCCESS
;
4617 static test_return_t
one_at_a_time_run (memcached_st
*memc
__attribute__((unused
)))
4622 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4626 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4627 test_true(one_at_a_time_values
[x
] == hash_val
);
4630 return TEST_SUCCESS
;
4633 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4638 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4642 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4643 test_true(md5_values
[x
] == hash_val
);
4646 return TEST_SUCCESS
;
4649 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4654 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4658 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4659 test_true(crc_values
[x
] == hash_val
);
4662 return TEST_SUCCESS
;
4665 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4670 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4674 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4675 test_true(fnv1_64_values
[x
] == hash_val
);
4678 return TEST_SUCCESS
;
4681 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4686 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4690 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4691 test_true(fnv1a_64_values
[x
] == hash_val
);
4694 return TEST_SUCCESS
;
4697 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4703 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4707 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4708 test_true(fnv1_32_values
[x
] == hash_val
);
4711 return TEST_SUCCESS
;
4714 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4719 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4723 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4724 test_true(fnv1a_32_values
[x
] == hash_val
);
4727 return TEST_SUCCESS
;
4730 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4735 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4739 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4740 test_true(hsieh_values
[x
] == hash_val
);
4743 return TEST_SUCCESS
;
4746 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4749 return TEST_SKIPPED
;
4754 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4758 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4759 test_true(murmur_values
[x
] == hash_val
);
4762 return TEST_SUCCESS
;
4766 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4772 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4776 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4777 test_true(jenkins_values
[x
] == hash_val
);
4780 return TEST_SUCCESS
;
4783 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4786 return libhashkit_md5(string
, string_length
);
4789 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4792 return libhashkit_crc32(string
, string_length
);
4795 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4799 const hashkit_st
*kit
;
4801 hashkit_return_t hash_rc
;
4803 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};
4804 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};
4806 kit
= memcached_get_hashkit(memc
);
4808 hashkit_clone(&new_kit
, kit
);
4809 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4810 test_true(hash_rc
== HASHKIT_SUCCESS
);
4812 memcached_set_hashkit(memc
, &new_kit
);
4815 Verify Setting the hash.
4817 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4821 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4822 test_true(md5_values
[x
] == hash_val
);
4827 Now check memcached_st.
4829 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4833 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4834 test_true(md5_hosts
[x
] == hash_val
);
4837 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4838 test_true(hash_rc
== HASHKIT_SUCCESS
);
4840 memcached_set_hashkit(memc
, &new_kit
);
4843 Verify Setting the hash.
4845 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4849 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4850 test_true(crc_values
[x
] == hash_val
);
4853 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4857 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4858 test_true(crc_hosts
[x
] == hash_val
);
4861 return TEST_SUCCESS
;
4865 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4867 memcached_return_t rc
;
4870 memcached_server_st
*server_pool
;
4875 memc
= memcached_create(NULL
);
4878 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4879 test_true(rc
== MEMCACHED_SUCCESS
);
4881 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4882 test_true(value
== 1);
4884 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4885 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4888 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");
4889 memcached_server_push(memc
, server_pool
);
4891 /* verify that the server list was parsed okay. */
4892 test_true(memcached_server_count(memc
) == 8);
4893 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4894 test_true(server_pool
[0].port
== 11211);
4895 test_true(server_pool
[0].weight
== 600);
4896 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4897 test_true(server_pool
[2].port
== 11211);
4898 test_true(server_pool
[2].weight
== 200);
4899 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4900 test_true(server_pool
[7].port
== 11211);
4901 test_true(server_pool
[7].weight
== 100);
4903 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4904 * us test the boundary wraparound.
4906 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4908 /* verify the standard ketama set. */
4909 for (x
= 0; x
< 99; x
++)
4911 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4912 memcached_server_instance_st instance
=
4913 memcached_server_instance_by_position(memc
, server_idx
);
4914 const char *hostname
= memcached_server_name(instance
);
4916 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4919 memcached_server_list_free(server_pool
);
4920 memcached_free(memc
);
4922 return TEST_SUCCESS
;
4925 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4927 memcached_return_t rc
;
4930 memcached_server_st
*server_pool
;
4935 memc
= memcached_create(NULL
);
4938 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4939 test_true(rc
== MEMCACHED_SUCCESS
);
4941 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4942 test_true(value
== 1);
4944 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4945 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4947 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");
4948 memcached_server_push(memc
, server_pool
);
4950 /* verify that the server list was parsed okay. */
4951 test_true(memcached_server_count(memc
) == 8);
4952 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4953 test_true(server_pool
[0].port
== 11211);
4954 test_true(server_pool
[0].weight
== 600);
4955 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4956 test_true(server_pool
[2].port
== 11211);
4957 test_true(server_pool
[2].weight
== 200);
4958 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4959 test_true(server_pool
[7].port
== 11211);
4960 test_true(server_pool
[7].weight
== 100);
4962 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4963 * us test the boundary wraparound.
4965 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4967 /* verify the standard ketama set. */
4968 for (x
= 0; x
< 99; x
++)
4970 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4972 memcached_server_instance_st instance
=
4973 memcached_server_instance_by_position(memc
, server_idx
);
4975 const char *hostname
= memcached_server_name(instance
);
4977 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
4980 memcached_server_list_free(server_pool
);
4981 memcached_free(memc
);
4983 return TEST_SUCCESS
;
4986 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4988 test_return_t test_rc
;
4989 test_rc
= pre_binary(memc
);
4991 if (test_rc
!= TEST_SUCCESS
)
4994 memcached_return_t ret
;
4995 const char *key
= "regression_bug_434484";
4996 size_t keylen
= strlen(key
);
4998 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4999 test_true(ret
== MEMCACHED_NOTSTORED
);
5001 size_t size
= 2048 * 1024;
5002 void *data
= calloc(1, size
);
5003 test_true(data
!= NULL
);
5004 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5005 test_true(ret
== MEMCACHED_E2BIG
);
5008 return TEST_SUCCESS
;
5011 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5013 test_return_t test_rc
;
5014 test_rc
= pre_binary(memc
);
5016 if (test_rc
!= TEST_SUCCESS
)
5019 memcached_return_t rc
;
5021 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5024 * I only want to hit only _one_ server so I know the number of requests I'm
5025 * sending in the pipleine to the server. Let's try to do a multiget of
5026 * 1024 (that should satisfy most users don't you think?). Future versions
5027 * will include a mget_execute function call if you need a higher number.
5029 uint32_t number_of_hosts
= memcached_server_count(memc
);
5030 memc
->number_of_hosts
= 1;
5031 const size_t max_keys
= 1024;
5032 char **keys
= calloc(max_keys
, sizeof(char*));
5033 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5035 for (size_t x
= 0; x
< max_keys
; ++x
)
5039 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5041 test_true(keys
[x
] != NULL
);
5045 * Run two times.. the first time we should have 100% cache miss,
5046 * and the second time we should have 100% cache hits
5048 for (size_t y
= 0; y
< 2; y
++)
5050 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5051 test_true(rc
== MEMCACHED_SUCCESS
);
5052 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5056 /* The first iteration should give me a 100% cache miss. verify that*/
5057 char blob
[1024]= { 0 };
5059 test_true(counter
== 0);
5061 for (size_t x
= 0; x
< max_keys
; ++x
)
5063 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5064 blob
, sizeof(blob
), 0, 0);
5065 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5070 /* Verify that we received all of the key/value pairs */
5071 test_true(counter
== max_keys
);
5075 /* Release allocated resources */
5076 for (size_t x
= 0; x
< max_keys
; ++x
)
5083 memc
->number_of_hosts
= number_of_hosts
;
5085 return TEST_SUCCESS
;
5088 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5090 memcached_return_t rc
;
5091 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5092 test_true(rc
== MEMCACHED_SUCCESS
);
5094 return regression_bug_434843(memc
);
5097 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5099 memcached_return_t rc
;
5100 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5101 test_true(rc
== MEMCACHED_SUCCESS
);
5103 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5104 test_true(rc
== MEMCACHED_SUCCESS
);
5105 test_true(bytes
!= NULL
);
5106 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5108 test_true(rc
== MEMCACHED_SUCCESS
);
5109 test_true(bytes_read
!= NULL
);
5111 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5112 "bytes_written", &rc
);
5113 test_true(rc
== MEMCACHED_SUCCESS
);
5114 test_true(bytes_written
!= NULL
);
5116 test_true(strcmp(bytes
, bytes_read
) != 0);
5117 test_true(strcmp(bytes
, bytes_written
) != 0);
5119 /* Release allocated resources */
5122 free(bytes_written
);
5123 memcached_stat_free(NULL
, memc_stat
);
5125 return TEST_SUCCESS
;
5129 * The test case isn't obvious so I should probably document why
5130 * it works the way it does. Bug 442914 was caused by a bug
5131 * in the logic in memcached_purge (it did not handle the case
5132 * where the number of bytes sent was equal to the watermark).
5133 * In this test case, create messages so that we hit that case
5134 * and then disable noreply mode and issue a new command to
5135 * verify that it isn't stuck. If we change the format for the
5136 * delete command or the watermarks, we need to update this
5139 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5141 memcached_return_t rc
;
5142 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5143 test_true(rc
== MEMCACHED_SUCCESS
);
5144 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5146 uint32_t number_of_hosts
= memcached_server_count(memc
);
5147 memc
->number_of_hosts
= 1;
5152 for (uint32_t x
= 0; x
< 250; ++x
)
5154 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5155 rc
= memcached_delete(memc
, k
, len
, 0);
5156 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5159 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5162 rc
= memcached_delete(memc
, k
, len
, 0);
5163 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5165 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5166 test_true(rc
== MEMCACHED_SUCCESS
);
5167 rc
= memcached_delete(memc
, k
, len
, 0);
5168 test_true(rc
== MEMCACHED_NOTFOUND
);
5170 memc
->number_of_hosts
= number_of_hosts
;
5172 return TEST_SUCCESS
;
5175 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5177 memcached_server_instance_st instance_one
;
5178 memcached_server_instance_st instance_two
;
5180 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5181 return TEST_SKIPPED
;
5183 memcached_return_t rc
;
5185 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5186 test_true(rc
== MEMCACHED_SUCCESS
);
5188 const size_t max_keys
= 100;
5189 char **keys
= calloc(max_keys
, sizeof(char*));
5190 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5192 for (size_t x
= 0; x
< max_keys
; ++x
)
5196 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5198 test_true(keys
[x
] != NULL
);
5199 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5200 test_true(rc
== MEMCACHED_SUCCESS
);
5204 ** We are using the quiet commands to store the replicas, so we need
5205 ** to ensure that all of them are processed before we can continue.
5206 ** In the test we go directly from storing the object to trying to
5207 ** receive the object from all of the different servers, so we
5208 ** could end up in a race condition (the memcached server hasn't yet
5209 ** processed the quiet command from the replication set when it process
5210 ** the request from the other client (created by the clone)). As a
5211 ** workaround for that we call memcached_quit to send the quit command
5212 ** to the server and wait for the response ;-) If you use the test code
5213 ** as an example for your own code, please note that you shouldn't need
5216 memcached_quit(memc
);
5218 /* Verify that all messages are stored, and we didn't stuff too much
5221 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5222 test_true(rc
== MEMCACHED_SUCCESS
);
5225 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5226 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5227 /* Verify that we received all of the key/value pairs */
5228 test_true(counter
== max_keys
);
5230 memcached_quit(memc
);
5232 * Don't do the following in your code. I am abusing the internal details
5233 * within the library, and this is not a supported interface.
5234 * This is to verify correct behavior in the library. Fake that two servers
5237 instance_one
= memcached_server_instance_by_position(memc
, 0);
5238 instance_two
= memcached_server_instance_by_position(memc
, 2);
5239 in_port_t port0
= instance_one
->port
;
5240 in_port_t port2
= instance_two
->port
;
5242 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5243 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5245 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5246 test_true(rc
== MEMCACHED_SUCCESS
);
5249 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5250 test_true(counter
== (unsigned int)max_keys
);
5252 /* restore the memc handle */
5253 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5254 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5256 memcached_quit(memc
);
5258 /* Remove half of the objects */
5259 for (size_t x
= 0; x
< max_keys
; ++x
)
5263 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5264 test_true(rc
== MEMCACHED_SUCCESS
);
5268 memcached_quit(memc
);
5269 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5270 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5272 /* now retry the command, this time we should have cache misses */
5273 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5274 test_true(rc
== MEMCACHED_SUCCESS
);
5277 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5278 test_true(counter
== (unsigned int)(max_keys
>> 1));
5280 /* Release allocated resources */
5281 for (size_t x
= 0; x
< max_keys
; ++x
)
5288 /* restore the memc handle */
5289 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5290 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5292 return TEST_SUCCESS
;
5295 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5297 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5298 test_true(memc_clone
!= NULL
);
5299 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5301 memcached_server_instance_st instance
=
5302 memcached_server_instance_by_position(memc_clone
, 0);
5304 if (instance
->major_version
> 1 ||
5305 (instance
->major_version
== 1 &&
5306 instance
->minor_version
> 2))
5308 /* Binary protocol doesn't support deferred delete */
5309 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5310 test_true(bin_clone
!= NULL
);
5311 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5312 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5313 memcached_free(bin_clone
);
5315 memcached_quit(memc_clone
);
5317 /* If we know the server version, deferred delete should fail
5318 * with invalid arguments */
5319 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5321 /* If we don't know the server version, we should get a protocol error */
5322 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5324 /* but there is a bug in some of the memcached servers (1.4) that treats
5325 * the counter as noreply so it doesn't send the proper error message
5327 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5329 /* And buffered mode should be disabled and we should get protocol error */
5330 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5331 rc
= memcached_delete(memc
, "foo", 3, 1);
5332 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5334 /* Same goes for noreply... */
5335 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5336 rc
= memcached_delete(memc
, "foo", 3, 1);
5337 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5339 /* but a normal request should go through (and be buffered) */
5340 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5341 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5343 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5344 /* unbuffered noreply should be success */
5345 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5346 /* unbuffered with reply should be not found... */
5347 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5348 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5351 memcached_free(memc_clone
);
5352 return TEST_SUCCESS
;
5356 /* Test memcached_server_get_last_disconnect
5357 * For a working server set, shall be NULL
5358 * For a set of non existing server, shall not be NULL
5360 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5362 memcached_return_t rc
;
5363 memcached_server_instance_st disconnected_server
;
5365 /* With the working set of server */
5366 const char *key
= "marmotte";
5367 const char *value
= "milka";
5369 rc
= memcached_set(memc
, key
, strlen(key
),
5370 value
, strlen(value
),
5371 (time_t)0, (uint32_t)0);
5372 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5374 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5375 test_true(disconnected_server
== NULL
);
5377 /* With a non existing server */
5379 memcached_server_st
*servers
;
5381 const char *server_list
= "localhost:9";
5383 servers
= memcached_servers_parse(server_list
);
5385 mine
= memcached_create(NULL
);
5386 rc
= memcached_server_push(mine
, servers
);
5387 test_true(rc
== MEMCACHED_SUCCESS
);
5388 memcached_server_list_free(servers
);
5391 rc
= memcached_set(mine
, key
, strlen(key
),
5392 value
, strlen(value
),
5393 (time_t)0, (uint32_t)0);
5394 test_true(rc
!= MEMCACHED_SUCCESS
);
5396 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5397 test_true(disconnected_server
!= NULL
);
5398 test_true(memcached_server_port(disconnected_server
)== 9);
5399 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5401 memcached_quit(mine
);
5402 memcached_free(mine
);
5404 return TEST_SUCCESS
;
5408 * This test ensures that the failure counter isn't incremented during
5409 * normal termination of the memcached instance.
5411 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5413 memcached_return_t rc
;
5414 memcached_server_instance_st instance
;
5416 /* Set value to force connection to the server */
5417 const char *key
= "marmotte";
5418 const char *value
= "milka";
5421 * Please note that I'm abusing the internal structures in libmemcached
5422 * in a non-portable way and you shouldn't be doing this. I'm only
5423 * doing this in order to verify that the library works the way it should
5425 uint32_t number_of_hosts
= memcached_server_count(memc
);
5426 memc
->number_of_hosts
= 1;
5428 /* Ensure that we are connected to the server by setting a value */
5429 rc
= memcached_set(memc
, key
, strlen(key
),
5430 value
, strlen(value
),
5431 (time_t)0, (uint32_t)0);
5432 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5435 instance
= memcached_server_instance_by_position(memc
, 0);
5436 /* The test is to see that the memcached_quit doesn't increase the
5437 * the server failure conter, so let's ensure that it is zero
5438 * before sending quit
5440 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5442 memcached_quit(memc
);
5444 /* Verify that it memcached_quit didn't increment the failure counter
5445 * Please note that this isn't bullet proof, because an error could
5448 test_true(instance
->server_failure_counter
== 0);
5450 /* restore the instance */
5451 memc
->number_of_hosts
= number_of_hosts
;
5453 return TEST_SUCCESS
;
5460 * Test that ensures mget_execute does not end into recursive calls that finally fails
5462 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5464 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5465 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5466 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5467 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5468 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5471 * I only want to hit _one_ server so I know the number of requests I'm
5472 * sending in the pipeline.
5474 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5475 memc
->number_of_hosts
= 1;
5476 size_t max_keys
= 20480;
5479 char **keys
= calloc(max_keys
, sizeof(char*));
5480 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5482 /* First add all of the items.. */
5483 char blob
[1024]= { 0 };
5484 memcached_return rc
;
5485 for (size_t x
= 0; x
< max_keys
; ++x
)
5488 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5490 assert(keys
[x
] != NULL
);
5491 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5492 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5495 /* Try to get all of them with a large multiget */
5497 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5498 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5499 (size_t)max_keys
, callbacks
, &counter
, 1);
5501 assert(rc
== MEMCACHED_SUCCESS
);
5502 char* the_value
= NULL
;
5503 char the_key
[MEMCACHED_MAX_KEY
];
5504 size_t the_key_length
;
5505 size_t the_value_length
;
5509 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5511 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5517 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5520 assert(rc
== MEMCACHED_END
);
5522 /* Verify that we got all of the items */
5523 assert(counter
== max_keys
);
5525 /* Release all allocated resources */
5526 for (size_t x
= 0; x
< max_keys
; ++x
)
5533 memc
->number_of_hosts
= number_of_hosts
;
5535 return TEST_SUCCESS
;
5539 * Test that the sasl authentication works. We cannot use the default
5540 * pool of servers, because that would require that all servers we want
5541 * to test supports SASL authentication, and that they use the default
5544 static test_return_t
sasl_auth_test(memcached_st
*memc
)
5546 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
5547 memcached_return_t rc
;
5549 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5550 test_true(rc
== MEMCACHED_SUCCESS
);
5551 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
5552 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
5553 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
5554 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
5555 memcached_quit(memc
);
5557 rc
= memcached_set_sasl_auth_data(memc
,
5558 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
5559 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
5560 test_true(rc
== MEMCACHED_SUCCESS
);
5562 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5563 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
5564 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
5566 memcached_quit(memc
);
5567 return TEST_SUCCESS
;
5570 return TEST_FAILURE
;
5574 /* Clean the server before beginning testing */
5576 {"flush", 0, (test_callback_fn
)flush_test
},
5577 {"init", 0, (test_callback_fn
)init_test
},
5578 {"allocation", 0, (test_callback_fn
)allocation_test
},
5579 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5580 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5581 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5582 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5583 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5584 {"clone_test", 0, (test_callback_fn
)clone_test
},
5585 {"connection_test", 0, (test_callback_fn
)connection_test
},
5586 {"callback_test", 0, (test_callback_fn
)callback_test
},
5587 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5588 {"error", 0, (test_callback_fn
)error_test
},
5589 {"set", 0, (test_callback_fn
)set_test
},
5590 {"set2", 0, (test_callback_fn
)set_test2
},
5591 {"set3", 0, (test_callback_fn
)set_test3
},
5592 {"dump", 1, (test_callback_fn
)dump_test
},
5593 {"add", 1, (test_callback_fn
)add_test
},
5594 {"replace", 1, (test_callback_fn
)replace_test
},
5595 {"delete", 1, (test_callback_fn
)delete_test
},
5596 {"get", 1, (test_callback_fn
)get_test
},
5597 {"get2", 0, (test_callback_fn
)get_test2
},
5598 {"get3", 0, (test_callback_fn
)get_test3
},
5599 {"get4", 0, (test_callback_fn
)get_test4
},
5600 {"partial mget", 0, (test_callback_fn
)get_test5
},
5601 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5602 {"increment", 0, (test_callback_fn
)increment_test
},
5603 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5604 {"decrement", 0, (test_callback_fn
)decrement_test
},
5605 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5606 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5607 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5608 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5609 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5610 {"quit", 0, (test_callback_fn
)quit_test
},
5611 {"mget", 1, (test_callback_fn
)mget_test
},
5612 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5613 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5614 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5615 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5616 {"mget_end", 0, (test_callback_fn
)mget_end
},
5617 {"get_stats", 0, (test_callback_fn
)get_stats
},
5618 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5619 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5620 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5621 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5622 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
5623 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
5624 {"read_through", 1, (test_callback_fn
)read_through
},
5625 {"delete_through", 1, (test_callback_fn
)delete_through
},
5626 {"noreply", 1, (test_callback_fn
)noreply_test
},
5627 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
5628 #ifdef HAVE_LIBMEMCACHEDUTIL
5629 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
5631 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
5635 test_st behavior_tests
[] ={
5636 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
5637 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
5641 test_st async_tests
[] ={
5642 {"add", 1, (test_callback_fn
)add_wrapper
},
5646 test_st string_tests
[] ={
5647 {"string static with null", 0, (test_callback_fn
)string_static_null
},
5648 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
5649 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
5650 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
5651 {"string append", 0, (test_callback_fn
)string_alloc_append
},
5652 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
5653 {0, 0, (test_callback_fn
)0}
5656 test_st result_tests
[] ={
5657 {"result static", 0, (test_callback_fn
)result_static
},
5658 {"result alloc", 0, (test_callback_fn
)result_alloc
},
5659 {0, 0, (test_callback_fn
)0}
5662 test_st version_1_2_3
[] ={
5663 {"append", 0, (test_callback_fn
)append_test
},
5664 {"prepend", 0, (test_callback_fn
)prepend_test
},
5665 {"cas", 0, (test_callback_fn
)cas_test
},
5666 {"cas2", 0, (test_callback_fn
)cas2_test
},
5667 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
5668 {0, 0, (test_callback_fn
)0}
5671 test_st user_tests
[] ={
5672 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
5673 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
5674 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
5675 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
5676 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
5677 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
5678 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
5679 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
5680 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
5681 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
5682 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
5683 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
5684 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
5685 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
5686 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
5687 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
5690 ** It seems to be something weird with the character sets..
5691 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
5692 ** guess I need to find out how this is supposed to work.. Perhaps I need
5693 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
5694 ** so just disable the code for now...).
5696 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
5698 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
5699 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
5700 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
5701 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
5702 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
5703 {0, 0, (test_callback_fn
)0}
5706 test_st replication_tests
[]= {
5707 {"set", 1, (test_callback_fn
)replication_set_test
},
5708 {"get", 0, (test_callback_fn
)replication_get_test
},
5709 {"mget", 0, (test_callback_fn
)replication_mget_test
},
5710 {"delete", 0, (test_callback_fn
)replication_delete_test
},
5711 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
5712 {0, 0, (test_callback_fn
)0}
5716 * The following test suite is used to verify that we don't introduce
5717 * regression bugs. If you want more information about the bug / test,
5718 * you should look in the bug report at
5719 * http://bugs.launchpad.net/libmemcached
5721 test_st regression_tests
[]= {
5722 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
5723 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
5724 {"lp:434843 buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
5725 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
5726 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
5727 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
5728 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
5729 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
5730 {0, 0, (test_callback_fn
)0}
5733 test_st sasl_auth_tests
[]= {
5734 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
5735 {0, 0, (test_callback_fn
)0}
5738 test_st ketama_compatibility
[]= {
5739 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
5740 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
5741 {0, 0, (test_callback_fn
)0}
5744 test_st generate_tests
[] ={
5745 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5746 {"generate_data", 1, (test_callback_fn
)generate_data
},
5747 {"get_read", 0, (test_callback_fn
)get_read
},
5748 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
5749 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
5750 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
5751 {"generate_data", 1, (test_callback_fn
)generate_data
},
5752 {"mget_read", 0, (test_callback_fn
)mget_read
},
5753 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
5754 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
5755 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5756 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
5757 {"generate_data", 1, (test_callback_fn
)generate_data
},
5758 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
5759 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5760 {0, 0, (test_callback_fn
)0}
5763 test_st consistent_tests
[] ={
5764 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5765 {"generate_data", 1, (test_callback_fn
)generate_data
},
5766 {"get_read", 0, (test_callback_fn
)get_read_count
},
5767 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5768 {0, 0, (test_callback_fn
)0}
5771 test_st consistent_weighted_tests
[] ={
5772 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5773 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
5774 {"get_read", 0, (test_callback_fn
)get_read_count
},
5775 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5776 {0, 0, (test_callback_fn
)0}
5779 test_st hsieh_availability
[] ={
5780 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
5781 {0, 0, (test_callback_fn
)0}
5785 test_st hash_sanity
[] ={
5786 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
5787 {0, 0, (test_callback_fn
)0}
5791 test_st ketama_auto_eject_hosts
[] ={
5792 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
5793 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
5794 {0, 0, (test_callback_fn
)0}
5797 test_st hash_tests
[] ={
5798 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
5799 {"md5", 0, (test_callback_fn
)md5_run
},
5800 {"crc", 0, (test_callback_fn
)crc_run
},
5801 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
5802 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
5803 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
5804 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
5805 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
5806 {"murmur", 0, (test_callback_fn
)murmur_run
},
5807 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
5808 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
5809 {0, 0, (test_callback_fn
)0}
5812 collection_st collection
[] ={
5814 {"hash_sanity", 0, 0, hash_sanity
},
5816 {"hsieh_availability", 0, 0, hsieh_availability
},
5817 {"block", 0, 0, tests
},
5818 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
5819 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
5820 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
5821 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
5822 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
5823 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
5824 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
5825 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
5826 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
5827 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
5828 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
5829 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
5830 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
5831 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
5832 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
5833 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
5834 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
5835 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
5836 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
5837 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
5838 #ifdef MEMCACHED_ENABLE_DEPRECATED
5839 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
5841 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
5842 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
5843 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
5844 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
5845 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
5846 {"string", 0, 0, string_tests
},
5847 {"result", 0, 0, result_tests
},
5848 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
5849 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
5850 {"user", 0, 0, user_tests
},
5851 {"generate", 0, 0, generate_tests
},
5852 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
5853 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
5854 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
5855 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
5856 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
5857 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
5858 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
5860 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
5861 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
5862 {"consistent_not", 0, 0, consistent_tests
},
5863 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
5864 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
5865 {"ketama_compat", 0, 0, ketama_compatibility
},
5866 {"test_hashes", 0, 0, hash_tests
},
5867 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
5868 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
5869 {"regression", 0, 0, regression_tests
},
5870 {"behaviors", 0, 0, behavior_tests
},
5874 #define SERVERS_TO_CREATE 5
5876 #include "libmemcached_world.h"
5878 void get_world(world_st
*world
)
5880 world
->collections
= collection
;
5882 world
->create
= (test_callback_create_fn
)world_create
;
5883 world
->destroy
= (test_callback_fn
)world_destroy
;
5885 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
5886 world
->test
.flush
= (test_callback_fn
)world_flush
;
5887 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
5888 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
5889 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
5891 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
5892 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
5894 world
->runner
= &defualt_libmemcached_runner
;