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};
381 // You have updated the memcache_error messages but not updated docs/tests.
382 test_true(MEMCACHED_MAXIMUM_RETURN
== 40);
383 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
386 const char *msg
= memcached_strerror(memc
, rc
);
387 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
388 MEMCACHED_HASH_JENKINS
);
389 if (values
[rc
] != hash_val
)
391 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
392 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
394 test_true(values
[rc
] == hash_val
);
400 static test_return_t
set_test(memcached_st
*memc
)
402 memcached_return_t rc
;
403 const char *key
= "foo";
404 const char *value
= "when we sanitize";
406 rc
= memcached_set(memc
, key
, strlen(key
),
407 value
, strlen(value
),
408 (time_t)0, (uint32_t)0);
409 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
414 static test_return_t
append_test(memcached_st
*memc
)
416 memcached_return_t rc
;
417 const char *key
= "fig";
418 const char *in_value
= "we";
419 char *out_value
= NULL
;
423 rc
= memcached_flush(memc
, 0);
424 test_true(rc
== MEMCACHED_SUCCESS
);
426 rc
= memcached_set(memc
, key
, strlen(key
),
427 in_value
, strlen(in_value
),
428 (time_t)0, (uint32_t)0);
429 test_true(rc
== MEMCACHED_SUCCESS
);
431 rc
= memcached_append(memc
, key
, strlen(key
),
432 " the", strlen(" the"),
433 (time_t)0, (uint32_t)0);
434 test_true(rc
== MEMCACHED_SUCCESS
);
436 rc
= memcached_append(memc
, key
, strlen(key
),
437 " people", strlen(" people"),
438 (time_t)0, (uint32_t)0);
439 test_true(rc
== MEMCACHED_SUCCESS
);
441 out_value
= memcached_get(memc
, key
, strlen(key
),
442 &value_length
, &flags
, &rc
);
443 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
444 test_true(strlen("we the people") == value_length
);
445 test_true(rc
== MEMCACHED_SUCCESS
);
451 static test_return_t
append_binary_test(memcached_st
*memc
)
453 memcached_return_t rc
;
454 const char *key
= "numbers";
455 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
461 rc
= memcached_flush(memc
, 0);
462 test_true(rc
== MEMCACHED_SUCCESS
);
464 rc
= memcached_set(memc
,
467 (time_t)0, (uint32_t)0);
468 test_true(rc
== MEMCACHED_SUCCESS
);
470 for (x
= 0; store_list
[x
] ; x
++)
472 rc
= memcached_append(memc
,
474 (char *)&store_list
[x
], sizeof(uint32_t),
475 (time_t)0, (uint32_t)0);
476 test_true(rc
== MEMCACHED_SUCCESS
);
479 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
480 &value_length
, &flags
, &rc
);
481 test_true((value_length
== (sizeof(uint32_t) * x
)));
482 test_true(rc
== MEMCACHED_SUCCESS
);
484 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
486 test_true(*ptr
== store_list
[x
- counter
]);
494 static test_return_t
cas2_test(memcached_st
*memc
)
496 memcached_return_t rc
;
497 const char *keys
[]= {"fudge", "son", "food"};
498 size_t key_length
[]= {5, 3, 4};
499 const char *value
= "we the people";
500 size_t value_length
= strlen("we the people");
502 memcached_result_st results_obj
;
503 memcached_result_st
*results
;
506 rc
= memcached_flush(memc
, 0);
507 test_true(rc
== MEMCACHED_SUCCESS
);
509 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
511 for (x
= 0; x
< 3; x
++)
513 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
514 keys
[x
], key_length
[x
],
515 (time_t)50, (uint32_t)9);
516 test_true(rc
== MEMCACHED_SUCCESS
);
519 rc
= memcached_mget(memc
, keys
, key_length
, 3);
521 results
= memcached_result_create(memc
, &results_obj
);
523 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
525 test_true(results
->item_cas
);
526 test_true(rc
== MEMCACHED_SUCCESS
);
527 test_true(memcached_result_cas(results
));
529 test_true(!memcmp(value
, "we the people", strlen("we the people")));
530 test_true(strlen("we the people") == value_length
);
531 test_true(rc
== MEMCACHED_SUCCESS
);
533 memcached_result_free(&results_obj
);
538 static test_return_t
cas_test(memcached_st
*memc
)
540 memcached_return_t rc
;
541 const char *key
= "fun";
542 size_t key_length
= strlen(key
);
543 const char *value
= "we the people";
544 const char* keys
[2] = { key
, NULL
};
545 size_t keylengths
[2] = { strlen(key
), 0 };
546 size_t value_length
= strlen(value
);
547 const char *value2
= "change the value";
548 size_t value2_length
= strlen(value2
);
550 memcached_result_st results_obj
;
551 memcached_result_st
*results
;
554 rc
= memcached_flush(memc
, 0);
555 test_true(rc
== MEMCACHED_SUCCESS
);
557 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
559 rc
= memcached_set(memc
, key
, strlen(key
),
560 value
, strlen(value
),
561 (time_t)0, (uint32_t)0);
562 test_true(rc
== MEMCACHED_SUCCESS
);
564 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
566 results
= memcached_result_create(memc
, &results_obj
);
568 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
570 test_true(rc
== MEMCACHED_SUCCESS
);
571 test_true(memcached_result_cas(results
));
572 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
573 test_true(strlen(memcached_result_value(results
)) == value_length
);
574 test_true(rc
== MEMCACHED_SUCCESS
);
575 uint64_t cas
= memcached_result_cas(results
);
578 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
579 test_true(rc
== MEMCACHED_END
);
580 test_true(results
== NULL
);
583 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
584 test_true(rc
== MEMCACHED_SUCCESS
);
587 * The item will have a new cas value, so try to set it again with the old
588 * value. This should fail!
590 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
591 test_true(rc
== MEMCACHED_DATA_EXISTS
);
593 memcached_result_free(&results_obj
);
598 static test_return_t
prepend_test(memcached_st
*memc
)
600 memcached_return_t rc
;
601 const char *key
= "fig";
602 const char *value
= "people";
603 char *out_value
= NULL
;
607 rc
= memcached_flush(memc
, 0);
608 test_true(rc
== MEMCACHED_SUCCESS
);
610 rc
= memcached_set(memc
, key
, strlen(key
),
611 value
, strlen(value
),
612 (time_t)0, (uint32_t)0);
613 test_true(rc
== MEMCACHED_SUCCESS
);
615 rc
= memcached_prepend(memc
, key
, strlen(key
),
616 "the ", strlen("the "),
617 (time_t)0, (uint32_t)0);
618 test_true(rc
== MEMCACHED_SUCCESS
);
620 rc
= memcached_prepend(memc
, key
, strlen(key
),
621 "we ", strlen("we "),
622 (time_t)0, (uint32_t)0);
623 test_true(rc
== MEMCACHED_SUCCESS
);
625 out_value
= memcached_get(memc
, key
, strlen(key
),
626 &value_length
, &flags
, &rc
);
627 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
628 test_true(strlen("we the people") == value_length
);
629 test_true(rc
== MEMCACHED_SUCCESS
);
636 Set the value, then quit to make sure it is flushed.
637 Come back in and test that add fails.
639 static test_return_t
add_test(memcached_st
*memc
)
641 memcached_return_t rc
;
642 const char *key
= "foo";
643 const char *value
= "when we sanitize";
644 unsigned long long setting_value
;
646 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
648 rc
= memcached_set(memc
, key
, strlen(key
),
649 value
, strlen(value
),
650 (time_t)0, (uint32_t)0);
651 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
652 memcached_quit(memc
);
653 rc
= memcached_add(memc
, key
, strlen(key
),
654 value
, strlen(value
),
655 (time_t)0, (uint32_t)0);
657 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
660 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
664 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
671 ** There was a problem of leaking filedescriptors in the initial release
672 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
673 ** systems it seems that the kernel is slow on reclaiming the resources
674 ** because the connects starts to time out (the test doesn't do much
675 ** anyway, so just loop 10 iterations)
677 static test_return_t
add_wrapper(memcached_st
*memc
)
680 unsigned int max
= 10000;
688 for (x
= 0; x
< max
; x
++)
694 static test_return_t
replace_test(memcached_st
*memc
)
696 memcached_return_t rc
;
697 const char *key
= "foo";
698 const char *value
= "when we sanitize";
699 const char *original
= "first we insert some data";
701 rc
= memcached_set(memc
, key
, strlen(key
),
702 original
, strlen(original
),
703 (time_t)0, (uint32_t)0);
704 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
706 rc
= memcached_replace(memc
, key
, strlen(key
),
707 value
, strlen(value
),
708 (time_t)0, (uint32_t)0);
709 test_true(rc
== MEMCACHED_SUCCESS
);
714 static test_return_t
delete_test(memcached_st
*memc
)
716 memcached_return_t rc
;
717 const char *key
= "foo";
718 const char *value
= "when we sanitize";
720 rc
= memcached_set(memc
, key
, strlen(key
),
721 value
, strlen(value
),
722 (time_t)0, (uint32_t)0);
723 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
725 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
726 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
731 static test_return_t
flush_test(memcached_st
*memc
)
733 memcached_return_t rc
;
735 rc
= memcached_flush(memc
, 0);
736 test_true(rc
== MEMCACHED_SUCCESS
);
741 static memcached_return_t
server_function(const memcached_st
*ptr
__attribute__((unused
)),
742 const memcached_server_st
*server
__attribute__((unused
)),
743 void *context
__attribute__((unused
)))
747 return MEMCACHED_SUCCESS
;
750 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
753 strcpy(context
, "foo bad");
754 memcached_server_fn callbacks
[1];
756 callbacks
[0]= server_function
;
757 memcached_server_cursor(memc
, callbacks
, context
, 1);
761 static test_return_t
bad_key_test(memcached_st
*memc
)
763 memcached_return_t rc
;
764 const char *key
= "foo bad";
766 size_t string_length
;
768 memcached_st
*memc_clone
;
770 size_t max_keylen
= 0xffff;
772 // Just skip if we are in binary mode.
773 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
776 memc_clone
= memcached_clone(NULL
, memc
);
777 test_true(memc_clone
);
779 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
780 test_true(rc
== MEMCACHED_SUCCESS
);
782 /* All keys are valid in the binary protocol (except for length) */
783 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
785 string
= memcached_get(memc_clone
, key
, strlen(key
),
786 &string_length
, &flags
, &rc
);
787 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
788 test_true(string_length
== 0);
792 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
793 test_true(rc
== MEMCACHED_SUCCESS
);
794 string
= memcached_get(memc_clone
, key
, strlen(key
),
795 &string_length
, &flags
, &rc
);
796 test_true(rc
== MEMCACHED_NOTFOUND
);
797 test_true(string_length
== 0);
800 /* Test multi key for bad keys */
801 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
802 size_t key_lengths
[] = { 7, 7, 7 };
804 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
805 test_true(rc
== MEMCACHED_SUCCESS
);
807 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
808 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
810 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
811 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
815 /* The following test should be moved to the end of this function when the
816 memcached server is updated to allow max size length of the keys in the
819 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
820 test_true(rc
== MEMCACHED_SUCCESS
);
822 char *longkey
= malloc(max_keylen
+ 1);
825 memset(longkey
, 'a', max_keylen
+ 1);
826 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
827 &string_length
, &flags
, &rc
);
828 test_true(rc
== MEMCACHED_NOTFOUND
);
829 test_true(string_length
== 0);
832 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
833 &string_length
, &flags
, &rc
);
834 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
835 test_true(string_length
== 0);
842 /* Make sure zero length keys are marked as bad */
844 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
845 test_true(rc
== MEMCACHED_SUCCESS
);
846 string
= memcached_get(memc_clone
, key
, 0,
847 &string_length
, &flags
, &rc
);
848 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
849 test_true(string_length
== 0);
852 memcached_free(memc_clone
);
857 #define READ_THROUGH_VALUE "set for me"
858 static memcached_return_t
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
859 char *key
__attribute__((unused
)),
860 size_t key_length
__attribute__((unused
)),
861 memcached_result_st
*result
)
864 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
867 static test_return_t
read_through(memcached_st
*memc
)
869 memcached_return_t rc
;
870 const char *key
= "foo";
872 size_t string_length
;
874 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
876 string
= memcached_get(memc
, key
, strlen(key
),
877 &string_length
, &flags
, &rc
);
879 test_true(rc
== MEMCACHED_NOTFOUND
);
880 test_false(string_length
);
883 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
885 test_true(rc
== MEMCACHED_SUCCESS
);
887 string
= memcached_get(memc
, key
, strlen(key
),
888 &string_length
, &flags
, &rc
);
890 test_true(rc
== MEMCACHED_SUCCESS
);
891 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
892 test_strcmp(READ_THROUGH_VALUE
, string
);
895 string
= memcached_get(memc
, key
, strlen(key
),
896 &string_length
, &flags
, &rc
);
898 test_true(rc
== MEMCACHED_SUCCESS
);
899 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
900 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
906 static memcached_return_t
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
908 size_t key_length
__attribute__((unused
)))
912 return MEMCACHED_SUCCESS
;
915 static test_return_t
delete_through(memcached_st
*memc
)
917 memcached_trigger_delete_key_fn callback
;
918 memcached_return_t rc
;
920 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
922 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
923 test_true(rc
== MEMCACHED_SUCCESS
);
928 static test_return_t
get_test(memcached_st
*memc
)
930 memcached_return_t rc
;
931 const char *key
= "foo";
933 size_t string_length
;
936 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
937 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
939 string
= memcached_get(memc
, key
, strlen(key
),
940 &string_length
, &flags
, &rc
);
942 test_true(rc
== MEMCACHED_NOTFOUND
);
943 test_false(string_length
);
949 static test_return_t
get_test2(memcached_st
*memc
)
951 memcached_return_t rc
;
952 const char *key
= "foo";
953 const char *value
= "when we sanitize";
955 size_t string_length
;
958 rc
= memcached_set(memc
, key
, strlen(key
),
959 value
, strlen(value
),
960 (time_t)0, (uint32_t)0);
961 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
963 string
= memcached_get(memc
, key
, strlen(key
),
964 &string_length
, &flags
, &rc
);
967 test_true(rc
== MEMCACHED_SUCCESS
);
968 test_true(string_length
== strlen(value
));
969 test_true(!memcmp(string
, value
, string_length
));
976 static test_return_t
set_test2(memcached_st
*memc
)
978 memcached_return_t rc
;
979 const char *key
= "foo";
980 const char *value
= "train in the brain";
981 size_t value_length
= strlen(value
);
984 for (x
= 0; x
< 10; x
++)
986 rc
= memcached_set(memc
, key
, strlen(key
),
988 (time_t)0, (uint32_t)0);
989 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
995 static test_return_t
set_test3(memcached_st
*memc
)
997 memcached_return_t rc
;
999 size_t value_length
= 8191;
1002 value
= (char*)malloc(value_length
);
1005 for (x
= 0; x
< value_length
; x
++)
1006 value
[x
] = (char) (x
% 127);
1008 /* The dump test relies on there being at least 32 items in memcached */
1009 for (x
= 0; x
< 32; x
++)
1013 sprintf(key
, "foo%u", x
);
1015 rc
= memcached_set(memc
, key
, strlen(key
),
1016 value
, value_length
,
1017 (time_t)0, (uint32_t)0);
1018 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1023 return TEST_SUCCESS
;
1026 static test_return_t
get_test3(memcached_st
*memc
)
1028 memcached_return_t rc
;
1029 const char *key
= "foo";
1031 size_t value_length
= 8191;
1033 size_t string_length
;
1037 value
= (char*)malloc(value_length
);
1040 for (x
= 0; x
< value_length
; x
++)
1041 value
[x
] = (char) (x
% 127);
1043 rc
= memcached_set(memc
, key
, strlen(key
),
1044 value
, value_length
,
1045 (time_t)0, (uint32_t)0);
1046 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1048 string
= memcached_get(memc
, key
, strlen(key
),
1049 &string_length
, &flags
, &rc
);
1051 test_true(rc
== MEMCACHED_SUCCESS
);
1053 test_true(string_length
== value_length
);
1054 test_true(!memcmp(string
, value
, string_length
));
1059 return TEST_SUCCESS
;
1062 static test_return_t
get_test4(memcached_st
*memc
)
1064 memcached_return_t rc
;
1065 const char *key
= "foo";
1067 size_t value_length
= 8191;
1069 size_t string_length
;
1073 value
= (char*)malloc(value_length
);
1076 for (x
= 0; x
< value_length
; x
++)
1077 value
[x
] = (char) (x
% 127);
1079 rc
= memcached_set(memc
, key
, strlen(key
),
1080 value
, value_length
,
1081 (time_t)0, (uint32_t)0);
1082 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1084 for (x
= 0; x
< 10; x
++)
1086 string
= memcached_get(memc
, key
, strlen(key
),
1087 &string_length
, &flags
, &rc
);
1089 test_true(rc
== MEMCACHED_SUCCESS
);
1091 test_true(string_length
== value_length
);
1092 test_true(!memcmp(string
, value
, string_length
));
1098 return TEST_SUCCESS
;
1102 * This test verifies that memcached_read_one_response doesn't try to
1103 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1104 * responses before you execute a storage command.
1106 static test_return_t
get_test5(memcached_st
*memc
)
1109 ** Request the same key twice, to ensure that we hash to the same server
1110 ** (so that we have multiple response values queued up) ;-)
1112 const char *keys
[]= { "key", "key" };
1113 size_t lengths
[]= { 3, 3 };
1117 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1118 keys
[0], lengths
[0], 0, 0);
1119 test_true(rc
== MEMCACHED_SUCCESS
);
1120 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1122 memcached_result_st results_obj
;
1123 memcached_result_st
*results
;
1124 results
=memcached_result_create(memc
, &results_obj
);
1126 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1128 memcached_result_free(&results_obj
);
1130 /* Don't read out the second result, but issue a set instead.. */
1131 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1132 test_true(rc
== MEMCACHED_SUCCESS
);
1134 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1135 &rlen
, &flags
, &rc
);
1136 test_true(val
== NULL
);
1137 test_true(rc
== MEMCACHED_NOTFOUND
);
1138 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1139 test_true(val
!= NULL
);
1140 test_true(rc
== MEMCACHED_SUCCESS
);
1143 return TEST_SUCCESS
;
1146 static test_return_t
mget_end(memcached_st
*memc
)
1148 const char *keys
[]= { "foo", "foo2" };
1149 size_t lengths
[]= { 3, 4 };
1150 const char *values
[]= { "fjord", "41" };
1152 memcached_return_t rc
;
1155 for (int i
= 0; i
< 2; i
++)
1157 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1158 (time_t)0, (uint32_t)0);
1159 test_true(rc
== MEMCACHED_SUCCESS
);
1163 size_t string_length
;
1166 // retrieve both via mget
1167 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1168 test_true(rc
== MEMCACHED_SUCCESS
);
1170 char key
[MEMCACHED_MAX_KEY
];
1173 // this should get both
1174 for (int i
= 0; i
< 2; i
++)
1176 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1178 test_true(rc
== MEMCACHED_SUCCESS
);
1180 if (key_length
== 4)
1182 test_true(string_length
== strlen(values
[val
]));
1183 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1187 // this should indicate end
1188 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1189 test_true(rc
== MEMCACHED_END
);
1192 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1193 test_true(rc
== MEMCACHED_SUCCESS
);
1195 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1196 test_true(key_length
== lengths
[0]);
1197 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1198 test_true(string_length
== strlen(values
[0]));
1199 test_true(strncmp(values
[0], string
, string_length
) == 0);
1200 test_true(rc
== MEMCACHED_SUCCESS
);
1203 // this should indicate end
1204 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1205 test_true(rc
== MEMCACHED_END
);
1207 return TEST_SUCCESS
;
1210 /* Do not copy the style of this code, I just access hosts to testthis function */
1211 static test_return_t
stats_servername_test(memcached_st
*memc
)
1213 memcached_return_t rc
;
1214 memcached_stat_st memc_stat
;
1215 memcached_server_instance_st instance
=
1216 memcached_server_instance_by_position(memc
, 0);
1218 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1219 memcached_server_name(instance
),
1220 memcached_server_port(instance
));
1222 return TEST_SUCCESS
;
1225 static test_return_t
increment_test(memcached_st
*memc
)
1227 uint64_t new_number
;
1228 memcached_return_t rc
;
1229 const char *key
= "number";
1230 const char *value
= "0";
1232 rc
= memcached_set(memc
, key
, strlen(key
),
1233 value
, strlen(value
),
1234 (time_t)0, (uint32_t)0);
1235 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1237 rc
= memcached_increment(memc
, key
, strlen(key
),
1239 test_true(rc
== MEMCACHED_SUCCESS
);
1240 test_true(new_number
== 1);
1242 rc
= memcached_increment(memc
, key
, strlen(key
),
1244 test_true(rc
== MEMCACHED_SUCCESS
);
1245 test_true(new_number
== 2);
1247 return TEST_SUCCESS
;
1250 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1252 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1254 uint64_t new_number
;
1255 memcached_return_t rc
;
1256 const char *key
= "number";
1257 uint64_t initial
= 0;
1259 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1260 1, initial
, 0, &new_number
);
1261 test_true(rc
== MEMCACHED_SUCCESS
);
1262 test_true(new_number
== initial
);
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
+ 1));
1269 return TEST_SUCCESS
;
1272 static test_return_t
decrement_test(memcached_st
*memc
)
1274 uint64_t new_number
;
1275 memcached_return_t rc
;
1276 const char *key
= "number";
1277 const char *value
= "3";
1279 rc
= memcached_set(memc
, key
, strlen(key
),
1280 value
, strlen(value
),
1281 (time_t)0, (uint32_t)0);
1282 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1284 rc
= memcached_decrement(memc
, key
, strlen(key
),
1286 test_true(rc
== MEMCACHED_SUCCESS
);
1287 test_true(new_number
== 2);
1289 rc
= memcached_decrement(memc
, key
, strlen(key
),
1291 test_true(rc
== MEMCACHED_SUCCESS
);
1292 test_true(new_number
== 1);
1294 return TEST_SUCCESS
;
1297 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1299 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1301 uint64_t new_number
;
1302 memcached_return_t rc
;
1303 const char *key
= "number";
1304 uint64_t initial
= 3;
1306 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1307 1, initial
, 0, &new_number
);
1308 test_true(rc
== MEMCACHED_SUCCESS
);
1309 test_true(new_number
== initial
);
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
- 1));
1316 return TEST_SUCCESS
;
1319 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1321 uint64_t new_number
;
1322 memcached_return_t rc
;
1323 const char *master_key
= "foo";
1324 const char *key
= "number";
1325 const char *value
= "0";
1327 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1329 value
, strlen(value
),
1330 (time_t)0, (uint32_t)0);
1331 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1333 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1335 test_true(rc
== MEMCACHED_SUCCESS
);
1336 test_true(new_number
== 1);
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
== 2);
1343 return TEST_SUCCESS
;
1346 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1348 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1350 uint64_t new_number
;
1351 memcached_return_t rc
;
1352 const char *master_key
= "foo";
1353 const char *key
= "number";
1354 uint64_t initial
= 0;
1356 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1358 1, initial
, 0, &new_number
);
1359 test_true(rc
== MEMCACHED_SUCCESS
);
1360 test_true(new_number
== initial
);
1362 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1364 1, initial
, 0, &new_number
);
1365 test_true(rc
== MEMCACHED_SUCCESS
);
1366 test_true(new_number
== (initial
+ 1));
1368 return TEST_SUCCESS
;
1371 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1373 uint64_t new_number
;
1374 memcached_return_t rc
;
1375 const char *master_key
= "foo";
1376 const char *key
= "number";
1377 const char *value
= "3";
1379 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1381 value
, strlen(value
),
1382 (time_t)0, (uint32_t)0);
1383 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1385 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1388 test_true(rc
== MEMCACHED_SUCCESS
);
1389 test_true(new_number
== 2);
1391 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1394 test_true(rc
== MEMCACHED_SUCCESS
);
1395 test_true(new_number
== 1);
1397 return TEST_SUCCESS
;
1400 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1402 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1404 uint64_t new_number
;
1405 memcached_return_t rc
;
1406 const char *master_key
= "foo";
1407 const char *key
= "number";
1408 uint64_t initial
= 3;
1410 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1412 1, initial
, 0, &new_number
);
1413 test_true(rc
== MEMCACHED_SUCCESS
);
1414 test_true(new_number
== initial
);
1416 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1418 1, initial
, 0, &new_number
);
1419 test_true(rc
== MEMCACHED_SUCCESS
);
1420 test_true(new_number
== (initial
- 1));
1422 return TEST_SUCCESS
;
1425 static test_return_t
quit_test(memcached_st
*memc
)
1427 memcached_return_t rc
;
1428 const char *key
= "fudge";
1429 const char *value
= "sanford and sun";
1431 rc
= memcached_set(memc
, key
, strlen(key
),
1432 value
, strlen(value
),
1433 (time_t)10, (uint32_t)3);
1434 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1435 memcached_quit(memc
);
1437 rc
= memcached_set(memc
, key
, strlen(key
),
1438 value
, strlen(value
),
1439 (time_t)50, (uint32_t)9);
1440 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1442 return TEST_SUCCESS
;
1445 static test_return_t
mget_result_test(memcached_st
*memc
)
1447 memcached_return_t rc
;
1448 const char *keys
[]= {"fudge", "son", "food"};
1449 size_t key_length
[]= {5, 3, 4};
1452 memcached_result_st results_obj
;
1453 memcached_result_st
*results
;
1455 results
= memcached_result_create(memc
, &results_obj
);
1457 test_true(&results_obj
== results
);
1459 /* We need to empty the server before continueing test */
1460 rc
= memcached_flush(memc
, 0);
1461 test_true(rc
== MEMCACHED_SUCCESS
);
1463 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1464 test_true(rc
== MEMCACHED_SUCCESS
);
1466 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1471 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1472 test_true(!results
);
1473 test_true(rc
== MEMCACHED_END
);
1475 for (x
= 0; x
< 3; x
++)
1477 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1478 keys
[x
], key_length
[x
],
1479 (time_t)50, (uint32_t)9);
1480 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1483 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1484 test_true(rc
== MEMCACHED_SUCCESS
);
1486 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1489 test_true(&results_obj
== results
);
1490 test_true(rc
== MEMCACHED_SUCCESS
);
1491 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1492 test_true(!memcmp(memcached_result_key_value(results
),
1493 memcached_result_value(results
),
1494 memcached_result_length(results
)));
1497 memcached_result_free(&results_obj
);
1499 return TEST_SUCCESS
;
1502 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1504 memcached_return_t rc
;
1505 const char *keys
[]= {"fudge", "son", "food"};
1506 size_t key_length
[]= {5, 3, 4};
1509 memcached_result_st
*results
;
1511 /* We need to empty the server before continueing test */
1512 rc
= memcached_flush(memc
, 0);
1513 test_true(rc
== MEMCACHED_SUCCESS
);
1515 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1516 test_true(rc
== MEMCACHED_SUCCESS
);
1518 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1522 test_true(!results
);
1523 test_true(rc
== MEMCACHED_END
);
1525 for (x
= 0; x
< 3; x
++)
1527 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1528 keys
[x
], key_length
[x
],
1529 (time_t)50, (uint32_t)9);
1530 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1533 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1534 test_true(rc
== MEMCACHED_SUCCESS
);
1537 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1540 test_true(rc
== MEMCACHED_SUCCESS
);
1541 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1542 test_true(!memcmp(memcached_result_key_value(results
),
1543 memcached_result_value(results
),
1544 memcached_result_length(results
)));
1545 memcached_result_free(results
);
1549 return TEST_SUCCESS
;
1552 /* Count the results */
1553 static memcached_return_t
callback_counter(const memcached_st
*ptr
__attribute__((unused
)),
1554 memcached_result_st
*result
__attribute__((unused
)),
1557 size_t *counter
= (size_t *)context
;
1559 *counter
= *counter
+ 1;
1561 return MEMCACHED_SUCCESS
;
1564 static test_return_t
mget_result_function(memcached_st
*memc
)
1566 memcached_return_t rc
;
1567 const char *keys
[]= {"fudge", "son", "food"};
1568 size_t key_length
[]= {5, 3, 4};
1571 memcached_execute_fn callbacks
[1];
1573 /* We need to empty the server before continueing test */
1574 rc
= memcached_flush(memc
, 0);
1575 for (x
= 0; x
< 3; x
++)
1577 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1578 keys
[x
], key_length
[x
],
1579 (time_t)50, (uint32_t)9);
1580 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1583 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1584 test_true(rc
== MEMCACHED_SUCCESS
);
1586 callbacks
[0]= &callback_counter
;
1588 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1590 test_true(counter
== 3);
1592 return TEST_SUCCESS
;
1595 static test_return_t
mget_test(memcached_st
*memc
)
1597 memcached_return_t rc
;
1598 const char *keys
[]= {"fudge", "son", "food"};
1599 size_t key_length
[]= {5, 3, 4};
1603 char return_key
[MEMCACHED_MAX_KEY
];
1604 size_t return_key_length
;
1606 size_t return_value_length
;
1608 /* We need to empty the server before continueing test */
1609 rc
= memcached_flush(memc
, 0);
1610 test_true(rc
== MEMCACHED_SUCCESS
);
1612 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1613 test_true(rc
== MEMCACHED_SUCCESS
);
1615 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1616 &return_value_length
, &flags
, &rc
)) != NULL
)
1618 test_true(return_value
);
1620 test_true(!return_value
);
1621 test_true(return_value_length
== 0);
1622 test_true(rc
== MEMCACHED_END
);
1624 for (x
= 0; x
< 3; x
++)
1626 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1627 keys
[x
], key_length
[x
],
1628 (time_t)50, (uint32_t)9);
1629 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1632 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1633 test_true(rc
== MEMCACHED_SUCCESS
);
1636 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1637 &return_value_length
, &flags
, &rc
)))
1639 test_true(return_value
);
1640 test_true(rc
== MEMCACHED_SUCCESS
);
1641 test_true(return_key_length
== return_value_length
);
1642 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1647 return TEST_SUCCESS
;
1650 static test_return_t
mget_execute(memcached_st
*memc
)
1654 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1658 * I only want to hit _one_ server so I know the number of requests I'm
1659 * sending in the pipeline.
1661 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1662 memc
->number_of_hosts
= 1;
1664 size_t max_keys
= binary
? 20480 : 1;
1667 char **keys
= calloc(max_keys
, sizeof(char*));
1668 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1670 /* First add all of the items.. */
1671 char blob
[1024] = {0};
1672 memcached_return_t rc
;
1673 for (size_t x
= 0; x
< max_keys
; ++x
)
1677 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
1679 test_true(keys
[x
] != NULL
);
1680 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1681 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1684 /* Try to get all of them with a large multiget */
1686 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1687 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1688 max_keys
, callbacks
, &counter
, 1);
1692 test_true(rc
== MEMCACHED_SUCCESS
);
1694 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1695 test_true(rc
== MEMCACHED_END
);
1697 /* Verify that we got all of the items */
1698 test_true(counter
== max_keys
);
1702 test_true(rc
== MEMCACHED_NOT_SUPPORTED
);
1703 test_true(counter
== 0);
1706 /* Release all allocated resources */
1707 for (size_t x
= 0; x
< max_keys
; ++x
)
1714 memc
->number_of_hosts
= number_of_hosts
;
1715 return TEST_SUCCESS
;
1718 static test_return_t
get_stats_keys(memcached_st
*memc
)
1722 memcached_stat_st memc_stat
;
1723 memcached_return_t rc
;
1725 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1726 test_true(rc
== MEMCACHED_SUCCESS
);
1727 for (ptr
= stat_list
; *ptr
; ptr
++)
1732 return TEST_SUCCESS
;
1735 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1737 const char *version_string
;
1739 version_string
= memcached_lib_version();
1741 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1743 return TEST_SUCCESS
;
1746 static test_return_t
get_stats(memcached_st
*memc
)
1750 memcached_return_t rc
;
1751 memcached_stat_st
*memc_stat
;
1753 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1754 test_true(rc
== MEMCACHED_SUCCESS
);
1756 test_true(rc
== MEMCACHED_SUCCESS
);
1757 test_true(memc_stat
);
1759 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1761 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1762 test_true(rc
== MEMCACHED_SUCCESS
);
1763 for (ptr
= stat_list
; *ptr
; ptr
++);
1768 memcached_stat_free(NULL
, memc_stat
);
1770 return TEST_SUCCESS
;
1773 static test_return_t
add_host_test(memcached_st
*memc
)
1776 memcached_server_st
*servers
;
1777 memcached_return_t rc
;
1778 char servername
[]= "0.example.com";
1780 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1782 test_true(1 == memcached_server_list_count(servers
));
1784 for (x
= 2; x
< 20; x
++)
1786 char buffer
[SMALL_STRING_LEN
];
1788 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1789 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1791 test_true(rc
== MEMCACHED_SUCCESS
);
1792 test_true(x
== memcached_server_list_count(servers
));
1795 rc
= memcached_server_push(memc
, servers
);
1796 test_true(rc
== MEMCACHED_SUCCESS
);
1797 rc
= memcached_server_push(memc
, servers
);
1798 test_true(rc
== MEMCACHED_SUCCESS
);
1800 memcached_server_list_free(servers
);
1802 return TEST_SUCCESS
;
1805 static memcached_return_t
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1807 return MEMCACHED_SUCCESS
;
1810 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1812 return MEMCACHED_SUCCESS
;
1815 static test_return_t
callback_test(memcached_st
*memc
)
1817 /* Test User Data */
1821 memcached_return_t rc
;
1823 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1824 test_true(rc
== MEMCACHED_SUCCESS
);
1825 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1826 test_true(*test_ptr
== x
);
1829 /* Test Clone Callback */
1831 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1832 void *clone_cb_ptr
= *(void **)&clone_cb
;
1833 void *temp_function
= NULL
;
1834 memcached_return_t rc
;
1836 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1838 test_true(rc
== MEMCACHED_SUCCESS
);
1839 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1840 test_true(temp_function
== clone_cb_ptr
);
1843 /* Test Cleanup Callback */
1845 memcached_cleanup_fn cleanup_cb
=
1846 (memcached_cleanup_fn
)cleanup_test_callback
;
1847 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1848 void *temp_function
= NULL
;
1849 memcached_return_t rc
;
1851 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1853 test_true(rc
== MEMCACHED_SUCCESS
);
1854 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1855 test_true(temp_function
== cleanup_cb_ptr
);
1858 return TEST_SUCCESS
;
1861 /* We don't test the behavior itself, we test the switches */
1862 static test_return_t
behavior_test(memcached_st
*memc
)
1867 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1868 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1869 test_true(value
== 1);
1871 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1872 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1873 test_true(value
== 1);
1875 set
= MEMCACHED_HASH_MD5
;
1876 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1877 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1878 test_true(value
== MEMCACHED_HASH_MD5
);
1882 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1883 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1884 test_true(value
== 0);
1886 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1887 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1888 test_true(value
== 0);
1890 set
= MEMCACHED_HASH_DEFAULT
;
1891 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1892 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1893 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1895 set
= MEMCACHED_HASH_CRC
;
1896 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1897 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1898 test_true(value
== MEMCACHED_HASH_CRC
);
1900 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1901 test_true(value
> 0);
1903 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1904 test_true(value
> 0);
1906 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1907 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1908 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1910 return TEST_SUCCESS
;
1913 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1915 memcached_return_t rc
;
1919 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
1920 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
1922 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
1924 if (rc
== MEMCACHED_SUCCESS
)
1926 test_true((bool)value
== set
);
1930 test_false((bool)value
== set
);
1933 return TEST_SUCCESS
;
1936 static test_return_t
fetch_all_results(memcached_st
*memc
)
1938 memcached_return_t rc
= MEMCACHED_SUCCESS
;
1939 char return_key
[MEMCACHED_MAX_KEY
];
1940 size_t return_key_length
;
1942 size_t return_value_length
;
1945 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1946 &return_value_length
, &flags
, &rc
)))
1948 test_true(return_value
);
1949 test_true(rc
== MEMCACHED_SUCCESS
);
1953 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
1956 /* Test case provided by Cal Haldenbrand */
1957 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
1959 unsigned int setter
= 1;
1961 unsigned long long total
= 0;
1964 char randomstuff
[6 * 1024];
1965 memcached_return_t rc
;
1967 memset(randomstuff
, 0, 6 * 1024);
1969 /* We just keep looking at the same values over and over */
1972 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1973 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1977 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
1981 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1982 memset(randomstuff
, 0, 6 * 1024);
1983 test_true(size
< 6 * 1024); /* Being safe here */
1985 for (j
= 0 ; j
< size
;j
++)
1986 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1989 snprintf(key
, sizeof(key
), "%u", x
);
1990 rc
= memcached_set(memc
, key
, strlen(key
),
1991 randomstuff
, strlen(randomstuff
), 10, 0);
1992 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1993 /* If we fail, lets try again */
1994 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1995 rc
= memcached_set(memc
, key
, strlen(key
),
1996 randomstuff
, strlen(randomstuff
), 10, 0);
1997 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2000 return TEST_SUCCESS
;
2003 /* Test case provided by Cal Haldenbrand */
2004 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2006 unsigned int setter
;
2010 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2011 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2013 setter
= 20 * 1024576;
2014 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2015 setter
= 20 * 1024576;
2016 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2017 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2018 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2020 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2023 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2025 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2026 char buffer
[SMALL_STRING_LEN
];
2031 memset(buffer
, 0, SMALL_STRING_LEN
);
2033 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2034 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2035 &val_len
, &flags
, &rc
);
2036 if (rc
!= MEMCACHED_SUCCESS
)
2038 if (rc
== MEMCACHED_NOTFOUND
)
2052 return TEST_SUCCESS
;
2055 /* Do a large mget() over all the keys we think exist */
2056 #define KEY_COUNT 3000 // * 1024576
2057 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2059 memcached_return_t rc
;
2060 unsigned int setter
;
2063 size_t key_lengths
[KEY_COUNT
];
2066 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2067 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2069 setter
= 20 * 1024576;
2070 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2071 setter
= 20 * 1024576;
2072 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2073 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2074 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2077 keys
= calloc(KEY_COUNT
, sizeof(char *));
2079 for (x
= 0; x
< KEY_COUNT
; x
++)
2083 snprintf(buffer
, 30, "%u", x
);
2084 keys
[x
]= strdup(buffer
);
2085 key_lengths
[x
]= strlen(keys
[x
]);
2088 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2089 test_true(rc
== MEMCACHED_SUCCESS
);
2091 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2093 for (x
= 0; x
< KEY_COUNT
; x
++)
2097 return TEST_SUCCESS
;
2100 /* Make sure we behave properly if server list has no values */
2101 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2103 memcached_return_t rc
;
2104 const char *keys
[]= {"fudge", "son", "food"};
2105 size_t key_length
[]= {5, 3, 4};
2108 char return_key
[MEMCACHED_MAX_KEY
];
2109 size_t return_key_length
;
2111 size_t return_value_length
;
2113 /* Here we free everything before running a bunch of mget tests */
2114 memcached_servers_reset(memc
);
2117 /* We need to empty the server before continueing test */
2118 rc
= memcached_flush(memc
, 0);
2119 test_true(rc
== MEMCACHED_NO_SERVERS
);
2121 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2122 test_true(rc
== MEMCACHED_NO_SERVERS
);
2124 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2125 &return_value_length
, &flags
, &rc
)) != NULL
)
2127 test_true(return_value
);
2129 test_true(!return_value
);
2130 test_true(return_value_length
== 0);
2131 test_true(rc
== MEMCACHED_NO_SERVERS
);
2133 for (x
= 0; x
< 3; x
++)
2135 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2136 keys
[x
], key_length
[x
],
2137 (time_t)50, (uint32_t)9);
2138 test_true(rc
== MEMCACHED_NO_SERVERS
);
2141 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2142 test_true(rc
== MEMCACHED_NO_SERVERS
);
2145 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2146 &return_value_length
, &flags
, &rc
)))
2148 test_true(return_value
);
2149 test_true(rc
== MEMCACHED_SUCCESS
);
2150 test_true(return_key_length
== return_value_length
);
2151 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2156 return TEST_SUCCESS
;
2159 #define VALUE_SIZE_BUG5 1048064
2160 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2162 memcached_return_t rc
;
2163 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2164 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2165 char return_key
[MEMCACHED_MAX_KEY
];
2166 size_t return_key_length
;
2168 size_t value_length
;
2172 char insert_data
[VALUE_SIZE_BUG5
];
2174 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2175 insert_data
[x
]= (signed char)rand();
2177 memcached_flush(memc
, 0);
2178 value
= memcached_get(memc
, keys
[0], key_length
[0],
2179 &value_length
, &flags
, &rc
);
2180 test_true(value
== NULL
);
2181 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2184 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2185 &value_length
, &flags
, &rc
)))
2187 test_true(count
== 0);
2189 for (x
= 0; x
< 4; x
++)
2191 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2192 insert_data
, VALUE_SIZE_BUG5
,
2193 (time_t)0, (uint32_t)0);
2194 test_true(rc
== MEMCACHED_SUCCESS
);
2197 for (x
= 0; x
< 10; x
++)
2199 value
= memcached_get(memc
, keys
[0], key_length
[0],
2200 &value_length
, &flags
, &rc
);
2204 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2206 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2207 &value_length
, &flags
, &rc
)))
2212 test_true(count
== 4);
2215 return TEST_SUCCESS
;
2218 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2220 memcached_return_t rc
;
2221 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2222 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2223 char return_key
[MEMCACHED_MAX_KEY
];
2224 size_t return_key_length
;
2226 size_t value_length
;
2230 char insert_data
[VALUE_SIZE_BUG5
];
2232 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2233 insert_data
[x
]= (signed char)rand();
2235 memcached_flush(memc
, 0);
2236 value
= memcached_get(memc
, keys
[0], key_length
[0],
2237 &value_length
, &flags
, &rc
);
2238 test_true(value
== NULL
);
2239 test_true(rc
== MEMCACHED_NOTFOUND
);
2240 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2241 test_true(rc
== MEMCACHED_SUCCESS
);
2244 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2245 &value_length
, &flags
, &rc
)))
2247 test_true(count
== 0);
2248 test_true(rc
== MEMCACHED_END
);
2250 for (x
= 0; x
< 4; x
++)
2252 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2253 insert_data
, VALUE_SIZE_BUG5
,
2254 (time_t)0, (uint32_t)0);
2255 test_true(rc
== MEMCACHED_SUCCESS
);
2258 for (x
= 0; x
< 2; x
++)
2260 value
= memcached_get(memc
, keys
[0], key_length
[0],
2261 &value_length
, &flags
, &rc
);
2265 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2266 test_true(rc
== MEMCACHED_SUCCESS
);
2268 /* We test for purge of partial complete fetches */
2269 for (count
= 3; count
; count
--)
2271 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2272 &value_length
, &flags
, &rc
);
2273 test_true(rc
== MEMCACHED_SUCCESS
);
2274 test_true(!(memcmp(value
, insert_data
, value_length
)));
2275 test_true(value_length
);
2280 return TEST_SUCCESS
;
2283 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2285 memcached_return_t rc
;
2287 memcached_st
*memc_clone
;
2289 memcached_server_st
*servers
;
2290 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";
2292 servers
= memcached_servers_parse(server_list
);
2295 mine
= memcached_create(NULL
);
2296 rc
= memcached_server_push(mine
, servers
);
2297 test_true(rc
== MEMCACHED_SUCCESS
);
2298 memcached_server_list_free(servers
);
2301 memc_clone
= memcached_clone(NULL
, mine
);
2303 memcached_quit(mine
);
2304 memcached_quit(memc_clone
);
2307 memcached_free(mine
);
2308 memcached_free(memc_clone
);
2310 return TEST_SUCCESS
;
2313 /* Test flag store/retrieve */
2314 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2316 memcached_return_t rc
;
2317 const char *keys
= "036790384900";
2318 size_t key_length
= strlen(keys
);
2319 char return_key
[MEMCACHED_MAX_KEY
];
2320 size_t return_key_length
;
2322 size_t value_length
;
2325 char insert_data
[VALUE_SIZE_BUG5
];
2327 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2328 insert_data
[x
]= (signed char)rand();
2330 memcached_flush(memc
, 0);
2333 rc
= memcached_set(memc
, keys
, key_length
,
2334 insert_data
, VALUE_SIZE_BUG5
,
2336 test_true(rc
== MEMCACHED_SUCCESS
);
2339 value
= memcached_get(memc
, keys
, key_length
,
2340 &value_length
, &flags
, &rc
);
2341 test_true(flags
== 245);
2345 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2348 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2349 &value_length
, &flags
, &rc
);
2350 test_true(flags
== 245);
2355 return TEST_SUCCESS
;
2358 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2360 memcached_return_t rc
;
2361 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2362 size_t key_length
[3];
2367 char return_key
[MEMCACHED_MAX_KEY
];
2368 size_t return_key_length
;
2370 size_t return_value_length
;
2373 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2374 key_length
[1]= strlen("fudge&*@#");
2375 key_length
[2]= strlen("for^#@&$not");
2378 for (x
= 0; x
< 3; x
++)
2380 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2381 keys
[x
], key_length
[x
],
2382 (time_t)50, (uint32_t)9);
2383 test_true(rc
== MEMCACHED_SUCCESS
);
2386 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2387 test_true(rc
== MEMCACHED_SUCCESS
);
2389 /* We need to empty the server before continueing test */
2390 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2391 &return_value_length
, &flags
, &rc
)) != NULL
)
2393 test_true(return_value
);
2397 test_true(count
== 3);
2399 return TEST_SUCCESS
;
2402 /* We are testing with aggressive timeout to get failures */
2403 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2405 const char *key
= "foo";
2407 size_t value_length
= 512;
2410 memcached_return_t rc
;
2411 unsigned int set
= 1;
2412 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2415 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2416 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2418 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2421 value
= (char*)malloc(value_length
* sizeof(char));
2423 for (x
= 0; x
< value_length
; x
++)
2424 value
[x
]= (char) (x
% 127);
2426 for (x
= 1; x
<= 100000; ++x
)
2428 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2430 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2431 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2433 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2438 memcached_free(mclone
);
2440 return TEST_SUCCESS
;
2444 We are looking failures in the async protocol
2446 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2448 const char *key
= "foo";
2450 size_t value_length
= 512;
2453 memcached_return_t rc
;
2454 unsigned int set
= 1;
2456 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2458 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2459 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2461 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2464 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2466 test_true(timeout
== -1);
2468 value
= (char*)malloc(value_length
* sizeof(char));
2470 for (x
= 0; x
< value_length
; x
++)
2471 value
[x
]= (char) (x
% 127);
2473 for (x
= 1; x
<= 100000; ++x
)
2475 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2479 memcached_free(mclone
);
2481 return TEST_SUCCESS
;
2485 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2487 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2489 memcached_return_t rc
;
2491 size_t value_length
;
2493 uint64_t number_value
;
2495 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2496 &value_length
, &flags
, &rc
);
2497 test_true(value
== NULL
);
2498 test_true(rc
== MEMCACHED_NOTFOUND
);
2500 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2503 test_true(value
== NULL
);
2504 /* The binary protocol will set the key if it doesn't exist */
2505 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2507 test_true(rc
== MEMCACHED_SUCCESS
);
2511 test_true(rc
== MEMCACHED_NOTFOUND
);
2514 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2516 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2517 &value_length
, &flags
, &rc
);
2519 test_true(rc
== MEMCACHED_SUCCESS
);
2522 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2524 test_true(number_value
== 2);
2525 test_true(rc
== MEMCACHED_SUCCESS
);
2527 return TEST_SUCCESS
;
2531 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2532 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2534 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2536 char key
[] = "key34567890";
2538 memcached_return_t rc
;
2539 size_t overflowSize
;
2541 char commandFirst
[]= "set key34567890 0 0 ";
2542 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2543 size_t commandLength
;
2546 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2548 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2550 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2552 overflow
= malloc(testSize
);
2553 test_true(overflow
!= NULL
);
2555 memset(overflow
, 'x', testSize
);
2556 rc
= memcached_set(memc
, key
, strlen(key
),
2557 overflow
, testSize
, 0, 0);
2558 test_true(rc
== MEMCACHED_SUCCESS
);
2562 return TEST_SUCCESS
;
2567 Test values of many different sizes
2568 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2569 set key34567890 0 0 8169 \r\n
2570 is sent followed by buffer of size 8169, followed by 8169
2572 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2575 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2576 memcached_return_t rc
;
2577 const char *key
= "foo";
2579 size_t value_length
= 18000;
2581 size_t string_length
;
2584 size_t current_length
;
2586 value
= (char*)malloc(value_length
);
2589 for (x
= 0; x
< value_length
; x
++)
2590 value
[x
] = (char) (x
% 127);
2592 for (current_length
= 0; current_length
< value_length
; current_length
++)
2594 rc
= memcached_set(memc
, key
, strlen(key
),
2595 value
, current_length
,
2596 (time_t)0, (uint32_t)0);
2597 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2599 string
= memcached_get(memc
, key
, strlen(key
),
2600 &string_length
, &flags
, &rc
);
2602 test_true(rc
== MEMCACHED_SUCCESS
);
2603 test_true(string_length
== current_length
);
2604 test_true(!memcmp(string
, value
, string_length
));
2611 return TEST_SUCCESS
;
2615 Look for zero length value problems
2617 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2620 memcached_return_t rc
;
2621 const char *key
= "mykey";
2626 for (x
= 0; x
< 2; x
++)
2628 rc
= memcached_set(memc
, key
, strlen(key
),
2630 (time_t)0, (uint32_t)0);
2632 test_true(rc
== MEMCACHED_SUCCESS
);
2634 value
= memcached_get(memc
, key
, strlen(key
),
2635 &length
, &flags
, &rc
);
2637 test_true(rc
== MEMCACHED_SUCCESS
);
2638 test_true(value
== NULL
);
2639 test_true(length
== 0);
2640 test_true(flags
== 0);
2642 value
= memcached_get(memc
, key
, strlen(key
),
2643 &length
, &flags
, &rc
);
2645 test_true(rc
== MEMCACHED_SUCCESS
);
2646 test_true(value
== NULL
);
2647 test_true(length
== 0);
2648 test_true(flags
== 0);
2651 return TEST_SUCCESS
;
2654 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2655 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2657 memcached_return_t rc
;
2658 const char *key
= "mykey";
2663 rc
= memcached_set(memc
, key
, strlen(key
),
2665 (time_t)0, UINT32_MAX
);
2667 test_true(rc
== MEMCACHED_SUCCESS
);
2669 value
= memcached_get(memc
, key
, strlen(key
),
2670 &length
, &flags
, &rc
);
2672 test_true(rc
== MEMCACHED_SUCCESS
);
2673 test_true(value
== NULL
);
2674 test_true(length
== 0);
2675 test_true(flags
== UINT32_MAX
);
2677 return TEST_SUCCESS
;
2681 /* Check the validity of chinese key*/
2682 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2684 memcached_return_t rc
;
2685 const char *key
= "豆瓣";
2686 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2691 rc
= memcached_set(memc
, key
, strlen(key
),
2692 value
, strlen(value
),
2695 test_true(rc
== MEMCACHED_SUCCESS
);
2697 value2
= memcached_get(memc
, key
, strlen(key
),
2698 &length
, &flags
, &rc
);
2700 test_true(length
==strlen(value
));
2701 test_true(rc
== MEMCACHED_SUCCESS
);
2702 test_true(memcmp(value
, value2
, length
)==0);
2705 return TEST_SUCCESS
;
2713 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2716 const memcached_server_st
*server
;
2717 memcached_return_t res
;
2721 memc
= memcached_create(NULL
);
2722 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2723 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2725 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2727 return TEST_SUCCESS
;
2730 /* CAS test from Andei */
2731 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2733 memcached_return_t status
;
2734 memcached_result_st
*result
, result_obj
;
2735 const char *key
= "abc";
2736 size_t key_len
= strlen("abc");
2737 const char *value
= "foobar";
2738 size_t value_len
= strlen(value
);
2740 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2742 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2743 test_true(status
== MEMCACHED_SUCCESS
);
2745 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2746 test_true(status
== MEMCACHED_SUCCESS
);
2748 result
= memcached_result_create(memc
, &result_obj
);
2751 memcached_result_create(memc
, &result_obj
);
2752 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2755 test_true(status
== MEMCACHED_SUCCESS
);
2757 memcached_result_free(result
);
2759 return TEST_SUCCESS
;
2762 #include "ketama_test_cases.h"
2763 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2765 memcached_return_t rc
;
2768 memcached_server_st
*server_pool
;
2773 memc
= memcached_create(NULL
);
2776 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2777 test_true(rc
== MEMCACHED_SUCCESS
);
2779 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2780 test_true(value
== 1);
2782 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2783 test_true(rc
== MEMCACHED_SUCCESS
);
2785 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2786 test_true(value
== MEMCACHED_HASH_MD5
);
2788 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");
2789 memcached_server_push(memc
, server_pool
);
2791 /* verify that the server list was parsed okay. */
2792 test_true(memcached_server_count(memc
) == 8);
2793 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2794 test_true(server_pool
[0].port
== 11211);
2795 test_true(server_pool
[0].weight
== 600);
2796 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2797 test_true(server_pool
[2].port
== 11211);
2798 test_true(server_pool
[2].weight
== 200);
2799 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2800 test_true(server_pool
[7].port
== 11211);
2801 test_true(server_pool
[7].weight
== 100);
2803 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2804 * us test the boundary wraparound.
2806 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2808 /* verify the standard ketama set. */
2809 for (x
= 0; x
< 99; x
++)
2811 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2813 memcached_server_instance_st instance
=
2814 memcached_server_instance_by_position(memc
, server_idx
);
2816 const char *hostname
= memcached_server_name(instance
);
2817 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2820 memcached_server_list_free(server_pool
);
2821 memcached_free(memc
);
2823 return TEST_SUCCESS
;
2826 /* Large mget() of missing keys with binary proto
2828 * If many binary quiet commands (such as getq's in an mget) fill the output
2829 * buffer and the server chooses not to respond, memcached_flush hangs. See
2830 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2833 /* sighandler_t function that always asserts false */
2834 static void fail(int unused
__attribute__((unused
)))
2840 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2842 memcached_return_t rc
;
2845 size_t* key_lengths
;
2846 void (*oldalarm
)(int);
2847 memcached_st
*memc_clone
;
2849 memc_clone
= memcached_clone(NULL
, memc
);
2850 test_true(memc_clone
);
2852 /* only binproto uses getq for mget */
2853 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2855 /* empty the cache to ensure misses (hence non-responses) */
2856 rc
= memcached_flush(memc_clone
, 0);
2857 test_true(rc
== MEMCACHED_SUCCESS
);
2859 key_lengths
= calloc(key_count
, sizeof(size_t));
2860 keys
= calloc(key_count
, sizeof(char *));
2862 for (x
= 0; x
< key_count
; x
++)
2866 snprintf(buffer
, 30, "%u", x
);
2867 keys
[x
]= strdup(buffer
);
2868 key_lengths
[x
]= strlen(keys
[x
]);
2871 oldalarm
= signal(SIGALRM
, fail
);
2874 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2875 test_true(rc
== MEMCACHED_SUCCESS
);
2878 signal(SIGALRM
, oldalarm
);
2880 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2882 for (x
= 0; x
< key_count
; x
++)
2887 memcached_free(memc_clone
);
2889 return TEST_SUCCESS
;
2892 static test_return_t
pre_binary(memcached_st
*memc
);
2894 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
2896 test_return_t test_rc
;
2897 test_rc
= pre_binary(memc
);
2899 if (test_rc
!= TEST_SUCCESS
)
2904 /* should work as of r580 */
2905 rc
= _user_supplied_bug21(memc
, 10);
2906 test_true(rc
== TEST_SUCCESS
);
2908 /* should fail as of r580 */
2909 rc
= _user_supplied_bug21(memc
, 1000);
2910 test_true(rc
== TEST_SUCCESS
);
2912 return TEST_SUCCESS
;
2915 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
2918 memcached_server_instance_st instance
;
2920 memcached_return_t rc
;
2921 memcached_st
*memc
= memcached_create(NULL
);
2924 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2925 test_true(rc
== MEMCACHED_SUCCESS
);
2927 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2928 test_true(value
== 1);
2930 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2931 test_true(rc
== MEMCACHED_SUCCESS
);
2933 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2934 test_true(value
== MEMCACHED_HASH_MD5
);
2936 /* server should be removed when in delay */
2937 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2938 test_true(rc
== MEMCACHED_SUCCESS
);
2940 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2941 test_true(value
== 1);
2943 memcached_server_st
*server_pool
;
2944 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");
2945 memcached_server_push(memc
, server_pool
);
2947 /* verify that the server list was parsed okay. */
2948 test_true(memcached_server_count(memc
) == 8);
2949 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2950 test_true(server_pool
[0].port
== 11211);
2951 test_true(server_pool
[0].weight
== 600);
2952 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2953 test_true(server_pool
[2].port
== 11211);
2954 test_true(server_pool
[2].weight
== 200);
2955 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2956 test_true(server_pool
[7].port
== 11211);
2957 test_true(server_pool
[7].weight
== 100);
2959 instance
= memcached_server_instance_by_position(memc
, 2);
2960 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
2961 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2964 This would not work if there were only two hosts.
2966 for (size_t x
= 0; x
< 99; x
++)
2968 memcached_autoeject(memc
);
2969 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2970 test_true(server_idx
!= 2);
2973 /* and re-added when it's back. */
2974 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
2975 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2976 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
2977 memc
->distribution
);
2978 for (size_t x
= 0; x
< 99; x
++)
2980 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2981 // We re-use instance from above.
2983 memcached_server_instance_by_position(memc
, server_idx
);
2984 const char *hostname
= memcached_server_name(instance
);
2985 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
2988 memcached_server_list_free(server_pool
);
2989 memcached_free(memc
);
2991 return TEST_SUCCESS
;
2994 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
2998 memcached_return_t rc
;
2999 memcached_st
*memc
= memcached_create(NULL
);
3003 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3004 test_true(rc
== MEMCACHED_SUCCESS
);
3006 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3007 test_true(value
== 1);
3009 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3010 test_true(rc
== MEMCACHED_SUCCESS
);
3012 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3013 test_true(value
== MEMCACHED_HASH_MD5
);
3016 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3018 memcached_server_st
*server_pool
;
3019 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");
3020 memcached_server_push(memc
, server_pool
);
3022 // @todo this needs to be refactored to actually test something.
3025 if ((fp
= fopen("ketama_keys.txt", "w")))
3029 printf("cannot write to file ketama_keys.txt");
3030 return TEST_FAILURE
;
3033 for (int x
= 0; x
< 10000; x
++)
3036 sprintf(key
, "%d", x
);
3038 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3039 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3040 in_port_t port
= memc
->hosts
[server_idx
].port
;
3041 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3045 memcached_server_list_free(server_pool
);
3046 memcached_free(memc
);
3048 return TEST_SUCCESS
;
3052 static test_return_t
result_static(memcached_st
*memc
)
3054 memcached_result_st result
;
3055 memcached_result_st
*result_ptr
;
3057 result_ptr
= memcached_result_create(memc
, &result
);
3058 test_true(result
.options
.is_allocated
== false);
3059 test_true(memcached_is_initialized(&result
) == true);
3060 test_true(result_ptr
);
3061 test_true(result_ptr
== &result
);
3063 memcached_result_free(&result
);
3065 test_true(result
.options
.is_allocated
== false);
3066 test_true(memcached_is_initialized(&result
) == false);
3068 return TEST_SUCCESS
;
3071 static test_return_t
result_alloc(memcached_st
*memc
)
3073 memcached_result_st
*result_ptr
;
3075 result_ptr
= memcached_result_create(memc
, NULL
);
3076 test_true(result_ptr
);
3077 test_true(result_ptr
->options
.is_allocated
== true);
3078 test_true(memcached_is_initialized(result_ptr
) == true);
3079 memcached_result_free(result_ptr
);
3081 return TEST_SUCCESS
;
3084 static test_return_t
string_static_null(memcached_st
*memc
)
3086 memcached_string_st string
;
3087 memcached_string_st
*string_ptr
;
3089 string_ptr
= memcached_string_create(memc
, &string
, 0);
3090 test_true(string
.options
.is_initialized
== true);
3091 test_true(string_ptr
);
3093 /* The following two better be the same! */
3094 test_true(memcached_is_allocated(string_ptr
) == false);
3095 test_true(memcached_is_allocated(&string
) == false);
3096 test_true(&string
== string_ptr
);
3098 test_true(string
.options
.is_initialized
== true);
3099 test_true(memcached_is_initialized(&string
) == true);
3100 memcached_string_free(&string
);
3101 test_true(memcached_is_initialized(&string
) == false);
3103 return TEST_SUCCESS
;
3106 static test_return_t
string_alloc_null(memcached_st
*memc
)
3108 memcached_string_st
*string
;
3110 string
= memcached_string_create(memc
, NULL
, 0);
3112 test_true(memcached_is_allocated(string
) == true);
3113 test_true(memcached_is_initialized(string
) == true);
3114 memcached_string_free(string
);
3116 return TEST_SUCCESS
;
3119 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3121 memcached_string_st
*string
;
3123 string
= memcached_string_create(memc
, NULL
, 1024);
3125 test_true(memcached_is_allocated(string
) == true);
3126 test_true(memcached_is_initialized(string
) == true);
3127 memcached_string_free(string
);
3129 return TEST_SUCCESS
;
3132 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3134 memcached_string_st
*string
;
3136 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3137 test_true(string
== NULL
);
3139 return TEST_SUCCESS
;
3142 static test_return_t
string_alloc_append(memcached_st
*memc
)
3145 char buffer
[SMALL_STRING_LEN
];
3146 memcached_string_st
*string
;
3148 /* Ring the bell! */
3149 memset(buffer
, 6, SMALL_STRING_LEN
);
3151 string
= memcached_string_create(memc
, NULL
, 100);
3153 test_true(memcached_is_allocated(string
) == true);
3154 test_true(memcached_is_initialized(string
) == true);
3156 for (x
= 0; x
< 1024; x
++)
3158 memcached_return_t rc
;
3159 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3160 test_true(rc
== MEMCACHED_SUCCESS
);
3162 test_true(memcached_is_allocated(string
) == true);
3163 memcached_string_free(string
);
3165 return TEST_SUCCESS
;
3168 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3170 memcached_return_t rc
;
3172 char buffer
[SMALL_STRING_LEN
];
3173 memcached_string_st
*string
;
3175 /* Ring the bell! */
3176 memset(buffer
, 6, SMALL_STRING_LEN
);
3178 string
= memcached_string_create(memc
, NULL
, 100);
3180 test_true(memcached_is_allocated(string
) == true);
3181 test_true(memcached_is_initialized(string
) == true);
3183 for (x
= 0; x
< 1024; x
++)
3185 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3186 test_true(rc
== MEMCACHED_SUCCESS
);
3188 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3189 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3190 test_true(memcached_is_allocated(string
) == true);
3191 memcached_string_free(string
);
3193 return TEST_SUCCESS
;
3196 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3198 pairs_free(global_pairs
);
3200 return TEST_SUCCESS
;
3203 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3205 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3206 global_count
= GLOBAL_COUNT
;
3208 for (size_t x
= 0; x
< global_count
; x
++)
3210 global_keys
[x
]= global_pairs
[x
].key
;
3211 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3214 return TEST_SUCCESS
;
3217 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3219 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3220 global_count
= GLOBAL2_COUNT
;
3222 for (size_t x
= 0; x
< global_count
; x
++)
3224 global_keys
[x
]= global_pairs
[x
].key
;
3225 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3228 return TEST_SUCCESS
;
3231 static test_return_t
generate_data(memcached_st
*memc
)
3233 execute_set(memc
, global_pairs
, global_count
);
3235 return TEST_SUCCESS
;
3238 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3240 memcached_stat_st
*stat_p
;
3241 memcached_return_t rc
;
3242 uint32_t host_index
= 0;
3243 execute_set(memc
, global_pairs
, global_count
);
3245 //TODO: hosts used size stats
3246 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3249 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3251 /* This test was changes so that "make test" would work properlly */
3253 memcached_server_instance_st instance
=
3254 memcached_server_instance_by_position(memc
, host_index
);
3256 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3258 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3261 memcached_stat_free(NULL
, stat_p
);
3263 return TEST_SUCCESS
;
3265 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3270 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3271 generate_data(memc
);
3273 return TEST_SUCCESS
;
3276 static test_return_t
get_read_count(memcached_st
*memc
)
3278 memcached_return_t rc
;
3279 memcached_st
*memc_clone
;
3281 memc_clone
= memcached_clone(NULL
, memc
);
3282 test_true(memc_clone
);
3284 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3288 size_t return_value_length
;
3292 for (size_t x
= count
= 0; x
< global_count
; x
++)
3294 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3295 &return_value_length
, &flags
, &rc
);
3296 if (rc
== MEMCACHED_SUCCESS
)
3305 memcached_free(memc_clone
);
3307 return TEST_SUCCESS
;
3310 static test_return_t
get_read(memcached_st
*memc
)
3312 memcached_return_t rc
;
3316 size_t return_value_length
;
3319 for (size_t x
= 0; x
< global_count
; x
++)
3321 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3322 &return_value_length
, &flags
, &rc
);
3324 test_true(return_value);
3325 test_true(rc == MEMCACHED_SUCCESS);
3327 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3332 return TEST_SUCCESS
;
3335 static test_return_t
mget_read(memcached_st
*memc
)
3337 memcached_return_t rc
;
3339 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3340 test_true(rc
== MEMCACHED_SUCCESS
);
3341 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
3343 return TEST_SUCCESS
;
3346 static test_return_t
mget_read_result(memcached_st
*memc
)
3348 memcached_return_t rc
;
3350 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3351 test_true(rc
== MEMCACHED_SUCCESS
);
3352 /* Turn this into a help function */
3354 memcached_result_st results_obj
;
3355 memcached_result_st
*results
;
3357 results
= memcached_result_create(memc
, &results_obj
);
3359 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3362 test_true(rc
== MEMCACHED_SUCCESS
);
3365 memcached_result_free(&results_obj
);
3368 return TEST_SUCCESS
;
3371 static test_return_t
mget_read_function(memcached_st
*memc
)
3373 memcached_return_t rc
;
3375 memcached_execute_fn callbacks
[1];
3377 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3378 test_true(rc
== MEMCACHED_SUCCESS
);
3380 callbacks
[0]= &callback_counter
;
3382 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3384 return TEST_SUCCESS
;
3387 static test_return_t
delete_generate(memcached_st
*memc
)
3389 for (size_t x
= 0; x
< global_count
; x
++)
3391 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3394 return TEST_SUCCESS
;
3397 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3402 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3404 for (size_t x
= 0; x
< global_count
; x
++)
3406 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3409 return TEST_SUCCESS
;
3412 static test_return_t
add_host_test1(memcached_st
*memc
)
3414 memcached_return_t rc
;
3415 char servername
[]= "0.example.com";
3416 memcached_server_st
*servers
;
3418 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3420 test_true(1 == memcached_server_list_count(servers
));
3422 for (size_t x
= 2; x
< 20; x
++)
3424 char buffer
[SMALL_STRING_LEN
];
3426 snprintf(buffer
, SMALL_STRING_LEN
, "%zu.example.com", 400+x
);
3427 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3429 test_true(rc
== MEMCACHED_SUCCESS
);
3430 test_true(x
== memcached_server_list_count(servers
));
3433 rc
= memcached_server_push(memc
, servers
);
3434 test_true(rc
== MEMCACHED_SUCCESS
);
3435 rc
= memcached_server_push(memc
, servers
);
3436 test_true(rc
== MEMCACHED_SUCCESS
);
3438 memcached_server_list_free(servers
);
3440 return TEST_SUCCESS
;
3443 static test_return_t
pre_nonblock(memcached_st
*memc
)
3445 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3447 return TEST_SUCCESS
;
3450 static test_return_t
pre_cork(memcached_st
*memc
)
3452 memcached_return_t rc
;
3455 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3458 return TEST_SKIPPED
;
3461 if (rc
== MEMCACHED_SUCCESS
)
3462 return TEST_SUCCESS
;
3464 return TEST_SKIPPED
;
3467 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3474 return TEST_SKIPPED
;
3477 if (rc
!= TEST_SUCCESS
)
3480 return pre_nonblock(memc
);
3483 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3485 memcached_return_t rc
= MEMCACHED_FAILURE
;
3486 memcached_st
*memc_clone
;
3487 memcached_server_instance_st instance
;
3489 memc_clone
= memcached_clone(NULL
, memc
);
3490 test_true(memc_clone
);
3491 // The memcached_version needs to be done on a clone, because the server
3492 // will not toggle protocol on an connection.
3493 memcached_version(memc_clone
);
3495 instance
= memcached_server_instance_by_position(memc_clone
, 0);
3497 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
3499 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3500 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3501 test_true(rc
== MEMCACHED_SUCCESS
);
3502 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3506 return TEST_SKIPPED
;
3509 memcached_free(memc_clone
);
3511 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3514 static test_return_t
pre_murmur(memcached_st
*memc
)
3516 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3518 return TEST_SUCCESS
;
3521 static test_return_t
pre_jenkins(memcached_st
*memc
)
3523 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3525 return TEST_SUCCESS
;
3529 static test_return_t
pre_md5(memcached_st
*memc
)
3531 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3533 return TEST_SUCCESS
;
3536 static test_return_t
pre_crc(memcached_st
*memc
)
3538 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3540 return TEST_SUCCESS
;
3543 static test_return_t
pre_hsieh(memcached_st
*memc
)
3545 #ifdef HAVE_HSIEH_HASH
3546 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3547 return TEST_SUCCESS
;
3550 return TEST_SKIPPED
;
3554 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3556 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3558 return TEST_SUCCESS
;
3561 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3563 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3565 return TEST_SUCCESS
;
3568 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3570 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3572 return TEST_SUCCESS
;
3575 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3577 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3579 return TEST_SUCCESS
;
3582 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3584 memcached_return_t rc
;
3587 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3588 test_true(rc
== MEMCACHED_SUCCESS
);
3590 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3591 test_true(value
== 1);
3593 return TEST_SUCCESS
;
3596 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3598 memcached_return_t rc
;
3601 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3602 test_true(rc
== MEMCACHED_SUCCESS
);
3604 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3605 test_true(value
== 1);
3607 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3608 test_true(rc
== MEMCACHED_SUCCESS
);
3610 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3611 test_true(value
== MEMCACHED_HASH_MD5
);
3613 return TEST_SUCCESS
;
3617 @note This should be testing to see if the server really supports the binary protocol.
3619 static test_return_t
pre_binary(memcached_st
*memc
)
3621 memcached_return_t rc
= MEMCACHED_FAILURE
;
3622 memcached_st
*memc_clone
;
3623 memcached_server_instance_st instance
;
3625 memc_clone
= memcached_clone(NULL
, memc
);
3626 test_true(memc_clone
);
3627 // The memcached_version needs to be done on a clone, because the server
3628 // will not toggle protocol on an connection.
3629 memcached_version(memc_clone
);
3631 instance
= memcached_server_instance_by_position(memc_clone
, 0);
3633 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
3635 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3636 test_true(rc
== MEMCACHED_SUCCESS
);
3637 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3640 memcached_free(memc_clone
);
3642 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3646 static test_return_t
pre_replication(memcached_st
*memc
)
3648 test_return_t test_rc
;
3649 test_rc
= pre_binary(memc
);
3651 if (test_rc
!= TEST_SUCCESS
)
3655 * Make sure that we store the item on all servers
3656 * (master + replicas == number of servers)
3658 memcached_return_t rc
;
3659 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3660 memcached_server_count(memc
) - 1);
3661 test_true(rc
== MEMCACHED_SUCCESS
);
3662 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3664 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3668 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3672 rc
= pre_replication(memc
);
3673 if (rc
!= TEST_SUCCESS
)
3676 rc
= pre_nonblock(memc
);
3682 static void my_free(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, void *context
)
3685 #ifdef HARD_MALLOC_TESTS
3686 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3694 static void *my_malloc(const memcached_st
*ptr
__attribute__((unused
)), const size_t size
, void *context
)
3697 #ifdef HARD_MALLOC_TESTS
3698 void *ret
= malloc(size
+ 8);
3701 ret
= (void*)((caddr_t
)ret
+ 8);
3704 void *ret
= malloc(size
);
3709 memset(ret
, 0xff, size
);
3716 static void *my_realloc(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
, void *context
)
3719 #ifdef HARD_MALLOC_TESTS
3720 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3721 void *nmem
= realloc(real_ptr
, size
+ 8);
3726 ret
= (void*)((caddr_t
)nmem
+ 8);
3731 return realloc(mem
, size
);
3736 static void *my_calloc(const memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
, void *context
)
3739 #ifdef HARD_MALLOC_TESTS
3740 void *mem
= my_malloc(ptr
, nelem
* size
);
3743 memset(mem
, 0, nelem
* size
);
3748 return calloc(nelem
, size
);
3753 static test_return_t
set_prefix(memcached_st
*memc
)
3755 memcached_return_t rc
;
3756 const char *key
= "mine";
3759 /* Make sure be default none exists */
3760 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3761 test_true(rc
== MEMCACHED_FAILURE
);
3763 /* Test a clean set */
3764 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3765 test_true(rc
== MEMCACHED_SUCCESS
);
3767 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3768 test_true(memcmp(value
, key
, 4) == 0);
3769 test_true(rc
== MEMCACHED_SUCCESS
);
3771 /* Test that we can turn it off */
3772 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3773 test_true(rc
== MEMCACHED_SUCCESS
);
3775 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3776 test_true(rc
== MEMCACHED_FAILURE
);
3778 /* Now setup for main test */
3779 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3780 test_true(rc
== MEMCACHED_SUCCESS
);
3782 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3783 test_true(rc
== MEMCACHED_SUCCESS
);
3784 test_true(memcmp(value
, key
, 4) == 0);
3786 /* Set to Zero, and then Set to something too large */
3789 memset(long_key
, 0, 255);
3791 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3792 test_true(rc
== MEMCACHED_SUCCESS
);
3794 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3795 test_true(rc
== MEMCACHED_FAILURE
);
3796 test_true(value
== NULL
);
3798 /* Test a long key for failure */
3799 /* TODO, extend test to determine based on setting, what result should be */
3800 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3801 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3802 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3803 test_true(rc
== MEMCACHED_SUCCESS
);
3805 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3806 strcpy(long_key
, "This is more then the allotted number of characters");
3807 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3808 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3810 /* Test for a bad prefix, but with a short key */
3811 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3812 test_true(rc
== MEMCACHED_SUCCESS
);
3814 strcpy(long_key
, "dog cat");
3815 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3816 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3819 return TEST_SUCCESS
;
3823 #ifdef MEMCACHED_ENABLE_DEPRECATED
3824 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3826 void *test_ptr
= NULL
;
3829 memcached_malloc_fn malloc_cb
=
3830 (memcached_malloc_fn
)my_malloc
;
3831 cb_ptr
= *(void **)&malloc_cb
;
3832 memcached_return_t rc
;
3834 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3835 test_true(rc
== MEMCACHED_SUCCESS
);
3836 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3837 test_true(rc
== MEMCACHED_SUCCESS
);
3838 test_true(test_ptr
== cb_ptr
);
3842 memcached_realloc_fn realloc_cb
=
3843 (memcached_realloc_fn
)my_realloc
;
3844 cb_ptr
= *(void **)&realloc_cb
;
3845 memcached_return_t rc
;
3847 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3848 test_true(rc
== MEMCACHED_SUCCESS
);
3849 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3850 test_true(rc
== MEMCACHED_SUCCESS
);
3851 test_true(test_ptr
== cb_ptr
);
3855 memcached_free_fn free_cb
=
3856 (memcached_free_fn
)my_free
;
3857 cb_ptr
= *(void **)&free_cb
;
3858 memcached_return_t rc
;
3860 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3861 test_true(rc
== MEMCACHED_SUCCESS
);
3862 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3863 test_true(rc
== MEMCACHED_SUCCESS
);
3864 test_true(test_ptr
== cb_ptr
);
3867 return TEST_SUCCESS
;
3872 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3874 memcached_return_t rc
;
3875 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3876 my_realloc
, my_calloc
, NULL
);
3877 test_true(rc
== MEMCACHED_FAILURE
);
3879 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3880 my_realloc
, my_calloc
, NULL
);
3882 memcached_malloc_fn mem_malloc
;
3883 memcached_free_fn mem_free
;
3884 memcached_realloc_fn mem_realloc
;
3885 memcached_calloc_fn mem_calloc
;
3886 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3887 &mem_realloc
, &mem_calloc
);
3889 test_true(mem_malloc
== my_malloc
);
3890 test_true(mem_realloc
== my_realloc
);
3891 test_true(mem_calloc
== my_calloc
);
3892 test_true(mem_free
== my_free
);
3894 return TEST_SUCCESS
;
3897 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
3900 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3901 memcached_hash_t hash
;
3902 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3903 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3906 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3907 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3909 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3911 if (hash
!= MEMCACHED_HASH_CRC
)
3912 return TEST_SKIPPED
;
3914 return TEST_SUCCESS
;
3917 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
3920 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3921 memcached_hash_t hash
;
3922 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3923 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
3926 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3927 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3929 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3931 if (hash
!= MEMCACHED_HASH_HSIEH
)
3932 return TEST_SKIPPED
;
3935 return TEST_SUCCESS
;
3938 static test_return_t
enable_cas(memcached_st
*memc
)
3940 unsigned int set
= 1;
3942 memcached_server_instance_st instance
=
3943 memcached_server_instance_by_position(memc
, 0);
3945 memcached_version(memc
);
3947 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
3948 || instance
->minor_version
> 2)
3950 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3952 return TEST_SUCCESS
;
3955 return TEST_SKIPPED
;
3958 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
3960 memcached_version(memc
);
3962 memcached_server_instance_st instance
=
3963 memcached_server_instance_by_position(memc
, 0);
3965 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
3966 || instance
->minor_version
> 2)
3967 return TEST_SUCCESS
;
3969 return TEST_SKIPPED
;
3972 static test_return_t
pre_unix_socket(memcached_st
*memc
)
3974 memcached_return_t rc
;
3977 memcached_servers_reset(memc
);
3979 if (stat("/tmp/memcached.socket", &buf
))
3980 return TEST_SKIPPED
;
3982 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3984 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
3987 static test_return_t
pre_nodelay(memcached_st
*memc
)
3989 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3990 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3992 return TEST_SUCCESS
;
3995 static test_return_t
pre_settimer(memcached_st
*memc
)
3997 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3998 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4000 return TEST_SUCCESS
;
4003 static test_return_t
poll_timeout(memcached_st
*memc
)
4009 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4011 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4013 test_true(timeout
== 100);
4015 return TEST_SUCCESS
;
4018 static test_return_t
noreply_test(memcached_st
*memc
)
4020 memcached_return_t ret
;
4021 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4022 test_true(ret
== MEMCACHED_SUCCESS
);
4023 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4024 test_true(ret
== MEMCACHED_SUCCESS
);
4025 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4026 test_true(ret
== MEMCACHED_SUCCESS
);
4027 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4028 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4029 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4031 for (int count
=0; count
< 5; ++count
)
4033 for (size_t x
= 0; x
< 100; ++x
)
4036 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4040 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4043 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4046 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4049 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4052 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4058 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4062 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4063 ** API and is _ONLY_ done this way to verify that the library works the
4064 ** way it is supposed to do!!!!
4067 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4069 memcached_server_instance_st instance
=
4070 memcached_server_instance_by_position(memc
, x
);
4071 no_msg
+=(int)(instance
->cursor_active
);
4074 test_true(no_msg
== 0);
4075 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4078 ** Now validate that all items was set properly!
4080 for (size_t x
= 0; x
< 100; ++x
)
4084 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4087 char* value
=memcached_get(memc
, key
, strlen(key
),
4088 &length
, &flags
, &ret
);
4089 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4092 case 0: /* FALLTHROUGH */
4093 case 1: /* FALLTHROUGH */
4095 test_true(strncmp(value
, key
, len
) == 0);
4096 test_true(len
== length
);
4099 test_true(length
== len
* 2);
4102 test_true(length
== len
* 3);
4112 /* Try setting an illegal cas value (should not return an error to
4113 * the caller (because we don't expect a return message from the server)
4115 const char* keys
[]= {"0"};
4116 size_t lengths
[]= {1};
4119 memcached_result_st results_obj
;
4120 memcached_result_st
*results
;
4121 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4122 test_true(ret
== MEMCACHED_SUCCESS
);
4124 results
= memcached_result_create(memc
, &results_obj
);
4126 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4128 test_true(ret
== MEMCACHED_SUCCESS
);
4129 uint64_t cas
= memcached_result_cas(results
);
4130 memcached_result_free(&results_obj
);
4132 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4133 test_true(ret
== MEMCACHED_SUCCESS
);
4136 * The item will have a new cas value, so try to set it again with the old
4137 * value. This should fail!
4139 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4140 test_true(ret
== MEMCACHED_SUCCESS
);
4141 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4142 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4143 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4146 return TEST_SUCCESS
;
4149 static test_return_t
analyzer_test(memcached_st
*memc
)
4151 memcached_return_t rc
;
4152 memcached_stat_st
*memc_stat
;
4153 memcached_analysis_st
*report
;
4155 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4156 test_true(rc
== MEMCACHED_SUCCESS
);
4157 test_true(memc_stat
);
4159 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4160 test_true(rc
== MEMCACHED_SUCCESS
);
4164 memcached_stat_free(NULL
, memc_stat
);
4166 return TEST_SUCCESS
;
4169 /* Count the objects */
4170 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
__attribute__((unused
)),
4171 const char *key
__attribute__((unused
)),
4172 size_t key_length
__attribute__((unused
)),
4175 size_t *counter
= (size_t *)context
;
4177 *counter
= *counter
+ 1;
4179 return MEMCACHED_SUCCESS
;
4182 static test_return_t
dump_test(memcached_st
*memc
)
4184 memcached_return_t rc
;
4186 memcached_dump_fn callbacks
[1];
4187 test_return_t main_rc
;
4189 callbacks
[0]= &callback_dump_counter
;
4191 /* No support for Binary protocol yet */
4192 if (memc
->flags
.binary_protocol
)
4193 return TEST_SUCCESS
;
4195 main_rc
= set_test3(memc
);
4197 test_true (main_rc
== TEST_SUCCESS
);
4199 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4200 test_true(rc
== MEMCACHED_SUCCESS
);
4202 /* We may have more then 32 if our previous flush has not completed */
4203 test_true(counter
>= 32);
4205 return TEST_SUCCESS
;
4208 #ifdef HAVE_LIBMEMCACHEDUTIL
4209 static void* connection_release(void *arg
)
4212 memcached_pool_st
* pool
;
4217 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4221 static test_return_t
connection_pool_test(memcached_st
*memc
)
4223 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
4224 test_true(pool
!= NULL
);
4225 memcached_st
* mmc
[10];
4226 memcached_return_t rc
;
4228 for (size_t x
= 0; x
< 10; ++x
)
4230 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4231 test_true(mmc
[x
] != NULL
);
4232 test_true(rc
== MEMCACHED_SUCCESS
);
4235 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4236 test_true(rc
== MEMCACHED_SUCCESS
);
4240 memcached_pool_st
* pool
;
4242 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4243 pthread_create(&tid
, NULL
, connection_release
, &item
);
4244 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4245 test_true(rc
== MEMCACHED_SUCCESS
);
4246 pthread_join(tid
, NULL
);
4247 test_true(mmc
[9] == item
.mmc
);
4248 const char *key
= "key";
4249 size_t keylen
= strlen(key
);
4251 // verify that I can do ops with all connections
4252 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4253 test_true(rc
== MEMCACHED_SUCCESS
);
4255 for (size_t x
= 0; x
< 10; ++x
)
4257 uint64_t number_value
;
4258 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4259 test_true(rc
== MEMCACHED_SUCCESS
);
4260 test_true(number_value
== (x
+1));
4264 for (size_t x
= 0; x
< 10; ++x
)
4266 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4270 /* verify that I can set behaviors on the pool when I don't have all
4271 * of the connections in the pool. It should however be enabled
4272 * when I push the item into the pool
4274 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4275 test_true(mmc
[0] != NULL
);
4277 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4278 test_true(rc
== MEMCACHED_SUCCESS
);
4280 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4281 test_true(mmc
[1] != NULL
);
4283 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4284 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4285 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4287 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4288 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4289 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4292 test_true(memcached_pool_destroy(pool
) == memc
);
4293 return TEST_SUCCESS
;
4297 static test_return_t
replication_set_test(memcached_st
*memc
)
4299 memcached_return_t rc
;
4300 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4301 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4303 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4304 test_true(rc
== MEMCACHED_SUCCESS
);
4307 ** We are using the quiet commands to store the replicas, so we need
4308 ** to ensure that all of them are processed before we can continue.
4309 ** In the test we go directly from storing the object to trying to
4310 ** receive the object from all of the different servers, so we
4311 ** could end up in a race condition (the memcached server hasn't yet
4312 ** processed the quiet command from the replication set when it process
4313 ** the request from the other client (created by the clone)). As a
4314 ** workaround for that we call memcached_quit to send the quit command
4315 ** to the server and wait for the response ;-) If you use the test code
4316 ** as an example for your own code, please note that you shouldn't need
4319 memcached_quit(memc
);
4322 ** "bubba" should now be stored on all of our servers. We don't have an
4323 ** easy to use API to address each individual server, so I'll just iterate
4324 ** through a bunch of "master keys" and I should most likely hit all of the
4327 for (int x
= 'a'; x
<= 'z'; ++x
)
4329 char key
[2]= { [0]= (char)x
};
4332 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4334 test_true(rc
== MEMCACHED_SUCCESS
);
4335 test_true(val
!= NULL
);
4339 memcached_free(memc_clone
);
4341 return TEST_SUCCESS
;
4344 static test_return_t
replication_get_test(memcached_st
*memc
)
4346 memcached_return_t rc
;
4349 * Don't do the following in your code. I am abusing the internal details
4350 * within the library, and this is not a supported interface.
4351 * This is to verify correct behavior in the library
4353 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4355 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4356 memcached_server_instance_st instance
=
4357 memcached_server_instance_by_position(memc_clone
, host
);
4359 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4361 for (int x
= 'a'; x
<= 'z'; ++x
)
4363 char key
[2]= { [0]= (char)x
};
4366 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4368 test_true(rc
== MEMCACHED_SUCCESS
);
4369 test_true(val
!= NULL
);
4373 memcached_free(memc_clone
);
4376 return TEST_SUCCESS
;
4379 static test_return_t
replication_mget_test(memcached_st
*memc
)
4381 memcached_return_t rc
;
4382 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4383 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4385 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4386 size_t len
[]= { 5, 4, 4, 4 };
4388 for (size_t x
= 0; x
< 4; ++x
)
4390 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4391 test_true(rc
== MEMCACHED_SUCCESS
);
4395 ** We are using the quiet commands to store the replicas, so we need
4396 ** to ensure that all of them are processed before we can continue.
4397 ** In the test we go directly from storing the object to trying to
4398 ** receive the object from all of the different servers, so we
4399 ** could end up in a race condition (the memcached server hasn't yet
4400 ** processed the quiet command from the replication set when it process
4401 ** the request from the other client (created by the clone)). As a
4402 ** workaround for that we call memcached_quit to send the quit command
4403 ** to the server and wait for the response ;-) If you use the test code
4404 ** as an example for your own code, please note that you shouldn't need
4407 memcached_quit(memc
);
4410 * Don't do the following in your code. I am abusing the internal details
4411 * within the library, and this is not a supported interface.
4412 * This is to verify correct behavior in the library
4414 memcached_result_st result_obj
;
4415 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4417 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4418 memcached_server_instance_st instance
=
4419 memcached_server_instance_by_position(new_clone
, host
);
4420 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4422 for (int x
= 'a'; x
<= 'z'; ++x
)
4424 char key
[2]= { [0]= (char)x
, [1]= 0 };
4426 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4427 test_true(rc
== MEMCACHED_SUCCESS
);
4429 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4433 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4437 test_true(hits
== 4);
4438 memcached_result_free(&result_obj
);
4441 memcached_free(new_clone
);
4444 memcached_free(memc_clone
);
4446 return TEST_SUCCESS
;
4449 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4451 memcached_result_st result_obj
;
4452 memcached_return_t rc
;
4453 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4454 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4455 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4457 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4458 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4460 for (int x
=0; x
< 7; ++x
)
4462 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4463 test_true(rc
== MEMCACHED_SUCCESS
);
4466 memcached_quit(memc
);
4468 for (size_t x
= 0; x
< 7; ++x
)
4470 const char key
[2]= { [0]= (const char)x
};
4472 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4473 test_true(rc
== MEMCACHED_SUCCESS
);
4475 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4479 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4483 test_true(hits
== 7);
4484 memcached_result_free(&result_obj
);
4486 memcached_free(memc_clone
);
4487 return TEST_SUCCESS
;
4490 static test_return_t
replication_delete_test(memcached_st
*memc
)
4492 memcached_return_t rc
;
4493 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4494 /* Delete the items from all of the servers except 1 */
4495 uint64_t repl
= memcached_behavior_get(memc
,
4496 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4497 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4499 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4500 size_t len
[]= { 5, 4, 4, 4 };
4502 for (size_t x
= 0; x
< 4; ++x
)
4504 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4505 test_true(rc
== MEMCACHED_SUCCESS
);
4509 * Don't do the following in your code. I am abusing the internal details
4510 * within the library, and this is not a supported interface.
4511 * This is to verify correct behavior in the library
4513 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4514 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4516 memcached_server_instance_st instance
=
4517 memcached_server_instance_by_position(memc_clone
, x
);
4519 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4520 if (++hash
== memc_clone
->number_of_hosts
)
4524 memcached_result_st result_obj
;
4525 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4527 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4529 const char key
[2]= { [0]= (const char)x
};
4531 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4532 test_true(rc
== MEMCACHED_SUCCESS
);
4534 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4538 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4542 test_true(hits
== 4);
4543 memcached_result_free(&result_obj
);
4546 memcached_free(memc_clone
);
4548 return TEST_SUCCESS
;
4552 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4556 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4557 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4558 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4559 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4560 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4561 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4562 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4563 #ifdef HAVE_HSIEH_HASH
4564 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4566 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4567 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4568 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4570 return TEST_SUCCESS
;
4574 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4576 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4577 #ifdef HAVE_HSIEH_HASH
4578 expected_rc
= MEMCACHED_SUCCESS
;
4580 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4581 (uint64_t)MEMCACHED_HASH_HSIEH
);
4582 test_true(rc
== expected_rc
);
4584 return TEST_SUCCESS
;
4587 static test_return_t
one_at_a_time_run (memcached_st
*memc
__attribute__((unused
)))
4592 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4596 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4597 test_true(one_at_a_time_values
[x
] == hash_val
);
4600 return TEST_SUCCESS
;
4603 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4608 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4612 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4613 test_true(md5_values
[x
] == hash_val
);
4616 return TEST_SUCCESS
;
4619 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4624 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4628 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4629 test_true(crc_values
[x
] == hash_val
);
4632 return TEST_SUCCESS
;
4635 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4640 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4644 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4645 test_true(fnv1_64_values
[x
] == hash_val
);
4648 return TEST_SUCCESS
;
4651 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4656 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4660 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4661 test_true(fnv1a_64_values
[x
] == hash_val
);
4664 return TEST_SUCCESS
;
4667 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4673 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4677 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4678 test_true(fnv1_32_values
[x
] == hash_val
);
4681 return TEST_SUCCESS
;
4684 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4689 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4693 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4694 test_true(fnv1a_32_values
[x
] == hash_val
);
4697 return TEST_SUCCESS
;
4700 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4705 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4709 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4710 test_true(hsieh_values
[x
] == hash_val
);
4713 return TEST_SUCCESS
;
4716 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4719 return TEST_SKIPPED
;
4724 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4728 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4729 test_true(murmur_values
[x
] == hash_val
);
4732 return TEST_SUCCESS
;
4736 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4742 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4746 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4747 test_true(jenkins_values
[x
] == hash_val
);
4750 return TEST_SUCCESS
;
4753 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4756 return libhashkit_md5(string
, string_length
);
4759 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4762 return libhashkit_crc32(string
, string_length
);
4765 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4769 const hashkit_st
*kit
;
4771 hashkit_return_t hash_rc
;
4773 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};
4774 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};
4776 kit
= memcached_get_hashkit(memc
);
4778 hashkit_clone(&new_kit
, kit
);
4779 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4780 test_true(hash_rc
== HASHKIT_SUCCESS
);
4782 memcached_set_hashkit(memc
, &new_kit
);
4785 Verify Setting the hash.
4787 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4791 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4792 test_true(md5_values
[x
] == hash_val
);
4797 Now check memcached_st.
4799 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4803 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4804 test_true(md5_hosts
[x
] == hash_val
);
4807 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4808 test_true(hash_rc
== HASHKIT_SUCCESS
);
4810 memcached_set_hashkit(memc
, &new_kit
);
4813 Verify Setting the hash.
4815 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4819 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4820 test_true(crc_values
[x
] == hash_val
);
4823 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4827 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4828 test_true(crc_hosts
[x
] == hash_val
);
4831 return TEST_SUCCESS
;
4835 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4837 memcached_return_t rc
;
4840 memcached_server_st
*server_pool
;
4845 memc
= memcached_create(NULL
);
4848 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4849 test_true(rc
== MEMCACHED_SUCCESS
);
4851 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4852 test_true(value
== 1);
4854 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4855 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4858 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");
4859 memcached_server_push(memc
, server_pool
);
4861 /* verify that the server list was parsed okay. */
4862 test_true(memcached_server_count(memc
) == 8);
4863 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4864 test_true(server_pool
[0].port
== 11211);
4865 test_true(server_pool
[0].weight
== 600);
4866 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4867 test_true(server_pool
[2].port
== 11211);
4868 test_true(server_pool
[2].weight
== 200);
4869 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4870 test_true(server_pool
[7].port
== 11211);
4871 test_true(server_pool
[7].weight
== 100);
4873 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4874 * us test the boundary wraparound.
4876 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4878 /* verify the standard ketama set. */
4879 for (x
= 0; x
< 99; x
++)
4881 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4882 memcached_server_instance_st instance
=
4883 memcached_server_instance_by_position(memc
, server_idx
);
4884 const char *hostname
= memcached_server_name(instance
);
4886 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4889 memcached_server_list_free(server_pool
);
4890 memcached_free(memc
);
4892 return TEST_SUCCESS
;
4895 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4897 memcached_return_t rc
;
4900 memcached_server_st
*server_pool
;
4905 memc
= memcached_create(NULL
);
4908 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4909 test_true(rc
== MEMCACHED_SUCCESS
);
4911 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4912 test_true(value
== 1);
4914 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4915 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4917 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");
4918 memcached_server_push(memc
, server_pool
);
4920 /* verify that the server list was parsed okay. */
4921 test_true(memcached_server_count(memc
) == 8);
4922 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4923 test_true(server_pool
[0].port
== 11211);
4924 test_true(server_pool
[0].weight
== 600);
4925 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4926 test_true(server_pool
[2].port
== 11211);
4927 test_true(server_pool
[2].weight
== 200);
4928 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4929 test_true(server_pool
[7].port
== 11211);
4930 test_true(server_pool
[7].weight
== 100);
4932 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4933 * us test the boundary wraparound.
4935 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4937 /* verify the standard ketama set. */
4938 for (x
= 0; x
< 99; x
++)
4940 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4942 memcached_server_instance_st instance
=
4943 memcached_server_instance_by_position(memc
, server_idx
);
4945 const char *hostname
= memcached_server_name(instance
);
4947 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
4950 memcached_server_list_free(server_pool
);
4951 memcached_free(memc
);
4953 return TEST_SUCCESS
;
4956 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4958 test_return_t test_rc
;
4959 test_rc
= pre_binary(memc
);
4961 if (test_rc
!= TEST_SUCCESS
)
4964 memcached_return_t ret
;
4965 const char *key
= "regression_bug_434484";
4966 size_t keylen
= strlen(key
);
4968 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4969 test_true(ret
== MEMCACHED_NOTSTORED
);
4971 size_t size
= 2048 * 1024;
4972 void *data
= calloc(1, size
);
4973 test_true(data
!= NULL
);
4974 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
4975 test_true(ret
== MEMCACHED_E2BIG
);
4978 return TEST_SUCCESS
;
4981 static test_return_t
regression_bug_434843(memcached_st
*memc
)
4983 test_return_t test_rc
;
4984 test_rc
= pre_binary(memc
);
4986 if (test_rc
!= TEST_SUCCESS
)
4989 memcached_return_t rc
;
4991 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
4994 * I only want to hit only _one_ server so I know the number of requests I'm
4995 * sending in the pipleine to the server. Let's try to do a multiget of
4996 * 1024 (that should satisfy most users don't you think?). Future versions
4997 * will include a mget_execute function call if you need a higher number.
4999 uint32_t number_of_hosts
= memcached_server_count(memc
);
5000 memc
->number_of_hosts
= 1;
5001 const size_t max_keys
= 1024;
5002 char **keys
= calloc(max_keys
, sizeof(char*));
5003 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5005 for (size_t x
= 0; x
< max_keys
; ++x
)
5009 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5011 test_true(keys
[x
] != NULL
);
5015 * Run two times.. the first time we should have 100% cache miss,
5016 * and the second time we should have 100% cache hits
5018 for (size_t y
= 0; y
< 2; y
++)
5020 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5021 test_true(rc
== MEMCACHED_SUCCESS
);
5022 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5026 /* The first iteration should give me a 100% cache miss. verify that*/
5027 char blob
[1024]= { 0 };
5029 test_true(counter
== 0);
5031 for (size_t x
= 0; x
< max_keys
; ++x
)
5033 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5034 blob
, sizeof(blob
), 0, 0);
5035 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5040 /* Verify that we received all of the key/value pairs */
5041 test_true(counter
== max_keys
);
5045 /* Release allocated resources */
5046 for (size_t x
= 0; x
< max_keys
; ++x
)
5053 memc
->number_of_hosts
= number_of_hosts
;
5055 return TEST_SUCCESS
;
5058 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5060 memcached_return_t rc
;
5061 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5062 test_true(rc
== MEMCACHED_SUCCESS
);
5064 return regression_bug_434843(memc
);
5067 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5069 memcached_return_t rc
;
5070 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5071 test_true(rc
== MEMCACHED_SUCCESS
);
5073 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5074 test_true(rc
== MEMCACHED_SUCCESS
);
5075 test_true(bytes
!= NULL
);
5076 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5078 test_true(rc
== MEMCACHED_SUCCESS
);
5079 test_true(bytes_read
!= NULL
);
5081 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5082 "bytes_written", &rc
);
5083 test_true(rc
== MEMCACHED_SUCCESS
);
5084 test_true(bytes_written
!= NULL
);
5086 test_true(strcmp(bytes
, bytes_read
) != 0);
5087 test_true(strcmp(bytes
, bytes_written
) != 0);
5089 /* Release allocated resources */
5092 free(bytes_written
);
5093 memcached_stat_free(NULL
, memc_stat
);
5095 return TEST_SUCCESS
;
5099 * The test case isn't obvious so I should probably document why
5100 * it works the way it does. Bug 442914 was caused by a bug
5101 * in the logic in memcached_purge (it did not handle the case
5102 * where the number of bytes sent was equal to the watermark).
5103 * In this test case, create messages so that we hit that case
5104 * and then disable noreply mode and issue a new command to
5105 * verify that it isn't stuck. If we change the format for the
5106 * delete command or the watermarks, we need to update this
5109 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5111 memcached_return_t rc
;
5112 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5113 test_true(rc
== MEMCACHED_SUCCESS
);
5114 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5116 uint32_t number_of_hosts
= memcached_server_count(memc
);
5117 memc
->number_of_hosts
= 1;
5122 for (uint32_t x
= 0; x
< 250; ++x
)
5124 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5125 rc
= memcached_delete(memc
, k
, len
, 0);
5126 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5129 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5132 rc
= memcached_delete(memc
, k
, len
, 0);
5133 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5135 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5136 test_true(rc
== MEMCACHED_SUCCESS
);
5137 rc
= memcached_delete(memc
, k
, len
, 0);
5138 test_true(rc
== MEMCACHED_NOTFOUND
);
5140 memc
->number_of_hosts
= number_of_hosts
;
5142 return TEST_SUCCESS
;
5145 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5147 memcached_server_instance_st instance_one
;
5148 memcached_server_instance_st instance_two
;
5150 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5151 return TEST_SKIPPED
;
5153 memcached_return_t rc
;
5155 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5156 test_true(rc
== MEMCACHED_SUCCESS
);
5158 const size_t max_keys
= 100;
5159 char **keys
= calloc(max_keys
, sizeof(char*));
5160 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5162 for (size_t x
= 0; x
< max_keys
; ++x
)
5166 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5168 test_true(keys
[x
] != NULL
);
5169 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5170 test_true(rc
== MEMCACHED_SUCCESS
);
5174 ** We are using the quiet commands to store the replicas, so we need
5175 ** to ensure that all of them are processed before we can continue.
5176 ** In the test we go directly from storing the object to trying to
5177 ** receive the object from all of the different servers, so we
5178 ** could end up in a race condition (the memcached server hasn't yet
5179 ** processed the quiet command from the replication set when it process
5180 ** the request from the other client (created by the clone)). As a
5181 ** workaround for that we call memcached_quit to send the quit command
5182 ** to the server and wait for the response ;-) If you use the test code
5183 ** as an example for your own code, please note that you shouldn't need
5186 memcached_quit(memc
);
5188 /* Verify that all messages are stored, and we didn't stuff too much
5191 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5192 test_true(rc
== MEMCACHED_SUCCESS
);
5195 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5196 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5197 /* Verify that we received all of the key/value pairs */
5198 test_true(counter
== max_keys
);
5200 memcached_quit(memc
);
5202 * Don't do the following in your code. I am abusing the internal details
5203 * within the library, and this is not a supported interface.
5204 * This is to verify correct behavior in the library. Fake that two servers
5207 instance_one
= memcached_server_instance_by_position(memc
, 0);
5208 instance_two
= memcached_server_instance_by_position(memc
, 2);
5209 in_port_t port0
= instance_one
->port
;
5210 in_port_t port2
= instance_two
->port
;
5212 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5213 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5215 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5216 test_true(rc
== MEMCACHED_SUCCESS
);
5219 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5220 test_true(counter
== (unsigned int)max_keys
);
5222 /* restore the memc handle */
5223 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5224 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5226 memcached_quit(memc
);
5228 /* Remove half of the objects */
5229 for (size_t x
= 0; x
< max_keys
; ++x
)
5233 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5234 test_true(rc
== MEMCACHED_SUCCESS
);
5238 memcached_quit(memc
);
5239 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5240 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5242 /* now retry the command, this time we should have cache misses */
5243 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5244 test_true(rc
== MEMCACHED_SUCCESS
);
5247 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5248 test_true(counter
== (unsigned int)(max_keys
>> 1));
5250 /* Release allocated resources */
5251 for (size_t x
= 0; x
< max_keys
; ++x
)
5258 /* restore the memc handle */
5259 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5260 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5262 return TEST_SUCCESS
;
5265 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5267 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5268 test_true(memc_clone
!= NULL
);
5269 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5271 memcached_server_instance_st instance
=
5272 memcached_server_instance_by_position(memc_clone
, 0);
5274 if (instance
->major_version
> 1 ||
5275 (instance
->major_version
== 1 &&
5276 instance
->minor_version
> 2))
5278 /* Binary protocol doesn't support deferred delete */
5279 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5280 test_true(bin_clone
!= NULL
);
5281 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5282 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5283 memcached_free(bin_clone
);
5285 memcached_quit(memc_clone
);
5287 /* If we know the server version, deferred delete should fail
5288 * with invalid arguments */
5289 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5291 /* If we don't know the server version, we should get a protocol error */
5292 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5294 /* but there is a bug in some of the memcached servers (1.4) that treats
5295 * the counter as noreply so it doesn't send the proper error message
5297 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5299 /* And buffered mode should be disabled and we should get protocol error */
5300 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5301 rc
= memcached_delete(memc
, "foo", 3, 1);
5302 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5304 /* Same goes for noreply... */
5305 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5306 rc
= memcached_delete(memc
, "foo", 3, 1);
5307 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5309 /* but a normal request should go through (and be buffered) */
5310 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5311 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5313 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5314 /* unbuffered noreply should be success */
5315 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5316 /* unbuffered with reply should be not found... */
5317 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5318 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5321 memcached_free(memc_clone
);
5322 return TEST_SUCCESS
;
5326 /* Test memcached_server_get_last_disconnect
5327 * For a working server set, shall be NULL
5328 * For a set of non existing server, shall not be NULL
5330 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5332 memcached_return_t rc
;
5333 memcached_server_instance_st disconnected_server
;
5335 /* With the working set of server */
5336 const char *key
= "marmotte";
5337 const char *value
= "milka";
5339 rc
= memcached_set(memc
, key
, strlen(key
),
5340 value
, strlen(value
),
5341 (time_t)0, (uint32_t)0);
5342 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5344 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5345 test_true(disconnected_server
== NULL
);
5347 /* With a non existing server */
5349 memcached_server_st
*servers
;
5351 const char *server_list
= "localhost:9";
5353 servers
= memcached_servers_parse(server_list
);
5355 mine
= memcached_create(NULL
);
5356 rc
= memcached_server_push(mine
, servers
);
5357 test_true(rc
== MEMCACHED_SUCCESS
);
5358 memcached_server_list_free(servers
);
5361 rc
= memcached_set(mine
, key
, strlen(key
),
5362 value
, strlen(value
),
5363 (time_t)0, (uint32_t)0);
5364 test_true(rc
!= MEMCACHED_SUCCESS
);
5366 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5367 test_true(disconnected_server
!= NULL
);
5368 test_true(memcached_server_port(disconnected_server
)== 9);
5369 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5371 memcached_quit(mine
);
5372 memcached_free(mine
);
5374 return TEST_SUCCESS
;
5378 * This test ensures that the failure counter isn't incremented during
5379 * normal termination of the memcached instance.
5381 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5383 memcached_return_t rc
;
5384 memcached_server_instance_st instance
;
5386 /* Set value to force connection to the server */
5387 const char *key
= "marmotte";
5388 const char *value
= "milka";
5391 * Please note that I'm abusing the internal structures in libmemcached
5392 * in a non-portable way and you shouldn't be doing this. I'm only
5393 * doing this in order to verify that the library works the way it should
5395 uint32_t number_of_hosts
= memcached_server_count(memc
);
5396 memc
->number_of_hosts
= 1;
5398 /* Ensure that we are connected to the server by setting a value */
5399 rc
= memcached_set(memc
, key
, strlen(key
),
5400 value
, strlen(value
),
5401 (time_t)0, (uint32_t)0);
5402 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5405 instance
= memcached_server_instance_by_position(memc
, 0);
5406 /* The test is to see that the memcached_quit doesn't increase the
5407 * the server failure conter, so let's ensure that it is zero
5408 * before sending quit
5410 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5412 memcached_quit(memc
);
5414 /* Verify that it memcached_quit didn't increment the failure counter
5415 * Please note that this isn't bullet proof, because an error could
5418 test_true(instance
->server_failure_counter
== 0);
5420 /* restore the instance */
5421 memc
->number_of_hosts
= number_of_hosts
;
5423 return TEST_SUCCESS
;
5430 * Test that ensures mget_execute does not end into recursive calls that finally fails
5432 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5434 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5435 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5436 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5437 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5438 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5441 * I only want to hit _one_ server so I know the number of requests I'm
5442 * sending in the pipeline.
5444 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5445 memc
->number_of_hosts
= 1;
5446 size_t max_keys
= 20480;
5449 char **keys
= calloc(max_keys
, sizeof(char*));
5450 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5452 /* First add all of the items.. */
5453 char blob
[1024]= { 0 };
5454 memcached_return rc
;
5455 for (size_t x
= 0; x
< max_keys
; ++x
)
5458 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5460 assert(keys
[x
] != NULL
);
5461 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5462 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5465 /* Try to get all of them with a large multiget */
5467 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5468 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5469 (size_t)max_keys
, callbacks
, &counter
, 1);
5471 assert(rc
== MEMCACHED_SUCCESS
);
5472 char* the_value
= NULL
;
5473 char the_key
[MEMCACHED_MAX_KEY
];
5474 size_t the_key_length
;
5475 size_t the_value_length
;
5479 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5481 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5487 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5490 assert(rc
== MEMCACHED_END
);
5492 /* Verify that we got all of the items */
5493 assert(counter
== max_keys
);
5495 /* Release all allocated resources */
5496 for (size_t x
= 0; x
< max_keys
; ++x
)
5503 memc
->number_of_hosts
= number_of_hosts
;
5505 return TEST_SUCCESS
;
5511 /* Clean the server before beginning testing */
5513 {"flush", 0, (test_callback_fn
)flush_test
},
5514 {"init", 0, (test_callback_fn
)init_test
},
5515 {"allocation", 0, (test_callback_fn
)allocation_test
},
5516 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5517 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5518 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5519 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5520 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5521 {"clone_test", 0, (test_callback_fn
)clone_test
},
5522 {"connection_test", 0, (test_callback_fn
)connection_test
},
5523 {"callback_test", 0, (test_callback_fn
)callback_test
},
5524 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5525 {"error", 0, (test_callback_fn
)error_test
},
5526 {"set", 0, (test_callback_fn
)set_test
},
5527 {"set2", 0, (test_callback_fn
)set_test2
},
5528 {"set3", 0, (test_callback_fn
)set_test3
},
5529 {"dump", 1, (test_callback_fn
)dump_test
},
5530 {"add", 1, (test_callback_fn
)add_test
},
5531 {"replace", 1, (test_callback_fn
)replace_test
},
5532 {"delete", 1, (test_callback_fn
)delete_test
},
5533 {"get", 1, (test_callback_fn
)get_test
},
5534 {"get2", 0, (test_callback_fn
)get_test2
},
5535 {"get3", 0, (test_callback_fn
)get_test3
},
5536 {"get4", 0, (test_callback_fn
)get_test4
},
5537 {"partial mget", 0, (test_callback_fn
)get_test5
},
5538 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5539 {"increment", 0, (test_callback_fn
)increment_test
},
5540 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5541 {"decrement", 0, (test_callback_fn
)decrement_test
},
5542 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5543 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5544 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5545 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5546 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5547 {"quit", 0, (test_callback_fn
)quit_test
},
5548 {"mget", 1, (test_callback_fn
)mget_test
},
5549 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5550 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5551 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5552 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5553 {"mget_end", 0, (test_callback_fn
)mget_end
},
5554 {"get_stats", 0, (test_callback_fn
)get_stats
},
5555 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5556 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5557 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5558 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5559 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
5560 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
5561 {"read_through", 1, (test_callback_fn
)read_through
},
5562 {"delete_through", 1, (test_callback_fn
)delete_through
},
5563 {"noreply", 1, (test_callback_fn
)noreply_test
},
5564 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
5565 #ifdef HAVE_LIBMEMCACHEDUTIL
5566 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
5568 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
5572 test_st behavior_tests
[] ={
5573 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
5574 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
5578 test_st async_tests
[] ={
5579 {"add", 1, (test_callback_fn
)add_wrapper
},
5583 test_st string_tests
[] ={
5584 {"string static with null", 0, (test_callback_fn
)string_static_null
},
5585 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
5586 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
5587 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
5588 {"string append", 0, (test_callback_fn
)string_alloc_append
},
5589 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
5590 {0, 0, (test_callback_fn
)0}
5593 test_st result_tests
[] ={
5594 {"result static", 0, (test_callback_fn
)result_static
},
5595 {"result alloc", 0, (test_callback_fn
)result_alloc
},
5596 {0, 0, (test_callback_fn
)0}
5599 test_st version_1_2_3
[] ={
5600 {"append", 0, (test_callback_fn
)append_test
},
5601 {"prepend", 0, (test_callback_fn
)prepend_test
},
5602 {"cas", 0, (test_callback_fn
)cas_test
},
5603 {"cas2", 0, (test_callback_fn
)cas2_test
},
5604 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
5605 {0, 0, (test_callback_fn
)0}
5608 test_st user_tests
[] ={
5609 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
5610 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
5611 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
5612 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
5613 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
5614 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
5615 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
5616 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
5617 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
5618 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
5619 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
5620 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
5621 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
5622 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
5623 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
5624 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
5627 ** It seems to be something weird with the character sets..
5628 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
5629 ** guess I need to find out how this is supposed to work.. Perhaps I need
5630 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
5631 ** so just disable the code for now...).
5633 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
5635 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
5636 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
5637 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
5638 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
5639 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
5640 {0, 0, (test_callback_fn
)0}
5643 test_st replication_tests
[]= {
5644 {"set", 1, (test_callback_fn
)replication_set_test
},
5645 {"get", 0, (test_callback_fn
)replication_get_test
},
5646 {"mget", 0, (test_callback_fn
)replication_mget_test
},
5647 {"delete", 0, (test_callback_fn
)replication_delete_test
},
5648 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
5649 {0, 0, (test_callback_fn
)0}
5653 * The following test suite is used to verify that we don't introduce
5654 * regression bugs. If you want more information about the bug / test,
5655 * you should look in the bug report at
5656 * http://bugs.launchpad.net/libmemcached
5658 test_st regression_tests
[]= {
5659 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
5660 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
5661 {"lp:434843 buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
5662 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
5663 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
5664 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
5665 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
5666 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
5667 {0, 0, (test_callback_fn
)0}
5670 test_st ketama_compatibility
[]= {
5671 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
5672 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
5673 {0, 0, (test_callback_fn
)0}
5676 test_st generate_tests
[] ={
5677 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5678 {"generate_data", 1, (test_callback_fn
)generate_data
},
5679 {"get_read", 0, (test_callback_fn
)get_read
},
5680 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
5681 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
5682 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
5683 {"generate_data", 1, (test_callback_fn
)generate_data
},
5684 {"mget_read", 0, (test_callback_fn
)mget_read
},
5685 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
5686 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
5687 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5688 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
5689 {"generate_data", 1, (test_callback_fn
)generate_data
},
5690 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
5691 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5692 {0, 0, (test_callback_fn
)0}
5695 test_st consistent_tests
[] ={
5696 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5697 {"generate_data", 1, (test_callback_fn
)generate_data
},
5698 {"get_read", 0, (test_callback_fn
)get_read_count
},
5699 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5700 {0, 0, (test_callback_fn
)0}
5703 test_st consistent_weighted_tests
[] ={
5704 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5705 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
5706 {"get_read", 0, (test_callback_fn
)get_read_count
},
5707 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5708 {0, 0, (test_callback_fn
)0}
5711 test_st hsieh_availability
[] ={
5712 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
5713 {0, 0, (test_callback_fn
)0}
5717 test_st hash_sanity
[] ={
5718 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
5719 {0, 0, (test_callback_fn
)0}
5723 test_st ketama_auto_eject_hosts
[] ={
5724 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
5725 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
5726 {0, 0, (test_callback_fn
)0}
5729 test_st hash_tests
[] ={
5730 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
5731 {"md5", 0, (test_callback_fn
)md5_run
},
5732 {"crc", 0, (test_callback_fn
)crc_run
},
5733 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
5734 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
5735 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
5736 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
5737 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
5738 {"murmur", 0, (test_callback_fn
)murmur_run
},
5739 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
5740 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
5741 {0, 0, (test_callback_fn
)0}
5744 collection_st collection
[] ={
5746 {"hash_sanity", 0, 0, hash_sanity
},
5748 {"hsieh_availability", 0, 0, hsieh_availability
},
5749 {"block", 0, 0, tests
},
5750 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
5751 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
5752 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
5753 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
5754 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
5755 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
5756 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
5757 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
5758 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
5759 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
5760 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
5761 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
5762 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
5763 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
5764 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
5765 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
5766 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
5767 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
5768 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
5769 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
5770 #ifdef MEMCACHED_ENABLE_DEPRECATED
5771 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
5773 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
5774 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
5775 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
5776 {"string", 0, 0, string_tests
},
5777 {"result", 0, 0, result_tests
},
5778 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
5779 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
5780 {"user", 0, 0, user_tests
},
5781 {"generate", 0, 0, generate_tests
},
5782 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
5783 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
5784 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
5785 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
5786 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
5787 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
5788 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
5790 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
5791 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
5792 {"consistent_not", 0, 0, consistent_tests
},
5793 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
5794 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
5795 {"ketama_compat", 0, 0, ketama_compatibility
},
5796 {"test_hashes", 0, 0, hash_tests
},
5797 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
5798 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
5799 {"regression", 0, 0, regression_tests
},
5800 {"behaviors", 0, 0, behavior_tests
},
5804 #define SERVERS_TO_CREATE 5
5806 #include "libmemcached_world.h"
5808 void get_world(world_st
*world
)
5810 world
->collections
= collection
;
5812 world
->create
= (test_callback_create_fn
)world_create
;
5813 world
->destroy
= (test_callback_fn
)world_destroy
;
5815 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
5816 world
->test
.flush
= (test_callback_fn
)world_flush
;
5817 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
5818 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
5819 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
5821 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
5822 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
5824 world
->runner
= &defualt_libmemcached_runner
;