2 Sample test application.
5 #include "libmemcached/common.h"
12 #include <sys/types.h>
17 #include "clients/generator.h"
18 #include "clients/execute.h"
21 #define INT64_MAX LONG_MAX
24 #define INT32_MAX INT_MAX
30 #ifdef HAVE_LIBMEMCACHEDUTIL
32 #include "libmemcached/memcached_util.h"
35 #define GLOBAL_COUNT 10000
36 #define GLOBAL2_COUNT 100
37 #define SERVERS_TO_CREATE 5
38 static uint32_t global_count
;
40 static pairs_st
*global_pairs
;
41 static const char *global_keys
[GLOBAL_COUNT
];
42 static size_t global_keys_length
[GLOBAL_COUNT
];
44 static test_return
init_test(memcached_st
*not_used
__attribute__((unused
)))
48 (void)memcached_create(&memc
);
49 memcached_free(&memc
);
54 static test_return
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
56 memcached_server_st
*server_list
;
59 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
60 assert(server_list
== NULL
);
62 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
63 assert(server_list
== NULL
);
65 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
66 assert(server_list
== NULL
);
71 #define TEST_PORT_COUNT 7
72 uint32_t test_ports
[TEST_PORT_COUNT
];
74 static memcached_return
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
77 uint32_t bigger
= *((uint32_t *)(context
));
78 assert(bigger
<= server
->port
);
79 *((uint32_t *)(context
))= server
->port
;
81 return MEMCACHED_SUCCESS
;
84 static test_return
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
87 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
89 memcached_server_function callbacks
[1];
90 memcached_st
*local_memc
;
92 local_memc
= memcached_create(NULL
);
94 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
96 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
98 test_ports
[x
]= (uint32_t)random() % 64000;
99 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
100 assert(local_memc
->number_of_hosts
== x
+ 1);
101 assert(local_memc
->hosts
[0].count
== x
+1);
102 assert(rc
== MEMCACHED_SUCCESS
);
105 callbacks
[0]= server_display_function
;
106 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
109 memcached_free(local_memc
);
114 static test_return
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
116 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
118 memcached_server_function callbacks
[1];
119 memcached_st
*local_memc
;
121 local_memc
= memcached_create(NULL
);
123 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
124 assert(rc
== MEMCACHED_SUCCESS
);
126 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
127 assert(rc
== MEMCACHED_SUCCESS
);
128 assert(local_memc
->hosts
[0].port
== 43043);
130 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
131 assert(rc
== MEMCACHED_SUCCESS
);
132 assert(local_memc
->hosts
[0].port
== 43042);
133 assert(local_memc
->hosts
[1].port
== 43043);
135 callbacks
[0]= server_display_function
;
136 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
139 memcached_free(local_memc
);
144 static memcached_return
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
147 uint32_t x
= *((uint32_t *)(context
));
149 assert(test_ports
[x
] == server
->port
);
150 *((uint32_t *)(context
))= ++x
;
152 return MEMCACHED_SUCCESS
;
155 static test_return
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
158 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
159 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
161 memcached_server_function callbacks
[1];
162 memcached_st
*local_memc
;
164 local_memc
= memcached_create(NULL
);
167 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
169 test_ports
[x
]= (uint32_t)(random() % 64000);
170 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
171 assert(local_memc
->number_of_hosts
== x
+1);
172 assert(local_memc
->hosts
[0].count
== x
+1);
173 assert(rc
== MEMCACHED_SUCCESS
);
176 callbacks
[0]= server_display_unsort_function
;
177 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
179 /* Now we sort old data! */
180 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
181 callbacks
[0]= server_display_function
;
182 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
185 memcached_free(local_memc
);
190 static test_return
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
193 memc
= memcached_create(NULL
);
195 memcached_free(memc
);
200 static test_return
clone_test(memcached_st
*memc
)
204 memcached_st
*memc_clone
;
205 memc_clone
= memcached_clone(NULL
, NULL
);
207 memcached_free(memc_clone
);
210 /* Can we init from null? */
212 memcached_st
*memc_clone
;
213 memc_clone
= memcached_clone(NULL
, memc
);
216 assert(memc_clone
->call_free
== memc
->call_free
);
217 assert(memc_clone
->call_malloc
== memc
->call_malloc
);
218 assert(memc_clone
->call_realloc
== memc
->call_realloc
);
219 assert(memc_clone
->call_calloc
== memc
->call_calloc
);
220 assert(memc_clone
->connect_timeout
== memc
->connect_timeout
);
221 assert(memc_clone
->delete_trigger
== memc
->delete_trigger
);
222 assert(memc_clone
->distribution
== memc
->distribution
);
223 assert(memc_clone
->flags
== memc
->flags
);
224 assert(memc_clone
->get_key_failure
== memc
->get_key_failure
);
225 assert(memc_clone
->hash
== memc
->hash
);
226 assert(memc_clone
->hash_continuum
== memc
->hash_continuum
);
227 assert(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
228 assert(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
229 assert(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
230 assert(memc_clone
->on_cleanup
== memc
->on_cleanup
);
231 assert(memc_clone
->on_clone
== memc
->on_clone
);
232 assert(memc_clone
->poll_timeout
== memc
->poll_timeout
);
233 assert(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
234 assert(memc_clone
->recv_size
== memc
->recv_size
);
235 assert(memc_clone
->retry_timeout
== memc
->retry_timeout
);
236 assert(memc_clone
->send_size
== memc
->send_size
);
237 assert(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
238 assert(memc_clone
->snd_timeout
== memc
->snd_timeout
);
239 assert(memc_clone
->user_data
== memc
->user_data
);
241 memcached_free(memc_clone
);
244 /* Can we init from struct? */
246 memcached_st declared_clone
;
247 memcached_st
*memc_clone
;
248 memset(&declared_clone
, 0 , sizeof(memcached_st
));
249 memc_clone
= memcached_clone(&declared_clone
, NULL
);
251 memcached_free(memc_clone
);
254 /* Can we init from struct? */
256 memcached_st declared_clone
;
257 memcached_st
*memc_clone
;
258 memset(&declared_clone
, 0 , sizeof(memcached_st
));
259 memc_clone
= memcached_clone(&declared_clone
, memc
);
261 memcached_free(memc_clone
);
267 static test_return
userdata_test(memcached_st
*memc
)
270 assert(memcached_set_user_data(memc
, foo
) == NULL
);
271 assert(memcached_get_user_data(memc
) == foo
);
272 assert(memcached_set_user_data(memc
, NULL
) == foo
);
277 static test_return
connection_test(memcached_st
*memc
)
281 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
282 assert(rc
== MEMCACHED_SUCCESS
);
287 static test_return
error_test(memcached_st
*memc
)
291 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
293 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
299 static test_return
set_test(memcached_st
*memc
)
302 const char *key
= "foo";
303 const char *value
= "when we sanitize";
305 rc
= memcached_set(memc
, key
, strlen(key
),
306 value
, strlen(value
),
307 (time_t)0, (uint32_t)0);
308 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
313 static test_return
append_test(memcached_st
*memc
)
316 const char *key
= "fig";
317 const char *in_value
= "we";
318 char *out_value
= NULL
;
322 rc
= memcached_flush(memc
, 0);
323 assert(rc
== MEMCACHED_SUCCESS
);
325 rc
= memcached_set(memc
, key
, strlen(key
),
326 in_value
, strlen(in_value
),
327 (time_t)0, (uint32_t)0);
328 assert(rc
== MEMCACHED_SUCCESS
);
330 rc
= memcached_append(memc
, key
, strlen(key
),
331 " the", strlen(" the"),
332 (time_t)0, (uint32_t)0);
333 assert(rc
== MEMCACHED_SUCCESS
);
335 rc
= memcached_append(memc
, key
, strlen(key
),
336 " people", strlen(" people"),
337 (time_t)0, (uint32_t)0);
338 assert(rc
== MEMCACHED_SUCCESS
);
340 out_value
= memcached_get(memc
, key
, strlen(key
),
341 &value_length
, &flags
, &rc
);
342 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
343 assert(strlen("we the people") == value_length
);
344 assert(rc
== MEMCACHED_SUCCESS
);
350 static test_return
append_binary_test(memcached_st
*memc
)
353 const char *key
= "numbers";
354 unsigned int *store_ptr
;
355 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
361 rc
= memcached_flush(memc
, 0);
362 assert(rc
== MEMCACHED_SUCCESS
);
364 rc
= memcached_set(memc
,
367 (time_t)0, (uint32_t)0);
368 assert(rc
== MEMCACHED_SUCCESS
);
370 for (x
= 0; store_list
[x
] ; x
++)
372 rc
= memcached_append(memc
,
374 (char *)&store_list
[x
], sizeof(unsigned int),
375 (time_t)0, (uint32_t)0);
376 assert(rc
== MEMCACHED_SUCCESS
);
379 value
= memcached_get(memc
, key
, strlen(key
),
380 &value_length
, &flags
, &rc
);
381 assert((value_length
== (sizeof(unsigned int) * x
)));
382 assert(rc
== MEMCACHED_SUCCESS
);
384 store_ptr
= (unsigned int *)value
;
386 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
388 assert(*store_ptr
== store_list
[x
++]);
396 static test_return
cas2_test(memcached_st
*memc
)
399 const char *keys
[]= {"fudge", "son", "food"};
400 size_t key_length
[]= {5, 3, 4};
401 const char *value
= "we the people";
402 size_t value_length
= strlen("we the people");
404 memcached_result_st results_obj
;
405 memcached_result_st
*results
;
408 rc
= memcached_flush(memc
, 0);
409 assert(rc
== MEMCACHED_SUCCESS
);
411 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
413 for (x
= 0; x
< 3; x
++)
415 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
416 keys
[x
], key_length
[x
],
417 (time_t)50, (uint32_t)9);
418 assert(rc
== MEMCACHED_SUCCESS
);
421 rc
= memcached_mget(memc
, keys
, key_length
, 3);
423 results
= memcached_result_create(memc
, &results_obj
);
425 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
427 assert(results
->cas
);
428 assert(rc
== MEMCACHED_SUCCESS
);
429 assert(memcached_result_cas(results
));
431 assert(!memcmp(value
, "we the people", strlen("we the people")));
432 assert(strlen("we the people") == value_length
);
433 assert(rc
== MEMCACHED_SUCCESS
);
435 memcached_result_free(&results_obj
);
440 static test_return
cas_test(memcached_st
*memc
)
443 const char *key
= "fun";
444 size_t key_length
= strlen(key
);
445 const char *value
= "we the people";
446 const char* keys
[2] = { key
, NULL
};
447 size_t keylengths
[2] = { strlen(key
), 0 };
448 size_t value_length
= strlen(value
);
449 const char *value2
= "change the value";
450 size_t value2_length
= strlen(value2
);
452 memcached_result_st results_obj
;
453 memcached_result_st
*results
;
456 rc
= memcached_flush(memc
, 0);
457 assert(rc
== MEMCACHED_SUCCESS
);
459 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
461 rc
= memcached_set(memc
, key
, strlen(key
),
462 value
, strlen(value
),
463 (time_t)0, (uint32_t)0);
464 assert(rc
== MEMCACHED_SUCCESS
);
466 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
468 results
= memcached_result_create(memc
, &results_obj
);
470 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
472 assert(rc
== MEMCACHED_SUCCESS
);
473 assert(memcached_result_cas(results
));
474 assert(!memcmp(value
, memcached_result_value(results
), value_length
));
475 assert(strlen(memcached_result_value(results
)) == value_length
);
476 assert(rc
== MEMCACHED_SUCCESS
);
477 uint64_t cas
= memcached_result_cas(results
);
480 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
481 assert(rc
== MEMCACHED_END
);
482 assert(results
== NULL
);
485 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
486 assert(rc
== MEMCACHED_SUCCESS
);
489 * The item will have a new cas value, so try to set it again with the old
490 * value. This should fail!
492 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
493 assert(rc
== MEMCACHED_DATA_EXISTS
);
495 memcached_result_free(&results_obj
);
500 static test_return
prepend_test(memcached_st
*memc
)
503 const char *key
= "fig";
504 const char *value
= "people";
505 char *out_value
= NULL
;
509 rc
= memcached_flush(memc
, 0);
510 assert(rc
== MEMCACHED_SUCCESS
);
512 rc
= memcached_set(memc
, key
, strlen(key
),
513 value
, strlen(value
),
514 (time_t)0, (uint32_t)0);
515 assert(rc
== MEMCACHED_SUCCESS
);
517 rc
= memcached_prepend(memc
, key
, strlen(key
),
518 "the ", strlen("the "),
519 (time_t)0, (uint32_t)0);
520 assert(rc
== MEMCACHED_SUCCESS
);
522 rc
= memcached_prepend(memc
, key
, strlen(key
),
523 "we ", strlen("we "),
524 (time_t)0, (uint32_t)0);
525 assert(rc
== MEMCACHED_SUCCESS
);
527 out_value
= memcached_get(memc
, key
, strlen(key
),
528 &value_length
, &flags
, &rc
);
529 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
530 assert(strlen("we the people") == value_length
);
531 assert(rc
== MEMCACHED_SUCCESS
);
538 Set the value, then quit to make sure it is flushed.
539 Come back in and test that add fails.
541 static test_return
add_test(memcached_st
*memc
)
544 const char *key
= "foo";
545 const char *value
= "when we sanitize";
546 unsigned long long setting_value
;
548 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
550 rc
= memcached_set(memc
, key
, strlen(key
),
551 value
, strlen(value
),
552 (time_t)0, (uint32_t)0);
553 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
554 memcached_quit(memc
);
555 rc
= memcached_add(memc
, key
, strlen(key
),
556 value
, strlen(value
),
557 (time_t)0, (uint32_t)0);
559 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
561 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
563 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
569 ** There was a problem of leaking filedescriptors in the initial release
570 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
571 ** systems it seems that the kernel is slow on reclaiming the resources
572 ** because the connects starts to time out (the test doesn't do much
573 ** anyway, so just loop 10 iterations)
575 static test_return
add_wrapper(memcached_st
*memc
)
578 unsigned int max
= 10000;
583 for (x
= 0; x
< max
; x
++)
589 static test_return
replace_test(memcached_st
*memc
)
592 const char *key
= "foo";
593 const char *value
= "when we sanitize";
594 const char *original
= "first we insert some data";
596 rc
= memcached_set(memc
, key
, strlen(key
),
597 original
, strlen(original
),
598 (time_t)0, (uint32_t)0);
599 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
601 rc
= memcached_replace(memc
, key
, strlen(key
),
602 value
, strlen(value
),
603 (time_t)0, (uint32_t)0);
604 assert(rc
== MEMCACHED_SUCCESS
);
609 static test_return
delete_test(memcached_st
*memc
)
612 const char *key
= "foo";
613 const char *value
= "when we sanitize";
615 rc
= memcached_set(memc
, key
, strlen(key
),
616 value
, strlen(value
),
617 (time_t)0, (uint32_t)0);
618 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
620 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
621 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
626 static test_return
flush_test(memcached_st
*memc
)
630 rc
= memcached_flush(memc
, 0);
631 assert(rc
== MEMCACHED_SUCCESS
);
636 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
637 memcached_server_st
*server
__attribute__((unused
)),
638 void *context
__attribute__((unused
)))
642 return MEMCACHED_SUCCESS
;
645 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
648 strcpy(context
, "foo bad");
649 memcached_server_function callbacks
[1];
651 callbacks
[0]= server_function
;
652 memcached_server_cursor(memc
, callbacks
, context
, 1);
656 static test_return
bad_key_test(memcached_st
*memc
)
659 const char *key
= "foo bad";
661 size_t string_length
;
663 memcached_st
*memc_clone
;
665 size_t max_keylen
= 0xffff;
667 memc_clone
= memcached_clone(NULL
, memc
);
670 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
671 assert(rc
== MEMCACHED_SUCCESS
);
673 /* All keys are valid in the binary protocol (except for length) */
674 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
676 string
= memcached_get(memc_clone
, key
, strlen(key
),
677 &string_length
, &flags
, &rc
);
678 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
679 assert(string_length
== 0);
683 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
684 assert(rc
== MEMCACHED_SUCCESS
);
685 string
= memcached_get(memc_clone
, key
, strlen(key
),
686 &string_length
, &flags
, &rc
);
687 assert(rc
== MEMCACHED_NOTFOUND
);
688 assert(string_length
== 0);
691 /* Test multi key for bad keys */
692 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
693 size_t key_lengths
[] = { 7, 7, 7 };
695 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
696 assert(rc
== MEMCACHED_SUCCESS
);
698 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
699 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
701 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
702 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
706 /* The following test should be moved to the end of this function when the
707 memcached server is updated to allow max size length of the keys in the
710 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
711 assert(rc
== MEMCACHED_SUCCESS
);
713 char *longkey
= malloc(max_keylen
+ 1);
716 memset(longkey
, 'a', max_keylen
+ 1);
717 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
718 &string_length
, &flags
, &rc
);
719 assert(rc
== MEMCACHED_NOTFOUND
);
720 assert(string_length
== 0);
723 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
724 &string_length
, &flags
, &rc
);
725 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
726 assert(string_length
== 0);
733 /* Make sure zero length keys are marked as bad */
735 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
736 assert(rc
== MEMCACHED_SUCCESS
);
737 string
= memcached_get(memc_clone
, key
, 0,
738 &string_length
, &flags
, &rc
);
739 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
740 assert(string_length
== 0);
743 memcached_free(memc_clone
);
748 #define READ_THROUGH_VALUE "set for me"
749 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
750 char *key
__attribute__((unused
)),
751 size_t key_length
__attribute__((unused
)),
752 memcached_result_st
*result
)
755 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
758 static test_return
read_through(memcached_st
*memc
)
761 const char *key
= "foo";
763 size_t string_length
;
765 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
767 string
= memcached_get(memc
, key
, strlen(key
),
768 &string_length
, &flags
, &rc
);
770 assert(rc
== MEMCACHED_NOTFOUND
);
771 assert(string_length
== 0);
774 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
776 assert(rc
== MEMCACHED_SUCCESS
);
778 string
= memcached_get(memc
, key
, strlen(key
),
779 &string_length
, &flags
, &rc
);
781 assert(rc
== MEMCACHED_SUCCESS
);
782 assert(string_length
== strlen(READ_THROUGH_VALUE
));
783 assert(!strcmp(READ_THROUGH_VALUE
, string
));
786 string
= memcached_get(memc
, key
, strlen(key
),
787 &string_length
, &flags
, &rc
);
789 assert(rc
== MEMCACHED_SUCCESS
);
790 assert(string_length
== strlen(READ_THROUGH_VALUE
));
791 assert(!strcmp(READ_THROUGH_VALUE
, string
));
797 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
799 size_t key_length
__attribute__((unused
)))
803 return MEMCACHED_SUCCESS
;
806 static test_return
delete_through(memcached_st
*memc
)
808 memcached_trigger_delete_key callback
;
811 callback
= (memcached_trigger_delete_key
)delete_trigger
;
813 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
814 assert(rc
== MEMCACHED_SUCCESS
);
819 static test_return
get_test(memcached_st
*memc
)
822 const char *key
= "foo";
824 size_t string_length
;
827 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
828 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
830 string
= memcached_get(memc
, key
, strlen(key
),
831 &string_length
, &flags
, &rc
);
833 assert(rc
== MEMCACHED_NOTFOUND
);
834 assert(string_length
== 0);
840 static test_return
get_test2(memcached_st
*memc
)
843 const char *key
= "foo";
844 const char *value
= "when we sanitize";
846 size_t string_length
;
849 rc
= memcached_set(memc
, key
, strlen(key
),
850 value
, strlen(value
),
851 (time_t)0, (uint32_t)0);
852 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
854 string
= memcached_get(memc
, key
, strlen(key
),
855 &string_length
, &flags
, &rc
);
858 assert(rc
== MEMCACHED_SUCCESS
);
859 assert(string_length
== strlen(value
));
860 assert(!memcmp(string
, value
, string_length
));
867 static test_return
set_test2(memcached_st
*memc
)
870 const char *key
= "foo";
871 const char *value
= "train in the brain";
872 size_t value_length
= strlen(value
);
875 for (x
= 0; x
< 10; x
++)
877 rc
= memcached_set(memc
, key
, strlen(key
),
879 (time_t)0, (uint32_t)0);
880 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
886 static test_return
set_test3(memcached_st
*memc
)
890 size_t value_length
= 8191;
893 value
= (char*)malloc(value_length
);
896 for (x
= 0; x
< value_length
; x
++)
897 value
[x
] = (char) (x
% 127);
899 /* The dump test relies on there being at least 32 items in memcached */
900 for (x
= 0; x
< 32; x
++)
904 sprintf(key
, "foo%u", x
);
906 rc
= memcached_set(memc
, key
, strlen(key
),
908 (time_t)0, (uint32_t)0);
909 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
917 static test_return
get_test3(memcached_st
*memc
)
920 const char *key
= "foo";
922 size_t value_length
= 8191;
924 size_t string_length
;
928 value
= (char*)malloc(value_length
);
931 for (x
= 0; x
< value_length
; x
++)
932 value
[x
] = (char) (x
% 127);
934 rc
= memcached_set(memc
, key
, strlen(key
),
936 (time_t)0, (uint32_t)0);
937 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
939 string
= memcached_get(memc
, key
, strlen(key
),
940 &string_length
, &flags
, &rc
);
942 assert(rc
== MEMCACHED_SUCCESS
);
944 assert(string_length
== value_length
);
945 assert(!memcmp(string
, value
, string_length
));
953 static test_return
get_test4(memcached_st
*memc
)
956 const char *key
= "foo";
958 size_t value_length
= 8191;
960 size_t string_length
;
964 value
= (char*)malloc(value_length
);
967 for (x
= 0; x
< value_length
; x
++)
968 value
[x
] = (char) (x
% 127);
970 rc
= memcached_set(memc
, key
, strlen(key
),
972 (time_t)0, (uint32_t)0);
973 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
975 for (x
= 0; x
< 10; x
++)
977 string
= memcached_get(memc
, key
, strlen(key
),
978 &string_length
, &flags
, &rc
);
980 assert(rc
== MEMCACHED_SUCCESS
);
982 assert(string_length
== value_length
);
983 assert(!memcmp(string
, value
, string_length
));
993 * This test verifies that memcached_read_one_response doesn't try to
994 * dereference a NIL-pointer if you issue a multi-get and don't read out all
995 * responses before you execute a storage command.
997 static test_return
get_test5(memcached_st
*memc
)
1000 ** Request the same key twice, to ensure that we hash to the same server
1001 ** (so that we have multiple response values queued up) ;-)
1003 const char *keys
[]= { "key", "key" };
1004 size_t lengths
[]= { 3, 3 };
1008 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1009 keys
[0], lengths
[0], 0, 0);
1010 assert(rc
== MEMCACHED_SUCCESS
);
1011 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1013 memcached_result_st results_obj
;
1014 memcached_result_st
*results
;
1015 results
=memcached_result_create(memc
, &results_obj
);
1017 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1019 memcached_result_free(&results_obj
);
1021 /* Don't read out the second result, but issue a set instead.. */
1022 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1023 assert(rc
== MEMCACHED_SUCCESS
);
1025 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1026 &rlen
, &flags
, &rc
);
1027 assert(val
== NULL
);
1028 assert(rc
== MEMCACHED_NOTFOUND
);
1029 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1030 assert(val
!= NULL
);
1031 assert(rc
== MEMCACHED_SUCCESS
);
1034 return TEST_SUCCESS
;
1037 /* Do not copy the style of this code, I just access hosts to testthis function */
1038 static test_return
stats_servername_test(memcached_st
*memc
)
1040 memcached_return rc
;
1041 memcached_stat_st memc_stat
;
1042 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1043 memc
->hosts
[0].hostname
,
1044 memc
->hosts
[0].port
);
1049 static test_return
increment_test(memcached_st
*memc
)
1051 uint64_t new_number
;
1052 memcached_return rc
;
1053 const char *key
= "number";
1054 const char *value
= "0";
1056 rc
= memcached_set(memc
, key
, strlen(key
),
1057 value
, strlen(value
),
1058 (time_t)0, (uint32_t)0);
1059 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1061 rc
= memcached_increment(memc
, key
, strlen(key
),
1063 assert(rc
== MEMCACHED_SUCCESS
);
1064 assert(new_number
== 1);
1066 rc
= memcached_increment(memc
, key
, strlen(key
),
1068 assert(rc
== MEMCACHED_SUCCESS
);
1069 assert(new_number
== 2);
1074 static test_return
increment_with_initial_test(memcached_st
*memc
)
1076 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1078 uint64_t new_number
;
1079 memcached_return rc
;
1080 const char *key
= "number";
1081 uint64_t initial
= 0;
1083 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1084 1, initial
, 0, &new_number
);
1085 assert(rc
== MEMCACHED_SUCCESS
);
1086 assert(new_number
== initial
);
1088 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1089 1, initial
, 0, &new_number
);
1090 assert(rc
== MEMCACHED_SUCCESS
);
1091 assert(new_number
== (initial
+ 1));
1096 static test_return
decrement_test(memcached_st
*memc
)
1098 uint64_t new_number
;
1099 memcached_return rc
;
1100 const char *key
= "number";
1101 const char *value
= "3";
1103 rc
= memcached_set(memc
, key
, strlen(key
),
1104 value
, strlen(value
),
1105 (time_t)0, (uint32_t)0);
1106 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1108 rc
= memcached_decrement(memc
, key
, strlen(key
),
1110 assert(rc
== MEMCACHED_SUCCESS
);
1111 assert(new_number
== 2);
1113 rc
= memcached_decrement(memc
, key
, strlen(key
),
1115 assert(rc
== MEMCACHED_SUCCESS
);
1116 assert(new_number
== 1);
1121 static test_return
decrement_with_initial_test(memcached_st
*memc
)
1123 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1125 uint64_t new_number
;
1126 memcached_return rc
;
1127 const char *key
= "number";
1128 uint64_t initial
= 3;
1130 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1131 1, initial
, 0, &new_number
);
1132 assert(rc
== MEMCACHED_SUCCESS
);
1133 assert(new_number
== initial
);
1135 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1136 1, initial
, 0, &new_number
);
1137 assert(rc
== MEMCACHED_SUCCESS
);
1138 assert(new_number
== (initial
- 1));
1143 static test_return
quit_test(memcached_st
*memc
)
1145 memcached_return rc
;
1146 const char *key
= "fudge";
1147 const char *value
= "sanford and sun";
1149 rc
= memcached_set(memc
, key
, strlen(key
),
1150 value
, strlen(value
),
1151 (time_t)10, (uint32_t)3);
1152 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1153 memcached_quit(memc
);
1155 rc
= memcached_set(memc
, key
, strlen(key
),
1156 value
, strlen(value
),
1157 (time_t)50, (uint32_t)9);
1158 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1163 static test_return
mget_result_test(memcached_st
*memc
)
1165 memcached_return rc
;
1166 const char *keys
[]= {"fudge", "son", "food"};
1167 size_t key_length
[]= {5, 3, 4};
1170 memcached_result_st results_obj
;
1171 memcached_result_st
*results
;
1173 results
= memcached_result_create(memc
, &results_obj
);
1175 assert(&results_obj
== results
);
1177 /* We need to empty the server before continueing test */
1178 rc
= memcached_flush(memc
, 0);
1179 assert(rc
== MEMCACHED_SUCCESS
);
1181 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1182 assert(rc
== MEMCACHED_SUCCESS
);
1184 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1189 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1191 assert(rc
== MEMCACHED_END
);
1193 for (x
= 0; x
< 3; x
++)
1195 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1196 keys
[x
], key_length
[x
],
1197 (time_t)50, (uint32_t)9);
1198 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1201 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1202 assert(rc
== MEMCACHED_SUCCESS
);
1204 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1207 assert(&results_obj
== results
);
1208 assert(rc
== MEMCACHED_SUCCESS
);
1209 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1210 assert(!memcmp(memcached_result_key_value(results
),
1211 memcached_result_value(results
),
1212 memcached_result_length(results
)));
1215 memcached_result_free(&results_obj
);
1220 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1222 memcached_return rc
;
1223 const char *keys
[]= {"fudge", "son", "food"};
1224 size_t key_length
[]= {5, 3, 4};
1227 memcached_result_st
*results
;
1229 /* We need to empty the server before continueing test */
1230 rc
= memcached_flush(memc
, 0);
1231 assert(rc
== MEMCACHED_SUCCESS
);
1233 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1234 assert(rc
== MEMCACHED_SUCCESS
);
1236 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1241 assert(rc
== MEMCACHED_END
);
1243 for (x
= 0; x
< 3; x
++)
1245 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1246 keys
[x
], key_length
[x
],
1247 (time_t)50, (uint32_t)9);
1248 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1251 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1252 assert(rc
== MEMCACHED_SUCCESS
);
1255 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1258 assert(rc
== MEMCACHED_SUCCESS
);
1259 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1260 assert(!memcmp(memcached_result_key_value(results
),
1261 memcached_result_value(results
),
1262 memcached_result_length(results
)));
1263 memcached_result_free(results
);
1270 /* Count the results */
1271 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1272 memcached_result_st
*result
__attribute__((unused
)),
1275 unsigned int *counter
= (unsigned int *)context
;
1277 *counter
= *counter
+ 1;
1279 return MEMCACHED_SUCCESS
;
1282 static test_return
mget_result_function(memcached_st
*memc
)
1284 memcached_return rc
;
1285 const char *keys
[]= {"fudge", "son", "food"};
1286 size_t key_length
[]= {5, 3, 4};
1288 unsigned int counter
;
1289 memcached_execute_function callbacks
[1];
1291 /* We need to empty the server before continueing test */
1292 rc
= memcached_flush(memc
, 0);
1293 for (x
= 0; x
< 3; x
++)
1295 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1296 keys
[x
], key_length
[x
],
1297 (time_t)50, (uint32_t)9);
1298 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1301 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1302 assert(rc
== MEMCACHED_SUCCESS
);
1304 callbacks
[0]= &callback_counter
;
1306 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1308 assert(counter
== 3);
1313 static test_return
mget_test(memcached_st
*memc
)
1315 memcached_return rc
;
1316 const char *keys
[]= {"fudge", "son", "food"};
1317 size_t key_length
[]= {5, 3, 4};
1321 char return_key
[MEMCACHED_MAX_KEY
];
1322 size_t return_key_length
;
1324 size_t return_value_length
;
1326 /* We need to empty the server before continueing test */
1327 rc
= memcached_flush(memc
, 0);
1328 assert(rc
== MEMCACHED_SUCCESS
);
1330 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1331 assert(rc
== MEMCACHED_SUCCESS
);
1333 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1334 &return_value_length
, &flags
, &rc
)) != NULL
)
1336 assert(return_value
);
1338 assert(!return_value
);
1339 assert(return_value_length
== 0);
1340 assert(rc
== MEMCACHED_END
);
1342 for (x
= 0; x
< 3; x
++)
1344 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1345 keys
[x
], key_length
[x
],
1346 (time_t)50, (uint32_t)9);
1347 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1350 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1351 assert(rc
== MEMCACHED_SUCCESS
);
1354 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1355 &return_value_length
, &flags
, &rc
)))
1357 assert(return_value
);
1358 assert(rc
== MEMCACHED_SUCCESS
);
1359 assert(return_key_length
== return_value_length
);
1360 assert(!memcmp(return_value
, return_key
, return_value_length
));
1368 static test_return
get_stats_keys(memcached_st
*memc
)
1372 memcached_stat_st memc_stat
;
1373 memcached_return rc
;
1375 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1376 assert(rc
== MEMCACHED_SUCCESS
);
1377 for (ptr
= list
; *ptr
; ptr
++)
1386 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1388 const char *version_string
;
1390 version_string
= memcached_lib_version();
1392 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1397 static test_return
get_stats(memcached_st
*memc
)
1402 memcached_return rc
;
1403 memcached_stat_st
*memc_stat
;
1405 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1406 assert(rc
== MEMCACHED_SUCCESS
);
1408 assert(rc
== MEMCACHED_SUCCESS
);
1411 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1413 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1414 assert(rc
== MEMCACHED_SUCCESS
);
1415 for (ptr
= list
; *ptr
; ptr
++);
1420 memcached_stat_free(NULL
, memc_stat
);
1425 static test_return
add_host_test(memcached_st
*memc
)
1428 memcached_server_st
*servers
;
1429 memcached_return rc
;
1430 char servername
[]= "0.example.com";
1432 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1434 assert(1 == memcached_server_list_count(servers
));
1436 for (x
= 2; x
< 20; x
++)
1438 char buffer
[SMALL_STRING_LEN
];
1440 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1441 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1443 assert(rc
== MEMCACHED_SUCCESS
);
1444 assert(x
== memcached_server_list_count(servers
));
1447 rc
= memcached_server_push(memc
, servers
);
1448 assert(rc
== MEMCACHED_SUCCESS
);
1449 rc
= memcached_server_push(memc
, servers
);
1450 assert(rc
== MEMCACHED_SUCCESS
);
1452 memcached_server_list_free(servers
);
1457 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1459 return MEMCACHED_SUCCESS
;
1462 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1464 return MEMCACHED_SUCCESS
;
1467 static test_return
callback_test(memcached_st
*memc
)
1469 /* Test User Data */
1473 memcached_return rc
;
1475 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1476 assert(rc
== MEMCACHED_SUCCESS
);
1477 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1478 assert(*test_ptr
== x
);
1481 /* Test Clone Callback */
1483 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1484 void *clone_cb_ptr
= *(void **)&clone_cb
;
1485 void *temp_function
= NULL
;
1486 memcached_return rc
;
1488 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1490 assert(rc
== MEMCACHED_SUCCESS
);
1491 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1492 assert(temp_function
== clone_cb_ptr
);
1495 /* Test Cleanup Callback */
1497 memcached_cleanup_func cleanup_cb
=
1498 (memcached_cleanup_func
)cleanup_test_callback
;
1499 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1500 void *temp_function
= NULL
;
1501 memcached_return rc
;
1503 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1505 assert(rc
== MEMCACHED_SUCCESS
);
1506 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1507 assert(temp_function
== cleanup_cb_ptr
);
1513 /* We don't test the behavior itself, we test the switches */
1514 static test_return
behavior_test(memcached_st
*memc
)
1519 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1520 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1523 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1524 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1527 set
= MEMCACHED_HASH_MD5
;
1528 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1529 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1530 assert(value
== MEMCACHED_HASH_MD5
);
1534 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1535 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1538 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1539 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1542 set
= MEMCACHED_HASH_DEFAULT
;
1543 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1544 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1545 assert(value
== MEMCACHED_HASH_DEFAULT
);
1547 set
= MEMCACHED_HASH_CRC
;
1548 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1549 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1550 assert(value
== MEMCACHED_HASH_CRC
);
1552 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1555 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1558 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1559 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1560 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1564 /* Test case provided by Cal Haldenbrand */
1565 static test_return
user_supplied_bug1(memcached_st
*memc
)
1567 unsigned int setter
= 1;
1570 unsigned long long total
= 0;
1573 char randomstuff
[6 * 1024];
1574 memcached_return rc
;
1576 memset(randomstuff
, 0, 6 * 1024);
1578 /* We just keep looking at the same values over and over */
1581 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1582 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1586 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1590 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1591 memset(randomstuff
, 0, 6 * 1024);
1592 assert(size
< 6 * 1024); /* Being safe here */
1594 for (j
= 0 ; j
< size
;j
++)
1595 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1598 sprintf(key
, "%d", x
);
1599 rc
= memcached_set(memc
, key
, strlen(key
),
1600 randomstuff
, strlen(randomstuff
), 10, 0);
1601 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1602 /* If we fail, lets try again */
1603 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1604 rc
= memcached_set(memc
, key
, strlen(key
),
1605 randomstuff
, strlen(randomstuff
), 10, 0);
1606 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1612 /* Test case provided by Cal Haldenbrand */
1613 static test_return
user_supplied_bug2(memcached_st
*memc
)
1616 unsigned int setter
;
1618 unsigned long long total
;
1621 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1622 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1624 setter
= 20 * 1024576;
1625 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1626 setter
= 20 * 1024576;
1627 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1628 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1629 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1631 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1634 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1636 memcached_return rc
= MEMCACHED_SUCCESS
;
1637 char buffer
[SMALL_STRING_LEN
];
1642 memset(buffer
, 0, SMALL_STRING_LEN
);
1644 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1645 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1646 &val_len
, &flags
, &rc
);
1647 if (rc
!= MEMCACHED_SUCCESS
)
1649 if (rc
== MEMCACHED_NOTFOUND
)
1666 /* Do a large mget() over all the keys we think exist */
1667 #define KEY_COUNT 3000 // * 1024576
1668 static test_return
user_supplied_bug3(memcached_st
*memc
)
1670 memcached_return rc
;
1671 unsigned int setter
;
1674 size_t key_lengths
[KEY_COUNT
];
1677 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1678 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1680 setter
= 20 * 1024576;
1681 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1682 setter
= 20 * 1024576;
1683 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1684 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1685 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1688 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1690 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1691 for (x
= 0; x
< KEY_COUNT
; x
++)
1695 snprintf(buffer
, 30, "%u", x
);
1696 keys
[x
]= strdup(buffer
);
1697 key_lengths
[x
]= strlen(keys
[x
]);
1700 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
1701 assert(rc
== MEMCACHED_SUCCESS
);
1703 /* Turn this into a help function */
1705 char return_key
[MEMCACHED_MAX_KEY
];
1706 size_t return_key_length
;
1708 size_t return_value_length
;
1711 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1712 &return_value_length
, &flags
, &rc
)))
1714 assert(return_value
);
1715 assert(rc
== MEMCACHED_SUCCESS
);
1720 for (x
= 0; x
< KEY_COUNT
; x
++)
1727 /* Make sure we behave properly if server list has no values */
1728 static test_return
user_supplied_bug4(memcached_st
*memc
)
1730 memcached_return rc
;
1731 const char *keys
[]= {"fudge", "son", "food"};
1732 size_t key_length
[]= {5, 3, 4};
1735 char return_key
[MEMCACHED_MAX_KEY
];
1736 size_t return_key_length
;
1738 size_t return_value_length
;
1740 /* Here we free everything before running a bunch of mget tests */
1742 memcached_server_list_free(memc
->hosts
);
1744 memc
->number_of_hosts
= 0;
1748 /* We need to empty the server before continueing test */
1749 rc
= memcached_flush(memc
, 0);
1750 assert(rc
== MEMCACHED_NO_SERVERS
);
1752 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1753 assert(rc
== MEMCACHED_NO_SERVERS
);
1755 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1756 &return_value_length
, &flags
, &rc
)) != NULL
)
1758 assert(return_value
);
1760 assert(!return_value
);
1761 assert(return_value_length
== 0);
1762 assert(rc
== MEMCACHED_NO_SERVERS
);
1764 for (x
= 0; x
< 3; x
++)
1766 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1767 keys
[x
], key_length
[x
],
1768 (time_t)50, (uint32_t)9);
1769 assert(rc
== MEMCACHED_NO_SERVERS
);
1772 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1773 assert(rc
== MEMCACHED_NO_SERVERS
);
1776 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1777 &return_value_length
, &flags
, &rc
)))
1779 assert(return_value
);
1780 assert(rc
== MEMCACHED_SUCCESS
);
1781 assert(return_key_length
== return_value_length
);
1782 assert(!memcmp(return_value
, return_key
, return_value_length
));
1790 #define VALUE_SIZE_BUG5 1048064
1791 static test_return
user_supplied_bug5(memcached_st
*memc
)
1793 memcached_return rc
;
1794 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1795 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1796 char return_key
[MEMCACHED_MAX_KEY
];
1797 size_t return_key_length
;
1799 size_t value_length
;
1803 char insert_data
[VALUE_SIZE_BUG5
];
1805 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1806 insert_data
[x
]= (signed char)rand();
1808 memcached_flush(memc
, 0);
1809 value
= memcached_get(memc
, keys
[0], key_length
[0],
1810 &value_length
, &flags
, &rc
);
1811 assert(value
== NULL
);
1812 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1815 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1816 &value_length
, &flags
, &rc
)))
1820 for (x
= 0; x
< 4; x
++)
1822 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1823 insert_data
, VALUE_SIZE_BUG5
,
1824 (time_t)0, (uint32_t)0);
1825 assert(rc
== MEMCACHED_SUCCESS
);
1828 for (x
= 0; x
< 10; x
++)
1830 value
= memcached_get(memc
, keys
[0], key_length
[0],
1831 &value_length
, &flags
, &rc
);
1835 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1837 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1838 &value_length
, &flags
, &rc
)))
1849 static test_return
user_supplied_bug6(memcached_st
*memc
)
1851 memcached_return rc
;
1852 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1853 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1854 char return_key
[MEMCACHED_MAX_KEY
];
1855 size_t return_key_length
;
1857 size_t value_length
;
1861 char insert_data
[VALUE_SIZE_BUG5
];
1863 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1864 insert_data
[x
]= (signed char)rand();
1866 memcached_flush(memc
, 0);
1867 value
= memcached_get(memc
, keys
[0], key_length
[0],
1868 &value_length
, &flags
, &rc
);
1869 assert(value
== NULL
);
1870 assert(rc
== MEMCACHED_NOTFOUND
);
1871 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1872 assert(rc
== MEMCACHED_SUCCESS
);
1875 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1876 &value_length
, &flags
, &rc
)))
1879 assert(rc
== MEMCACHED_END
);
1881 for (x
= 0; x
< 4; x
++)
1883 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1884 insert_data
, VALUE_SIZE_BUG5
,
1885 (time_t)0, (uint32_t)0);
1886 assert(rc
== MEMCACHED_SUCCESS
);
1889 for (x
= 0; x
< 2; x
++)
1891 value
= memcached_get(memc
, keys
[0], key_length
[0],
1892 &value_length
, &flags
, &rc
);
1896 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1897 assert(rc
== MEMCACHED_SUCCESS
);
1899 /* We test for purge of partial complete fetches */
1900 for (count
= 3; count
; count
--)
1902 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1903 &value_length
, &flags
, &rc
);
1904 assert(rc
== MEMCACHED_SUCCESS
);
1905 assert(!(memcmp(value
, insert_data
, value_length
)));
1906 assert(value_length
);
1914 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1916 memcached_return rc
;
1918 memcached_st
*memc_clone
;
1920 memcached_server_st
*servers
;
1921 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";
1923 servers
= memcached_servers_parse(server_list
);
1926 mine
= memcached_create(NULL
);
1927 rc
= memcached_server_push(mine
, servers
);
1928 assert(rc
== MEMCACHED_SUCCESS
);
1929 memcached_server_list_free(servers
);
1932 memc_clone
= memcached_clone(NULL
, mine
);
1934 memcached_quit(mine
);
1935 memcached_quit(memc_clone
);
1938 memcached_free(mine
);
1939 memcached_free(memc_clone
);
1944 /* Test flag store/retrieve */
1945 static test_return
user_supplied_bug7(memcached_st
*memc
)
1947 memcached_return rc
;
1948 const char *keys
= "036790384900";
1949 size_t key_length
= strlen(keys
);
1950 char return_key
[MEMCACHED_MAX_KEY
];
1951 size_t return_key_length
;
1953 size_t value_length
;
1956 char insert_data
[VALUE_SIZE_BUG5
];
1958 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1959 insert_data
[x
]= (signed char)rand();
1961 memcached_flush(memc
, 0);
1964 rc
= memcached_set(memc
, keys
, key_length
,
1965 insert_data
, VALUE_SIZE_BUG5
,
1967 assert(rc
== MEMCACHED_SUCCESS
);
1970 value
= memcached_get(memc
, keys
, key_length
,
1971 &value_length
, &flags
, &rc
);
1972 assert(flags
== 245);
1976 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1979 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1980 &value_length
, &flags
, &rc
);
1981 assert(flags
== 245);
1989 static test_return
user_supplied_bug9(memcached_st
*memc
)
1991 memcached_return rc
;
1992 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1993 size_t key_length
[3];
1998 char return_key
[MEMCACHED_MAX_KEY
];
1999 size_t return_key_length
;
2001 size_t return_value_length
;
2004 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2005 key_length
[1]= strlen("fudge&*@#");
2006 key_length
[2]= strlen("for^#@&$not");
2009 for (x
= 0; x
< 3; x
++)
2011 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2012 keys
[x
], key_length
[x
],
2013 (time_t)50, (uint32_t)9);
2014 assert(rc
== MEMCACHED_SUCCESS
);
2017 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2018 assert(rc
== MEMCACHED_SUCCESS
);
2020 /* We need to empty the server before continueing test */
2021 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2022 &return_value_length
, &flags
, &rc
)) != NULL
)
2024 assert(return_value
);
2033 /* We are testing with aggressive timeout to get failures */
2034 static test_return
user_supplied_bug10(memcached_st
*memc
)
2036 const char *key
= "foo";
2038 size_t value_length
= 512;
2041 memcached_return rc
;
2042 unsigned int set
= 1;
2043 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2046 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2047 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2049 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2052 value
= (char*)malloc(value_length
* sizeof(char));
2054 for (x
= 0; x
< value_length
; x
++)
2055 value
[x
]= (char) (x
% 127);
2057 for (x
= 1; x
<= 100000; ++x
)
2059 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2061 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2062 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2064 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2069 memcached_free(mclone
);
2075 We are looking failures in the async protocol
2077 static test_return
user_supplied_bug11(memcached_st
*memc
)
2079 const char *key
= "foo";
2081 size_t value_length
= 512;
2084 memcached_return rc
;
2085 unsigned int set
= 1;
2087 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2089 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2090 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2092 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2095 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2097 assert(timeout
== -1);
2099 value
= (char*)malloc(value_length
* sizeof(char));
2101 for (x
= 0; x
< value_length
; x
++)
2102 value
[x
]= (char) (x
% 127);
2104 for (x
= 1; x
<= 100000; ++x
)
2106 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2110 memcached_free(mclone
);
2116 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2118 static test_return
user_supplied_bug12(memcached_st
*memc
)
2120 memcached_return rc
;
2122 size_t value_length
;
2124 uint64_t number_value
;
2126 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2127 &value_length
, &flags
, &rc
);
2128 assert(value
== NULL
);
2129 assert(rc
== MEMCACHED_NOTFOUND
);
2131 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2134 assert(value
== NULL
);
2135 /* The binary protocol will set the key if it doesn't exist */
2136 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2137 assert(rc
== MEMCACHED_SUCCESS
);
2139 assert(rc
== MEMCACHED_NOTFOUND
);
2141 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2143 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2144 &value_length
, &flags
, &rc
);
2146 assert(rc
== MEMCACHED_SUCCESS
);
2149 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2151 assert(number_value
== 2);
2152 assert(rc
== MEMCACHED_SUCCESS
);
2158 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2159 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2161 static test_return
user_supplied_bug13(memcached_st
*memc
)
2163 char key
[] = "key34567890";
2165 memcached_return rc
;
2166 size_t overflowSize
;
2168 char commandFirst
[]= "set key34567890 0 0 ";
2169 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2170 size_t commandLength
;
2173 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2175 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2177 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2179 overflow
= malloc(testSize
);
2180 assert(overflow
!= NULL
);
2182 memset(overflow
, 'x', testSize
);
2183 rc
= memcached_set(memc
, key
, strlen(key
),
2184 overflow
, testSize
, 0, 0);
2185 assert(rc
== MEMCACHED_SUCCESS
);
2194 Test values of many different sizes
2195 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2196 set key34567890 0 0 8169 \r\n
2197 is sent followed by buffer of size 8169, followed by 8169
2199 static test_return
user_supplied_bug14(memcached_st
*memc
)
2202 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2203 memcached_return rc
;
2204 const char *key
= "foo";
2206 size_t value_length
= 18000;
2208 size_t string_length
;
2211 size_t current_length
;
2213 value
= (char*)malloc(value_length
);
2216 for (x
= 0; x
< value_length
; x
++)
2217 value
[x
] = (char) (x
% 127);
2219 for (current_length
= 0; current_length
< value_length
; current_length
++)
2221 rc
= memcached_set(memc
, key
, strlen(key
),
2222 value
, current_length
,
2223 (time_t)0, (uint32_t)0);
2224 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2226 string
= memcached_get(memc
, key
, strlen(key
),
2227 &string_length
, &flags
, &rc
);
2229 assert(rc
== MEMCACHED_SUCCESS
);
2230 assert(string_length
== current_length
);
2231 assert(!memcmp(string
, value
, string_length
));
2242 Look for zero length value problems
2244 static test_return
user_supplied_bug15(memcached_st
*memc
)
2247 memcached_return rc
;
2248 const char *key
= "mykey";
2253 for (x
= 0; x
< 2; x
++)
2255 rc
= memcached_set(memc
, key
, strlen(key
),
2257 (time_t)0, (uint32_t)0);
2259 assert(rc
== MEMCACHED_SUCCESS
);
2261 value
= memcached_get(memc
, key
, strlen(key
),
2262 &length
, &flags
, &rc
);
2264 assert(rc
== MEMCACHED_SUCCESS
);
2265 assert(value
== NULL
);
2266 assert(length
== 0);
2269 value
= memcached_get(memc
, key
, strlen(key
),
2270 &length
, &flags
, &rc
);
2272 assert(rc
== MEMCACHED_SUCCESS
);
2273 assert(value
== NULL
);
2274 assert(length
== 0);
2281 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2282 static test_return
user_supplied_bug16(memcached_st
*memc
)
2284 memcached_return rc
;
2285 const char *key
= "mykey";
2290 rc
= memcached_set(memc
, key
, strlen(key
),
2292 (time_t)0, UINT32_MAX
);
2294 assert(rc
== MEMCACHED_SUCCESS
);
2296 value
= memcached_get(memc
, key
, strlen(key
),
2297 &length
, &flags
, &rc
);
2299 assert(rc
== MEMCACHED_SUCCESS
);
2300 assert(value
== NULL
);
2301 assert(length
== 0);
2302 assert(flags
== UINT32_MAX
);
2307 /* Check the validity of chinese key*/
2308 static test_return
user_supplied_bug17(memcached_st
*memc
)
2310 memcached_return rc
;
2311 const char *key
= "豆瓣";
2312 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2317 rc
= memcached_set(memc
, key
, strlen(key
),
2318 value
, strlen(value
),
2321 assert(rc
== MEMCACHED_SUCCESS
);
2323 value2
= memcached_get(memc
, key
, strlen(key
),
2324 &length
, &flags
, &rc
);
2326 assert(length
==strlen(value
));
2327 assert(rc
== MEMCACHED_SUCCESS
);
2328 assert(memcmp(value
, value2
, length
)==0);
2338 static test_return
user_supplied_bug19(memcached_st
*memc
)
2341 memcached_server_st
*s
;
2342 memcached_return res
;
2346 m
= memcached_create(NULL
);
2347 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2348 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2350 s
= memcached_server_by_key(m
, "a", 1, &res
);
2351 memcached_server_free(s
);
2358 /* CAS test from Andei */
2359 static test_return
user_supplied_bug20(memcached_st
*memc
)
2361 memcached_return status
;
2362 memcached_result_st
*result
, result_obj
;
2363 const char *key
= "abc";
2364 size_t key_len
= strlen("abc");
2365 const char *value
= "foobar";
2366 size_t value_len
= strlen(value
);
2368 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2370 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2371 assert(status
== MEMCACHED_SUCCESS
);
2373 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2374 assert(status
== MEMCACHED_SUCCESS
);
2376 result
= memcached_result_create(memc
, &result_obj
);
2379 memcached_result_create(memc
, &result_obj
);
2380 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2383 assert(status
== MEMCACHED_SUCCESS
);
2385 memcached_result_free(result
);
2390 #include "ketama_test_cases.h"
2391 static test_return
user_supplied_bug18(memcached_st
*trash
)
2393 memcached_return rc
;
2396 memcached_server_st
*server_pool
;
2401 memc
= memcached_create(NULL
);
2404 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2405 assert(rc
== MEMCACHED_SUCCESS
);
2407 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2410 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2411 assert(rc
== MEMCACHED_SUCCESS
);
2413 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2414 assert(value
== MEMCACHED_HASH_MD5
);
2416 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");
2417 memcached_server_push(memc
, server_pool
);
2419 /* verify that the server list was parsed okay. */
2420 assert(memc
->number_of_hosts
== 8);
2421 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2422 assert(server_pool
[0].port
== 11211);
2423 assert(server_pool
[0].weight
== 600);
2424 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2425 assert(server_pool
[2].port
== 11211);
2426 assert(server_pool
[2].weight
== 200);
2427 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2428 assert(server_pool
[7].port
== 11211);
2429 assert(server_pool
[7].weight
== 100);
2431 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2432 * us test the boundary wraparound.
2434 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2436 /* verify the standard ketama set. */
2437 for (x
= 0; x
< 99; x
++)
2439 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2440 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2441 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2444 memcached_server_list_free(server_pool
);
2445 memcached_free(memc
);
2450 static test_return
auto_eject_hosts(memcached_st
*trash
)
2454 memcached_return rc
;
2455 memcached_st
*memc
= memcached_create(NULL
);
2458 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2459 assert(rc
== MEMCACHED_SUCCESS
);
2461 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2464 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2465 assert(rc
== MEMCACHED_SUCCESS
);
2467 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2468 assert(value
== MEMCACHED_HASH_MD5
);
2470 /* server should be removed when in delay */
2471 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2472 assert(rc
== MEMCACHED_SUCCESS
);
2474 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2477 memcached_server_st
*server_pool
;
2478 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");
2479 memcached_server_push(memc
, server_pool
);
2481 /* verify that the server list was parsed okay. */
2482 assert(memc
->number_of_hosts
== 8);
2483 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2484 assert(server_pool
[0].port
== 11211);
2485 assert(server_pool
[0].weight
== 600);
2486 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2487 assert(server_pool
[2].port
== 11211);
2488 assert(server_pool
[2].weight
== 200);
2489 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2490 assert(server_pool
[7].port
== 11211);
2491 assert(server_pool
[7].weight
== 100);
2493 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2494 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2496 for (int x
= 0; x
< 99; x
++)
2498 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2499 assert(server_idx
!= 2);
2502 /* and re-added when it's back. */
2503 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2504 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2505 run_distribution(memc
);
2506 for (int x
= 0; x
< 99; x
++)
2508 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2509 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2510 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2513 memcached_server_list_free(server_pool
);
2514 memcached_free(memc
);
2516 return TEST_SUCCESS
;
2519 static test_return
result_static(memcached_st
*memc
)
2521 memcached_result_st result
;
2522 memcached_result_st
*result_ptr
;
2524 result_ptr
= memcached_result_create(memc
, &result
);
2525 assert(result
.is_allocated
== false);
2527 memcached_result_free(&result
);
2532 static test_return
result_alloc(memcached_st
*memc
)
2534 memcached_result_st
*result
;
2536 result
= memcached_result_create(memc
, NULL
);
2538 memcached_result_free(result
);
2543 static test_return
string_static_null(memcached_st
*memc
)
2545 memcached_string_st string
;
2546 memcached_string_st
*string_ptr
;
2548 string_ptr
= memcached_string_create(memc
, &string
, 0);
2549 assert(string
.is_allocated
== false);
2551 memcached_string_free(&string
);
2556 static test_return
string_alloc_null(memcached_st
*memc
)
2558 memcached_string_st
*string
;
2560 string
= memcached_string_create(memc
, NULL
, 0);
2562 memcached_string_free(string
);
2567 static test_return
string_alloc_with_size(memcached_st
*memc
)
2569 memcached_string_st
*string
;
2571 string
= memcached_string_create(memc
, NULL
, 1024);
2573 memcached_string_free(string
);
2578 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2580 memcached_string_st
*string
;
2582 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2583 assert(string
== NULL
);
2588 static test_return
string_alloc_append(memcached_st
*memc
)
2591 char buffer
[SMALL_STRING_LEN
];
2592 memcached_string_st
*string
;
2594 /* Ring the bell! */
2595 memset(buffer
, 6, SMALL_STRING_LEN
);
2597 string
= memcached_string_create(memc
, NULL
, 100);
2600 for (x
= 0; x
< 1024; x
++)
2602 memcached_return rc
;
2603 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2604 assert(rc
== MEMCACHED_SUCCESS
);
2606 memcached_string_free(string
);
2611 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2613 memcached_return rc
;
2615 char buffer
[SMALL_STRING_LEN
];
2616 memcached_string_st
*string
;
2618 /* Ring the bell! */
2619 memset(buffer
, 6, SMALL_STRING_LEN
);
2621 string
= memcached_string_create(memc
, NULL
, 100);
2624 for (x
= 0; x
< 1024; x
++)
2626 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2627 assert(rc
== MEMCACHED_SUCCESS
);
2629 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
2630 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2631 memcached_string_free(string
);
2636 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2638 pairs_free(global_pairs
);
2643 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2645 unsigned long long x
;
2646 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2647 global_count
= GLOBAL_COUNT
;
2649 for (x
= 0; x
< global_count
; x
++)
2651 global_keys
[x
]= global_pairs
[x
].key
;
2652 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2658 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2660 unsigned long long x
;
2661 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2662 global_count
= GLOBAL2_COUNT
;
2664 for (x
= 0; x
< global_count
; x
++)
2666 global_keys
[x
]= global_pairs
[x
].key
;
2667 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2673 static test_return
generate_data(memcached_st
*memc
)
2675 execute_set(memc
, global_pairs
, global_count
);
2680 static test_return
generate_data_with_stats(memcached_st
*memc
)
2682 memcached_stat_st
*stat_p
;
2683 memcached_return rc
;
2684 uint32_t host_index
= 0;
2685 execute_set(memc
, global_pairs
, global_count
);
2687 //TODO: hosts used size stats
2688 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2691 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2693 /* This test was changes so that "make test" would work properlly */
2695 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
);
2697 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
2700 memcached_stat_free(NULL
, stat_p
);
2704 static test_return
generate_buffer_data(memcached_st
*memc
)
2709 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2710 generate_data(memc
);
2715 static test_return
get_read_count(memcached_st
*memc
)
2718 memcached_return rc
;
2719 memcached_st
*memc_clone
;
2721 memc_clone
= memcached_clone(NULL
, memc
);
2724 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
2728 size_t return_value_length
;
2732 for (x
= count
= 0; x
< global_count
; x
++)
2734 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
2735 &return_value_length
, &flags
, &rc
);
2736 if (rc
== MEMCACHED_SUCCESS
)
2743 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2746 memcached_free(memc_clone
);
2751 static test_return
get_read(memcached_st
*memc
)
2754 memcached_return rc
;
2758 size_t return_value_length
;
2761 for (x
= 0; x
< global_count
; x
++)
2763 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2764 &return_value_length
, &flags
, &rc
);
2766 assert(return_value);
2767 assert(rc == MEMCACHED_SUCCESS);
2769 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2777 static test_return
mget_read(memcached_st
*memc
)
2779 memcached_return rc
;
2781 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2782 assert(rc
== MEMCACHED_SUCCESS
);
2783 /* Turn this into a help function */
2785 char return_key
[MEMCACHED_MAX_KEY
];
2786 size_t return_key_length
;
2788 size_t return_value_length
;
2791 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2792 &return_value_length
, &flags
, &rc
)))
2794 assert(return_value
);
2795 assert(rc
== MEMCACHED_SUCCESS
);
2803 static test_return
mget_read_result(memcached_st
*memc
)
2805 memcached_return rc
;
2807 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2808 assert(rc
== MEMCACHED_SUCCESS
);
2809 /* Turn this into a help function */
2811 memcached_result_st results_obj
;
2812 memcached_result_st
*results
;
2814 results
= memcached_result_create(memc
, &results_obj
);
2816 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2819 assert(rc
== MEMCACHED_SUCCESS
);
2822 memcached_result_free(&results_obj
);
2828 static test_return
mget_read_function(memcached_st
*memc
)
2830 memcached_return rc
;
2831 unsigned int counter
;
2832 memcached_execute_function callbacks
[1];
2834 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2835 assert(rc
== MEMCACHED_SUCCESS
);
2837 callbacks
[0]= &callback_counter
;
2839 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2844 static test_return
delete_generate(memcached_st
*memc
)
2848 for (x
= 0; x
< global_count
; x
++)
2850 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2856 static test_return
delete_buffer_generate(memcached_st
*memc
)
2862 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2864 for (x
= 0; x
< global_count
; x
++)
2866 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2872 static test_return
add_host_test1(memcached_st
*memc
)
2875 memcached_return rc
;
2876 char servername
[]= "0.example.com";
2877 memcached_server_st
*servers
;
2879 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2881 assert(1 == memcached_server_list_count(servers
));
2883 for (x
= 2; x
< 20; x
++)
2885 char buffer
[SMALL_STRING_LEN
];
2887 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2888 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2890 assert(rc
== MEMCACHED_SUCCESS
);
2891 assert(x
== memcached_server_list_count(servers
));
2894 rc
= memcached_server_push(memc
, servers
);
2895 assert(rc
== MEMCACHED_SUCCESS
);
2896 rc
= memcached_server_push(memc
, servers
);
2897 assert(rc
== MEMCACHED_SUCCESS
);
2899 memcached_server_list_free(servers
);
2904 static memcached_return
pre_nonblock(memcached_st
*memc
)
2906 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2908 return MEMCACHED_SUCCESS
;
2911 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
2913 memcached_return rc
= MEMCACHED_FAILURE
;
2914 memcached_st
*memc_clone
;
2916 memc_clone
= memcached_clone(NULL
, memc
);
2918 // The memcached_version needs to be done on a clone, because the server
2919 // will not toggle protocol on an connection.
2920 memcached_version(memc_clone
);
2922 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
2924 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2925 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2926 assert(rc
== MEMCACHED_SUCCESS
);
2927 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2930 memcached_free(memc_clone
);
2934 static memcached_return
pre_murmur(memcached_st
*memc
)
2936 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2938 return MEMCACHED_SUCCESS
;
2941 static memcached_return
pre_jenkins(memcached_st
*memc
)
2943 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
2945 return MEMCACHED_SUCCESS
;
2949 static memcached_return
pre_md5(memcached_st
*memc
)
2951 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2953 return MEMCACHED_SUCCESS
;
2956 static memcached_return
pre_crc(memcached_st
*memc
)
2958 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2960 return MEMCACHED_SUCCESS
;
2963 static memcached_return
pre_hsieh(memcached_st
*memc
)
2965 #ifdef HAVE_HSIEH_HASH
2966 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2967 return MEMCACHED_SUCCESS
;
2970 return MEMCACHED_FAILURE
;
2974 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2976 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2978 return MEMCACHED_SUCCESS
;
2981 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2983 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2985 return MEMCACHED_SUCCESS
;
2988 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2990 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2992 return MEMCACHED_SUCCESS
;
2995 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2997 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2999 return MEMCACHED_SUCCESS
;
3002 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3004 memcached_return rc
;
3007 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3008 assert(rc
== MEMCACHED_SUCCESS
);
3010 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3013 return MEMCACHED_SUCCESS
;
3016 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3018 memcached_return rc
;
3021 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3022 assert(rc
== MEMCACHED_SUCCESS
);
3024 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3027 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3028 assert(rc
== MEMCACHED_SUCCESS
);
3030 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3031 assert(value
== MEMCACHED_HASH_MD5
);
3032 return MEMCACHED_SUCCESS
;
3035 static memcached_return
pre_binary(memcached_st
*memc
)
3037 memcached_return rc
= MEMCACHED_FAILURE
;
3038 memcached_st
*memc_clone
;
3040 memc_clone
= memcached_clone(NULL
, memc
);
3042 // The memcached_version needs to be done on a clone, because the server
3043 // will not toggle protocol on an connection.
3044 memcached_version(memc_clone
);
3046 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3048 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3049 assert(rc
== MEMCACHED_SUCCESS
);
3050 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3053 memcached_free(memc_clone
);
3057 static memcached_return
pre_replication(memcached_st
*memc
)
3059 memcached_return rc
= MEMCACHED_FAILURE
;
3060 if (pre_binary(memc
) == MEMCACHED_SUCCESS
)
3063 * Make sure that we store the item on all servers
3064 * (master + replicas == number of servers)
3066 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3067 memc
->number_of_hosts
- 1);
3068 assert(rc
== MEMCACHED_SUCCESS
);
3069 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3075 static memcached_return
pre_replication_noblock(memcached_st
*memc
)
3077 memcached_return rc
= MEMCACHED_FAILURE
;
3078 if (pre_replication(memc
) == MEMCACHED_SUCCESS
&&
3079 pre_nonblock(memc
) == MEMCACHED_SUCCESS
)
3080 rc
= MEMCACHED_SUCCESS
;
3085 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3090 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3092 void *ret
= malloc(size
);
3094 memset(ret
, 0xff, size
);
3099 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3101 return realloc(mem
, size
);
3104 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3106 return calloc(nelem
, size
);
3109 static memcached_return
set_prefix(memcached_st
*memc
)
3111 memcached_return rc
;
3112 const char *key
= "mine";
3115 /* Make sure be default none exists */
3116 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3117 assert(rc
== MEMCACHED_FAILURE
);
3119 /* Test a clean set */
3120 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3121 assert(rc
== MEMCACHED_SUCCESS
);
3123 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3124 assert(memcmp(value
, key
, 4) == 0);
3125 assert(rc
== MEMCACHED_SUCCESS
);
3127 /* Test that we can turn it off */
3128 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3129 assert(rc
== MEMCACHED_SUCCESS
);
3131 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3132 assert(rc
== MEMCACHED_FAILURE
);
3134 /* Now setup for main test */
3135 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3136 assert(rc
== MEMCACHED_SUCCESS
);
3138 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3139 assert(rc
== MEMCACHED_SUCCESS
);
3140 assert(memcmp(value
, key
, 4) == 0);
3142 /* Set to Zero, and then Set to something too large */
3145 memset(long_key
, 0, 255);
3147 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3148 assert(rc
== MEMCACHED_SUCCESS
);
3150 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3151 assert(rc
== MEMCACHED_FAILURE
);
3152 assert(value
== NULL
);
3154 /* Test a long key for failure */
3155 /* TODO, extend test to determine based on setting, what result should be */
3156 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3157 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3158 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3159 assert(rc
== MEMCACHED_SUCCESS
);
3161 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3162 strcpy(long_key
, "This is more then the allotted number of characters");
3163 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3164 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3166 /* Test for a bad prefix, but with a short key */
3167 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3168 assert(rc
== MEMCACHED_SUCCESS
);
3170 strcpy(long_key
, "dog cat");
3171 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3172 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3175 return MEMCACHED_SUCCESS
;
3178 #ifdef MEMCACHED_ENABLE_DEPRECATED
3179 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3181 void *test_ptr
= NULL
;
3184 memcached_malloc_function malloc_cb
=
3185 (memcached_malloc_function
)my_malloc
;
3186 cb_ptr
= *(void **)&malloc_cb
;
3187 memcached_return rc
;
3189 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3190 assert(rc
== MEMCACHED_SUCCESS
);
3191 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3192 assert(rc
== MEMCACHED_SUCCESS
);
3193 assert(test_ptr
== cb_ptr
);
3197 memcached_realloc_function realloc_cb
=
3198 (memcached_realloc_function
)my_realloc
;
3199 cb_ptr
= *(void **)&realloc_cb
;
3200 memcached_return rc
;
3202 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3203 assert(rc
== MEMCACHED_SUCCESS
);
3204 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3205 assert(rc
== MEMCACHED_SUCCESS
);
3206 assert(test_ptr
== cb_ptr
);
3210 memcached_free_function free_cb
=
3211 (memcached_free_function
)my_free
;
3212 cb_ptr
= *(void **)&free_cb
;
3213 memcached_return rc
;
3215 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3216 assert(rc
== MEMCACHED_SUCCESS
);
3217 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3218 assert(rc
== MEMCACHED_SUCCESS
);
3219 assert(test_ptr
== cb_ptr
);
3221 return MEMCACHED_SUCCESS
;
3225 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3227 memcached_return rc
;
3228 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3229 my_realloc
, my_calloc
);
3230 assert(rc
== MEMCACHED_FAILURE
);
3232 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3233 my_realloc
, my_calloc
);
3235 memcached_malloc_function mem_malloc
;
3236 memcached_free_function mem_free
;
3237 memcached_realloc_function mem_realloc
;
3238 memcached_calloc_function mem_calloc
;
3239 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3240 &mem_realloc
, &mem_calloc
);
3242 assert(mem_malloc
== my_malloc
);
3243 assert(mem_realloc
== my_realloc
);
3244 assert(mem_calloc
== my_calloc
);
3245 assert(mem_free
== my_free
);
3247 return MEMCACHED_SUCCESS
;
3250 static memcached_return
enable_consistent(memcached_st
*memc
)
3252 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3253 memcached_hash hash
;
3254 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3255 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3256 return MEMCACHED_FAILURE
;
3258 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3259 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3261 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3262 assert(hash
== MEMCACHED_HASH_HSIEH
);
3265 return MEMCACHED_SUCCESS
;
3268 static memcached_return
enable_cas(memcached_st
*memc
)
3270 unsigned int set
= 1;
3272 memcached_version(memc
);
3274 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3275 || memc
->hosts
[0].minor_version
> 2)
3277 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3279 return MEMCACHED_SUCCESS
;
3282 return MEMCACHED_FAILURE
;
3285 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3287 memcached_version(memc
);
3289 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3290 || memc
->hosts
[0].minor_version
> 2)
3291 return MEMCACHED_SUCCESS
;
3293 return MEMCACHED_FAILURE
;
3296 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3298 memcached_return rc
;
3301 memcached_server_list_free(memc
->hosts
);
3303 memc
->number_of_hosts
= 0;
3305 if (stat("/tmp/memcached.socket", &buf
))
3306 return MEMCACHED_FAILURE
;
3308 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3313 static memcached_return
pre_nodelay(memcached_st
*memc
)
3315 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3316 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3318 return MEMCACHED_SUCCESS
;
3321 static memcached_return
pre_settimer(memcached_st
*memc
)
3323 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3324 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3326 return MEMCACHED_SUCCESS
;
3329 static memcached_return
poll_timeout(memcached_st
*memc
)
3335 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3337 timeout
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3339 assert(timeout
== 100);
3341 return MEMCACHED_SUCCESS
;
3344 static test_return
noreply_test(memcached_st
*memc
)
3346 memcached_return ret
;
3347 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3348 assert(ret
== MEMCACHED_SUCCESS
);
3349 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3350 assert(ret
== MEMCACHED_SUCCESS
);
3351 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3352 assert(ret
== MEMCACHED_SUCCESS
);
3353 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3354 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3355 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3357 for (int count
=0; count
< 5; ++count
)
3359 for (int x
=0; x
< 100; ++x
)
3362 size_t len
= (size_t)sprintf(key
, "%d", x
);
3366 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3369 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3372 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3375 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3378 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3384 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3388 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3389 ** API and is _ONLY_ done this way to verify that the library works the
3390 ** way it is supposed to do!!!!
3393 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3394 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3396 assert(no_msg
== 0);
3397 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3400 ** Now validate that all items was set properly!
3402 for (int x
=0; x
< 100; ++x
)
3405 size_t len
= (size_t)sprintf(key
, "%d", x
);
3408 char* value
=memcached_get(memc
, key
, strlen(key
),
3409 &length
, &flags
, &ret
);
3410 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3413 case 0: /* FALLTHROUGH */
3414 case 1: /* FALLTHROUGH */
3416 assert(strncmp(value
, key
, len
) == 0);
3417 assert(len
== length
);
3420 assert(length
== len
* 2);
3423 assert(length
== len
* 3);
3433 /* Try setting an illegal cas value (should not return an error to
3434 * the caller (because we don't expect a return message from the server)
3436 const char* keys
[]= {"0"};
3437 size_t lengths
[]= {1};
3440 memcached_result_st results_obj
;
3441 memcached_result_st
*results
;
3442 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3443 assert(ret
== MEMCACHED_SUCCESS
);
3445 results
= memcached_result_create(memc
, &results_obj
);
3447 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3449 assert(ret
== MEMCACHED_SUCCESS
);
3450 uint64_t cas
= memcached_result_cas(results
);
3451 memcached_result_free(&results_obj
);
3453 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3454 assert(ret
== MEMCACHED_SUCCESS
);
3457 * The item will have a new cas value, so try to set it again with the old
3458 * value. This should fail!
3460 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3461 assert(ret
== MEMCACHED_SUCCESS
);
3462 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3463 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3464 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3467 return TEST_SUCCESS
;
3470 static test_return
analyzer_test(memcached_st
*memc
)
3472 memcached_return rc
;
3473 memcached_stat_st
*memc_stat
;
3474 memcached_analysis_st
*report
;
3476 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3477 assert(rc
== MEMCACHED_SUCCESS
);
3480 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3481 assert(rc
== MEMCACHED_SUCCESS
);
3485 memcached_stat_free(NULL
, memc_stat
);
3487 return TEST_SUCCESS
;
3490 /* Count the objects */
3491 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3492 const char *key
__attribute__((unused
)),
3493 size_t key_length
__attribute__((unused
)),
3496 uint32_t *counter
= (uint32_t *)context
;
3498 *counter
= *counter
+ 1;
3500 return MEMCACHED_SUCCESS
;
3503 static test_return
dump_test(memcached_st
*memc
)
3505 memcached_return rc
;
3506 uint32_t counter
= 0;
3507 memcached_dump_func callbacks
[1];
3508 test_return main_rc
;
3510 callbacks
[0]= &callback_dump_counter
;
3512 /* No support for Binary protocol yet */
3513 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3514 return TEST_SUCCESS
;
3516 main_rc
= set_test3(memc
);
3518 assert (main_rc
== TEST_SUCCESS
);
3520 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3521 assert(rc
== MEMCACHED_SUCCESS
);
3523 /* We may have more then 32 if our previous flush has not completed */
3524 assert(counter
>= 32);
3526 return TEST_SUCCESS
;
3529 #ifdef HAVE_LIBMEMCACHEDUTIL
3530 static void* connection_release(void *arg
) {
3532 memcached_pool_st
* pool
;
3537 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3541 static test_return
connection_pool_test(memcached_st
*memc
)
3543 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3544 assert(pool
!= NULL
);
3545 memcached_st
* mmc
[10];
3546 memcached_return rc
;
3548 for (int x
= 0; x
< 10; ++x
) {
3549 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3550 assert(mmc
[x
] != NULL
);
3551 assert(rc
== MEMCACHED_SUCCESS
);
3554 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3555 assert(rc
== MEMCACHED_SUCCESS
);
3559 memcached_pool_st
* pool
;
3561 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3562 pthread_create(&tid
, NULL
, connection_release
, &item
);
3563 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3564 assert(rc
== MEMCACHED_SUCCESS
);
3565 pthread_join(tid
, NULL
);
3566 assert(mmc
[9] == item
.mmc
);
3567 const char *key
= "key";
3568 size_t keylen
= strlen(key
);
3570 // verify that I can do ops with all connections
3571 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3572 assert(rc
== MEMCACHED_SUCCESS
);
3574 for (unsigned int x
= 0; x
< 10; ++x
) {
3575 uint64_t number_value
;
3576 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3577 assert(rc
== MEMCACHED_SUCCESS
);
3578 assert(number_value
== (x
+1));
3582 for (int x
= 0; x
< 10; ++x
)
3583 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3585 assert(memcached_pool_destroy(pool
) == memc
);
3586 return TEST_SUCCESS
;
3590 static test_return
replication_set_test(memcached_st
*memc
)
3592 memcached_return rc
;
3593 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3594 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3596 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3597 assert(rc
== MEMCACHED_SUCCESS
);
3600 ** We are using the quiet commands to store the replicas, so we need
3601 ** to ensure that all of them are processed before we can continue.
3602 ** In the test we go directly from storing the object to trying to
3603 ** receive the object from all of the different servers, so we
3604 ** could end up in a race condition (the memcached server hasn't yet
3605 ** processed the quiet command from the replication set when it process
3606 ** the request from the other client (created by the clone)). As a
3607 ** workaround for that we call memcached_quit to send the quit command
3608 ** to the server and wait for the response ;-) If you use the test code
3609 ** as an example for your own code, please note that you shouldn't need
3612 memcached_quit(memc
);
3615 ** "bubba" should now be stored on all of our servers. We don't have an
3616 ** easy to use API to address each individual server, so I'll just iterate
3617 ** through a bunch of "master keys" and I should most likely hit all of the
3620 for (int x
= 'a'; x
<= 'z'; ++x
)
3622 char key
[2]= { [0]= (char)x
};
3625 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3627 assert(rc
== MEMCACHED_SUCCESS
);
3628 assert(val
!= NULL
);
3632 memcached_free(memc_clone
);
3634 return TEST_SUCCESS
;
3637 static test_return
replication_get_test(memcached_st
*memc
)
3639 memcached_return rc
;
3642 * Don't do the following in your code. I am abusing the internal details
3643 * within the library, and this is not a supported interface.
3644 * This is to verify correct behavior in the library
3646 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
3648 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3649 memc_clone
->hosts
[host
].port
= 0;
3651 for (int x
= 'a'; x
<= 'z'; ++x
)
3653 char key
[2]= { [0]= (char)x
};
3656 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3658 assert(rc
== MEMCACHED_SUCCESS
);
3659 assert(val
!= NULL
);
3663 memcached_free(memc_clone
);
3666 return TEST_SUCCESS
;
3669 static test_return
replication_mget_test(memcached_st
*memc
)
3671 memcached_return rc
;
3672 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3673 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3675 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3676 size_t len
[]= { 5, 4, 4, 4 };
3678 for (int x
=0; x
< 4; ++x
)
3680 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3681 assert(rc
== MEMCACHED_SUCCESS
);
3685 ** We are using the quiet commands to store the replicas, so we need
3686 ** to ensure that all of them are processed before we can continue.
3687 ** In the test we go directly from storing the object to trying to
3688 ** receive the object from all of the different servers, so we
3689 ** could end up in a race condition (the memcached server hasn't yet
3690 ** processed the quiet command from the replication set when it process
3691 ** the request from the other client (created by the clone)). As a
3692 ** workaround for that we call memcached_quit to send the quit command
3693 ** to the server and wait for the response ;-) If you use the test code
3694 ** as an example for your own code, please note that you shouldn't need
3697 memcached_quit(memc
);
3700 * Don't do the following in your code. I am abusing the internal details
3701 * within the library, and this is not a supported interface.
3702 * This is to verify correct behavior in the library
3704 memcached_result_st result_obj
;
3705 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
3707 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
3708 new_clone
->hosts
[host
].port
= 0;
3710 for (int x
= 'a'; x
<= 'z'; ++x
)
3712 const char key
[2]= { [0]= (const char)x
};
3714 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
3715 assert(rc
== MEMCACHED_SUCCESS
);
3717 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
3721 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
3726 memcached_result_free(&result_obj
);
3729 memcached_free(new_clone
);
3732 return TEST_SUCCESS
;
3735 static test_return
replication_delete_test(memcached_st
*memc
)
3737 memcached_return rc
;
3738 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3739 /* Delete the items from all of the servers except 1 */
3740 uint64_t repl
= memcached_behavior_get(memc
,
3741 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3742 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3744 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3745 size_t len
[]= { 5, 4, 4, 4 };
3747 for (int x
=0; x
< 4; ++x
)
3749 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
3750 assert(rc
== MEMCACHED_SUCCESS
);
3754 * Don't do the following in your code. I am abusing the internal details
3755 * within the library, and this is not a supported interface.
3756 * This is to verify correct behavior in the library
3758 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
3759 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
3761 memc_clone
->hosts
[hash
].port
= 0;
3762 if (++hash
== memc_clone
->number_of_hosts
)
3766 memcached_result_st result_obj
;
3767 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
3769 for (int x
= 'a'; x
<= 'z'; ++x
)
3771 const char key
[2]= { [0]= (const char)x
};
3773 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
3774 assert(rc
== MEMCACHED_SUCCESS
);
3776 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
3780 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
3785 memcached_result_free(&result_obj
);
3788 memcached_free(memc_clone
);
3790 return TEST_SUCCESS
;
3793 static void increment_request_id(uint16_t *id
)
3796 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
3800 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
3802 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
3803 assert(ids
!= NULL
);
3806 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3807 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
3812 static test_return
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
3815 memcached_server_st
*cur_server
= memc
->hosts
;
3816 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
3818 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3820 assert(cur_server
[x
].cursor_active
== 0);
3821 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
3823 free(expected_req_ids
);
3826 return TEST_SUCCESS
;
3830 ** There is a little bit of a hack here, instead of removing
3831 ** the servers, I just set num host to 0 and them add then new udp servers
3833 static memcached_return
init_udp(memcached_st
*memc
)
3835 memcached_version(memc
);
3836 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3837 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
3838 || memc
->hosts
[0].micro_version
< 6)
3839 return MEMCACHED_FAILURE
;
3841 uint32_t num_hosts
= memc
->number_of_hosts
;
3843 memcached_server_st servers
[num_hosts
];
3844 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
3845 for (x
= 0; x
< num_hosts
; x
++)
3846 memcached_server_free(&memc
->hosts
[x
]);
3848 memc
->number_of_hosts
= 0;
3849 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
3850 for (x
= 0; x
< num_hosts
; x
++)
3852 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
3853 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3856 return MEMCACHED_SUCCESS
;
3859 static memcached_return
binary_init_udp(memcached_st
*memc
)
3862 return init_udp(memc
);
3865 /* Make sure that I cant add a tcp server to a udp client */
3866 static test_return
add_tcp_server_udp_client_test(memcached_st
*memc
)
3868 memcached_server_st server
;
3869 memcached_server_clone(&server
, &memc
->hosts
[0]);
3870 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3871 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3872 return TEST_SUCCESS
;
3875 /* Make sure that I cant add a udp server to a tcp client */
3876 static test_return
add_udp_server_tcp_client_test(memcached_st
*memc
)
3878 memcached_server_st server
;
3879 memcached_server_clone(&server
, &memc
->hosts
[0]);
3880 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3882 memcached_st tcp_client
;
3883 memcached_create(&tcp_client
);
3884 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3885 return TEST_SUCCESS
;
3888 static test_return
set_udp_behavior_test(memcached_st
*memc
)
3891 memcached_quit(memc
);
3892 memc
->number_of_hosts
= 0;
3893 run_distribution(memc
);
3894 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
3895 assert(memc
->flags
& MEM_USE_UDP
);
3896 assert(memc
->flags
& MEM_NOREPLY
);;
3898 assert(memc
->number_of_hosts
== 0);
3900 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
3901 assert(!(memc
->flags
& MEM_USE_UDP
));
3902 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
3903 assert(!(memc
->flags
& MEM_NOREPLY
));
3904 return TEST_SUCCESS
;
3907 static test_return
udp_set_test(memcached_st
*memc
)
3910 unsigned int num_iters
= 1025; //request id rolls over at 1024
3911 for (x
= 0; x
< num_iters
;x
++)
3913 memcached_return rc
;
3914 const char *key
= "foo";
3915 const char *value
= "when we sanitize";
3916 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3917 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
3918 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3919 rc
= memcached_set(memc
, key
, strlen(key
),
3920 value
, strlen(value
),
3921 (time_t)0, (uint32_t)0);
3922 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3923 /** NB, the check below assumes that if new write_ptr is less than
3924 * the original write_ptr that we have flushed. For large payloads, this
3925 * maybe an invalid assumption, but for the small payload we have it is OK
3927 if (rc
== MEMCACHED_SUCCESS
||
3928 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3929 increment_request_id(&expected_ids
[server_key
]);
3931 if (rc
== MEMCACHED_SUCCESS
)
3933 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3937 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3938 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3940 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3942 return TEST_SUCCESS
;
3945 static test_return
udp_buffered_set_test(memcached_st
*memc
)
3947 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3948 return udp_set_test(memc
);
3951 static test_return
udp_set_too_big_test(memcached_st
*memc
)
3953 memcached_return rc
;
3954 const char *key
= "bar";
3955 char value
[MAX_UDP_DATAGRAM_LENGTH
];
3956 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3957 rc
= memcached_set(memc
, key
, strlen(key
),
3958 value
, MAX_UDP_DATAGRAM_LENGTH
,
3959 (time_t)0, (uint32_t)0);
3960 assert(rc
== MEMCACHED_WRITE_FAILURE
);
3961 return post_udp_op_check(memc
,expected_ids
);
3964 static test_return
udp_delete_test(memcached_st
*memc
)
3967 unsigned int num_iters
= 1025; //request id rolls over at 1024
3968 for (x
= 0; x
< num_iters
;x
++)
3970 memcached_return rc
;
3971 const char *key
= "foo";
3972 uint16_t *expected_ids
=get_udp_request_ids(memc
);
3973 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3974 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3975 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
3976 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3977 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3978 increment_request_id(&expected_ids
[server_key
]);
3979 if (rc
== MEMCACHED_SUCCESS
)
3980 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3983 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3984 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3986 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3988 return TEST_SUCCESS
;
3991 static test_return
udp_buffered_delete_test(memcached_st
*memc
)
3993 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3994 return udp_delete_test(memc
);
3997 static test_return
udp_verbosity_test(memcached_st
*memc
)
3999 memcached_return rc
;
4000 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4002 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4003 increment_request_id(&expected_ids
[x
]);
4005 rc
= memcached_verbosity(memc
,3);
4006 assert(rc
== MEMCACHED_SUCCESS
);
4007 return post_udp_op_check(memc
,expected_ids
);
4010 static test_return
udp_quit_test(memcached_st
*memc
)
4012 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4013 memcached_quit(memc
);
4014 return post_udp_op_check(memc
, expected_ids
);
4017 static test_return
udp_flush_test(memcached_st
*memc
)
4019 memcached_return rc
;
4020 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4022 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4023 increment_request_id(&expected_ids
[x
]);
4025 rc
= memcached_flush(memc
,0);
4026 assert(rc
== MEMCACHED_SUCCESS
);
4027 return post_udp_op_check(memc
,expected_ids
);
4030 static test_return
udp_incr_test(memcached_st
*memc
)
4032 memcached_return rc
;
4033 const char *key
= "incr";
4034 const char *value
= "1";
4035 rc
= memcached_set(memc
, key
, strlen(key
),
4036 value
, strlen(value
),
4037 (time_t)0, (uint32_t)0);
4039 assert(rc
== MEMCACHED_SUCCESS
);
4040 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4041 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4042 increment_request_id(&expected_ids
[server_key
]);
4044 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4045 assert(rc
== MEMCACHED_SUCCESS
);
4046 return post_udp_op_check(memc
, expected_ids
);
4049 static test_return
udp_decr_test(memcached_st
*memc
)
4051 memcached_return rc
;
4052 const char *key
= "decr";
4053 const char *value
= "1";
4054 rc
= memcached_set(memc
, key
, strlen(key
),
4055 value
, strlen(value
),
4056 (time_t)0, (uint32_t)0);
4058 assert(rc
== MEMCACHED_SUCCESS
);
4059 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4060 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4061 increment_request_id(&expected_ids
[server_key
]);
4063 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4064 assert(rc
== MEMCACHED_SUCCESS
);
4065 return post_udp_op_check(memc
, expected_ids
);
4069 static test_return
udp_stat_test(memcached_st
*memc
)
4071 memcached_stat_st
* rv
= NULL
;
4072 memcached_return rc
;
4074 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4075 rv
= memcached_stat(memc
, args
, &rc
);
4077 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4078 return post_udp_op_check(memc
, expected_ids
);
4081 static test_return
udp_version_test(memcached_st
*memc
)
4083 memcached_return rc
;
4084 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4085 rc
= memcached_version(memc
);
4086 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4087 return post_udp_op_check(memc
, expected_ids
);
4090 static test_return
udp_get_test(memcached_st
*memc
)
4092 memcached_return rc
;
4093 const char *key
= "foo";
4095 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4096 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4097 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4098 assert(val
== NULL
);
4099 return post_udp_op_check(memc
, expected_ids
);
4102 static test_return
udp_mixed_io_test(memcached_st
*memc
)
4105 test_st mixed_io_ops
[] ={
4106 {"udp_set_test", 0, udp_set_test
},
4107 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4108 {"udp_delete_test", 0, udp_delete_test
},
4109 {"udp_verbosity_test", 0, udp_verbosity_test
},
4110 {"udp_quit_test", 0, udp_quit_test
},
4111 {"udp_flush_test", 0, udp_flush_test
},
4112 {"udp_incr_test", 0, udp_incr_test
},
4113 {"udp_decr_test", 0, udp_decr_test
},
4114 {"udp_version_test", 0, udp_version_test
}
4117 for (x
= 0; x
< 500; x
++)
4119 current_op
= mixed_io_ops
[random() % 9];
4120 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4122 return TEST_SUCCESS
;
4125 static test_return
hsieh_avaibility_test (memcached_st
*memc
)
4127 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4128 #ifdef HAVE_HSIEH_HASH
4129 expected_rc
= MEMCACHED_SUCCESS
;
4131 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4132 (uint64_t)MEMCACHED_HASH_HSIEH
);
4133 assert(rc
== expected_rc
);
4134 return TEST_SUCCESS
;
4137 static const char *list
[]=
4167 static test_return
md5_run (memcached_st
*memc
__attribute__((unused
)))
4171 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4172 245758794U, 2550894432U, 121710495U, 3053817768U,
4173 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4174 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4175 2138080750U, 217675895U, 2230934345U, 1234361223U,
4176 3968582726U, 2455685270U, 1293568479U, 199067604U,
4180 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4184 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4185 assert(values
[x
] == hash_val
);
4188 return TEST_SUCCESS
;
4191 static test_return
crc_run (memcached_st
*memc
__attribute__((unused
)))
4195 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4196 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4197 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4198 17300U, 28156U, 9192U, 20484U, 16925U };
4200 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4204 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4205 assert(values
[x
] == hash_val
);
4208 return TEST_SUCCESS
;
4211 static test_return
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4215 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4216 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4217 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4218 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4219 2815549194U, 2562818319U, 224996066U, 2680194749U,
4220 3035305390U, 246890365U, 2395624193U, 4145193337U,
4223 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4227 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4228 assert(values
[x
] == hash_val
);
4231 return TEST_SUCCESS
;
4234 static test_return
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4238 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4239 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4240 734810122U, 1516407546U, 560948863U, 1767346780U,
4241 561034892U, 4156330026U, 3716417003U, 3475297030U,
4242 1518272172U, 227211583U, 3938128828U, 126112909U,
4243 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4246 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4250 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4251 assert(values
[x
] == hash_val
);
4254 return TEST_SUCCESS
;
4257 static test_return
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4261 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4262 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4263 344481048U, 3865235296U, 2181839183U, 119581266U,
4264 510234242U, 4248244304U, 1362796839U, 103389328U,
4265 1449620010U, 182962511U, 3554262370U, 3206747549U,
4266 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4270 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4274 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4275 assert(values
[x
] == hash_val
);
4278 return TEST_SUCCESS
;
4281 static test_return
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4285 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4286 459261019U, 3521085446U, 18738364U, 1625305005U,
4287 2162232970U, 777243802U, 3323728671U, 132336572U,
4288 3654473228U, 260679466U, 1169454059U, 2698319462U,
4289 1062177260U, 235516991U, 2218399068U, 405302637U,
4290 1128467232U, 3579622413U, 2138539289U, 96429129U,
4293 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4297 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4298 assert(values
[x
] == hash_val
);
4301 return TEST_SUCCESS
;
4304 static test_return
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4308 #ifdef HAVE_HSIEH_HASH
4309 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4310 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4311 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4312 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4315 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 };
4318 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4322 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4323 assert(values
[x
] == hash_val
);
4326 return TEST_SUCCESS
;
4329 static test_return
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4333 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4334 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4335 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4336 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4337 2815549194U, 2562818319U, 224996066U, 2680194749U,
4338 3035305390U, 246890365U, 2395624193U, 4145193337U,
4341 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4345 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4346 assert(values
[x
] == hash_val
);
4349 return TEST_SUCCESS
;
4352 static test_return
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4356 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4357 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4358 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4359 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4360 4153263658U, 3824871822U, 3072759809U, 798622255U,
4361 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4365 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4369 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4370 assert(values
[x
] == hash_val
);
4373 return TEST_SUCCESS
;
4376 test_st udp_setup_server_tests
[] ={
4377 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
4378 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
4379 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
4383 test_st upd_io_tests
[] ={
4384 {"udp_set_test", 0, udp_set_test
},
4385 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
4386 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4387 {"udp_delete_test", 0, udp_delete_test
},
4388 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
4389 {"udp_verbosity_test", 0, udp_verbosity_test
},
4390 {"udp_quit_test", 0, udp_quit_test
},
4391 {"udp_flush_test", 0, udp_flush_test
},
4392 {"udp_incr_test", 0, udp_incr_test
},
4393 {"udp_decr_test", 0, udp_decr_test
},
4394 {"udp_stat_test", 0, udp_stat_test
},
4395 {"udp_version_test", 0, udp_version_test
},
4396 {"udp_get_test", 0, udp_get_test
},
4397 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
4401 /* Clean the server before beginning testing */
4403 {"flush", 0, flush_test
},
4404 {"init", 0, init_test
},
4405 {"allocation", 0, allocation_test
},
4406 {"server_list_null_test", 0, server_list_null_test
},
4407 {"server_unsort", 0, server_unsort_test
},
4408 {"server_sort", 0, server_sort_test
},
4409 {"server_sort2", 0, server_sort2_test
},
4410 {"clone_test", 0, clone_test
},
4411 {"connection_test", 0, connection_test
},
4412 {"callback_test", 0, callback_test
},
4413 {"behavior_test", 0, behavior_test
},
4414 {"userdata_test", 0, userdata_test
},
4415 {"error", 0, error_test
},
4416 {"set", 0, set_test
},
4417 {"set2", 0, set_test2
},
4418 {"set3", 0, set_test3
},
4419 {"dump", 1, dump_test
},
4420 {"add", 1, add_test
},
4421 {"replace", 1, replace_test
},
4422 {"delete", 1, delete_test
},
4423 {"get", 1, get_test
},
4424 {"get2", 0, get_test2
},
4425 {"get3", 0, get_test3
},
4426 {"get4", 0, get_test4
},
4427 {"partial mget", 0, get_test5
},
4428 {"stats_servername", 0, stats_servername_test
},
4429 {"increment", 0, increment_test
},
4430 {"increment_with_initial", 1, increment_with_initial_test
},
4431 {"decrement", 0, decrement_test
},
4432 {"decrement_with_initial", 1, decrement_with_initial_test
},
4433 {"quit", 0, quit_test
},
4434 {"mget", 1, mget_test
},
4435 {"mget_result", 1, mget_result_test
},
4436 {"mget_result_alloc", 1, mget_result_alloc_test
},
4437 {"mget_result_function", 1, mget_result_function
},
4438 {"get_stats", 0, get_stats
},
4439 {"add_host_test", 0, add_host_test
},
4440 {"add_host_test_1", 0, add_host_test1
},
4441 {"get_stats_keys", 0, get_stats_keys
},
4442 {"behavior_test", 0, get_stats_keys
},
4443 {"callback_test", 0, get_stats_keys
},
4444 {"version_string_test", 0, version_string_test
},
4445 {"bad_key", 1, bad_key_test
},
4446 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
4447 {"read_through", 1, read_through
},
4448 {"delete_through", 1, delete_through
},
4449 {"noreply", 1, noreply_test
},
4450 {"analyzer", 1, analyzer_test
},
4451 #ifdef HAVE_LIBMEMCACHEDUTIL
4452 {"connectionpool", 1, connection_pool_test
},
4457 test_st async_tests
[] ={
4458 {"add", 1, add_wrapper
},
4462 test_st string_tests
[] ={
4463 {"string static with null", 0, string_static_null
},
4464 {"string alloc with null", 0, string_alloc_null
},
4465 {"string alloc with 1K", 0, string_alloc_with_size
},
4466 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
4467 {"string append", 0, string_alloc_append
},
4468 {"string append failure (too big)", 0, string_alloc_append_toobig
},
4472 test_st result_tests
[] ={
4473 {"result static", 0, result_static
},
4474 {"result alloc", 0, result_alloc
},
4478 test_st version_1_2_3
[] ={
4479 {"append", 0, append_test
},
4480 {"prepend", 0, prepend_test
},
4481 {"cas", 0, cas_test
},
4482 {"cas2", 0, cas2_test
},
4483 {"append_binary", 0, append_binary_test
},
4487 test_st user_tests
[] ={
4488 {"user_supplied_bug1", 0, user_supplied_bug1
},
4489 {"user_supplied_bug2", 0, user_supplied_bug2
},
4490 {"user_supplied_bug3", 0, user_supplied_bug3
},
4491 {"user_supplied_bug4", 0, user_supplied_bug4
},
4492 {"user_supplied_bug5", 1, user_supplied_bug5
},
4493 {"user_supplied_bug6", 1, user_supplied_bug6
},
4494 {"user_supplied_bug7", 1, user_supplied_bug7
},
4495 {"user_supplied_bug8", 1, user_supplied_bug8
},
4496 {"user_supplied_bug9", 1, user_supplied_bug9
},
4497 {"user_supplied_bug10", 1, user_supplied_bug10
},
4498 {"user_supplied_bug11", 1, user_supplied_bug11
},
4499 {"user_supplied_bug12", 1, user_supplied_bug12
},
4500 {"user_supplied_bug13", 1, user_supplied_bug13
},
4501 {"user_supplied_bug14", 1, user_supplied_bug14
},
4502 {"user_supplied_bug15", 1, user_supplied_bug15
},
4503 {"user_supplied_bug16", 1, user_supplied_bug16
},
4506 ** It seems to be something weird with the character sets..
4507 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4508 ** guess I need to find out how this is supposed to work.. Perhaps I need
4509 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4510 ** so just disable the code for now...).
4512 {"user_supplied_bug17", 1, user_supplied_bug17
},
4514 {"user_supplied_bug18", 1, user_supplied_bug18
},
4515 {"user_supplied_bug19", 1, user_supplied_bug19
},
4516 {"user_supplied_bug20", 1, user_supplied_bug20
},
4520 test_st replication_tests
[]= {
4521 {"set", 1, replication_set_test
},
4522 {"get", 0, replication_get_test
},
4523 {"mget", 0, replication_mget_test
},
4524 {"delete", 0, replication_delete_test
},
4528 test_st generate_tests
[] ={
4529 {"generate_pairs", 1, generate_pairs
},
4530 {"generate_data", 1, generate_data
},
4531 {"get_read", 0, get_read
},
4532 {"delete_generate", 0, delete_generate
},
4533 {"generate_buffer_data", 1, generate_buffer_data
},
4534 {"delete_buffer", 0, delete_buffer_generate
},
4535 {"generate_data", 1, generate_data
},
4536 {"mget_read", 0, mget_read
},
4537 {"mget_read_result", 0, mget_read_result
},
4538 {"mget_read_function", 0, mget_read_function
},
4539 {"cleanup", 1, cleanup_pairs
},
4540 {"generate_large_pairs", 1, generate_large_pairs
},
4541 {"generate_data", 1, generate_data
},
4542 {"generate_buffer_data", 1, generate_buffer_data
},
4543 {"cleanup", 1, cleanup_pairs
},
4547 test_st consistent_tests
[] ={
4548 {"generate_pairs", 1, generate_pairs
},
4549 {"generate_data", 1, generate_data
},
4550 {"get_read", 0, get_read_count
},
4551 {"cleanup", 1, cleanup_pairs
},
4555 test_st consistent_weighted_tests
[] ={
4556 {"generate_pairs", 1, generate_pairs
},
4557 {"generate_data", 1, generate_data_with_stats
},
4558 {"get_read", 0, get_read_count
},
4559 {"cleanup", 1, cleanup_pairs
},
4563 test_st hsieh_availability
[] ={
4564 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
4568 test_st ketama_auto_eject_hosts
[] ={
4569 {"auto_eject_hosts", 1, auto_eject_hosts
},
4573 test_st hash_tests
[] ={
4574 {"md5", 0, md5_run
},
4575 {"crc", 0, crc_run
},
4576 {"fnv1_64", 0, fnv1_64_run
},
4577 {"fnv1a_64", 0, fnv1a_64_run
},
4578 {"fnv1_32", 0, fnv1_32_run
},
4579 {"fnv1a_32", 0, fnv1a_32_run
},
4580 {"hsieh", 0, hsieh_run
},
4581 {"murmur", 0, murmur_run
},
4582 {"jenkis", 0, jenkins_run
},
4586 collection_st collection
[] ={
4587 {"hsieh_availability",0,0,hsieh_availability
},
4588 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
4589 {"udp_io", init_udp
, 0, upd_io_tests
},
4590 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
4591 {"block", 0, 0, tests
},
4592 {"binary", pre_binary
, 0, tests
},
4593 {"nonblock", pre_nonblock
, 0, tests
},
4594 {"nodelay", pre_nodelay
, 0, tests
},
4595 {"settimer", pre_settimer
, 0, tests
},
4596 {"md5", pre_md5
, 0, tests
},
4597 {"crc", pre_crc
, 0, tests
},
4598 {"hsieh", pre_hsieh
, 0, tests
},
4599 {"jenkins", pre_jenkins
, 0, tests
},
4600 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
4601 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
4602 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
4603 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
4604 {"ketama", pre_behavior_ketama
, 0, tests
},
4605 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
4606 {"unix_socket", pre_unix_socket
, 0, tests
},
4607 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
4608 {"poll_timeout", poll_timeout
, 0, tests
},
4609 {"gets", enable_cas
, 0, tests
},
4610 {"consistent", enable_consistent
, 0, tests
},
4611 #ifdef MEMCACHED_ENABLE_DEPRECATED
4612 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
4614 {"memory_allocators", set_memory_alloc
, 0, tests
},
4615 {"prefix", set_prefix
, 0, tests
},
4616 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
4617 {"string", 0, 0, string_tests
},
4618 {"result", 0, 0, result_tests
},
4619 {"async", pre_nonblock
, 0, async_tests
},
4620 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
4621 {"user", 0, 0, user_tests
},
4622 {"generate", 0, 0, generate_tests
},
4623 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
4624 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
4625 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
4626 {"generate_md5", pre_md5
, 0, generate_tests
},
4627 {"generate_murmur", pre_murmur
, 0, generate_tests
},
4628 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
4629 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
4630 {"consistent_not", 0, 0, consistent_tests
},
4631 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
4632 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
4633 {"test_hashes", 0, 0, hash_tests
},
4634 {"replication", pre_replication
, 0, replication_tests
},
4635 {"replication_noblock", pre_replication_noblock
, 0, replication_tests
},
4639 #define SERVERS_TO_CREATE 5
4641 /* Prototypes for functions we will pass to test framework */
4642 void *world_create(void);
4643 void world_destroy(void *p
);
4645 void *world_create(void)
4647 server_startup_st
*construct
;
4649 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
4650 memset(construct
, 0, sizeof(server_startup_st
));
4651 construct
->count
= SERVERS_TO_CREATE
;
4653 server_startup(construct
);
4659 void world_destroy(void *p
)
4661 server_startup_st
*construct
= (server_startup_st
*)p
;
4662 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
4663 memcached_server_list_free(servers
);
4665 server_shutdown(construct
);
4669 void get_world(world_st
*world
)
4671 world
->collections
= collection
;
4672 world
->create
= world_create
;
4673 world
->destroy
= world_destroy
;