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 memset(&declared_clone
, 0 , sizeof(memcached_st
));
215 clone
= memcached_clone(&declared_clone
, NULL
);
217 memcached_free(clone
);
220 /* Can we init from struct? */
222 memcached_st declared_clone
;
224 memset(&declared_clone
, 0 , sizeof(memcached_st
));
225 clone
= memcached_clone(&declared_clone
, memc
);
227 memcached_free(clone
);
233 static test_return
connection_test(memcached_st
*memc
)
237 rc
= memcached_server_add(memc
, "localhost", 0);
238 assert(rc
== MEMCACHED_SUCCESS
);
243 static test_return
error_test(memcached_st
*memc
)
247 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
249 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
255 static test_return
set_test(memcached_st
*memc
)
259 char *value
= "when we sanitize";
261 rc
= memcached_set(memc
, key
, strlen(key
),
262 value
, strlen(value
),
263 (time_t)0, (uint32_t)0);
264 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
269 static test_return
append_test(memcached_st
*memc
)
277 rc
= memcached_flush(memc
, 0);
278 assert(rc
== MEMCACHED_SUCCESS
);
280 rc
= memcached_set(memc
, key
, strlen(key
),
281 value
, strlen(value
),
282 (time_t)0, (uint32_t)0);
283 assert(rc
== MEMCACHED_SUCCESS
);
285 rc
= memcached_append(memc
, key
, strlen(key
),
286 " the", strlen(" the"),
287 (time_t)0, (uint32_t)0);
288 assert(rc
== MEMCACHED_SUCCESS
);
290 rc
= memcached_append(memc
, key
, strlen(key
),
291 " people", strlen(" people"),
292 (time_t)0, (uint32_t)0);
293 assert(rc
== MEMCACHED_SUCCESS
);
295 value
= memcached_get(memc
, key
, strlen(key
),
296 &value_length
, &flags
, &rc
);
297 assert(!memcmp(value
, "we the people", strlen("we the people")));
298 assert(strlen("we the people") == value_length
);
299 assert(rc
== MEMCACHED_SUCCESS
);
305 static test_return
append_binary_test(memcached_st
*memc
)
308 char *key
= "numbers";
309 unsigned int *store_ptr
;
310 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
316 rc
= memcached_flush(memc
, 0);
317 assert(rc
== MEMCACHED_SUCCESS
);
319 rc
= memcached_set(memc
,
322 (time_t)0, (uint32_t)0);
323 assert(rc
== MEMCACHED_SUCCESS
);
325 for (x
= 0; store_list
[x
] ; x
++)
327 rc
= memcached_append(memc
,
329 (char *)&store_list
[x
], sizeof(unsigned int),
330 (time_t)0, (uint32_t)0);
331 assert(rc
== MEMCACHED_SUCCESS
);
334 value
= memcached_get(memc
, key
, strlen(key
),
335 &value_length
, &flags
, &rc
);
336 assert((value_length
== (sizeof(unsigned int) * x
)));
337 assert(rc
== MEMCACHED_SUCCESS
);
339 store_ptr
= (unsigned int *)value
;
341 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
343 assert(*store_ptr
== store_list
[x
++]);
351 static test_return
cas2_test(memcached_st
*memc
)
354 char *keys
[]= {"fudge", "son", "food"};
355 size_t key_length
[]= {5, 3, 4};
356 char *value
= "we the people";
357 size_t value_length
= strlen("we the people");
359 memcached_result_st results_obj
;
360 memcached_result_st
*results
;
363 rc
= memcached_flush(memc
, 0);
364 assert(rc
== MEMCACHED_SUCCESS
);
366 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
368 for (x
= 0; x
< 3; x
++)
370 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
371 keys
[x
], key_length
[x
],
372 (time_t)50, (uint32_t)9);
373 assert(rc
== MEMCACHED_SUCCESS
);
376 rc
= memcached_mget(memc
, keys
, key_length
, 3);
378 results
= memcached_result_create(memc
, &results_obj
);
380 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
382 assert(results
->cas
);
383 assert(rc
== MEMCACHED_SUCCESS
);
384 WATCHPOINT_ASSERT(memcached_result_cas(results
));
386 assert(!memcmp(value
, "we the people", strlen("we the people")));
387 assert(strlen("we the people") == value_length
);
388 assert(rc
== MEMCACHED_SUCCESS
);
390 memcached_result_free(&results_obj
);
395 static test_return
cas_test(memcached_st
*memc
)
399 size_t key_length
= strlen("fun");
400 char *value
= "we the people";
401 size_t value_length
= strlen("we the people");
402 memcached_result_st results_obj
;
403 memcached_result_st
*results
;
406 rc
= memcached_flush(memc
, 0);
407 assert(rc
== MEMCACHED_SUCCESS
);
409 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
411 rc
= memcached_set(memc
, key
, strlen(key
),
412 value
, strlen(value
),
413 (time_t)0, (uint32_t)0);
414 assert(rc
== MEMCACHED_SUCCESS
);
416 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
418 results
= memcached_result_create(memc
, &results_obj
);
420 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
422 assert(rc
== MEMCACHED_SUCCESS
);
423 WATCHPOINT_ASSERT(memcached_result_cas(results
));
425 assert(!memcmp(value
, "we the people", strlen("we the people")));
426 assert(strlen("we the people") == value_length
);
427 assert(rc
== MEMCACHED_SUCCESS
);
429 rc
= memcached_cas(memc
, key
, key_length
,
430 "change the value", strlen("change the value"),
431 0, 0, memcached_result_cas(results
));
433 assert(rc
== MEMCACHED_SUCCESS
);
435 rc
= memcached_cas(memc
, key
, key_length
,
436 "change the value", strlen("change the value"),
439 assert(rc
== MEMCACHED_DATA_EXISTS
);
442 memcached_result_free(&results_obj
);
447 static test_return
prepend_test(memcached_st
*memc
)
451 char *value
= "people";
455 rc
= memcached_flush(memc
, 0);
456 assert(rc
== MEMCACHED_SUCCESS
);
458 rc
= memcached_set(memc
, key
, strlen(key
),
459 value
, strlen(value
),
460 (time_t)0, (uint32_t)0);
461 assert(rc
== MEMCACHED_SUCCESS
);
463 rc
= memcached_prepend(memc
, key
, strlen(key
),
464 "the ", strlen("the "),
465 (time_t)0, (uint32_t)0);
466 assert(rc
== MEMCACHED_SUCCESS
);
468 rc
= memcached_prepend(memc
, key
, strlen(key
),
469 "we ", strlen("we "),
470 (time_t)0, (uint32_t)0);
471 assert(rc
== MEMCACHED_SUCCESS
);
473 value
= memcached_get(memc
, key
, strlen(key
),
474 &value_length
, &flags
, &rc
);
475 assert(!memcmp(value
, "we the people", strlen("we the people")));
476 assert(strlen("we the people") == value_length
);
477 assert(rc
== MEMCACHED_SUCCESS
);
484 Set the value, then quit to make sure it is flushed.
485 Come back in and test that add fails.
487 static test_return
add_test(memcached_st
*memc
)
491 char *value
= "when we sanitize";
492 unsigned long long setting_value
;
494 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
496 rc
= memcached_set(memc
, key
, strlen(key
),
497 value
, strlen(value
),
498 (time_t)0, (uint32_t)0);
499 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
500 memcached_quit(memc
);
501 rc
= memcached_add(memc
, key
, strlen(key
),
502 value
, strlen(value
),
503 (time_t)0, (uint32_t)0);
505 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
507 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
509 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
514 static test_return
add_wrapper(memcached_st
*memc
)
518 for (x
= 0; x
< 10000; x
++)
524 static test_return
replace_test(memcached_st
*memc
)
528 char *value
= "when we sanitize";
529 char *original
= "first we insert some data";
531 rc
= memcached_set(memc
, key
, strlen(key
),
532 original
, strlen(original
),
533 (time_t)0, (uint32_t)0);
534 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
536 rc
= memcached_replace(memc
, key
, strlen(key
),
537 value
, strlen(value
),
538 (time_t)0, (uint32_t)0);
539 assert(rc
== MEMCACHED_SUCCESS
);
544 static test_return
delete_test(memcached_st
*memc
)
548 char *value
= "when we sanitize";
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
);
555 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
556 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
561 static test_return
flush_test(memcached_st
*memc
)
565 rc
= memcached_flush(memc
, 0);
566 assert(rc
== MEMCACHED_SUCCESS
);
571 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
572 memcached_server_st
*server
__attribute__((unused
)),
573 void *context
__attribute__((unused
)))
577 return MEMCACHED_SUCCESS
;
580 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
582 char *context
= "foo bad";
583 memcached_server_function callbacks
[1];
585 callbacks
[0]= server_function
;
586 memcached_server_cursor(memc
, callbacks
, context
, 1);
591 static test_return
bad_key_test(memcached_st
*memc
)
594 char *key
= "foo bad";
596 size_t string_length
;
601 clone
= memcached_clone(NULL
, memc
);
604 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
605 assert(rc
== MEMCACHED_SUCCESS
);
607 string
= memcached_get(clone
, key
, strlen(key
),
608 &string_length
, &flags
, &rc
);
609 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
610 assert(string_length
== 0);
614 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
615 assert(rc
== MEMCACHED_SUCCESS
);
616 string
= memcached_get(clone
, key
, strlen(key
),
617 &string_length
, &flags
, &rc
);
618 assert(rc
== MEMCACHED_NOTFOUND
);
619 assert(string_length
== 0);
622 /* Test multi key for bad keys */
624 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
625 size_t key_lengths
[] = { 7, 7, 7 };
627 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
628 assert(rc
== MEMCACHED_SUCCESS
);
630 rc
= memcached_mget(clone
, keys
, key_lengths
, 3);
631 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
633 rc
= memcached_mget_by_key(clone
, "foo daddy", 9, keys
, key_lengths
, 1);
634 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
637 /* Make sure zero length keys are marked as bad */
639 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
640 assert(rc
== MEMCACHED_SUCCESS
);
641 string
= memcached_get(clone
, key
, 0,
642 &string_length
, &flags
, &rc
);
643 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
644 assert(string_length
== 0);
647 memcached_free(clone
);
652 #define READ_THROUGH_VALUE "set for me"
653 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
654 char *key
__attribute__((unused
)),
655 size_t key_length
__attribute__((unused
)),
656 memcached_result_st
*result
)
659 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
662 static test_return
read_through(memcached_st
*memc
)
667 size_t string_length
;
670 string
= memcached_get(memc
, key
, strlen(key
),
671 &string_length
, &flags
, &rc
);
673 assert(rc
== MEMCACHED_NOTFOUND
);
674 assert(string_length
== 0);
677 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, (void *)read_through_trigger
);
678 assert(rc
== MEMCACHED_SUCCESS
);
680 string
= memcached_get(memc
, key
, strlen(key
),
681 &string_length
, &flags
, &rc
);
683 assert(rc
== MEMCACHED_SUCCESS
);
684 assert(string_length
== strlen(READ_THROUGH_VALUE
));
685 assert(!strcmp(READ_THROUGH_VALUE
, string
));
688 string
= memcached_get(memc
, key
, strlen(key
),
689 &string_length
, &flags
, &rc
);
691 assert(rc
== MEMCACHED_SUCCESS
);
692 assert(string_length
== strlen(READ_THROUGH_VALUE
));
693 assert(!strcmp(READ_THROUGH_VALUE
, string
));
699 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
701 size_t key_length
__attribute__((unused
)))
705 return MEMCACHED_SUCCESS
;
708 static test_return
delete_through(memcached_st
*memc
)
710 memcached_trigger_delete_key callback
;
713 callback
= delete_trigger
;
715 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, callback
);
716 assert(rc
== MEMCACHED_SUCCESS
);
721 static test_return
get_test(memcached_st
*memc
)
726 size_t string_length
;
729 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
730 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
732 string
= memcached_get(memc
, key
, strlen(key
),
733 &string_length
, &flags
, &rc
);
735 assert(rc
== MEMCACHED_NOTFOUND
);
736 assert(string_length
== 0);
742 static test_return
get_test2(memcached_st
*memc
)
746 char *value
= "when we sanitize";
748 size_t string_length
;
751 rc
= memcached_set(memc
, key
, strlen(key
),
752 value
, strlen(value
),
753 (time_t)0, (uint32_t)0);
754 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
756 string
= memcached_get(memc
, key
, strlen(key
),
757 &string_length
, &flags
, &rc
);
760 assert(rc
== MEMCACHED_SUCCESS
);
761 assert(string_length
== strlen(value
));
762 assert(!memcmp(string
, value
, string_length
));
769 static test_return
set_test2(memcached_st
*memc
)
773 char *value
= "train in the brain";
774 size_t value_length
= strlen(value
);
777 for (x
= 0; x
< 10; x
++)
779 rc
= memcached_set(memc
, key
, strlen(key
),
781 (time_t)0, (uint32_t)0);
782 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
788 static test_return
set_test3(memcached_st
*memc
)
793 size_t value_length
= 8191;
796 value
= (char*)malloc(value_length
);
799 for (x
= 0; x
< value_length
; x
++)
800 value
[x
] = (char) (x
% 127);
802 for (x
= 0; x
< 1; x
++)
804 rc
= memcached_set(memc
, key
, strlen(key
),
806 (time_t)0, (uint32_t)0);
807 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
815 static test_return
get_test3(memcached_st
*memc
)
820 size_t value_length
= 8191;
822 size_t string_length
;
826 value
= (char*)malloc(value_length
);
829 for (x
= 0; x
< value_length
; x
++)
830 value
[x
] = (char) (x
% 127);
832 rc
= memcached_set(memc
, key
, strlen(key
),
834 (time_t)0, (uint32_t)0);
835 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
837 string
= memcached_get(memc
, key
, strlen(key
),
838 &string_length
, &flags
, &rc
);
840 assert(rc
== MEMCACHED_SUCCESS
);
842 assert(string_length
== value_length
);
843 assert(!memcmp(string
, value
, string_length
));
851 static test_return
get_test4(memcached_st
*memc
)
856 size_t value_length
= 8191;
858 size_t string_length
;
862 value
= (char*)malloc(value_length
);
865 for (x
= 0; x
< value_length
; x
++)
866 value
[x
] = (char) (x
% 127);
868 rc
= memcached_set(memc
, key
, strlen(key
),
870 (time_t)0, (uint32_t)0);
871 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
873 for (x
= 0; x
< 10; x
++)
875 string
= memcached_get(memc
, key
, strlen(key
),
876 &string_length
, &flags
, &rc
);
878 assert(rc
== MEMCACHED_SUCCESS
);
880 assert(string_length
== value_length
);
881 assert(!memcmp(string
, value
, string_length
));
890 /* Do not copy the style of this code, I just access hosts to testthis function */
891 static test_return
stats_servername_test(memcached_st
*memc
)
894 memcached_stat_st stat
;
895 rc
= memcached_stat_servername(&stat
, NULL
,
896 memc
->hosts
[0].hostname
,
897 memc
->hosts
[0].port
);
902 static test_return
increment_test(memcached_st
*memc
)
909 rc
= memcached_set(memc
, key
, strlen(key
),
910 value
, strlen(value
),
911 (time_t)0, (uint32_t)0);
912 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
914 rc
= memcached_increment(memc
, key
, strlen(key
),
916 assert(rc
== MEMCACHED_SUCCESS
);
917 assert(new_number
== 1);
919 rc
= memcached_increment(memc
, key
, strlen(key
),
921 assert(rc
== MEMCACHED_SUCCESS
);
922 assert(new_number
== 2);
927 static test_return
decrement_test(memcached_st
*memc
)
934 rc
= memcached_set(memc
, key
, strlen(key
),
935 value
, strlen(value
),
936 (time_t)0, (uint32_t)0);
937 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
939 rc
= memcached_decrement(memc
, key
, strlen(key
),
941 assert(rc
== MEMCACHED_SUCCESS
);
942 assert(new_number
== 2);
944 rc
= memcached_decrement(memc
, key
, strlen(key
),
946 assert(rc
== MEMCACHED_SUCCESS
);
947 assert(new_number
== 1);
952 static test_return
quit_test(memcached_st
*memc
)
956 char *value
= "sanford and sun";
958 rc
= memcached_set(memc
, key
, strlen(key
),
959 value
, strlen(value
),
960 (time_t)10, (uint32_t)3);
961 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
962 memcached_quit(memc
);
964 rc
= memcached_set(memc
, key
, strlen(key
),
965 value
, strlen(value
),
966 (time_t)50, (uint32_t)9);
967 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
972 static test_return
mget_result_test(memcached_st
*memc
)
975 char *keys
[]= {"fudge", "son", "food"};
976 size_t key_length
[]= {5, 3, 4};
979 memcached_result_st results_obj
;
980 memcached_result_st
*results
;
982 results
= memcached_result_create(memc
, &results_obj
);
984 assert(&results_obj
== results
);
986 /* We need to empty the server before continueing test */
987 rc
= memcached_flush(memc
, 0);
988 assert(rc
== MEMCACHED_SUCCESS
);
990 rc
= memcached_mget(memc
, keys
, key_length
, 3);
991 assert(rc
== MEMCACHED_SUCCESS
);
993 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
998 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1000 assert(rc
== MEMCACHED_END
);
1002 for (x
= 0; x
< 3; x
++)
1004 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1005 keys
[x
], key_length
[x
],
1006 (time_t)50, (uint32_t)9);
1007 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1010 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1011 assert(rc
== MEMCACHED_SUCCESS
);
1013 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1016 assert(&results_obj
== results
);
1017 assert(rc
== MEMCACHED_SUCCESS
);
1018 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1019 assert(!memcmp(memcached_result_key_value(results
),
1020 memcached_result_value(results
),
1021 memcached_result_length(results
)));
1024 memcached_result_free(&results_obj
);
1029 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1031 memcached_return rc
;
1032 char *keys
[]= {"fudge", "son", "food"};
1033 size_t key_length
[]= {5, 3, 4};
1036 memcached_result_st
*results
;
1038 /* We need to empty the server before continueing test */
1039 rc
= memcached_flush(memc
, 0);
1040 assert(rc
== MEMCACHED_SUCCESS
);
1042 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1043 assert(rc
== MEMCACHED_SUCCESS
);
1045 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1050 assert(rc
== MEMCACHED_END
);
1052 for (x
= 0; x
< 3; x
++)
1054 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1055 keys
[x
], key_length
[x
],
1056 (time_t)50, (uint32_t)9);
1057 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1060 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1061 assert(rc
== MEMCACHED_SUCCESS
);
1064 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1067 assert(rc
== MEMCACHED_SUCCESS
);
1068 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1069 assert(!memcmp(memcached_result_key_value(results
),
1070 memcached_result_value(results
),
1071 memcached_result_length(results
)));
1072 memcached_result_free(results
);
1079 /* Count the results */
1080 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1081 memcached_result_st
*result
__attribute__((unused
)),
1084 unsigned int *counter
= (unsigned int *)context
;
1086 *counter
= *counter
+ 1;
1088 return MEMCACHED_SUCCESS
;
1091 static test_return
mget_result_function(memcached_st
*memc
)
1093 memcached_return rc
;
1094 char *keys
[]= {"fudge", "son", "food"};
1095 size_t key_length
[]= {5, 3, 4};
1097 unsigned int counter
;
1098 memcached_execute_function callbacks
[1];
1100 /* We need to empty the server before continueing test */
1101 rc
= memcached_flush(memc
, 0);
1102 for (x
= 0; x
< 3; x
++)
1104 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1105 keys
[x
], key_length
[x
],
1106 (time_t)50, (uint32_t)9);
1107 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1110 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1111 assert(rc
== MEMCACHED_SUCCESS
);
1113 callbacks
[0]= &callback_counter
;
1115 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1117 assert(counter
== 3);
1122 static test_return
mget_test(memcached_st
*memc
)
1124 memcached_return rc
;
1125 char *keys
[]= {"fudge", "son", "food"};
1126 size_t key_length
[]= {5, 3, 4};
1130 char return_key
[MEMCACHED_MAX_KEY
];
1131 size_t return_key_length
;
1133 size_t return_value_length
;
1135 /* We need to empty the server before continueing test */
1136 rc
= memcached_flush(memc
, 0);
1137 assert(rc
== MEMCACHED_SUCCESS
);
1139 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1140 assert(rc
== MEMCACHED_SUCCESS
);
1142 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1143 &return_value_length
, &flags
, &rc
)) != NULL
)
1145 assert(return_value
);
1147 assert(!return_value
);
1148 assert(return_value_length
== 0);
1149 assert(rc
== MEMCACHED_END
);
1151 for (x
= 0; x
< 3; x
++)
1153 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1154 keys
[x
], key_length
[x
],
1155 (time_t)50, (uint32_t)9);
1156 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1159 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1160 assert(rc
== MEMCACHED_SUCCESS
);
1163 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1164 &return_value_length
, &flags
, &rc
)))
1166 assert(return_value
);
1167 assert(rc
== MEMCACHED_SUCCESS
);
1168 assert(return_key_length
== return_value_length
);
1169 assert(!memcmp(return_value
, return_key
, return_value_length
));
1177 static test_return
get_stats_keys(memcached_st
*memc
)
1181 memcached_stat_st stat
;
1182 memcached_return rc
;
1184 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
1185 assert(rc
== MEMCACHED_SUCCESS
);
1186 for (ptr
= list
; *ptr
; ptr
++)
1195 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1197 const char *version_string
;
1199 version_string
= memcached_lib_version();
1201 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1206 static test_return
get_stats(memcached_st
*memc
)
1211 memcached_return rc
;
1212 memcached_stat_st
*stat
;
1214 stat
= memcached_stat(memc
, NULL
, &rc
);
1215 assert(rc
== MEMCACHED_SUCCESS
);
1217 assert(rc
== MEMCACHED_SUCCESS
);
1220 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1222 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
1223 assert(rc
== MEMCACHED_SUCCESS
);
1224 for (ptr
= list
; *ptr
; ptr
++);
1229 memcached_stat_free(NULL
, stat
);
1234 static test_return
add_host_test(memcached_st
*memc
)
1237 memcached_server_st
*servers
;
1238 memcached_return rc
;
1239 char servername
[]= "0.example.com";
1241 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1243 assert(1 == memcached_server_list_count(servers
));
1245 for (x
= 2; x
< 20; x
++)
1247 char buffer
[SMALL_STRING_LEN
];
1249 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1250 servers
= memcached_server_list_append(servers
, buffer
, 401,
1252 assert(rc
== MEMCACHED_SUCCESS
);
1253 assert(x
== memcached_server_list_count(servers
));
1256 rc
= memcached_server_push(memc
, servers
);
1257 assert(rc
== MEMCACHED_SUCCESS
);
1258 rc
= memcached_server_push(memc
, servers
);
1259 assert(rc
== MEMCACHED_SUCCESS
);
1261 memcached_server_list_free(servers
);
1266 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*clone
__attribute__((unused
)))
1268 return MEMCACHED_SUCCESS
;
1271 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1273 return MEMCACHED_SUCCESS
;
1276 static test_return
callback_test(memcached_st
*memc
)
1278 /* Test User Data */
1282 memcached_return rc
;
1284 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1285 assert(rc
== MEMCACHED_SUCCESS
);
1286 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1287 assert(*test_ptr
== x
);
1290 /* Test Clone Callback */
1292 memcached_clone_func temp_function
;
1293 memcached_return rc
;
1295 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
1296 assert(rc
== MEMCACHED_SUCCESS
);
1297 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1298 assert(temp_function
== clone_test_callback
);
1301 /* Test Cleanup Callback */
1303 memcached_cleanup_func temp_function
;
1304 memcached_return rc
;
1306 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1307 assert(rc
== MEMCACHED_SUCCESS
);
1308 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1309 assert(temp_function
== cleanup_test_callback
);
1315 /* We don't test the behavior itself, we test the switches */
1316 static test_return
behavior_test(memcached_st
*memc
)
1321 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1322 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1325 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1326 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1329 set
= MEMCACHED_HASH_MD5
;
1330 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1331 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1332 assert(value
== MEMCACHED_HASH_MD5
);
1336 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1337 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1340 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1341 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1344 set
= MEMCACHED_HASH_DEFAULT
;
1345 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1346 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1347 assert(value
== MEMCACHED_HASH_DEFAULT
);
1349 set
= MEMCACHED_HASH_CRC
;
1350 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1351 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1352 assert(value
== MEMCACHED_HASH_CRC
);
1354 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1357 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1363 /* Test case provided by Cal Haldenbrand */
1364 static test_return
user_supplied_bug1(memcached_st
*memc
)
1366 unsigned int setter
= 1;
1369 unsigned long long total
= 0;
1372 char randomstuff
[6 * 1024];
1373 memcached_return rc
;
1375 memset(randomstuff
, 0, 6 * 1024);
1377 /* We just keep looking at the same values over and over */
1380 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1381 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1385 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1389 size
= (rand() % ( 5 * 1024 ) ) + 400;
1390 memset(randomstuff
, 0, 6 * 1024);
1391 assert(size
< 6 * 1024); /* Being safe here */
1393 for (j
= 0 ; j
< size
;j
++)
1394 randomstuff
[j
] = (char) (rand() % 26) + 97;
1397 sprintf(key
, "%d", x
);
1398 rc
= memcached_set(memc
, key
, strlen(key
),
1399 randomstuff
, strlen(randomstuff
), 10, 0);
1400 /* If we fail, lets try again */
1401 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1402 rc
= memcached_set(memc
, key
, strlen(key
),
1403 randomstuff
, strlen(randomstuff
), 10, 0);
1404 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1410 /* Test case provided by Cal Haldenbrand */
1411 static test_return
user_supplied_bug2(memcached_st
*memc
)
1414 unsigned int setter
;
1416 unsigned long long total
;
1419 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1420 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1422 setter
= 20 * 1024576;
1423 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1424 setter
= 20 * 1024576;
1425 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1426 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1427 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1429 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1432 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1434 memcached_return rc
= MEMCACHED_SUCCESS
;
1435 char buffer
[SMALL_STRING_LEN
];
1440 memset(buffer
, 0, SMALL_STRING_LEN
);
1442 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1443 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1444 &val_len
, &flags
, &rc
);
1445 if (rc
!= MEMCACHED_SUCCESS
)
1447 if (rc
== MEMCACHED_NOTFOUND
)
1451 WATCHPOINT_ERROR(rc
);
1465 /* Do a large mget() over all the keys we think exist */
1466 #define KEY_COUNT 3000 // * 1024576
1467 static test_return
user_supplied_bug3(memcached_st
*memc
)
1469 memcached_return rc
;
1470 unsigned int setter
;
1473 size_t key_lengths
[KEY_COUNT
];
1476 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1477 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1479 setter
= 20 * 1024576;
1480 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1481 setter
= 20 * 1024576;
1482 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1483 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1484 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1487 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1489 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1490 for (x
= 0; x
< KEY_COUNT
; x
++)
1494 snprintf(buffer
, 30, "%u", x
);
1495 keys
[x
]= strdup(buffer
);
1496 key_lengths
[x
]= strlen(keys
[x
]);
1499 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1500 assert(rc
== MEMCACHED_SUCCESS
);
1502 /* Turn this into a help function */
1504 char return_key
[MEMCACHED_MAX_KEY
];
1505 size_t return_key_length
;
1507 size_t return_value_length
;
1510 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1511 &return_value_length
, &flags
, &rc
)))
1513 assert(return_value
);
1514 assert(rc
== MEMCACHED_SUCCESS
);
1519 for (x
= 0; x
< KEY_COUNT
; x
++)
1526 /* Make sure we behave properly if server list has no values */
1527 static test_return
user_supplied_bug4(memcached_st
*memc
)
1529 memcached_return rc
;
1530 char *keys
[]= {"fudge", "son", "food"};
1531 size_t key_length
[]= {5, 3, 4};
1534 char return_key
[MEMCACHED_MAX_KEY
];
1535 size_t return_key_length
;
1537 size_t return_value_length
;
1539 /* Here we free everything before running a bunch of mget tests */
1541 memcached_server_list_free(memc
->hosts
);
1543 memc
->number_of_hosts
= 0;
1547 /* We need to empty the server before continueing test */
1548 rc
= memcached_flush(memc
, 0);
1549 assert(rc
== MEMCACHED_NO_SERVERS
);
1551 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1552 assert(rc
== MEMCACHED_NO_SERVERS
);
1554 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1555 &return_value_length
, &flags
, &rc
)) != NULL
)
1557 assert(return_value
);
1559 assert(!return_value
);
1560 assert(return_value_length
== 0);
1561 assert(rc
== MEMCACHED_NO_SERVERS
);
1563 for (x
= 0; x
< 3; x
++)
1565 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1566 keys
[x
], key_length
[x
],
1567 (time_t)50, (uint32_t)9);
1568 assert(rc
== MEMCACHED_NO_SERVERS
);
1571 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1572 assert(rc
== MEMCACHED_NO_SERVERS
);
1575 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1576 &return_value_length
, &flags
, &rc
)))
1578 assert(return_value
);
1579 assert(rc
== MEMCACHED_SUCCESS
);
1580 assert(return_key_length
== return_value_length
);
1581 assert(!memcmp(return_value
, return_key
, return_value_length
));
1589 #define VALUE_SIZE_BUG5 1048064
1590 static test_return
user_supplied_bug5(memcached_st
*memc
)
1592 memcached_return rc
;
1593 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1594 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1595 char return_key
[MEMCACHED_MAX_KEY
];
1596 size_t return_key_length
;
1598 size_t value_length
;
1602 char insert_data
[VALUE_SIZE_BUG5
];
1604 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1605 insert_data
[x
]= rand();
1607 memcached_flush(memc
, 0);
1608 value
= memcached_get(memc
, keys
[0], key_length
[0],
1609 &value_length
, &flags
, &rc
);
1610 assert(value
== NULL
);
1611 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1614 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1615 &value_length
, &flags
, &rc
)))
1619 for (x
= 0; x
< 4; x
++)
1621 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1622 insert_data
, VALUE_SIZE_BUG5
,
1623 (time_t)0, (uint32_t)0);
1624 assert(rc
== MEMCACHED_SUCCESS
);
1627 for (x
= 0; x
< 10; x
++)
1629 value
= memcached_get(memc
, keys
[0], key_length
[0],
1630 &value_length
, &flags
, &rc
);
1634 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1636 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1637 &value_length
, &flags
, &rc
)))
1648 static test_return
user_supplied_bug6(memcached_st
*memc
)
1650 memcached_return rc
;
1651 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1652 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1653 char return_key
[MEMCACHED_MAX_KEY
];
1654 size_t return_key_length
;
1656 size_t value_length
;
1660 char insert_data
[VALUE_SIZE_BUG5
];
1662 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1663 insert_data
[x
]= rand();
1665 memcached_flush(memc
, 0);
1666 value
= memcached_get(memc
, keys
[0], key_length
[0],
1667 &value_length
, &flags
, &rc
);
1668 assert(value
== NULL
);
1669 assert(rc
== MEMCACHED_NOTFOUND
);
1670 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1671 assert(rc
== MEMCACHED_SUCCESS
);
1674 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1675 &value_length
, &flags
, &rc
)))
1678 assert(rc
== MEMCACHED_END
);
1680 for (x
= 0; x
< 4; x
++)
1682 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1683 insert_data
, VALUE_SIZE_BUG5
,
1684 (time_t)0, (uint32_t)0);
1685 assert(rc
== MEMCACHED_SUCCESS
);
1688 for (x
= 0; x
< 2; x
++)
1690 value
= memcached_get(memc
, keys
[0], key_length
[0],
1691 &value_length
, &flags
, &rc
);
1695 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1696 assert(rc
== MEMCACHED_SUCCESS
);
1698 /* We test for purge of partial complete fetches */
1699 for (count
= 3; count
; count
--)
1701 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1702 &value_length
, &flags
, &rc
);
1703 assert(rc
== MEMCACHED_SUCCESS
);
1704 assert(!(memcmp(value
, insert_data
, value_length
)));
1705 assert(value_length
);
1713 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1715 memcached_return rc
;
1717 memcached_st
*clone
;
1719 memcached_server_st
*servers
;
1720 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";
1722 servers
= memcached_servers_parse(server_list
);
1725 mine
= memcached_create(NULL
);
1726 rc
= memcached_server_push(mine
, servers
);
1727 assert(rc
== MEMCACHED_SUCCESS
);
1728 memcached_server_list_free(servers
);
1731 clone
= memcached_clone(NULL
, mine
);
1733 memcached_quit(mine
);
1734 memcached_quit(clone
);
1737 memcached_free(mine
);
1738 memcached_free(clone
);
1743 /* Test flag store/retrieve */
1744 static test_return
user_supplied_bug7(memcached_st
*memc
)
1746 memcached_return rc
;
1747 char *keys
= "036790384900";
1748 size_t key_length
= strlen("036790384900");
1749 char return_key
[MEMCACHED_MAX_KEY
];
1750 size_t return_key_length
;
1752 size_t value_length
;
1755 char insert_data
[VALUE_SIZE_BUG5
];
1757 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1758 insert_data
[x
]= rand();
1760 memcached_flush(memc
, 0);
1763 rc
= memcached_set(memc
, keys
, key_length
,
1764 insert_data
, VALUE_SIZE_BUG5
,
1766 assert(rc
== MEMCACHED_SUCCESS
);
1769 value
= memcached_get(memc
, keys
, key_length
,
1770 &value_length
, &flags
, &rc
);
1771 assert(flags
== 245);
1775 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1778 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1779 &value_length
, &flags
, &rc
);
1780 assert(flags
== 245);
1788 static test_return
user_supplied_bug9(memcached_st
*memc
)
1790 memcached_return rc
;
1791 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1792 size_t key_length
[3];
1797 char return_key
[MEMCACHED_MAX_KEY
];
1798 size_t return_key_length
;
1800 size_t return_value_length
;
1803 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1804 key_length
[1]= strlen("fudge&*@#");
1805 key_length
[2]= strlen("for^#@&$not");
1808 for (x
= 0; x
< 3; x
++)
1810 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1811 keys
[x
], key_length
[x
],
1812 (time_t)50, (uint32_t)9);
1813 assert(rc
== MEMCACHED_SUCCESS
);
1816 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1817 assert(rc
== MEMCACHED_SUCCESS
);
1819 /* We need to empty the server before continueing test */
1820 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1821 &return_value_length
, &flags
, &rc
)) != NULL
)
1823 assert(return_value
);
1832 /* We are testing with aggressive timeout to get failures */
1833 static test_return
user_supplied_bug10(memcached_st
*memc
)
1837 size_t value_length
= 512;
1840 memcached_return rc
;
1841 unsigned int set
= 1;
1842 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1845 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1846 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1848 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1850 value
= (char*)malloc(value_length
* sizeof(char));
1852 for (x
= 0; x
< value_length
; x
++)
1853 value
[x
]= (char) (x
% 127);
1855 for (x
= 1; x
<= 100000; ++x
)
1857 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1859 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1861 if (rc
== MEMCACHED_WRITE_FAILURE
)
1866 memcached_free(mclone
);
1872 We are looking failures in the async protocol
1874 static test_return
user_supplied_bug11(memcached_st
*memc
)
1878 size_t value_length
= 512;
1881 memcached_return rc
;
1882 unsigned int set
= 1;
1884 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1886 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1887 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1889 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1891 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1893 assert(timeout
== -1);
1895 value
= (char*)malloc(value_length
* sizeof(char));
1897 for (x
= 0; x
< value_length
; x
++)
1898 value
[x
]= (char) (x
% 127);
1900 for (x
= 1; x
<= 100000; ++x
)
1902 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1906 memcached_free(mclone
);
1912 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1914 static test_return
user_supplied_bug12(memcached_st
*memc
)
1916 memcached_return rc
;
1918 size_t value_length
;
1920 uint64_t number_value
;
1922 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1923 &value_length
, &flags
, &rc
);
1924 assert(value
== NULL
);
1925 assert(rc
== MEMCACHED_NOTFOUND
);
1927 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1930 assert(value
== NULL
);
1931 assert(rc
== MEMCACHED_NOTFOUND
);
1933 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1935 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1936 &value_length
, &flags
, &rc
);
1938 assert(rc
== MEMCACHED_SUCCESS
);
1941 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1943 assert(number_value
== 2);
1944 assert(rc
== MEMCACHED_SUCCESS
);
1950 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1951 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1953 static test_return
user_supplied_bug13(memcached_st
*memc
)
1955 char key
[] = "key34567890";
1957 memcached_return rc
;
1958 size_t overflowSize
;
1960 char commandFirst
[]= "set key34567890 0 0 ";
1961 char commandLast
[] = " \r\n"; /* first line of command sent to server */
1962 size_t commandLength
;
1965 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
1967 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
1969 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
1971 overflow
= malloc(testSize
);
1972 assert(overflow
!= NULL
);
1974 memset(overflow
, 'x', testSize
);
1975 rc
= memcached_set(memc
, key
, strlen(key
),
1976 overflow
, testSize
, 0, 0);
1977 assert(rc
== MEMCACHED_SUCCESS
);
1986 Test values of many different sizes
1987 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1988 set key34567890 0 0 8169 \r\n
1989 is sent followed by buffer of size 8169, followed by 8169
1991 static test_return
user_supplied_bug14(memcached_st
*memc
)
1994 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1995 memcached_return rc
;
1998 size_t value_length
= 18000;
2000 size_t string_length
;
2003 size_t current_length
;
2005 value
= (char*)malloc(value_length
);
2008 for (x
= 0; x
< value_length
; x
++)
2009 value
[x
] = (char) (x
% 127);
2011 for (current_length
= 0; current_length
< value_length
; current_length
++)
2013 rc
= memcached_set(memc
, key
, strlen(key
),
2014 value
, current_length
,
2015 (time_t)0, (uint32_t)0);
2016 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2018 string
= memcached_get(memc
, key
, strlen(key
),
2019 &string_length
, &flags
, &rc
);
2021 assert(rc
== MEMCACHED_SUCCESS
);
2022 assert(string_length
== current_length
);
2023 assert(!memcmp(string
, value
, string_length
));
2034 Look for zero length value problems
2036 static test_return
user_supplied_bug15(memcached_st
*memc
)
2039 memcached_return rc
;
2045 for (x
= 0; x
< 2; x
++)
2047 rc
= memcached_set(memc
, key
, strlen(key
),
2049 (time_t)0, (uint32_t)0);
2051 assert(rc
== MEMCACHED_SUCCESS
);
2053 value
= memcached_get(memc
, key
, strlen(key
),
2054 &length
, &flags
, &rc
);
2056 assert(rc
== MEMCACHED_SUCCESS
);
2057 assert(value
== NULL
);
2058 assert(length
== 0);
2061 value
= memcached_get(memc
, key
, strlen(key
),
2062 &length
, &flags
, &rc
);
2064 assert(rc
== MEMCACHED_SUCCESS
);
2065 assert(value
== NULL
);
2066 assert(length
== 0);
2073 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2074 static test_return
user_supplied_bug16(memcached_st
*memc
)
2076 memcached_return rc
;
2082 rc
= memcached_set(memc
, key
, strlen(key
),
2084 (time_t)0, UINT32_MAX
);
2086 assert(rc
== MEMCACHED_SUCCESS
);
2088 value
= memcached_get(memc
, key
, strlen(key
),
2089 &length
, &flags
, &rc
);
2091 assert(rc
== MEMCACHED_SUCCESS
);
2092 assert(value
== NULL
);
2093 assert(length
== 0);
2094 assert(flags
== UINT32_MAX
);
2099 /* Check the validity of chinese key*/
2100 static test_return
user_supplied_bug17(memcached_st
*memc
)
2102 memcached_return rc
;
2104 char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2109 rc
= memcached_set(memc
, key
, strlen(key
),
2110 value
, strlen(value
),
2113 assert(rc
== MEMCACHED_SUCCESS
);
2115 value2
= memcached_get(memc
, key
, strlen(key
),
2116 &length
, &flags
, &rc
);
2118 assert(length
==strlen(value
));
2119 assert(rc
== MEMCACHED_SUCCESS
);
2120 assert(memcmp(value
, value2
, length
)==0);
2127 static test_return
result_static(memcached_st
*memc
)
2129 memcached_result_st result
;
2130 memcached_result_st
*result_ptr
;
2132 result_ptr
= memcached_result_create(memc
, &result
);
2133 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2135 memcached_result_free(&result
);
2140 static test_return
result_alloc(memcached_st
*memc
)
2142 memcached_result_st
*result
;
2144 result
= memcached_result_create(memc
, NULL
);
2146 memcached_result_free(result
);
2151 static test_return
string_static_null(memcached_st
*memc
)
2153 memcached_string_st string
;
2154 memcached_string_st
*string_ptr
;
2156 string_ptr
= memcached_string_create(memc
, &string
, 0);
2157 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2159 memcached_string_free(&string
);
2164 static test_return
string_alloc_null(memcached_st
*memc
)
2166 memcached_string_st
*string
;
2168 string
= memcached_string_create(memc
, NULL
, 0);
2170 memcached_string_free(string
);
2175 static test_return
string_alloc_with_size(memcached_st
*memc
)
2177 memcached_string_st
*string
;
2179 string
= memcached_string_create(memc
, NULL
, 1024);
2181 memcached_string_free(string
);
2186 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2188 memcached_string_st
*string
;
2190 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
2191 assert(string
== NULL
);
2196 static test_return
string_alloc_append(memcached_st
*memc
)
2199 char buffer
[SMALL_STRING_LEN
];
2200 memcached_string_st
*string
;
2202 /* Ring the bell! */
2203 memset(buffer
, 6, SMALL_STRING_LEN
);
2205 string
= memcached_string_create(memc
, NULL
, 100);
2208 for (x
= 0; x
< 1024; x
++)
2210 memcached_return rc
;
2211 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2212 assert(rc
== MEMCACHED_SUCCESS
);
2214 memcached_string_free(string
);
2219 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2221 memcached_return rc
;
2223 char buffer
[SMALL_STRING_LEN
];
2224 memcached_string_st
*string
;
2226 /* Ring the bell! */
2227 memset(buffer
, 6, SMALL_STRING_LEN
);
2229 string
= memcached_string_create(memc
, NULL
, 100);
2232 for (x
= 0; x
< 1024; x
++)
2234 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2235 assert(rc
== MEMCACHED_SUCCESS
);
2237 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
2238 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2239 memcached_string_free(string
);
2244 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2246 pairs_free(global_pairs
);
2251 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2253 unsigned long long x
;
2254 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2255 global_count
= GLOBAL_COUNT
;
2257 for (x
= 0; x
< global_count
; x
++)
2259 global_keys
[x
]= global_pairs
[x
].key
;
2260 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2266 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2268 unsigned long long x
;
2269 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2270 global_count
= GLOBAL2_COUNT
;
2272 for (x
= 0; x
< global_count
; x
++)
2274 global_keys
[x
]= global_pairs
[x
].key
;
2275 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2281 static test_return
generate_data(memcached_st
*memc
)
2283 execute_set(memc
, global_pairs
, global_count
);
2288 static test_return
generate_data_with_stats(memcached_st
*memc
)
2290 memcached_stat_st
*stat_p
;
2291 memcached_return rc
;
2292 uint32_t host_index
= 0;
2293 execute_set(memc
, global_pairs
, global_count
);
2295 //TODO: hosts used size stats
2296 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2299 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2301 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
);
2304 memcached_stat_free(NULL
, stat_p
);
2308 static test_return
generate_buffer_data(memcached_st
*memc
)
2313 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2314 generate_data(memc
);
2319 static test_return
get_read_count(memcached_st
*memc
)
2322 memcached_return rc
;
2323 memcached_st
*clone
;
2325 clone
= memcached_clone(NULL
, memc
);
2328 memcached_server_add(clone
, "localhost", 6666);
2332 size_t return_value_length
;
2336 for (x
= count
= 0; x
< global_count
; x
++)
2338 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2339 &return_value_length
, &flags
, &rc
);
2340 if (rc
== MEMCACHED_SUCCESS
)
2347 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2350 memcached_free(clone
);
2355 static test_return
get_read(memcached_st
*memc
)
2358 memcached_return rc
;
2362 size_t return_value_length
;
2365 for (x
= 0; x
< global_count
; x
++)
2367 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2368 &return_value_length
, &flags
, &rc
);
2370 assert(return_value);
2371 assert(rc == MEMCACHED_SUCCESS);
2373 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2381 static test_return
mget_read(memcached_st
*memc
)
2383 memcached_return rc
;
2385 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2386 assert(rc
== MEMCACHED_SUCCESS
);
2387 /* Turn this into a help function */
2389 char return_key
[MEMCACHED_MAX_KEY
];
2390 size_t return_key_length
;
2392 size_t return_value_length
;
2395 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2396 &return_value_length
, &flags
, &rc
)))
2398 assert(return_value
);
2399 assert(rc
== MEMCACHED_SUCCESS
);
2407 static test_return
mget_read_result(memcached_st
*memc
)
2409 memcached_return rc
;
2411 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2412 assert(rc
== MEMCACHED_SUCCESS
);
2413 /* Turn this into a help function */
2415 memcached_result_st results_obj
;
2416 memcached_result_st
*results
;
2418 results
= memcached_result_create(memc
, &results_obj
);
2420 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2423 assert(rc
== MEMCACHED_SUCCESS
);
2426 memcached_result_free(&results_obj
);
2432 static test_return
mget_read_function(memcached_st
*memc
)
2434 memcached_return rc
;
2435 unsigned int counter
;
2436 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
2438 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2439 assert(rc
== MEMCACHED_SUCCESS
);
2441 callbacks
[0]= &callback_counter
;
2443 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2448 static test_return
delete_generate(memcached_st
*memc
)
2452 for (x
= 0; x
< global_count
; x
++)
2454 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2460 static test_return
delete_buffer_generate(memcached_st
*memc
)
2466 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2468 for (x
= 0; x
< global_count
; x
++)
2470 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2476 static test_return
free_data(memcached_st
*memc
__attribute__((unused
)))
2478 pairs_free(global_pairs
);
2483 static test_return
add_host_test1(memcached_st
*memc
)
2486 memcached_return rc
;
2487 char servername
[]= "0.example.com";
2488 memcached_server_st
*servers
;
2490 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
2492 assert(1 == memcached_server_list_count(servers
));
2494 for (x
= 2; x
< 20; x
++)
2496 char buffer
[SMALL_STRING_LEN
];
2498 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2499 servers
= memcached_server_list_append(servers
, buffer
, 401,
2501 assert(rc
== MEMCACHED_SUCCESS
);
2502 assert(x
== memcached_server_list_count(servers
));
2505 rc
= memcached_server_push(memc
, servers
);
2506 assert(rc
== MEMCACHED_SUCCESS
);
2507 rc
= memcached_server_push(memc
, servers
);
2508 assert(rc
== MEMCACHED_SUCCESS
);
2510 memcached_server_list_free(servers
);
2515 static memcached_return
pre_nonblock(memcached_st
*memc
)
2517 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2519 return MEMCACHED_SUCCESS
;
2522 static memcached_return
pre_murmur(memcached_st
*memc
)
2524 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2526 return MEMCACHED_SUCCESS
;
2529 static memcached_return
pre_jenkins(memcached_st
*memc
)
2531 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
2533 return MEMCACHED_SUCCESS
;
2537 static memcached_return
pre_md5(memcached_st
*memc
)
2539 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2541 return MEMCACHED_SUCCESS
;
2544 static memcached_return
pre_crc(memcached_st
*memc
)
2546 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2548 return MEMCACHED_SUCCESS
;
2551 static memcached_return
pre_hsieh(memcached_st
*memc
)
2553 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2555 return MEMCACHED_SUCCESS
;
2558 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2560 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2562 return MEMCACHED_SUCCESS
;
2565 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2567 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2569 return MEMCACHED_SUCCESS
;
2572 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2574 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2576 return MEMCACHED_SUCCESS
;
2579 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2581 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2583 return MEMCACHED_SUCCESS
;
2586 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
2588 memcached_return rc
;
2591 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
2592 assert(rc
== MEMCACHED_SUCCESS
);
2594 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
2597 return MEMCACHED_SUCCESS
;
2600 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
2602 memcached_return rc
;
2605 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2606 assert(rc
== MEMCACHED_SUCCESS
);
2608 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2611 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2612 assert(rc
== MEMCACHED_SUCCESS
);
2614 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2615 assert(value
== MEMCACHED_HASH_MD5
);
2616 return MEMCACHED_SUCCESS
;
2619 static memcached_return
pre_binary(memcached_st
*memc
)
2621 memcached_return rc
= MEMCACHED_FAILURE
;
2622 memcached_st
*clone
;
2624 clone
= memcached_clone(NULL
, memc
);
2626 // The memcached_version needs to be done on a clone, because the server
2627 // will not toggle protocol on an connection.
2628 memcached_version(clone
);
2630 if (clone
->hosts
[0].major_version
>= 1 && clone
->hosts
[0].minor_version
> 2)
2632 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2633 assert(rc
== MEMCACHED_SUCCESS
);
2634 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2637 memcached_free(clone
);
2641 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
2646 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
2648 return malloc(size
);
2651 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
2653 return realloc(mem
, size
);
2656 static memcached_return
set_prefix(memcached_st
*memc
)
2658 memcached_return rc
;
2659 const char *key
= "mine";
2662 /* Make sure be default none exists */
2663 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2664 assert(rc
== MEMCACHED_FAILURE
);
2666 /* Test a clean set */
2667 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2668 assert(rc
== MEMCACHED_SUCCESS
);
2670 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2671 assert(memcmp(value
, key
, 4) == 0);
2672 assert(rc
== MEMCACHED_SUCCESS
);
2674 /* Test that we can turn it off */
2675 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2676 assert(rc
== MEMCACHED_SUCCESS
);
2678 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2679 assert(rc
== MEMCACHED_FAILURE
);
2681 /* Now setup for main test */
2682 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2683 assert(rc
== MEMCACHED_SUCCESS
);
2685 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2686 assert(rc
== MEMCACHED_SUCCESS
);
2687 assert(memcmp(value
, key
, 4) == 0);
2689 /* Set to Zero, and then Set to something too large */
2692 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2693 assert(rc
== MEMCACHED_SUCCESS
);
2695 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2696 assert(rc
== MEMCACHED_FAILURE
);
2697 assert(value
== NULL
);
2699 /* Test a long key for failure */
2700 long_key
= "Thisismorethentheallottednumberofcharacters";
2701 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2702 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2704 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2705 long_key
= "This is more then the allotted number of characters";
2706 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2707 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2709 /* Test for a bad prefix, but with a short key */
2710 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
2711 assert(rc
== MEMCACHED_SUCCESS
);
2713 long_key
= "dog cat";
2714 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2715 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2718 return MEMCACHED_SUCCESS
;
2721 static memcached_return
set_memory_alloc(memcached_st
*memc
)
2724 memcached_malloc_function test_ptr
;
2725 memcached_return rc
;
2727 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2728 assert(rc
== MEMCACHED_SUCCESS
);
2729 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2730 assert(rc
== MEMCACHED_SUCCESS
);
2731 assert(test_ptr
== my_malloc
);
2735 memcached_realloc_function test_ptr
;
2736 memcached_return rc
;
2738 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2739 assert(rc
== MEMCACHED_SUCCESS
);
2740 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2741 assert(rc
== MEMCACHED_SUCCESS
);
2742 assert(test_ptr
== my_realloc
);
2746 memcached_free_function test_ptr
;
2747 memcached_return rc
;
2749 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2750 assert(rc
== MEMCACHED_SUCCESS
);
2751 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2752 assert(rc
== MEMCACHED_SUCCESS
);
2753 assert(test_ptr
== my_free
);
2756 return MEMCACHED_SUCCESS
;
2759 static memcached_return
enable_consistent(memcached_st
*memc
)
2761 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2762 memcached_hash hash
;
2763 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2766 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2767 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2769 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2770 assert(hash
== MEMCACHED_HASH_HSIEH
);
2773 return MEMCACHED_SUCCESS
;
2776 static memcached_return
enable_cas(memcached_st
*memc
)
2778 unsigned int set
= 1;
2780 memcached_version(memc
);
2782 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
2783 || memc
->hosts
[0].minor_version
> 2)
2785 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
2787 return MEMCACHED_SUCCESS
;
2790 return MEMCACHED_FAILURE
;
2793 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
2795 memcached_version(memc
);
2797 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
2798 || memc
->hosts
[0].minor_version
> 2)
2799 return MEMCACHED_SUCCESS
;
2801 return MEMCACHED_FAILURE
;
2804 static memcached_return
pre_unix_socket(memcached_st
*memc
)
2806 memcached_return rc
;
2809 memcached_server_list_free(memc
->hosts
);
2811 memc
->number_of_hosts
= 0;
2813 if (stat("/tmp/memcached.socket", &buf
))
2814 return MEMCACHED_FAILURE
;
2816 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2821 static memcached_return
pre_nodelay(memcached_st
*memc
)
2823 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2824 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2826 return MEMCACHED_SUCCESS
;
2829 static memcached_return
pre_settimer(memcached_st
*memc
)
2831 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
2832 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
2834 return MEMCACHED_SUCCESS
;
2837 static memcached_return
poll_timeout(memcached_st
*memc
)
2843 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
2845 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2847 assert(timeout
== 100);
2849 return MEMCACHED_SUCCESS
;
2853 /* Clean the server before beginning testing */
2855 {"flush", 0, flush_test
},
2856 {"init", 0, init_test
},
2857 {"allocation", 0, allocation_test
},
2858 {"server_list_null_test", 0, server_list_null_test
},
2859 {"server_unsort", 0, server_unsort_test
},
2860 {"server_sort", 0, server_sort_test
},
2861 {"server_sort2", 0, server_sort2_test
},
2862 {"clone_test", 0, clone_test
},
2863 {"error", 0, error_test
},
2864 {"set", 0, set_test
},
2865 {"set2", 0, set_test2
},
2866 {"set3", 0, set_test3
},
2867 {"add", 1, add_test
},
2868 {"replace", 1, replace_test
},
2869 {"delete", 1, delete_test
},
2870 {"get", 1, get_test
},
2871 {"get2", 0, get_test2
},
2872 {"get3", 0, get_test3
},
2873 {"get4", 0, get_test4
},
2874 {"stats_servername", 0, stats_servername_test
},
2875 {"increment", 0, increment_test
},
2876 {"decrement", 0, decrement_test
},
2877 {"quit", 0, quit_test
},
2878 {"mget", 1, mget_test
},
2879 {"mget_result", 1, mget_result_test
},
2880 {"mget_result_alloc", 1, mget_result_alloc_test
},
2881 {"mget_result_function", 1, mget_result_function
},
2882 {"get_stats", 0, get_stats
},
2883 {"add_host_test", 0, add_host_test
},
2884 {"add_host_test_1", 0, add_host_test1
},
2885 {"get_stats_keys", 0, get_stats_keys
},
2886 {"behavior_test", 0, get_stats_keys
},
2887 {"callback_test", 0, get_stats_keys
},
2888 {"version_string_test", 0, version_string_test
},
2889 {"bad_key", 1, bad_key_test
},
2890 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
2891 {"read_through", 1, read_through
},
2892 {"delete_through", 1, delete_through
},
2896 test_st async_tests
[] ={
2897 {"add", 1, add_wrapper
},
2901 test_st string_tests
[] ={
2902 {"string static with null", 0, string_static_null
},
2903 {"string alloc with null", 0, string_alloc_null
},
2904 {"string alloc with 1K", 0, string_alloc_with_size
},
2905 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2906 {"string append", 0, string_alloc_append
},
2907 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2911 test_st result_tests
[] ={
2912 {"result static", 0, result_static
},
2913 {"result alloc", 0, result_alloc
},
2917 test_st version_1_2_3
[] ={
2918 {"append", 0, append_test
},
2919 {"prepend", 0, prepend_test
},
2920 {"cas", 0, cas_test
},
2921 {"cas2", 0, cas2_test
},
2922 {"append_binary", 0, append_binary_test
},
2926 test_st user_tests
[] ={
2927 {"user_supplied_bug1", 0, user_supplied_bug1
},
2928 {"user_supplied_bug2", 0, user_supplied_bug2
},
2929 {"user_supplied_bug3", 0, user_supplied_bug3
},
2930 {"user_supplied_bug4", 0, user_supplied_bug4
},
2931 {"user_supplied_bug5", 1, user_supplied_bug5
},
2932 {"user_supplied_bug6", 1, user_supplied_bug6
},
2933 {"user_supplied_bug7", 1, user_supplied_bug7
},
2934 {"user_supplied_bug8", 1, user_supplied_bug8
},
2935 {"user_supplied_bug9", 1, user_supplied_bug9
},
2936 {"user_supplied_bug10", 1, user_supplied_bug10
},
2937 {"user_supplied_bug11", 1, user_supplied_bug11
},
2938 {"user_supplied_bug12", 1, user_supplied_bug12
},
2939 {"user_supplied_bug13", 1, user_supplied_bug13
},
2940 {"user_supplied_bug14", 1, user_supplied_bug14
},
2941 {"user_supplied_bug15", 1, user_supplied_bug15
},
2942 {"user_supplied_bug16", 1, user_supplied_bug16
},
2943 {"user_supplied_bug17", 1, user_supplied_bug17
},
2947 test_st generate_tests
[] ={
2948 {"generate_pairs", 1, generate_pairs
},
2949 {"generate_data", 1, generate_data
},
2950 {"get_read", 0, get_read
},
2951 {"delete_generate", 0, delete_generate
},
2952 {"generate_buffer_data", 1, generate_buffer_data
},
2953 {"delete_buffer", 0, delete_buffer_generate
},
2954 {"generate_data", 1, generate_data
},
2955 {"mget_read", 0, mget_read
},
2956 {"mget_read_result", 0, mget_read_result
},
2957 {"mget_read_function", 0, mget_read_function
},
2958 {"cleanup", 1, cleanup_pairs
},
2959 {"generate_large_pairs", 1, generate_large_pairs
},
2960 {"generate_data", 1, generate_data
},
2961 {"generate_buffer_data", 1, generate_buffer_data
},
2962 {"cleanup", 1, cleanup_pairs
},
2966 test_st consistent_tests
[] ={
2967 {"generate_pairs", 1, generate_pairs
},
2968 {"generate_data", 1, generate_data
},
2969 {"get_read", 0, get_read_count
},
2970 {"cleanup", 1, cleanup_pairs
},
2974 test_st consistent_weighted_tests
[] ={
2975 {"generate_pairs", 1, generate_pairs
},
2976 {"generate_data", 1, generate_data_with_stats
},
2977 {"get_read", 0, get_read_count
},
2978 {"cleanup", 1, cleanup_pairs
},
2982 collection_st collection
[] ={
2983 {"block", 0, 0, tests
},
2984 {"binary", pre_binary
, 0, tests
},
2985 {"nonblock", pre_nonblock
, 0, tests
},
2986 {"nodelay", pre_nodelay
, 0, tests
},
2987 {"settimer", pre_settimer
, 0, tests
},
2988 {"md5", pre_md5
, 0, tests
},
2989 {"crc", pre_crc
, 0, tests
},
2990 {"hsieh", pre_hsieh
, 0, tests
},
2991 {"jenkins", pre_jenkins
, 0, tests
},
2992 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2993 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2994 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2995 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2996 {"ketama", pre_behavior_ketama
, 0, tests
},
2997 {"unix_socket", pre_unix_socket
, 0, tests
},
2998 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2999 {"poll_timeout", poll_timeout
, 0, tests
},
3000 {"gets", enable_cas
, 0, tests
},
3001 {"consistent", enable_consistent
, 0, tests
},
3002 {"memory_allocators", set_memory_alloc
, 0, tests
},
3003 {"prefix", set_prefix
, 0, tests
},
3004 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
3005 {"string", 0, 0, string_tests
},
3006 {"result", 0, 0, result_tests
},
3007 {"async", pre_nonblock
, 0, async_tests
},
3008 {"user", 0, 0, user_tests
},
3009 {"generate", 0, 0, generate_tests
},
3010 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
3011 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
3012 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
3013 {"generate_md5", pre_md5
, 0, generate_tests
},
3014 {"generate_murmur", pre_murmur
, 0, generate_tests
},
3015 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
3016 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
3017 {"consistent_not", 0, 0, consistent_tests
},
3018 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
3019 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
3023 #define SERVERS_TO_CREATE 5
3025 /* Prototypes for functions we will pass to test framework */
3026 void *world_create(void);
3027 void world_destroy(void *p
);
3029 void *world_create(void)
3031 server_startup_st
*construct
;
3033 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
3034 memset(construct
, 0, sizeof(server_startup_st
));
3035 construct
->count
= SERVERS_TO_CREATE
;
3037 server_startup(construct
);
3043 void world_destroy(void *p
)
3045 server_startup_st
*construct
= (server_startup_st
*)p
;
3046 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
3047 memcached_server_list_free(servers
);
3049 server_shutdown(construct
);
3053 void get_world(world_st
*world
)
3055 world
->collections
= collection
;
3056 world
->create
= world_create
;
3057 world
->destroy
= world_destroy
;