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 get_stats(memcached_st
*memc
)
898 memcached_stat_st
*stat
;
900 stat
= memcached_stat(memc
, NULL
, &rc
);
901 assert(rc
== MEMCACHED_SUCCESS
);
903 assert(rc
== MEMCACHED_SUCCESS
);
906 for (x
= 0; x
< memcached_server_count(memc
); x
++)
908 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
909 assert(rc
== MEMCACHED_SUCCESS
);
910 for (ptr
= list
; *ptr
; ptr
++);
915 memcached_stat_free(NULL
, stat
);
920 uint8_t add_host_test(memcached_st
*memc
)
923 memcached_server_st
*servers
;
925 char servername
[]= "0.example.com";
927 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
929 assert(1 == memcached_server_list_count(servers
));
931 for (x
= 2; x
< 20; x
++)
933 char buffer
[SMALL_STRING_LEN
];
935 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
936 servers
= memcached_server_list_append(servers
, buffer
, 401,
938 assert(rc
== MEMCACHED_SUCCESS
);
939 assert(x
== memcached_server_list_count(servers
));
942 rc
= memcached_server_push(memc
, servers
);
943 assert(rc
== MEMCACHED_SUCCESS
);
944 rc
= memcached_server_push(memc
, servers
);
945 assert(rc
== MEMCACHED_SUCCESS
);
947 memcached_server_list_free(servers
);
952 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
954 return MEMCACHED_SUCCESS
;
957 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
959 return MEMCACHED_SUCCESS
;
962 uint8_t callback_test(memcached_st
*memc
)
970 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
971 assert(rc
== MEMCACHED_SUCCESS
);
972 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
973 assert(*test_ptr
== x
);
976 /* Test Clone Callback */
978 memcached_clone_func temp_function
;
981 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
982 assert(rc
== MEMCACHED_SUCCESS
);
983 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
984 assert(temp_function
== clone_test_callback
);
987 /* Test Cleanup Callback */
989 memcached_cleanup_func temp_function
;
992 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
993 assert(rc
== MEMCACHED_SUCCESS
);
994 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
995 assert(temp_function
== cleanup_test_callback
);
1001 /* We don't test the behavior itself, we test the switches */
1002 uint8_t behavior_test(memcached_st
*memc
)
1004 unsigned long long value
;
1005 unsigned int set
= 1;
1007 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1008 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1011 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1012 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1015 set
= MEMCACHED_HASH_MD5
;
1016 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1017 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1018 assert(value
== MEMCACHED_HASH_MD5
);
1022 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1023 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1026 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1027 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1030 set
= MEMCACHED_HASH_DEFAULT
;
1031 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1032 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1033 assert(value
== MEMCACHED_HASH_DEFAULT
);
1035 set
= MEMCACHED_HASH_CRC
;
1036 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
1037 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1038 assert(value
== MEMCACHED_HASH_CRC
);
1040 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1043 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1049 /* Test case provided by Cal Haldenbrand */
1050 uint8_t user_supplied_bug1(memcached_st
*memc
)
1052 unsigned int setter
= 1;
1055 unsigned long long total
= 0;
1058 char randomstuff
[6 * 1024];
1059 memcached_return rc
;
1061 memset(randomstuff
, 0, 6 * 1024);
1063 /* We just keep looking at the same values over and over */
1066 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1067 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1071 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1075 size
= (rand() % ( 5 * 1024 ) ) + 400;
1076 memset(randomstuff
, 0, 6 * 1024);
1077 assert(size
< 6 * 1024); /* Being safe here */
1079 for (j
= 0 ; j
< size
;j
++)
1080 randomstuff
[j
] = (char) (rand() % 26) + 97;
1083 sprintf(key
, "%d", x
);
1084 rc
= memcached_set(memc
, key
, strlen(key
),
1085 randomstuff
, strlen(randomstuff
), 10, 0);
1086 /* If we fail, lets try again */
1087 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1088 rc
= memcached_set(memc
, key
, strlen(key
),
1089 randomstuff
, strlen(randomstuff
), 10, 0);
1090 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1096 /* Test case provided by Cal Haldenbrand */
1097 uint8_t user_supplied_bug2(memcached_st
*memc
)
1100 unsigned int setter
;
1102 unsigned long long total
;
1105 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1106 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1108 setter
= 20 * 1024576;
1109 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
1110 setter
= 20 * 1024576;
1111 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
1112 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1113 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1115 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1118 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1120 memcached_return rc
= MEMCACHED_SUCCESS
;
1121 char buffer
[SMALL_STRING_LEN
];
1126 memset(buffer
, 0, SMALL_STRING_LEN
);
1128 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1129 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1130 &val_len
, &flags
, &rc
);
1131 if (rc
!= MEMCACHED_SUCCESS
)
1133 if (rc
== MEMCACHED_NOTFOUND
)
1148 /* Do a large mget() over all the keys we think exist */
1149 #define KEY_COUNT 3000 // * 1024576
1150 uint8_t user_supplied_bug3(memcached_st
*memc
)
1152 memcached_return rc
;
1153 unsigned int setter
;
1156 size_t key_lengths
[KEY_COUNT
];
1159 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
1160 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
1162 setter
= 20 * 1024576;
1163 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
1164 setter
= 20 * 1024576;
1165 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
1166 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1167 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1170 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1172 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1173 for (x
= 0; x
< KEY_COUNT
; x
++)
1177 snprintf(buffer
, 30, "%u", x
);
1178 keys
[x
]= strdup(buffer
);
1179 key_lengths
[x
]= strlen(keys
[x
]);
1182 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1183 assert(rc
== MEMCACHED_SUCCESS
);
1185 /* Turn this into a help function */
1187 char return_key
[MEMCACHED_MAX_KEY
];
1188 size_t return_key_length
;
1190 size_t return_value_length
;
1193 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1194 &return_value_length
, &flags
, &rc
)))
1196 assert(return_value
);
1197 assert(rc
== MEMCACHED_SUCCESS
);
1202 for (x
= 0; x
< KEY_COUNT
; x
++)
1209 /* Make sure we behave properly if server list has no values */
1210 uint8_t user_supplied_bug4(memcached_st
*memc
)
1212 memcached_return rc
;
1213 char *keys
[]= {"fudge", "son", "food"};
1214 size_t key_length
[]= {5, 3, 4};
1217 char return_key
[MEMCACHED_MAX_KEY
];
1218 size_t return_key_length
;
1220 size_t return_value_length
;
1222 /* Here we free everything before running a bunch of mget tests */
1224 memcached_server_list_free(memc
->hosts
);
1226 memc
->number_of_hosts
= 0;
1230 /* We need to empty the server before continueing test */
1231 rc
= memcached_flush(memc
, 0);
1232 assert(rc
== MEMCACHED_NO_SERVERS
);
1234 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1235 assert(rc
== MEMCACHED_NO_SERVERS
);
1237 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1238 &return_value_length
, &flags
, &rc
)) != NULL
)
1240 assert(return_value
);
1242 assert(!return_value
);
1243 assert(return_value_length
== 0);
1244 assert(rc
== MEMCACHED_NO_SERVERS
);
1246 for (x
= 0; x
< 3; x
++)
1248 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1249 keys
[x
], key_length
[x
],
1250 (time_t)50, (uint32_t)9);
1251 assert(rc
== MEMCACHED_NO_SERVERS
);
1254 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1255 assert(rc
== MEMCACHED_NO_SERVERS
);
1258 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1259 &return_value_length
, &flags
, &rc
)))
1261 assert(return_value
);
1262 assert(rc
== MEMCACHED_SUCCESS
);
1263 assert(return_key_length
== return_value_length
);
1264 assert(!memcmp(return_value
, return_key
, return_value_length
));
1272 #define VALUE_SIZE_BUG5 1048064
1273 uint8_t user_supplied_bug5(memcached_st
*memc
)
1275 memcached_return rc
;
1276 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1277 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1278 char return_key
[MEMCACHED_MAX_KEY
];
1279 size_t return_key_length
;
1281 size_t value_length
;
1285 char insert_data
[VALUE_SIZE_BUG5
];
1287 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1288 insert_data
[x
]= rand();
1290 memcached_flush(memc
, 0);
1291 value
= memcached_get(memc
, keys
[0], key_length
[0],
1292 &value_length
, &flags
, &rc
);
1293 assert(value
== NULL
);
1294 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1297 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1298 &value_length
, &flags
, &rc
)))
1302 for (x
= 0; x
< 4; x
++)
1304 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1305 insert_data
, VALUE_SIZE_BUG5
,
1306 (time_t)0, (uint32_t)0);
1307 assert(rc
== MEMCACHED_SUCCESS
);
1310 for (x
= 0; x
< 10; x
++)
1312 value
= memcached_get(memc
, keys
[0], key_length
[0],
1313 &value_length
, &flags
, &rc
);
1317 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1319 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1320 &value_length
, &flags
, &rc
)))
1331 uint8_t user_supplied_bug6(memcached_st
*memc
)
1333 memcached_return rc
;
1334 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1335 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1336 char return_key
[MEMCACHED_MAX_KEY
];
1337 size_t return_key_length
;
1339 size_t value_length
;
1343 char insert_data
[VALUE_SIZE_BUG5
];
1345 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1346 insert_data
[x
]= rand();
1348 memcached_flush(memc
, 0);
1349 value
= memcached_get(memc
, keys
[0], key_length
[0],
1350 &value_length
, &flags
, &rc
);
1351 assert(value
== NULL
);
1352 assert(rc
== MEMCACHED_NOTFOUND
);
1353 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1354 assert(rc
== MEMCACHED_SUCCESS
);
1357 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1358 &value_length
, &flags
, &rc
)))
1361 assert(rc
== MEMCACHED_END
);
1363 for (x
= 0; x
< 4; x
++)
1365 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1366 insert_data
, VALUE_SIZE_BUG5
,
1367 (time_t)0, (uint32_t)0);
1368 assert(rc
== MEMCACHED_SUCCESS
);
1371 for (x
= 0; x
< 2; x
++)
1373 value
= memcached_get(memc
, keys
[0], key_length
[0],
1374 &value_length
, &flags
, &rc
);
1378 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1379 assert(rc
== MEMCACHED_SUCCESS
);
1381 /* We test for purge of partial complete fetches */
1382 for (count
= 3; count
; count
--)
1384 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1385 &value_length
, &flags
, &rc
);
1386 assert(rc
== MEMCACHED_SUCCESS
);
1387 assert(!(memcmp(value
, insert_data
, value_length
)));
1388 assert(value_length
);
1396 uint8_t user_supplied_bug8(memcached_st
*memc
)
1398 memcached_return rc
;
1400 memcached_st
*clone
;
1402 memcached_server_st
*servers
;
1403 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";
1405 servers
= memcached_servers_parse(server_list
);
1408 mine
= memcached_create(NULL
);
1409 rc
= memcached_server_push(mine
, servers
);
1410 assert(rc
== MEMCACHED_SUCCESS
);
1411 memcached_server_list_free(servers
);
1414 clone
= memcached_clone(NULL
, mine
);
1416 memcached_quit(mine
);
1417 memcached_quit(clone
);
1420 memcached_free(mine
);
1421 memcached_free(clone
);
1426 /* Test flag store/retrieve */
1427 uint8_t user_supplied_bug7(memcached_st
*memc
)
1429 memcached_return rc
;
1430 char *keys
= "036790384900";
1431 size_t key_length
= strlen("036790384900");
1432 char return_key
[MEMCACHED_MAX_KEY
];
1433 size_t return_key_length
;
1435 size_t value_length
;
1438 char insert_data
[VALUE_SIZE_BUG5
];
1440 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1441 insert_data
[x
]= rand();
1443 memcached_flush(memc
, 0);
1446 rc
= memcached_set(memc
, keys
, key_length
,
1447 insert_data
, VALUE_SIZE_BUG5
,
1449 assert(rc
== MEMCACHED_SUCCESS
);
1452 value
= memcached_get(memc
, keys
, key_length
,
1453 &value_length
, &flags
, &rc
);
1454 assert(flags
== 245);
1458 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1461 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1462 &value_length
, &flags
, &rc
);
1463 assert(flags
== 245);
1471 uint8_t user_supplied_bug9(memcached_st
*memc
)
1473 memcached_return rc
;
1474 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1475 size_t key_length
[3];
1480 char return_key
[MEMCACHED_MAX_KEY
];
1481 size_t return_key_length
;
1483 size_t return_value_length
;
1486 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1487 key_length
[1]= strlen("fudge&*@#");
1488 key_length
[2]= strlen("for^#@&$not");
1491 for (x
= 0; x
< 3; x
++)
1493 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1494 keys
[x
], key_length
[x
],
1495 (time_t)50, (uint32_t)9);
1496 assert(rc
== MEMCACHED_SUCCESS
);
1499 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1500 assert(rc
== MEMCACHED_SUCCESS
);
1502 /* We need to empty the server before continueing test */
1503 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1504 &return_value_length
, &flags
, &rc
)) != NULL
)
1506 assert(return_value
);
1515 /* We are testing with aggressive timeout to get failures */
1516 uint8_t user_supplied_bug10(memcached_st
*memc
)
1520 size_t value_length
= 512;
1523 memcached_return rc
;
1524 unsigned int set
= 1;
1525 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1528 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1529 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1531 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
1533 value
= (char*)malloc(value_length
* sizeof(char));
1535 for (x
= 0; x
< value_length
; x
++)
1536 value
[x
]= (char) (x
% 127);
1538 for (x
= 1; x
<= 100000; ++x
)
1540 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1542 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1544 if (rc
== MEMCACHED_WRITE_FAILURE
)
1549 memcached_free(mclone
);
1555 We are looking failures in the async protocol
1557 uint8_t user_supplied_bug11(memcached_st
*memc
)
1561 size_t value_length
= 512;
1564 memcached_return rc
;
1565 unsigned int set
= 1;
1567 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1569 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
1570 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
1572 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
1574 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1576 assert(timeout
== -1);
1578 value
= (char*)malloc(value_length
* sizeof(char));
1580 for (x
= 0; x
< value_length
; x
++)
1581 value
[x
]= (char) (x
% 127);
1583 for (x
= 1; x
<= 100000; ++x
)
1585 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1589 memcached_free(mclone
);
1594 uint8_t result_static(memcached_st
*memc
)
1596 memcached_result_st result
;
1597 memcached_result_st
*result_ptr
;
1599 result_ptr
= memcached_result_create(memc
, &result
);
1600 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1602 memcached_result_free(&result
);
1607 uint8_t result_alloc(memcached_st
*memc
)
1609 memcached_result_st
*result
;
1611 result
= memcached_result_create(memc
, NULL
);
1613 memcached_result_free(result
);
1618 uint8_t string_static_null(memcached_st
*memc
)
1620 memcached_string_st string
;
1621 memcached_string_st
*string_ptr
;
1623 string_ptr
= memcached_string_create(memc
, &string
, 0);
1624 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1626 memcached_string_free(&string
);
1631 uint8_t string_alloc_null(memcached_st
*memc
)
1633 memcached_string_st
*string
;
1635 string
= memcached_string_create(memc
, NULL
, 0);
1637 memcached_string_free(string
);
1642 uint8_t string_alloc_with_size(memcached_st
*memc
)
1644 memcached_string_st
*string
;
1646 string
= memcached_string_create(memc
, NULL
, 1024);
1648 memcached_string_free(string
);
1653 uint8_t string_alloc_with_size_toobig(memcached_st
*memc
)
1655 memcached_string_st
*string
;
1657 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
1658 assert(string
== NULL
);
1663 uint8_t string_alloc_append(memcached_st
*memc
)
1666 char buffer
[SMALL_STRING_LEN
];
1667 memcached_string_st
*string
;
1669 /* Ring the bell! */
1670 memset(buffer
, 6, SMALL_STRING_LEN
);
1672 string
= memcached_string_create(memc
, NULL
, 100);
1675 for (x
= 0; x
< 1024; x
++)
1677 memcached_return rc
;
1678 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1679 assert(rc
== MEMCACHED_SUCCESS
);
1681 memcached_string_free(string
);
1686 uint8_t string_alloc_append_toobig(memcached_st
*memc
)
1688 memcached_return rc
;
1690 char buffer
[SMALL_STRING_LEN
];
1691 memcached_string_st
*string
;
1693 /* Ring the bell! */
1694 memset(buffer
, 6, SMALL_STRING_LEN
);
1696 string
= memcached_string_create(memc
, NULL
, 100);
1699 for (x
= 0; x
< 1024; x
++)
1701 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1702 assert(rc
== MEMCACHED_SUCCESS
);
1704 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
1705 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
1706 memcached_string_free(string
);
1711 uint8_t cleanup_pairs(memcached_st
*memc
)
1713 pairs_free(global_pairs
);
1718 uint8_t generate_data(memcached_st
*memc
)
1720 unsigned long long x
;
1721 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1722 execute_set(memc
, global_pairs
, GLOBAL_COUNT
);
1724 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1726 global_keys
[x
]= global_pairs
[x
].key
;
1727 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1733 uint8_t generate_buffer_data(memcached_st
*memc
)
1738 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1739 generate_data(memc
);
1745 uint8_t mset_data(memcached_st
*memc
)
1747 unsigned long long x
;
1748 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1750 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1752 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1754 global_keys
[x
]= global_pairs
[x
].key
;
1755 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1762 uint8_t get_read(memcached_st
*memc
)
1765 memcached_return rc
;
1769 size_t return_value_length
;
1772 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1774 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
1775 &return_value_length
, &flags
, &rc
);
1777 assert(return_value);
1778 assert(rc == MEMCACHED_SUCCESS);
1780 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
1788 uint8_t mget_read(memcached_st
*memc
)
1790 memcached_return rc
;
1792 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1793 assert(rc
== MEMCACHED_SUCCESS
);
1794 /* Turn this into a help function */
1796 char return_key
[MEMCACHED_MAX_KEY
];
1797 size_t return_key_length
;
1799 size_t return_value_length
;
1802 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1803 &return_value_length
, &flags
, &rc
)))
1805 assert(return_value
);
1806 assert(rc
== MEMCACHED_SUCCESS
);
1814 uint8_t mget_read_result(memcached_st
*memc
)
1816 memcached_return rc
;
1818 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1819 assert(rc
== MEMCACHED_SUCCESS
);
1820 /* Turn this into a help function */
1822 memcached_result_st results_obj
;
1823 memcached_result_st
*results
;
1825 results
= memcached_result_create(memc
, &results_obj
);
1827 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1830 assert(rc
== MEMCACHED_SUCCESS
);
1833 memcached_result_free(&results_obj
);
1839 uint8_t mget_read_function(memcached_st
*memc
)
1841 memcached_return rc
;
1842 unsigned int counter
;
1843 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
1845 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1846 assert(rc
== MEMCACHED_SUCCESS
);
1848 callbacks
[0]= &callback_counter
;
1850 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1855 uint8_t delete_generate(memcached_st
*memc
)
1859 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1861 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1867 uint8_t delete_buffer_generate(memcached_st
*memc
)
1873 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, &latch
);
1875 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1877 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
1883 uint8_t free_data(memcached_st
*memc
)
1885 pairs_free(global_pairs
);
1890 uint8_t add_host_test1(memcached_st
*memc
)
1893 memcached_return rc
;
1894 char servername
[]= "0.example.com";
1895 memcached_server_st
*servers
;
1897 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1899 assert(1 == memcached_server_list_count(servers
));
1901 for (x
= 2; x
< 20; x
++)
1903 char buffer
[SMALL_STRING_LEN
];
1905 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1906 servers
= memcached_server_list_append(servers
, buffer
, 401,
1908 assert(rc
== MEMCACHED_SUCCESS
);
1909 assert(x
== memcached_server_list_count(servers
));
1912 rc
= memcached_server_push(memc
, servers
);
1913 assert(rc
== MEMCACHED_SUCCESS
);
1914 rc
= memcached_server_push(memc
, servers
);
1915 assert(rc
== MEMCACHED_SUCCESS
);
1917 memcached_server_list_free(servers
);
1922 memcached_return
pre_nonblock(memcached_st
*memc
)
1924 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1926 return MEMCACHED_SUCCESS
;
1929 memcached_return
pre_md5(memcached_st
*memc
)
1931 memcached_hash value
= MEMCACHED_HASH_MD5
;
1932 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1934 return MEMCACHED_SUCCESS
;
1937 memcached_return
pre_crc(memcached_st
*memc
)
1939 memcached_hash value
= MEMCACHED_HASH_CRC
;
1940 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1942 return MEMCACHED_SUCCESS
;
1945 memcached_return
pre_hsieh(memcached_st
*memc
)
1947 memcached_hash value
= MEMCACHED_HASH_HSIEH
;
1948 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1950 return MEMCACHED_SUCCESS
;
1953 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
1955 memcached_hash value
= MEMCACHED_HASH_FNV1_64
;
1956 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1958 return MEMCACHED_SUCCESS
;
1961 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
1963 memcached_hash value
= MEMCACHED_HASH_FNV1A_64
;
1964 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1966 return MEMCACHED_SUCCESS
;
1969 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
1971 memcached_hash value
= MEMCACHED_HASH_FNV1_32
;
1972 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1974 return MEMCACHED_SUCCESS
;
1977 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
1979 memcached_hash value
= MEMCACHED_HASH_FNV1A_32
;
1980 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1982 return MEMCACHED_SUCCESS
;
1985 memcached_return
pre_hash_ketama(memcached_st
*memc
)
1987 memcached_hash value
= MEMCACHED_HASH_KETAMA
;
1988 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1990 return MEMCACHED_SUCCESS
;
1992 void my_free(memcached_st
*ptr
, void *mem
)
1997 void *my_malloc(memcached_st
*ptr
, const size_t size
)
1999 return malloc(size
);
2002 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2004 return realloc(mem
, size
);
2007 memcached_return
set_memory_alloc(memcached_st
*memc
)
2010 memcached_malloc_function test_ptr
;
2011 memcached_return rc
;
2013 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2014 assert(rc
== MEMCACHED_SUCCESS
);
2015 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
2016 assert(test_ptr
== (memcached_malloc_function
)my_malloc
);
2020 memcached_realloc_function test_ptr
;
2021 memcached_return rc
;
2023 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2024 assert(rc
== MEMCACHED_SUCCESS
);
2025 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
2026 assert(test_ptr
== my_realloc
);
2030 memcached_free_function test_ptr
;
2031 memcached_return rc
;
2033 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2034 assert(rc
== MEMCACHED_SUCCESS
);
2035 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
2036 assert(test_ptr
== my_free
);
2039 return MEMCACHED_SUCCESS
;
2042 memcached_return
enable_consistent(memcached_st
*memc
)
2044 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2045 memcached_hash hash
;
2046 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, &value
);
2049 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2050 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2052 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2053 assert(hash
== MEMCACHED_HASH_HSIEH
);
2056 return MEMCACHED_SUCCESS
;
2059 memcached_return
enable_cas(memcached_st
*memc
)
2061 unsigned int set
= 1;
2063 memcached_version(memc
);
2065 if (memc
->hosts
[0].major_version
>= 1 &&
2066 memc
->hosts
[0].minor_version
>= 2 &&
2067 memc
->hosts
[0].micro_version
>= 4)
2069 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, &set
);
2071 return MEMCACHED_SUCCESS
;
2074 return MEMCACHED_FAILURE
;
2077 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2079 memcached_version(memc
);
2081 if (memc
->hosts
[0].major_version
>= 1 &&
2082 memc
->hosts
[0].minor_version
>= 2 &&
2083 memc
->hosts
[0].micro_version
>= 4)
2084 return MEMCACHED_SUCCESS
;
2086 return MEMCACHED_FAILURE
;
2089 memcached_return
pre_unix_socket(memcached_st
*memc
)
2091 memcached_return rc
;
2094 memcached_server_list_free(memc
->hosts
);
2096 memc
->number_of_hosts
= 0;
2098 if (stat("/tmp/memcached.socket", &buf
))
2099 return MEMCACHED_FAILURE
;
2101 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2106 memcached_return
pre_udp(memcached_st
*memc
)
2108 memcached_return rc
;
2110 memcached_server_list_free(memc
->hosts
);
2112 memc
->number_of_hosts
= 0;
2115 return MEMCACHED_FAILURE
;
2117 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2122 memcached_return
pre_nodelay(memcached_st
*memc
)
2124 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
2125 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
2127 return MEMCACHED_SUCCESS
;
2130 memcached_return
poll_timeout(memcached_st
*memc
)
2136 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, &timeout
);
2138 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2140 assert(timeout
== 100);
2142 return MEMCACHED_SUCCESS
;
2146 /* Clean the server before beginning testing */
2148 {"flush", 0, flush_test
},
2149 {"init", 0, init_test
},
2150 {"allocation", 0, allocation_test
},
2151 {"server_list_null_test", 0, server_list_null_test
},
2152 {"clone_test", 0, clone_test
},
2153 {"error", 0, error_test
},
2154 {"set", 0, set_test
},
2155 {"set2", 0, set_test2
},
2156 {"set3", 0, set_test3
},
2157 {"add", 1, add_test
},
2158 {"replace", 0, replace_test
},
2159 {"delete", 1, delete_test
},
2160 {"get", 1, get_test
},
2161 {"get2", 0, get_test2
},
2162 {"get3", 0, get_test3
},
2163 {"get4", 0, get_test4
},
2164 {"stats_servername", 0, stats_servername_test
},
2165 {"increment", 0, increment_test
},
2166 {"decrement", 0, decrement_test
},
2167 {"quit", 0, quit_test
},
2168 {"mget", 1, mget_test
},
2169 {"mget_result", 1, mget_result_test
},
2170 {"mget_result_alloc", 1, mget_result_alloc_test
},
2171 {"mget_result_function", 1, mget_result_function
},
2172 {"get_stats", 0, get_stats
},
2173 {"add_host_test", 0, add_host_test
},
2174 {"get_stats_keys", 0, get_stats_keys
},
2175 {"behavior_test", 0, get_stats_keys
},
2176 {"callback_test", 0, get_stats_keys
},
2180 test_st async_tests
[] ={
2181 {"add", 1, add_wrapper
},
2185 test_st string_tests
[] ={
2186 {"string static with null", 0, string_static_null
},
2187 {"string alloc with null", 0, string_alloc_null
},
2188 {"string alloc with 1K", 0, string_alloc_with_size
},
2189 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2190 {"string append", 0, string_alloc_append
},
2191 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2195 test_st result_tests
[] ={
2196 {"result static", 0, result_static
},
2197 {"result alloc", 0, result_alloc
},
2201 test_st version_1_2_3
[] ={
2202 {"append", 0, append_test
},
2203 {"prepend", 0, prepend_test
},
2204 {"cas", 0, cas_test
},
2205 {"cas2", 0, cas2_test
},
2206 {"append_binary", 0, append_binary_test
},
2210 test_st user_tests
[] ={
2211 {"user_supplied_bug1", 0, user_supplied_bug1
},
2212 {"user_supplied_bug2", 0, user_supplied_bug2
},
2213 {"user_supplied_bug3", 0, user_supplied_bug3
},
2214 {"user_supplied_bug4", 0, user_supplied_bug4
},
2215 {"user_supplied_bug5", 1, user_supplied_bug5
},
2216 {"user_supplied_bug6", 1, user_supplied_bug6
},
2217 {"user_supplied_bug7", 1, user_supplied_bug7
},
2218 {"user_supplied_bug8", 1, user_supplied_bug8
},
2219 {"user_supplied_bug9", 1, user_supplied_bug9
},
2220 {"user_supplied_bug10", 1, user_supplied_bug10
},
2221 {"user_supplied_bug11", 1, user_supplied_bug11
},
2225 test_st generate_tests
[] ={
2226 {"generate_data", 1, generate_data
},
2227 {"get_read", 0, get_read
},
2228 {"delete_generate", 0, delete_generate
},
2229 {"generate_buffer_data", 1, generate_buffer_data
},
2230 {"delete_buffer", 0, delete_buffer_generate
},
2231 {"generate_data", 1, generate_data
},
2232 {"mget_read", 0, mget_read
},
2233 {"mget_read_result", 0, mget_read_result
},
2234 {"mget_read_function", 0, mget_read_function
},
2235 {"cleanup", 1, cleanup_pairs
},
2240 collection_st collection
[] ={
2241 {"block", 0, 0, tests
},
2242 {"nonblock", pre_nonblock
, 0, tests
},
2243 {"nodelay", pre_nodelay
, 0, tests
},
2244 {"md5", pre_md5
, 0, tests
},
2245 {"crc", pre_crc
, 0, tests
},
2246 {"hsieh", pre_hsieh
, 0, tests
},
2247 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2248 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2249 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2250 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2251 {"ketama", pre_hash_ketama
, 0, tests
},
2252 {"unix_socket", pre_unix_socket
, 0, tests
},
2253 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2254 {"poll_timeout", poll_timeout
, 0, tests
},
2255 {"gets", enable_cas
, 0, tests
},
2256 {"consistent", enable_consistent
, 0, tests
},
2257 {"memory_allocators", set_memory_alloc
, 0, tests
},
2258 // {"udp", pre_udp, 0, tests},
2259 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2260 {"string", 0, 0, string_tests
},
2261 {"result", 0, 0, result_tests
},
2262 {"async", pre_nonblock
, 0, async_tests
},
2263 {"user", 0, 0, user_tests
},
2264 {"generate", 0, 0, generate_tests
},
2265 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2266 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2267 {"generate_md5", pre_md5
, 0, generate_tests
},
2268 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2272 collection_st
*gets_collections(void)