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
894 const char *version_string
;
895 version_string
= memcached_lib_version();
896 assert(!strcmp(version_string
, MEMCACHE_VERSION_STRING
));
900 uint8_t get_stats(memcached_st
*memc
)
906 memcached_stat_st
*stat
;
908 stat
= memcached_stat(memc
, NULL
, &rc
);
909 assert(rc
== MEMCACHED_SUCCESS
);
911 assert(rc
== MEMCACHED_SUCCESS
);
914 for (x
= 0; x
< memcached_server_count(memc
); x
++)
916 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
917 assert(rc
== MEMCACHED_SUCCESS
);
918 for (ptr
= list
; *ptr
; ptr
++);
923 memcached_stat_free(NULL
, stat
);
928 uint8_t add_host_test(memcached_st
*memc
)
931 memcached_server_st
*servers
;
933 char servername
[]= "0.example.com";
935 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
937 assert(1 == memcached_server_list_count(servers
));
939 for (x
= 2; x
< 20; x
++)
941 char buffer
[SMALL_STRING_LEN
];
943 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
944 servers
= memcached_server_list_append(servers
, buffer
, 401,
946 assert(rc
== MEMCACHED_SUCCESS
);
947 assert(x
== memcached_server_list_count(servers
));
950 rc
= memcached_server_push(memc
, servers
);
951 assert(rc
== MEMCACHED_SUCCESS
);
952 rc
= memcached_server_push(memc
, servers
);
953 assert(rc
== MEMCACHED_SUCCESS
);
955 memcached_server_list_free(servers
);
960 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
962 return MEMCACHED_SUCCESS
;
965 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
967 return MEMCACHED_SUCCESS
;
970 uint8_t callback_test(memcached_st
*memc
)
978 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
979 assert(rc
== MEMCACHED_SUCCESS
);
980 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
981 assert(*test_ptr
== x
);
984 /* Test Clone Callback */
986 clone_func temp_function
;
989 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
990 assert(rc
== MEMCACHED_SUCCESS
);
991 temp_function
= (clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
992 assert(temp_function
== clone_test_callback
);
995 /* Test Cleanup Callback */
997 cleanup_func temp_function
;
1000 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1001 assert(rc
== MEMCACHED_SUCCESS
);
1002 temp_function
= (cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1003 assert(temp_function
== cleanup_test_callback
);
1009 /* We don't test the behavior itself, we test the switches */
1010 uint8_t behavior_test(memcached_st
*memc
)
1012 unsigned long long value
;
1013 unsigned int set
= 1;
1015 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1016 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1019 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1020 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1023 set
= MEMCACHED_HASH_MD5
;
1024 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1025 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1026 assert(value
== MEMCACHED_HASH_MD5
);
1030 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1031 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1034 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1035 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1038 set
= MEMCACHED_HASH_DEFAULT
;
1039 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1040 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1041 assert(value
== MEMCACHED_HASH_DEFAULT
);
1043 set
= MEMCACHED_HASH_CRC
;
1044 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1045 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1046 assert(value
== MEMCACHED_HASH_CRC
);
1048 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1051 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1058 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USER_DATA
, &x
);
1059 test_ptr
= (int *)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_USER_DATA
);
1060 assert(*test_ptr
== x
);
1066 /* Test case provided by Cal Haldenbrand */
1067 uint8_t user_supplied_bug1(memcached_st
*memc
)
1069 unsigned int setter
= 1;
1072 unsigned long long total
= 0;
1075 char randomstuff
[6 * 1024];
1076 memcached_return rc
;
1078 memset(randomstuff
, 0, 6 * 1024);
1080 /* We just keep looking at the same values over and over */
1083 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1084 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1088 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1092 size
= (rand() % ( 5 * 1024 ) ) + 400;
1093 memset(randomstuff
, 0, 6 * 1024);
1094 assert(size
< 6 * 1024); /* Being safe here */
1096 for (j
= 0 ; j
< size
;j
++)
1097 randomstuff
[j
] = (char) (rand() % 26) + 97;
1100 sprintf(key
, "%d", x
);
1101 rc
= memcached_set(memc
, key
, strlen(key
),
1102 randomstuff
, strlen(randomstuff
), 10, 0);
1103 /* If we fail, lets try again */
1104 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1105 rc
= memcached_set(memc
, key
, strlen(key
),
1106 randomstuff
, strlen(randomstuff
), 10, 0);
1107 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1113 /* Test case provided by Cal Haldenbrand */
1114 uint8_t user_supplied_bug2(memcached_st
*memc
)
1117 unsigned int setter
;
1119 unsigned long long total
;
1122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1123 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1125 setter
= 20 * 1024576;
1126 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
1127 setter
= 20 * 1024576;
1128 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
1129 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1130 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1132 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1135 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1137 memcached_return rc
= MEMCACHED_SUCCESS
;
1138 char buffer
[SMALL_STRING_LEN
];
1143 memset(buffer
, 0, SMALL_STRING_LEN
);
1145 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1146 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1147 &val_len
, &flags
, &rc
);
1148 if (rc
!= MEMCACHED_SUCCESS
)
1150 if (rc
== MEMCACHED_NOTFOUND
)
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
);
1611 uint8_t result_static(memcached_st
*memc
)
1613 memcached_result_st result
;
1614 memcached_result_st
*result_ptr
;
1616 result_ptr
= memcached_result_create(memc
, &result
);
1617 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1619 memcached_result_free(&result
);
1624 uint8_t result_alloc(memcached_st
*memc
)
1626 memcached_result_st
*result
;
1628 result
= memcached_result_create(memc
, NULL
);
1630 memcached_result_free(result
);
1635 uint8_t string_static_null(memcached_st
*memc
)
1637 memcached_string_st string
;
1638 memcached_string_st
*string_ptr
;
1640 string_ptr
= memcached_string_create(memc
, &string
, 0);
1641 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1643 memcached_string_free(&string
);
1648 uint8_t string_alloc_null(memcached_st
*memc
)
1650 memcached_string_st
*string
;
1652 string
= memcached_string_create(memc
, NULL
, 0);
1654 memcached_string_free(string
);
1659 uint8_t string_alloc_with_size(memcached_st
*memc
)
1661 memcached_string_st
*string
;
1663 string
= memcached_string_create(memc
, NULL
, 1024);
1665 memcached_string_free(string
);
1670 uint8_t string_alloc_with_size_toobig(memcached_st
*memc
)
1672 memcached_string_st
*string
;
1674 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
1675 assert(string
== NULL
);
1680 uint8_t string_alloc_append(memcached_st
*memc
)
1683 char buffer
[SMALL_STRING_LEN
];
1684 memcached_string_st
*string
;
1686 /* Ring the bell! */
1687 memset(buffer
, 6, SMALL_STRING_LEN
);
1689 string
= memcached_string_create(memc
, NULL
, 100);
1692 for (x
= 0; x
< 1024; x
++)
1694 memcached_return rc
;
1695 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1696 assert(rc
== MEMCACHED_SUCCESS
);
1698 memcached_string_free(string
);
1703 uint8_t string_alloc_append_toobig(memcached_st
*memc
)
1705 memcached_return rc
;
1707 char buffer
[SMALL_STRING_LEN
];
1708 memcached_string_st
*string
;
1710 /* Ring the bell! */
1711 memset(buffer
, 6, SMALL_STRING_LEN
);
1713 string
= memcached_string_create(memc
, NULL
, 100);
1716 for (x
= 0; x
< 1024; x
++)
1718 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1719 assert(rc
== MEMCACHED_SUCCESS
);
1721 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
1722 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
1723 memcached_string_free(string
);
1728 uint8_t cleanup_pairs(memcached_st
*memc
)
1730 pairs_free(global_pairs
);
1735 uint8_t generate_data(memcached_st
*memc
)
1737 unsigned long long x
;
1738 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1739 execute_set(memc
, global_pairs
, GLOBAL_COUNT
);
1741 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1743 global_keys
[x
]= global_pairs
[x
].key
;
1744 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1750 uint8_t generate_buffer_data(memcached_st
*memc
)
1755 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1756 generate_data(memc
);
1762 uint8_t mset_data(memcached_st
*memc
)
1764 unsigned long long x
;
1765 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1767 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1769 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1771 global_keys
[x
]= global_pairs
[x
].key
;
1772 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1779 uint8_t get_read(memcached_st
*memc
)
1782 memcached_return rc
;
1786 size_t return_value_length
;
1789 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1791 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
1792 &return_value_length
, &flags
, &rc
);
1794 assert(return_value);
1795 assert(rc == MEMCACHED_SUCCESS);
1797 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
1805 uint8_t mget_read(memcached_st
*memc
)
1807 memcached_return rc
;
1809 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1810 assert(rc
== MEMCACHED_SUCCESS
);
1811 /* Turn this into a help function */
1813 char return_key
[MEMCACHED_MAX_KEY
];
1814 size_t return_key_length
;
1816 size_t return_value_length
;
1819 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1820 &return_value_length
, &flags
, &rc
)))
1822 assert(return_value
);
1823 assert(rc
== MEMCACHED_SUCCESS
);
1831 uint8_t mget_read_result(memcached_st
*memc
)
1833 memcached_return rc
;
1835 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1836 assert(rc
== MEMCACHED_SUCCESS
);
1837 /* Turn this into a help function */
1839 memcached_result_st results_obj
;
1840 memcached_result_st
*results
;
1842 results
= memcached_result_create(memc
, &results_obj
);
1844 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1847 assert(rc
== MEMCACHED_SUCCESS
);
1850 memcached_result_free(&results_obj
);
1856 uint8_t mget_read_function(memcached_st
*memc
)
1858 memcached_return rc
;
1859 unsigned int counter
;
1860 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
1862 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1863 assert(rc
== MEMCACHED_SUCCESS
);
1865 callbacks
[0]= &callback_counter
;
1867 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1872 uint8_t delete_generate(memcached_st
*memc
)
1876 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1878 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1884 uint8_t delete_buffer_generate(memcached_st
*memc
)
1890 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1892 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1894 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1900 uint8_t free_data(memcached_st
*memc
)
1902 pairs_free(global_pairs
);
1907 uint8_t add_host_test1(memcached_st
*memc
)
1910 memcached_return rc
;
1911 char servername
[]= "0.example.com";
1912 memcached_server_st
*servers
;
1914 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1916 assert(1 == memcached_server_list_count(servers
));
1918 for (x
= 2; x
< 20; x
++)
1920 char buffer
[SMALL_STRING_LEN
];
1922 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1923 servers
= memcached_server_list_append(servers
, buffer
, 401,
1925 assert(rc
== MEMCACHED_SUCCESS
);
1926 assert(x
== memcached_server_list_count(servers
));
1929 rc
= memcached_server_push(memc
, servers
);
1930 assert(rc
== MEMCACHED_SUCCESS
);
1931 rc
= memcached_server_push(memc
, servers
);
1932 assert(rc
== MEMCACHED_SUCCESS
);
1934 memcached_server_list_free(servers
);
1939 memcached_return
pre_nonblock(memcached_st
*memc
)
1941 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1943 return MEMCACHED_SUCCESS
;
1946 memcached_return
pre_md5(memcached_st
*memc
)
1948 memcached_hash value
= MEMCACHED_HASH_MD5
;
1949 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1951 return MEMCACHED_SUCCESS
;
1954 memcached_return
pre_crc(memcached_st
*memc
)
1956 memcached_hash value
= MEMCACHED_HASH_CRC
;
1957 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1959 return MEMCACHED_SUCCESS
;
1962 memcached_return
pre_hsieh(memcached_st
*memc
)
1964 memcached_hash value
= MEMCACHED_HASH_HSIEH
;
1965 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1967 return MEMCACHED_SUCCESS
;
1970 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
1972 memcached_hash value
= MEMCACHED_HASH_FNV1_64
;
1973 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1975 return MEMCACHED_SUCCESS
;
1978 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
1980 memcached_hash value
= MEMCACHED_HASH_FNV1A_64
;
1981 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1983 return MEMCACHED_SUCCESS
;
1986 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
1988 memcached_hash value
= MEMCACHED_HASH_FNV1_32
;
1989 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1991 return MEMCACHED_SUCCESS
;
1994 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
1996 memcached_hash value
= MEMCACHED_HASH_FNV1A_32
;
1997 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1999 return MEMCACHED_SUCCESS
;
2002 memcached_return
pre_hash_ketama(memcached_st
*memc
)
2004 memcached_hash value
= MEMCACHED_HASH_KETAMA
;
2005 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
2007 return MEMCACHED_SUCCESS
;
2010 memcached_return
enable_consistent(memcached_st
*memc
)
2012 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2013 memcached_hash hash
;
2014 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, &value
);
2017 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2018 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2020 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2021 assert(hash
== MEMCACHED_HASH_HSIEH
);
2024 return MEMCACHED_SUCCESS
;
2027 memcached_return
enable_cas(memcached_st
*memc
)
2029 unsigned int set
= 1;
2031 memcached_version(memc
);
2033 if (memc
->hosts
[0].major_version
>= 1 &&
2034 memc
->hosts
[0].minor_version
>= 2 &&
2035 memc
->hosts
[0].micro_version
>= 4)
2037 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
2039 return MEMCACHED_SUCCESS
;
2042 return MEMCACHED_FAILURE
;
2045 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2047 memcached_version(memc
);
2049 if (memc
->hosts
[0].major_version
>= 1 &&
2050 memc
->hosts
[0].minor_version
>= 2 &&
2051 memc
->hosts
[0].micro_version
>= 4)
2052 return MEMCACHED_SUCCESS
;
2054 return MEMCACHED_FAILURE
;
2057 memcached_return
pre_unix_socket(memcached_st
*memc
)
2059 memcached_return rc
;
2062 memcached_server_list_free(memc
->hosts
);
2064 memc
->number_of_hosts
= 0;
2066 if (stat("/tmp/memcached.socket", &buf
))
2067 return MEMCACHED_FAILURE
;
2069 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2074 memcached_return
pre_udp(memcached_st
*memc
)
2076 memcached_return rc
;
2078 memcached_server_list_free(memc
->hosts
);
2080 memc
->number_of_hosts
= 0;
2083 return MEMCACHED_FAILURE
;
2085 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2090 memcached_return
pre_nodelay(memcached_st
*memc
)
2092 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
2093 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
2095 return MEMCACHED_SUCCESS
;
2098 memcached_return
poll_timeout(memcached_st
*memc
)
2104 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
2106 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2108 assert(timeout
== 100);
2110 return MEMCACHED_SUCCESS
;
2114 /* Clean the server before beginning testing */
2116 {"flush", 0, flush_test
},
2117 {"init", 0, init_test
},
2118 {"allocation", 0, allocation_test
},
2119 {"server_list_null_test", 0, server_list_null_test
},
2120 {"clone_test", 0, clone_test
},
2121 {"error", 0, error_test
},
2122 {"set", 0, set_test
},
2123 {"set2", 0, set_test2
},
2124 {"set3", 0, set_test3
},
2125 {"add", 1, add_test
},
2126 {"replace", 0, replace_test
},
2127 {"delete", 1, delete_test
},
2128 {"get", 1, get_test
},
2129 {"get2", 0, get_test2
},
2130 {"get3", 0, get_test3
},
2131 {"get4", 0, get_test4
},
2132 {"stats_servername", 0, stats_servername_test
},
2133 {"increment", 0, increment_test
},
2134 {"decrement", 0, decrement_test
},
2135 {"quit", 0, quit_test
},
2136 {"mget", 1, mget_test
},
2137 {"mget_result", 1, mget_result_test
},
2138 {"mget_result_alloc", 1, mget_result_alloc_test
},
2139 {"mget_result_function", 1, mget_result_function
},
2140 {"get_stats", 0, get_stats
},
2141 {"add_host_test", 0, add_host_test
},
2142 {"get_stats_keys", 0, get_stats_keys
},
2143 {"behavior_test", 0, get_stats_keys
},
2144 {"callback_test", 0, get_stats_keys
},
2145 {"version_string_test", 0, version_string_test
},
2149 test_st async_tests
[] ={
2150 {"add", 1, add_wrapper
},
2154 test_st string_tests
[] ={
2155 {"string static with null", 0, string_static_null
},
2156 {"string alloc with null", 0, string_alloc_null
},
2157 {"string alloc with 1K", 0, string_alloc_with_size
},
2158 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2159 {"string append", 0, string_alloc_append
},
2160 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2164 test_st result_tests
[] ={
2165 {"result static", 0, result_static
},
2166 {"result alloc", 0, result_alloc
},
2170 test_st version_1_2_3
[] ={
2171 {"append", 0, append_test
},
2172 {"prepend", 0, prepend_test
},
2173 {"cas", 0, cas_test
},
2174 {"cas2", 0, cas2_test
},
2175 {"append_binary", 0, append_binary_test
},
2179 test_st user_tests
[] ={
2180 {"user_supplied_bug1", 0, user_supplied_bug1
},
2181 {"user_supplied_bug2", 0, user_supplied_bug2
},
2182 {"user_supplied_bug3", 0, user_supplied_bug3
},
2183 {"user_supplied_bug4", 0, user_supplied_bug4
},
2184 {"user_supplied_bug5", 1, user_supplied_bug5
},
2185 {"user_supplied_bug6", 1, user_supplied_bug6
},
2186 {"user_supplied_bug7", 1, user_supplied_bug7
},
2187 {"user_supplied_bug8", 1, user_supplied_bug8
},
2188 {"user_supplied_bug9", 1, user_supplied_bug9
},
2189 {"user_supplied_bug10", 1, user_supplied_bug10
},
2190 {"user_supplied_bug11", 1, user_supplied_bug11
},
2194 test_st generate_tests
[] ={
2195 {"generate_data", 1, generate_data
},
2196 {"get_read", 0, get_read
},
2197 {"delete_generate", 0, delete_generate
},
2198 {"generate_buffer_data", 1, generate_buffer_data
},
2199 {"delete_buffer", 0, delete_buffer_generate
},
2200 {"generate_data", 1, generate_data
},
2201 {"mget_read", 0, mget_read
},
2202 {"mget_read_result", 0, mget_read_result
},
2203 {"mget_read_function", 0, mget_read_function
},
2204 {"cleanup", 1, cleanup_pairs
},
2209 collection_st collection
[] ={
2210 {"block", 0, 0, tests
},
2211 {"nonblock", pre_nonblock
, 0, tests
},
2212 {"nodelay", pre_nodelay
, 0, tests
},
2213 {"md5", pre_md5
, 0, tests
},
2214 {"crc", pre_crc
, 0, tests
},
2215 {"hsieh", pre_hsieh
, 0, tests
},
2216 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2217 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2218 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2219 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2220 {"ketama", pre_hash_ketama
, 0, tests
},
2221 {"unix_socket", pre_unix_socket
, 0, tests
},
2222 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2223 {"poll_timeout", poll_timeout
, 0, tests
},
2224 {"gets", enable_cas
, 0, tests
},
2225 {"consistent", enable_consistent
, 0, tests
},
2226 // {"udp", pre_udp, 0, tests},
2227 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2228 {"string", 0, 0, string_tests
},
2229 {"result", 0, 0, result_tests
},
2230 {"async", pre_nonblock
, 0, async_tests
},
2231 {"user", 0, 0, user_tests
},
2232 {"generate", 0, 0, generate_tests
},
2233 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2234 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2235 {"generate_md5", pre_md5
, 0, generate_tests
},
2236 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2240 collection_st
*gets_collections(void)