2 Sample test application.
10 #include <sys/types.h>
14 #include "../lib/common.h"
15 #include "../src/generator.h"
16 #include "../src/execute.h"
20 #define GLOBAL_COUNT 100000
21 static pairs_st
*global_pairs
;
22 static char *global_keys
[GLOBAL_COUNT
];
23 static size_t global_keys_length
[GLOBAL_COUNT
];
25 uint8_t init_test(memcached_st
*not_used
)
29 (void)memcached_create(&memc
);
30 memcached_free(&memc
);
35 uint8_t allocation_test(memcached_st
*not_used
)
38 memc
= memcached_create(NULL
);
45 uint8_t clone_test(memcached_st
*memc
)
50 clone
= memcached_clone(NULL
, NULL
);
52 memcached_free(clone
);
55 /* Can we init from null? */
58 clone
= memcached_clone(NULL
, memc
);
60 memcached_free(clone
);
63 /* Can we init from struct? */
65 memcached_st declared_clone
;
67 clone
= memcached_clone(&declared_clone
, NULL
);
69 memcached_free(clone
);
72 /* Can we init from struct? */
74 memcached_st declared_clone
;
76 clone
= memcached_clone(&declared_clone
, memc
);
78 memcached_free(clone
);
84 uint8_t connection_test(memcached_st
*memc
)
88 rc
= memcached_server_add(memc
, "localhost", 0);
89 assert(rc
== MEMCACHED_SUCCESS
);
94 uint8_t error_test(memcached_st
*memc
)
98 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
100 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
106 uint8_t set_test(memcached_st
*memc
)
110 char *value
= "when we sanitize";
112 rc
= memcached_set(memc
, key
, strlen(key
),
113 value
, strlen(value
),
114 (time_t)0, (uint16_t)0);
115 assert(rc
== MEMCACHED_SUCCESS
);
120 uint8_t append_test(memcached_st
*memc
)
128 rc
= memcached_flush(memc
, 0);
129 assert(rc
== MEMCACHED_SUCCESS
);
131 rc
= memcached_set(memc
, key
, strlen(key
),
132 value
, strlen(value
),
133 (time_t)0, (uint16_t)0);
134 assert(rc
== MEMCACHED_SUCCESS
);
136 rc
= memcached_append(memc
, key
, strlen(key
),
137 " the", strlen(" the"),
138 (time_t)0, (uint16_t)0);
139 assert(rc
== MEMCACHED_SUCCESS
);
141 rc
= memcached_append(memc
, key
, strlen(key
),
142 " people", strlen(" people"),
143 (time_t)0, (uint16_t)0);
144 assert(rc
== MEMCACHED_SUCCESS
);
146 value
= memcached_get(memc
, key
, strlen(key
),
147 &value_length
, &flags
, &rc
);
148 assert(!memcmp(value
, "we the people", strlen("we the people")));
149 assert(strlen("we the people") == value_length
);
150 assert(rc
== MEMCACHED_SUCCESS
);
155 uint8_t cas_test(memcached_st
*memc
)
159 size_t key_length
= strlen("fun");
160 char *value
= "we the people";
161 size_t value_length
= strlen("we the people");
162 memcached_result_st results_obj
;
163 memcached_result_st
*results
;
165 rc
= memcached_flush(memc
, 0);
166 assert(rc
== MEMCACHED_SUCCESS
);
168 rc
= memcached_set(memc
, key
, strlen(key
),
169 value
, strlen(value
),
170 (time_t)0, (uint16_t)0);
171 assert(rc
== MEMCACHED_SUCCESS
);
173 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
175 results
= memcached_result_create(memc
, &results_obj
);
177 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
179 assert(rc
== MEMCACHED_SUCCESS
);
180 WATCHPOINT_NUMBER(memcached_result_cas(results
));
181 WATCHPOINT_ASSERT(memcached_result_cas(results
));
183 assert(!memcmp(value
, "we the people", strlen("we the people")));
184 assert(strlen("we the people") == value_length
);
185 assert(rc
== MEMCACHED_SUCCESS
);
187 memcached_result_free(&results_obj
);
192 uint8_t prepend_test(memcached_st
*memc
)
196 char *value
= "people";
200 rc
= memcached_flush(memc
, 0);
201 assert(rc
== MEMCACHED_SUCCESS
);
203 rc
= memcached_set(memc
, key
, strlen(key
),
204 value
, strlen(value
),
205 (time_t)0, (uint16_t)0);
206 assert(rc
== MEMCACHED_SUCCESS
);
208 rc
= memcached_prepend(memc
, key
, strlen(key
),
209 "the ", strlen("the "),
210 (time_t)0, (uint16_t)0);
211 assert(rc
== MEMCACHED_SUCCESS
);
213 rc
= memcached_prepend(memc
, key
, strlen(key
),
214 "we ", strlen("we "),
215 (time_t)0, (uint16_t)0);
216 assert(rc
== MEMCACHED_SUCCESS
);
218 value
= memcached_get(memc
, key
, strlen(key
),
219 &value_length
, &flags
, &rc
);
220 assert(!memcmp(value
, "we the people", strlen("we the people")));
221 assert(strlen("we the people") == value_length
);
222 assert(rc
== MEMCACHED_SUCCESS
);
227 uint8_t add_test(memcached_st
*memc
)
231 char *value
= "when we sanitize";
233 rc
= memcached_add(memc
, key
, strlen(key
),
234 value
, strlen(value
),
235 (time_t)0, (uint16_t)0);
236 assert(rc
== MEMCACHED_NOTSTORED
);
241 uint8_t replace_test(memcached_st
*memc
)
245 char *value
= "when we sanitize";
247 rc
= memcached_replace(memc
, key
, strlen(key
),
248 value
, strlen(value
),
249 (time_t)0, (uint16_t)0);
250 assert(rc
== MEMCACHED_SUCCESS
);
255 uint8_t delete_test(memcached_st
*memc
)
259 char *value
= "when we sanitize";
261 rc
= memcached_set(memc
, key
, strlen(key
),
262 value
, strlen(value
),
263 (time_t)0, (uint16_t)0);
264 assert(rc
== MEMCACHED_SUCCESS
);
266 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
267 assert(rc
== MEMCACHED_SUCCESS
);
272 uint8_t flush_test(memcached_st
*memc
)
276 rc
= memcached_flush(memc
, 0);
277 assert(rc
== MEMCACHED_SUCCESS
);
282 uint8_t get_test(memcached_st
*memc
)
287 size_t string_length
;
290 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
291 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
);
293 string
= memcached_get(memc
, key
, strlen(key
),
294 &string_length
, &flags
, &rc
);
296 assert(rc
== MEMCACHED_NOTFOUND
);
297 assert(string_length
== 0);
303 uint8_t get_test2(memcached_st
*memc
)
307 char *value
= "when we sanitize";
309 size_t string_length
;
312 rc
= memcached_set(memc
, key
, strlen(key
),
313 value
, strlen(value
),
314 (time_t)0, (uint16_t)0);
315 assert(rc
== MEMCACHED_SUCCESS
);
317 string
= memcached_get(memc
, key
, strlen(key
),
318 &string_length
, &flags
, &rc
);
321 assert(rc
== MEMCACHED_SUCCESS
);
322 assert(string_length
== strlen(value
));
323 assert(!memcmp(string
, value
, string_length
));
330 uint8_t set_test2(memcached_st
*memc
)
334 char *value
= "train in the brain";
335 size_t value_length
= strlen(value
);
338 for (x
= 0; x
< 10; x
++)
340 rc
= memcached_set(memc
, key
, strlen(key
),
342 (time_t)0, (uint16_t)0);
343 assert(rc
== MEMCACHED_SUCCESS
);
349 uint8_t set_test3(memcached_st
*memc
)
354 size_t value_length
= 8191;
357 value
= (char*)malloc(value_length
);
360 for (x
= 0; x
< value_length
; x
++)
361 value
[x
] = (char) (x
% 127);
363 for (x
= 0; x
< 1; x
++)
365 rc
= memcached_set(memc
, key
, strlen(key
),
367 (time_t)0, (uint16_t)0);
368 assert(rc
== MEMCACHED_SUCCESS
);
376 uint8_t get_test3(memcached_st
*memc
)
381 size_t value_length
= 8191;
383 size_t string_length
;
387 value
= (char*)malloc(value_length
);
390 for (x
= 0; x
< value_length
; x
++)
391 value
[x
] = (char) (x
% 127);
393 rc
= memcached_set(memc
, key
, strlen(key
),
395 (time_t)0, (uint16_t)0);
396 assert(rc
== MEMCACHED_SUCCESS
);
398 string
= memcached_get(memc
, key
, strlen(key
),
399 &string_length
, &flags
, &rc
);
401 assert(rc
== MEMCACHED_SUCCESS
);
403 assert(string_length
== value_length
);
404 assert(!memcmp(string
, value
, string_length
));
412 uint8_t get_test4(memcached_st
*memc
)
417 size_t value_length
= 8191;
419 size_t string_length
;
423 value
= (char*)malloc(value_length
);
426 for (x
= 0; x
< value_length
; x
++)
427 value
[x
] = (char) (x
% 127);
429 rc
= memcached_set(memc
, key
, strlen(key
),
431 (time_t)0, (uint16_t)0);
432 assert(rc
== MEMCACHED_SUCCESS
);
434 for (x
= 0; x
< 10; x
++)
436 string
= memcached_get(memc
, key
, strlen(key
),
437 &string_length
, &flags
, &rc
);
439 assert(rc
== MEMCACHED_SUCCESS
);
441 assert(string_length
== value_length
);
442 assert(!memcmp(string
, value
, string_length
));
451 uint8_t stats_servername_test(memcached_st
*memc
)
454 memcached_stat_st stat
;
455 rc
= memcached_stat_servername(&stat
, NULL
,
457 MEMCACHED_DEFAULT_PORT
);
462 uint8_t increment_test(memcached_st
*memc
)
469 rc
= memcached_set(memc
, key
, strlen(key
),
470 value
, strlen(value
),
471 (time_t)0, (uint16_t)0);
472 assert(rc
== MEMCACHED_SUCCESS
);
474 rc
= memcached_increment(memc
, key
, strlen(key
),
476 assert(rc
== MEMCACHED_SUCCESS
);
477 assert(new_number
== 1);
479 rc
= memcached_increment(memc
, key
, strlen(key
),
481 assert(rc
== MEMCACHED_SUCCESS
);
482 assert(new_number
== 2);
487 uint8_t decrement_test(memcached_st
*memc
)
494 rc
= memcached_set(memc
, key
, strlen(key
),
495 value
, strlen(value
),
496 (time_t)0, (uint16_t)0);
497 assert(rc
== MEMCACHED_SUCCESS
);
499 rc
= memcached_decrement(memc
, key
, strlen(key
),
501 assert(rc
== MEMCACHED_SUCCESS
);
502 assert(new_number
== 2);
504 rc
= memcached_decrement(memc
, key
, strlen(key
),
506 assert(rc
== MEMCACHED_SUCCESS
);
507 assert(new_number
== 1);
512 uint8_t quit_test(memcached_st
*memc
)
516 char *value
= "sanford and sun";
518 rc
= memcached_set(memc
, key
, strlen(key
),
519 value
, strlen(value
),
520 (time_t)10, (uint16_t)3);
521 assert(rc
== MEMCACHED_SUCCESS
);
522 memcached_quit(memc
);
524 rc
= memcached_set(memc
, key
, strlen(key
),
525 value
, strlen(value
),
526 (time_t)50, (uint16_t)9);
527 assert(rc
== MEMCACHED_SUCCESS
);
532 uint8_t mget_result_test(memcached_st
*memc
)
535 char *keys
[]= {"fudge", "son", "food"};
536 size_t key_length
[]= {5, 3, 4};
539 memcached_result_st results_obj
;
540 memcached_result_st
*results
;
542 results
= memcached_result_create(memc
, &results_obj
);
544 assert(&results_obj
== results
);
546 /* We need to empty the server before continueing test */
547 rc
= memcached_flush(memc
, 0);
548 assert(rc
== MEMCACHED_SUCCESS
);
550 rc
= memcached_mget(memc
, keys
, key_length
, 3);
551 assert(rc
== MEMCACHED_SUCCESS
);
553 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
557 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
559 assert(rc
== MEMCACHED_NOTFOUND
);
561 for (x
= 0; x
< 3; x
++)
563 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
564 keys
[x
], key_length
[x
],
565 (time_t)50, (uint16_t)9);
566 assert(rc
== MEMCACHED_SUCCESS
);
569 rc
= memcached_mget(memc
, keys
, key_length
, 3);
570 assert(rc
== MEMCACHED_SUCCESS
);
572 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
575 assert(&results_obj
== results
);
576 assert(rc
== MEMCACHED_SUCCESS
);
577 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
578 assert(!memcmp(memcached_result_key_value(results
),
579 memcached_result_value(results
),
580 memcached_result_length(results
)));
583 memcached_result_free(&results_obj
);
588 uint8_t mget_result_alloc_test(memcached_st
*memc
)
591 char *keys
[]= {"fudge", "son", "food"};
592 size_t key_length
[]= {5, 3, 4};
595 memcached_result_st
*results
;
597 /* We need to empty the server before continueing test */
598 rc
= memcached_flush(memc
, 0);
599 assert(rc
== MEMCACHED_SUCCESS
);
601 rc
= memcached_mget(memc
, keys
, key_length
, 3);
602 assert(rc
== MEMCACHED_SUCCESS
);
604 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
609 assert(rc
== MEMCACHED_NOTFOUND
);
611 for (x
= 0; x
< 3; x
++)
613 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
614 keys
[x
], key_length
[x
],
615 (time_t)50, (uint16_t)9);
616 assert(rc
== MEMCACHED_SUCCESS
);
619 rc
= memcached_mget(memc
, keys
, key_length
, 3);
620 assert(rc
== MEMCACHED_SUCCESS
);
623 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
626 assert(rc
== MEMCACHED_SUCCESS
);
627 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
628 assert(!memcmp(memcached_result_key_value(results
),
629 memcached_result_value(results
),
630 memcached_result_length(results
)));
631 memcached_result_free(results
);
638 uint8_t mget_test(memcached_st
*memc
)
641 char *keys
[]= {"fudge", "son", "food"};
642 size_t key_length
[]= {5, 3, 4};
646 char return_key
[MEMCACHED_MAX_KEY
];
647 size_t return_key_length
;
649 size_t return_value_length
;
651 /* We need to empty the server before continueing test */
652 rc
= memcached_flush(memc
, 0);
653 assert(rc
== MEMCACHED_SUCCESS
);
655 rc
= memcached_mget(memc
, keys
, key_length
, 3);
656 assert(rc
== MEMCACHED_SUCCESS
);
658 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
659 &return_value_length
, &flags
, &rc
)) != NULL
)
661 assert(return_value
);
663 assert(!return_value
);
664 assert(return_value_length
== 0);
665 assert(rc
== MEMCACHED_NOTFOUND
);
667 for (x
= 0; x
< 3; x
++)
669 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
670 keys
[x
], key_length
[x
],
671 (time_t)50, (uint16_t)9);
672 assert(rc
== MEMCACHED_SUCCESS
);
675 rc
= memcached_mget(memc
, keys
, key_length
, 3);
676 assert(rc
== MEMCACHED_SUCCESS
);
679 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
680 &return_value_length
, &flags
, &rc
)))
682 assert(return_value
);
683 assert(rc
== MEMCACHED_SUCCESS
);
684 assert(return_key_length
== return_value_length
);
685 assert(!memcmp(return_value
, return_key
, return_value_length
));
693 uint8_t get_stats_keys(memcached_st
*memc
)
697 memcached_stat_st stat
;
700 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
701 assert(rc
== MEMCACHED_SUCCESS
);
702 for (ptr
= list
; *ptr
; ptr
++)
703 printf("Found key %s\n", *ptr
);
711 uint8_t get_stats(memcached_st
*memc
)
717 memcached_stat_st
*stat
;
719 stat
= memcached_stat(memc
, NULL
, &rc
);
720 assert(rc
== MEMCACHED_SUCCESS
);
722 assert(rc
== MEMCACHED_SUCCESS
);
725 for (x
= 0; x
< memcached_server_count(memc
); x
++)
727 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
728 assert(rc
== MEMCACHED_SUCCESS
);
729 for (ptr
= list
; *ptr
; ptr
++)
730 printf("Found key %s\n", *ptr
);
735 memcached_stat_free(NULL
, stat
);
740 uint8_t add_host_test(memcached_st
*memc
)
743 memcached_server_st
*servers
;
745 char servername
[]= "0.example.com";
747 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
749 assert(1 == memcached_server_list_count(servers
));
751 for (x
= 2; x
< 20; x
++)
753 char buffer
[SMALL_STRING_LEN
];
755 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
756 servers
= memcached_server_list_append(servers
, buffer
, 401,
758 assert(rc
== MEMCACHED_SUCCESS
);
759 assert(x
== memcached_server_list_count(servers
));
762 rc
= memcached_server_push(memc
, servers
);
763 assert(rc
== MEMCACHED_SUCCESS
);
764 rc
= memcached_server_push(memc
, servers
);
765 assert(rc
== MEMCACHED_SUCCESS
);
767 memcached_server_list_free(servers
);
772 /* We don't test the behavior itself, we test the switches */
773 uint8_t behavior_test(memcached_st
*memc
)
775 unsigned long long value
;
778 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
779 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
782 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
783 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
786 set
= MEMCACHED_HASH_MD5
;
787 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
788 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
789 assert(value
== MEMCACHED_HASH_MD5
);
793 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
794 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
797 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
798 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
801 set
= MEMCACHED_HASH_DEFAULT
;
802 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
803 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
804 assert(value
== MEMCACHED_HASH_DEFAULT
);
806 set
= MEMCACHED_HASH_CRC
;
807 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
808 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
809 assert(value
== MEMCACHED_HASH_CRC
);
811 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
814 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
820 /* Test case provided by Cal Haldenbrand */
821 uint8_t user_supplied_bug1(memcached_st
*memc
)
823 unsigned int setter
= 1;
826 unsigned long long total
= 0;
829 char randomstuff
[6 * 1024];
832 memset(randomstuff
, 0, 6 * 1024);
834 /* We just keep looking at the same values over and over */
837 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
838 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
842 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
846 size
= (rand() % ( 5 * 1024 ) ) + 400;
847 memset(randomstuff
, 0, 6 * 1024);
848 assert(size
< 6 * 1024); /* Being safe here */
850 for (j
= 0 ; j
< size
;j
++)
851 randomstuff
[j
] = (char) (rand() % 26) + 97;
854 sprintf(key
, "%d", x
);
855 rc
= memcached_set(memc
, key
, strlen(key
),
856 randomstuff
, strlen(randomstuff
), 10, 0);
857 /* If we fail, lets try again */
858 if (rc
!= MEMCACHED_SUCCESS
)
859 rc
= memcached_set(memc
, key
, strlen(key
),
860 randomstuff
, strlen(randomstuff
), 10, 0);
861 assert(rc
== MEMCACHED_SUCCESS
);
867 /* Test case provided by Cal Haldenbrand */
868 uint8_t user_supplied_bug2(memcached_st
*memc
)
873 unsigned long long total
;
876 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
877 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
879 setter
= 20 * 1024576;
880 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
881 setter
= 20 * 1024576;
882 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
883 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
884 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
886 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
889 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
891 memcached_return rc
= MEMCACHED_SUCCESS
;
892 char buffer
[SMALL_STRING_LEN
];
897 memset(buffer
, 0, SMALL_STRING_LEN
);
899 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
900 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
901 &val_len
, &flags
, &rc
);
902 if (rc
!= MEMCACHED_SUCCESS
)
904 if (rc
== MEMCACHED_NOTFOUND
)
919 /* Do a large mget() over all the keys we think exist */
920 #define KEY_COUNT 3000 // * 1024576
921 uint8_t user_supplied_bug3(memcached_st
*memc
)
927 size_t key_lengths
[KEY_COUNT
];
930 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
931 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
933 setter
= 20 * 1024576;
934 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
935 setter
= 20 * 1024576;
936 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
937 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
938 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
941 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
943 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
944 for (x
= 0; x
< KEY_COUNT
; x
++)
948 snprintf(buffer
, 30, "%u", x
);
949 keys
[x
]= strdup(buffer
);
950 key_lengths
[x
]= strlen(keys
[x
]);
953 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
954 assert(rc
== MEMCACHED_SUCCESS
);
956 /* Turn this into a help function */
958 char return_key
[MEMCACHED_MAX_KEY
];
959 size_t return_key_length
;
961 size_t return_value_length
;
964 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
965 &return_value_length
, &flags
, &rc
)))
967 assert(return_value
);
968 assert(rc
== MEMCACHED_SUCCESS
);
973 for (x
= 0; x
< KEY_COUNT
; x
++)
980 /* Make sure we behave properly if server list has no values */
981 uint8_t user_supplied_bug4(memcached_st
*memc
)
984 char *keys
[]= {"fudge", "son", "food"};
985 size_t key_length
[]= {5, 3, 4};
989 /* Here we free everything before running a bunch of mget tests */
991 memcached_server_list_free(memc
->hosts
);
993 memc
->number_of_hosts
= 0;
996 char return_key
[MEMCACHED_MAX_KEY
];
997 size_t return_key_length
;
999 size_t return_value_length
;
1001 /* We need to empty the server before continueing test */
1002 rc
= memcached_flush(memc
, 0);
1003 assert(rc
== MEMCACHED_NO_SERVERS
);
1005 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1006 assert(rc
== MEMCACHED_NO_SERVERS
);
1008 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1009 &return_value_length
, &flags
, &rc
)) != NULL
)
1011 assert(return_value
);
1013 assert(!return_value
);
1014 assert(return_value_length
== 0);
1015 assert(rc
== MEMCACHED_NO_SERVERS
);
1017 for (x
= 0; x
< 3; x
++)
1019 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1020 keys
[x
], key_length
[x
],
1021 (time_t)50, (uint16_t)9);
1022 assert(rc
== MEMCACHED_NO_SERVERS
);
1025 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1026 assert(rc
== MEMCACHED_NO_SERVERS
);
1029 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1030 &return_value_length
, &flags
, &rc
)))
1032 assert(return_value
);
1033 assert(rc
== MEMCACHED_SUCCESS
);
1034 assert(return_key_length
== return_value_length
);
1035 assert(!memcmp(return_value
, return_key
, return_value_length
));
1043 #define VALUE_SIZE_BUG5 1048064
1044 uint8_t user_supplied_bug5(memcached_st
*memc
)
1046 memcached_return rc
;
1047 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1048 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1049 char return_key
[MEMCACHED_MAX_KEY
];
1050 size_t return_key_length
;
1052 size_t value_length
;
1056 char insert_data
[VALUE_SIZE_BUG5
];
1058 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1059 insert_data
[x
]= rand();
1061 memcached_flush(memc
, 0);
1062 value
= memcached_get(memc
, keys
[0], key_length
[0],
1063 &value_length
, &flags
, &rc
);
1064 assert(value
== NULL
);
1065 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1068 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1069 &value_length
, &flags
, &rc
)))
1073 for (x
= 0; x
< 4; x
++)
1075 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1076 insert_data
, VALUE_SIZE_BUG5
,
1077 (time_t)0, (uint16_t)0);
1078 assert(rc
== MEMCACHED_SUCCESS
);
1081 for (x
= 0; x
< 10; x
++)
1083 value
= memcached_get(memc
, keys
[0], key_length
[0],
1084 &value_length
, &flags
, &rc
);
1088 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1090 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1091 &value_length
, &flags
, &rc
)))
1102 uint8_t user_supplied_bug6(memcached_st
*memc
)
1104 memcached_return rc
;
1105 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1106 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1107 char return_key
[MEMCACHED_MAX_KEY
];
1108 size_t return_key_length
;
1110 size_t value_length
;
1114 char insert_data
[VALUE_SIZE_BUG5
];
1116 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1117 insert_data
[x
]= rand();
1119 memcached_flush(memc
, 0);
1120 value
= memcached_get(memc
, keys
[0], key_length
[0],
1121 &value_length
, &flags
, &rc
);
1122 assert(value
== NULL
);
1123 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1126 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1127 &value_length
, &flags
, &rc
)))
1131 for (x
= 0; x
< 4; x
++)
1133 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1134 insert_data
, VALUE_SIZE_BUG5
,
1135 (time_t)0, (uint16_t)0);
1136 assert(rc
== MEMCACHED_SUCCESS
);
1139 for (x
= 0; x
< 10; x
++)
1141 value
= memcached_get(memc
, keys
[0], key_length
[0],
1142 &value_length
, &flags
, &rc
);
1146 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1148 /* We test for purge of partial complete fetches */
1149 for (count
= 3; count
; count
--)
1151 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1152 &value_length
, &flags
, &rc
);
1154 assert(rc
== MEMCACHED_SUCCESS
);
1161 /* Test flag store/retrieve */
1162 uint8_t user_supplied_bug7(memcached_st
*memc
)
1164 memcached_return rc
;
1165 char *keys
= "036790384900";
1166 size_t key_length
= strlen("036790384900");
1167 char return_key
[MEMCACHED_MAX_KEY
];
1168 size_t return_key_length
;
1170 size_t value_length
;
1173 char insert_data
[VALUE_SIZE_BUG5
];
1175 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1176 insert_data
[x
]= rand();
1178 memcached_flush(memc
, 0);
1181 rc
= memcached_set(memc
, keys
, key_length
,
1182 insert_data
, VALUE_SIZE_BUG5
,
1184 assert(rc
== MEMCACHED_SUCCESS
);
1187 value
= memcached_get(memc
, keys
, key_length
,
1188 &value_length
, &flags
, &rc
);
1189 assert(flags
== 245);
1193 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1196 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1197 &value_length
, &flags
, &rc
);
1198 assert(flags
== 245);
1206 uint8_t result_static(memcached_st
*memc
)
1208 memcached_result_st result
;
1209 memcached_result_st
*result_ptr
;
1211 result_ptr
= memcached_result_create(memc
, &result
);
1212 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1214 memcached_result_free(&result
);
1219 uint8_t result_alloc(memcached_st
*memc
)
1221 memcached_result_st
*result
;
1223 result
= memcached_result_create(memc
, NULL
);
1225 memcached_result_free(result
);
1230 uint8_t string_static_null(memcached_st
*memc
)
1232 memcached_string_st string
;
1233 memcached_string_st
*string_ptr
;
1235 string_ptr
= memcached_string_create(memc
, &string
, 0);
1236 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1238 memcached_string_free(&string
);
1243 uint8_t string_alloc_null(memcached_st
*memc
)
1245 memcached_string_st
*string
;
1247 string
= memcached_string_create(memc
, NULL
, 0);
1249 memcached_string_free(string
);
1254 uint8_t string_alloc_with_size(memcached_st
*memc
)
1256 memcached_string_st
*string
;
1258 string
= memcached_string_create(memc
, NULL
, 1024);
1260 memcached_string_free(string
);
1265 uint8_t string_alloc_with_size_toobig(memcached_st
*memc
)
1267 memcached_string_st
*string
;
1269 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
1270 assert(string
== NULL
);
1275 uint8_t string_alloc_append(memcached_st
*memc
)
1278 char buffer
[SMALL_STRING_LEN
];
1279 memcached_string_st
*string
;
1281 /* Ring the bell! */
1282 memset(buffer
, 6, SMALL_STRING_LEN
);
1284 string
= memcached_string_create(memc
, NULL
, 100);
1287 for (x
= 0; x
< 1024; x
++)
1289 memcached_return rc
;
1290 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1291 assert(rc
== MEMCACHED_SUCCESS
);
1293 memcached_string_free(string
);
1298 uint8_t string_alloc_append_toobig(memcached_st
*memc
)
1300 memcached_return rc
;
1302 char buffer
[SMALL_STRING_LEN
];
1303 memcached_string_st
*string
;
1305 /* Ring the bell! */
1306 memset(buffer
, 6, SMALL_STRING_LEN
);
1308 string
= memcached_string_create(memc
, NULL
, 100);
1311 for (x
= 0; x
< 1024; x
++)
1313 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1314 assert(rc
== MEMCACHED_SUCCESS
);
1316 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
1317 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
1318 memcached_string_free(string
);
1323 uint8_t generate_data(memcached_st
*memc
)
1325 unsigned long long x
;
1326 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1327 execute_set(memc
, global_pairs
, GLOBAL_COUNT
);
1329 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1331 global_keys
[x
]= global_pairs
[x
].key
;
1332 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1338 uint8_t get_read(memcached_st
*memc
)
1341 memcached_return rc
;
1345 size_t return_value_length
;
1348 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1350 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
1351 &return_value_length
, &flags
, &rc
);
1353 assert(return_value);
1354 assert(rc == MEMCACHED_SUCCESS);
1356 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
1364 uint8_t mget_read(memcached_st
*memc
)
1366 memcached_return rc
;
1368 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1369 assert(rc
== MEMCACHED_SUCCESS
);
1370 /* Turn this into a help function */
1372 char return_key
[MEMCACHED_MAX_KEY
];
1373 size_t return_key_length
;
1375 size_t return_value_length
;
1378 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1379 &return_value_length
, &flags
, &rc
)))
1381 assert(return_value
);
1382 assert(rc
== MEMCACHED_SUCCESS
);
1390 uint8_t mget_read_result(memcached_st
*memc
)
1392 memcached_return rc
;
1394 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1395 assert(rc
== MEMCACHED_SUCCESS
);
1396 /* Turn this into a help function */
1398 memcached_result_st results_obj
;
1399 memcached_result_st
*results
;
1401 results
= memcached_result_create(memc
, &results_obj
);
1403 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1406 assert(rc
== MEMCACHED_SUCCESS
);
1409 memcached_result_free(&results_obj
);
1415 uint8_t free_data(memcached_st
*memc
)
1417 pairs_free(global_pairs
);
1422 uint8_t add_host_test1(memcached_st
*memc
)
1425 memcached_return rc
;
1426 char servername
[]= "0.example.com";
1427 memcached_server_st
*servers
;
1429 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1431 assert(1 == memcached_server_list_count(servers
));
1433 for (x
= 2; x
< 20; x
++)
1435 char buffer
[SMALL_STRING_LEN
];
1437 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1438 servers
= memcached_server_list_append(servers
, buffer
, 401,
1440 assert(rc
== MEMCACHED_SUCCESS
);
1441 assert(x
== memcached_server_list_count(servers
));
1444 rc
= memcached_server_push(memc
, servers
);
1445 assert(rc
== MEMCACHED_SUCCESS
);
1446 rc
= memcached_server_push(memc
, servers
);
1447 assert(rc
== MEMCACHED_SUCCESS
);
1449 memcached_server_list_free(servers
);
1454 memcached_return
pre_nonblock(memcached_st
*memc
)
1456 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1458 return MEMCACHED_SUCCESS
;
1461 memcached_return
pre_md5(memcached_st
*memc
)
1463 memcached_hash value
= MEMCACHED_HASH_MD5
;
1464 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1466 return MEMCACHED_SUCCESS
;
1469 memcached_return
pre_crc(memcached_st
*memc
)
1471 memcached_hash value
= MEMCACHED_HASH_CRC
;
1472 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1474 return MEMCACHED_SUCCESS
;
1477 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
1479 memcached_hash value
= MEMCACHED_HASH_FNV1_64
;
1480 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1482 return MEMCACHED_SUCCESS
;
1485 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
1487 memcached_hash value
= MEMCACHED_HASH_FNV1A_64
;
1488 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1490 return MEMCACHED_SUCCESS
;
1493 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
1495 memcached_hash value
= MEMCACHED_HASH_FNV1_32
;
1496 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1498 return MEMCACHED_SUCCESS
;
1501 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
1503 memcached_hash value
= MEMCACHED_HASH_FNV1A_32
;
1504 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1506 return MEMCACHED_SUCCESS
;
1509 memcached_return
pre_hash_ketama(memcached_st
*memc
)
1511 memcached_hash value
= MEMCACHED_HASH_KETAMA
;
1512 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1514 return MEMCACHED_SUCCESS
;
1517 memcached_return
check_for_1_2_3(memcached_st
*memc
)
1519 memcached_version(memc
);
1521 if (memc
->hosts
[0].major_version
>= 1 &&
1522 memc
->hosts
[0].minor_version
>= 2 &&
1523 memc
->hosts
[0].micro_version
>= 4)
1524 return MEMCACHED_SUCCESS
;
1526 return MEMCACHED_FAILURE
;
1529 memcached_return
pre_unix_socket(memcached_st
*memc
)
1531 memcached_return rc
;
1534 memcached_server_list_free(memc
->hosts
);
1536 memc
->number_of_hosts
= 0;
1538 if (stat("/tmp/memcached.socket", &buf
))
1539 return MEMCACHED_FAILURE
;
1541 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
1546 memcached_return
pre_udp(memcached_st
*memc
)
1548 memcached_return rc
;
1550 memcached_server_list_free(memc
->hosts
);
1552 memc
->number_of_hosts
= 0;
1555 return MEMCACHED_FAILURE
;
1557 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
1562 memcached_return
pre_nodelay(memcached_st
*memc
)
1564 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1565 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
1567 return MEMCACHED_SUCCESS
;
1571 /* Clean the server before beginning testing */
1573 {"flush", 0, flush_test
},
1574 {"init", 0, init_test
},
1575 {"allocation", 0, allocation_test
},
1576 {"clone_test", 0, clone_test
},
1577 {"error", 0, error_test
},
1578 {"set", 0, set_test
},
1579 {"set2", 0, set_test2
},
1580 {"set3", 0, set_test3
},
1581 {"add", 0, add_test
},
1582 {"replace", 0, replace_test
},
1583 {"delete", 1, delete_test
},
1584 {"get", 1, get_test
},
1585 {"get2", 0, get_test2
},
1586 {"get3", 0, get_test3
},
1587 {"get4", 0, get_test4
},
1588 {"stats_servername", 0, stats_servername_test
},
1589 {"increment", 0, increment_test
},
1590 {"decrement", 0, decrement_test
},
1591 {"quit", 0, quit_test
},
1592 {"mget", 1, mget_test
},
1593 {"mget_result", 1, mget_result_test
},
1594 {"mget_result_alloc", 1, mget_result_alloc_test
},
1595 {"get_stats", 0, get_stats
},
1596 {"add_host_test", 0, add_host_test
},
1597 {"get_stats_keys", 0, get_stats_keys
},
1598 {"behavior_test", 0, get_stats_keys
},
1602 test_st string_tests
[] ={
1603 {"string static with null", 0, string_static_null
},
1604 {"string alloc with null", 0, string_alloc_null
},
1605 {"string alloc with 1K", 0, string_alloc_with_size
},
1606 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
1607 {"string append", 0, string_alloc_append
},
1608 {"string append failure (too big)", 0, string_alloc_append_toobig
},
1612 test_st result_tests
[] ={
1613 {"result static", 0, result_static
},
1614 {"result alloc", 0, result_alloc
},
1618 test_st version_1_2_3
[] ={
1619 {"append", 0, append_test
},
1620 {"prepend", 0, prepend_test
},
1621 // {"cas", 0, cas_test },
1625 test_st user_tests
[] ={
1626 {"user_supplied_bug1", 0, user_supplied_bug1
},
1627 {"user_supplied_bug2", 0, user_supplied_bug2
},
1628 {"user_supplied_bug3", 0, user_supplied_bug3
},
1629 {"user_supplied_bug4", 0, user_supplied_bug4
},
1630 {"user_supplied_bug5", 1, user_supplied_bug5
},
1631 {"user_supplied_bug6", 1, user_supplied_bug6
},
1632 {"user_supplied_bug7", 1, user_supplied_bug7
},
1636 test_st generate_tests
[] ={
1637 {"generate_data", 0, generate_data
},
1638 {"get_read", 0, get_read
},
1639 {"mget_read", 0, mget_read
},
1640 {"mget_read_result", 0, mget_read_result
},
1645 collection_st collection
[] ={
1646 {"block", 0, 0, tests
},
1647 {"nonblock", pre_nonblock
, 0, tests
},
1648 {"nodelay", pre_nodelay
, 0, tests
},
1649 {"md5", pre_md5
, 0, tests
},
1650 {"crc", pre_crc
, 0, tests
},
1651 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
1652 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
1653 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
1654 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
1655 {"ketama", pre_hash_ketama
, 0, tests
},
1656 {"unix_socket", pre_unix_socket
, 0, tests
},
1657 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
1658 // {"udp", pre_udp, 0, tests},
1659 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
1660 {"string", 0, 0, string_tests
},
1661 {"result", 0, 0, result_tests
},
1662 {"user", 0, 0, user_tests
},
1663 {"generate", 0, 0, generate_tests
},
1664 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
1668 collection_st
*gets_collections(void)