2 Sample test application.
10 #include <sys/types.h>
14 #include "../lib/common.h"
15 #include "../src/generator.h"
16 #include "../src/execute.h"
19 #define INT64_MAX LONG_MAX
22 #define INT32_MAX INT_MAX
28 #define GLOBAL_COUNT 100000
29 #define GLOBAL2_COUNT 1000
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 uint8_t init_test(memcached_st
*not_used
)
40 (void)memcached_create(&memc
);
41 memcached_free(&memc
);
46 uint8_t server_list_null_test(memcached_st
*ptr
)
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 uint8_t allocation_test(memcached_st
*not_used
)
66 memc
= memcached_create(NULL
);
73 uint8_t clone_test(memcached_st
*memc
)
78 clone
= memcached_clone(NULL
, NULL
);
80 memcached_free(clone
);
83 /* Can we init from null? */
86 clone
= memcached_clone(NULL
, memc
);
88 memcached_free(clone
);
91 /* Can we init from struct? */
93 memcached_st declared_clone
;
95 clone
= memcached_clone(&declared_clone
, NULL
);
97 memcached_free(clone
);
100 /* Can we init from struct? */
102 memcached_st declared_clone
;
104 clone
= memcached_clone(&declared_clone
, memc
);
106 memcached_free(clone
);
112 uint8_t connection_test(memcached_st
*memc
)
116 rc
= memcached_server_add(memc
, "localhost", 0);
117 assert(rc
== MEMCACHED_SUCCESS
);
122 uint8_t error_test(memcached_st
*memc
)
126 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
128 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
134 uint8_t set_test(memcached_st
*memc
)
138 char *value
= "when we sanitize";
140 rc
= memcached_set(memc
, key
, strlen(key
),
141 value
, strlen(value
),
142 (time_t)0, (uint32_t)0);
143 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
148 uint8_t append_test(memcached_st
*memc
)
156 rc
= memcached_flush(memc
, 0);
157 assert(rc
== MEMCACHED_SUCCESS
);
159 rc
= memcached_set(memc
, key
, strlen(key
),
160 value
, strlen(value
),
161 (time_t)0, (uint32_t)0);
162 assert(rc
== MEMCACHED_SUCCESS
);
164 rc
= memcached_append(memc
, key
, strlen(key
),
165 " the", strlen(" the"),
166 (time_t)0, (uint32_t)0);
167 assert(rc
== MEMCACHED_SUCCESS
);
169 rc
= memcached_append(memc
, key
, strlen(key
),
170 " people", strlen(" people"),
171 (time_t)0, (uint32_t)0);
172 assert(rc
== MEMCACHED_SUCCESS
);
174 value
= memcached_get(memc
, key
, strlen(key
),
175 &value_length
, &flags
, &rc
);
176 assert(!memcmp(value
, "we the people", strlen("we the people")));
177 assert(strlen("we the people") == value_length
);
178 assert(rc
== MEMCACHED_SUCCESS
);
184 uint8_t append_binary_test(memcached_st
*memc
)
187 char *key
= "numbers";
188 unsigned int *store_ptr
;
189 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
195 rc
= memcached_flush(memc
, 0);
196 assert(rc
== MEMCACHED_SUCCESS
);
198 rc
= memcached_set(memc
,
201 (time_t)0, (uint32_t)0);
202 assert(rc
== MEMCACHED_SUCCESS
);
204 for (x
= 0; store_list
[x
] ; x
++)
206 rc
= memcached_append(memc
,
208 (char *)&store_list
[x
], sizeof(unsigned int),
209 (time_t)0, (uint32_t)0);
210 assert(rc
== MEMCACHED_SUCCESS
);
213 value
= memcached_get(memc
, key
, strlen(key
),
214 &value_length
, &flags
, &rc
);
215 assert((value_length
== (sizeof(unsigned int) * x
)));
216 assert(rc
== MEMCACHED_SUCCESS
);
218 store_ptr
= (unsigned int *)value
;
220 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
222 assert(*store_ptr
== store_list
[x
++]);
230 uint8_t cas2_test(memcached_st
*memc
)
233 char *keys
[]= {"fudge", "son", "food"};
234 size_t key_length
[]= {5, 3, 4};
235 char *value
= "we the people";
236 size_t value_length
= strlen("we the people");
238 memcached_result_st results_obj
;
239 memcached_result_st
*results
;
242 rc
= memcached_flush(memc
, 0);
243 assert(rc
== MEMCACHED_SUCCESS
);
245 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
247 for (x
= 0; x
< 3; x
++)
249 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
250 keys
[x
], key_length
[x
],
251 (time_t)50, (uint32_t)9);
252 assert(rc
== MEMCACHED_SUCCESS
);
255 rc
= memcached_mget(memc
, keys
, key_length
, 3);
257 results
= memcached_result_create(memc
, &results_obj
);
259 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
261 assert(results
->cas
);
262 assert(rc
== MEMCACHED_SUCCESS
);
263 WATCHPOINT_ASSERT(memcached_result_cas(results
));
265 assert(!memcmp(value
, "we the people", strlen("we the people")));
266 assert(strlen("we the people") == value_length
);
267 assert(rc
== MEMCACHED_SUCCESS
);
269 memcached_result_free(&results_obj
);
274 uint8_t cas_test(memcached_st
*memc
)
278 size_t key_length
= strlen("fun");
279 char *value
= "we the people";
280 size_t value_length
= strlen("we the people");
281 memcached_result_st results_obj
;
282 memcached_result_st
*results
;
285 rc
= memcached_flush(memc
, 0);
286 assert(rc
== MEMCACHED_SUCCESS
);
288 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
290 rc
= memcached_set(memc
, key
, strlen(key
),
291 value
, strlen(value
),
292 (time_t)0, (uint32_t)0);
293 assert(rc
== MEMCACHED_SUCCESS
);
295 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
297 results
= memcached_result_create(memc
, &results_obj
);
299 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
301 assert(rc
== MEMCACHED_SUCCESS
);
302 WATCHPOINT_ASSERT(memcached_result_cas(results
));
304 assert(!memcmp(value
, "we the people", strlen("we the people")));
305 assert(strlen("we the people") == value_length
);
306 assert(rc
== MEMCACHED_SUCCESS
);
308 memcached_result_free(&results_obj
);
313 uint8_t prepend_test(memcached_st
*memc
)
317 char *value
= "people";
321 rc
= memcached_flush(memc
, 0);
322 assert(rc
== MEMCACHED_SUCCESS
);
324 rc
= memcached_set(memc
, key
, strlen(key
),
325 value
, strlen(value
),
326 (time_t)0, (uint32_t)0);
327 assert(rc
== MEMCACHED_SUCCESS
);
329 rc
= memcached_prepend(memc
, key
, strlen(key
),
330 "the ", strlen("the "),
331 (time_t)0, (uint32_t)0);
332 assert(rc
== MEMCACHED_SUCCESS
);
334 rc
= memcached_prepend(memc
, key
, strlen(key
),
335 "we ", strlen("we "),
336 (time_t)0, (uint32_t)0);
337 assert(rc
== MEMCACHED_SUCCESS
);
339 value
= memcached_get(memc
, key
, strlen(key
),
340 &value_length
, &flags
, &rc
);
341 assert(!memcmp(value
, "we the people", strlen("we the people")));
342 assert(strlen("we the people") == value_length
);
343 assert(rc
== MEMCACHED_SUCCESS
);
350 Set the value, then quit to make sure it is flushed.
351 Come back in and test that add fails.
353 uint8_t add_test(memcached_st
*memc
)
357 char *value
= "when we sanitize";
359 rc
= memcached_set(memc
, key
, strlen(key
),
360 value
, strlen(value
),
361 (time_t)0, (uint32_t)0);
362 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
363 memcached_quit(memc
);
364 rc
= memcached_add(memc
, key
, strlen(key
),
365 value
, strlen(value
),
366 (time_t)0, (uint32_t)0);
367 assert(rc
== MEMCACHED_NOTSTORED
);
372 uint8_t add_wrapper(memcached_st
*memc
)
376 for (x
= 0; x
< 10000; x
++)
382 uint8_t replace_test(memcached_st
*memc
)
386 char *value
= "when we sanitize";
388 rc
= memcached_replace(memc
, key
, strlen(key
),
389 value
, strlen(value
),
390 (time_t)0, (uint32_t)0);
391 assert(rc
== MEMCACHED_SUCCESS
);
396 uint8_t delete_test(memcached_st
*memc
)
400 char *value
= "when we sanitize";
402 rc
= memcached_set(memc
, key
, strlen(key
),
403 value
, strlen(value
),
404 (time_t)0, (uint32_t)0);
405 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
407 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
408 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
413 uint8_t flush_test(memcached_st
*memc
)
417 rc
= memcached_flush(memc
, 0);
418 assert(rc
== MEMCACHED_SUCCESS
);
423 uint8_t get_test(memcached_st
*memc
)
428 size_t string_length
;
431 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
432 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
434 string
= memcached_get(memc
, key
, strlen(key
),
435 &string_length
, &flags
, &rc
);
437 assert(rc
== MEMCACHED_NOTFOUND
);
438 assert(string_length
== 0);
444 uint8_t get_test2(memcached_st
*memc
)
448 char *value
= "when we sanitize";
450 size_t string_length
;
453 rc
= memcached_set(memc
, key
, strlen(key
),
454 value
, strlen(value
),
455 (time_t)0, (uint32_t)0);
456 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
458 string
= memcached_get(memc
, key
, strlen(key
),
459 &string_length
, &flags
, &rc
);
462 assert(rc
== MEMCACHED_SUCCESS
);
463 assert(string_length
== strlen(value
));
464 assert(!memcmp(string
, value
, string_length
));
471 uint8_t set_test2(memcached_st
*memc
)
475 char *value
= "train in the brain";
476 size_t value_length
= strlen(value
);
479 for (x
= 0; x
< 10; x
++)
481 rc
= memcached_set(memc
, key
, strlen(key
),
483 (time_t)0, (uint32_t)0);
484 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
490 uint8_t set_test3(memcached_st
*memc
)
495 size_t value_length
= 8191;
498 value
= (char*)malloc(value_length
);
501 for (x
= 0; x
< value_length
; x
++)
502 value
[x
] = (char) (x
% 127);
504 for (x
= 0; x
< 1; x
++)
506 rc
= memcached_set(memc
, key
, strlen(key
),
508 (time_t)0, (uint32_t)0);
509 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
517 uint8_t get_test3(memcached_st
*memc
)
522 size_t value_length
= 8191;
524 size_t string_length
;
528 value
= (char*)malloc(value_length
);
531 for (x
= 0; x
< value_length
; x
++)
532 value
[x
] = (char) (x
% 127);
534 rc
= memcached_set(memc
, key
, strlen(key
),
536 (time_t)0, (uint32_t)0);
537 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
539 string
= memcached_get(memc
, key
, strlen(key
),
540 &string_length
, &flags
, &rc
);
542 assert(rc
== MEMCACHED_SUCCESS
);
544 assert(string_length
== value_length
);
545 assert(!memcmp(string
, value
, string_length
));
553 uint8_t get_test4(memcached_st
*memc
)
558 size_t value_length
= 8191;
560 size_t string_length
;
564 value
= (char*)malloc(value_length
);
567 for (x
= 0; x
< value_length
; x
++)
568 value
[x
] = (char) (x
% 127);
570 rc
= memcached_set(memc
, key
, strlen(key
),
572 (time_t)0, (uint32_t)0);
573 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
575 for (x
= 0; x
< 10; x
++)
577 string
= memcached_get(memc
, key
, strlen(key
),
578 &string_length
, &flags
, &rc
);
580 assert(rc
== MEMCACHED_SUCCESS
);
582 assert(string_length
== value_length
);
583 assert(!memcmp(string
, value
, string_length
));
592 /* Do not copy the style of this code, I just access hosts to testthis function */
593 uint8_t stats_servername_test(memcached_st
*memc
)
596 memcached_stat_st stat
;
597 rc
= memcached_stat_servername(&stat
, NULL
,
598 memc
->hosts
[0].hostname
,
599 memc
->hosts
[0].port
);
604 uint8_t increment_test(memcached_st
*memc
)
611 rc
= memcached_set(memc
, key
, strlen(key
),
612 value
, strlen(value
),
613 (time_t)0, (uint32_t)0);
614 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
616 rc
= memcached_increment(memc
, key
, strlen(key
),
618 assert(rc
== MEMCACHED_SUCCESS
);
619 assert(new_number
== 1);
621 rc
= memcached_increment(memc
, key
, strlen(key
),
623 assert(rc
== MEMCACHED_SUCCESS
);
624 assert(new_number
== 2);
629 uint8_t decrement_test(memcached_st
*memc
)
636 rc
= memcached_set(memc
, key
, strlen(key
),
637 value
, strlen(value
),
638 (time_t)0, (uint32_t)0);
639 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
641 rc
= memcached_decrement(memc
, key
, strlen(key
),
643 assert(rc
== MEMCACHED_SUCCESS
);
644 assert(new_number
== 2);
646 rc
= memcached_decrement(memc
, key
, strlen(key
),
648 assert(rc
== MEMCACHED_SUCCESS
);
649 assert(new_number
== 1);
654 uint8_t quit_test(memcached_st
*memc
)
658 char *value
= "sanford and sun";
660 rc
= memcached_set(memc
, key
, strlen(key
),
661 value
, strlen(value
),
662 (time_t)10, (uint32_t)3);
663 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
664 memcached_quit(memc
);
666 rc
= memcached_set(memc
, key
, strlen(key
),
667 value
, strlen(value
),
668 (time_t)50, (uint32_t)9);
669 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
674 uint8_t mget_result_test(memcached_st
*memc
)
677 char *keys
[]= {"fudge", "son", "food"};
678 size_t key_length
[]= {5, 3, 4};
681 memcached_result_st results_obj
;
682 memcached_result_st
*results
;
684 results
= memcached_result_create(memc
, &results_obj
);
686 assert(&results_obj
== results
);
688 /* We need to empty the server before continueing test */
689 rc
= memcached_flush(memc
, 0);
690 assert(rc
== MEMCACHED_SUCCESS
);
692 rc
= memcached_mget(memc
, keys
, key_length
, 3);
693 assert(rc
== MEMCACHED_SUCCESS
);
695 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
700 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
702 assert(rc
== MEMCACHED_END
);
704 for (x
= 0; x
< 3; x
++)
706 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
707 keys
[x
], key_length
[x
],
708 (time_t)50, (uint32_t)9);
709 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
712 rc
= memcached_mget(memc
, keys
, key_length
, 3);
713 assert(rc
== MEMCACHED_SUCCESS
);
715 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
718 assert(&results_obj
== results
);
719 assert(rc
== MEMCACHED_SUCCESS
);
720 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
721 assert(!memcmp(memcached_result_key_value(results
),
722 memcached_result_value(results
),
723 memcached_result_length(results
)));
726 memcached_result_free(&results_obj
);
731 uint8_t mget_result_alloc_test(memcached_st
*memc
)
734 char *keys
[]= {"fudge", "son", "food"};
735 size_t key_length
[]= {5, 3, 4};
738 memcached_result_st
*results
;
740 /* We need to empty the server before continueing test */
741 rc
= memcached_flush(memc
, 0);
742 assert(rc
== MEMCACHED_SUCCESS
);
744 rc
= memcached_mget(memc
, keys
, key_length
, 3);
745 assert(rc
== MEMCACHED_SUCCESS
);
747 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
752 assert(rc
== MEMCACHED_END
);
754 for (x
= 0; x
< 3; x
++)
756 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
757 keys
[x
], key_length
[x
],
758 (time_t)50, (uint32_t)9);
759 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
762 rc
= memcached_mget(memc
, keys
, key_length
, 3);
763 assert(rc
== MEMCACHED_SUCCESS
);
766 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
769 assert(rc
== MEMCACHED_SUCCESS
);
770 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
771 assert(!memcmp(memcached_result_key_value(results
),
772 memcached_result_value(results
),
773 memcached_result_length(results
)));
774 memcached_result_free(results
);
781 /* Count the results */
782 unsigned int callback_counter(memcached_st
*ptr
, memcached_result_st
*result
, void *context
)
784 unsigned int *counter
= (unsigned int *)context
;
786 *counter
= *counter
+ 1;
791 uint8_t mget_result_function(memcached_st
*memc
)
794 char *keys
[]= {"fudge", "son", "food"};
795 size_t key_length
[]= {5, 3, 4};
797 unsigned int counter
;
798 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
800 /* We need to empty the server before continueing test */
801 rc
= memcached_flush(memc
, 0);
802 for (x
= 0; x
< 3; x
++)
804 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
805 keys
[x
], key_length
[x
],
806 (time_t)50, (uint32_t)9);
807 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
810 rc
= memcached_mget(memc
, keys
, key_length
, 3);
811 assert(rc
== MEMCACHED_SUCCESS
);
813 callbacks
[0]= &callback_counter
;
815 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
817 assert(counter
== 3);
822 uint8_t mget_test(memcached_st
*memc
)
825 char *keys
[]= {"fudge", "son", "food"};
826 size_t key_length
[]= {5, 3, 4};
830 char return_key
[MEMCACHED_MAX_KEY
];
831 size_t return_key_length
;
833 size_t return_value_length
;
835 /* We need to empty the server before continueing test */
836 rc
= memcached_flush(memc
, 0);
837 assert(rc
== MEMCACHED_SUCCESS
);
839 rc
= memcached_mget(memc
, keys
, key_length
, 3);
840 assert(rc
== MEMCACHED_SUCCESS
);
842 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
843 &return_value_length
, &flags
, &rc
)) != NULL
)
845 assert(return_value
);
847 assert(!return_value
);
848 assert(return_value_length
== 0);
849 assert(rc
== MEMCACHED_END
);
851 for (x
= 0; x
< 3; x
++)
853 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
854 keys
[x
], key_length
[x
],
855 (time_t)50, (uint32_t)9);
856 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
859 rc
= memcached_mget(memc
, keys
, key_length
, 3);
860 assert(rc
== MEMCACHED_SUCCESS
);
863 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
864 &return_value_length
, &flags
, &rc
)))
866 assert(return_value
);
867 assert(rc
== MEMCACHED_SUCCESS
);
868 assert(return_key_length
== return_value_length
);
869 assert(!memcmp(return_value
, return_key
, return_value_length
));
877 uint8_t get_stats_keys(memcached_st
*memc
)
881 memcached_stat_st stat
;
884 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
885 assert(rc
== MEMCACHED_SUCCESS
);
886 for (ptr
= list
; *ptr
; ptr
++)
895 uint8_t version_string_test(memcached_st
*memc
)
897 const char *version_string
;
899 version_string
= memcached_lib_version();
901 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
906 uint8_t get_stats(memcached_st
*memc
)
912 memcached_stat_st
*stat
;
914 stat
= memcached_stat(memc
, NULL
, &rc
);
915 assert(rc
== MEMCACHED_SUCCESS
);
917 assert(rc
== MEMCACHED_SUCCESS
);
920 for (x
= 0; x
< memcached_server_count(memc
); x
++)
922 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
923 assert(rc
== MEMCACHED_SUCCESS
);
924 for (ptr
= list
; *ptr
; ptr
++);
929 memcached_stat_free(NULL
, stat
);
934 uint8_t add_host_test(memcached_st
*memc
)
937 memcached_server_st
*servers
;
939 char servername
[]= "0.example.com";
941 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
943 assert(1 == memcached_server_list_count(servers
));
945 for (x
= 2; x
< 20; x
++)
947 char buffer
[SMALL_STRING_LEN
];
949 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
950 servers
= memcached_server_list_append(servers
, buffer
, 401,
952 assert(rc
== MEMCACHED_SUCCESS
);
953 assert(x
== memcached_server_list_count(servers
));
956 rc
= memcached_server_push(memc
, servers
);
957 assert(rc
== MEMCACHED_SUCCESS
);
958 rc
= memcached_server_push(memc
, servers
);
959 assert(rc
== MEMCACHED_SUCCESS
);
961 memcached_server_list_free(servers
);
966 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
968 return MEMCACHED_SUCCESS
;
971 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
973 return MEMCACHED_SUCCESS
;
976 uint8_t callback_test(memcached_st
*memc
)
984 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
985 assert(rc
== MEMCACHED_SUCCESS
);
986 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
987 assert(*test_ptr
== x
);
990 /* Test Clone Callback */
992 memcached_clone_func temp_function
;
995 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
996 assert(rc
== MEMCACHED_SUCCESS
);
997 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
998 assert(temp_function
== clone_test_callback
);
1001 /* Test Cleanup Callback */
1003 memcached_cleanup_func temp_function
;
1004 memcached_return rc
;
1006 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1007 assert(rc
== MEMCACHED_SUCCESS
);
1008 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1009 assert(temp_function
== cleanup_test_callback
);
1015 /* We don't test the behavior itself, we test the switches */
1016 uint8_t behavior_test(memcached_st
*memc
)
1018 unsigned long long value
;
1019 unsigned int set
= 1;
1021 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1022 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1025 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1026 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1029 set
= MEMCACHED_HASH_MD5
;
1030 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1031 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1032 assert(value
== MEMCACHED_HASH_MD5
);
1036 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1037 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1040 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1041 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1044 set
= MEMCACHED_HASH_DEFAULT
;
1045 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1046 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1047 assert(value
== MEMCACHED_HASH_DEFAULT
);
1049 set
= MEMCACHED_HASH_CRC
;
1050 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1051 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1052 assert(value
== MEMCACHED_HASH_CRC
);
1054 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1057 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1063 /* Test case provided by Cal Haldenbrand */
1064 uint8_t user_supplied_bug1(memcached_st
*memc
)
1066 unsigned int setter
= 1;
1069 unsigned long long total
= 0;
1072 char randomstuff
[6 * 1024];
1073 memcached_return rc
;
1075 memset(randomstuff
, 0, 6 * 1024);
1077 /* We just keep looking at the same values over and over */
1080 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1081 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1085 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1089 size
= (rand() % ( 5 * 1024 ) ) + 400;
1090 memset(randomstuff
, 0, 6 * 1024);
1091 assert(size
< 6 * 1024); /* Being safe here */
1093 for (j
= 0 ; j
< size
;j
++)
1094 randomstuff
[j
] = (char) (rand() % 26) + 97;
1097 sprintf(key
, "%d", x
);
1098 rc
= memcached_set(memc
, key
, strlen(key
),
1099 randomstuff
, strlen(randomstuff
), 10, 0);
1100 /* If we fail, lets try again */
1101 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1102 rc
= memcached_set(memc
, key
, strlen(key
),
1103 randomstuff
, strlen(randomstuff
), 10, 0);
1104 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1110 /* Test case provided by Cal Haldenbrand */
1111 uint8_t user_supplied_bug2(memcached_st
*memc
)
1114 unsigned int setter
;
1116 unsigned long long total
;
1119 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1120 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1122 setter
= 20 * 1024576;
1123 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
1124 setter
= 20 * 1024576;
1125 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
1126 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1127 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1129 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1132 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1134 memcached_return rc
= MEMCACHED_SUCCESS
;
1135 char buffer
[SMALL_STRING_LEN
];
1140 memset(buffer
, 0, SMALL_STRING_LEN
);
1142 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1143 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1144 &val_len
, &flags
, &rc
);
1145 if (rc
!= MEMCACHED_SUCCESS
)
1147 if (rc
== MEMCACHED_NOTFOUND
)
1151 WATCHPOINT_ERROR(rc
);
1165 /* Do a large mget() over all the keys we think exist */
1166 #define KEY_COUNT 3000 // * 1024576
1167 uint8_t user_supplied_bug3(memcached_st
*memc
)
1169 memcached_return rc
;
1170 unsigned int setter
;
1173 size_t key_lengths
[KEY_COUNT
];
1176 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1177 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1179 setter
= 20 * 1024576;
1180 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
1181 setter
= 20 * 1024576;
1182 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
1183 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1184 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1187 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1189 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1190 for (x
= 0; x
< KEY_COUNT
; x
++)
1194 snprintf(buffer
, 30, "%u", x
);
1195 keys
[x
]= strdup(buffer
);
1196 key_lengths
[x
]= strlen(keys
[x
]);
1199 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1200 assert(rc
== MEMCACHED_SUCCESS
);
1202 /* Turn this into a help function */
1204 char return_key
[MEMCACHED_MAX_KEY
];
1205 size_t return_key_length
;
1207 size_t return_value_length
;
1210 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1211 &return_value_length
, &flags
, &rc
)))
1213 assert(return_value
);
1214 assert(rc
== MEMCACHED_SUCCESS
);
1219 for (x
= 0; x
< KEY_COUNT
; x
++)
1226 /* Make sure we behave properly if server list has no values */
1227 uint8_t user_supplied_bug4(memcached_st
*memc
)
1229 memcached_return rc
;
1230 char *keys
[]= {"fudge", "son", "food"};
1231 size_t key_length
[]= {5, 3, 4};
1234 char return_key
[MEMCACHED_MAX_KEY
];
1235 size_t return_key_length
;
1237 size_t return_value_length
;
1239 /* Here we free everything before running a bunch of mget tests */
1241 memcached_server_list_free(memc
->hosts
);
1243 memc
->number_of_hosts
= 0;
1247 /* We need to empty the server before continueing test */
1248 rc
= memcached_flush(memc
, 0);
1249 assert(rc
== MEMCACHED_NO_SERVERS
);
1251 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1252 assert(rc
== MEMCACHED_NO_SERVERS
);
1254 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1255 &return_value_length
, &flags
, &rc
)) != NULL
)
1257 assert(return_value
);
1259 assert(!return_value
);
1260 assert(return_value_length
== 0);
1261 assert(rc
== MEMCACHED_NO_SERVERS
);
1263 for (x
= 0; x
< 3; x
++)
1265 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1266 keys
[x
], key_length
[x
],
1267 (time_t)50, (uint32_t)9);
1268 assert(rc
== MEMCACHED_NO_SERVERS
);
1271 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1272 assert(rc
== MEMCACHED_NO_SERVERS
);
1275 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1276 &return_value_length
, &flags
, &rc
)))
1278 assert(return_value
);
1279 assert(rc
== MEMCACHED_SUCCESS
);
1280 assert(return_key_length
== return_value_length
);
1281 assert(!memcmp(return_value
, return_key
, return_value_length
));
1289 #define VALUE_SIZE_BUG5 1048064
1290 uint8_t user_supplied_bug5(memcached_st
*memc
)
1292 memcached_return rc
;
1293 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1294 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1295 char return_key
[MEMCACHED_MAX_KEY
];
1296 size_t return_key_length
;
1298 size_t value_length
;
1302 char insert_data
[VALUE_SIZE_BUG5
];
1304 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1305 insert_data
[x
]= rand();
1307 memcached_flush(memc
, 0);
1308 value
= memcached_get(memc
, keys
[0], key_length
[0],
1309 &value_length
, &flags
, &rc
);
1310 assert(value
== NULL
);
1311 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1314 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1315 &value_length
, &flags
, &rc
)))
1319 for (x
= 0; x
< 4; x
++)
1321 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1322 insert_data
, VALUE_SIZE_BUG5
,
1323 (time_t)0, (uint32_t)0);
1324 assert(rc
== MEMCACHED_SUCCESS
);
1327 for (x
= 0; x
< 10; x
++)
1329 value
= memcached_get(memc
, keys
[0], key_length
[0],
1330 &value_length
, &flags
, &rc
);
1334 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1336 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1337 &value_length
, &flags
, &rc
)))
1348 uint8_t user_supplied_bug6(memcached_st
*memc
)
1350 memcached_return rc
;
1351 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1352 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1353 char return_key
[MEMCACHED_MAX_KEY
];
1354 size_t return_key_length
;
1356 size_t value_length
;
1360 char insert_data
[VALUE_SIZE_BUG5
];
1362 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1363 insert_data
[x
]= rand();
1365 memcached_flush(memc
, 0);
1366 value
= memcached_get(memc
, keys
[0], key_length
[0],
1367 &value_length
, &flags
, &rc
);
1368 assert(value
== NULL
);
1369 assert(rc
== MEMCACHED_NOTFOUND
);
1370 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1371 assert(rc
== MEMCACHED_SUCCESS
);
1374 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1375 &value_length
, &flags
, &rc
)))
1378 assert(rc
== MEMCACHED_END
);
1380 for (x
= 0; x
< 4; x
++)
1382 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1383 insert_data
, VALUE_SIZE_BUG5
,
1384 (time_t)0, (uint32_t)0);
1385 assert(rc
== MEMCACHED_SUCCESS
);
1388 for (x
= 0; x
< 2; x
++)
1390 value
= memcached_get(memc
, keys
[0], key_length
[0],
1391 &value_length
, &flags
, &rc
);
1395 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1396 assert(rc
== MEMCACHED_SUCCESS
);
1398 /* We test for purge of partial complete fetches */
1399 for (count
= 3; count
; count
--)
1401 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1402 &value_length
, &flags
, &rc
);
1403 assert(rc
== MEMCACHED_SUCCESS
);
1404 assert(!(memcmp(value
, insert_data
, value_length
)));
1405 assert(value_length
);
1413 uint8_t user_supplied_bug8(memcached_st
*memc
)
1415 memcached_return rc
;
1417 memcached_st
*clone
;
1419 memcached_server_st
*servers
;
1420 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";
1422 servers
= memcached_servers_parse(server_list
);
1425 mine
= memcached_create(NULL
);
1426 rc
= memcached_server_push(mine
, servers
);
1427 assert(rc
== MEMCACHED_SUCCESS
);
1428 memcached_server_list_free(servers
);
1431 clone
= memcached_clone(NULL
, mine
);
1433 memcached_quit(mine
);
1434 memcached_quit(clone
);
1437 memcached_free(mine
);
1438 memcached_free(clone
);
1443 /* Test flag store/retrieve */
1444 uint8_t user_supplied_bug7(memcached_st
*memc
)
1446 memcached_return rc
;
1447 char *keys
= "036790384900";
1448 size_t key_length
= strlen("036790384900");
1449 char return_key
[MEMCACHED_MAX_KEY
];
1450 size_t return_key_length
;
1452 size_t value_length
;
1455 char insert_data
[VALUE_SIZE_BUG5
];
1457 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1458 insert_data
[x
]= rand();
1460 memcached_flush(memc
, 0);
1463 rc
= memcached_set(memc
, keys
, key_length
,
1464 insert_data
, VALUE_SIZE_BUG5
,
1466 assert(rc
== MEMCACHED_SUCCESS
);
1469 value
= memcached_get(memc
, keys
, key_length
,
1470 &value_length
, &flags
, &rc
);
1471 assert(flags
== 245);
1475 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1478 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1479 &value_length
, &flags
, &rc
);
1480 assert(flags
== 245);
1488 uint8_t user_supplied_bug9(memcached_st
*memc
)
1490 memcached_return rc
;
1491 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1492 size_t key_length
[3];
1497 char return_key
[MEMCACHED_MAX_KEY
];
1498 size_t return_key_length
;
1500 size_t return_value_length
;
1503 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1504 key_length
[1]= strlen("fudge&*@#");
1505 key_length
[2]= strlen("for^#@&$not");
1508 for (x
= 0; x
< 3; x
++)
1510 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1511 keys
[x
], key_length
[x
],
1512 (time_t)50, (uint32_t)9);
1513 assert(rc
== MEMCACHED_SUCCESS
);
1516 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1517 assert(rc
== MEMCACHED_SUCCESS
);
1519 /* We need to empty the server before continueing test */
1520 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1521 &return_value_length
, &flags
, &rc
)) != NULL
)
1523 assert(return_value
);
1532 /* We are testing with aggressive timeout to get failures */
1533 uint8_t user_supplied_bug10(memcached_st
*memc
)
1537 size_t value_length
= 512;
1540 memcached_return rc
;
1541 unsigned int set
= 1;
1542 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1545 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1546 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1548 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
1550 value
= (char*)malloc(value_length
* sizeof(char));
1552 for (x
= 0; x
< value_length
; x
++)
1553 value
[x
]= (char) (x
% 127);
1555 for (x
= 1; x
<= 100000; ++x
)
1557 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1559 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1561 if (rc
== MEMCACHED_WRITE_FAILURE
)
1566 memcached_free(mclone
);
1572 We are looking failures in the async protocol
1574 uint8_t user_supplied_bug11(memcached_st
*memc
)
1578 size_t value_length
= 512;
1581 memcached_return rc
;
1582 unsigned int set
= 1;
1584 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1586 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1587 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1589 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
1591 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1593 assert(timeout
== -1);
1595 value
= (char*)malloc(value_length
* sizeof(char));
1597 for (x
= 0; x
< value_length
; x
++)
1598 value
[x
]= (char) (x
% 127);
1600 for (x
= 1; x
<= 100000; ++x
)
1602 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1606 memcached_free(mclone
);
1612 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1614 uint8_t user_supplied_bug12(memcached_st
*memc
)
1616 memcached_return rc
;
1618 size_t value_length
;
1620 uint64_t number_value
;
1622 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1623 &value_length
, &flags
, &rc
);
1624 assert(value
== NULL
);
1625 assert(rc
== MEMCACHED_NOTFOUND
);
1627 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1630 assert(value
== NULL
);
1631 assert(rc
== MEMCACHED_NOTFOUND
);
1633 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1635 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1636 &value_length
, &flags
, &rc
);
1638 assert(rc
== MEMCACHED_SUCCESS
);
1641 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1643 assert(number_value
== 2);
1644 assert(rc
== MEMCACHED_SUCCESS
);
1650 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1651 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1653 uint8_t user_supplied_bug13(memcached_st
*memc
)
1655 char key
[] = "key34567890";
1657 memcached_return rc
;
1658 size_t overflowSize
;
1660 char commandFirst
[]= "set key34567890 0 0 ";
1661 char commandLast
[] = " \r\n"; /* first line of command sent to server */
1662 size_t commandLength
;
1665 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
1667 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
1669 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
1671 overflow
= malloc(testSize
);
1672 assert(overflow
!= NULL
);
1674 memset(overflow
, 'x', testSize
);
1675 rc
= memcached_set(memc
, key
, strlen(key
),
1676 overflow
, testSize
, 0, 0);
1677 assert(rc
== MEMCACHED_SUCCESS
);
1684 uint8_t result_static(memcached_st
*memc
)
1686 memcached_result_st result
;
1687 memcached_result_st
*result_ptr
;
1689 result_ptr
= memcached_result_create(memc
, &result
);
1690 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1692 memcached_result_free(&result
);
1697 uint8_t result_alloc(memcached_st
*memc
)
1699 memcached_result_st
*result
;
1701 result
= memcached_result_create(memc
, NULL
);
1703 memcached_result_free(result
);
1708 uint8_t string_static_null(memcached_st
*memc
)
1710 memcached_string_st string
;
1711 memcached_string_st
*string_ptr
;
1713 string_ptr
= memcached_string_create(memc
, &string
, 0);
1714 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1716 memcached_string_free(&string
);
1721 uint8_t string_alloc_null(memcached_st
*memc
)
1723 memcached_string_st
*string
;
1725 string
= memcached_string_create(memc
, NULL
, 0);
1727 memcached_string_free(string
);
1732 uint8_t string_alloc_with_size(memcached_st
*memc
)
1734 memcached_string_st
*string
;
1736 string
= memcached_string_create(memc
, NULL
, 1024);
1738 memcached_string_free(string
);
1743 uint8_t string_alloc_with_size_toobig(memcached_st
*memc
)
1745 memcached_string_st
*string
;
1747 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
1748 assert(string
== NULL
);
1753 uint8_t string_alloc_append(memcached_st
*memc
)
1756 char buffer
[SMALL_STRING_LEN
];
1757 memcached_string_st
*string
;
1759 /* Ring the bell! */
1760 memset(buffer
, 6, SMALL_STRING_LEN
);
1762 string
= memcached_string_create(memc
, NULL
, 100);
1765 for (x
= 0; x
< 1024; x
++)
1767 memcached_return rc
;
1768 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1769 assert(rc
== MEMCACHED_SUCCESS
);
1771 memcached_string_free(string
);
1776 uint8_t string_alloc_append_toobig(memcached_st
*memc
)
1778 memcached_return rc
;
1780 char buffer
[SMALL_STRING_LEN
];
1781 memcached_string_st
*string
;
1783 /* Ring the bell! */
1784 memset(buffer
, 6, SMALL_STRING_LEN
);
1786 string
= memcached_string_create(memc
, NULL
, 100);
1789 for (x
= 0; x
< 1024; x
++)
1791 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1792 assert(rc
== MEMCACHED_SUCCESS
);
1794 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
1795 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
1796 memcached_string_free(string
);
1801 uint8_t cleanup_pairs(memcached_st
*memc
)
1803 pairs_free(global_pairs
);
1808 uint8_t generate_pairs(memcached_st
*memc
)
1810 unsigned long long x
;
1811 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
1812 global_count
= GLOBAL_COUNT
;
1814 for (x
= 0; x
< global_count
; x
++)
1816 global_keys
[x
]= global_pairs
[x
].key
;
1817 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1823 uint8_t generate_large_pairs(memcached_st
*memc
)
1825 unsigned long long x
;
1826 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
1827 global_count
= GLOBAL2_COUNT
;
1829 for (x
= 0; x
< global_count
; x
++)
1831 global_keys
[x
]= global_pairs
[x
].key
;
1832 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1838 uint8_t generate_data(memcached_st
*memc
)
1840 execute_set(memc
, global_pairs
, global_count
);
1845 uint8_t generate_buffer_data(memcached_st
*memc
)
1850 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1851 generate_data(memc
);
1856 uint8_t get_read(memcached_st
*memc
)
1859 memcached_return rc
;
1863 size_t return_value_length
;
1866 for (x
= 0; x
< global_count
; x
++)
1868 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
1869 &return_value_length
, &flags
, &rc
);
1871 assert(return_value);
1872 assert(rc == MEMCACHED_SUCCESS);
1874 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
1882 uint8_t mget_read(memcached_st
*memc
)
1884 memcached_return rc
;
1886 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
1887 assert(rc
== MEMCACHED_SUCCESS
);
1888 /* Turn this into a help function */
1890 char return_key
[MEMCACHED_MAX_KEY
];
1891 size_t return_key_length
;
1893 size_t return_value_length
;
1896 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1897 &return_value_length
, &flags
, &rc
)))
1899 assert(return_value
);
1900 assert(rc
== MEMCACHED_SUCCESS
);
1908 uint8_t mget_read_result(memcached_st
*memc
)
1910 memcached_return rc
;
1912 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
1913 assert(rc
== MEMCACHED_SUCCESS
);
1914 /* Turn this into a help function */
1916 memcached_result_st results_obj
;
1917 memcached_result_st
*results
;
1919 results
= memcached_result_create(memc
, &results_obj
);
1921 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1924 assert(rc
== MEMCACHED_SUCCESS
);
1927 memcached_result_free(&results_obj
);
1933 uint8_t mget_read_function(memcached_st
*memc
)
1935 memcached_return rc
;
1936 unsigned int counter
;
1937 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
1939 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
1940 assert(rc
== MEMCACHED_SUCCESS
);
1942 callbacks
[0]= &callback_counter
;
1944 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1949 uint8_t delete_generate(memcached_st
*memc
)
1953 for (x
= 0; x
< global_count
; x
++)
1955 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1961 uint8_t delete_buffer_generate(memcached_st
*memc
)
1967 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1969 for (x
= 0; x
< global_count
; x
++)
1971 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1977 uint8_t free_data(memcached_st
*memc
)
1979 pairs_free(global_pairs
);
1984 uint8_t add_host_test1(memcached_st
*memc
)
1987 memcached_return rc
;
1988 char servername
[]= "0.example.com";
1989 memcached_server_st
*servers
;
1991 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1993 assert(1 == memcached_server_list_count(servers
));
1995 for (x
= 2; x
< 20; x
++)
1997 char buffer
[SMALL_STRING_LEN
];
1999 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2000 servers
= memcached_server_list_append(servers
, buffer
, 401,
2002 assert(rc
== MEMCACHED_SUCCESS
);
2003 assert(x
== memcached_server_list_count(servers
));
2006 rc
= memcached_server_push(memc
, servers
);
2007 assert(rc
== MEMCACHED_SUCCESS
);
2008 rc
= memcached_server_push(memc
, servers
);
2009 assert(rc
== MEMCACHED_SUCCESS
);
2011 memcached_server_list_free(servers
);
2016 memcached_return
pre_nonblock(memcached_st
*memc
)
2018 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
2020 return MEMCACHED_SUCCESS
;
2023 memcached_return
pre_md5(memcached_st
*memc
)
2025 memcached_hash value
= MEMCACHED_HASH_MD5
;
2026 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2028 return MEMCACHED_SUCCESS
;
2031 memcached_return
pre_crc(memcached_st
*memc
)
2033 memcached_hash value
= MEMCACHED_HASH_CRC
;
2034 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2036 return MEMCACHED_SUCCESS
;
2039 memcached_return
pre_hsieh(memcached_st
*memc
)
2041 memcached_hash value
= MEMCACHED_HASH_HSIEH
;
2042 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2044 return MEMCACHED_SUCCESS
;
2047 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2049 memcached_hash value
= MEMCACHED_HASH_FNV1_64
;
2050 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2052 return MEMCACHED_SUCCESS
;
2055 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2057 memcached_hash value
= MEMCACHED_HASH_FNV1A_64
;
2058 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2060 return MEMCACHED_SUCCESS
;
2063 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2065 memcached_hash value
= MEMCACHED_HASH_FNV1_32
;
2066 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2068 return MEMCACHED_SUCCESS
;
2071 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2073 memcached_hash value
= MEMCACHED_HASH_FNV1A_32
;
2074 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2076 return MEMCACHED_SUCCESS
;
2079 memcached_return
pre_hash_ketama(memcached_st
*memc
)
2081 memcached_hash value
= MEMCACHED_HASH_KETAMA
;
2082 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2084 return MEMCACHED_SUCCESS
;
2087 void my_free(memcached_st
*ptr
, void *mem
)
2092 void *my_malloc(memcached_st
*ptr
, const size_t size
)
2094 return malloc(size
);
2097 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2099 return realloc(mem
, size
);
2102 memcached_return
set_memory_alloc(memcached_st
*memc
)
2105 memcached_malloc_function test_ptr
;
2106 memcached_return rc
;
2108 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2109 assert(rc
== MEMCACHED_SUCCESS
);
2110 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2111 assert(rc
== MEMCACHED_SUCCESS
);
2112 assert(test_ptr
== my_malloc
);
2116 memcached_realloc_function test_ptr
;
2117 memcached_return rc
;
2119 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2120 assert(rc
== MEMCACHED_SUCCESS
);
2121 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2122 assert(rc
== MEMCACHED_SUCCESS
);
2123 assert(test_ptr
== my_realloc
);
2127 memcached_free_function test_ptr
;
2128 memcached_return rc
;
2130 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2131 assert(rc
== MEMCACHED_SUCCESS
);
2132 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2133 assert(rc
== MEMCACHED_SUCCESS
);
2134 assert(test_ptr
== my_free
);
2137 return MEMCACHED_SUCCESS
;
2140 memcached_return
enable_consistent(memcached_st
*memc
)
2142 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2143 memcached_hash hash
;
2144 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, &value
);
2147 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2148 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2150 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2151 assert(hash
== MEMCACHED_HASH_HSIEH
);
2154 return MEMCACHED_SUCCESS
;
2157 memcached_return
enable_cas(memcached_st
*memc
)
2159 unsigned int set
= 1;
2161 memcached_version(memc
);
2163 if (memc
->hosts
[0].major_version
>= 1 &&
2164 memc
->hosts
[0].minor_version
>= 2 &&
2165 memc
->hosts
[0].micro_version
>= 4)
2167 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
2169 return MEMCACHED_SUCCESS
;
2172 return MEMCACHED_FAILURE
;
2175 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2177 memcached_version(memc
);
2179 if (memc
->hosts
[0].major_version
>= 1 &&
2180 memc
->hosts
[0].minor_version
>= 2 &&
2181 memc
->hosts
[0].micro_version
>= 4)
2182 return MEMCACHED_SUCCESS
;
2184 return MEMCACHED_FAILURE
;
2187 memcached_return
pre_unix_socket(memcached_st
*memc
)
2189 memcached_return rc
;
2192 memcached_server_list_free(memc
->hosts
);
2194 memc
->number_of_hosts
= 0;
2196 if (stat("/tmp/memcached.socket", &buf
))
2197 return MEMCACHED_FAILURE
;
2199 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2204 memcached_return
pre_udp(memcached_st
*memc
)
2206 memcached_return rc
;
2208 memcached_server_list_free(memc
->hosts
);
2210 memc
->number_of_hosts
= 0;
2213 return MEMCACHED_FAILURE
;
2215 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2220 memcached_return
pre_nodelay(memcached_st
*memc
)
2222 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
2223 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
2225 return MEMCACHED_SUCCESS
;
2228 memcached_return
poll_timeout(memcached_st
*memc
)
2234 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
2236 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2238 assert(timeout
== 100);
2240 return MEMCACHED_SUCCESS
;
2244 /* Clean the server before beginning testing */
2246 {"flush", 0, flush_test
},
2247 {"init", 0, init_test
},
2248 {"allocation", 0, allocation_test
},
2249 {"server_list_null_test", 0, server_list_null_test
},
2250 {"clone_test", 0, clone_test
},
2251 {"error", 0, error_test
},
2252 {"set", 0, set_test
},
2253 {"set2", 0, set_test2
},
2254 {"set3", 0, set_test3
},
2255 {"add", 1, add_test
},
2256 {"replace", 0, replace_test
},
2257 {"delete", 1, delete_test
},
2258 {"get", 1, get_test
},
2259 {"get2", 0, get_test2
},
2260 {"get3", 0, get_test3
},
2261 {"get4", 0, get_test4
},
2262 {"stats_servername", 0, stats_servername_test
},
2263 {"increment", 0, increment_test
},
2264 {"decrement", 0, decrement_test
},
2265 {"quit", 0, quit_test
},
2266 {"mget", 1, mget_test
},
2267 {"mget_result", 1, mget_result_test
},
2268 {"mget_result_alloc", 1, mget_result_alloc_test
},
2269 {"mget_result_function", 1, mget_result_function
},
2270 {"get_stats", 0, get_stats
},
2271 {"add_host_test", 0, add_host_test
},
2272 {"get_stats_keys", 0, get_stats_keys
},
2273 {"behavior_test", 0, get_stats_keys
},
2274 {"callback_test", 0, get_stats_keys
},
2275 {"version_string_test", 0, version_string_test
},
2279 test_st async_tests
[] ={
2280 {"add", 1, add_wrapper
},
2284 test_st string_tests
[] ={
2285 {"string static with null", 0, string_static_null
},
2286 {"string alloc with null", 0, string_alloc_null
},
2287 {"string alloc with 1K", 0, string_alloc_with_size
},
2288 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2289 {"string append", 0, string_alloc_append
},
2290 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2294 test_st result_tests
[] ={
2295 {"result static", 0, result_static
},
2296 {"result alloc", 0, result_alloc
},
2300 test_st version_1_2_3
[] ={
2301 {"append", 0, append_test
},
2302 {"prepend", 0, prepend_test
},
2303 {"cas", 0, cas_test
},
2304 {"cas2", 0, cas2_test
},
2305 {"append_binary", 0, append_binary_test
},
2309 test_st user_tests
[] ={
2310 {"user_supplied_bug1", 0, user_supplied_bug1
},
2311 {"user_supplied_bug2", 0, user_supplied_bug2
},
2312 {"user_supplied_bug3", 0, user_supplied_bug3
},
2313 {"user_supplied_bug4", 0, user_supplied_bug4
},
2314 {"user_supplied_bug5", 1, user_supplied_bug5
},
2315 {"user_supplied_bug6", 1, user_supplied_bug6
},
2316 {"user_supplied_bug7", 1, user_supplied_bug7
},
2317 {"user_supplied_bug8", 1, user_supplied_bug8
},
2318 {"user_supplied_bug9", 1, user_supplied_bug9
},
2319 {"user_supplied_bug10", 1, user_supplied_bug10
},
2320 {"user_supplied_bug11", 1, user_supplied_bug11
},
2321 {"user_supplied_bug12", 1, user_supplied_bug12
},
2322 {"user_supplied_bug13", 1, user_supplied_bug13
},
2326 test_st generate_tests
[] ={
2327 {"generate_pairs", 1, generate_pairs
},
2328 {"generate_data", 1, generate_data
},
2329 {"get_read", 0, get_read
},
2330 {"delete_generate", 0, delete_generate
},
2331 {"generate_buffer_data", 1, generate_buffer_data
},
2332 {"delete_buffer", 0, delete_buffer_generate
},
2333 {"generate_data", 1, generate_data
},
2334 {"mget_read", 0, mget_read
},
2335 {"mget_read_result", 0, mget_read_result
},
2336 {"mget_read_function", 0, mget_read_function
},
2337 {"cleanup", 1, cleanup_pairs
},
2338 {"generate_large_pairs", 1, generate_large_pairs
},
2339 {"generate_data", 1, generate_data
},
2340 {"generate_buffer_data", 1, generate_buffer_data
},
2341 {"cleanup", 1, cleanup_pairs
},
2346 collection_st collection
[] ={
2347 {"block", 0, 0, tests
},
2348 {"nonblock", pre_nonblock
, 0, tests
},
2349 {"nodelay", pre_nodelay
, 0, tests
},
2350 {"md5", pre_md5
, 0, tests
},
2351 {"crc", pre_crc
, 0, tests
},
2352 {"hsieh", pre_hsieh
, 0, tests
},
2353 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2354 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2355 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2356 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2357 {"ketama", pre_hash_ketama
, 0, tests
},
2358 {"unix_socket", pre_unix_socket
, 0, tests
},
2359 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2360 {"poll_timeout", poll_timeout
, 0, tests
},
2361 {"gets", enable_cas
, 0, tests
},
2362 {"consistent", enable_consistent
, 0, tests
},
2363 {"memory_allocators", set_memory_alloc
, 0, tests
},
2364 // {"udp", pre_udp, 0, tests},
2365 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2366 {"string", 0, 0, string_tests
},
2367 {"result", 0, 0, result_tests
},
2368 {"async", pre_nonblock
, 0, async_tests
},
2369 {"user", 0, 0, user_tests
},
2370 {"generate", 0, 0, generate_tests
},
2371 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2372 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2373 {"generate_md5", pre_md5
, 0, generate_tests
},
2374 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2378 collection_st
*gets_collections(void)