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 append_binary_test(memcached_st
*memc
)
158 char *key
= "numbers";
159 unsigned int *store_ptr
;
160 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
166 rc
= memcached_flush(memc
, 0);
167 assert(rc
== MEMCACHED_SUCCESS
);
169 rc
= memcached_set(memc
,
172 (time_t)0, (uint16_t)0);
173 assert(rc
== MEMCACHED_SUCCESS
);
175 for (x
= 0; store_list
[x
] ; x
++)
177 rc
= memcached_append(memc
,
179 (char *)&store_list
[x
], sizeof(unsigned int),
180 (time_t)0, (uint16_t)0);
181 assert(rc
== MEMCACHED_SUCCESS
);
184 value
= memcached_get(memc
, key
, strlen(key
),
185 &value_length
, &flags
, &rc
);
186 assert((value_length
== (sizeof(unsigned int) * x
)));
187 assert(rc
== MEMCACHED_SUCCESS
);
189 store_ptr
= (unsigned int *)value
;
193 assert(*store_ptr
== store_list
[x
++]);
200 uint8_t cas_test(memcached_st
*memc
)
204 size_t key_length
= strlen("fun");
205 char *value
= "we the people";
206 size_t value_length
= strlen("we the people");
207 memcached_result_st results_obj
;
208 memcached_result_st
*results
;
210 rc
= memcached_flush(memc
, 0);
211 assert(rc
== MEMCACHED_SUCCESS
);
213 rc
= memcached_set(memc
, key
, strlen(key
),
214 value
, strlen(value
),
215 (time_t)0, (uint16_t)0);
216 assert(rc
== MEMCACHED_SUCCESS
);
218 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
220 results
= memcached_result_create(memc
, &results_obj
);
222 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
224 assert(rc
== MEMCACHED_SUCCESS
);
225 WATCHPOINT_NUMBER(memcached_result_cas(results
));
226 WATCHPOINT_ASSERT(memcached_result_cas(results
));
228 assert(!memcmp(value
, "we the people", strlen("we the people")));
229 assert(strlen("we the people") == value_length
);
230 assert(rc
== MEMCACHED_SUCCESS
);
232 memcached_result_free(&results_obj
);
237 uint8_t prepend_test(memcached_st
*memc
)
241 char *value
= "people";
245 rc
= memcached_flush(memc
, 0);
246 assert(rc
== MEMCACHED_SUCCESS
);
248 rc
= memcached_set(memc
, key
, strlen(key
),
249 value
, strlen(value
),
250 (time_t)0, (uint16_t)0);
251 assert(rc
== MEMCACHED_SUCCESS
);
253 rc
= memcached_prepend(memc
, key
, strlen(key
),
254 "the ", strlen("the "),
255 (time_t)0, (uint16_t)0);
256 assert(rc
== MEMCACHED_SUCCESS
);
258 rc
= memcached_prepend(memc
, key
, strlen(key
),
259 "we ", strlen("we "),
260 (time_t)0, (uint16_t)0);
261 assert(rc
== MEMCACHED_SUCCESS
);
263 value
= memcached_get(memc
, key
, strlen(key
),
264 &value_length
, &flags
, &rc
);
265 assert(!memcmp(value
, "we the people", strlen("we the people")));
266 assert(strlen("we the people") == value_length
);
267 assert(rc
== MEMCACHED_SUCCESS
);
272 uint8_t add_test(memcached_st
*memc
)
276 char *value
= "when we sanitize";
278 rc
= memcached_add(memc
, key
, strlen(key
),
279 value
, strlen(value
),
280 (time_t)0, (uint16_t)0);
281 assert(rc
== MEMCACHED_NOTSTORED
);
286 uint8_t replace_test(memcached_st
*memc
)
290 char *value
= "when we sanitize";
292 rc
= memcached_replace(memc
, key
, strlen(key
),
293 value
, strlen(value
),
294 (time_t)0, (uint16_t)0);
295 assert(rc
== MEMCACHED_SUCCESS
);
300 uint8_t delete_test(memcached_st
*memc
)
304 char *value
= "when we sanitize";
306 rc
= memcached_set(memc
, key
, strlen(key
),
307 value
, strlen(value
),
308 (time_t)0, (uint16_t)0);
309 assert(rc
== MEMCACHED_SUCCESS
);
311 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
312 assert(rc
== MEMCACHED_SUCCESS
);
317 uint8_t flush_test(memcached_st
*memc
)
321 rc
= memcached_flush(memc
, 0);
322 assert(rc
== MEMCACHED_SUCCESS
);
327 uint8_t get_test(memcached_st
*memc
)
332 size_t string_length
;
335 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
336 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
);
338 string
= memcached_get(memc
, key
, strlen(key
),
339 &string_length
, &flags
, &rc
);
341 assert(rc
== MEMCACHED_NOTFOUND
);
342 assert(string_length
== 0);
348 uint8_t get_test2(memcached_st
*memc
)
352 char *value
= "when we sanitize";
354 size_t string_length
;
357 rc
= memcached_set(memc
, key
, strlen(key
),
358 value
, strlen(value
),
359 (time_t)0, (uint16_t)0);
360 assert(rc
== MEMCACHED_SUCCESS
);
362 string
= memcached_get(memc
, key
, strlen(key
),
363 &string_length
, &flags
, &rc
);
366 assert(rc
== MEMCACHED_SUCCESS
);
367 assert(string_length
== strlen(value
));
368 assert(!memcmp(string
, value
, string_length
));
375 uint8_t set_test2(memcached_st
*memc
)
379 char *value
= "train in the brain";
380 size_t value_length
= strlen(value
);
383 for (x
= 0; x
< 10; x
++)
385 rc
= memcached_set(memc
, key
, strlen(key
),
387 (time_t)0, (uint16_t)0);
388 assert(rc
== MEMCACHED_SUCCESS
);
394 uint8_t set_test3(memcached_st
*memc
)
399 size_t value_length
= 8191;
402 value
= (char*)malloc(value_length
);
405 for (x
= 0; x
< value_length
; x
++)
406 value
[x
] = (char) (x
% 127);
408 for (x
= 0; x
< 1; x
++)
410 rc
= memcached_set(memc
, key
, strlen(key
),
412 (time_t)0, (uint16_t)0);
413 assert(rc
== MEMCACHED_SUCCESS
);
421 uint8_t get_test3(memcached_st
*memc
)
426 size_t value_length
= 8191;
428 size_t string_length
;
432 value
= (char*)malloc(value_length
);
435 for (x
= 0; x
< value_length
; x
++)
436 value
[x
] = (char) (x
% 127);
438 rc
= memcached_set(memc
, key
, strlen(key
),
440 (time_t)0, (uint16_t)0);
441 assert(rc
== MEMCACHED_SUCCESS
);
443 string
= memcached_get(memc
, key
, strlen(key
),
444 &string_length
, &flags
, &rc
);
446 assert(rc
== MEMCACHED_SUCCESS
);
448 assert(string_length
== value_length
);
449 assert(!memcmp(string
, value
, string_length
));
457 uint8_t get_test4(memcached_st
*memc
)
462 size_t value_length
= 8191;
464 size_t string_length
;
468 value
= (char*)malloc(value_length
);
471 for (x
= 0; x
< value_length
; x
++)
472 value
[x
] = (char) (x
% 127);
474 rc
= memcached_set(memc
, key
, strlen(key
),
476 (time_t)0, (uint16_t)0);
477 assert(rc
== MEMCACHED_SUCCESS
);
479 for (x
= 0; x
< 10; x
++)
481 string
= memcached_get(memc
, key
, strlen(key
),
482 &string_length
, &flags
, &rc
);
484 assert(rc
== MEMCACHED_SUCCESS
);
486 assert(string_length
== value_length
);
487 assert(!memcmp(string
, value
, string_length
));
496 uint8_t stats_servername_test(memcached_st
*memc
)
499 memcached_stat_st stat
;
500 rc
= memcached_stat_servername(&stat
, NULL
,
502 MEMCACHED_DEFAULT_PORT
);
507 uint8_t increment_test(memcached_st
*memc
)
514 rc
= memcached_set(memc
, key
, strlen(key
),
515 value
, strlen(value
),
516 (time_t)0, (uint16_t)0);
517 assert(rc
== MEMCACHED_SUCCESS
);
519 rc
= memcached_increment(memc
, key
, strlen(key
),
521 assert(rc
== MEMCACHED_SUCCESS
);
522 assert(new_number
== 1);
524 rc
= memcached_increment(memc
, key
, strlen(key
),
526 assert(rc
== MEMCACHED_SUCCESS
);
527 assert(new_number
== 2);
532 uint8_t decrement_test(memcached_st
*memc
)
539 rc
= memcached_set(memc
, key
, strlen(key
),
540 value
, strlen(value
),
541 (time_t)0, (uint16_t)0);
542 assert(rc
== MEMCACHED_SUCCESS
);
544 rc
= memcached_decrement(memc
, key
, strlen(key
),
546 assert(rc
== MEMCACHED_SUCCESS
);
547 assert(new_number
== 2);
549 rc
= memcached_decrement(memc
, key
, strlen(key
),
551 assert(rc
== MEMCACHED_SUCCESS
);
552 assert(new_number
== 1);
557 uint8_t quit_test(memcached_st
*memc
)
561 char *value
= "sanford and sun";
563 rc
= memcached_set(memc
, key
, strlen(key
),
564 value
, strlen(value
),
565 (time_t)10, (uint16_t)3);
566 assert(rc
== MEMCACHED_SUCCESS
);
567 memcached_quit(memc
);
569 rc
= memcached_set(memc
, key
, strlen(key
),
570 value
, strlen(value
),
571 (time_t)50, (uint16_t)9);
572 assert(rc
== MEMCACHED_SUCCESS
);
577 uint8_t mget_result_test(memcached_st
*memc
)
580 char *keys
[]= {"fudge", "son", "food"};
581 size_t key_length
[]= {5, 3, 4};
584 memcached_result_st results_obj
;
585 memcached_result_st
*results
;
587 results
= memcached_result_create(memc
, &results_obj
);
589 assert(&results_obj
== results
);
591 /* We need to empty the server before continueing test */
592 rc
= memcached_flush(memc
, 0);
593 assert(rc
== MEMCACHED_SUCCESS
);
595 rc
= memcached_mget(memc
, keys
, key_length
, 3);
596 assert(rc
== MEMCACHED_SUCCESS
);
598 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
602 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
604 assert(rc
== MEMCACHED_NOTFOUND
);
606 for (x
= 0; x
< 3; x
++)
608 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
609 keys
[x
], key_length
[x
],
610 (time_t)50, (uint16_t)9);
611 assert(rc
== MEMCACHED_SUCCESS
);
614 rc
= memcached_mget(memc
, keys
, key_length
, 3);
615 assert(rc
== MEMCACHED_SUCCESS
);
617 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
620 assert(&results_obj
== results
);
621 assert(rc
== MEMCACHED_SUCCESS
);
622 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
623 assert(!memcmp(memcached_result_key_value(results
),
624 memcached_result_value(results
),
625 memcached_result_length(results
)));
628 memcached_result_free(&results_obj
);
633 uint8_t mget_result_alloc_test(memcached_st
*memc
)
636 char *keys
[]= {"fudge", "son", "food"};
637 size_t key_length
[]= {5, 3, 4};
640 memcached_result_st
*results
;
642 /* We need to empty the server before continueing test */
643 rc
= memcached_flush(memc
, 0);
644 assert(rc
== MEMCACHED_SUCCESS
);
646 rc
= memcached_mget(memc
, keys
, key_length
, 3);
647 assert(rc
== MEMCACHED_SUCCESS
);
649 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
654 assert(rc
== MEMCACHED_NOTFOUND
);
656 for (x
= 0; x
< 3; x
++)
658 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
659 keys
[x
], key_length
[x
],
660 (time_t)50, (uint16_t)9);
661 assert(rc
== MEMCACHED_SUCCESS
);
664 rc
= memcached_mget(memc
, keys
, key_length
, 3);
665 assert(rc
== MEMCACHED_SUCCESS
);
668 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
671 assert(rc
== MEMCACHED_SUCCESS
);
672 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
673 assert(!memcmp(memcached_result_key_value(results
),
674 memcached_result_value(results
),
675 memcached_result_length(results
)));
676 memcached_result_free(results
);
683 uint8_t mget_test(memcached_st
*memc
)
686 char *keys
[]= {"fudge", "son", "food"};
687 size_t key_length
[]= {5, 3, 4};
691 char return_key
[MEMCACHED_MAX_KEY
];
692 size_t return_key_length
;
694 size_t return_value_length
;
696 /* We need to empty the server before continueing test */
697 rc
= memcached_flush(memc
, 0);
698 assert(rc
== MEMCACHED_SUCCESS
);
700 rc
= memcached_mget(memc
, keys
, key_length
, 3);
701 assert(rc
== MEMCACHED_SUCCESS
);
703 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
704 &return_value_length
, &flags
, &rc
)) != NULL
)
706 assert(return_value
);
708 assert(!return_value
);
709 assert(return_value_length
== 0);
710 assert(rc
== MEMCACHED_NOTFOUND
);
712 for (x
= 0; x
< 3; x
++)
714 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
715 keys
[x
], key_length
[x
],
716 (time_t)50, (uint16_t)9);
717 assert(rc
== MEMCACHED_SUCCESS
);
720 rc
= memcached_mget(memc
, keys
, key_length
, 3);
721 assert(rc
== MEMCACHED_SUCCESS
);
724 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
725 &return_value_length
, &flags
, &rc
)))
727 assert(return_value
);
728 assert(rc
== MEMCACHED_SUCCESS
);
729 assert(return_key_length
== return_value_length
);
730 assert(!memcmp(return_value
, return_key
, return_value_length
));
738 uint8_t get_stats_keys(memcached_st
*memc
)
742 memcached_stat_st stat
;
745 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
746 assert(rc
== MEMCACHED_SUCCESS
);
747 for (ptr
= list
; *ptr
; ptr
++)
748 printf("Found key %s\n", *ptr
);
756 uint8_t get_stats(memcached_st
*memc
)
762 memcached_stat_st
*stat
;
764 stat
= memcached_stat(memc
, NULL
, &rc
);
765 assert(rc
== MEMCACHED_SUCCESS
);
767 assert(rc
== MEMCACHED_SUCCESS
);
770 for (x
= 0; x
< memcached_server_count(memc
); x
++)
772 list
= memcached_stat_get_keys(memc
, &stat
[x
], &rc
);
773 assert(rc
== MEMCACHED_SUCCESS
);
774 for (ptr
= list
; *ptr
; ptr
++)
775 printf("Found key %s\n", *ptr
);
780 memcached_stat_free(NULL
, stat
);
785 uint8_t add_host_test(memcached_st
*memc
)
788 memcached_server_st
*servers
;
790 char servername
[]= "0.example.com";
792 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
794 assert(1 == memcached_server_list_count(servers
));
796 for (x
= 2; x
< 20; x
++)
798 char buffer
[SMALL_STRING_LEN
];
800 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
801 servers
= memcached_server_list_append(servers
, buffer
, 401,
803 assert(rc
== MEMCACHED_SUCCESS
);
804 assert(x
== memcached_server_list_count(servers
));
807 rc
= memcached_server_push(memc
, servers
);
808 assert(rc
== MEMCACHED_SUCCESS
);
809 rc
= memcached_server_push(memc
, servers
);
810 assert(rc
== MEMCACHED_SUCCESS
);
812 memcached_server_list_free(servers
);
817 /* We don't test the behavior itself, we test the switches */
818 uint8_t behavior_test(memcached_st
*memc
)
820 unsigned long long value
;
823 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
824 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
827 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
828 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
831 set
= MEMCACHED_HASH_MD5
;
832 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
833 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
834 assert(value
== MEMCACHED_HASH_MD5
);
838 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
839 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
842 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
843 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
846 set
= MEMCACHED_HASH_DEFAULT
;
847 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
848 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
849 assert(value
== MEMCACHED_HASH_DEFAULT
);
851 set
= MEMCACHED_HASH_CRC
;
852 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &set
);
853 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
854 assert(value
== MEMCACHED_HASH_CRC
);
856 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
859 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
865 /* Test case provided by Cal Haldenbrand */
866 uint8_t user_supplied_bug1(memcached_st
*memc
)
868 unsigned int setter
= 1;
871 unsigned long long total
= 0;
874 char randomstuff
[6 * 1024];
877 memset(randomstuff
, 0, 6 * 1024);
879 /* We just keep looking at the same values over and over */
882 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
883 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
887 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
891 size
= (rand() % ( 5 * 1024 ) ) + 400;
892 memset(randomstuff
, 0, 6 * 1024);
893 assert(size
< 6 * 1024); /* Being safe here */
895 for (j
= 0 ; j
< size
;j
++)
896 randomstuff
[j
] = (char) (rand() % 26) + 97;
899 sprintf(key
, "%d", x
);
900 rc
= memcached_set(memc
, key
, strlen(key
),
901 randomstuff
, strlen(randomstuff
), 10, 0);
902 /* If we fail, lets try again */
903 if (rc
!= MEMCACHED_SUCCESS
)
904 rc
= memcached_set(memc
, key
, strlen(key
),
905 randomstuff
, strlen(randomstuff
), 10, 0);
906 assert(rc
== MEMCACHED_SUCCESS
);
912 /* Test case provided by Cal Haldenbrand */
913 uint8_t user_supplied_bug2(memcached_st
*memc
)
918 unsigned long long total
;
921 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
922 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
924 setter
= 20 * 1024576;
925 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
926 setter
= 20 * 1024576;
927 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
928 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
929 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
931 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
934 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
936 memcached_return rc
= MEMCACHED_SUCCESS
;
937 char buffer
[SMALL_STRING_LEN
];
942 memset(buffer
, 0, SMALL_STRING_LEN
);
944 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
945 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
946 &val_len
, &flags
, &rc
);
947 if (rc
!= MEMCACHED_SUCCESS
)
949 if (rc
== MEMCACHED_NOTFOUND
)
964 /* Do a large mget() over all the keys we think exist */
965 #define KEY_COUNT 3000 // * 1024576
966 uint8_t user_supplied_bug3(memcached_st
*memc
)
972 size_t key_lengths
[KEY_COUNT
];
975 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
976 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
978 setter
= 20 * 1024576;
979 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, &setter
);
980 setter
= 20 * 1024576;
981 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, &setter
);
982 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
983 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
986 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
988 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
989 for (x
= 0; x
< KEY_COUNT
; x
++)
993 snprintf(buffer
, 30, "%u", x
);
994 keys
[x
]= strdup(buffer
);
995 key_lengths
[x
]= strlen(keys
[x
]);
998 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
999 assert(rc
== MEMCACHED_SUCCESS
);
1001 /* Turn this into a help function */
1003 char return_key
[MEMCACHED_MAX_KEY
];
1004 size_t return_key_length
;
1006 size_t return_value_length
;
1009 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1010 &return_value_length
, &flags
, &rc
)))
1012 assert(return_value
);
1013 assert(rc
== MEMCACHED_SUCCESS
);
1018 for (x
= 0; x
< KEY_COUNT
; x
++)
1025 /* Make sure we behave properly if server list has no values */
1026 uint8_t user_supplied_bug4(memcached_st
*memc
)
1028 memcached_return rc
;
1029 char *keys
[]= {"fudge", "son", "food"};
1030 size_t key_length
[]= {5, 3, 4};
1034 /* Here we free everything before running a bunch of mget tests */
1036 memcached_server_list_free(memc
->hosts
);
1038 memc
->number_of_hosts
= 0;
1041 char return_key
[MEMCACHED_MAX_KEY
];
1042 size_t return_key_length
;
1044 size_t return_value_length
;
1046 /* We need to empty the server before continueing test */
1047 rc
= memcached_flush(memc
, 0);
1048 assert(rc
== MEMCACHED_NO_SERVERS
);
1050 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1051 assert(rc
== MEMCACHED_NO_SERVERS
);
1053 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1054 &return_value_length
, &flags
, &rc
)) != NULL
)
1056 assert(return_value
);
1058 assert(!return_value
);
1059 assert(return_value_length
== 0);
1060 assert(rc
== MEMCACHED_NO_SERVERS
);
1062 for (x
= 0; x
< 3; x
++)
1064 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1065 keys
[x
], key_length
[x
],
1066 (time_t)50, (uint16_t)9);
1067 assert(rc
== MEMCACHED_NO_SERVERS
);
1070 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1071 assert(rc
== MEMCACHED_NO_SERVERS
);
1074 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1075 &return_value_length
, &flags
, &rc
)))
1077 assert(return_value
);
1078 assert(rc
== MEMCACHED_SUCCESS
);
1079 assert(return_key_length
== return_value_length
);
1080 assert(!memcmp(return_value
, return_key
, return_value_length
));
1088 #define VALUE_SIZE_BUG5 1048064
1089 uint8_t user_supplied_bug5(memcached_st
*memc
)
1091 memcached_return rc
;
1092 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1093 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1094 char return_key
[MEMCACHED_MAX_KEY
];
1095 size_t return_key_length
;
1097 size_t value_length
;
1101 char insert_data
[VALUE_SIZE_BUG5
];
1103 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1104 insert_data
[x
]= rand();
1106 memcached_flush(memc
, 0);
1107 value
= memcached_get(memc
, keys
[0], key_length
[0],
1108 &value_length
, &flags
, &rc
);
1109 assert(value
== NULL
);
1110 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1113 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1114 &value_length
, &flags
, &rc
)))
1118 for (x
= 0; x
< 4; x
++)
1120 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1121 insert_data
, VALUE_SIZE_BUG5
,
1122 (time_t)0, (uint16_t)0);
1123 assert(rc
== MEMCACHED_SUCCESS
);
1126 for (x
= 0; x
< 10; x
++)
1128 value
= memcached_get(memc
, keys
[0], key_length
[0],
1129 &value_length
, &flags
, &rc
);
1133 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1135 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1136 &value_length
, &flags
, &rc
)))
1147 uint8_t user_supplied_bug6(memcached_st
*memc
)
1149 memcached_return rc
;
1150 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1151 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1152 char return_key
[MEMCACHED_MAX_KEY
];
1153 size_t return_key_length
;
1155 size_t value_length
;
1159 char insert_data
[VALUE_SIZE_BUG5
];
1161 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1162 insert_data
[x
]= rand();
1164 memcached_flush(memc
, 0);
1165 value
= memcached_get(memc
, keys
[0], key_length
[0],
1166 &value_length
, &flags
, &rc
);
1167 assert(value
== NULL
);
1168 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1171 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1172 &value_length
, &flags
, &rc
)))
1176 for (x
= 0; x
< 4; x
++)
1178 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1179 insert_data
, VALUE_SIZE_BUG5
,
1180 (time_t)0, (uint16_t)0);
1181 assert(rc
== MEMCACHED_SUCCESS
);
1184 for (x
= 0; x
< 10; x
++)
1186 value
= memcached_get(memc
, keys
[0], key_length
[0],
1187 &value_length
, &flags
, &rc
);
1191 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1193 /* We test for purge of partial complete fetches */
1194 for (count
= 3; count
; count
--)
1196 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1197 &value_length
, &flags
, &rc
);
1199 assert(rc
== MEMCACHED_SUCCESS
);
1206 /* Test flag store/retrieve */
1207 uint8_t user_supplied_bug7(memcached_st
*memc
)
1209 memcached_return rc
;
1210 char *keys
= "036790384900";
1211 size_t key_length
= strlen("036790384900");
1212 char return_key
[MEMCACHED_MAX_KEY
];
1213 size_t return_key_length
;
1215 size_t value_length
;
1218 char insert_data
[VALUE_SIZE_BUG5
];
1220 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1221 insert_data
[x
]= rand();
1223 memcached_flush(memc
, 0);
1226 rc
= memcached_set(memc
, keys
, key_length
,
1227 insert_data
, VALUE_SIZE_BUG5
,
1229 assert(rc
== MEMCACHED_SUCCESS
);
1232 value
= memcached_get(memc
, keys
, key_length
,
1233 &value_length
, &flags
, &rc
);
1234 assert(flags
== 245);
1238 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1241 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1242 &value_length
, &flags
, &rc
);
1243 assert(flags
== 245);
1251 uint8_t result_static(memcached_st
*memc
)
1253 memcached_result_st result
;
1254 memcached_result_st
*result_ptr
;
1256 result_ptr
= memcached_result_create(memc
, &result
);
1257 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1259 memcached_result_free(&result
);
1264 uint8_t result_alloc(memcached_st
*memc
)
1266 memcached_result_st
*result
;
1268 result
= memcached_result_create(memc
, NULL
);
1270 memcached_result_free(result
);
1275 uint8_t string_static_null(memcached_st
*memc
)
1277 memcached_string_st string
;
1278 memcached_string_st
*string_ptr
;
1280 string_ptr
= memcached_string_create(memc
, &string
, 0);
1281 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1283 memcached_string_free(&string
);
1288 uint8_t string_alloc_null(memcached_st
*memc
)
1290 memcached_string_st
*string
;
1292 string
= memcached_string_create(memc
, NULL
, 0);
1294 memcached_string_free(string
);
1299 uint8_t string_alloc_with_size(memcached_st
*memc
)
1301 memcached_string_st
*string
;
1303 string
= memcached_string_create(memc
, NULL
, 1024);
1305 memcached_string_free(string
);
1310 uint8_t string_alloc_with_size_toobig(memcached_st
*memc
)
1312 memcached_string_st
*string
;
1314 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
1315 assert(string
== NULL
);
1320 uint8_t string_alloc_append(memcached_st
*memc
)
1323 char buffer
[SMALL_STRING_LEN
];
1324 memcached_string_st
*string
;
1326 /* Ring the bell! */
1327 memset(buffer
, 6, SMALL_STRING_LEN
);
1329 string
= memcached_string_create(memc
, NULL
, 100);
1332 for (x
= 0; x
< 1024; x
++)
1334 memcached_return rc
;
1335 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1336 assert(rc
== MEMCACHED_SUCCESS
);
1338 memcached_string_free(string
);
1343 uint8_t string_alloc_append_toobig(memcached_st
*memc
)
1345 memcached_return rc
;
1347 char buffer
[SMALL_STRING_LEN
];
1348 memcached_string_st
*string
;
1350 /* Ring the bell! */
1351 memset(buffer
, 6, SMALL_STRING_LEN
);
1353 string
= memcached_string_create(memc
, NULL
, 100);
1356 for (x
= 0; x
< 1024; x
++)
1358 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1359 assert(rc
== MEMCACHED_SUCCESS
);
1361 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
1362 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
1363 memcached_string_free(string
);
1368 uint8_t generate_data(memcached_st
*memc
)
1370 unsigned long long x
;
1371 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1372 execute_set(memc
, global_pairs
, GLOBAL_COUNT
);
1374 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1376 global_keys
[x
]= global_pairs
[x
].key
;
1377 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1383 uint8_t get_read(memcached_st
*memc
)
1386 memcached_return rc
;
1390 size_t return_value_length
;
1393 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1395 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
1396 &return_value_length
, &flags
, &rc
);
1398 assert(return_value);
1399 assert(rc == MEMCACHED_SUCCESS);
1401 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
1409 uint8_t mget_read(memcached_st
*memc
)
1411 memcached_return rc
;
1413 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1414 assert(rc
== MEMCACHED_SUCCESS
);
1415 /* Turn this into a help function */
1417 char return_key
[MEMCACHED_MAX_KEY
];
1418 size_t return_key_length
;
1420 size_t return_value_length
;
1423 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1424 &return_value_length
, &flags
, &rc
)))
1426 assert(return_value
);
1427 assert(rc
== MEMCACHED_SUCCESS
);
1435 uint8_t mget_read_result(memcached_st
*memc
)
1437 memcached_return rc
;
1439 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1440 assert(rc
== MEMCACHED_SUCCESS
);
1441 /* Turn this into a help function */
1443 memcached_result_st results_obj
;
1444 memcached_result_st
*results
;
1446 results
= memcached_result_create(memc
, &results_obj
);
1448 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1451 assert(rc
== MEMCACHED_SUCCESS
);
1454 memcached_result_free(&results_obj
);
1460 uint8_t free_data(memcached_st
*memc
)
1462 pairs_free(global_pairs
);
1467 uint8_t add_host_test1(memcached_st
*memc
)
1470 memcached_return rc
;
1471 char servername
[]= "0.example.com";
1472 memcached_server_st
*servers
;
1474 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1476 assert(1 == memcached_server_list_count(servers
));
1478 for (x
= 2; x
< 20; x
++)
1480 char buffer
[SMALL_STRING_LEN
];
1482 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1483 servers
= memcached_server_list_append(servers
, buffer
, 401,
1485 assert(rc
== MEMCACHED_SUCCESS
);
1486 assert(x
== memcached_server_list_count(servers
));
1489 rc
= memcached_server_push(memc
, servers
);
1490 assert(rc
== MEMCACHED_SUCCESS
);
1491 rc
= memcached_server_push(memc
, servers
);
1492 assert(rc
== MEMCACHED_SUCCESS
);
1494 memcached_server_list_free(servers
);
1499 memcached_return
pre_nonblock(memcached_st
*memc
)
1501 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1503 return MEMCACHED_SUCCESS
;
1506 memcached_return
pre_md5(memcached_st
*memc
)
1508 memcached_hash value
= MEMCACHED_HASH_MD5
;
1509 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1511 return MEMCACHED_SUCCESS
;
1514 memcached_return
pre_crc(memcached_st
*memc
)
1516 memcached_hash value
= MEMCACHED_HASH_CRC
;
1517 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1519 return MEMCACHED_SUCCESS
;
1522 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
1524 memcached_hash value
= MEMCACHED_HASH_FNV1_64
;
1525 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1527 return MEMCACHED_SUCCESS
;
1530 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
1532 memcached_hash value
= MEMCACHED_HASH_FNV1A_64
;
1533 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1535 return MEMCACHED_SUCCESS
;
1538 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
1540 memcached_hash value
= MEMCACHED_HASH_FNV1_32
;
1541 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1543 return MEMCACHED_SUCCESS
;
1546 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
1548 memcached_hash value
= MEMCACHED_HASH_FNV1A_32
;
1549 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1551 return MEMCACHED_SUCCESS
;
1554 memcached_return
pre_hash_ketama(memcached_st
*memc
)
1556 memcached_hash value
= MEMCACHED_HASH_KETAMA
;
1557 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1559 return MEMCACHED_SUCCESS
;
1562 memcached_return
check_for_1_2_3(memcached_st
*memc
)
1564 memcached_version(memc
);
1566 if (memc
->hosts
[0].major_version
>= 1 &&
1567 memc
->hosts
[0].minor_version
>= 2 &&
1568 memc
->hosts
[0].micro_version
>= 4)
1569 return MEMCACHED_SUCCESS
;
1571 return MEMCACHED_FAILURE
;
1574 memcached_return
pre_unix_socket(memcached_st
*memc
)
1576 memcached_return rc
;
1579 memcached_server_list_free(memc
->hosts
);
1581 memc
->number_of_hosts
= 0;
1583 if (stat("/tmp/memcached.socket", &buf
))
1584 return MEMCACHED_FAILURE
;
1586 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
1591 memcached_return
pre_udp(memcached_st
*memc
)
1593 memcached_return rc
;
1595 memcached_server_list_free(memc
->hosts
);
1597 memc
->number_of_hosts
= 0;
1600 return MEMCACHED_FAILURE
;
1602 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
1607 memcached_return
pre_nodelay(memcached_st
*memc
)
1609 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1610 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
1612 return MEMCACHED_SUCCESS
;
1616 /* Clean the server before beginning testing */
1618 {"flush", 0, flush_test
},
1619 {"init", 0, init_test
},
1620 {"allocation", 0, allocation_test
},
1621 {"clone_test", 0, clone_test
},
1622 {"error", 0, error_test
},
1623 {"set", 0, set_test
},
1624 {"set2", 0, set_test2
},
1625 {"set3", 0, set_test3
},
1626 {"add", 0, add_test
},
1627 {"replace", 0, replace_test
},
1628 {"delete", 1, delete_test
},
1629 {"get", 1, get_test
},
1630 {"get2", 0, get_test2
},
1631 {"get3", 0, get_test3
},
1632 {"get4", 0, get_test4
},
1633 {"stats_servername", 0, stats_servername_test
},
1634 {"increment", 0, increment_test
},
1635 {"decrement", 0, decrement_test
},
1636 {"quit", 0, quit_test
},
1637 {"mget", 1, mget_test
},
1638 {"mget_result", 1, mget_result_test
},
1639 {"mget_result_alloc", 1, mget_result_alloc_test
},
1640 {"get_stats", 0, get_stats
},
1641 {"add_host_test", 0, add_host_test
},
1642 {"get_stats_keys", 0, get_stats_keys
},
1643 {"behavior_test", 0, get_stats_keys
},
1647 test_st string_tests
[] ={
1648 {"string static with null", 0, string_static_null
},
1649 {"string alloc with null", 0, string_alloc_null
},
1650 {"string alloc with 1K", 0, string_alloc_with_size
},
1651 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
1652 {"string append", 0, string_alloc_append
},
1653 {"string append failure (too big)", 0, string_alloc_append_toobig
},
1657 test_st result_tests
[] ={
1658 {"result static", 0, result_static
},
1659 {"result alloc", 0, result_alloc
},
1663 test_st version_1_2_3
[] ={
1664 {"append", 0, append_test
},
1665 {"prepend", 0, prepend_test
},
1666 // {"cas", 0, cas_test },
1667 {"append_binary", 0, append_binary_test
},
1671 test_st user_tests
[] ={
1672 {"user_supplied_bug1", 0, user_supplied_bug1
},
1673 {"user_supplied_bug2", 0, user_supplied_bug2
},
1674 {"user_supplied_bug3", 0, user_supplied_bug3
},
1675 {"user_supplied_bug4", 0, user_supplied_bug4
},
1676 {"user_supplied_bug5", 1, user_supplied_bug5
},
1677 {"user_supplied_bug6", 1, user_supplied_bug6
},
1678 {"user_supplied_bug7", 1, user_supplied_bug7
},
1682 test_st generate_tests
[] ={
1683 {"generate_data", 0, generate_data
},
1684 {"get_read", 0, get_read
},
1685 {"mget_read", 0, mget_read
},
1686 {"mget_read_result", 0, mget_read_result
},
1691 collection_st collection
[] ={
1692 {"block", 0, 0, tests
},
1693 {"nonblock", pre_nonblock
, 0, tests
},
1694 {"nodelay", pre_nodelay
, 0, tests
},
1695 {"md5", pre_md5
, 0, tests
},
1696 {"crc", pre_crc
, 0, tests
},
1697 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
1698 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
1699 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
1700 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
1701 {"ketama", pre_hash_ketama
, 0, tests
},
1702 {"unix_socket", pre_unix_socket
, 0, tests
},
1703 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
1704 // {"udp", pre_udp, 0, tests},
1705 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
1706 {"string", 0, 0, string_tests
},
1707 {"result", 0, 0, result_tests
},
1708 {"user", 0, 0, user_tests
},
1709 {"generate", 0, 0, generate_tests
},
1710 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
1714 collection_st
*gets_collections(void)