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 static pairs_st
*global_pairs
;
30 static char *global_keys
[GLOBAL_COUNT
];
31 static size_t global_keys_length
[GLOBAL_COUNT
];
33 uint8_t init_test(memcached_st
*not_used
)
37 (void)memcached_create(&memc
);
38 memcached_free(&memc
);
43 uint8_t server_list_null_test(memcached_st
*ptr
)
45 memcached_server_st
*server_list
;
48 server_list
= memcached_server_list_append(NULL
, NULL
, 0, NULL
);
49 assert(server_list
== NULL
);
51 server_list
= memcached_server_list_append(NULL
, "localhost", 0, NULL
);
52 assert(server_list
== NULL
);
54 server_list
= memcached_server_list_append(NULL
, NULL
, 0, &rc
);
55 assert(server_list
== NULL
);
60 uint8_t allocation_test(memcached_st
*not_used
)
63 memc
= memcached_create(NULL
);
70 uint8_t clone_test(memcached_st
*memc
)
75 clone
= memcached_clone(NULL
, NULL
);
77 memcached_free(clone
);
80 /* Can we init from null? */
83 clone
= memcached_clone(NULL
, memc
);
85 memcached_free(clone
);
88 /* Can we init from struct? */
90 memcached_st declared_clone
;
92 clone
= memcached_clone(&declared_clone
, NULL
);
94 memcached_free(clone
);
97 /* Can we init from struct? */
99 memcached_st declared_clone
;
101 clone
= memcached_clone(&declared_clone
, memc
);
103 memcached_free(clone
);
109 uint8_t connection_test(memcached_st
*memc
)
113 rc
= memcached_server_add(memc
, "localhost", 0);
114 assert(rc
== MEMCACHED_SUCCESS
);
119 uint8_t error_test(memcached_st
*memc
)
123 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
125 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
131 uint8_t set_test(memcached_st
*memc
)
135 char *value
= "when we sanitize";
137 rc
= memcached_set(memc
, key
, strlen(key
),
138 value
, strlen(value
),
139 (time_t)0, (uint32_t)0);
140 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
145 uint8_t append_test(memcached_st
*memc
)
153 rc
= memcached_flush(memc
, 0);
154 assert(rc
== MEMCACHED_SUCCESS
);
156 rc
= memcached_set(memc
, key
, strlen(key
),
157 value
, strlen(value
),
158 (time_t)0, (uint32_t)0);
159 assert(rc
== MEMCACHED_SUCCESS
);
161 rc
= memcached_append(memc
, key
, strlen(key
),
162 " the", strlen(" the"),
163 (time_t)0, (uint32_t)0);
164 assert(rc
== MEMCACHED_SUCCESS
);
166 rc
= memcached_append(memc
, key
, strlen(key
),
167 " people", strlen(" people"),
168 (time_t)0, (uint32_t)0);
169 assert(rc
== MEMCACHED_SUCCESS
);
171 value
= memcached_get(memc
, key
, strlen(key
),
172 &value_length
, &flags
, &rc
);
173 assert(!memcmp(value
, "we the people", strlen("we the people")));
174 assert(strlen("we the people") == value_length
);
175 assert(rc
== MEMCACHED_SUCCESS
);
181 uint8_t append_binary_test(memcached_st
*memc
)
184 char *key
= "numbers";
185 unsigned int *store_ptr
;
186 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
192 rc
= memcached_flush(memc
, 0);
193 assert(rc
== MEMCACHED_SUCCESS
);
195 rc
= memcached_set(memc
,
198 (time_t)0, (uint32_t)0);
199 assert(rc
== MEMCACHED_SUCCESS
);
201 for (x
= 0; store_list
[x
] ; x
++)
203 rc
= memcached_append(memc
,
205 (char *)&store_list
[x
], sizeof(unsigned int),
206 (time_t)0, (uint32_t)0);
207 assert(rc
== MEMCACHED_SUCCESS
);
210 value
= memcached_get(memc
, key
, strlen(key
),
211 &value_length
, &flags
, &rc
);
212 assert((value_length
== (sizeof(unsigned int) * x
)));
213 assert(rc
== MEMCACHED_SUCCESS
);
215 store_ptr
= (unsigned int *)value
;
217 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
219 assert(*store_ptr
== store_list
[x
++]);
227 uint8_t cas2_test(memcached_st
*memc
)
230 char *keys
[]= {"fudge", "son", "food"};
231 size_t key_length
[]= {5, 3, 4};
232 char *value
= "we the people";
233 size_t value_length
= strlen("we the people");
235 memcached_result_st results_obj
;
236 memcached_result_st
*results
;
239 rc
= memcached_flush(memc
, 0);
240 assert(rc
== MEMCACHED_SUCCESS
);
242 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
244 for (x
= 0; x
< 3; x
++)
246 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
247 keys
[x
], key_length
[x
],
248 (time_t)50, (uint32_t)9);
249 assert(rc
== MEMCACHED_SUCCESS
);
252 rc
= memcached_mget(memc
, keys
, key_length
, 3);
254 results
= memcached_result_create(memc
, &results_obj
);
256 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
258 assert(results
->cas
);
259 assert(rc
== MEMCACHED_SUCCESS
);
260 WATCHPOINT_ASSERT(memcached_result_cas(results
));
262 assert(!memcmp(value
, "we the people", strlen("we the people")));
263 assert(strlen("we the people") == value_length
);
264 assert(rc
== MEMCACHED_SUCCESS
);
266 memcached_result_free(&results_obj
);
271 uint8_t cas_test(memcached_st
*memc
)
275 size_t key_length
= strlen("fun");
276 char *value
= "we the people";
277 size_t value_length
= strlen("we the people");
278 memcached_result_st results_obj
;
279 memcached_result_st
*results
;
282 rc
= memcached_flush(memc
, 0);
283 assert(rc
== MEMCACHED_SUCCESS
);
285 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
287 rc
= memcached_set(memc
, key
, strlen(key
),
288 value
, strlen(value
),
289 (time_t)0, (uint32_t)0);
290 assert(rc
== MEMCACHED_SUCCESS
);
292 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
294 results
= memcached_result_create(memc
, &results_obj
);
296 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
298 assert(rc
== MEMCACHED_SUCCESS
);
299 WATCHPOINT_ASSERT(memcached_result_cas(results
));
301 assert(!memcmp(value
, "we the people", strlen("we the people")));
302 assert(strlen("we the people") == value_length
);
303 assert(rc
== MEMCACHED_SUCCESS
);
305 memcached_result_free(&results_obj
);
310 uint8_t prepend_test(memcached_st
*memc
)
314 char *value
= "people";
318 rc
= memcached_flush(memc
, 0);
319 assert(rc
== MEMCACHED_SUCCESS
);
321 rc
= memcached_set(memc
, key
, strlen(key
),
322 value
, strlen(value
),
323 (time_t)0, (uint32_t)0);
324 assert(rc
== MEMCACHED_SUCCESS
);
326 rc
= memcached_prepend(memc
, key
, strlen(key
),
327 "the ", strlen("the "),
328 (time_t)0, (uint32_t)0);
329 assert(rc
== MEMCACHED_SUCCESS
);
331 rc
= memcached_prepend(memc
, key
, strlen(key
),
332 "we ", strlen("we "),
333 (time_t)0, (uint32_t)0);
334 assert(rc
== MEMCACHED_SUCCESS
);
336 value
= memcached_get(memc
, key
, strlen(key
),
337 &value_length
, &flags
, &rc
);
338 assert(!memcmp(value
, "we the people", strlen("we the people")));
339 assert(strlen("we the people") == value_length
);
340 assert(rc
== MEMCACHED_SUCCESS
);
347 Set the value, then quit to make sure it is flushed.
348 Come back in and test that add fails.
350 uint8_t add_test(memcached_st
*memc
)
354 char *value
= "when we sanitize";
356 rc
= memcached_set(memc
, key
, strlen(key
),
357 value
, strlen(value
),
358 (time_t)0, (uint32_t)0);
359 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
360 memcached_quit(memc
);
361 rc
= memcached_add(memc
, key
, strlen(key
),
362 value
, strlen(value
),
363 (time_t)0, (uint32_t)0);
364 assert(rc
== MEMCACHED_NOTSTORED
);
369 uint8_t add_wrapper(memcached_st
*memc
)
373 for (x
= 0; x
< 10000; x
++)
379 uint8_t replace_test(memcached_st
*memc
)
383 char *value
= "when we sanitize";
385 rc
= memcached_replace(memc
, key
, strlen(key
),
386 value
, strlen(value
),
387 (time_t)0, (uint32_t)0);
388 assert(rc
== MEMCACHED_SUCCESS
);
393 uint8_t delete_test(memcached_st
*memc
)
397 char *value
= "when we sanitize";
399 rc
= memcached_set(memc
, key
, strlen(key
),
400 value
, strlen(value
),
401 (time_t)0, (uint32_t)0);
402 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
404 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
405 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
410 uint8_t flush_test(memcached_st
*memc
)
414 rc
= memcached_flush(memc
, 0);
415 assert(rc
== MEMCACHED_SUCCESS
);
420 uint8_t get_test(memcached_st
*memc
)
425 size_t string_length
;
428 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
429 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
431 string
= memcached_get(memc
, key
, strlen(key
),
432 &string_length
, &flags
, &rc
);
434 assert(rc
== MEMCACHED_NOTFOUND
);
435 assert(string_length
== 0);
441 uint8_t get_test2(memcached_st
*memc
)
445 char *value
= "when we sanitize";
447 size_t string_length
;
450 rc
= memcached_set(memc
, key
, strlen(key
),
451 value
, strlen(value
),
452 (time_t)0, (uint32_t)0);
453 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
455 string
= memcached_get(memc
, key
, strlen(key
),
456 &string_length
, &flags
, &rc
);
459 assert(rc
== MEMCACHED_SUCCESS
);
460 assert(string_length
== strlen(value
));
461 assert(!memcmp(string
, value
, string_length
));
468 uint8_t set_test2(memcached_st
*memc
)
472 char *value
= "train in the brain";
473 size_t value_length
= strlen(value
);
476 for (x
= 0; x
< 10; x
++)
478 rc
= memcached_set(memc
, key
, strlen(key
),
480 (time_t)0, (uint32_t)0);
481 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
487 uint8_t set_test3(memcached_st
*memc
)
492 size_t value_length
= 8191;
495 value
= (char*)malloc(value_length
);
498 for (x
= 0; x
< value_length
; x
++)
499 value
[x
] = (char) (x
% 127);
501 for (x
= 0; x
< 1; x
++)
503 rc
= memcached_set(memc
, key
, strlen(key
),
505 (time_t)0, (uint32_t)0);
506 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
514 uint8_t get_test3(memcached_st
*memc
)
519 size_t value_length
= 8191;
521 size_t string_length
;
525 value
= (char*)malloc(value_length
);
528 for (x
= 0; x
< value_length
; x
++)
529 value
[x
] = (char) (x
% 127);
531 rc
= memcached_set(memc
, key
, strlen(key
),
533 (time_t)0, (uint32_t)0);
534 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
536 string
= memcached_get(memc
, key
, strlen(key
),
537 &string_length
, &flags
, &rc
);
539 assert(rc
== MEMCACHED_SUCCESS
);
541 assert(string_length
== value_length
);
542 assert(!memcmp(string
, value
, string_length
));
550 uint8_t get_test4(memcached_st
*memc
)
555 size_t value_length
= 8191;
557 size_t string_length
;
561 value
= (char*)malloc(value_length
);
564 for (x
= 0; x
< value_length
; x
++)
565 value
[x
] = (char) (x
% 127);
567 rc
= memcached_set(memc
, key
, strlen(key
),
569 (time_t)0, (uint32_t)0);
570 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
572 for (x
= 0; x
< 10; x
++)
574 string
= memcached_get(memc
, key
, strlen(key
),
575 &string_length
, &flags
, &rc
);
577 assert(rc
== MEMCACHED_SUCCESS
);
579 assert(string_length
== value_length
);
580 assert(!memcmp(string
, value
, string_length
));
589 /* Do not copy the style of this code, I just access hosts to testthis function */
590 uint8_t stats_servername_test(memcached_st
*memc
)
593 memcached_stat_st stat
;
594 rc
= memcached_stat_servername(&stat
, NULL
,
595 memc
->hosts
[0].hostname
,
596 memc
->hosts
[0].port
);
601 uint8_t increment_test(memcached_st
*memc
)
608 rc
= memcached_set(memc
, key
, strlen(key
),
609 value
, strlen(value
),
610 (time_t)0, (uint32_t)0);
611 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
613 rc
= memcached_increment(memc
, key
, strlen(key
),
615 assert(rc
== MEMCACHED_SUCCESS
);
616 assert(new_number
== 1);
618 rc
= memcached_increment(memc
, key
, strlen(key
),
620 assert(rc
== MEMCACHED_SUCCESS
);
621 assert(new_number
== 2);
626 uint8_t decrement_test(memcached_st
*memc
)
633 rc
= memcached_set(memc
, key
, strlen(key
),
634 value
, strlen(value
),
635 (time_t)0, (uint32_t)0);
636 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
638 rc
= memcached_decrement(memc
, key
, strlen(key
),
640 assert(rc
== MEMCACHED_SUCCESS
);
641 assert(new_number
== 2);
643 rc
= memcached_decrement(memc
, key
, strlen(key
),
645 assert(rc
== MEMCACHED_SUCCESS
);
646 assert(new_number
== 1);
651 uint8_t quit_test(memcached_st
*memc
)
655 char *value
= "sanford and sun";
657 rc
= memcached_set(memc
, key
, strlen(key
),
658 value
, strlen(value
),
659 (time_t)10, (uint32_t)3);
660 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
661 memcached_quit(memc
);
663 rc
= memcached_set(memc
, key
, strlen(key
),
664 value
, strlen(value
),
665 (time_t)50, (uint32_t)9);
666 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
671 uint8_t mget_result_test(memcached_st
*memc
)
674 char *keys
[]= {"fudge", "son", "food"};
675 size_t key_length
[]= {5, 3, 4};
678 memcached_result_st results_obj
;
679 memcached_result_st
*results
;
681 results
= memcached_result_create(memc
, &results_obj
);
683 assert(&results_obj
== results
);
685 /* We need to empty the server before continueing test */
686 rc
= memcached_flush(memc
, 0);
687 assert(rc
== MEMCACHED_SUCCESS
);
689 rc
= memcached_mget(memc
, keys
, key_length
, 3);
690 assert(rc
== MEMCACHED_SUCCESS
);
692 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
697 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
699 assert(rc
== MEMCACHED_END
);
701 for (x
= 0; x
< 3; x
++)
703 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
704 keys
[x
], key_length
[x
],
705 (time_t)50, (uint32_t)9);
706 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
709 rc
= memcached_mget(memc
, keys
, key_length
, 3);
710 assert(rc
== MEMCACHED_SUCCESS
);
712 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
715 assert(&results_obj
== results
);
716 assert(rc
== MEMCACHED_SUCCESS
);
717 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
718 assert(!memcmp(memcached_result_key_value(results
),
719 memcached_result_value(results
),
720 memcached_result_length(results
)));
723 memcached_result_free(&results_obj
);
728 uint8_t mget_result_alloc_test(memcached_st
*memc
)
731 char *keys
[]= {"fudge", "son", "food"};
732 size_t key_length
[]= {5, 3, 4};
735 memcached_result_st
*results
;
737 /* We need to empty the server before continueing test */
738 rc
= memcached_flush(memc
, 0);
739 assert(rc
== MEMCACHED_SUCCESS
);
741 rc
= memcached_mget(memc
, keys
, key_length
, 3);
742 assert(rc
== MEMCACHED_SUCCESS
);
744 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
749 assert(rc
== MEMCACHED_END
);
751 for (x
= 0; x
< 3; x
++)
753 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
754 keys
[x
], key_length
[x
],
755 (time_t)50, (uint32_t)9);
756 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
759 rc
= memcached_mget(memc
, keys
, key_length
, 3);
760 assert(rc
== MEMCACHED_SUCCESS
);
763 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
766 assert(rc
== MEMCACHED_SUCCESS
);
767 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
768 assert(!memcmp(memcached_result_key_value(results
),
769 memcached_result_value(results
),
770 memcached_result_length(results
)));
771 memcached_result_free(results
);
778 /* Count the results */
779 unsigned int callback_counter(memcached_st
*ptr
, memcached_result_st
*result
, void *context
)
781 unsigned int *counter
= (unsigned int *)context
;
783 *counter
= *counter
+ 1;
788 uint8_t mget_result_function(memcached_st
*memc
)
791 char *keys
[]= {"fudge", "son", "food"};
792 size_t key_length
[]= {5, 3, 4};
794 unsigned int counter
;
795 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
797 /* We need to empty the server before continueing test */
798 rc
= memcached_flush(memc
, 0);
799 for (x
= 0; x
< 3; x
++)
801 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
802 keys
[x
], key_length
[x
],
803 (time_t)50, (uint32_t)9);
804 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
807 rc
= memcached_mget(memc
, keys
, key_length
, 3);
808 assert(rc
== MEMCACHED_SUCCESS
);
810 callbacks
[0]= &callback_counter
;
812 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
814 assert(counter
== 3);
819 uint8_t mget_test(memcached_st
*memc
)
822 char *keys
[]= {"fudge", "son", "food"};
823 size_t key_length
[]= {5, 3, 4};
827 char return_key
[MEMCACHED_MAX_KEY
];
828 size_t return_key_length
;
830 size_t return_value_length
;
832 /* We need to empty the server before continueing test */
833 rc
= memcached_flush(memc
, 0);
834 assert(rc
== MEMCACHED_SUCCESS
);
836 rc
= memcached_mget(memc
, keys
, key_length
, 3);
837 assert(rc
== MEMCACHED_SUCCESS
);
839 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
840 &return_value_length
, &flags
, &rc
)) != NULL
)
842 assert(return_value
);
844 assert(!return_value
);
845 assert(return_value_length
== 0);
846 assert(rc
== MEMCACHED_END
);
848 for (x
= 0; x
< 3; x
++)
850 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
851 keys
[x
], key_length
[x
],
852 (time_t)50, (uint32_t)9);
853 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
856 rc
= memcached_mget(memc
, keys
, key_length
, 3);
857 assert(rc
== MEMCACHED_SUCCESS
);
860 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
861 &return_value_length
, &flags
, &rc
)))
863 assert(return_value
);
864 assert(rc
== MEMCACHED_SUCCESS
);
865 assert(return_key_length
== return_value_length
);
866 assert(!memcmp(return_value
, return_key
, return_value_length
));
874 uint8_t get_stats_keys(memcached_st
*memc
)
878 memcached_stat_st stat
;
881 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
882 assert(rc
== MEMCACHED_SUCCESS
);
883 for (ptr
= list
; *ptr
; ptr
++)
892 uint8_t version_string_test(memcached_st
*memc
)
894 const char *version_string
;
896 version_string
= memcached_lib_version();
898 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
903 uint8_t get_stats(memcached_st
*memc
)
909 memcached_stat_st
*stat
;
911 stat
= memcached_stat(memc
, NULL
, &rc
);
912 assert(rc
== MEMCACHED_SUCCESS
);
914 assert(rc
== MEMCACHED_SUCCESS
);
917 for (x
= 0; x
< memcached_server_count(memc
); x
++)
919 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
920 assert(rc
== MEMCACHED_SUCCESS
);
921 for (ptr
= list
; *ptr
; ptr
++);
926 memcached_stat_free(NULL
, stat
);
931 uint8_t add_host_test(memcached_st
*memc
)
934 memcached_server_st
*servers
;
936 char servername
[]= "0.example.com";
938 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
940 assert(1 == memcached_server_list_count(servers
));
942 for (x
= 2; x
< 20; x
++)
944 char buffer
[SMALL_STRING_LEN
];
946 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
947 servers
= memcached_server_list_append(servers
, buffer
, 401,
949 assert(rc
== MEMCACHED_SUCCESS
);
950 assert(x
== memcached_server_list_count(servers
));
953 rc
= memcached_server_push(memc
, servers
);
954 assert(rc
== MEMCACHED_SUCCESS
);
955 rc
= memcached_server_push(memc
, servers
);
956 assert(rc
== MEMCACHED_SUCCESS
);
958 memcached_server_list_free(servers
);
963 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
965 return MEMCACHED_SUCCESS
;
968 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
970 return MEMCACHED_SUCCESS
;
973 uint8_t callback_test(memcached_st
*memc
)
981 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
982 assert(rc
== MEMCACHED_SUCCESS
);
983 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
984 assert(*test_ptr
== x
);
987 /* Test Clone Callback */
989 memcached_clone_func temp_function
;
992 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
993 assert(rc
== MEMCACHED_SUCCESS
);
994 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
995 assert(temp_function
== clone_test_callback
);
998 /* Test Cleanup Callback */
1000 memcached_cleanup_func temp_function
;
1001 memcached_return rc
;
1003 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1004 assert(rc
== MEMCACHED_SUCCESS
);
1005 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1006 assert(temp_function
== cleanup_test_callback
);
1012 /* We don't test the behavior itself, we test the switches */
1013 uint8_t behavior_test(memcached_st
*memc
)
1015 unsigned long long value
;
1016 unsigned int set
= 1;
1018 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1019 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1022 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1023 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1026 set
= MEMCACHED_HASH_MD5
;
1027 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1028 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1029 assert(value
== MEMCACHED_HASH_MD5
);
1033 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1034 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1037 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1038 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1041 set
= MEMCACHED_HASH_DEFAULT
;
1042 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1043 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1044 assert(value
== MEMCACHED_HASH_DEFAULT
);
1046 set
= MEMCACHED_HASH_CRC
;
1047 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1048 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1049 assert(value
== MEMCACHED_HASH_CRC
);
1051 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1054 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1060 /* Test case provided by Cal Haldenbrand */
1061 uint8_t user_supplied_bug1(memcached_st
*memc
)
1063 unsigned int setter
= 1;
1066 unsigned long long total
= 0;
1069 char randomstuff
[6 * 1024];
1070 memcached_return rc
;
1072 memset(randomstuff
, 0, 6 * 1024);
1074 /* We just keep looking at the same values over and over */
1077 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1078 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1082 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1086 size
= (rand() % ( 5 * 1024 ) ) + 400;
1087 memset(randomstuff
, 0, 6 * 1024);
1088 assert(size
< 6 * 1024); /* Being safe here */
1090 for (j
= 0 ; j
< size
;j
++)
1091 randomstuff
[j
] = (char) (rand() % 26) + 97;
1094 sprintf(key
, "%d", x
);
1095 rc
= memcached_set(memc
, key
, strlen(key
),
1096 randomstuff
, strlen(randomstuff
), 10, 0);
1097 /* If we fail, lets try again */
1098 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1099 rc
= memcached_set(memc
, key
, strlen(key
),
1100 randomstuff
, strlen(randomstuff
), 10, 0);
1101 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1107 /* Test case provided by Cal Haldenbrand */
1108 uint8_t user_supplied_bug2(memcached_st
*memc
)
1111 unsigned int setter
;
1113 unsigned long long total
;
1116 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1117 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1119 setter
= 20 * 1024576;
1120 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
1121 setter
= 20 * 1024576;
1122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
1123 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1124 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1126 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1129 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1131 memcached_return rc
= MEMCACHED_SUCCESS
;
1132 char buffer
[SMALL_STRING_LEN
];
1137 memset(buffer
, 0, SMALL_STRING_LEN
);
1139 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1140 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1141 &val_len
, &flags
, &rc
);
1142 if (rc
!= MEMCACHED_SUCCESS
)
1144 if (rc
== MEMCACHED_NOTFOUND
)
1159 /* Do a large mget() over all the keys we think exist */
1160 #define KEY_COUNT 3000 // * 1024576
1161 uint8_t user_supplied_bug3(memcached_st
*memc
)
1163 memcached_return rc
;
1164 unsigned int setter
;
1167 size_t key_lengths
[KEY_COUNT
];
1170 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1171 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1173 setter
= 20 * 1024576;
1174 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
1175 setter
= 20 * 1024576;
1176 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
1177 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1178 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1181 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1183 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1184 for (x
= 0; x
< KEY_COUNT
; x
++)
1188 snprintf(buffer
, 30, "%u", x
);
1189 keys
[x
]= strdup(buffer
);
1190 key_lengths
[x
]= strlen(keys
[x
]);
1193 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1194 assert(rc
== MEMCACHED_SUCCESS
);
1196 /* Turn this into a help function */
1198 char return_key
[MEMCACHED_MAX_KEY
];
1199 size_t return_key_length
;
1201 size_t return_value_length
;
1204 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1205 &return_value_length
, &flags
, &rc
)))
1207 assert(return_value
);
1208 assert(rc
== MEMCACHED_SUCCESS
);
1213 for (x
= 0; x
< KEY_COUNT
; x
++)
1220 /* Make sure we behave properly if server list has no values */
1221 uint8_t user_supplied_bug4(memcached_st
*memc
)
1223 memcached_return rc
;
1224 char *keys
[]= {"fudge", "son", "food"};
1225 size_t key_length
[]= {5, 3, 4};
1228 char return_key
[MEMCACHED_MAX_KEY
];
1229 size_t return_key_length
;
1231 size_t return_value_length
;
1233 /* Here we free everything before running a bunch of mget tests */
1235 memcached_server_list_free(memc
->hosts
);
1237 memc
->number_of_hosts
= 0;
1241 /* We need to empty the server before continueing test */
1242 rc
= memcached_flush(memc
, 0);
1243 assert(rc
== MEMCACHED_NO_SERVERS
);
1245 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1246 assert(rc
== MEMCACHED_NO_SERVERS
);
1248 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1249 &return_value_length
, &flags
, &rc
)) != NULL
)
1251 assert(return_value
);
1253 assert(!return_value
);
1254 assert(return_value_length
== 0);
1255 assert(rc
== MEMCACHED_NO_SERVERS
);
1257 for (x
= 0; x
< 3; x
++)
1259 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1260 keys
[x
], key_length
[x
],
1261 (time_t)50, (uint32_t)9);
1262 assert(rc
== MEMCACHED_NO_SERVERS
);
1265 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1266 assert(rc
== MEMCACHED_NO_SERVERS
);
1269 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1270 &return_value_length
, &flags
, &rc
)))
1272 assert(return_value
);
1273 assert(rc
== MEMCACHED_SUCCESS
);
1274 assert(return_key_length
== return_value_length
);
1275 assert(!memcmp(return_value
, return_key
, return_value_length
));
1283 #define VALUE_SIZE_BUG5 1048064
1284 uint8_t user_supplied_bug5(memcached_st
*memc
)
1286 memcached_return rc
;
1287 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1288 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1289 char return_key
[MEMCACHED_MAX_KEY
];
1290 size_t return_key_length
;
1292 size_t value_length
;
1296 char insert_data
[VALUE_SIZE_BUG5
];
1298 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1299 insert_data
[x
]= rand();
1301 memcached_flush(memc
, 0);
1302 value
= memcached_get(memc
, keys
[0], key_length
[0],
1303 &value_length
, &flags
, &rc
);
1304 assert(value
== NULL
);
1305 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1308 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1309 &value_length
, &flags
, &rc
)))
1313 for (x
= 0; x
< 4; x
++)
1315 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1316 insert_data
, VALUE_SIZE_BUG5
,
1317 (time_t)0, (uint32_t)0);
1318 assert(rc
== MEMCACHED_SUCCESS
);
1321 for (x
= 0; x
< 10; x
++)
1323 value
= memcached_get(memc
, keys
[0], key_length
[0],
1324 &value_length
, &flags
, &rc
);
1328 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1330 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1331 &value_length
, &flags
, &rc
)))
1342 uint8_t user_supplied_bug6(memcached_st
*memc
)
1344 memcached_return rc
;
1345 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1346 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1347 char return_key
[MEMCACHED_MAX_KEY
];
1348 size_t return_key_length
;
1350 size_t value_length
;
1354 char insert_data
[VALUE_SIZE_BUG5
];
1356 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1357 insert_data
[x
]= rand();
1359 memcached_flush(memc
, 0);
1360 value
= memcached_get(memc
, keys
[0], key_length
[0],
1361 &value_length
, &flags
, &rc
);
1362 assert(value
== NULL
);
1363 assert(rc
== MEMCACHED_NOTFOUND
);
1364 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1365 assert(rc
== MEMCACHED_SUCCESS
);
1368 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1369 &value_length
, &flags
, &rc
)))
1372 assert(rc
== MEMCACHED_END
);
1374 for (x
= 0; x
< 4; x
++)
1376 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1377 insert_data
, VALUE_SIZE_BUG5
,
1378 (time_t)0, (uint32_t)0);
1379 assert(rc
== MEMCACHED_SUCCESS
);
1382 for (x
= 0; x
< 2; x
++)
1384 value
= memcached_get(memc
, keys
[0], key_length
[0],
1385 &value_length
, &flags
, &rc
);
1389 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1390 assert(rc
== MEMCACHED_SUCCESS
);
1392 /* We test for purge of partial complete fetches */
1393 for (count
= 3; count
; count
--)
1395 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1396 &value_length
, &flags
, &rc
);
1397 assert(rc
== MEMCACHED_SUCCESS
);
1398 assert(!(memcmp(value
, insert_data
, value_length
)));
1399 assert(value_length
);
1407 uint8_t user_supplied_bug8(memcached_st
*memc
)
1409 memcached_return rc
;
1411 memcached_st
*clone
;
1413 memcached_server_st
*servers
;
1414 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";
1416 servers
= memcached_servers_parse(server_list
);
1419 mine
= memcached_create(NULL
);
1420 rc
= memcached_server_push(mine
, servers
);
1421 assert(rc
== MEMCACHED_SUCCESS
);
1422 memcached_server_list_free(servers
);
1425 clone
= memcached_clone(NULL
, mine
);
1427 memcached_quit(mine
);
1428 memcached_quit(clone
);
1431 memcached_free(mine
);
1432 memcached_free(clone
);
1437 /* Test flag store/retrieve */
1438 uint8_t user_supplied_bug7(memcached_st
*memc
)
1440 memcached_return rc
;
1441 char *keys
= "036790384900";
1442 size_t key_length
= strlen("036790384900");
1443 char return_key
[MEMCACHED_MAX_KEY
];
1444 size_t return_key_length
;
1446 size_t value_length
;
1449 char insert_data
[VALUE_SIZE_BUG5
];
1451 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1452 insert_data
[x
]= rand();
1454 memcached_flush(memc
, 0);
1457 rc
= memcached_set(memc
, keys
, key_length
,
1458 insert_data
, VALUE_SIZE_BUG5
,
1460 assert(rc
== MEMCACHED_SUCCESS
);
1463 value
= memcached_get(memc
, keys
, key_length
,
1464 &value_length
, &flags
, &rc
);
1465 assert(flags
== 245);
1469 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1472 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1473 &value_length
, &flags
, &rc
);
1474 assert(flags
== 245);
1482 uint8_t user_supplied_bug9(memcached_st
*memc
)
1484 memcached_return rc
;
1485 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1486 size_t key_length
[3];
1491 char return_key
[MEMCACHED_MAX_KEY
];
1492 size_t return_key_length
;
1494 size_t return_value_length
;
1497 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1498 key_length
[1]= strlen("fudge&*@#");
1499 key_length
[2]= strlen("for^#@&$not");
1502 for (x
= 0; x
< 3; x
++)
1504 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1505 keys
[x
], key_length
[x
],
1506 (time_t)50, (uint32_t)9);
1507 assert(rc
== MEMCACHED_SUCCESS
);
1510 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1511 assert(rc
== MEMCACHED_SUCCESS
);
1513 /* We need to empty the server before continueing test */
1514 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1515 &return_value_length
, &flags
, &rc
)) != NULL
)
1517 assert(return_value
);
1526 /* We are testing with aggressive timeout to get failures */
1527 uint8_t user_supplied_bug10(memcached_st
*memc
)
1531 size_t value_length
= 512;
1534 memcached_return rc
;
1535 unsigned int set
= 1;
1536 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1539 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1540 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1542 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
1544 value
= (char*)malloc(value_length
* sizeof(char));
1546 for (x
= 0; x
< value_length
; x
++)
1547 value
[x
]= (char) (x
% 127);
1549 for (x
= 1; x
<= 100000; ++x
)
1551 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1553 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1555 if (rc
== MEMCACHED_WRITE_FAILURE
)
1560 memcached_free(mclone
);
1566 We are looking failures in the async protocol
1568 uint8_t user_supplied_bug11(memcached_st
*memc
)
1572 size_t value_length
= 512;
1575 memcached_return rc
;
1576 unsigned int set
= 1;
1578 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1580 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1581 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1583 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
1585 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1587 assert(timeout
== -1);
1589 value
= (char*)malloc(value_length
* sizeof(char));
1591 for (x
= 0; x
< value_length
; x
++)
1592 value
[x
]= (char) (x
% 127);
1594 for (x
= 1; x
<= 100000; ++x
)
1596 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1600 memcached_free(mclone
);
1606 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1608 uint8_t user_supplied_bug12(memcached_st
*memc
)
1610 memcached_return rc
;
1612 size_t value_length
;
1614 uint64_t number_value
;
1616 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1617 &value_length
, &flags
, &rc
);
1618 assert(value
== NULL
);
1619 assert(rc
== MEMCACHED_NOTFOUND
);
1621 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1624 assert(value
== NULL
);
1625 assert(rc
== MEMCACHED_NOTFOUND
);
1627 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1629 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1630 &value_length
, &flags
, &rc
);
1632 assert(rc
== MEMCACHED_SUCCESS
);
1635 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1637 assert(number_value
== 2);
1638 assert(rc
== MEMCACHED_SUCCESS
);
1643 uint8_t result_static(memcached_st
*memc
)
1645 memcached_result_st result
;
1646 memcached_result_st
*result_ptr
;
1648 result_ptr
= memcached_result_create(memc
, &result
);
1649 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1651 memcached_result_free(&result
);
1656 uint8_t result_alloc(memcached_st
*memc
)
1658 memcached_result_st
*result
;
1660 result
= memcached_result_create(memc
, NULL
);
1662 memcached_result_free(result
);
1667 uint8_t string_static_null(memcached_st
*memc
)
1669 memcached_string_st string
;
1670 memcached_string_st
*string_ptr
;
1672 string_ptr
= memcached_string_create(memc
, &string
, 0);
1673 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1675 memcached_string_free(&string
);
1680 uint8_t string_alloc_null(memcached_st
*memc
)
1682 memcached_string_st
*string
;
1684 string
= memcached_string_create(memc
, NULL
, 0);
1686 memcached_string_free(string
);
1691 uint8_t string_alloc_with_size(memcached_st
*memc
)
1693 memcached_string_st
*string
;
1695 string
= memcached_string_create(memc
, NULL
, 1024);
1697 memcached_string_free(string
);
1702 uint8_t string_alloc_with_size_toobig(memcached_st
*memc
)
1704 memcached_string_st
*string
;
1706 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
1707 assert(string
== NULL
);
1712 uint8_t string_alloc_append(memcached_st
*memc
)
1715 char buffer
[SMALL_STRING_LEN
];
1716 memcached_string_st
*string
;
1718 /* Ring the bell! */
1719 memset(buffer
, 6, SMALL_STRING_LEN
);
1721 string
= memcached_string_create(memc
, NULL
, 100);
1724 for (x
= 0; x
< 1024; x
++)
1726 memcached_return rc
;
1727 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1728 assert(rc
== MEMCACHED_SUCCESS
);
1730 memcached_string_free(string
);
1735 uint8_t string_alloc_append_toobig(memcached_st
*memc
)
1737 memcached_return rc
;
1739 char buffer
[SMALL_STRING_LEN
];
1740 memcached_string_st
*string
;
1742 /* Ring the bell! */
1743 memset(buffer
, 6, SMALL_STRING_LEN
);
1745 string
= memcached_string_create(memc
, NULL
, 100);
1748 for (x
= 0; x
< 1024; x
++)
1750 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1751 assert(rc
== MEMCACHED_SUCCESS
);
1753 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
1754 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
1755 memcached_string_free(string
);
1760 uint8_t cleanup_pairs(memcached_st
*memc
)
1762 pairs_free(global_pairs
);
1767 uint8_t generate_data(memcached_st
*memc
)
1769 unsigned long long x
;
1770 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1771 execute_set(memc
, global_pairs
, GLOBAL_COUNT
);
1773 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1775 global_keys
[x
]= global_pairs
[x
].key
;
1776 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1782 uint8_t generate_buffer_data(memcached_st
*memc
)
1787 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1788 generate_data(memc
);
1794 uint8_t mset_data(memcached_st
*memc
)
1796 unsigned long long x
;
1797 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1799 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1801 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1803 global_keys
[x
]= global_pairs
[x
].key
;
1804 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1811 uint8_t get_read(memcached_st
*memc
)
1814 memcached_return rc
;
1818 size_t return_value_length
;
1821 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1823 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
1824 &return_value_length
, &flags
, &rc
);
1826 assert(return_value);
1827 assert(rc == MEMCACHED_SUCCESS);
1829 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
1837 uint8_t mget_read(memcached_st
*memc
)
1839 memcached_return rc
;
1841 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1842 assert(rc
== MEMCACHED_SUCCESS
);
1843 /* Turn this into a help function */
1845 char return_key
[MEMCACHED_MAX_KEY
];
1846 size_t return_key_length
;
1848 size_t return_value_length
;
1851 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1852 &return_value_length
, &flags
, &rc
)))
1854 assert(return_value
);
1855 assert(rc
== MEMCACHED_SUCCESS
);
1863 uint8_t mget_read_result(memcached_st
*memc
)
1865 memcached_return rc
;
1867 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1868 assert(rc
== MEMCACHED_SUCCESS
);
1869 /* Turn this into a help function */
1871 memcached_result_st results_obj
;
1872 memcached_result_st
*results
;
1874 results
= memcached_result_create(memc
, &results_obj
);
1876 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1879 assert(rc
== MEMCACHED_SUCCESS
);
1882 memcached_result_free(&results_obj
);
1888 uint8_t mget_read_function(memcached_st
*memc
)
1890 memcached_return rc
;
1891 unsigned int counter
;
1892 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
1894 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1895 assert(rc
== MEMCACHED_SUCCESS
);
1897 callbacks
[0]= &callback_counter
;
1899 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1904 uint8_t delete_generate(memcached_st
*memc
)
1908 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1910 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1916 uint8_t delete_buffer_generate(memcached_st
*memc
)
1922 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1924 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1926 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1932 uint8_t free_data(memcached_st
*memc
)
1934 pairs_free(global_pairs
);
1939 uint8_t add_host_test1(memcached_st
*memc
)
1942 memcached_return rc
;
1943 char servername
[]= "0.example.com";
1944 memcached_server_st
*servers
;
1946 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1948 assert(1 == memcached_server_list_count(servers
));
1950 for (x
= 2; x
< 20; x
++)
1952 char buffer
[SMALL_STRING_LEN
];
1954 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1955 servers
= memcached_server_list_append(servers
, buffer
, 401,
1957 assert(rc
== MEMCACHED_SUCCESS
);
1958 assert(x
== memcached_server_list_count(servers
));
1961 rc
= memcached_server_push(memc
, servers
);
1962 assert(rc
== MEMCACHED_SUCCESS
);
1963 rc
= memcached_server_push(memc
, servers
);
1964 assert(rc
== MEMCACHED_SUCCESS
);
1966 memcached_server_list_free(servers
);
1971 memcached_return
pre_nonblock(memcached_st
*memc
)
1973 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1975 return MEMCACHED_SUCCESS
;
1978 memcached_return
pre_md5(memcached_st
*memc
)
1980 memcached_hash value
= MEMCACHED_HASH_MD5
;
1981 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1983 return MEMCACHED_SUCCESS
;
1986 memcached_return
pre_crc(memcached_st
*memc
)
1988 memcached_hash value
= MEMCACHED_HASH_CRC
;
1989 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1991 return MEMCACHED_SUCCESS
;
1994 memcached_return
pre_hsieh(memcached_st
*memc
)
1996 memcached_hash value
= MEMCACHED_HASH_HSIEH
;
1997 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1999 return MEMCACHED_SUCCESS
;
2002 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2004 memcached_hash value
= MEMCACHED_HASH_FNV1_64
;
2005 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2007 return MEMCACHED_SUCCESS
;
2010 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2012 memcached_hash value
= MEMCACHED_HASH_FNV1A_64
;
2013 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2015 return MEMCACHED_SUCCESS
;
2018 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2020 memcached_hash value
= MEMCACHED_HASH_FNV1_32
;
2021 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2023 return MEMCACHED_SUCCESS
;
2026 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2028 memcached_hash value
= MEMCACHED_HASH_FNV1A_32
;
2029 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2031 return MEMCACHED_SUCCESS
;
2034 memcached_return
pre_hash_ketama(memcached_st
*memc
)
2036 memcached_hash value
= MEMCACHED_HASH_KETAMA
;
2037 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2039 return MEMCACHED_SUCCESS
;
2042 void my_free(memcached_st
*ptr
, void *mem
)
2047 void *my_malloc(memcached_st
*ptr
, const size_t size
)
2049 return malloc(size
);
2052 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2054 return realloc(mem
, size
);
2057 memcached_return
set_memory_alloc(memcached_st
*memc
)
2060 memcached_malloc_function test_ptr
;
2061 memcached_return rc
;
2063 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2064 assert(rc
== MEMCACHED_SUCCESS
);
2065 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2066 assert(rc
== MEMCACHED_SUCCESS
);
2067 assert(test_ptr
== my_malloc
);
2071 memcached_realloc_function test_ptr
;
2072 memcached_return rc
;
2074 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2075 assert(rc
== MEMCACHED_SUCCESS
);
2076 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2077 assert(rc
== MEMCACHED_SUCCESS
);
2078 assert(test_ptr
== my_realloc
);
2082 memcached_free_function test_ptr
;
2083 memcached_return rc
;
2085 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2086 assert(rc
== MEMCACHED_SUCCESS
);
2087 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2088 assert(rc
== MEMCACHED_SUCCESS
);
2089 assert(test_ptr
== my_free
);
2092 return MEMCACHED_SUCCESS
;
2095 memcached_return
enable_consistent(memcached_st
*memc
)
2097 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2098 memcached_hash hash
;
2099 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, &value
);
2102 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2103 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2105 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2106 assert(hash
== MEMCACHED_HASH_HSIEH
);
2109 return MEMCACHED_SUCCESS
;
2112 memcached_return
enable_cas(memcached_st
*memc
)
2114 unsigned int set
= 1;
2116 memcached_version(memc
);
2118 if (memc
->hosts
[0].major_version
>= 1 &&
2119 memc
->hosts
[0].minor_version
>= 2 &&
2120 memc
->hosts
[0].micro_version
>= 4)
2122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
2124 return MEMCACHED_SUCCESS
;
2127 return MEMCACHED_FAILURE
;
2130 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2132 memcached_version(memc
);
2134 if (memc
->hosts
[0].major_version
>= 1 &&
2135 memc
->hosts
[0].minor_version
>= 2 &&
2136 memc
->hosts
[0].micro_version
>= 4)
2137 return MEMCACHED_SUCCESS
;
2139 return MEMCACHED_FAILURE
;
2142 memcached_return
pre_unix_socket(memcached_st
*memc
)
2144 memcached_return rc
;
2147 memcached_server_list_free(memc
->hosts
);
2149 memc
->number_of_hosts
= 0;
2151 if (stat("/tmp/memcached.socket", &buf
))
2152 return MEMCACHED_FAILURE
;
2154 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2159 memcached_return
pre_udp(memcached_st
*memc
)
2161 memcached_return rc
;
2163 memcached_server_list_free(memc
->hosts
);
2165 memc
->number_of_hosts
= 0;
2168 return MEMCACHED_FAILURE
;
2170 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2175 memcached_return
pre_nodelay(memcached_st
*memc
)
2177 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
2178 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
2180 return MEMCACHED_SUCCESS
;
2183 memcached_return
poll_timeout(memcached_st
*memc
)
2189 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
2191 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2193 assert(timeout
== 100);
2195 return MEMCACHED_SUCCESS
;
2199 /* Clean the server before beginning testing */
2201 {"flush", 0, flush_test
},
2202 {"init", 0, init_test
},
2203 {"allocation", 0, allocation_test
},
2204 {"server_list_null_test", 0, server_list_null_test
},
2205 {"clone_test", 0, clone_test
},
2206 {"error", 0, error_test
},
2207 {"set", 0, set_test
},
2208 {"set2", 0, set_test2
},
2209 {"set3", 0, set_test3
},
2210 {"add", 1, add_test
},
2211 {"replace", 0, replace_test
},
2212 {"delete", 1, delete_test
},
2213 {"get", 1, get_test
},
2214 {"get2", 0, get_test2
},
2215 {"get3", 0, get_test3
},
2216 {"get4", 0, get_test4
},
2217 {"stats_servername", 0, stats_servername_test
},
2218 {"increment", 0, increment_test
},
2219 {"decrement", 0, decrement_test
},
2220 {"quit", 0, quit_test
},
2221 {"mget", 1, mget_test
},
2222 {"mget_result", 1, mget_result_test
},
2223 {"mget_result_alloc", 1, mget_result_alloc_test
},
2224 {"mget_result_function", 1, mget_result_function
},
2225 {"get_stats", 0, get_stats
},
2226 {"add_host_test", 0, add_host_test
},
2227 {"get_stats_keys", 0, get_stats_keys
},
2228 {"behavior_test", 0, get_stats_keys
},
2229 {"callback_test", 0, get_stats_keys
},
2230 {"version_string_test", 0, version_string_test
},
2234 test_st async_tests
[] ={
2235 {"add", 1, add_wrapper
},
2239 test_st string_tests
[] ={
2240 {"string static with null", 0, string_static_null
},
2241 {"string alloc with null", 0, string_alloc_null
},
2242 {"string alloc with 1K", 0, string_alloc_with_size
},
2243 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2244 {"string append", 0, string_alloc_append
},
2245 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2249 test_st result_tests
[] ={
2250 {"result static", 0, result_static
},
2251 {"result alloc", 0, result_alloc
},
2255 test_st version_1_2_3
[] ={
2256 {"append", 0, append_test
},
2257 {"prepend", 0, prepend_test
},
2258 {"cas", 0, cas_test
},
2259 {"cas2", 0, cas2_test
},
2260 {"append_binary", 0, append_binary_test
},
2264 test_st user_tests
[] ={
2265 {"user_supplied_bug1", 0, user_supplied_bug1
},
2266 {"user_supplied_bug2", 0, user_supplied_bug2
},
2267 {"user_supplied_bug3", 0, user_supplied_bug3
},
2268 {"user_supplied_bug4", 0, user_supplied_bug4
},
2269 {"user_supplied_bug5", 1, user_supplied_bug5
},
2270 {"user_supplied_bug6", 1, user_supplied_bug6
},
2271 {"user_supplied_bug7", 1, user_supplied_bug7
},
2272 {"user_supplied_bug8", 1, user_supplied_bug8
},
2273 {"user_supplied_bug9", 1, user_supplied_bug9
},
2274 {"user_supplied_bug10", 1, user_supplied_bug10
},
2275 {"user_supplied_bug11", 1, user_supplied_bug11
},
2276 {"user_supplied_bug12", 1, user_supplied_bug12
},
2280 test_st generate_tests
[] ={
2281 {"generate_data", 1, generate_data
},
2282 {"get_read", 0, get_read
},
2283 {"delete_generate", 0, delete_generate
},
2284 {"generate_buffer_data", 1, generate_buffer_data
},
2285 {"delete_buffer", 0, delete_buffer_generate
},
2286 {"generate_data", 1, generate_data
},
2287 {"mget_read", 0, mget_read
},
2288 {"mget_read_result", 0, mget_read_result
},
2289 {"mget_read_function", 0, mget_read_function
},
2290 {"cleanup", 1, cleanup_pairs
},
2295 collection_st collection
[] ={
2296 {"block", 0, 0, tests
},
2297 {"nonblock", pre_nonblock
, 0, tests
},
2298 {"nodelay", pre_nodelay
, 0, tests
},
2299 {"md5", pre_md5
, 0, tests
},
2300 {"crc", pre_crc
, 0, tests
},
2301 {"hsieh", pre_hsieh
, 0, tests
},
2302 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2303 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2304 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2305 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2306 {"ketama", pre_hash_ketama
, 0, tests
},
2307 {"unix_socket", pre_unix_socket
, 0, tests
},
2308 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2309 {"poll_timeout", poll_timeout
, 0, tests
},
2310 {"gets", enable_cas
, 0, tests
},
2311 {"consistent", enable_consistent
, 0, tests
},
2312 {"memory_allocators", set_memory_alloc
, 0, tests
},
2313 // {"udp", pre_udp, 0, tests},
2314 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2315 {"string", 0, 0, string_tests
},
2316 {"result", 0, 0, result_tests
},
2317 {"async", pre_nonblock
, 0, async_tests
},
2318 {"user", 0, 0, user_tests
},
2319 {"generate", 0, 0, generate_tests
},
2320 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2321 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2322 {"generate_md5", pre_md5
, 0, generate_tests
},
2323 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2327 collection_st
*gets_collections(void)