2 Sample test application.
5 #include "libmemcached/common.h"
12 #include <sys/types.h>
18 #include "clients/generator.h"
19 #include "clients/execute.h"
22 #define INT64_MAX LONG_MAX
25 #define INT32_MAX INT_MAX
31 #ifdef HAVE_LIBMEMCACHEDUTIL
33 #include "libmemcached/memcached_util.h"
36 #define GLOBAL_COUNT 10000
37 #define GLOBAL2_COUNT 100
38 #define SERVERS_TO_CREATE 5
39 static uint32_t global_count
;
41 static pairs_st
*global_pairs
;
42 static const char *global_keys
[GLOBAL_COUNT
];
43 static size_t global_keys_length
[GLOBAL_COUNT
];
45 static test_return_t
init_test(memcached_st
*not_used
__attribute__((unused
)))
49 (void)memcached_create(&memc
);
50 memcached_free(&memc
);
55 static test_return_t
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
57 memcached_server_st
*server_list
;
60 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
61 assert(server_list
== NULL
);
63 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
64 assert(server_list
== NULL
);
66 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
67 assert(server_list
== NULL
);
72 #define TEST_PORT_COUNT 7
73 uint32_t test_ports
[TEST_PORT_COUNT
];
75 static memcached_return
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
78 uint32_t bigger
= *((uint32_t *)(context
));
79 assert(bigger
<= server
->port
);
80 *((uint32_t *)(context
))= server
->port
;
82 return MEMCACHED_SUCCESS
;
85 static test_return_t
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
88 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
90 memcached_server_function callbacks
[1];
91 memcached_st
*local_memc
;
93 local_memc
= memcached_create(NULL
);
95 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
97 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
99 test_ports
[x
]= (uint32_t)random() % 64000;
100 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
101 assert(local_memc
->number_of_hosts
== x
+ 1);
102 assert(local_memc
->hosts
[0].count
== x
+1);
103 assert(rc
== MEMCACHED_SUCCESS
);
106 callbacks
[0]= server_display_function
;
107 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
110 memcached_free(local_memc
);
115 static test_return_t
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
117 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
119 memcached_server_function callbacks
[1];
120 memcached_st
*local_memc
;
122 local_memc
= memcached_create(NULL
);
124 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
125 assert(rc
== MEMCACHED_SUCCESS
);
127 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
128 assert(rc
== MEMCACHED_SUCCESS
);
129 assert(local_memc
->hosts
[0].port
== 43043);
131 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
132 assert(rc
== MEMCACHED_SUCCESS
);
133 assert(local_memc
->hosts
[0].port
== 43042);
134 assert(local_memc
->hosts
[1].port
== 43043);
136 callbacks
[0]= server_display_function
;
137 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
140 memcached_free(local_memc
);
145 static memcached_return
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
148 uint32_t x
= *((uint32_t *)(context
));
150 assert(test_ports
[x
] == server
->port
);
151 *((uint32_t *)(context
))= ++x
;
153 return MEMCACHED_SUCCESS
;
156 static test_return_t
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
159 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
160 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
162 memcached_server_function callbacks
[1];
163 memcached_st
*local_memc
;
165 local_memc
= memcached_create(NULL
);
168 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
170 test_ports
[x
]= (uint32_t)(random() % 64000);
171 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
172 assert(local_memc
->number_of_hosts
== x
+1);
173 assert(local_memc
->hosts
[0].count
== x
+1);
174 assert(rc
== MEMCACHED_SUCCESS
);
177 callbacks
[0]= server_display_unsort_function
;
178 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
180 /* Now we sort old data! */
181 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
182 callbacks
[0]= server_display_function
;
183 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
186 memcached_free(local_memc
);
191 static test_return_t
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
194 memc
= memcached_create(NULL
);
196 memcached_free(memc
);
201 static test_return_t
clone_test(memcached_st
*memc
)
205 memcached_st
*memc_clone
;
206 memc_clone
= memcached_clone(NULL
, NULL
);
208 memcached_free(memc_clone
);
211 /* Can we init from null? */
213 memcached_st
*memc_clone
;
214 memc_clone
= memcached_clone(NULL
, memc
);
217 assert(memc_clone
->call_free
== memc
->call_free
);
218 assert(memc_clone
->call_malloc
== memc
->call_malloc
);
219 assert(memc_clone
->call_realloc
== memc
->call_realloc
);
220 assert(memc_clone
->call_calloc
== memc
->call_calloc
);
221 assert(memc_clone
->connect_timeout
== memc
->connect_timeout
);
222 assert(memc_clone
->delete_trigger
== memc
->delete_trigger
);
223 assert(memc_clone
->distribution
== memc
->distribution
);
224 assert(memc_clone
->flags
== memc
->flags
);
225 assert(memc_clone
->get_key_failure
== memc
->get_key_failure
);
226 assert(memc_clone
->hash
== memc
->hash
);
227 assert(memc_clone
->hash_continuum
== memc
->hash_continuum
);
228 assert(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
229 assert(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
230 assert(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
231 assert(memc_clone
->on_cleanup
== memc
->on_cleanup
);
232 assert(memc_clone
->on_clone
== memc
->on_clone
);
233 assert(memc_clone
->poll_timeout
== memc
->poll_timeout
);
234 assert(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
235 assert(memc_clone
->recv_size
== memc
->recv_size
);
236 assert(memc_clone
->retry_timeout
== memc
->retry_timeout
);
237 assert(memc_clone
->send_size
== memc
->send_size
);
238 assert(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
239 assert(memc_clone
->snd_timeout
== memc
->snd_timeout
);
240 assert(memc_clone
->user_data
== memc
->user_data
);
242 memcached_free(memc_clone
);
245 /* Can we init from struct? */
247 memcached_st declared_clone
;
248 memcached_st
*memc_clone
;
249 memset(&declared_clone
, 0 , sizeof(memcached_st
));
250 memc_clone
= memcached_clone(&declared_clone
, NULL
);
252 memcached_free(memc_clone
);
255 /* Can we init from struct? */
257 memcached_st declared_clone
;
258 memcached_st
*memc_clone
;
259 memset(&declared_clone
, 0 , sizeof(memcached_st
));
260 memc_clone
= memcached_clone(&declared_clone
, memc
);
262 memcached_free(memc_clone
);
268 static test_return_t
userdata_test(memcached_st
*memc
)
271 assert(memcached_set_user_data(memc
, foo
) == NULL
);
272 assert(memcached_get_user_data(memc
) == foo
);
273 assert(memcached_set_user_data(memc
, NULL
) == foo
);
278 static test_return_t
connection_test(memcached_st
*memc
)
282 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
283 assert(rc
== MEMCACHED_SUCCESS
);
288 static test_return_t
error_test(memcached_st
*memc
)
291 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
292 982370485U, 1263635348U, 4242906218U, 3829656100U,
293 1891735253U, 334139633U, 2257084983U, 3088286104U,
294 13199785U, 2542027183U, 1097051614U, 199566778U,
295 2748246961U, 2465192557U, 1664094137U, 2405439045U,
296 1842224848U, 692413798U, 3479807801U, 919913813U,
297 4269430871U, 610793021U, 527273862U, 1437122909U,
298 2300930706U, 2943759320U, 674306647U, 2400528935U,
299 54481931U, 4186304426U, 1741088401U, 2979625118U,
300 4159057246U, 3425930182U, 2593724503U};
302 // You have updated the memcache_error messages but not updated docs/tests.
303 assert(MEMCACHED_MAXIMUM_RETURN
== 39);
304 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
307 const char *msg
= memcached_strerror(memc
, rc
);
308 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
309 MEMCACHED_HASH_JENKINS
);
310 assert(values
[rc
] == hash_val
);
316 static test_return_t
set_test(memcached_st
*memc
)
319 const char *key
= "foo";
320 const char *value
= "when we sanitize";
322 rc
= memcached_set(memc
, key
, strlen(key
),
323 value
, strlen(value
),
324 (time_t)0, (uint32_t)0);
325 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
330 static test_return_t
append_test(memcached_st
*memc
)
333 const char *key
= "fig";
334 const char *in_value
= "we";
335 char *out_value
= NULL
;
339 rc
= memcached_flush(memc
, 0);
340 assert(rc
== MEMCACHED_SUCCESS
);
342 rc
= memcached_set(memc
, key
, strlen(key
),
343 in_value
, strlen(in_value
),
344 (time_t)0, (uint32_t)0);
345 assert(rc
== MEMCACHED_SUCCESS
);
347 rc
= memcached_append(memc
, key
, strlen(key
),
348 " the", strlen(" the"),
349 (time_t)0, (uint32_t)0);
350 assert(rc
== MEMCACHED_SUCCESS
);
352 rc
= memcached_append(memc
, key
, strlen(key
),
353 " people", strlen(" people"),
354 (time_t)0, (uint32_t)0);
355 assert(rc
== MEMCACHED_SUCCESS
);
357 out_value
= memcached_get(memc
, key
, strlen(key
),
358 &value_length
, &flags
, &rc
);
359 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
360 assert(strlen("we the people") == value_length
);
361 assert(rc
== MEMCACHED_SUCCESS
);
367 static test_return_t
append_binary_test(memcached_st
*memc
)
370 const char *key
= "numbers";
371 unsigned int *store_ptr
;
372 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
378 rc
= memcached_flush(memc
, 0);
379 assert(rc
== MEMCACHED_SUCCESS
);
381 rc
= memcached_set(memc
,
384 (time_t)0, (uint32_t)0);
385 assert(rc
== MEMCACHED_SUCCESS
);
387 for (x
= 0; store_list
[x
] ; x
++)
389 rc
= memcached_append(memc
,
391 (char *)&store_list
[x
], sizeof(unsigned int),
392 (time_t)0, (uint32_t)0);
393 assert(rc
== MEMCACHED_SUCCESS
);
396 value
= memcached_get(memc
, key
, strlen(key
),
397 &value_length
, &flags
, &rc
);
398 assert((value_length
== (sizeof(unsigned int) * x
)));
399 assert(rc
== MEMCACHED_SUCCESS
);
401 store_ptr
= (unsigned int *)value
;
403 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
405 assert(*store_ptr
== store_list
[x
++]);
413 static test_return_t
cas2_test(memcached_st
*memc
)
416 const char *keys
[]= {"fudge", "son", "food"};
417 size_t key_length
[]= {5, 3, 4};
418 const char *value
= "we the people";
419 size_t value_length
= strlen("we the people");
421 memcached_result_st results_obj
;
422 memcached_result_st
*results
;
425 rc
= memcached_flush(memc
, 0);
426 assert(rc
== MEMCACHED_SUCCESS
);
428 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
430 for (x
= 0; x
< 3; x
++)
432 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
433 keys
[x
], key_length
[x
],
434 (time_t)50, (uint32_t)9);
435 assert(rc
== MEMCACHED_SUCCESS
);
438 rc
= memcached_mget(memc
, keys
, key_length
, 3);
440 results
= memcached_result_create(memc
, &results_obj
);
442 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
444 assert(results
->cas
);
445 assert(rc
== MEMCACHED_SUCCESS
);
446 assert(memcached_result_cas(results
));
448 assert(!memcmp(value
, "we the people", strlen("we the people")));
449 assert(strlen("we the people") == value_length
);
450 assert(rc
== MEMCACHED_SUCCESS
);
452 memcached_result_free(&results_obj
);
457 static test_return_t
cas_test(memcached_st
*memc
)
460 const char *key
= "fun";
461 size_t key_length
= strlen(key
);
462 const char *value
= "we the people";
463 const char* keys
[2] = { key
, NULL
};
464 size_t keylengths
[2] = { strlen(key
), 0 };
465 size_t value_length
= strlen(value
);
466 const char *value2
= "change the value";
467 size_t value2_length
= strlen(value2
);
469 memcached_result_st results_obj
;
470 memcached_result_st
*results
;
473 rc
= memcached_flush(memc
, 0);
474 assert(rc
== MEMCACHED_SUCCESS
);
476 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
478 rc
= memcached_set(memc
, key
, strlen(key
),
479 value
, strlen(value
),
480 (time_t)0, (uint32_t)0);
481 assert(rc
== MEMCACHED_SUCCESS
);
483 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
485 results
= memcached_result_create(memc
, &results_obj
);
487 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
489 assert(rc
== MEMCACHED_SUCCESS
);
490 assert(memcached_result_cas(results
));
491 assert(!memcmp(value
, memcached_result_value(results
), value_length
));
492 assert(strlen(memcached_result_value(results
)) == value_length
);
493 assert(rc
== MEMCACHED_SUCCESS
);
494 uint64_t cas
= memcached_result_cas(results
);
497 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
498 assert(rc
== MEMCACHED_END
);
499 assert(results
== NULL
);
502 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
503 assert(rc
== MEMCACHED_SUCCESS
);
506 * The item will have a new cas value, so try to set it again with the old
507 * value. This should fail!
509 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
510 assert(rc
== MEMCACHED_DATA_EXISTS
);
512 memcached_result_free(&results_obj
);
517 static test_return_t
prepend_test(memcached_st
*memc
)
520 const char *key
= "fig";
521 const char *value
= "people";
522 char *out_value
= NULL
;
526 rc
= memcached_flush(memc
, 0);
527 assert(rc
== MEMCACHED_SUCCESS
);
529 rc
= memcached_set(memc
, key
, strlen(key
),
530 value
, strlen(value
),
531 (time_t)0, (uint32_t)0);
532 assert(rc
== MEMCACHED_SUCCESS
);
534 rc
= memcached_prepend(memc
, key
, strlen(key
),
535 "the ", strlen("the "),
536 (time_t)0, (uint32_t)0);
537 assert(rc
== MEMCACHED_SUCCESS
);
539 rc
= memcached_prepend(memc
, key
, strlen(key
),
540 "we ", strlen("we "),
541 (time_t)0, (uint32_t)0);
542 assert(rc
== MEMCACHED_SUCCESS
);
544 out_value
= memcached_get(memc
, key
, strlen(key
),
545 &value_length
, &flags
, &rc
);
546 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
547 assert(strlen("we the people") == value_length
);
548 assert(rc
== MEMCACHED_SUCCESS
);
555 Set the value, then quit to make sure it is flushed.
556 Come back in and test that add fails.
558 static test_return_t
add_test(memcached_st
*memc
)
561 const char *key
= "foo";
562 const char *value
= "when we sanitize";
563 unsigned long long setting_value
;
565 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
567 rc
= memcached_set(memc
, key
, strlen(key
),
568 value
, strlen(value
),
569 (time_t)0, (uint32_t)0);
570 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
571 memcached_quit(memc
);
572 rc
= memcached_add(memc
, key
, strlen(key
),
573 value
, strlen(value
),
574 (time_t)0, (uint32_t)0);
576 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
578 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
580 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
586 ** There was a problem of leaking filedescriptors in the initial release
587 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
588 ** systems it seems that the kernel is slow on reclaiming the resources
589 ** because the connects starts to time out (the test doesn't do much
590 ** anyway, so just loop 10 iterations)
592 static test_return_t
add_wrapper(memcached_st
*memc
)
595 unsigned int max
= 10000;
603 for (x
= 0; x
< max
; x
++)
609 static test_return_t
replace_test(memcached_st
*memc
)
612 const char *key
= "foo";
613 const char *value
= "when we sanitize";
614 const char *original
= "first we insert some data";
616 rc
= memcached_set(memc
, key
, strlen(key
),
617 original
, strlen(original
),
618 (time_t)0, (uint32_t)0);
619 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
621 rc
= memcached_replace(memc
, key
, strlen(key
),
622 value
, strlen(value
),
623 (time_t)0, (uint32_t)0);
624 assert(rc
== MEMCACHED_SUCCESS
);
629 static test_return_t
delete_test(memcached_st
*memc
)
632 const char *key
= "foo";
633 const char *value
= "when we sanitize";
635 rc
= memcached_set(memc
, key
, strlen(key
),
636 value
, strlen(value
),
637 (time_t)0, (uint32_t)0);
638 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
640 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
641 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
646 static test_return_t
flush_test(memcached_st
*memc
)
650 rc
= memcached_flush(memc
, 0);
651 assert(rc
== MEMCACHED_SUCCESS
);
656 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
657 memcached_server_st
*server
__attribute__((unused
)),
658 void *context
__attribute__((unused
)))
662 return MEMCACHED_SUCCESS
;
665 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
668 strcpy(context
, "foo bad");
669 memcached_server_function callbacks
[1];
671 callbacks
[0]= server_function
;
672 memcached_server_cursor(memc
, callbacks
, context
, 1);
676 static test_return_t
bad_key_test(memcached_st
*memc
)
679 const char *key
= "foo bad";
681 size_t string_length
;
683 memcached_st
*memc_clone
;
685 size_t max_keylen
= 0xffff;
687 memc_clone
= memcached_clone(NULL
, memc
);
690 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
691 assert(rc
== MEMCACHED_SUCCESS
);
693 /* All keys are valid in the binary protocol (except for length) */
694 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
696 string
= memcached_get(memc_clone
, key
, strlen(key
),
697 &string_length
, &flags
, &rc
);
698 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
699 assert(string_length
== 0);
703 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
704 assert(rc
== MEMCACHED_SUCCESS
);
705 string
= memcached_get(memc_clone
, key
, strlen(key
),
706 &string_length
, &flags
, &rc
);
707 assert(rc
== MEMCACHED_NOTFOUND
);
708 assert(string_length
== 0);
711 /* Test multi key for bad keys */
712 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
713 size_t key_lengths
[] = { 7, 7, 7 };
715 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
716 assert(rc
== MEMCACHED_SUCCESS
);
718 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
719 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
721 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
722 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
726 /* The following test should be moved to the end of this function when the
727 memcached server is updated to allow max size length of the keys in the
730 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
731 assert(rc
== MEMCACHED_SUCCESS
);
733 char *longkey
= malloc(max_keylen
+ 1);
736 memset(longkey
, 'a', max_keylen
+ 1);
737 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
738 &string_length
, &flags
, &rc
);
739 assert(rc
== MEMCACHED_NOTFOUND
);
740 assert(string_length
== 0);
743 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
744 &string_length
, &flags
, &rc
);
745 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
746 assert(string_length
== 0);
753 /* Make sure zero length keys are marked as bad */
755 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
756 assert(rc
== MEMCACHED_SUCCESS
);
757 string
= memcached_get(memc_clone
, key
, 0,
758 &string_length
, &flags
, &rc
);
759 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
760 assert(string_length
== 0);
763 memcached_free(memc_clone
);
768 #define READ_THROUGH_VALUE "set for me"
769 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
770 char *key
__attribute__((unused
)),
771 size_t key_length
__attribute__((unused
)),
772 memcached_result_st
*result
)
775 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
778 static test_return_t
read_through(memcached_st
*memc
)
781 const char *key
= "foo";
783 size_t string_length
;
785 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
787 string
= memcached_get(memc
, key
, strlen(key
),
788 &string_length
, &flags
, &rc
);
790 assert(rc
== MEMCACHED_NOTFOUND
);
791 assert(string_length
== 0);
794 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
796 assert(rc
== MEMCACHED_SUCCESS
);
798 string
= memcached_get(memc
, key
, strlen(key
),
799 &string_length
, &flags
, &rc
);
801 assert(rc
== MEMCACHED_SUCCESS
);
802 assert(string_length
== strlen(READ_THROUGH_VALUE
));
803 assert(!strcmp(READ_THROUGH_VALUE
, string
));
806 string
= memcached_get(memc
, key
, strlen(key
),
807 &string_length
, &flags
, &rc
);
809 assert(rc
== MEMCACHED_SUCCESS
);
810 assert(string_length
== strlen(READ_THROUGH_VALUE
));
811 assert(!strcmp(READ_THROUGH_VALUE
, string
));
817 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
819 size_t key_length
__attribute__((unused
)))
823 return MEMCACHED_SUCCESS
;
826 static test_return_t
delete_through(memcached_st
*memc
)
828 memcached_trigger_delete_key callback
;
831 callback
= (memcached_trigger_delete_key
)delete_trigger
;
833 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
834 assert(rc
== MEMCACHED_SUCCESS
);
839 static test_return_t
get_test(memcached_st
*memc
)
842 const char *key
= "foo";
844 size_t string_length
;
847 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
848 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
850 string
= memcached_get(memc
, key
, strlen(key
),
851 &string_length
, &flags
, &rc
);
853 assert(rc
== MEMCACHED_NOTFOUND
);
854 assert(string_length
== 0);
860 static test_return_t
get_test2(memcached_st
*memc
)
863 const char *key
= "foo";
864 const char *value
= "when we sanitize";
866 size_t string_length
;
869 rc
= memcached_set(memc
, key
, strlen(key
),
870 value
, strlen(value
),
871 (time_t)0, (uint32_t)0);
872 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
874 string
= memcached_get(memc
, key
, strlen(key
),
875 &string_length
, &flags
, &rc
);
878 assert(rc
== MEMCACHED_SUCCESS
);
879 assert(string_length
== strlen(value
));
880 assert(!memcmp(string
, value
, string_length
));
887 static test_return_t
set_test2(memcached_st
*memc
)
890 const char *key
= "foo";
891 const char *value
= "train in the brain";
892 size_t value_length
= strlen(value
);
895 for (x
= 0; x
< 10; x
++)
897 rc
= memcached_set(memc
, key
, strlen(key
),
899 (time_t)0, (uint32_t)0);
900 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
906 static test_return_t
set_test3(memcached_st
*memc
)
910 size_t value_length
= 8191;
913 value
= (char*)malloc(value_length
);
916 for (x
= 0; x
< value_length
; x
++)
917 value
[x
] = (char) (x
% 127);
919 /* The dump test relies on there being at least 32 items in memcached */
920 for (x
= 0; x
< 32; x
++)
924 sprintf(key
, "foo%u", x
);
926 rc
= memcached_set(memc
, key
, strlen(key
),
928 (time_t)0, (uint32_t)0);
929 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
937 static test_return_t
get_test3(memcached_st
*memc
)
940 const char *key
= "foo";
942 size_t value_length
= 8191;
944 size_t string_length
;
948 value
= (char*)malloc(value_length
);
951 for (x
= 0; x
< value_length
; x
++)
952 value
[x
] = (char) (x
% 127);
954 rc
= memcached_set(memc
, key
, strlen(key
),
956 (time_t)0, (uint32_t)0);
957 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
959 string
= memcached_get(memc
, key
, strlen(key
),
960 &string_length
, &flags
, &rc
);
962 assert(rc
== MEMCACHED_SUCCESS
);
964 assert(string_length
== value_length
);
965 assert(!memcmp(string
, value
, string_length
));
973 static test_return_t
get_test4(memcached_st
*memc
)
976 const char *key
= "foo";
978 size_t value_length
= 8191;
980 size_t string_length
;
984 value
= (char*)malloc(value_length
);
987 for (x
= 0; x
< value_length
; x
++)
988 value
[x
] = (char) (x
% 127);
990 rc
= memcached_set(memc
, key
, strlen(key
),
992 (time_t)0, (uint32_t)0);
993 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
995 for (x
= 0; x
< 10; x
++)
997 string
= memcached_get(memc
, key
, strlen(key
),
998 &string_length
, &flags
, &rc
);
1000 assert(rc
== MEMCACHED_SUCCESS
);
1002 assert(string_length
== value_length
);
1003 assert(!memcmp(string
, value
, string_length
));
1009 return TEST_SUCCESS
;
1013 * This test verifies that memcached_read_one_response doesn't try to
1014 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1015 * responses before you execute a storage command.
1017 static test_return_t
get_test5(memcached_st
*memc
)
1020 ** Request the same key twice, to ensure that we hash to the same server
1021 ** (so that we have multiple response values queued up) ;-)
1023 const char *keys
[]= { "key", "key" };
1024 size_t lengths
[]= { 3, 3 };
1028 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1029 keys
[0], lengths
[0], 0, 0);
1030 assert(rc
== MEMCACHED_SUCCESS
);
1031 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1033 memcached_result_st results_obj
;
1034 memcached_result_st
*results
;
1035 results
=memcached_result_create(memc
, &results_obj
);
1037 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1039 memcached_result_free(&results_obj
);
1041 /* Don't read out the second result, but issue a set instead.. */
1042 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1043 assert(rc
== MEMCACHED_SUCCESS
);
1045 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1046 &rlen
, &flags
, &rc
);
1047 assert(val
== NULL
);
1048 assert(rc
== MEMCACHED_NOTFOUND
);
1049 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1050 assert(val
!= NULL
);
1051 assert(rc
== MEMCACHED_SUCCESS
);
1054 return TEST_SUCCESS
;
1057 static test_return_t
mget_end(memcached_st
*memc
)
1059 const char *keys
[]= { "foo", "foo2" };
1060 size_t lengths
[]= { 3, 4 };
1061 const char *values
[]= { "fjord", "41" };
1063 memcached_return rc
;
1066 for (int i
= 0; i
< 2; i
++)
1068 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1069 (time_t)0, (uint32_t)0);
1070 assert(rc
== MEMCACHED_SUCCESS
);
1074 size_t string_length
;
1077 // retrieve both via mget
1078 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1079 assert(rc
== MEMCACHED_SUCCESS
);
1081 char key
[MEMCACHED_MAX_KEY
];
1084 // this should get both
1085 for (int i
= 0; i
< 2; i
++)
1087 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1089 assert(rc
== MEMCACHED_SUCCESS
);
1091 if (key_length
== 4)
1093 assert(string_length
== strlen(values
[val
]));
1094 assert(strncmp(values
[val
], string
, string_length
) == 0);
1098 // this should indicate end
1099 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1100 assert(rc
== MEMCACHED_END
);
1103 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1104 assert(rc
== MEMCACHED_SUCCESS
);
1106 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1107 assert(key_length
== lengths
[0]);
1108 assert(strncmp(keys
[0], key
, key_length
) == 0);
1109 assert(string_length
== strlen(values
[0]));
1110 assert(strncmp(values
[0], string
, string_length
) == 0);
1111 assert(rc
== MEMCACHED_SUCCESS
);
1114 // this should indicate end
1115 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1116 assert(rc
== MEMCACHED_END
);
1118 return TEST_SUCCESS
;
1121 /* Do not copy the style of this code, I just access hosts to testthis function */
1122 static test_return_t
stats_servername_test(memcached_st
*memc
)
1124 memcached_return rc
;
1125 memcached_stat_st memc_stat
;
1126 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1127 memc
->hosts
[0].hostname
,
1128 memc
->hosts
[0].port
);
1130 return TEST_SUCCESS
;
1133 static test_return_t
increment_test(memcached_st
*memc
)
1135 uint64_t new_number
;
1136 memcached_return rc
;
1137 const char *key
= "number";
1138 const char *value
= "0";
1140 rc
= memcached_set(memc
, key
, strlen(key
),
1141 value
, strlen(value
),
1142 (time_t)0, (uint32_t)0);
1143 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1145 rc
= memcached_increment(memc
, key
, strlen(key
),
1147 assert(rc
== MEMCACHED_SUCCESS
);
1148 assert(new_number
== 1);
1150 rc
= memcached_increment(memc
, key
, strlen(key
),
1152 assert(rc
== MEMCACHED_SUCCESS
);
1153 assert(new_number
== 2);
1155 return TEST_SUCCESS
;
1158 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1160 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1162 uint64_t new_number
;
1163 memcached_return rc
;
1164 const char *key
= "number";
1165 uint64_t initial
= 0;
1167 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1168 1, initial
, 0, &new_number
);
1169 assert(rc
== MEMCACHED_SUCCESS
);
1170 assert(new_number
== initial
);
1172 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1173 1, initial
, 0, &new_number
);
1174 assert(rc
== MEMCACHED_SUCCESS
);
1175 assert(new_number
== (initial
+ 1));
1177 return TEST_SUCCESS
;
1180 static test_return_t
decrement_test(memcached_st
*memc
)
1182 uint64_t new_number
;
1183 memcached_return rc
;
1184 const char *key
= "number";
1185 const char *value
= "3";
1187 rc
= memcached_set(memc
, key
, strlen(key
),
1188 value
, strlen(value
),
1189 (time_t)0, (uint32_t)0);
1190 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1192 rc
= memcached_decrement(memc
, key
, strlen(key
),
1194 assert(rc
== MEMCACHED_SUCCESS
);
1195 assert(new_number
== 2);
1197 rc
= memcached_decrement(memc
, key
, strlen(key
),
1199 assert(rc
== MEMCACHED_SUCCESS
);
1200 assert(new_number
== 1);
1202 return TEST_SUCCESS
;
1205 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1207 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1209 uint64_t new_number
;
1210 memcached_return rc
;
1211 const char *key
= "number";
1212 uint64_t initial
= 3;
1214 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1215 1, initial
, 0, &new_number
);
1216 assert(rc
== MEMCACHED_SUCCESS
);
1217 assert(new_number
== initial
);
1219 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1220 1, initial
, 0, &new_number
);
1221 assert(rc
== MEMCACHED_SUCCESS
);
1222 assert(new_number
== (initial
- 1));
1224 return TEST_SUCCESS
;
1227 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1229 uint64_t new_number
;
1230 memcached_return rc
;
1231 const char *master_key
= "foo";
1232 const char *key
= "number";
1233 const char *value
= "0";
1235 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1237 value
, strlen(value
),
1238 (time_t)0, (uint32_t)0);
1239 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1241 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1243 assert(rc
== MEMCACHED_SUCCESS
);
1244 assert(new_number
== 1);
1246 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1248 assert(rc
== MEMCACHED_SUCCESS
);
1249 assert(new_number
== 2);
1251 return TEST_SUCCESS
;
1254 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1256 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1258 uint64_t new_number
;
1259 memcached_return rc
;
1260 const char *master_key
= "foo";
1261 const char *key
= "number";
1262 uint64_t initial
= 0;
1264 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1266 1, initial
, 0, &new_number
);
1267 assert(rc
== MEMCACHED_SUCCESS
);
1268 assert(new_number
== initial
);
1270 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1272 1, initial
, 0, &new_number
);
1273 assert(rc
== MEMCACHED_SUCCESS
);
1274 assert(new_number
== (initial
+ 1));
1276 return TEST_SUCCESS
;
1279 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1281 uint64_t new_number
;
1282 memcached_return rc
;
1283 const char *master_key
= "foo";
1284 const char *key
= "number";
1285 const char *value
= "3";
1287 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1289 value
, strlen(value
),
1290 (time_t)0, (uint32_t)0);
1291 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1293 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1296 assert(rc
== MEMCACHED_SUCCESS
);
1297 assert(new_number
== 2);
1299 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1302 assert(rc
== MEMCACHED_SUCCESS
);
1303 assert(new_number
== 1);
1305 return TEST_SUCCESS
;
1308 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1310 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1312 uint64_t new_number
;
1313 memcached_return rc
;
1314 const char *master_key
= "foo";
1315 const char *key
= "number";
1316 uint64_t initial
= 3;
1318 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1320 1, initial
, 0, &new_number
);
1321 assert(rc
== MEMCACHED_SUCCESS
);
1322 assert(new_number
== initial
);
1324 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1326 1, initial
, 0, &new_number
);
1327 assert(rc
== MEMCACHED_SUCCESS
);
1328 assert(new_number
== (initial
- 1));
1330 return TEST_SUCCESS
;
1333 static test_return_t
quit_test(memcached_st
*memc
)
1335 memcached_return rc
;
1336 const char *key
= "fudge";
1337 const char *value
= "sanford and sun";
1339 rc
= memcached_set(memc
, key
, strlen(key
),
1340 value
, strlen(value
),
1341 (time_t)10, (uint32_t)3);
1342 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1343 memcached_quit(memc
);
1345 rc
= memcached_set(memc
, key
, strlen(key
),
1346 value
, strlen(value
),
1347 (time_t)50, (uint32_t)9);
1348 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1350 return TEST_SUCCESS
;
1353 static test_return_t
mget_result_test(memcached_st
*memc
)
1355 memcached_return rc
;
1356 const char *keys
[]= {"fudge", "son", "food"};
1357 size_t key_length
[]= {5, 3, 4};
1360 memcached_result_st results_obj
;
1361 memcached_result_st
*results
;
1363 results
= memcached_result_create(memc
, &results_obj
);
1365 assert(&results_obj
== results
);
1367 /* We need to empty the server before continueing test */
1368 rc
= memcached_flush(memc
, 0);
1369 assert(rc
== MEMCACHED_SUCCESS
);
1371 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1372 assert(rc
== MEMCACHED_SUCCESS
);
1374 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1379 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1381 assert(rc
== MEMCACHED_END
);
1383 for (x
= 0; x
< 3; x
++)
1385 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1386 keys
[x
], key_length
[x
],
1387 (time_t)50, (uint32_t)9);
1388 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1391 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1392 assert(rc
== MEMCACHED_SUCCESS
);
1394 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1397 assert(&results_obj
== results
);
1398 assert(rc
== MEMCACHED_SUCCESS
);
1399 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1400 assert(!memcmp(memcached_result_key_value(results
),
1401 memcached_result_value(results
),
1402 memcached_result_length(results
)));
1405 memcached_result_free(&results_obj
);
1407 return TEST_SUCCESS
;
1410 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1412 memcached_return rc
;
1413 const char *keys
[]= {"fudge", "son", "food"};
1414 size_t key_length
[]= {5, 3, 4};
1417 memcached_result_st
*results
;
1419 /* We need to empty the server before continueing test */
1420 rc
= memcached_flush(memc
, 0);
1421 assert(rc
== MEMCACHED_SUCCESS
);
1423 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1424 assert(rc
== MEMCACHED_SUCCESS
);
1426 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1431 assert(rc
== MEMCACHED_END
);
1433 for (x
= 0; x
< 3; x
++)
1435 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1436 keys
[x
], key_length
[x
],
1437 (time_t)50, (uint32_t)9);
1438 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1441 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1442 assert(rc
== MEMCACHED_SUCCESS
);
1445 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1448 assert(rc
== MEMCACHED_SUCCESS
);
1449 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1450 assert(!memcmp(memcached_result_key_value(results
),
1451 memcached_result_value(results
),
1452 memcached_result_length(results
)));
1453 memcached_result_free(results
);
1457 return TEST_SUCCESS
;
1460 /* Count the results */
1461 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1462 memcached_result_st
*result
__attribute__((unused
)),
1465 unsigned int *counter
= (unsigned int *)context
;
1467 *counter
= *counter
+ 1;
1469 return MEMCACHED_SUCCESS
;
1472 static test_return_t
mget_result_function(memcached_st
*memc
)
1474 memcached_return rc
;
1475 const char *keys
[]= {"fudge", "son", "food"};
1476 size_t key_length
[]= {5, 3, 4};
1478 unsigned int counter
;
1479 memcached_execute_function callbacks
[1];
1481 /* We need to empty the server before continueing test */
1482 rc
= memcached_flush(memc
, 0);
1483 for (x
= 0; x
< 3; x
++)
1485 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1486 keys
[x
], key_length
[x
],
1487 (time_t)50, (uint32_t)9);
1488 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1491 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1492 assert(rc
== MEMCACHED_SUCCESS
);
1494 callbacks
[0]= &callback_counter
;
1496 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1498 assert(counter
== 3);
1500 return TEST_SUCCESS
;
1503 static test_return_t
mget_test(memcached_st
*memc
)
1505 memcached_return rc
;
1506 const char *keys
[]= {"fudge", "son", "food"};
1507 size_t key_length
[]= {5, 3, 4};
1511 char return_key
[MEMCACHED_MAX_KEY
];
1512 size_t return_key_length
;
1514 size_t return_value_length
;
1516 /* We need to empty the server before continueing test */
1517 rc
= memcached_flush(memc
, 0);
1518 assert(rc
== MEMCACHED_SUCCESS
);
1520 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1521 assert(rc
== MEMCACHED_SUCCESS
);
1523 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1524 &return_value_length
, &flags
, &rc
)) != NULL
)
1526 assert(return_value
);
1528 assert(!return_value
);
1529 assert(return_value_length
== 0);
1530 assert(rc
== MEMCACHED_END
);
1532 for (x
= 0; x
< 3; x
++)
1534 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1535 keys
[x
], key_length
[x
],
1536 (time_t)50, (uint32_t)9);
1537 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1540 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1541 assert(rc
== MEMCACHED_SUCCESS
);
1544 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1545 &return_value_length
, &flags
, &rc
)))
1547 assert(return_value
);
1548 assert(rc
== MEMCACHED_SUCCESS
);
1549 assert(return_key_length
== return_value_length
);
1550 assert(!memcmp(return_value
, return_key
, return_value_length
));
1555 return TEST_SUCCESS
;
1558 static test_return_t
mget_execute(memcached_st
*memc
)
1561 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1565 * I only want to hit _one_ server so I know the number of requests I'm
1566 * sending in the pipeline.
1568 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1569 memc
->number_of_hosts
= 1;
1571 int max_keys
= binary
? 20480 : 1;
1574 char **keys
= calloc((size_t)max_keys
, sizeof(char*));
1575 size_t *key_length
=calloc((size_t)max_keys
, sizeof(size_t));
1577 /* First add all of the items.. */
1578 char blob
[1024] = {0};
1579 memcached_return rc
;
1580 for (int x
= 0; x
< max_keys
; ++x
)
1583 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
1585 assert(keys
[x
] != NULL
);
1586 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1587 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1590 /* Try to get all of them with a large multiget */
1591 unsigned int counter
= 0;
1592 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
1593 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1594 (size_t)max_keys
, callbacks
, &counter
, 1);
1598 assert(rc
== MEMCACHED_SUCCESS
);
1600 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1601 assert(rc
== MEMCACHED_END
);
1603 /* Verify that we got all of the items */
1604 assert(counter
== (unsigned int)max_keys
);
1608 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
1609 assert(counter
== 0);
1612 /* Release all allocated resources */
1613 for (int x
= 0; x
< max_keys
; ++x
)
1618 memc
->number_of_hosts
= number_of_hosts
;
1619 return TEST_SUCCESS
;
1622 static test_return_t
get_stats_keys(memcached_st
*memc
)
1626 memcached_stat_st memc_stat
;
1627 memcached_return rc
;
1629 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1630 assert(rc
== MEMCACHED_SUCCESS
);
1631 for (ptr
= list
; *ptr
; ptr
++)
1637 return TEST_SUCCESS
;
1640 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1642 const char *version_string
;
1644 version_string
= memcached_lib_version();
1646 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1648 return TEST_SUCCESS
;
1651 static test_return_t
get_stats(memcached_st
*memc
)
1656 memcached_return rc
;
1657 memcached_stat_st
*memc_stat
;
1659 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1660 assert(rc
== MEMCACHED_SUCCESS
);
1662 assert(rc
== MEMCACHED_SUCCESS
);
1665 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1667 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1668 assert(rc
== MEMCACHED_SUCCESS
);
1669 for (ptr
= list
; *ptr
; ptr
++);
1674 memcached_stat_free(NULL
, memc_stat
);
1676 return TEST_SUCCESS
;
1679 static test_return_t
add_host_test(memcached_st
*memc
)
1682 memcached_server_st
*servers
;
1683 memcached_return rc
;
1684 char servername
[]= "0.example.com";
1686 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1688 assert(1 == memcached_server_list_count(servers
));
1690 for (x
= 2; x
< 20; x
++)
1692 char buffer
[SMALL_STRING_LEN
];
1694 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1695 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1697 assert(rc
== MEMCACHED_SUCCESS
);
1698 assert(x
== memcached_server_list_count(servers
));
1701 rc
= memcached_server_push(memc
, servers
);
1702 assert(rc
== MEMCACHED_SUCCESS
);
1703 rc
= memcached_server_push(memc
, servers
);
1704 assert(rc
== MEMCACHED_SUCCESS
);
1706 memcached_server_list_free(servers
);
1708 return TEST_SUCCESS
;
1711 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1713 return MEMCACHED_SUCCESS
;
1716 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1718 return MEMCACHED_SUCCESS
;
1721 static test_return_t
callback_test(memcached_st
*memc
)
1723 /* Test User Data */
1727 memcached_return rc
;
1729 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1730 assert(rc
== MEMCACHED_SUCCESS
);
1731 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1732 assert(*test_ptr
== x
);
1735 /* Test Clone Callback */
1737 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1738 void *clone_cb_ptr
= *(void **)&clone_cb
;
1739 void *temp_function
= NULL
;
1740 memcached_return rc
;
1742 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1744 assert(rc
== MEMCACHED_SUCCESS
);
1745 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1746 assert(temp_function
== clone_cb_ptr
);
1749 /* Test Cleanup Callback */
1751 memcached_cleanup_func cleanup_cb
=
1752 (memcached_cleanup_func
)cleanup_test_callback
;
1753 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1754 void *temp_function
= NULL
;
1755 memcached_return rc
;
1757 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1759 assert(rc
== MEMCACHED_SUCCESS
);
1760 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1761 assert(temp_function
== cleanup_cb_ptr
);
1764 return TEST_SUCCESS
;
1767 /* We don't test the behavior itself, we test the switches */
1768 static test_return_t
behavior_test(memcached_st
*memc
)
1773 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1774 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1777 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1778 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1781 set
= MEMCACHED_HASH_MD5
;
1782 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1783 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1784 assert(value
== MEMCACHED_HASH_MD5
);
1788 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1789 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1792 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1793 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1796 set
= MEMCACHED_HASH_DEFAULT
;
1797 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1798 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1799 assert(value
== MEMCACHED_HASH_DEFAULT
);
1801 set
= MEMCACHED_HASH_CRC
;
1802 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1803 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1804 assert(value
== MEMCACHED_HASH_CRC
);
1806 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1809 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1812 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1813 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1814 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1815 return TEST_SUCCESS
;
1818 static test_return_t
fetch_all_results(memcached_st
*memc
)
1820 memcached_return rc
= MEMCACHED_SUCCESS
;
1821 char return_key
[MEMCACHED_MAX_KEY
];
1822 size_t return_key_length
;
1824 size_t return_value_length
;
1827 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1828 &return_value_length
, &flags
, &rc
)))
1830 assert(return_value
);
1831 assert(rc
== MEMCACHED_SUCCESS
);
1835 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
1838 /* Test case provided by Cal Haldenbrand */
1839 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
1841 unsigned int setter
= 1;
1844 unsigned long long total
= 0;
1847 char randomstuff
[6 * 1024];
1848 memcached_return rc
;
1850 memset(randomstuff
, 0, 6 * 1024);
1852 /* We just keep looking at the same values over and over */
1855 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1856 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1860 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1864 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1865 memset(randomstuff
, 0, 6 * 1024);
1866 assert(size
< 6 * 1024); /* Being safe here */
1868 for (j
= 0 ; j
< size
;j
++)
1869 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1872 sprintf(key
, "%d", x
);
1873 rc
= memcached_set(memc
, key
, strlen(key
),
1874 randomstuff
, strlen(randomstuff
), 10, 0);
1875 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1876 /* If we fail, lets try again */
1877 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1878 rc
= memcached_set(memc
, key
, strlen(key
),
1879 randomstuff
, strlen(randomstuff
), 10, 0);
1880 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1883 return TEST_SUCCESS
;
1886 /* Test case provided by Cal Haldenbrand */
1887 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
1890 unsigned int setter
;
1892 unsigned long long total
;
1895 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1896 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1898 setter
= 20 * 1024576;
1899 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1900 setter
= 20 * 1024576;
1901 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1902 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1903 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1905 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1908 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1910 memcached_return rc
= MEMCACHED_SUCCESS
;
1911 char buffer
[SMALL_STRING_LEN
];
1916 memset(buffer
, 0, SMALL_STRING_LEN
);
1918 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1919 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1920 &val_len
, &flags
, &rc
);
1921 if (rc
!= MEMCACHED_SUCCESS
)
1923 if (rc
== MEMCACHED_NOTFOUND
)
1937 return TEST_SUCCESS
;
1940 /* Do a large mget() over all the keys we think exist */
1941 #define KEY_COUNT 3000 // * 1024576
1942 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
1944 memcached_return rc
;
1945 unsigned int setter
;
1948 size_t key_lengths
[KEY_COUNT
];
1951 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1952 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1954 setter
= 20 * 1024576;
1955 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1956 setter
= 20 * 1024576;
1957 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1958 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1959 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1962 keys
= calloc(KEY_COUNT
, sizeof(char *));
1964 for (x
= 0; x
< KEY_COUNT
; x
++)
1968 snprintf(buffer
, 30, "%u", x
);
1969 keys
[x
]= strdup(buffer
);
1970 key_lengths
[x
]= strlen(keys
[x
]);
1973 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
1974 assert(rc
== MEMCACHED_SUCCESS
);
1976 assert(fetch_all_results(memc
) == TEST_SUCCESS
);
1978 for (x
= 0; x
< KEY_COUNT
; x
++)
1982 return TEST_SUCCESS
;
1985 /* Make sure we behave properly if server list has no values */
1986 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
1988 memcached_return rc
;
1989 const char *keys
[]= {"fudge", "son", "food"};
1990 size_t key_length
[]= {5, 3, 4};
1993 char return_key
[MEMCACHED_MAX_KEY
];
1994 size_t return_key_length
;
1996 size_t return_value_length
;
1998 /* Here we free everything before running a bunch of mget tests */
2000 memcached_server_list_free(memc
->hosts
);
2002 memc
->number_of_hosts
= 0;
2006 /* We need to empty the server before continueing test */
2007 rc
= memcached_flush(memc
, 0);
2008 assert(rc
== MEMCACHED_NO_SERVERS
);
2010 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2011 assert(rc
== MEMCACHED_NO_SERVERS
);
2013 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2014 &return_value_length
, &flags
, &rc
)) != NULL
)
2016 assert(return_value
);
2018 assert(!return_value
);
2019 assert(return_value_length
== 0);
2020 assert(rc
== MEMCACHED_NO_SERVERS
);
2022 for (x
= 0; x
< 3; x
++)
2024 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2025 keys
[x
], key_length
[x
],
2026 (time_t)50, (uint32_t)9);
2027 assert(rc
== MEMCACHED_NO_SERVERS
);
2030 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2031 assert(rc
== MEMCACHED_NO_SERVERS
);
2034 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2035 &return_value_length
, &flags
, &rc
)))
2037 assert(return_value
);
2038 assert(rc
== MEMCACHED_SUCCESS
);
2039 assert(return_key_length
== return_value_length
);
2040 assert(!memcmp(return_value
, return_key
, return_value_length
));
2045 return TEST_SUCCESS
;
2048 #define VALUE_SIZE_BUG5 1048064
2049 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2051 memcached_return rc
;
2052 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2053 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2054 char return_key
[MEMCACHED_MAX_KEY
];
2055 size_t return_key_length
;
2057 size_t value_length
;
2061 char insert_data
[VALUE_SIZE_BUG5
];
2063 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2064 insert_data
[x
]= (signed char)rand();
2066 memcached_flush(memc
, 0);
2067 value
= memcached_get(memc
, keys
[0], key_length
[0],
2068 &value_length
, &flags
, &rc
);
2069 assert(value
== NULL
);
2070 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2073 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2074 &value_length
, &flags
, &rc
)))
2078 for (x
= 0; x
< 4; x
++)
2080 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2081 insert_data
, VALUE_SIZE_BUG5
,
2082 (time_t)0, (uint32_t)0);
2083 assert(rc
== MEMCACHED_SUCCESS
);
2086 for (x
= 0; x
< 10; x
++)
2088 value
= memcached_get(memc
, keys
[0], key_length
[0],
2089 &value_length
, &flags
, &rc
);
2093 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2095 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2096 &value_length
, &flags
, &rc
)))
2104 return TEST_SUCCESS
;
2107 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2109 memcached_return rc
;
2110 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2111 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2112 char return_key
[MEMCACHED_MAX_KEY
];
2113 size_t return_key_length
;
2115 size_t value_length
;
2119 char insert_data
[VALUE_SIZE_BUG5
];
2121 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2122 insert_data
[x
]= (signed char)rand();
2124 memcached_flush(memc
, 0);
2125 value
= memcached_get(memc
, keys
[0], key_length
[0],
2126 &value_length
, &flags
, &rc
);
2127 assert(value
== NULL
);
2128 assert(rc
== MEMCACHED_NOTFOUND
);
2129 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2130 assert(rc
== MEMCACHED_SUCCESS
);
2133 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2134 &value_length
, &flags
, &rc
)))
2137 assert(rc
== MEMCACHED_END
);
2139 for (x
= 0; x
< 4; x
++)
2141 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2142 insert_data
, VALUE_SIZE_BUG5
,
2143 (time_t)0, (uint32_t)0);
2144 assert(rc
== MEMCACHED_SUCCESS
);
2147 for (x
= 0; x
< 2; x
++)
2149 value
= memcached_get(memc
, keys
[0], key_length
[0],
2150 &value_length
, &flags
, &rc
);
2154 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2155 assert(rc
== MEMCACHED_SUCCESS
);
2157 /* We test for purge of partial complete fetches */
2158 for (count
= 3; count
; count
--)
2160 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2161 &value_length
, &flags
, &rc
);
2162 assert(rc
== MEMCACHED_SUCCESS
);
2163 assert(!(memcmp(value
, insert_data
, value_length
)));
2164 assert(value_length
);
2169 return TEST_SUCCESS
;
2172 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2174 memcached_return rc
;
2176 memcached_st
*memc_clone
;
2178 memcached_server_st
*servers
;
2179 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";
2181 servers
= memcached_servers_parse(server_list
);
2184 mine
= memcached_create(NULL
);
2185 rc
= memcached_server_push(mine
, servers
);
2186 assert(rc
== MEMCACHED_SUCCESS
);
2187 memcached_server_list_free(servers
);
2190 memc_clone
= memcached_clone(NULL
, mine
);
2192 memcached_quit(mine
);
2193 memcached_quit(memc_clone
);
2196 memcached_free(mine
);
2197 memcached_free(memc_clone
);
2199 return TEST_SUCCESS
;
2202 /* Test flag store/retrieve */
2203 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2205 memcached_return rc
;
2206 const char *keys
= "036790384900";
2207 size_t key_length
= strlen(keys
);
2208 char return_key
[MEMCACHED_MAX_KEY
];
2209 size_t return_key_length
;
2211 size_t value_length
;
2214 char insert_data
[VALUE_SIZE_BUG5
];
2216 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2217 insert_data
[x
]= (signed char)rand();
2219 memcached_flush(memc
, 0);
2222 rc
= memcached_set(memc
, keys
, key_length
,
2223 insert_data
, VALUE_SIZE_BUG5
,
2225 assert(rc
== MEMCACHED_SUCCESS
);
2228 value
= memcached_get(memc
, keys
, key_length
,
2229 &value_length
, &flags
, &rc
);
2230 assert(flags
== 245);
2234 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2237 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2238 &value_length
, &flags
, &rc
);
2239 assert(flags
== 245);
2244 return TEST_SUCCESS
;
2247 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2249 memcached_return rc
;
2250 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2251 size_t key_length
[3];
2256 char return_key
[MEMCACHED_MAX_KEY
];
2257 size_t return_key_length
;
2259 size_t return_value_length
;
2262 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2263 key_length
[1]= strlen("fudge&*@#");
2264 key_length
[2]= strlen("for^#@&$not");
2267 for (x
= 0; x
< 3; x
++)
2269 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2270 keys
[x
], key_length
[x
],
2271 (time_t)50, (uint32_t)9);
2272 assert(rc
== MEMCACHED_SUCCESS
);
2275 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2276 assert(rc
== MEMCACHED_SUCCESS
);
2278 /* We need to empty the server before continueing test */
2279 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2280 &return_value_length
, &flags
, &rc
)) != NULL
)
2282 assert(return_value
);
2288 return TEST_SUCCESS
;
2291 /* We are testing with aggressive timeout to get failures */
2292 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2294 const char *key
= "foo";
2296 size_t value_length
= 512;
2299 memcached_return rc
;
2300 unsigned int set
= 1;
2301 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2304 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2305 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2307 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2310 value
= (char*)malloc(value_length
* sizeof(char));
2312 for (x
= 0; x
< value_length
; x
++)
2313 value
[x
]= (char) (x
% 127);
2315 for (x
= 1; x
<= 100000; ++x
)
2317 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2319 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2320 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2322 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2327 memcached_free(mclone
);
2329 return TEST_SUCCESS
;
2333 We are looking failures in the async protocol
2335 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2337 const char *key
= "foo";
2339 size_t value_length
= 512;
2342 memcached_return rc
;
2343 unsigned int set
= 1;
2345 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2347 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2348 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2350 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2353 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2355 assert(timeout
== -1);
2357 value
= (char*)malloc(value_length
* sizeof(char));
2359 for (x
= 0; x
< value_length
; x
++)
2360 value
[x
]= (char) (x
% 127);
2362 for (x
= 1; x
<= 100000; ++x
)
2364 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2368 memcached_free(mclone
);
2370 return TEST_SUCCESS
;
2374 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2376 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2378 memcached_return rc
;
2380 size_t value_length
;
2382 uint64_t number_value
;
2384 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2385 &value_length
, &flags
, &rc
);
2386 assert(value
== NULL
);
2387 assert(rc
== MEMCACHED_NOTFOUND
);
2389 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2392 assert(value
== NULL
);
2393 /* The binary protocol will set the key if it doesn't exist */
2394 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2395 assert(rc
== MEMCACHED_SUCCESS
);
2397 assert(rc
== MEMCACHED_NOTFOUND
);
2399 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2401 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2402 &value_length
, &flags
, &rc
);
2404 assert(rc
== MEMCACHED_SUCCESS
);
2407 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2409 assert(number_value
== 2);
2410 assert(rc
== MEMCACHED_SUCCESS
);
2412 return TEST_SUCCESS
;
2416 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2417 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2419 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2421 char key
[] = "key34567890";
2423 memcached_return rc
;
2424 size_t overflowSize
;
2426 char commandFirst
[]= "set key34567890 0 0 ";
2427 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2428 size_t commandLength
;
2431 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2433 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2435 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2437 overflow
= malloc(testSize
);
2438 assert(overflow
!= NULL
);
2440 memset(overflow
, 'x', testSize
);
2441 rc
= memcached_set(memc
, key
, strlen(key
),
2442 overflow
, testSize
, 0, 0);
2443 assert(rc
== MEMCACHED_SUCCESS
);
2447 return TEST_SUCCESS
;
2452 Test values of many different sizes
2453 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2454 set key34567890 0 0 8169 \r\n
2455 is sent followed by buffer of size 8169, followed by 8169
2457 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2460 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2461 memcached_return rc
;
2462 const char *key
= "foo";
2464 size_t value_length
= 18000;
2466 size_t string_length
;
2469 size_t current_length
;
2471 value
= (char*)malloc(value_length
);
2474 for (x
= 0; x
< value_length
; x
++)
2475 value
[x
] = (char) (x
% 127);
2477 for (current_length
= 0; current_length
< value_length
; current_length
++)
2479 rc
= memcached_set(memc
, key
, strlen(key
),
2480 value
, current_length
,
2481 (time_t)0, (uint32_t)0);
2482 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2484 string
= memcached_get(memc
, key
, strlen(key
),
2485 &string_length
, &flags
, &rc
);
2487 assert(rc
== MEMCACHED_SUCCESS
);
2488 assert(string_length
== current_length
);
2489 assert(!memcmp(string
, value
, string_length
));
2496 return TEST_SUCCESS
;
2500 Look for zero length value problems
2502 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2505 memcached_return rc
;
2506 const char *key
= "mykey";
2511 for (x
= 0; x
< 2; x
++)
2513 rc
= memcached_set(memc
, key
, strlen(key
),
2515 (time_t)0, (uint32_t)0);
2517 assert(rc
== MEMCACHED_SUCCESS
);
2519 value
= memcached_get(memc
, key
, strlen(key
),
2520 &length
, &flags
, &rc
);
2522 assert(rc
== MEMCACHED_SUCCESS
);
2523 assert(value
== NULL
);
2524 assert(length
== 0);
2527 value
= memcached_get(memc
, key
, strlen(key
),
2528 &length
, &flags
, &rc
);
2530 assert(rc
== MEMCACHED_SUCCESS
);
2531 assert(value
== NULL
);
2532 assert(length
== 0);
2536 return TEST_SUCCESS
;
2539 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2540 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2542 memcached_return rc
;
2543 const char *key
= "mykey";
2548 rc
= memcached_set(memc
, key
, strlen(key
),
2550 (time_t)0, UINT32_MAX
);
2552 assert(rc
== MEMCACHED_SUCCESS
);
2554 value
= memcached_get(memc
, key
, strlen(key
),
2555 &length
, &flags
, &rc
);
2557 assert(rc
== MEMCACHED_SUCCESS
);
2558 assert(value
== NULL
);
2559 assert(length
== 0);
2560 assert(flags
== UINT32_MAX
);
2562 return TEST_SUCCESS
;
2566 /* Check the validity of chinese key*/
2567 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2569 memcached_return rc
;
2570 const char *key
= "豆瓣";
2571 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2576 rc
= memcached_set(memc
, key
, strlen(key
),
2577 value
, strlen(value
),
2580 assert(rc
== MEMCACHED_SUCCESS
);
2582 value2
= memcached_get(memc
, key
, strlen(key
),
2583 &length
, &flags
, &rc
);
2585 assert(length
==strlen(value
));
2586 assert(rc
== MEMCACHED_SUCCESS
);
2587 assert(memcmp(value
, value2
, length
)==0);
2590 return TEST_SUCCESS
;
2598 static test_return_t
user_supplied_bug19(memcached_st
*memc
)
2601 memcached_server_st
*s
;
2602 memcached_return res
;
2606 m
= memcached_create(NULL
);
2607 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2608 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2610 s
= memcached_server_by_key(m
, "a", 1, &res
);
2611 memcached_server_free(s
);
2615 return TEST_SUCCESS
;
2618 /* CAS test from Andei */
2619 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2621 memcached_return status
;
2622 memcached_result_st
*result
, result_obj
;
2623 const char *key
= "abc";
2624 size_t key_len
= strlen("abc");
2625 const char *value
= "foobar";
2626 size_t value_len
= strlen(value
);
2628 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2630 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2631 assert(status
== MEMCACHED_SUCCESS
);
2633 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2634 assert(status
== MEMCACHED_SUCCESS
);
2636 result
= memcached_result_create(memc
, &result_obj
);
2639 memcached_result_create(memc
, &result_obj
);
2640 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2643 assert(status
== MEMCACHED_SUCCESS
);
2645 memcached_result_free(result
);
2647 return TEST_SUCCESS
;
2650 #include "ketama_test_cases.h"
2651 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2653 memcached_return rc
;
2656 memcached_server_st
*server_pool
;
2661 memc
= memcached_create(NULL
);
2664 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2665 assert(rc
== MEMCACHED_SUCCESS
);
2667 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2670 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2671 assert(rc
== MEMCACHED_SUCCESS
);
2673 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2674 assert(value
== MEMCACHED_HASH_MD5
);
2676 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");
2677 memcached_server_push(memc
, server_pool
);
2679 /* verify that the server list was parsed okay. */
2680 assert(memc
->number_of_hosts
== 8);
2681 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2682 assert(server_pool
[0].port
== 11211);
2683 assert(server_pool
[0].weight
== 600);
2684 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2685 assert(server_pool
[2].port
== 11211);
2686 assert(server_pool
[2].weight
== 200);
2687 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2688 assert(server_pool
[7].port
== 11211);
2689 assert(server_pool
[7].weight
== 100);
2691 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2692 * us test the boundary wraparound.
2694 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2696 /* verify the standard ketama set. */
2697 for (x
= 0; x
< 99; x
++)
2699 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2700 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2701 assert(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
2704 memcached_server_list_free(server_pool
);
2705 memcached_free(memc
);
2707 return TEST_SUCCESS
;
2710 /* Large mget() of missing keys with binary proto
2712 * If many binary quiet commands (such as getq's in an mget) fill the output
2713 * buffer and the server chooses not to respond, memcached_flush hangs. See
2714 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2717 /* sighandler_t function that always asserts false */
2718 static void fail(int unused
__attribute__((unused
)))
2724 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2726 memcached_return rc
;
2729 size_t* key_lengths
;
2730 void (*oldalarm
)(int);
2731 memcached_st
*memc_clone
;
2733 memc_clone
= memcached_clone(NULL
, memc
);
2736 /* only binproto uses getq for mget */
2737 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2739 /* empty the cache to ensure misses (hence non-responses) */
2740 rc
= memcached_flush(memc_clone
, 0);
2741 assert(rc
== MEMCACHED_SUCCESS
);
2743 key_lengths
= calloc(key_count
, sizeof(size_t));
2744 keys
= calloc(key_count
, sizeof(char *));
2746 for (x
= 0; x
< key_count
; x
++)
2750 snprintf(buffer
, 30, "%u", x
);
2751 keys
[x
]= strdup(buffer
);
2752 key_lengths
[x
]= strlen(keys
[x
]);
2755 oldalarm
= signal(SIGALRM
, fail
);
2758 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2759 assert(rc
== MEMCACHED_SUCCESS
);
2762 signal(SIGALRM
, oldalarm
);
2764 assert(fetch_all_results(memc
) == TEST_SUCCESS
);
2766 for (x
= 0; x
< key_count
; x
++)
2771 memcached_free(memc_clone
);
2773 return TEST_SUCCESS
;
2776 static memcached_return
pre_binary(memcached_st
*memc
);
2778 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
2780 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
2781 return TEST_SKIPPED
;
2785 /* should work as of r580 */
2786 rc
= _user_supplied_bug21(memc
, 10);
2787 assert(rc
== TEST_SUCCESS
);
2789 /* should fail as of r580 */
2790 rc
= _user_supplied_bug21(memc
, 1000);
2791 assert(rc
== TEST_SUCCESS
);
2793 return TEST_SUCCESS
;
2796 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
2800 memcached_return rc
;
2801 memcached_st
*memc
= memcached_create(NULL
);
2804 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2805 assert(rc
== MEMCACHED_SUCCESS
);
2807 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2810 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2811 assert(rc
== MEMCACHED_SUCCESS
);
2813 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2814 assert(value
== MEMCACHED_HASH_MD5
);
2816 /* server should be removed when in delay */
2817 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2818 assert(rc
== MEMCACHED_SUCCESS
);
2820 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2823 memcached_server_st
*server_pool
;
2824 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");
2825 memcached_server_push(memc
, server_pool
);
2827 /* verify that the server list was parsed okay. */
2828 assert(memc
->number_of_hosts
== 8);
2829 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2830 assert(server_pool
[0].port
== 11211);
2831 assert(server_pool
[0].weight
== 600);
2832 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2833 assert(server_pool
[2].port
== 11211);
2834 assert(server_pool
[2].weight
== 200);
2835 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2836 assert(server_pool
[7].port
== 11211);
2837 assert(server_pool
[7].weight
== 100);
2839 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2840 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2842 for (int x
= 0; x
< 99; x
++)
2844 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2845 assert(server_idx
!= 2);
2848 /* and re-added when it's back. */
2849 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2850 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2851 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
2852 memc
->distribution
);
2853 for (int x
= 0; x
< 99; x
++)
2855 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2856 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2857 assert(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
2860 memcached_server_list_free(server_pool
);
2861 memcached_free(memc
);
2863 return TEST_SUCCESS
;
2866 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
2870 memcached_return rc
;
2871 memcached_st
*memc
= memcached_create(NULL
);
2875 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2876 assert(rc
== MEMCACHED_SUCCESS
);
2878 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2881 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2882 assert(rc
== MEMCACHED_SUCCESS
);
2884 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2885 assert(value
== MEMCACHED_HASH_MD5
);
2888 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
,
2889 MEMCACHED_KETAMA_COMPAT_SPY
) == MEMCACHED_SUCCESS
);
2891 memcached_server_st
*server_pool
;
2892 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");
2893 memcached_server_push(memc
, server_pool
);
2896 if ((fp
= fopen("ketama_keys.txt", "w")))
2900 printf("cannot write to file ketama_keys.txt");
2901 return TEST_FAILURE
;
2904 for (int x
= 0; x
< 10000; x
++)
2907 sprintf(key
, "%d", x
);
2909 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
2910 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2911 unsigned int port
= memc
->hosts
[server_idx
].port
;
2912 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
2915 memcached_server_list_free(server_pool
);
2916 memcached_free(memc
);
2918 return TEST_SUCCESS
;
2922 static test_return_t
result_static(memcached_st
*memc
)
2924 memcached_result_st result
;
2925 memcached_result_st
*result_ptr
;
2927 result_ptr
= memcached_result_create(memc
, &result
);
2928 assert(result
.is_allocated
== false);
2930 memcached_result_free(&result
);
2932 return TEST_SUCCESS
;
2935 static test_return_t
result_alloc(memcached_st
*memc
)
2937 memcached_result_st
*result
;
2939 result
= memcached_result_create(memc
, NULL
);
2941 memcached_result_free(result
);
2943 return TEST_SUCCESS
;
2946 static test_return_t
string_static_null(memcached_st
*memc
)
2948 memcached_string_st string
;
2949 memcached_string_st
*string_ptr
;
2951 string_ptr
= memcached_string_create(memc
, &string
, 0);
2952 assert(string
.is_allocated
== false);
2954 memcached_string_free(&string
);
2956 return TEST_SUCCESS
;
2959 static test_return_t
string_alloc_null(memcached_st
*memc
)
2961 memcached_string_st
*string
;
2963 string
= memcached_string_create(memc
, NULL
, 0);
2965 memcached_string_free(string
);
2967 return TEST_SUCCESS
;
2970 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
2972 memcached_string_st
*string
;
2974 string
= memcached_string_create(memc
, NULL
, 1024);
2976 memcached_string_free(string
);
2978 return TEST_SUCCESS
;
2981 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
2983 memcached_string_st
*string
;
2985 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2986 assert(string
== NULL
);
2988 return TEST_SUCCESS
;
2991 static test_return_t
string_alloc_append(memcached_st
*memc
)
2994 char buffer
[SMALL_STRING_LEN
];
2995 memcached_string_st
*string
;
2997 /* Ring the bell! */
2998 memset(buffer
, 6, SMALL_STRING_LEN
);
3000 string
= memcached_string_create(memc
, NULL
, 100);
3003 for (x
= 0; x
< 1024; x
++)
3005 memcached_return rc
;
3006 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3007 assert(rc
== MEMCACHED_SUCCESS
);
3009 memcached_string_free(string
);
3011 return TEST_SUCCESS
;
3014 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3016 memcached_return rc
;
3018 char buffer
[SMALL_STRING_LEN
];
3019 memcached_string_st
*string
;
3021 /* Ring the bell! */
3022 memset(buffer
, 6, SMALL_STRING_LEN
);
3024 string
= memcached_string_create(memc
, NULL
, 100);
3027 for (x
= 0; x
< 1024; x
++)
3029 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3030 assert(rc
== MEMCACHED_SUCCESS
);
3032 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3033 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3034 memcached_string_free(string
);
3036 return TEST_SUCCESS
;
3039 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3041 pairs_free(global_pairs
);
3043 return TEST_SUCCESS
;
3046 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3048 unsigned long long x
;
3049 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3050 global_count
= GLOBAL_COUNT
;
3052 for (x
= 0; x
< global_count
; x
++)
3054 global_keys
[x
]= global_pairs
[x
].key
;
3055 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3058 return TEST_SUCCESS
;
3061 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3063 unsigned long long x
;
3064 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3065 global_count
= GLOBAL2_COUNT
;
3067 for (x
= 0; x
< global_count
; x
++)
3069 global_keys
[x
]= global_pairs
[x
].key
;
3070 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3073 return TEST_SUCCESS
;
3076 static test_return_t
generate_data(memcached_st
*memc
)
3078 execute_set(memc
, global_pairs
, global_count
);
3080 return TEST_SUCCESS
;
3083 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3085 memcached_stat_st
*stat_p
;
3086 memcached_return rc
;
3087 uint32_t host_index
= 0;
3088 execute_set(memc
, global_pairs
, global_count
);
3090 //TODO: hosts used size stats
3091 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3094 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3096 /* This test was changes so that "make test" would work properlly */
3098 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, (memc
->hosts
)[host_index
].hostname
, (memc
->hosts
)[host_index
].port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3100 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
3103 memcached_stat_free(NULL
, stat_p
);
3105 return TEST_SUCCESS
;
3107 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3112 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3113 generate_data(memc
);
3115 return TEST_SUCCESS
;
3118 static test_return_t
get_read_count(memcached_st
*memc
)
3121 memcached_return rc
;
3122 memcached_st
*memc_clone
;
3124 memc_clone
= memcached_clone(NULL
, memc
);
3127 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3131 size_t return_value_length
;
3135 for (x
= count
= 0; x
< global_count
; x
++)
3137 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3138 &return_value_length
, &flags
, &rc
);
3139 if (rc
== MEMCACHED_SUCCESS
)
3148 memcached_free(memc_clone
);
3150 return TEST_SUCCESS
;
3153 static test_return_t
get_read(memcached_st
*memc
)
3156 memcached_return rc
;
3160 size_t return_value_length
;
3163 for (x
= 0; x
< global_count
; x
++)
3165 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3166 &return_value_length
, &flags
, &rc
);
3168 assert(return_value);
3169 assert(rc == MEMCACHED_SUCCESS);
3171 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3176 return TEST_SUCCESS
;
3179 static test_return_t
mget_read(memcached_st
*memc
)
3181 memcached_return rc
;
3183 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3184 assert(rc
== MEMCACHED_SUCCESS
);
3185 assert(fetch_all_results(memc
) == TEST_SUCCESS
);
3187 return TEST_SUCCESS
;
3190 static test_return_t
mget_read_result(memcached_st
*memc
)
3192 memcached_return rc
;
3194 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3195 assert(rc
== MEMCACHED_SUCCESS
);
3196 /* Turn this into a help function */
3198 memcached_result_st results_obj
;
3199 memcached_result_st
*results
;
3201 results
= memcached_result_create(memc
, &results_obj
);
3203 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3206 assert(rc
== MEMCACHED_SUCCESS
);
3209 memcached_result_free(&results_obj
);
3212 return TEST_SUCCESS
;
3215 static test_return_t
mget_read_function(memcached_st
*memc
)
3217 memcached_return rc
;
3218 unsigned int counter
;
3219 memcached_execute_function callbacks
[1];
3221 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3222 assert(rc
== MEMCACHED_SUCCESS
);
3224 callbacks
[0]= &callback_counter
;
3226 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3228 return TEST_SUCCESS
;
3231 static test_return_t
delete_generate(memcached_st
*memc
)
3235 for (x
= 0; x
< global_count
; x
++)
3237 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3240 return TEST_SUCCESS
;
3243 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3249 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3251 for (x
= 0; x
< global_count
; x
++)
3253 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3256 return TEST_SUCCESS
;
3259 static test_return_t
add_host_test1(memcached_st
*memc
)
3262 memcached_return rc
;
3263 char servername
[]= "0.example.com";
3264 memcached_server_st
*servers
;
3266 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3268 assert(1 == memcached_server_list_count(servers
));
3270 for (x
= 2; x
< 20; x
++)
3272 char buffer
[SMALL_STRING_LEN
];
3274 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
3275 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3277 assert(rc
== MEMCACHED_SUCCESS
);
3278 assert(x
== memcached_server_list_count(servers
));
3281 rc
= memcached_server_push(memc
, servers
);
3282 assert(rc
== MEMCACHED_SUCCESS
);
3283 rc
= memcached_server_push(memc
, servers
);
3284 assert(rc
== MEMCACHED_SUCCESS
);
3286 memcached_server_list_free(servers
);
3288 return TEST_SUCCESS
;
3291 static memcached_return
pre_nonblock(memcached_st
*memc
)
3293 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3295 return MEMCACHED_SUCCESS
;
3298 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
3300 memcached_return rc
= MEMCACHED_FAILURE
;
3301 memcached_st
*memc_clone
;
3303 memc_clone
= memcached_clone(NULL
, memc
);
3305 // The memcached_version needs to be done on a clone, because the server
3306 // will not toggle protocol on an connection.
3307 memcached_version(memc_clone
);
3309 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3311 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3312 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3313 assert(rc
== MEMCACHED_SUCCESS
);
3314 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3317 memcached_free(memc_clone
);
3321 static memcached_return
pre_murmur(memcached_st
*memc
)
3323 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3325 return MEMCACHED_SUCCESS
;
3328 static memcached_return
pre_jenkins(memcached_st
*memc
)
3330 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3332 return MEMCACHED_SUCCESS
;
3336 static memcached_return
pre_md5(memcached_st
*memc
)
3338 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3340 return MEMCACHED_SUCCESS
;
3343 static memcached_return
pre_crc(memcached_st
*memc
)
3345 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3347 return MEMCACHED_SUCCESS
;
3350 static memcached_return
pre_hsieh(memcached_st
*memc
)
3352 #ifdef HAVE_HSIEH_HASH
3353 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3354 return MEMCACHED_SUCCESS
;
3357 return MEMCACHED_FAILURE
;
3361 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
3363 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
3365 return MEMCACHED_SUCCESS
;
3368 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
3370 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3372 return MEMCACHED_SUCCESS
;
3375 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
3377 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3379 return MEMCACHED_SUCCESS
;
3382 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
3384 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3386 return MEMCACHED_SUCCESS
;
3389 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3391 memcached_return rc
;
3394 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3395 assert(rc
== MEMCACHED_SUCCESS
);
3397 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3400 return MEMCACHED_SUCCESS
;
3403 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3405 memcached_return rc
;
3408 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3409 assert(rc
== MEMCACHED_SUCCESS
);
3411 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3414 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3415 assert(rc
== MEMCACHED_SUCCESS
);
3417 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3418 assert(value
== MEMCACHED_HASH_MD5
);
3419 return MEMCACHED_SUCCESS
;
3422 static memcached_return
pre_binary(memcached_st
*memc
)
3424 memcached_return rc
= MEMCACHED_FAILURE
;
3425 memcached_st
*memc_clone
;
3427 memc_clone
= memcached_clone(NULL
, memc
);
3429 // The memcached_version needs to be done on a clone, because the server
3430 // will not toggle protocol on an connection.
3431 memcached_version(memc_clone
);
3433 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3435 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3436 assert(rc
== MEMCACHED_SUCCESS
);
3437 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3440 memcached_free(memc_clone
);
3445 static memcached_return
pre_replication(memcached_st
*memc
)
3447 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
3448 return MEMCACHED_FAILURE
;
3451 * Make sure that we store the item on all servers
3452 * (master + replicas == number of servers)
3454 memcached_return rc
;
3455 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3456 memc
->number_of_hosts
- 1);
3457 assert(rc
== MEMCACHED_SUCCESS
);
3458 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3463 static memcached_return
pre_replication_noblock(memcached_st
*memc
)
3465 memcached_return rc
= MEMCACHED_FAILURE
;
3466 if (pre_replication(memc
) == MEMCACHED_SUCCESS
&&
3467 pre_nonblock(memc
) == MEMCACHED_SUCCESS
)
3468 rc
= MEMCACHED_SUCCESS
;
3473 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3478 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3480 void *ret
= malloc(size
);
3482 memset(ret
, 0xff, size
);
3487 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3489 return realloc(mem
, size
);
3492 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3494 return calloc(nelem
, size
);
3497 static memcached_return
set_prefix(memcached_st
*memc
)
3499 memcached_return rc
;
3500 const char *key
= "mine";
3503 /* Make sure be default none exists */
3504 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3505 assert(rc
== MEMCACHED_FAILURE
);
3507 /* Test a clean set */
3508 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3509 assert(rc
== MEMCACHED_SUCCESS
);
3511 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3512 assert(memcmp(value
, key
, 4) == 0);
3513 assert(rc
== MEMCACHED_SUCCESS
);
3515 /* Test that we can turn it off */
3516 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3517 assert(rc
== MEMCACHED_SUCCESS
);
3519 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3520 assert(rc
== MEMCACHED_FAILURE
);
3522 /* Now setup for main test */
3523 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3524 assert(rc
== MEMCACHED_SUCCESS
);
3526 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3527 assert(rc
== MEMCACHED_SUCCESS
);
3528 assert(memcmp(value
, key
, 4) == 0);
3530 /* Set to Zero, and then Set to something too large */
3533 memset(long_key
, 0, 255);
3535 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3536 assert(rc
== MEMCACHED_SUCCESS
);
3538 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3539 assert(rc
== MEMCACHED_FAILURE
);
3540 assert(value
== NULL
);
3542 /* Test a long key for failure */
3543 /* TODO, extend test to determine based on setting, what result should be */
3544 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3545 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3546 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3547 assert(rc
== MEMCACHED_SUCCESS
);
3549 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3550 strcpy(long_key
, "This is more then the allotted number of characters");
3551 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3552 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3554 /* Test for a bad prefix, but with a short key */
3555 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3556 assert(rc
== MEMCACHED_SUCCESS
);
3558 strcpy(long_key
, "dog cat");
3559 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3560 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3563 return MEMCACHED_SUCCESS
;
3566 #ifdef MEMCACHED_ENABLE_DEPRECATED
3567 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3569 void *test_ptr
= NULL
;
3572 memcached_malloc_function malloc_cb
=
3573 (memcached_malloc_function
)my_malloc
;
3574 cb_ptr
= *(void **)&malloc_cb
;
3575 memcached_return rc
;
3577 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3578 assert(rc
== MEMCACHED_SUCCESS
);
3579 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3580 assert(rc
== MEMCACHED_SUCCESS
);
3581 assert(test_ptr
== cb_ptr
);
3585 memcached_realloc_function realloc_cb
=
3586 (memcached_realloc_function
)my_realloc
;
3587 cb_ptr
= *(void **)&realloc_cb
;
3588 memcached_return rc
;
3590 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3591 assert(rc
== MEMCACHED_SUCCESS
);
3592 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3593 assert(rc
== MEMCACHED_SUCCESS
);
3594 assert(test_ptr
== cb_ptr
);
3598 memcached_free_function free_cb
=
3599 (memcached_free_function
)my_free
;
3600 cb_ptr
= *(void **)&free_cb
;
3601 memcached_return rc
;
3603 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3604 assert(rc
== MEMCACHED_SUCCESS
);
3605 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3606 assert(rc
== MEMCACHED_SUCCESS
);
3607 assert(test_ptr
== cb_ptr
);
3609 return MEMCACHED_SUCCESS
;
3613 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3615 memcached_return rc
;
3616 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3617 my_realloc
, my_calloc
);
3618 assert(rc
== MEMCACHED_FAILURE
);
3620 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3621 my_realloc
, my_calloc
);
3623 memcached_malloc_function mem_malloc
;
3624 memcached_free_function mem_free
;
3625 memcached_realloc_function mem_realloc
;
3626 memcached_calloc_function mem_calloc
;
3627 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3628 &mem_realloc
, &mem_calloc
);
3630 assert(mem_malloc
== my_malloc
);
3631 assert(mem_realloc
== my_realloc
);
3632 assert(mem_calloc
== my_calloc
);
3633 assert(mem_free
== my_free
);
3635 return MEMCACHED_SUCCESS
;
3638 static memcached_return
enable_consistent(memcached_st
*memc
)
3640 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3641 memcached_hash hash
;
3642 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3643 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3644 return MEMCACHED_FAILURE
;
3646 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3647 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3649 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3650 assert(hash
== MEMCACHED_HASH_HSIEH
);
3653 return MEMCACHED_SUCCESS
;
3656 static memcached_return
enable_cas(memcached_st
*memc
)
3658 unsigned int set
= 1;
3660 memcached_version(memc
);
3662 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3663 || memc
->hosts
[0].minor_version
> 2)
3665 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3667 return MEMCACHED_SUCCESS
;
3670 return MEMCACHED_FAILURE
;
3673 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3675 memcached_version(memc
);
3677 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3678 || memc
->hosts
[0].minor_version
> 2)
3679 return MEMCACHED_SUCCESS
;
3681 return MEMCACHED_FAILURE
;
3684 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3686 memcached_return rc
;
3689 memcached_server_list_free(memc
->hosts
);
3691 memc
->number_of_hosts
= 0;
3693 if (stat("/tmp/memcached.socket", &buf
))
3694 return MEMCACHED_FAILURE
;
3696 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3701 static memcached_return
pre_nodelay(memcached_st
*memc
)
3703 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3704 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3706 return MEMCACHED_SUCCESS
;
3709 static memcached_return
pre_settimer(memcached_st
*memc
)
3711 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3712 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3714 return MEMCACHED_SUCCESS
;
3717 static memcached_return
poll_timeout(memcached_st
*memc
)
3723 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3725 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3727 assert(timeout
== 100);
3729 return MEMCACHED_SUCCESS
;
3732 static test_return_t
noreply_test(memcached_st
*memc
)
3734 memcached_return ret
;
3735 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3736 assert(ret
== MEMCACHED_SUCCESS
);
3737 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3738 assert(ret
== MEMCACHED_SUCCESS
);
3739 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3740 assert(ret
== MEMCACHED_SUCCESS
);
3741 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3742 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3743 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3745 for (int count
=0; count
< 5; ++count
)
3747 for (int x
=0; x
< 100; ++x
)
3750 size_t len
= (size_t)sprintf(key
, "%d", x
);
3754 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3757 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3760 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3763 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3766 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3772 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3776 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3777 ** API and is _ONLY_ done this way to verify that the library works the
3778 ** way it is supposed to do!!!!
3781 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3782 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3784 assert(no_msg
== 0);
3785 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3788 ** Now validate that all items was set properly!
3790 for (int x
=0; x
< 100; ++x
)
3793 size_t len
= (size_t)sprintf(key
, "%d", x
);
3796 char* value
=memcached_get(memc
, key
, strlen(key
),
3797 &length
, &flags
, &ret
);
3798 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3801 case 0: /* FALLTHROUGH */
3802 case 1: /* FALLTHROUGH */
3804 assert(strncmp(value
, key
, len
) == 0);
3805 assert(len
== length
);
3808 assert(length
== len
* 2);
3811 assert(length
== len
* 3);
3821 /* Try setting an illegal cas value (should not return an error to
3822 * the caller (because we don't expect a return message from the server)
3824 const char* keys
[]= {"0"};
3825 size_t lengths
[]= {1};
3828 memcached_result_st results_obj
;
3829 memcached_result_st
*results
;
3830 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3831 assert(ret
== MEMCACHED_SUCCESS
);
3833 results
= memcached_result_create(memc
, &results_obj
);
3835 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3837 assert(ret
== MEMCACHED_SUCCESS
);
3838 uint64_t cas
= memcached_result_cas(results
);
3839 memcached_result_free(&results_obj
);
3841 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3842 assert(ret
== MEMCACHED_SUCCESS
);
3845 * The item will have a new cas value, so try to set it again with the old
3846 * value. This should fail!
3848 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3849 assert(ret
== MEMCACHED_SUCCESS
);
3850 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3851 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3852 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3855 return TEST_SUCCESS
;
3858 static test_return_t
analyzer_test(memcached_st
*memc
)
3860 memcached_return rc
;
3861 memcached_stat_st
*memc_stat
;
3862 memcached_analysis_st
*report
;
3864 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3865 assert(rc
== MEMCACHED_SUCCESS
);
3868 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3869 assert(rc
== MEMCACHED_SUCCESS
);
3873 memcached_stat_free(NULL
, memc_stat
);
3875 return TEST_SUCCESS
;
3878 /* Count the objects */
3879 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3880 const char *key
__attribute__((unused
)),
3881 size_t key_length
__attribute__((unused
)),
3884 uint32_t *counter
= (uint32_t *)context
;
3886 *counter
= *counter
+ 1;
3888 return MEMCACHED_SUCCESS
;
3891 static test_return_t
dump_test(memcached_st
*memc
)
3893 memcached_return rc
;
3894 uint32_t counter
= 0;
3895 memcached_dump_func callbacks
[1];
3896 test_return_t main_rc
;
3898 callbacks
[0]= &callback_dump_counter
;
3900 /* No support for Binary protocol yet */
3901 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3902 return TEST_SUCCESS
;
3904 main_rc
= set_test3(memc
);
3906 assert (main_rc
== TEST_SUCCESS
);
3908 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3909 assert(rc
== MEMCACHED_SUCCESS
);
3911 /* We may have more then 32 if our previous flush has not completed */
3912 assert(counter
>= 32);
3914 return TEST_SUCCESS
;
3917 #ifdef HAVE_LIBMEMCACHEDUTIL
3918 static void* connection_release(void *arg
) {
3920 memcached_pool_st
* pool
;
3925 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3929 static test_return_t
connection_pool_test(memcached_st
*memc
)
3931 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3932 assert(pool
!= NULL
);
3933 memcached_st
* mmc
[10];
3934 memcached_return rc
;
3936 for (int x
= 0; x
< 10; ++x
) {
3937 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3938 assert(mmc
[x
] != NULL
);
3939 assert(rc
== MEMCACHED_SUCCESS
);
3942 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3943 assert(rc
== MEMCACHED_SUCCESS
);
3947 memcached_pool_st
* pool
;
3949 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3950 pthread_create(&tid
, NULL
, connection_release
, &item
);
3951 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3952 assert(rc
== MEMCACHED_SUCCESS
);
3953 pthread_join(tid
, NULL
);
3954 assert(mmc
[9] == item
.mmc
);
3955 const char *key
= "key";
3956 size_t keylen
= strlen(key
);
3958 // verify that I can do ops with all connections
3959 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3960 assert(rc
== MEMCACHED_SUCCESS
);
3962 for (unsigned int x
= 0; x
< 10; ++x
) {
3963 uint64_t number_value
;
3964 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3965 assert(rc
== MEMCACHED_SUCCESS
);
3966 assert(number_value
== (x
+1));
3970 for (int x
= 0; x
< 10; ++x
)
3971 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3974 /* verify that I can set behaviors on the pool when I don't have all
3975 * of the connections in the pool. It should however be enabled
3976 * when I push the item into the pool
3978 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
3979 assert(mmc
[0] != NULL
);
3981 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
3982 assert(rc
== MEMCACHED_SUCCESS
);
3984 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
3985 assert(mmc
[1] != NULL
);
3987 assert(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
3988 assert(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
3989 assert(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
3991 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
3992 assert(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
3993 assert(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
3996 assert(memcached_pool_destroy(pool
) == memc
);
3997 return TEST_SUCCESS
;
4001 static test_return_t
replication_set_test(memcached_st
*memc
)
4003 memcached_return rc
;
4004 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4005 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4007 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4008 assert(rc
== MEMCACHED_SUCCESS
);
4011 ** We are using the quiet commands to store the replicas, so we need
4012 ** to ensure that all of them are processed before we can continue.
4013 ** In the test we go directly from storing the object to trying to
4014 ** receive the object from all of the different servers, so we
4015 ** could end up in a race condition (the memcached server hasn't yet
4016 ** processed the quiet command from the replication set when it process
4017 ** the request from the other client (created by the clone)). As a
4018 ** workaround for that we call memcached_quit to send the quit command
4019 ** to the server and wait for the response ;-) If you use the test code
4020 ** as an example for your own code, please note that you shouldn't need
4023 memcached_quit(memc
);
4026 ** "bubba" should now be stored on all of our servers. We don't have an
4027 ** easy to use API to address each individual server, so I'll just iterate
4028 ** through a bunch of "master keys" and I should most likely hit all of the
4031 for (int x
= 'a'; x
<= 'z'; ++x
)
4033 char key
[2]= { [0]= (char)x
};
4036 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4038 assert(rc
== MEMCACHED_SUCCESS
);
4039 assert(val
!= NULL
);
4043 memcached_free(memc_clone
);
4045 return TEST_SUCCESS
;
4048 static test_return_t
replication_get_test(memcached_st
*memc
)
4050 memcached_return rc
;
4053 * Don't do the following in your code. I am abusing the internal details
4054 * within the library, and this is not a supported interface.
4055 * This is to verify correct behavior in the library
4057 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
4059 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4060 memc_clone
->hosts
[host
].port
= 0;
4062 for (int x
= 'a'; x
<= 'z'; ++x
)
4064 char key
[2]= { [0]= (char)x
};
4067 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4069 assert(rc
== MEMCACHED_SUCCESS
);
4070 assert(val
!= NULL
);
4074 memcached_free(memc_clone
);
4077 return TEST_SUCCESS
;
4080 static test_return_t
replication_mget_test(memcached_st
*memc
)
4082 memcached_return rc
;
4083 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4084 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4086 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4087 size_t len
[]= { 5, 4, 4, 4 };
4089 for (int x
=0; x
< 4; ++x
)
4091 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4092 assert(rc
== MEMCACHED_SUCCESS
);
4096 ** We are using the quiet commands to store the replicas, so we need
4097 ** to ensure that all of them are processed before we can continue.
4098 ** In the test we go directly from storing the object to trying to
4099 ** receive the object from all of the different servers, so we
4100 ** could end up in a race condition (the memcached server hasn't yet
4101 ** processed the quiet command from the replication set when it process
4102 ** the request from the other client (created by the clone)). As a
4103 ** workaround for that we call memcached_quit to send the quit command
4104 ** to the server and wait for the response ;-) If you use the test code
4105 ** as an example for your own code, please note that you shouldn't need
4108 memcached_quit(memc
);
4111 * Don't do the following in your code. I am abusing the internal details
4112 * within the library, and this is not a supported interface.
4113 * This is to verify correct behavior in the library
4115 memcached_result_st result_obj
;
4116 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4118 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4119 new_clone
->hosts
[host
].port
= 0;
4121 for (int x
= 'a'; x
<= 'z'; ++x
)
4123 const char key
[2]= { [0]= (const char)x
};
4125 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4126 assert(rc
== MEMCACHED_SUCCESS
);
4128 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4132 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4137 memcached_result_free(&result_obj
);
4140 memcached_free(new_clone
);
4143 memcached_free(memc_clone
);
4145 return TEST_SUCCESS
;
4148 static test_return_t
replication_delete_test(memcached_st
*memc
)
4150 memcached_return rc
;
4151 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4152 /* Delete the items from all of the servers except 1 */
4153 uint64_t repl
= memcached_behavior_get(memc
,
4154 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4155 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4157 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4158 size_t len
[]= { 5, 4, 4, 4 };
4160 for (int x
=0; x
< 4; ++x
)
4162 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4163 assert(rc
== MEMCACHED_SUCCESS
);
4167 * Don't do the following in your code. I am abusing the internal details
4168 * within the library, and this is not a supported interface.
4169 * This is to verify correct behavior in the library
4171 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4172 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4174 memc_clone
->hosts
[hash
].port
= 0;
4175 if (++hash
== memc_clone
->number_of_hosts
)
4179 memcached_result_st result_obj
;
4180 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4182 for (int x
= 'a'; x
<= 'z'; ++x
)
4184 const char key
[2]= { [0]= (const char)x
};
4186 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4187 assert(rc
== MEMCACHED_SUCCESS
);
4189 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4193 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4198 memcached_result_free(&result_obj
);
4201 memcached_free(memc_clone
);
4203 return TEST_SUCCESS
;
4206 static void increment_request_id(uint16_t *id
)
4209 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
4213 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
4215 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
4216 assert(ids
!= NULL
);
4219 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4220 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
4225 static test_return_t
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
4228 memcached_server_st
*cur_server
= memc
->hosts
;
4229 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
4231 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4233 assert(cur_server
[x
].cursor_active
== 0);
4234 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
4236 free(expected_req_ids
);
4239 return TEST_SUCCESS
;
4243 ** There is a little bit of a hack here, instead of removing
4244 ** the servers, I just set num host to 0 and them add then new udp servers
4246 static memcached_return
init_udp(memcached_st
*memc
)
4248 memcached_version(memc
);
4249 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
4250 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
4251 || memc
->hosts
[0].micro_version
< 6)
4252 return MEMCACHED_FAILURE
;
4254 uint32_t num_hosts
= memc
->number_of_hosts
;
4256 memcached_server_st servers
[num_hosts
];
4257 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
4258 for (x
= 0; x
< num_hosts
; x
++)
4259 memcached_server_free(&memc
->hosts
[x
]);
4261 memc
->number_of_hosts
= 0;
4262 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
4263 for (x
= 0; x
< num_hosts
; x
++)
4265 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
4266 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4269 return MEMCACHED_SUCCESS
;
4272 static memcached_return
binary_init_udp(memcached_st
*memc
)
4275 return init_udp(memc
);
4278 /* Make sure that I cant add a tcp server to a udp client */
4279 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
4281 memcached_server_st server
;
4282 memcached_server_clone(&server
, &memc
->hosts
[0]);
4283 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4284 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4285 return TEST_SUCCESS
;
4288 /* Make sure that I cant add a udp server to a tcp client */
4289 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
4291 memcached_server_st server
;
4292 memcached_server_clone(&server
, &memc
->hosts
[0]);
4293 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4295 memcached_st tcp_client
;
4296 memcached_create(&tcp_client
);
4297 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4298 return TEST_SUCCESS
;
4301 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
4304 memcached_quit(memc
);
4305 memc
->number_of_hosts
= 0;
4306 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
);
4307 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
4308 assert(memc
->flags
& MEM_USE_UDP
);
4309 assert(memc
->flags
& MEM_NOREPLY
);;
4311 assert(memc
->number_of_hosts
== 0);
4313 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
4314 assert(!(memc
->flags
& MEM_USE_UDP
));
4315 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
4316 assert(!(memc
->flags
& MEM_NOREPLY
));
4317 return TEST_SUCCESS
;
4320 static test_return_t
udp_set_test(memcached_st
*memc
)
4323 unsigned int num_iters
= 1025; //request id rolls over at 1024
4324 for (x
= 0; x
< num_iters
;x
++)
4326 memcached_return rc
;
4327 const char *key
= "foo";
4328 const char *value
= "when we sanitize";
4329 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4330 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
4331 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4332 rc
= memcached_set(memc
, key
, strlen(key
),
4333 value
, strlen(value
),
4334 (time_t)0, (uint32_t)0);
4335 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4336 /** NB, the check below assumes that if new write_ptr is less than
4337 * the original write_ptr that we have flushed. For large payloads, this
4338 * maybe an invalid assumption, but for the small payload we have it is OK
4340 if (rc
== MEMCACHED_SUCCESS
||
4341 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4342 increment_request_id(&expected_ids
[server_key
]);
4344 if (rc
== MEMCACHED_SUCCESS
)
4346 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4350 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4351 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4353 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4355 return TEST_SUCCESS
;
4358 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
4360 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4361 return udp_set_test(memc
);
4364 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
4366 memcached_return rc
;
4367 const char *key
= "bar";
4368 char value
[MAX_UDP_DATAGRAM_LENGTH
];
4369 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4370 rc
= memcached_set(memc
, key
, strlen(key
),
4371 value
, MAX_UDP_DATAGRAM_LENGTH
,
4372 (time_t)0, (uint32_t)0);
4373 assert(rc
== MEMCACHED_WRITE_FAILURE
);
4374 return post_udp_op_check(memc
,expected_ids
);
4377 static test_return_t
udp_delete_test(memcached_st
*memc
)
4380 unsigned int num_iters
= 1025; //request id rolls over at 1024
4381 for (x
= 0; x
< num_iters
;x
++)
4383 memcached_return rc
;
4384 const char *key
= "foo";
4385 uint16_t *expected_ids
=get_udp_request_ids(memc
);
4386 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4387 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4388 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
4389 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4390 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4391 increment_request_id(&expected_ids
[server_key
]);
4392 if (rc
== MEMCACHED_SUCCESS
)
4393 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4396 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4397 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4399 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4401 return TEST_SUCCESS
;
4404 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
4406 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4407 return udp_delete_test(memc
);
4410 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
4412 memcached_return rc
;
4413 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4415 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4416 increment_request_id(&expected_ids
[x
]);
4418 rc
= memcached_verbosity(memc
,3);
4419 assert(rc
== MEMCACHED_SUCCESS
);
4420 return post_udp_op_check(memc
,expected_ids
);
4423 static test_return_t
udp_quit_test(memcached_st
*memc
)
4425 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4426 memcached_quit(memc
);
4427 return post_udp_op_check(memc
, expected_ids
);
4430 static test_return_t
udp_flush_test(memcached_st
*memc
)
4432 memcached_return rc
;
4433 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4435 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4436 increment_request_id(&expected_ids
[x
]);
4438 rc
= memcached_flush(memc
,0);
4439 assert(rc
== MEMCACHED_SUCCESS
);
4440 return post_udp_op_check(memc
,expected_ids
);
4443 static test_return_t
udp_incr_test(memcached_st
*memc
)
4445 memcached_return rc
;
4446 const char *key
= "incr";
4447 const char *value
= "1";
4448 rc
= memcached_set(memc
, key
, strlen(key
),
4449 value
, strlen(value
),
4450 (time_t)0, (uint32_t)0);
4452 assert(rc
== MEMCACHED_SUCCESS
);
4453 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4454 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4455 increment_request_id(&expected_ids
[server_key
]);
4457 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4458 assert(rc
== MEMCACHED_SUCCESS
);
4459 return post_udp_op_check(memc
, expected_ids
);
4462 static test_return_t
udp_decr_test(memcached_st
*memc
)
4464 memcached_return rc
;
4465 const char *key
= "decr";
4466 const char *value
= "1";
4467 rc
= memcached_set(memc
, key
, strlen(key
),
4468 value
, strlen(value
),
4469 (time_t)0, (uint32_t)0);
4471 assert(rc
== MEMCACHED_SUCCESS
);
4472 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4473 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4474 increment_request_id(&expected_ids
[server_key
]);
4476 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4477 assert(rc
== MEMCACHED_SUCCESS
);
4478 return post_udp_op_check(memc
, expected_ids
);
4482 static test_return_t
udp_stat_test(memcached_st
*memc
)
4484 memcached_stat_st
* rv
= NULL
;
4485 memcached_return rc
;
4487 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4488 rv
= memcached_stat(memc
, args
, &rc
);
4490 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4491 return post_udp_op_check(memc
, expected_ids
);
4494 static test_return_t
udp_version_test(memcached_st
*memc
)
4496 memcached_return rc
;
4497 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4498 rc
= memcached_version(memc
);
4499 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4500 return post_udp_op_check(memc
, expected_ids
);
4503 static test_return_t
udp_get_test(memcached_st
*memc
)
4505 memcached_return rc
;
4506 const char *key
= "foo";
4508 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4509 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4510 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4511 assert(val
== NULL
);
4512 return post_udp_op_check(memc
, expected_ids
);
4515 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
4518 test_st mixed_io_ops
[] ={
4519 {"udp_set_test", 0, udp_set_test
},
4520 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4521 {"udp_delete_test", 0, udp_delete_test
},
4522 {"udp_verbosity_test", 0, udp_verbosity_test
},
4523 {"udp_quit_test", 0, udp_quit_test
},
4524 {"udp_flush_test", 0, udp_flush_test
},
4525 {"udp_incr_test", 0, udp_incr_test
},
4526 {"udp_decr_test", 0, udp_decr_test
},
4527 {"udp_version_test", 0, udp_version_test
}
4530 for (x
= 0; x
< 500; x
++)
4532 current_op
= mixed_io_ops
[random() % 9];
4533 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4535 return TEST_SUCCESS
;
4538 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4540 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4541 #ifdef HAVE_HSIEH_HASH
4542 expected_rc
= MEMCACHED_SUCCESS
;
4544 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4545 (uint64_t)MEMCACHED_HASH_HSIEH
);
4546 assert(rc
== expected_rc
);
4547 return TEST_SUCCESS
;
4550 static const char *list
[]=
4580 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4584 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4585 245758794U, 2550894432U, 121710495U, 3053817768U,
4586 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4587 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4588 2138080750U, 217675895U, 2230934345U, 1234361223U,
4589 3968582726U, 2455685270U, 1293568479U, 199067604U,
4593 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4597 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4598 assert(values
[x
] == hash_val
);
4601 return TEST_SUCCESS
;
4604 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4608 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4609 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4610 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4611 17300U, 28156U, 9192U, 20484U, 16925U };
4613 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4617 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4618 assert(values
[x
] == hash_val
);
4621 return TEST_SUCCESS
;
4624 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4628 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4629 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4630 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4631 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4632 2815549194U, 2562818319U, 224996066U, 2680194749U,
4633 3035305390U, 246890365U, 2395624193U, 4145193337U,
4636 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4640 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4641 assert(values
[x
] == hash_val
);
4644 return TEST_SUCCESS
;
4647 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4651 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4652 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4653 734810122U, 1516407546U, 560948863U, 1767346780U,
4654 561034892U, 4156330026U, 3716417003U, 3475297030U,
4655 1518272172U, 227211583U, 3938128828U, 126112909U,
4656 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4659 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4663 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4664 assert(values
[x
] == hash_val
);
4667 return TEST_SUCCESS
;
4670 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4674 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4675 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4676 344481048U, 3865235296U, 2181839183U, 119581266U,
4677 510234242U, 4248244304U, 1362796839U, 103389328U,
4678 1449620010U, 182962511U, 3554262370U, 3206747549U,
4679 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4683 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4687 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4688 assert(values
[x
] == hash_val
);
4691 return TEST_SUCCESS
;
4694 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4698 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4699 459261019U, 3521085446U, 18738364U, 1625305005U,
4700 2162232970U, 777243802U, 3323728671U, 132336572U,
4701 3654473228U, 260679466U, 1169454059U, 2698319462U,
4702 1062177260U, 235516991U, 2218399068U, 405302637U,
4703 1128467232U, 3579622413U, 2138539289U, 96429129U,
4706 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4710 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4711 assert(values
[x
] == hash_val
);
4714 return TEST_SUCCESS
;
4717 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4721 #ifdef HAVE_HSIEH_HASH
4722 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4723 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4724 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4725 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4728 uint32_t values
[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
4731 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4735 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4736 assert(values
[x
] == hash_val
);
4739 return TEST_SUCCESS
;
4742 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4746 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4747 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4748 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4749 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4750 2815549194U, 2562818319U, 224996066U, 2680194749U,
4751 3035305390U, 246890365U, 2395624193U, 4145193337U,
4754 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4758 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4759 assert(values
[x
] == hash_val
);
4762 return TEST_SUCCESS
;
4765 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4769 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4770 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4771 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4772 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4773 4153263658U, 3824871822U, 3072759809U, 798622255U,
4774 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4778 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4782 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4783 assert(values
[x
] == hash_val
);
4786 return TEST_SUCCESS
;
4790 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4792 memcached_return rc
;
4795 memcached_server_st
*server_pool
;
4800 memc
= memcached_create(NULL
);
4803 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4804 assert(rc
== MEMCACHED_SUCCESS
);
4806 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4809 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
,
4810 MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED
) == MEMCACHED_SUCCESS
);
4812 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
) ==
4813 MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED
);
4815 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");
4816 memcached_server_push(memc
, server_pool
);
4818 /* verify that the server list was parsed okay. */
4819 assert(memc
->number_of_hosts
== 8);
4820 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
4821 assert(server_pool
[0].port
== 11211);
4822 assert(server_pool
[0].weight
== 600);
4823 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
4824 assert(server_pool
[2].port
== 11211);
4825 assert(server_pool
[2].weight
== 200);
4826 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
4827 assert(server_pool
[7].port
== 11211);
4828 assert(server_pool
[7].weight
== 100);
4830 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4831 * us test the boundary wraparound.
4833 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4835 /* verify the standard ketama set. */
4836 for (x
= 0; x
< 99; x
++)
4838 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4839 char *hostname
= memc
->hosts
[server_idx
].hostname
;
4840 assert(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
4843 memcached_server_list_free(server_pool
);
4844 memcached_free(memc
);
4846 return TEST_SUCCESS
;
4849 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4851 memcached_return rc
;
4854 memcached_server_st
*server_pool
;
4859 memc
= memcached_create(NULL
);
4862 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4863 assert(rc
== MEMCACHED_SUCCESS
);
4865 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4868 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
,
4869 MEMCACHED_KETAMA_COMPAT_SPY
) == MEMCACHED_SUCCESS
);
4871 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
) ==
4872 MEMCACHED_KETAMA_COMPAT_SPY
);
4874 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");
4875 memcached_server_push(memc
, server_pool
);
4877 /* verify that the server list was parsed okay. */
4878 assert(memc
->number_of_hosts
== 8);
4879 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
4880 assert(server_pool
[0].port
== 11211);
4881 assert(server_pool
[0].weight
== 600);
4882 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
4883 assert(server_pool
[2].port
== 11211);
4884 assert(server_pool
[2].weight
== 200);
4885 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
4886 assert(server_pool
[7].port
== 11211);
4887 assert(server_pool
[7].weight
== 100);
4889 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4890 * us test the boundary wraparound.
4892 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4894 /* verify the standard ketama set. */
4895 for (x
= 0; x
< 99; x
++)
4897 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4898 char *hostname
= memc
->hosts
[server_idx
].hostname
;
4899 assert(strcmp(hostname
, ketama_test_cases_spy
[x
].server
) == 0);
4902 memcached_server_list_free(server_pool
);
4903 memcached_free(memc
);
4905 return TEST_SUCCESS
;
4908 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4910 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
4911 return TEST_SKIPPED
;
4913 memcached_return ret
;
4914 const char *key
= "regression_bug_434484";
4915 size_t keylen
= strlen(key
);
4917 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4918 assert(ret
== MEMCACHED_NOTSTORED
);
4920 size_t size
= 2048 * 1024;
4921 void *data
= calloc(1, size
);
4922 assert(data
!= NULL
);
4923 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
4924 assert(ret
== MEMCACHED_E2BIG
);
4927 return TEST_SUCCESS
;
4930 static test_return_t
regression_bug_434843(memcached_st
*memc
)
4932 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
4933 return TEST_SKIPPED
;
4935 memcached_return rc
;
4936 unsigned int counter
= 0;
4937 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
4940 * I only want to hit only _one_ server so I know the number of requests I'm
4941 * sending in the pipleine to the server. Let's try to do a multiget of
4942 * 1024 (that should satisfy most users don't you think?). Future versions
4943 * will include a mget_execute function call if you need a higher number.
4945 uint32_t number_of_hosts
= memc
->number_of_hosts
;
4946 memc
->number_of_hosts
= 1;
4947 const size_t max_keys
= 1024;
4948 char **keys
= calloc(max_keys
, sizeof(char*));
4949 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
4951 for (int x
= 0; x
< (int)max_keys
; ++x
)
4954 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
4956 assert(keys
[x
] != NULL
);
4960 * Run two times.. the first time we should have 100% cache miss,
4961 * and the second time we should have 100% cache hits
4963 for (int y
= 0; y
< 2; ++y
)
4965 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
4966 assert(rc
== MEMCACHED_SUCCESS
);
4967 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
4970 /* The first iteration should give me a 100% cache miss. verify that*/
4971 assert(counter
== 0);
4972 char blob
[1024]= { 0 };
4973 for (int x
= 0; x
< (int)max_keys
; ++x
)
4975 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
4976 blob
, sizeof(blob
), 0, 0);
4977 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4982 /* Verify that we received all of the key/value pairs */
4983 assert(counter
== (unsigned int)max_keys
);
4987 /* Release allocated resources */
4988 for (size_t x
= 0; x
< max_keys
; ++x
)
4993 memc
->number_of_hosts
= number_of_hosts
;
4994 return TEST_SUCCESS
;
4997 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
4999 memcached_return rc
;
5000 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5001 assert(rc
== MEMCACHED_SUCCESS
);
5003 return regression_bug_434843(memc
);
5006 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5008 memcached_return rc
;
5009 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5010 assert(rc
== MEMCACHED_SUCCESS
);
5012 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5013 assert(rc
== MEMCACHED_SUCCESS
);
5014 assert(bytes
!= NULL
);
5015 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5017 assert(rc
== MEMCACHED_SUCCESS
);
5018 assert(bytes_read
!= NULL
);
5020 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5021 "bytes_written", &rc
);
5022 assert(rc
== MEMCACHED_SUCCESS
);
5023 assert(bytes_written
!= NULL
);
5025 assert(strcmp(bytes
, bytes_read
) != 0);
5026 assert(strcmp(bytes
, bytes_written
) != 0);
5028 /* Release allocated resources */
5031 free(bytes_written
);
5032 memcached_stat_free(NULL
, memc_stat
);
5033 return TEST_SUCCESS
;
5037 * The test case isn't obvious so I should probably document why
5038 * it works the way it does. Bug 442914 was caused by a bug
5039 * in the logic in memcached_purge (it did not handle the case
5040 * where the number of bytes sent was equal to the watermark).
5041 * In this test case, create messages so that we hit that case
5042 * and then disable noreply mode and issue a new command to
5043 * verify that it isn't stuck. If we change the format for the
5044 * delete command or the watermarks, we need to update this
5047 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5049 memcached_return rc
;
5050 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5051 assert(rc
== MEMCACHED_SUCCESS
);
5052 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5054 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5055 memc
->number_of_hosts
= 1;
5060 for (int x
= 0; x
< 250; ++x
)
5062 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5063 rc
= memcached_delete(memc
, k
, len
, 0);
5064 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5067 len
= (size_t)snprintf(k
, sizeof(k
), "%037u", 251);
5068 rc
= memcached_delete(memc
, k
, len
, 0);
5069 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5071 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5072 assert(rc
== MEMCACHED_SUCCESS
);
5073 rc
= memcached_delete(memc
, k
, len
, 0);
5074 assert(rc
== MEMCACHED_NOTFOUND
);
5076 memc
->number_of_hosts
= number_of_hosts
;
5078 return TEST_SUCCESS
;
5081 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5083 if (memc
->number_of_hosts
< 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5084 return TEST_SKIPPED
;
5086 memcached_return rc
;
5088 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5089 assert(rc
== MEMCACHED_SUCCESS
);
5091 const size_t max_keys
= 100;
5092 char **keys
= calloc(max_keys
, sizeof(char*));
5093 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5095 for (int x
= 0; x
< (int)max_keys
; ++x
)
5098 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
5100 assert(keys
[x
] != NULL
);
5101 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5102 assert(rc
== MEMCACHED_SUCCESS
);
5106 ** We are using the quiet commands to store the replicas, so we need
5107 ** to ensure that all of them are processed before we can continue.
5108 ** In the test we go directly from storing the object to trying to
5109 ** receive the object from all of the different servers, so we
5110 ** could end up in a race condition (the memcached server hasn't yet
5111 ** processed the quiet command from the replication set when it process
5112 ** the request from the other client (created by the clone)). As a
5113 ** workaround for that we call memcached_quit to send the quit command
5114 ** to the server and wait for the response ;-) If you use the test code
5115 ** as an example for your own code, please note that you shouldn't need
5118 memcached_quit(memc
);
5120 /* Verify that all messages are stored, and we didn't stuff too much
5123 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5124 assert(rc
== MEMCACHED_SUCCESS
);
5126 unsigned int counter
= 0;
5127 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5128 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5129 /* Verify that we received all of the key/value pairs */
5130 assert(counter
== (unsigned int)max_keys
);
5132 memcached_quit(memc
);
5134 * Don't do the following in your code. I am abusing the internal details
5135 * within the library, and this is not a supported interface.
5136 * This is to verify correct behavior in the library. Fake that two servers
5139 unsigned int port0
= memc
->hosts
[0].port
;
5140 unsigned int port2
= memc
->hosts
[2].port
;
5141 memc
->hosts
[0].port
= 0;
5142 memc
->hosts
[2].port
= 0;
5144 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5145 assert(rc
== MEMCACHED_SUCCESS
);
5148 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5149 assert(counter
== (unsigned int)max_keys
);
5151 /* restore the memc handle */
5152 memc
->hosts
[0].port
= port0
;
5153 memc
->hosts
[2].port
= port2
;
5155 memcached_quit(memc
);
5157 /* Remove half of the objects */
5158 for (int x
= 0; x
< (int)max_keys
; ++x
)
5161 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5162 assert(rc
== MEMCACHED_SUCCESS
);
5165 memcached_quit(memc
);
5166 memc
->hosts
[0].port
= 0;
5167 memc
->hosts
[2].port
= 0;
5169 /* now retry the command, this time we should have cache misses */
5170 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5171 assert(rc
== MEMCACHED_SUCCESS
);
5174 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5175 assert(counter
== (unsigned int)(max_keys
>> 1));
5177 /* Release allocated resources */
5178 for (size_t x
= 0; x
< max_keys
; ++x
)
5183 /* restore the memc handle */
5184 memc
->hosts
[0].port
= port0
;
5185 memc
->hosts
[2].port
= port2
;
5186 return TEST_SUCCESS
;
5189 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5191 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5192 assert(memc_clone
!= NULL
);
5193 assert(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5195 if (memc_clone
->hosts
[0].major_version
> 1 ||
5196 (memc_clone
->hosts
[0].major_version
== 1 &&
5197 memc_clone
->hosts
[0].minor_version
> 2))
5199 /* Binary protocol doesn't support deferred delete */
5200 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5201 assert(bin_clone
!= NULL
);
5202 assert(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5203 assert(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5204 memcached_free(bin_clone
);
5206 memcached_quit(memc_clone
);
5208 /* If we know the server version, deferred delete should fail
5209 * with invalid arguments */
5210 assert(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5212 /* If we don't know the server version, we should get a protocol error */
5213 memcached_return rc
= memcached_delete(memc
, "foo", 3, 1);
5214 /* but there is a bug in some of the memcached servers (1.4) that treats
5215 * the counter as noreply so it doesn't send the proper error message
5217 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5219 /* And buffered mode should be disabled and we should get protocol error */
5220 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5221 rc
= memcached_delete(memc
, "foo", 3, 1);
5222 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5224 /* Same goes for noreply... */
5225 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5226 rc
= memcached_delete(memc
, "foo", 3, 1);
5227 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5229 /* but a normal request should go through (and be buffered) */
5230 assert((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5231 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5233 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5234 /* unbuffered noreply should be success */
5235 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5236 /* unbuffered with reply should be not found... */
5237 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5238 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5241 memcached_free(memc_clone
);
5242 return TEST_SUCCESS
;
5246 /* Test memcached_server_get_last_disconnect
5247 * For a working server set, shall be NULL
5248 * For a set of non existing server, shall not be NULL
5250 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5252 memcached_return rc
;
5253 memcached_server_st
*disconnected_server
;
5255 /* With the working set of server */
5256 const char *key
= "marmotte";
5257 const char *value
= "milka";
5259 rc
= memcached_set(memc
, key
, strlen(key
),
5260 value
, strlen(value
),
5261 (time_t)0, (uint32_t)0);
5262 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5264 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5265 assert(disconnected_server
== NULL
);
5267 /* With a non existing server */
5269 memcached_server_st
*servers
;
5271 const char *server_list
= "localhost:9";
5273 servers
= memcached_servers_parse(server_list
);
5275 mine
= memcached_create(NULL
);
5276 rc
= memcached_server_push(mine
, servers
);
5277 assert(rc
== MEMCACHED_SUCCESS
);
5278 memcached_server_list_free(servers
);
5281 rc
= memcached_set(mine
, key
, strlen(key
),
5282 value
, strlen(value
),
5283 (time_t)0, (uint32_t)0);
5284 assert(rc
!= MEMCACHED_SUCCESS
);
5286 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5287 assert(disconnected_server
!= NULL
);
5288 assert(disconnected_server
->port
== 9);
5289 assert(strncmp(disconnected_server
->hostname
,"localhost",9) == 0);
5291 memcached_quit(mine
);
5292 memcached_free(mine
);
5294 return TEST_SUCCESS
;
5298 * This test ensures that the failure counter isn't incremented during
5299 * normal termination of the memcached instance.
5301 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5303 memcached_return rc
;
5305 /* Set value to force connection to the server */
5306 const char *key
= "marmotte";
5307 const char *value
= "milka";
5310 * Please note that I'm abusing the internal structures in libmemcached
5311 * in a non-portable way and you shouldn't be doing this. I'm only
5312 * doing this in order to verify that the library works the way it should
5314 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5315 memc
->number_of_hosts
= 1;
5317 /* Ensure that we are connected to the server by setting a value */
5318 rc
= memcached_set(memc
, key
, strlen(key
),
5319 value
, strlen(value
),
5320 (time_t)0, (uint32_t)0);
5321 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5324 /* The test is to see that the memcached_quit doesn't increase the
5325 * the server failure conter, so let's ensure that it is zero
5326 * before sending quit
5328 memc
->hosts
[0].server_failure_counter
= 0;
5330 memcached_quit(memc
);
5332 /* Verify that it memcached_quit didn't increment the failure counter
5333 * Please note that this isn't bullet proof, because an error could
5336 assert(memc
->hosts
[0].server_failure_counter
== 0);
5338 /* restore the instance */
5339 memc
->number_of_hosts
= number_of_hosts
;
5341 return TEST_SUCCESS
;
5344 test_st udp_setup_server_tests
[] ={
5345 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
5346 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
5347 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
5351 test_st upd_io_tests
[] ={
5352 {"udp_set_test", 0, udp_set_test
},
5353 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
5354 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
5355 {"udp_delete_test", 0, udp_delete_test
},
5356 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
5357 {"udp_verbosity_test", 0, udp_verbosity_test
},
5358 {"udp_quit_test", 0, udp_quit_test
},
5359 {"udp_flush_test", 0, udp_flush_test
},
5360 {"udp_incr_test", 0, udp_incr_test
},
5361 {"udp_decr_test", 0, udp_decr_test
},
5362 {"udp_stat_test", 0, udp_stat_test
},
5363 {"udp_version_test", 0, udp_version_test
},
5364 {"udp_get_test", 0, udp_get_test
},
5365 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
5369 /* Clean the server before beginning testing */
5371 {"flush", 0, flush_test
},
5372 {"init", 0, init_test
},
5373 {"allocation", 0, allocation_test
},
5374 {"server_list_null_test", 0, server_list_null_test
},
5375 {"server_unsort", 0, server_unsort_test
},
5376 {"server_sort", 0, server_sort_test
},
5377 {"server_sort2", 0, server_sort2_test
},
5378 {"clone_test", 0, clone_test
},
5379 {"connection_test", 0, connection_test
},
5380 {"callback_test", 0, callback_test
},
5381 {"behavior_test", 0, behavior_test
},
5382 {"userdata_test", 0, userdata_test
},
5383 {"error", 0, error_test
},
5384 {"set", 0, set_test
},
5385 {"set2", 0, set_test2
},
5386 {"set3", 0, set_test3
},
5387 {"dump", 1, dump_test
},
5388 {"add", 1, add_test
},
5389 {"replace", 1, replace_test
},
5390 {"delete", 1, delete_test
},
5391 {"get", 1, get_test
},
5392 {"get2", 0, get_test2
},
5393 {"get3", 0, get_test3
},
5394 {"get4", 0, get_test4
},
5395 {"partial mget", 0, get_test5
},
5396 {"stats_servername", 0, stats_servername_test
},
5397 {"increment", 0, increment_test
},
5398 {"increment_with_initial", 1, increment_with_initial_test
},
5399 {"decrement", 0, decrement_test
},
5400 {"decrement_with_initial", 1, decrement_with_initial_test
},
5401 {"increment_by_key", 0, increment_by_key_test
},
5402 {"increment_with_initial_by_key", 1, increment_with_initial_by_key_test
},
5403 {"decrement_by_key", 0, decrement_by_key_test
},
5404 {"decrement_with_initial_by_key", 1, decrement_with_initial_by_key_test
},
5405 {"quit", 0, quit_test
},
5406 {"mget", 1, mget_test
},
5407 {"mget_result", 1, mget_result_test
},
5408 {"mget_result_alloc", 1, mget_result_alloc_test
},
5409 {"mget_result_function", 1, mget_result_function
},
5410 {"mget_execute", 1, mget_execute
},
5411 {"mget_end", 0, mget_end
},
5412 {"get_stats", 0, get_stats
},
5413 {"add_host_test", 0, add_host_test
},
5414 {"add_host_test_1", 0, add_host_test1
},
5415 {"get_stats_keys", 0, get_stats_keys
},
5416 {"behavior_test", 0, get_stats_keys
},
5417 {"callback_test", 0, get_stats_keys
},
5418 {"version_string_test", 0, version_string_test
},
5419 {"bad_key", 1, bad_key_test
},
5420 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
5421 {"read_through", 1, read_through
},
5422 {"delete_through", 1, delete_through
},
5423 {"noreply", 1, noreply_test
},
5424 {"analyzer", 1, analyzer_test
},
5425 #ifdef HAVE_LIBMEMCACHEDUTIL
5426 {"connectionpool", 1, connection_pool_test
},
5428 {"test_get_last_disconnect", 1, test_get_last_disconnect
},
5432 test_st async_tests
[] ={
5433 {"add", 1, add_wrapper
},
5437 test_st string_tests
[] ={
5438 {"string static with null", 0, string_static_null
},
5439 {"string alloc with null", 0, string_alloc_null
},
5440 {"string alloc with 1K", 0, string_alloc_with_size
},
5441 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
5442 {"string append", 0, string_alloc_append
},
5443 {"string append failure (too big)", 0, string_alloc_append_toobig
},
5447 test_st result_tests
[] ={
5448 {"result static", 0, result_static
},
5449 {"result alloc", 0, result_alloc
},
5453 test_st version_1_2_3
[] ={
5454 {"append", 0, append_test
},
5455 {"prepend", 0, prepend_test
},
5456 {"cas", 0, cas_test
},
5457 {"cas2", 0, cas2_test
},
5458 {"append_binary", 0, append_binary_test
},
5462 test_st user_tests
[] ={
5463 {"user_supplied_bug1", 0, user_supplied_bug1
},
5464 {"user_supplied_bug2", 0, user_supplied_bug2
},
5465 {"user_supplied_bug3", 0, user_supplied_bug3
},
5466 {"user_supplied_bug4", 0, user_supplied_bug4
},
5467 {"user_supplied_bug5", 1, user_supplied_bug5
},
5468 {"user_supplied_bug6", 1, user_supplied_bug6
},
5469 {"user_supplied_bug7", 1, user_supplied_bug7
},
5470 {"user_supplied_bug8", 1, user_supplied_bug8
},
5471 {"user_supplied_bug9", 1, user_supplied_bug9
},
5472 {"user_supplied_bug10", 1, user_supplied_bug10
},
5473 {"user_supplied_bug11", 1, user_supplied_bug11
},
5474 {"user_supplied_bug12", 1, user_supplied_bug12
},
5475 {"user_supplied_bug13", 1, user_supplied_bug13
},
5476 {"user_supplied_bug14", 1, user_supplied_bug14
},
5477 {"user_supplied_bug15", 1, user_supplied_bug15
},
5478 {"user_supplied_bug16", 1, user_supplied_bug16
},
5481 ** It seems to be something weird with the character sets..
5482 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
5483 ** guess I need to find out how this is supposed to work.. Perhaps I need
5484 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
5485 ** so just disable the code for now...).
5487 {"user_supplied_bug17", 1, user_supplied_bug17
},
5489 {"user_supplied_bug18", 1, user_supplied_bug18
},
5490 {"user_supplied_bug19", 1, user_supplied_bug19
},
5491 {"user_supplied_bug20", 1, user_supplied_bug20
},
5492 {"user_supplied_bug21", 1, user_supplied_bug21
},
5493 {"wrong_failure_counter_test", 1, wrong_failure_counter_test
},
5497 test_st replication_tests
[]= {
5498 {"set", 1, replication_set_test
},
5499 {"get", 0, replication_get_test
},
5500 {"mget", 0, replication_mget_test
},
5501 {"delete", 0, replication_delete_test
},
5506 * The following test suite is used to verify that we don't introduce
5507 * regression bugs. If you want more information about the bug / test,
5508 * you should look in the bug report at
5509 * http://bugs.launchpad.net/libmemcached
5511 test_st regression_tests
[]= {
5512 {"lp:434484", 1, regression_bug_434484
},
5513 {"lp:434843", 1, regression_bug_434843
},
5514 {"lp:434843 buffered", 1, regression_bug_434843_buffered
},
5515 {"lp:421108", 1, regression_bug_421108
},
5516 {"lp:442914", 1, regression_bug_442914
},
5517 {"lp:447342", 1, regression_bug_447342
},
5518 {"lp:463297", 1, regression_bug_463297
},
5522 test_st ketama_compatibility
[]= {
5523 {"libmemcached", 1, ketama_compatibility_libmemcached
},
5524 {"spymemcached", 1, ketama_compatibility_spymemcached
},
5528 test_st generate_tests
[] ={
5529 {"generate_pairs", 1, generate_pairs
},
5530 {"generate_data", 1, generate_data
},
5531 {"get_read", 0, get_read
},
5532 {"delete_generate", 0, delete_generate
},
5533 {"generate_buffer_data", 1, generate_buffer_data
},
5534 {"delete_buffer", 0, delete_buffer_generate
},
5535 {"generate_data", 1, generate_data
},
5536 {"mget_read", 0, mget_read
},
5537 {"mget_read_result", 0, mget_read_result
},
5538 {"mget_read_function", 0, mget_read_function
},
5539 {"cleanup", 1, cleanup_pairs
},
5540 {"generate_large_pairs", 1, generate_large_pairs
},
5541 {"generate_data", 1, generate_data
},
5542 {"generate_buffer_data", 1, generate_buffer_data
},
5543 {"cleanup", 1, cleanup_pairs
},
5547 test_st consistent_tests
[] ={
5548 {"generate_pairs", 1, generate_pairs
},
5549 {"generate_data", 1, generate_data
},
5550 {"get_read", 0, get_read_count
},
5551 {"cleanup", 1, cleanup_pairs
},
5555 test_st consistent_weighted_tests
[] ={
5556 {"generate_pairs", 1, generate_pairs
},
5557 {"generate_data", 1, generate_data_with_stats
},
5558 {"get_read", 0, get_read_count
},
5559 {"cleanup", 1, cleanup_pairs
},
5563 test_st hsieh_availability
[] ={
5564 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
5568 test_st ketama_auto_eject_hosts
[] ={
5569 {"auto_eject_hosts", 1, auto_eject_hosts
},
5570 {"output_ketama_weighted_keys", 1, output_ketama_weighted_keys
},
5574 test_st hash_tests
[] ={
5575 {"md5", 0, md5_run
},
5576 {"crc", 0, crc_run
},
5577 {"fnv1_64", 0, fnv1_64_run
},
5578 {"fnv1a_64", 0, fnv1a_64_run
},
5579 {"fnv1_32", 0, fnv1_32_run
},
5580 {"fnv1a_32", 0, fnv1a_32_run
},
5581 {"hsieh", 0, hsieh_run
},
5582 {"murmur", 0, murmur_run
},
5583 {"jenkis", 0, jenkins_run
},
5587 collection_st collection
[] ={
5588 {"hsieh_availability",0,0,hsieh_availability
},
5589 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
5590 {"udp_io", init_udp
, 0, upd_io_tests
},
5591 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
5592 {"block", 0, 0, tests
},
5593 {"binary", pre_binary
, 0, tests
},
5594 {"nonblock", pre_nonblock
, 0, tests
},
5595 {"nodelay", pre_nodelay
, 0, tests
},
5596 {"settimer", pre_settimer
, 0, tests
},
5597 {"md5", pre_md5
, 0, tests
},
5598 {"crc", pre_crc
, 0, tests
},
5599 {"hsieh", pre_hsieh
, 0, tests
},
5600 {"jenkins", pre_jenkins
, 0, tests
},
5601 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
5602 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
5603 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
5604 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
5605 {"ketama", pre_behavior_ketama
, 0, tests
},
5606 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
5607 {"unix_socket", pre_unix_socket
, 0, tests
},
5608 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
5609 {"poll_timeout", poll_timeout
, 0, tests
},
5610 {"gets", enable_cas
, 0, tests
},
5611 {"consistent", enable_consistent
, 0, tests
},
5612 #ifdef MEMCACHED_ENABLE_DEPRECATED
5613 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
5615 {"memory_allocators", set_memory_alloc
, 0, tests
},
5616 {"prefix", set_prefix
, 0, tests
},
5617 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
5618 {"string", 0, 0, string_tests
},
5619 {"result", 0, 0, result_tests
},
5620 {"async", pre_nonblock
, 0, async_tests
},
5621 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
5622 {"user", 0, 0, user_tests
},
5623 {"generate", 0, 0, generate_tests
},
5624 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
5625 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
5626 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
5627 {"generate_md5", pre_md5
, 0, generate_tests
},
5628 {"generate_murmur", pre_murmur
, 0, generate_tests
},
5629 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
5630 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
5631 {"consistent_not", 0, 0, consistent_tests
},
5632 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
5633 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
5634 {"ketama_compat", 0, 0, ketama_compatibility
},
5635 {"test_hashes", 0, 0, hash_tests
},
5636 {"replication", pre_replication
, 0, replication_tests
},
5637 {"replication_noblock", pre_replication_noblock
, 0, replication_tests
},
5638 {"regression", 0, 0, regression_tests
},
5642 #define SERVERS_TO_CREATE 5
5644 /* Prototypes for functions we will pass to test framework */
5645 void *world_create(void);
5646 void world_destroy(void *p
);
5648 void *world_create(void)
5650 server_startup_st
*construct
;
5652 construct
= calloc(sizeof(server_startup_st
), 1);
5653 construct
->count
= SERVERS_TO_CREATE
;
5655 server_startup(construct
);
5661 void world_destroy(void *p
)
5663 server_startup_st
*construct
= (server_startup_st
*)p
;
5664 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
5665 memcached_server_list_free(servers
);
5667 server_shutdown(construct
);
5671 void get_world(world_st
*world
)
5673 world
->collections
= collection
;
5674 world
->create
= world_create
;
5675 world
->destroy
= world_destroy
;