2 Sample test application.
14 #include "../clients/generator.h"
15 #include "../clients/execute.h"
18 #define INT64_MAX LONG_MAX
21 #define INT32_MAX INT_MAX
27 #define GLOBAL_COUNT 10000
28 #define GLOBAL2_COUNT 100
29 #define SERVERS_TO_CREATE 5
30 static uint32_t global_count
;
32 static pairs_st
*global_pairs
;
33 static char *global_keys
[GLOBAL_COUNT
];
34 static size_t global_keys_length
[GLOBAL_COUNT
];
36 static test_return
init_test(memcached_st
*not_used
__attribute__((unused
)))
40 (void)memcached_create(&memc
);
41 memcached_free(&memc
);
46 static test_return
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
48 memcached_server_st
*server_list
;
51 server_list
= memcached_server_list_append(NULL
, NULL
, 0, NULL
);
52 assert(server_list
== NULL
);
54 server_list
= memcached_server_list_append(NULL
, "localhost", 0, NULL
);
55 assert(server_list
== NULL
);
57 server_list
= memcached_server_list_append(NULL
, NULL
, 0, &rc
);
58 assert(server_list
== NULL
);
63 #define TEST_PORT_COUNT 7
64 uint32_t test_ports
[TEST_PORT_COUNT
];
66 static memcached_return
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
69 uint32_t bigger
= *((uint32_t *)(context
));
70 assert(bigger
<= server
->port
);
71 *((uint32_t *)(context
))= server
->port
;
73 return MEMCACHED_SUCCESS
;
76 static test_return
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
79 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
81 memcached_server_function callbacks
[1];
82 memcached_st
*local_memc
;
84 local_memc
= memcached_create(NULL
);
86 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
88 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
90 test_ports
[x
]= random() % 64000;
91 rc
= memcached_server_add(local_memc
, "localhost", test_ports
[x
]);
92 assert(local_memc
->number_of_hosts
== x
+ 1);
93 assert(local_memc
->hosts
[0].count
== x
+1);
94 assert(rc
== MEMCACHED_SUCCESS
);
97 callbacks
[0]= server_display_function
;
98 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
101 memcached_free(local_memc
);
106 static test_return
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
108 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
110 memcached_server_function callbacks
[1];
111 memcached_st
*local_memc
;
113 local_memc
= memcached_create(NULL
);
115 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
116 assert(rc
== MEMCACHED_SUCCESS
);
118 rc
= memcached_server_add(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043);
119 assert(rc
== MEMCACHED_SUCCESS
);
120 assert(local_memc
->hosts
[0].port
== 43043);
122 rc
= memcached_server_add(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042);
123 assert(rc
== MEMCACHED_SUCCESS
);
124 assert(local_memc
->hosts
[0].port
== 43042);
125 assert(local_memc
->hosts
[1].port
== 43043);
127 callbacks
[0]= server_display_function
;
128 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
131 memcached_free(local_memc
);
136 static memcached_return
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
139 uint32_t x
= *((uint32_t *)(context
));
141 assert(test_ports
[x
] == server
->port
);
142 *((uint32_t *)(context
))= ++x
;
144 return MEMCACHED_SUCCESS
;
147 static test_return
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
150 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
151 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
153 memcached_server_function callbacks
[1];
154 memcached_st
*local_memc
;
156 local_memc
= memcached_create(NULL
);
159 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
161 test_ports
[x
]= random() % 64000;
162 rc
= memcached_server_add(local_memc
, "localhost", test_ports
[x
]);
163 assert(local_memc
->number_of_hosts
== x
+1);
164 assert(local_memc
->hosts
[0].count
== x
+1);
165 assert(rc
== MEMCACHED_SUCCESS
);
168 callbacks
[0]= server_display_unsort_function
;
169 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
171 /* Now we sort old data! */
172 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
173 callbacks
[0]= server_display_function
;
174 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
177 memcached_free(local_memc
);
182 static test_return
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
185 memc
= memcached_create(NULL
);
187 memcached_free(memc
);
192 static test_return
clone_test(memcached_st
*memc
)
197 clone
= memcached_clone(NULL
, NULL
);
199 memcached_free(clone
);
202 /* Can we init from null? */
205 clone
= memcached_clone(NULL
, memc
);
207 memcached_free(clone
);
210 /* Can we init from struct? */
212 memcached_st declared_clone
;
214 clone
= memcached_clone(&declared_clone
, NULL
);
216 memcached_free(clone
);
219 /* Can we init from struct? */
221 memcached_st declared_clone
;
223 clone
= memcached_clone(&declared_clone
, memc
);
225 memcached_free(clone
);
231 static test_return
connection_test(memcached_st
*memc
)
235 rc
= memcached_server_add(memc
, "localhost", 0);
236 assert(rc
== MEMCACHED_SUCCESS
);
241 static test_return
error_test(memcached_st
*memc
)
245 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
247 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
253 static test_return
set_test(memcached_st
*memc
)
257 char *value
= "when we sanitize";
259 rc
= memcached_set(memc
, key
, strlen(key
),
260 value
, strlen(value
),
261 (time_t)0, (uint32_t)0);
262 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
267 static test_return
append_test(memcached_st
*memc
)
275 rc
= memcached_flush(memc
, 0);
276 assert(rc
== MEMCACHED_SUCCESS
);
278 rc
= memcached_set(memc
, key
, strlen(key
),
279 value
, strlen(value
),
280 (time_t)0, (uint32_t)0);
281 assert(rc
== MEMCACHED_SUCCESS
);
283 rc
= memcached_append(memc
, key
, strlen(key
),
284 " the", strlen(" the"),
285 (time_t)0, (uint32_t)0);
286 assert(rc
== MEMCACHED_SUCCESS
);
288 rc
= memcached_append(memc
, key
, strlen(key
),
289 " people", strlen(" people"),
290 (time_t)0, (uint32_t)0);
291 assert(rc
== MEMCACHED_SUCCESS
);
293 value
= memcached_get(memc
, key
, strlen(key
),
294 &value_length
, &flags
, &rc
);
295 assert(!memcmp(value
, "we the people", strlen("we the people")));
296 assert(strlen("we the people") == value_length
);
297 assert(rc
== MEMCACHED_SUCCESS
);
303 static test_return
append_binary_test(memcached_st
*memc
)
306 char *key
= "numbers";
307 unsigned int *store_ptr
;
308 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
314 rc
= memcached_flush(memc
, 0);
315 assert(rc
== MEMCACHED_SUCCESS
);
317 rc
= memcached_set(memc
,
320 (time_t)0, (uint32_t)0);
321 assert(rc
== MEMCACHED_SUCCESS
);
323 for (x
= 0; store_list
[x
] ; x
++)
325 rc
= memcached_append(memc
,
327 (char *)&store_list
[x
], sizeof(unsigned int),
328 (time_t)0, (uint32_t)0);
329 assert(rc
== MEMCACHED_SUCCESS
);
332 value
= memcached_get(memc
, key
, strlen(key
),
333 &value_length
, &flags
, &rc
);
334 assert((value_length
== (sizeof(unsigned int) * x
)));
335 assert(rc
== MEMCACHED_SUCCESS
);
337 store_ptr
= (unsigned int *)value
;
339 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
341 assert(*store_ptr
== store_list
[x
++]);
349 static test_return
cas2_test(memcached_st
*memc
)
352 char *keys
[]= {"fudge", "son", "food"};
353 size_t key_length
[]= {5, 3, 4};
354 char *value
= "we the people";
355 size_t value_length
= strlen("we the people");
357 memcached_result_st results_obj
;
358 memcached_result_st
*results
;
361 rc
= memcached_flush(memc
, 0);
362 assert(rc
== MEMCACHED_SUCCESS
);
364 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
366 for (x
= 0; x
< 3; x
++)
368 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
369 keys
[x
], key_length
[x
],
370 (time_t)50, (uint32_t)9);
371 assert(rc
== MEMCACHED_SUCCESS
);
374 rc
= memcached_mget(memc
, keys
, key_length
, 3);
376 results
= memcached_result_create(memc
, &results_obj
);
378 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
380 assert(results
->cas
);
381 assert(rc
== MEMCACHED_SUCCESS
);
382 WATCHPOINT_ASSERT(memcached_result_cas(results
));
384 assert(!memcmp(value
, "we the people", strlen("we the people")));
385 assert(strlen("we the people") == value_length
);
386 assert(rc
== MEMCACHED_SUCCESS
);
388 memcached_result_free(&results_obj
);
393 static test_return
cas_test(memcached_st
*memc
)
397 size_t key_length
= strlen("fun");
398 char *value
= "we the people";
399 size_t value_length
= strlen("we the people");
400 memcached_result_st results_obj
;
401 memcached_result_st
*results
;
404 rc
= memcached_flush(memc
, 0);
405 assert(rc
== MEMCACHED_SUCCESS
);
407 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
409 rc
= memcached_set(memc
, key
, strlen(key
),
410 value
, strlen(value
),
411 (time_t)0, (uint32_t)0);
412 assert(rc
== MEMCACHED_SUCCESS
);
414 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
416 results
= memcached_result_create(memc
, &results_obj
);
418 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
420 assert(rc
== MEMCACHED_SUCCESS
);
421 WATCHPOINT_ASSERT(memcached_result_cas(results
));
423 assert(!memcmp(value
, "we the people", strlen("we the people")));
424 assert(strlen("we the people") == value_length
);
425 assert(rc
== MEMCACHED_SUCCESS
);
427 rc
= memcached_cas(memc
, key
, key_length
,
428 "change the value", strlen("change the value"),
429 0, 0, memcached_result_cas(results
));
431 assert(rc
== MEMCACHED_SUCCESS
);
433 rc
= memcached_cas(memc
, key
, key_length
,
434 "change the value", strlen("change the value"),
437 assert(rc
== MEMCACHED_DATA_EXISTS
);
440 memcached_result_free(&results_obj
);
445 static test_return
prepend_test(memcached_st
*memc
)
449 char *value
= "people";
453 rc
= memcached_flush(memc
, 0);
454 assert(rc
== MEMCACHED_SUCCESS
);
456 rc
= memcached_set(memc
, key
, strlen(key
),
457 value
, strlen(value
),
458 (time_t)0, (uint32_t)0);
459 assert(rc
== MEMCACHED_SUCCESS
);
461 rc
= memcached_prepend(memc
, key
, strlen(key
),
462 "the ", strlen("the "),
463 (time_t)0, (uint32_t)0);
464 assert(rc
== MEMCACHED_SUCCESS
);
466 rc
= memcached_prepend(memc
, key
, strlen(key
),
467 "we ", strlen("we "),
468 (time_t)0, (uint32_t)0);
469 assert(rc
== MEMCACHED_SUCCESS
);
471 value
= memcached_get(memc
, key
, strlen(key
),
472 &value_length
, &flags
, &rc
);
473 assert(!memcmp(value
, "we the people", strlen("we the people")));
474 assert(strlen("we the people") == value_length
);
475 assert(rc
== MEMCACHED_SUCCESS
);
482 Set the value, then quit to make sure it is flushed.
483 Come back in and test that add fails.
485 static test_return
add_test(memcached_st
*memc
)
489 char *value
= "when we sanitize";
490 unsigned long long setting_value
;
492 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
494 rc
= memcached_set(memc
, key
, strlen(key
),
495 value
, strlen(value
),
496 (time_t)0, (uint32_t)0);
497 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
498 memcached_quit(memc
);
499 rc
= memcached_add(memc
, key
, strlen(key
),
500 value
, strlen(value
),
501 (time_t)0, (uint32_t)0);
503 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
505 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
507 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
512 static test_return
add_wrapper(memcached_st
*memc
)
516 for (x
= 0; x
< 10000; x
++)
522 static test_return
replace_test(memcached_st
*memc
)
526 char *value
= "when we sanitize";
527 char *original
= "first we insert some data";
529 rc
= memcached_set(memc
, key
, strlen(key
),
530 original
, strlen(original
),
531 (time_t)0, (uint32_t)0);
532 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
534 rc
= memcached_replace(memc
, key
, strlen(key
),
535 value
, strlen(value
),
536 (time_t)0, (uint32_t)0);
537 assert(rc
== MEMCACHED_SUCCESS
);
542 static test_return
delete_test(memcached_st
*memc
)
546 char *value
= "when we sanitize";
548 rc
= memcached_set(memc
, key
, strlen(key
),
549 value
, strlen(value
),
550 (time_t)0, (uint32_t)0);
551 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
553 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
554 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
559 static test_return
flush_test(memcached_st
*memc
)
563 rc
= memcached_flush(memc
, 0);
564 assert(rc
== MEMCACHED_SUCCESS
);
569 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
570 memcached_server_st
*server
__attribute__((unused
)),
571 void *context
__attribute__((unused
)))
575 return MEMCACHED_SUCCESS
;
578 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
580 char *context
= "foo bad";
581 memcached_server_function callbacks
[1];
583 callbacks
[0]= server_function
;
584 memcached_server_cursor(memc
, callbacks
, context
, 1);
589 static test_return
bad_key_test(memcached_st
*memc
)
592 char *key
= "foo bad";
594 size_t string_length
;
599 clone
= memcached_clone(NULL
, memc
);
602 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
603 assert(rc
== MEMCACHED_SUCCESS
);
605 string
= memcached_get(clone
, key
, strlen(key
),
606 &string_length
, &flags
, &rc
);
607 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
608 assert(string_length
== 0);
612 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
613 assert(rc
== MEMCACHED_SUCCESS
);
614 string
= memcached_get(clone
, key
, strlen(key
),
615 &string_length
, &flags
, &rc
);
616 assert(rc
== MEMCACHED_NOTFOUND
);
617 assert(string_length
== 0);
620 /* Test multi key for bad keys */
622 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
623 size_t key_lengths
[] = { 7, 7, 7 };
625 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
626 assert(rc
== MEMCACHED_SUCCESS
);
628 rc
= memcached_mget(clone
, keys
, key_lengths
, 3);
629 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
631 rc
= memcached_mget_by_key(clone
, "foo daddy", 9, keys
, key_lengths
, 1);
632 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
635 /* Make sure zero length keys are marked as bad */
637 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
638 assert(rc
== MEMCACHED_SUCCESS
);
639 string
= memcached_get(clone
, key
, 0,
640 &string_length
, &flags
, &rc
);
641 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
642 assert(string_length
== 0);
645 memcached_free(clone
);
650 #define READ_THROUGH_VALUE "set for me"
651 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
652 char *key
__attribute__((unused
)),
653 size_t key_length
__attribute__((unused
)),
654 memcached_result_st
*result
)
657 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
660 static test_return
read_through(memcached_st
*memc
)
665 size_t string_length
;
668 string
= memcached_get(memc
, key
, strlen(key
),
669 &string_length
, &flags
, &rc
);
671 assert(rc
== MEMCACHED_NOTFOUND
);
672 assert(string_length
== 0);
675 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, (void *)read_through_trigger
);
676 assert(rc
== MEMCACHED_SUCCESS
);
678 string
= memcached_get(memc
, key
, strlen(key
),
679 &string_length
, &flags
, &rc
);
681 assert(rc
== MEMCACHED_SUCCESS
);
682 assert(string_length
== strlen(READ_THROUGH_VALUE
));
683 assert(!strcmp(READ_THROUGH_VALUE
, string
));
686 string
= memcached_get(memc
, key
, strlen(key
),
687 &string_length
, &flags
, &rc
);
689 assert(rc
== MEMCACHED_SUCCESS
);
690 assert(string_length
== strlen(READ_THROUGH_VALUE
));
691 assert(!strcmp(READ_THROUGH_VALUE
, string
));
697 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
699 size_t key_length
__attribute__((unused
)))
703 return MEMCACHED_SUCCESS
;
706 static test_return
delete_through(memcached_st
*memc
)
708 memcached_trigger_delete_key callback
;
711 callback
= delete_trigger
;
713 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, callback
);
714 assert(rc
== MEMCACHED_SUCCESS
);
719 static test_return
get_test(memcached_st
*memc
)
724 size_t string_length
;
727 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
728 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
730 string
= memcached_get(memc
, key
, strlen(key
),
731 &string_length
, &flags
, &rc
);
733 assert(rc
== MEMCACHED_NOTFOUND
);
734 assert(string_length
== 0);
740 static test_return
get_test2(memcached_st
*memc
)
744 char *value
= "when we sanitize";
746 size_t string_length
;
749 rc
= memcached_set(memc
, key
, strlen(key
),
750 value
, strlen(value
),
751 (time_t)0, (uint32_t)0);
752 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
754 string
= memcached_get(memc
, key
, strlen(key
),
755 &string_length
, &flags
, &rc
);
758 assert(rc
== MEMCACHED_SUCCESS
);
759 assert(string_length
== strlen(value
));
760 assert(!memcmp(string
, value
, string_length
));
767 static test_return
set_test2(memcached_st
*memc
)
771 char *value
= "train in the brain";
772 size_t value_length
= strlen(value
);
775 for (x
= 0; x
< 10; x
++)
777 rc
= memcached_set(memc
, key
, strlen(key
),
779 (time_t)0, (uint32_t)0);
780 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
786 static test_return
set_test3(memcached_st
*memc
)
791 size_t value_length
= 8191;
794 value
= (char*)malloc(value_length
);
797 for (x
= 0; x
< value_length
; x
++)
798 value
[x
] = (char) (x
% 127);
800 for (x
= 0; x
< 1; x
++)
802 rc
= memcached_set(memc
, key
, strlen(key
),
804 (time_t)0, (uint32_t)0);
805 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
813 static test_return
get_test3(memcached_st
*memc
)
818 size_t value_length
= 8191;
820 size_t string_length
;
824 value
= (char*)malloc(value_length
);
827 for (x
= 0; x
< value_length
; x
++)
828 value
[x
] = (char) (x
% 127);
830 rc
= memcached_set(memc
, key
, strlen(key
),
832 (time_t)0, (uint32_t)0);
833 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
835 string
= memcached_get(memc
, key
, strlen(key
),
836 &string_length
, &flags
, &rc
);
838 assert(rc
== MEMCACHED_SUCCESS
);
840 assert(string_length
== value_length
);
841 assert(!memcmp(string
, value
, string_length
));
849 static test_return
get_test4(memcached_st
*memc
)
854 size_t value_length
= 8191;
856 size_t string_length
;
860 value
= (char*)malloc(value_length
);
863 for (x
= 0; x
< value_length
; x
++)
864 value
[x
] = (char) (x
% 127);
866 rc
= memcached_set(memc
, key
, strlen(key
),
868 (time_t)0, (uint32_t)0);
869 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
871 for (x
= 0; x
< 10; x
++)
873 string
= memcached_get(memc
, key
, strlen(key
),
874 &string_length
, &flags
, &rc
);
876 assert(rc
== MEMCACHED_SUCCESS
);
878 assert(string_length
== value_length
);
879 assert(!memcmp(string
, value
, string_length
));
888 /* Do not copy the style of this code, I just access hosts to testthis function */
889 static test_return
stats_servername_test(memcached_st
*memc
)
892 memcached_stat_st stat
;
893 rc
= memcached_stat_servername(&stat
, NULL
,
894 memc
->hosts
[0].hostname
,
895 memc
->hosts
[0].port
);
900 static test_return
increment_test(memcached_st
*memc
)
907 rc
= memcached_set(memc
, key
, strlen(key
),
908 value
, strlen(value
),
909 (time_t)0, (uint32_t)0);
910 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
912 rc
= memcached_increment(memc
, key
, strlen(key
),
914 assert(rc
== MEMCACHED_SUCCESS
);
915 assert(new_number
== 1);
917 rc
= memcached_increment(memc
, key
, strlen(key
),
919 assert(rc
== MEMCACHED_SUCCESS
);
920 assert(new_number
== 2);
925 static test_return
decrement_test(memcached_st
*memc
)
932 rc
= memcached_set(memc
, key
, strlen(key
),
933 value
, strlen(value
),
934 (time_t)0, (uint32_t)0);
935 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
937 rc
= memcached_decrement(memc
, key
, strlen(key
),
939 assert(rc
== MEMCACHED_SUCCESS
);
940 assert(new_number
== 2);
942 rc
= memcached_decrement(memc
, key
, strlen(key
),
944 assert(rc
== MEMCACHED_SUCCESS
);
945 assert(new_number
== 1);
950 static test_return
quit_test(memcached_st
*memc
)
954 char *value
= "sanford and sun";
956 rc
= memcached_set(memc
, key
, strlen(key
),
957 value
, strlen(value
),
958 (time_t)10, (uint32_t)3);
959 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
960 memcached_quit(memc
);
962 rc
= memcached_set(memc
, key
, strlen(key
),
963 value
, strlen(value
),
964 (time_t)50, (uint32_t)9);
965 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
970 static test_return
mget_result_test(memcached_st
*memc
)
973 char *keys
[]= {"fudge", "son", "food"};
974 size_t key_length
[]= {5, 3, 4};
977 memcached_result_st results_obj
;
978 memcached_result_st
*results
;
980 results
= memcached_result_create(memc
, &results_obj
);
982 assert(&results_obj
== results
);
984 /* We need to empty the server before continueing test */
985 rc
= memcached_flush(memc
, 0);
986 assert(rc
== MEMCACHED_SUCCESS
);
988 rc
= memcached_mget(memc
, keys
, key_length
, 3);
989 assert(rc
== MEMCACHED_SUCCESS
);
991 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
996 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
998 assert(rc
== MEMCACHED_END
);
1000 for (x
= 0; x
< 3; x
++)
1002 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1003 keys
[x
], key_length
[x
],
1004 (time_t)50, (uint32_t)9);
1005 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1008 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1009 assert(rc
== MEMCACHED_SUCCESS
);
1011 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1014 assert(&results_obj
== results
);
1015 assert(rc
== MEMCACHED_SUCCESS
);
1016 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1017 assert(!memcmp(memcached_result_key_value(results
),
1018 memcached_result_value(results
),
1019 memcached_result_length(results
)));
1022 memcached_result_free(&results_obj
);
1027 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1029 memcached_return rc
;
1030 char *keys
[]= {"fudge", "son", "food"};
1031 size_t key_length
[]= {5, 3, 4};
1034 memcached_result_st
*results
;
1036 /* We need to empty the server before continueing test */
1037 rc
= memcached_flush(memc
, 0);
1038 assert(rc
== MEMCACHED_SUCCESS
);
1040 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1041 assert(rc
== MEMCACHED_SUCCESS
);
1043 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1048 assert(rc
== MEMCACHED_END
);
1050 for (x
= 0; x
< 3; x
++)
1052 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1053 keys
[x
], key_length
[x
],
1054 (time_t)50, (uint32_t)9);
1055 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1058 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1059 assert(rc
== MEMCACHED_SUCCESS
);
1062 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1065 assert(rc
== MEMCACHED_SUCCESS
);
1066 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1067 assert(!memcmp(memcached_result_key_value(results
),
1068 memcached_result_value(results
),
1069 memcached_result_length(results
)));
1070 memcached_result_free(results
);
1077 /* Count the results */
1078 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1079 memcached_result_st
*result
__attribute__((unused
)),
1082 unsigned int *counter
= (unsigned int *)context
;
1084 *counter
= *counter
+ 1;
1086 return MEMCACHED_SUCCESS
;
1089 static test_return
mget_result_function(memcached_st
*memc
)
1091 memcached_return rc
;
1092 char *keys
[]= {"fudge", "son", "food"};
1093 size_t key_length
[]= {5, 3, 4};
1095 unsigned int counter
;
1096 memcached_execute_function callbacks
[1];
1098 /* We need to empty the server before continueing test */
1099 rc
= memcached_flush(memc
, 0);
1100 for (x
= 0; x
< 3; x
++)
1102 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1103 keys
[x
], key_length
[x
],
1104 (time_t)50, (uint32_t)9);
1105 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1108 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1109 assert(rc
== MEMCACHED_SUCCESS
);
1111 callbacks
[0]= &callback_counter
;
1113 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1115 assert(counter
== 3);
1120 static test_return
mget_test(memcached_st
*memc
)
1122 memcached_return rc
;
1123 char *keys
[]= {"fudge", "son", "food"};
1124 size_t key_length
[]= {5, 3, 4};
1128 char return_key
[MEMCACHED_MAX_KEY
];
1129 size_t return_key_length
;
1131 size_t return_value_length
;
1133 /* We need to empty the server before continueing test */
1134 rc
= memcached_flush(memc
, 0);
1135 assert(rc
== MEMCACHED_SUCCESS
);
1137 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1138 assert(rc
== MEMCACHED_SUCCESS
);
1140 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1141 &return_value_length
, &flags
, &rc
)) != NULL
)
1143 assert(return_value
);
1145 assert(!return_value
);
1146 assert(return_value_length
== 0);
1147 assert(rc
== MEMCACHED_END
);
1149 for (x
= 0; x
< 3; x
++)
1151 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1152 keys
[x
], key_length
[x
],
1153 (time_t)50, (uint32_t)9);
1154 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1157 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1158 assert(rc
== MEMCACHED_SUCCESS
);
1161 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1162 &return_value_length
, &flags
, &rc
)))
1164 assert(return_value
);
1165 assert(rc
== MEMCACHED_SUCCESS
);
1166 assert(return_key_length
== return_value_length
);
1167 assert(!memcmp(return_value
, return_key
, return_value_length
));
1175 static test_return
get_stats_keys(memcached_st
*memc
)
1179 memcached_stat_st stat
;
1180 memcached_return rc
;
1182 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
1183 assert(rc
== MEMCACHED_SUCCESS
);
1184 for (ptr
= list
; *ptr
; ptr
++)
1193 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1195 const char *version_string
;
1197 version_string
= memcached_lib_version();
1199 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1204 static test_return
get_stats(memcached_st
*memc
)
1209 memcached_return rc
;
1210 memcached_stat_st
*stat
;
1212 stat
= memcached_stat(memc
, NULL
, &rc
);
1213 assert(rc
== MEMCACHED_SUCCESS
);
1215 assert(rc
== MEMCACHED_SUCCESS
);
1218 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1220 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
1221 assert(rc
== MEMCACHED_SUCCESS
);
1222 for (ptr
= list
; *ptr
; ptr
++);
1227 memcached_stat_free(NULL
, stat
);
1232 static test_return
add_host_test(memcached_st
*memc
)
1235 memcached_server_st
*servers
;
1236 memcached_return rc
;
1237 char servername
[]= "0.example.com";
1239 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1241 assert(1 == memcached_server_list_count(servers
));
1243 for (x
= 2; x
< 20; x
++)
1245 char buffer
[SMALL_STRING_LEN
];
1247 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1248 servers
= memcached_server_list_append(servers
, buffer
, 401,
1250 assert(rc
== MEMCACHED_SUCCESS
);
1251 assert(x
== memcached_server_list_count(servers
));
1254 rc
= memcached_server_push(memc
, servers
);
1255 assert(rc
== MEMCACHED_SUCCESS
);
1256 rc
= memcached_server_push(memc
, servers
);
1257 assert(rc
== MEMCACHED_SUCCESS
);
1259 memcached_server_list_free(servers
);
1264 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*clone
__attribute__((unused
)))
1266 return MEMCACHED_SUCCESS
;
1269 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1271 return MEMCACHED_SUCCESS
;
1274 static test_return
callback_test(memcached_st
*memc
)
1276 /* Test User Data */
1280 memcached_return rc
;
1282 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1283 assert(rc
== MEMCACHED_SUCCESS
);
1284 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1285 assert(*test_ptr
== x
);
1288 /* Test Clone Callback */
1290 memcached_clone_func temp_function
;
1291 memcached_return rc
;
1293 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
1294 assert(rc
== MEMCACHED_SUCCESS
);
1295 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1296 assert(temp_function
== clone_test_callback
);
1299 /* Test Cleanup Callback */
1301 memcached_cleanup_func temp_function
;
1302 memcached_return rc
;
1304 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1305 assert(rc
== MEMCACHED_SUCCESS
);
1306 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1307 assert(temp_function
== cleanup_test_callback
);
1313 /* We don't test the behavior itself, we test the switches */
1314 static test_return
behavior_test(memcached_st
*memc
)
1319 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1320 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1323 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1324 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1327 set
= MEMCACHED_HASH_MD5
;
1328 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1329 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1330 assert(value
== MEMCACHED_HASH_MD5
);
1334 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1335 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1338 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1339 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1342 set
= MEMCACHED_HASH_DEFAULT
;
1343 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1344 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1345 assert(value
== MEMCACHED_HASH_DEFAULT
);
1347 set
= MEMCACHED_HASH_CRC
;
1348 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1349 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1350 assert(value
== MEMCACHED_HASH_CRC
);
1352 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1355 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1361 /* Test case provided by Cal Haldenbrand */
1362 static test_return
user_supplied_bug1(memcached_st
*memc
)
1364 unsigned int setter
= 1;
1367 unsigned long long total
= 0;
1370 char randomstuff
[6 * 1024];
1371 memcached_return rc
;
1373 memset(randomstuff
, 0, 6 * 1024);
1375 /* We just keep looking at the same values over and over */
1378 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1379 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1383 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1387 size
= (rand() % ( 5 * 1024 ) ) + 400;
1388 memset(randomstuff
, 0, 6 * 1024);
1389 assert(size
< 6 * 1024); /* Being safe here */
1391 for (j
= 0 ; j
< size
;j
++)
1392 randomstuff
[j
] = (char) (rand() % 26) + 97;
1395 sprintf(key
, "%d", x
);
1396 rc
= memcached_set(memc
, key
, strlen(key
),
1397 randomstuff
, strlen(randomstuff
), 10, 0);
1398 /* If we fail, lets try again */
1399 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1400 rc
= memcached_set(memc
, key
, strlen(key
),
1401 randomstuff
, strlen(randomstuff
), 10, 0);
1402 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1408 /* Test case provided by Cal Haldenbrand */
1409 static test_return
user_supplied_bug2(memcached_st
*memc
)
1412 unsigned int setter
;
1414 unsigned long long total
;
1417 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1418 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1420 setter
= 20 * 1024576;
1421 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1422 setter
= 20 * 1024576;
1423 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1424 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1425 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1427 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1430 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1432 memcached_return rc
= MEMCACHED_SUCCESS
;
1433 char buffer
[SMALL_STRING_LEN
];
1438 memset(buffer
, 0, SMALL_STRING_LEN
);
1440 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1441 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1442 &val_len
, &flags
, &rc
);
1443 if (rc
!= MEMCACHED_SUCCESS
)
1445 if (rc
== MEMCACHED_NOTFOUND
)
1449 WATCHPOINT_ERROR(rc
);
1463 /* Do a large mget() over all the keys we think exist */
1464 #define KEY_COUNT 3000 // * 1024576
1465 static test_return
user_supplied_bug3(memcached_st
*memc
)
1467 memcached_return rc
;
1468 unsigned int setter
;
1471 size_t key_lengths
[KEY_COUNT
];
1474 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1475 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1477 setter
= 20 * 1024576;
1478 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1479 setter
= 20 * 1024576;
1480 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1481 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1482 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1485 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1487 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1488 for (x
= 0; x
< KEY_COUNT
; x
++)
1492 snprintf(buffer
, 30, "%u", x
);
1493 keys
[x
]= strdup(buffer
);
1494 key_lengths
[x
]= strlen(keys
[x
]);
1497 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1498 assert(rc
== MEMCACHED_SUCCESS
);
1500 /* Turn this into a help function */
1502 char return_key
[MEMCACHED_MAX_KEY
];
1503 size_t return_key_length
;
1505 size_t return_value_length
;
1508 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1509 &return_value_length
, &flags
, &rc
)))
1511 assert(return_value
);
1512 assert(rc
== MEMCACHED_SUCCESS
);
1517 for (x
= 0; x
< KEY_COUNT
; x
++)
1524 /* Make sure we behave properly if server list has no values */
1525 static test_return
user_supplied_bug4(memcached_st
*memc
)
1527 memcached_return rc
;
1528 char *keys
[]= {"fudge", "son", "food"};
1529 size_t key_length
[]= {5, 3, 4};
1532 char return_key
[MEMCACHED_MAX_KEY
];
1533 size_t return_key_length
;
1535 size_t return_value_length
;
1537 /* Here we free everything before running a bunch of mget tests */
1539 memcached_server_list_free(memc
->hosts
);
1541 memc
->number_of_hosts
= 0;
1545 /* We need to empty the server before continueing test */
1546 rc
= memcached_flush(memc
, 0);
1547 assert(rc
== MEMCACHED_NO_SERVERS
);
1549 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1550 assert(rc
== MEMCACHED_NO_SERVERS
);
1552 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1553 &return_value_length
, &flags
, &rc
)) != NULL
)
1555 assert(return_value
);
1557 assert(!return_value
);
1558 assert(return_value_length
== 0);
1559 assert(rc
== MEMCACHED_NO_SERVERS
);
1561 for (x
= 0; x
< 3; x
++)
1563 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1564 keys
[x
], key_length
[x
],
1565 (time_t)50, (uint32_t)9);
1566 assert(rc
== MEMCACHED_NO_SERVERS
);
1569 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1570 assert(rc
== MEMCACHED_NO_SERVERS
);
1573 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1574 &return_value_length
, &flags
, &rc
)))
1576 assert(return_value
);
1577 assert(rc
== MEMCACHED_SUCCESS
);
1578 assert(return_key_length
== return_value_length
);
1579 assert(!memcmp(return_value
, return_key
, return_value_length
));
1587 #define VALUE_SIZE_BUG5 1048064
1588 static test_return
user_supplied_bug5(memcached_st
*memc
)
1590 memcached_return rc
;
1591 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1592 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1593 char return_key
[MEMCACHED_MAX_KEY
];
1594 size_t return_key_length
;
1596 size_t value_length
;
1600 char insert_data
[VALUE_SIZE_BUG5
];
1602 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1603 insert_data
[x
]= rand();
1605 memcached_flush(memc
, 0);
1606 value
= memcached_get(memc
, keys
[0], key_length
[0],
1607 &value_length
, &flags
, &rc
);
1608 assert(value
== NULL
);
1609 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1612 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1613 &value_length
, &flags
, &rc
)))
1617 for (x
= 0; x
< 4; x
++)
1619 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1620 insert_data
, VALUE_SIZE_BUG5
,
1621 (time_t)0, (uint32_t)0);
1622 assert(rc
== MEMCACHED_SUCCESS
);
1625 for (x
= 0; x
< 10; x
++)
1627 value
= memcached_get(memc
, keys
[0], key_length
[0],
1628 &value_length
, &flags
, &rc
);
1632 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1634 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1635 &value_length
, &flags
, &rc
)))
1646 static test_return
user_supplied_bug6(memcached_st
*memc
)
1648 memcached_return rc
;
1649 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1650 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1651 char return_key
[MEMCACHED_MAX_KEY
];
1652 size_t return_key_length
;
1654 size_t value_length
;
1658 char insert_data
[VALUE_SIZE_BUG5
];
1660 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1661 insert_data
[x
]= rand();
1663 memcached_flush(memc
, 0);
1664 value
= memcached_get(memc
, keys
[0], key_length
[0],
1665 &value_length
, &flags
, &rc
);
1666 assert(value
== NULL
);
1667 assert(rc
== MEMCACHED_NOTFOUND
);
1668 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1669 assert(rc
== MEMCACHED_SUCCESS
);
1672 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1673 &value_length
, &flags
, &rc
)))
1676 assert(rc
== MEMCACHED_END
);
1678 for (x
= 0; x
< 4; x
++)
1680 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1681 insert_data
, VALUE_SIZE_BUG5
,
1682 (time_t)0, (uint32_t)0);
1683 assert(rc
== MEMCACHED_SUCCESS
);
1686 for (x
= 0; x
< 2; x
++)
1688 value
= memcached_get(memc
, keys
[0], key_length
[0],
1689 &value_length
, &flags
, &rc
);
1693 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1694 assert(rc
== MEMCACHED_SUCCESS
);
1696 /* We test for purge of partial complete fetches */
1697 for (count
= 3; count
; count
--)
1699 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1700 &value_length
, &flags
, &rc
);
1701 assert(rc
== MEMCACHED_SUCCESS
);
1702 assert(!(memcmp(value
, insert_data
, value_length
)));
1703 assert(value_length
);
1711 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1713 memcached_return rc
;
1715 memcached_st
*clone
;
1717 memcached_server_st
*servers
;
1718 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";
1720 servers
= memcached_servers_parse(server_list
);
1723 mine
= memcached_create(NULL
);
1724 rc
= memcached_server_push(mine
, servers
);
1725 assert(rc
== MEMCACHED_SUCCESS
);
1726 memcached_server_list_free(servers
);
1729 clone
= memcached_clone(NULL
, mine
);
1731 memcached_quit(mine
);
1732 memcached_quit(clone
);
1735 memcached_free(mine
);
1736 memcached_free(clone
);
1741 /* Test flag store/retrieve */
1742 static test_return
user_supplied_bug7(memcached_st
*memc
)
1744 memcached_return rc
;
1745 char *keys
= "036790384900";
1746 size_t key_length
= strlen("036790384900");
1747 char return_key
[MEMCACHED_MAX_KEY
];
1748 size_t return_key_length
;
1750 size_t value_length
;
1753 char insert_data
[VALUE_SIZE_BUG5
];
1755 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1756 insert_data
[x
]= rand();
1758 memcached_flush(memc
, 0);
1761 rc
= memcached_set(memc
, keys
, key_length
,
1762 insert_data
, VALUE_SIZE_BUG5
,
1764 assert(rc
== MEMCACHED_SUCCESS
);
1767 value
= memcached_get(memc
, keys
, key_length
,
1768 &value_length
, &flags
, &rc
);
1769 assert(flags
== 245);
1773 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1776 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1777 &value_length
, &flags
, &rc
);
1778 assert(flags
== 245);
1786 static test_return
user_supplied_bug9(memcached_st
*memc
)
1788 memcached_return rc
;
1789 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1790 size_t key_length
[3];
1795 char return_key
[MEMCACHED_MAX_KEY
];
1796 size_t return_key_length
;
1798 size_t return_value_length
;
1801 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1802 key_length
[1]= strlen("fudge&*@#");
1803 key_length
[2]= strlen("for^#@&$not");
1806 for (x
= 0; x
< 3; x
++)
1808 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1809 keys
[x
], key_length
[x
],
1810 (time_t)50, (uint32_t)9);
1811 assert(rc
== MEMCACHED_SUCCESS
);
1814 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1815 assert(rc
== MEMCACHED_SUCCESS
);
1817 /* We need to empty the server before continueing test */
1818 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1819 &return_value_length
, &flags
, &rc
)) != NULL
)
1821 assert(return_value
);
1830 /* We are testing with aggressive timeout to get failures */
1831 static test_return
user_supplied_bug10(memcached_st
*memc
)
1835 size_t value_length
= 512;
1838 memcached_return rc
;
1839 unsigned int set
= 1;
1840 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1843 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1844 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1846 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1848 value
= (char*)malloc(value_length
* sizeof(char));
1850 for (x
= 0; x
< value_length
; x
++)
1851 value
[x
]= (char) (x
% 127);
1853 for (x
= 1; x
<= 100000; ++x
)
1855 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1857 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1859 if (rc
== MEMCACHED_WRITE_FAILURE
)
1864 memcached_free(mclone
);
1870 We are looking failures in the async protocol
1872 static test_return
user_supplied_bug11(memcached_st
*memc
)
1876 size_t value_length
= 512;
1879 memcached_return rc
;
1880 unsigned int set
= 1;
1882 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1884 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1885 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1887 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1889 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1891 assert(timeout
== -1);
1893 value
= (char*)malloc(value_length
* sizeof(char));
1895 for (x
= 0; x
< value_length
; x
++)
1896 value
[x
]= (char) (x
% 127);
1898 for (x
= 1; x
<= 100000; ++x
)
1900 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1904 memcached_free(mclone
);
1910 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1912 static test_return
user_supplied_bug12(memcached_st
*memc
)
1914 memcached_return rc
;
1916 size_t value_length
;
1918 uint64_t number_value
;
1920 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1921 &value_length
, &flags
, &rc
);
1922 assert(value
== NULL
);
1923 assert(rc
== MEMCACHED_NOTFOUND
);
1925 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1928 assert(value
== NULL
);
1929 assert(rc
== MEMCACHED_NOTFOUND
);
1931 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1933 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1934 &value_length
, &flags
, &rc
);
1936 assert(rc
== MEMCACHED_SUCCESS
);
1939 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1941 assert(number_value
== 2);
1942 assert(rc
== MEMCACHED_SUCCESS
);
1948 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1949 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1951 static test_return
user_supplied_bug13(memcached_st
*memc
)
1953 char key
[] = "key34567890";
1955 memcached_return rc
;
1956 size_t overflowSize
;
1958 char commandFirst
[]= "set key34567890 0 0 ";
1959 char commandLast
[] = " \r\n"; /* first line of command sent to server */
1960 size_t commandLength
;
1963 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
1965 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
1967 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
1969 overflow
= malloc(testSize
);
1970 assert(overflow
!= NULL
);
1972 memset(overflow
, 'x', testSize
);
1973 rc
= memcached_set(memc
, key
, strlen(key
),
1974 overflow
, testSize
, 0, 0);
1975 assert(rc
== MEMCACHED_SUCCESS
);
1984 Test values of many different sizes
1985 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1986 set key34567890 0 0 8169 \r\n
1987 is sent followed by buffer of size 8169, followed by 8169
1989 static test_return
user_supplied_bug14(memcached_st
*memc
)
1992 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1993 memcached_return rc
;
1996 size_t value_length
= 18000;
1998 size_t string_length
;
2001 size_t current_length
;
2003 value
= (char*)malloc(value_length
);
2006 for (x
= 0; x
< value_length
; x
++)
2007 value
[x
] = (char) (x
% 127);
2009 for (current_length
= 0; current_length
< value_length
; current_length
++)
2011 rc
= memcached_set(memc
, key
, strlen(key
),
2012 value
, current_length
,
2013 (time_t)0, (uint32_t)0);
2014 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2016 string
= memcached_get(memc
, key
, strlen(key
),
2017 &string_length
, &flags
, &rc
);
2019 assert(rc
== MEMCACHED_SUCCESS
);
2020 assert(string_length
== current_length
);
2021 assert(!memcmp(string
, value
, string_length
));
2032 Look for zero length value problems
2034 static test_return
user_supplied_bug15(memcached_st
*memc
)
2037 memcached_return rc
;
2043 for (x
= 0; x
< 2; x
++)
2045 rc
= memcached_set(memc
, key
, strlen(key
),
2047 (time_t)0, (uint32_t)0);
2049 assert(rc
== MEMCACHED_SUCCESS
);
2051 value
= memcached_get(memc
, key
, strlen(key
),
2052 &length
, &flags
, &rc
);
2054 assert(rc
== MEMCACHED_SUCCESS
);
2055 assert(value
== NULL
);
2056 assert(length
== 0);
2059 value
= memcached_get(memc
, key
, strlen(key
),
2060 &length
, &flags
, &rc
);
2062 assert(rc
== MEMCACHED_SUCCESS
);
2063 assert(value
== NULL
);
2064 assert(length
== 0);
2071 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2072 static test_return
user_supplied_bug16(memcached_st
*memc
)
2074 memcached_return rc
;
2080 rc
= memcached_set(memc
, key
, strlen(key
),
2082 (time_t)0, UINT32_MAX
);
2084 assert(rc
== MEMCACHED_SUCCESS
);
2086 value
= memcached_get(memc
, key
, strlen(key
),
2087 &length
, &flags
, &rc
);
2089 assert(rc
== MEMCACHED_SUCCESS
);
2090 assert(value
== NULL
);
2091 assert(length
== 0);
2092 assert(flags
== UINT32_MAX
);
2097 /* Check the validity of chinese key*/
2098 static test_return
user_supplied_bug17(memcached_st
*memc
)
2100 memcached_return rc
;
2102 char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2107 rc
= memcached_set(memc
, key
, strlen(key
),
2108 value
, strlen(value
),
2111 assert(rc
== MEMCACHED_SUCCESS
);
2113 value2
= memcached_get(memc
, key
, strlen(key
),
2114 &length
, &flags
, &rc
);
2116 assert(length
==strlen(value
));
2117 assert(rc
== MEMCACHED_SUCCESS
);
2118 assert(memcmp(value
, value2
, length
)==0);
2125 static test_return
result_static(memcached_st
*memc
)
2127 memcached_result_st result
;
2128 memcached_result_st
*result_ptr
;
2130 result_ptr
= memcached_result_create(memc
, &result
);
2131 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2133 memcached_result_free(&result
);
2138 static test_return
result_alloc(memcached_st
*memc
)
2140 memcached_result_st
*result
;
2142 result
= memcached_result_create(memc
, NULL
);
2144 memcached_result_free(result
);
2149 static test_return
string_static_null(memcached_st
*memc
)
2151 memcached_string_st string
;
2152 memcached_string_st
*string_ptr
;
2154 string_ptr
= memcached_string_create(memc
, &string
, 0);
2155 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2157 memcached_string_free(&string
);
2162 static test_return
string_alloc_null(memcached_st
*memc
)
2164 memcached_string_st
*string
;
2166 string
= memcached_string_create(memc
, NULL
, 0);
2168 memcached_string_free(string
);
2173 static test_return
string_alloc_with_size(memcached_st
*memc
)
2175 memcached_string_st
*string
;
2177 string
= memcached_string_create(memc
, NULL
, 1024);
2179 memcached_string_free(string
);
2184 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2186 memcached_string_st
*string
;
2188 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
2189 assert(string
== NULL
);
2194 static test_return
string_alloc_append(memcached_st
*memc
)
2197 char buffer
[SMALL_STRING_LEN
];
2198 memcached_string_st
*string
;
2200 /* Ring the bell! */
2201 memset(buffer
, 6, SMALL_STRING_LEN
);
2203 string
= memcached_string_create(memc
, NULL
, 100);
2206 for (x
= 0; x
< 1024; x
++)
2208 memcached_return rc
;
2209 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2210 assert(rc
== MEMCACHED_SUCCESS
);
2212 memcached_string_free(string
);
2217 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2219 memcached_return rc
;
2221 char buffer
[SMALL_STRING_LEN
];
2222 memcached_string_st
*string
;
2224 /* Ring the bell! */
2225 memset(buffer
, 6, SMALL_STRING_LEN
);
2227 string
= memcached_string_create(memc
, NULL
, 100);
2230 for (x
= 0; x
< 1024; x
++)
2232 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2233 assert(rc
== MEMCACHED_SUCCESS
);
2235 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
2236 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2237 memcached_string_free(string
);
2242 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2244 pairs_free(global_pairs
);
2249 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2251 unsigned long long x
;
2252 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2253 global_count
= GLOBAL_COUNT
;
2255 for (x
= 0; x
< global_count
; x
++)
2257 global_keys
[x
]= global_pairs
[x
].key
;
2258 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2264 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2266 unsigned long long x
;
2267 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2268 global_count
= GLOBAL2_COUNT
;
2270 for (x
= 0; x
< global_count
; x
++)
2272 global_keys
[x
]= global_pairs
[x
].key
;
2273 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2279 static test_return
generate_data(memcached_st
*memc
)
2281 execute_set(memc
, global_pairs
, global_count
);
2286 static test_return
generate_data_with_stats(memcached_st
*memc
)
2288 memcached_stat_st
*stat_p
;
2289 memcached_return rc
;
2290 uint32_t host_index
= 0;
2291 execute_set(memc
, global_pairs
, global_count
);
2293 //TODO: hosts used size stats
2294 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2297 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2299 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
);
2302 memcached_stat_free(NULL
, stat_p
);
2306 static test_return
generate_buffer_data(memcached_st
*memc
)
2311 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2312 generate_data(memc
);
2317 static test_return
get_read_count(memcached_st
*memc
)
2320 memcached_return rc
;
2321 memcached_st
*clone
;
2323 clone
= memcached_clone(NULL
, memc
);
2326 memcached_server_add(clone
, "localhost", 6666);
2330 size_t return_value_length
;
2334 for (x
= count
= 0; x
< global_count
; x
++)
2336 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2337 &return_value_length
, &flags
, &rc
);
2338 if (rc
== MEMCACHED_SUCCESS
)
2345 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2348 memcached_free(clone
);
2353 static test_return
get_read(memcached_st
*memc
)
2356 memcached_return rc
;
2360 size_t return_value_length
;
2363 for (x
= 0; x
< global_count
; x
++)
2365 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2366 &return_value_length
, &flags
, &rc
);
2368 assert(return_value);
2369 assert(rc == MEMCACHED_SUCCESS);
2371 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2379 static test_return
mget_read(memcached_st
*memc
)
2381 memcached_return rc
;
2383 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2384 assert(rc
== MEMCACHED_SUCCESS
);
2385 /* Turn this into a help function */
2387 char return_key
[MEMCACHED_MAX_KEY
];
2388 size_t return_key_length
;
2390 size_t return_value_length
;
2393 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2394 &return_value_length
, &flags
, &rc
)))
2396 assert(return_value
);
2397 assert(rc
== MEMCACHED_SUCCESS
);
2405 static test_return
mget_read_result(memcached_st
*memc
)
2407 memcached_return rc
;
2409 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2410 assert(rc
== MEMCACHED_SUCCESS
);
2411 /* Turn this into a help function */
2413 memcached_result_st results_obj
;
2414 memcached_result_st
*results
;
2416 results
= memcached_result_create(memc
, &results_obj
);
2418 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2421 assert(rc
== MEMCACHED_SUCCESS
);
2424 memcached_result_free(&results_obj
);
2430 static test_return
mget_read_function(memcached_st
*memc
)
2432 memcached_return rc
;
2433 unsigned int counter
;
2434 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
2436 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2437 assert(rc
== MEMCACHED_SUCCESS
);
2439 callbacks
[0]= &callback_counter
;
2441 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2446 static test_return
delete_generate(memcached_st
*memc
)
2450 for (x
= 0; x
< global_count
; x
++)
2452 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2458 static test_return
delete_buffer_generate(memcached_st
*memc
)
2464 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2466 for (x
= 0; x
< global_count
; x
++)
2468 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2474 static test_return
free_data(memcached_st
*memc
__attribute__((unused
)))
2476 pairs_free(global_pairs
);
2481 static test_return
add_host_test1(memcached_st
*memc
)
2484 memcached_return rc
;
2485 char servername
[]= "0.example.com";
2486 memcached_server_st
*servers
;
2488 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
2490 assert(1 == memcached_server_list_count(servers
));
2492 for (x
= 2; x
< 20; x
++)
2494 char buffer
[SMALL_STRING_LEN
];
2496 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2497 servers
= memcached_server_list_append(servers
, buffer
, 401,
2499 assert(rc
== MEMCACHED_SUCCESS
);
2500 assert(x
== memcached_server_list_count(servers
));
2503 rc
= memcached_server_push(memc
, servers
);
2504 assert(rc
== MEMCACHED_SUCCESS
);
2505 rc
= memcached_server_push(memc
, servers
);
2506 assert(rc
== MEMCACHED_SUCCESS
);
2508 memcached_server_list_free(servers
);
2513 static memcached_return
pre_nonblock(memcached_st
*memc
)
2515 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2517 return MEMCACHED_SUCCESS
;
2520 static memcached_return
pre_murmur(memcached_st
*memc
)
2522 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2524 return MEMCACHED_SUCCESS
;
2527 static memcached_return
pre_md5(memcached_st
*memc
)
2529 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2531 return MEMCACHED_SUCCESS
;
2534 static memcached_return
pre_crc(memcached_st
*memc
)
2536 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2538 return MEMCACHED_SUCCESS
;
2541 static memcached_return
pre_hsieh(memcached_st
*memc
)
2543 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2545 return MEMCACHED_SUCCESS
;
2548 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2550 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2552 return MEMCACHED_SUCCESS
;
2555 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2557 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2559 return MEMCACHED_SUCCESS
;
2562 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2564 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2566 return MEMCACHED_SUCCESS
;
2569 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2571 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2573 return MEMCACHED_SUCCESS
;
2576 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
2578 memcached_return rc
;
2581 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
2582 assert(rc
== MEMCACHED_SUCCESS
);
2584 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
2587 return MEMCACHED_SUCCESS
;
2590 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
2592 memcached_return rc
;
2595 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2596 assert(rc
== MEMCACHED_SUCCESS
);
2598 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2601 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2602 assert(rc
== MEMCACHED_SUCCESS
);
2604 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2605 assert(value
== MEMCACHED_HASH_MD5
);
2606 return MEMCACHED_SUCCESS
;
2609 static memcached_return
pre_binary(memcached_st
*memc
)
2611 memcached_return rc
= MEMCACHED_FAILURE
;
2612 memcached_st
*clone
;
2614 clone
= memcached_clone(NULL
, memc
);
2616 // The memcached_version needs to be done on a clone, because the server
2617 // will not toggle protocol on an connection.
2618 memcached_version(clone
);
2620 if (clone
->hosts
[0].major_version
>= 1 && clone
->hosts
[0].minor_version
> 2)
2622 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2623 assert(rc
== MEMCACHED_SUCCESS
);
2624 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2627 memcached_free(clone
);
2631 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
2636 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
2638 return malloc(size
);
2641 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
2643 return realloc(mem
, size
);
2646 static memcached_return
set_prefix(memcached_st
*memc
)
2648 memcached_return rc
;
2649 const char *key
= "mine";
2652 /* Make sure be default none exists */
2653 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2654 assert(rc
== MEMCACHED_FAILURE
);
2656 /* Test a clean set */
2657 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2658 assert(rc
== MEMCACHED_SUCCESS
);
2660 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2661 assert(memcmp(value
, key
, 4) == 0);
2662 assert(rc
== MEMCACHED_SUCCESS
);
2664 /* Test that we can turn it off */
2665 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2666 assert(rc
== MEMCACHED_SUCCESS
);
2668 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2669 assert(rc
== MEMCACHED_FAILURE
);
2671 /* Now setup for main test */
2672 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2673 assert(rc
== MEMCACHED_SUCCESS
);
2675 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2676 assert(rc
== MEMCACHED_SUCCESS
);
2677 assert(memcmp(value
, key
, 4) == 0);
2679 /* Set to Zero, and then Set to something too large */
2682 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2683 assert(rc
== MEMCACHED_SUCCESS
);
2685 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2686 assert(rc
== MEMCACHED_FAILURE
);
2687 assert(value
== NULL
);
2689 /* Test a long key for failure */
2690 long_key
= "Thisismorethentheallottednumberofcharacters";
2691 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2692 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2694 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2695 long_key
= "This is more then the allotted number of characters";
2696 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2697 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2699 /* Test for a bad prefix, but with a short key */
2700 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
2701 assert(rc
== MEMCACHED_SUCCESS
);
2703 long_key
= "dog cat";
2704 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2705 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2708 return MEMCACHED_SUCCESS
;
2711 static memcached_return
set_memory_alloc(memcached_st
*memc
)
2714 memcached_malloc_function test_ptr
;
2715 memcached_return rc
;
2717 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2718 assert(rc
== MEMCACHED_SUCCESS
);
2719 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2720 assert(rc
== MEMCACHED_SUCCESS
);
2721 assert(test_ptr
== my_malloc
);
2725 memcached_realloc_function test_ptr
;
2726 memcached_return rc
;
2728 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2729 assert(rc
== MEMCACHED_SUCCESS
);
2730 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2731 assert(rc
== MEMCACHED_SUCCESS
);
2732 assert(test_ptr
== my_realloc
);
2736 memcached_free_function test_ptr
;
2737 memcached_return rc
;
2739 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2740 assert(rc
== MEMCACHED_SUCCESS
);
2741 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2742 assert(rc
== MEMCACHED_SUCCESS
);
2743 assert(test_ptr
== my_free
);
2746 return MEMCACHED_SUCCESS
;
2749 static memcached_return
enable_consistent(memcached_st
*memc
)
2751 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2752 memcached_hash hash
;
2753 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2756 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2757 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2759 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2760 assert(hash
== MEMCACHED_HASH_HSIEH
);
2763 return MEMCACHED_SUCCESS
;
2766 static memcached_return
enable_cas(memcached_st
*memc
)
2768 unsigned int set
= 1;
2770 memcached_version(memc
);
2772 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
2773 || memc
->hosts
[0].minor_version
> 2)
2775 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
2777 return MEMCACHED_SUCCESS
;
2780 return MEMCACHED_FAILURE
;
2783 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
2785 memcached_version(memc
);
2787 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
2788 || memc
->hosts
[0].minor_version
> 2)
2789 return MEMCACHED_SUCCESS
;
2791 return MEMCACHED_FAILURE
;
2794 static memcached_return
pre_unix_socket(memcached_st
*memc
)
2796 memcached_return rc
;
2799 memcached_server_list_free(memc
->hosts
);
2801 memc
->number_of_hosts
= 0;
2803 if (stat("/tmp/memcached.socket", &buf
))
2804 return MEMCACHED_FAILURE
;
2806 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2811 static memcached_return
pre_nodelay(memcached_st
*memc
)
2813 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2814 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2816 return MEMCACHED_SUCCESS
;
2819 static memcached_return
pre_settimer(memcached_st
*memc
)
2821 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
2822 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
2824 return MEMCACHED_SUCCESS
;
2827 static memcached_return
poll_timeout(memcached_st
*memc
)
2833 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
2835 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2837 assert(timeout
== 100);
2839 return MEMCACHED_SUCCESS
;
2843 /* Clean the server before beginning testing */
2845 {"flush", 0, flush_test
},
2846 {"init", 0, init_test
},
2847 {"allocation", 0, allocation_test
},
2848 {"server_list_null_test", 0, server_list_null_test
},
2849 {"server_unsort", 0, server_unsort_test
},
2850 {"server_sort", 0, server_sort_test
},
2851 {"server_sort2", 0, server_sort2_test
},
2852 {"clone_test", 0, clone_test
},
2853 {"error", 0, error_test
},
2854 {"set", 0, set_test
},
2855 {"set2", 0, set_test2
},
2856 {"set3", 0, set_test3
},
2857 {"add", 1, add_test
},
2858 {"replace", 1, replace_test
},
2859 {"delete", 1, delete_test
},
2860 {"get", 1, get_test
},
2861 {"get2", 0, get_test2
},
2862 {"get3", 0, get_test3
},
2863 {"get4", 0, get_test4
},
2864 {"stats_servername", 0, stats_servername_test
},
2865 {"increment", 0, increment_test
},
2866 {"decrement", 0, decrement_test
},
2867 {"quit", 0, quit_test
},
2868 {"mget", 1, mget_test
},
2869 {"mget_result", 1, mget_result_test
},
2870 {"mget_result_alloc", 1, mget_result_alloc_test
},
2871 {"mget_result_function", 1, mget_result_function
},
2872 {"get_stats", 0, get_stats
},
2873 {"add_host_test", 0, add_host_test
},
2874 {"add_host_test_1", 0, add_host_test1
},
2875 {"get_stats_keys", 0, get_stats_keys
},
2876 {"behavior_test", 0, get_stats_keys
},
2877 {"callback_test", 0, get_stats_keys
},
2878 {"version_string_test", 0, version_string_test
},
2879 {"bad_key", 1, bad_key_test
},
2880 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
2881 {"read_through", 1, read_through
},
2882 {"delete_through", 1, delete_through
},
2886 test_st async_tests
[] ={
2887 {"add", 1, add_wrapper
},
2891 test_st string_tests
[] ={
2892 {"string static with null", 0, string_static_null
},
2893 {"string alloc with null", 0, string_alloc_null
},
2894 {"string alloc with 1K", 0, string_alloc_with_size
},
2895 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2896 {"string append", 0, string_alloc_append
},
2897 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2901 test_st result_tests
[] ={
2902 {"result static", 0, result_static
},
2903 {"result alloc", 0, result_alloc
},
2907 test_st version_1_2_3
[] ={
2908 {"append", 0, append_test
},
2909 {"prepend", 0, prepend_test
},
2910 {"cas", 0, cas_test
},
2911 {"cas2", 0, cas2_test
},
2912 {"append_binary", 0, append_binary_test
},
2916 test_st user_tests
[] ={
2917 {"user_supplied_bug1", 0, user_supplied_bug1
},
2918 {"user_supplied_bug2", 0, user_supplied_bug2
},
2919 {"user_supplied_bug3", 0, user_supplied_bug3
},
2920 {"user_supplied_bug4", 0, user_supplied_bug4
},
2921 {"user_supplied_bug5", 1, user_supplied_bug5
},
2922 {"user_supplied_bug6", 1, user_supplied_bug6
},
2923 {"user_supplied_bug7", 1, user_supplied_bug7
},
2924 {"user_supplied_bug8", 1, user_supplied_bug8
},
2925 {"user_supplied_bug9", 1, user_supplied_bug9
},
2926 {"user_supplied_bug10", 1, user_supplied_bug10
},
2927 {"user_supplied_bug11", 1, user_supplied_bug11
},
2928 {"user_supplied_bug12", 1, user_supplied_bug12
},
2929 {"user_supplied_bug13", 1, user_supplied_bug13
},
2930 {"user_supplied_bug14", 1, user_supplied_bug14
},
2931 {"user_supplied_bug15", 1, user_supplied_bug15
},
2932 {"user_supplied_bug16", 1, user_supplied_bug16
},
2933 {"user_supplied_bug17", 1, user_supplied_bug17
},
2937 test_st generate_tests
[] ={
2938 {"generate_pairs", 1, generate_pairs
},
2939 {"generate_data", 1, generate_data
},
2940 {"get_read", 0, get_read
},
2941 {"delete_generate", 0, delete_generate
},
2942 {"generate_buffer_data", 1, generate_buffer_data
},
2943 {"delete_buffer", 0, delete_buffer_generate
},
2944 {"generate_data", 1, generate_data
},
2945 {"mget_read", 0, mget_read
},
2946 {"mget_read_result", 0, mget_read_result
},
2947 {"mget_read_function", 0, mget_read_function
},
2948 {"cleanup", 1, cleanup_pairs
},
2949 {"generate_large_pairs", 1, generate_large_pairs
},
2950 {"generate_data", 1, generate_data
},
2951 {"generate_buffer_data", 1, generate_buffer_data
},
2952 {"cleanup", 1, cleanup_pairs
},
2956 test_st consistent_tests
[] ={
2957 {"generate_pairs", 1, generate_pairs
},
2958 {"generate_data", 1, generate_data
},
2959 {"get_read", 0, get_read_count
},
2960 {"cleanup", 1, cleanup_pairs
},
2964 test_st consistent_weighted_tests
[] ={
2965 {"generate_pairs", 1, generate_pairs
},
2966 {"generate_data", 1, generate_data_with_stats
},
2967 {"get_read", 0, get_read_count
},
2968 {"cleanup", 1, cleanup_pairs
},
2972 collection_st collection
[] ={
2973 {"block", 0, 0, tests
},
2974 {"binary", pre_binary
, 0, tests
},
2975 {"nonblock", pre_nonblock
, 0, tests
},
2976 {"nodelay", pre_nodelay
, 0, tests
},
2977 {"settimer", pre_settimer
, 0, tests
},
2978 {"md5", pre_md5
, 0, tests
},
2979 {"crc", pre_crc
, 0, tests
},
2980 {"hsieh", pre_hsieh
, 0, tests
},
2981 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2982 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2983 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2984 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2985 {"ketama", pre_behavior_ketama
, 0, tests
},
2986 {"unix_socket", pre_unix_socket
, 0, tests
},
2987 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2988 {"poll_timeout", poll_timeout
, 0, tests
},
2989 {"gets", enable_cas
, 0, tests
},
2990 {"consistent", enable_consistent
, 0, tests
},
2991 {"memory_allocators", set_memory_alloc
, 0, tests
},
2992 {"prefix", set_prefix
, 0, tests
},
2993 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2994 {"string", 0, 0, string_tests
},
2995 {"result", 0, 0, result_tests
},
2996 {"async", pre_nonblock
, 0, async_tests
},
2997 {"user", 0, 0, user_tests
},
2998 {"generate", 0, 0, generate_tests
},
2999 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
3000 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
3001 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
3002 {"generate_md5", pre_md5
, 0, generate_tests
},
3003 {"generate_murmur", pre_murmur
, 0, generate_tests
},
3004 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
3005 {"consistent_not", 0, 0, consistent_tests
},
3006 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
3007 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
3011 #define SERVERS_TO_CREATE 5
3013 /* Prototypes for functions we will pass to test framework */
3014 void *world_create(void);
3015 void world_destroy(void *p
);
3017 void *world_create(void)
3019 server_startup_st
*construct
;
3021 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
3022 memset(construct
, 0, sizeof(server_startup_st
));
3023 construct
->count
= SERVERS_TO_CREATE
;
3025 server_startup(construct
);
3031 void world_destroy(void *p
)
3033 server_startup_st
*construct
= (server_startup_st
*)p
;
3034 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
3035 memcached_server_list_free(servers
);
3037 server_shutdown(construct
);
3041 void get_world(world_st
*world
)
3043 world
->collections
= collection
;
3044 world
->create
= world_create
;
3045 world
->destroy
= world_destroy
;