2 Sample test application.
14 #include "../clients/generator.h"
15 #include "../clients/execute.h"
18 #define INT64_MAX LONG_MAX
21 #define INT32_MAX INT_MAX
27 #define GLOBAL_COUNT 10000
28 #define GLOBAL2_COUNT 100
29 #define SERVERS_TO_CREATE 5
30 static uint32_t global_count
;
32 static pairs_st
*global_pairs
;
33 static char *global_keys
[GLOBAL_COUNT
];
34 static size_t global_keys_length
[GLOBAL_COUNT
];
36 test_return
init_test(memcached_st
*not_used
)
40 (void)memcached_create(&memc
);
41 memcached_free(&memc
);
46 test_return
server_list_null_test(memcached_st
*ptr
)
48 memcached_server_st
*server_list
;
51 server_list
= memcached_server_list_append(NULL
, NULL
, 0, NULL
);
52 assert(server_list
== NULL
);
54 server_list
= memcached_server_list_append(NULL
, "localhost", 0, NULL
);
55 assert(server_list
== NULL
);
57 server_list
= memcached_server_list_append(NULL
, NULL
, 0, &rc
);
58 assert(server_list
== NULL
);
63 #define TEST_PORT_COUNT 7
64 uint32_t test_ports
[TEST_PORT_COUNT
];
66 memcached_return
server_display_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
69 uint32_t bigger
= *((uint32_t *)(context
));
70 assert(bigger
<= server
->port
);
71 *((uint32_t *)(context
))= server
->port
;
73 return MEMCACHED_SUCCESS
;
76 test_return
server_sort_test(memcached_st
*ptr
)
79 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
81 memcached_server_function callbacks
[1];
82 memcached_st
*local_memc
;
84 local_memc
= memcached_create(NULL
);
86 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
88 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
90 test_ports
[x
]= random() % 64000;
91 rc
= memcached_server_add(local_memc
, "localhost", test_ports
[x
]);
92 assert(local_memc
->number_of_hosts
== x
+1);
93 assert(local_memc
->hosts
[0].count
== x
+1);
94 assert(rc
== MEMCACHED_SUCCESS
);
97 callbacks
[0]= server_display_function
;
98 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
101 memcached_free(local_memc
);
106 test_return
server_sort2_test(memcached_st
*ptr
)
108 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
110 memcached_server_function callbacks
[1];
111 memcached_st
*local_memc
;
113 local_memc
= memcached_create(NULL
);
115 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
116 assert(rc
== MEMCACHED_SUCCESS
);
118 rc
= memcached_server_add(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043);
119 assert(rc
== MEMCACHED_SUCCESS
);
120 assert(local_memc
->hosts
[0].port
== 43043);
122 rc
= memcached_server_add(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042);
123 assert(rc
== MEMCACHED_SUCCESS
);
124 assert(local_memc
->hosts
[0].port
== 43042);
125 assert(local_memc
->hosts
[1].port
== 43043);
127 callbacks
[0]= server_display_function
;
128 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
131 memcached_free(local_memc
);
136 memcached_return
server_display_unsort_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
139 uint32_t x
= *((uint32_t *)(context
));
141 assert(test_ports
[x
] == server
->port
);
142 *((uint32_t *)(context
))= ++x
;
144 return MEMCACHED_SUCCESS
;
147 test_return
server_unsort_test(memcached_st
*ptr
)
150 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
151 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
153 memcached_server_function callbacks
[1];
154 memcached_st
*local_memc
;
156 local_memc
= memcached_create(NULL
);
159 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
161 test_ports
[x
]= random() % 64000;
162 rc
= memcached_server_add(local_memc
, "localhost", test_ports
[x
]);
163 assert(local_memc
->number_of_hosts
== x
+1);
164 assert(local_memc
->hosts
[0].count
== x
+1);
165 assert(rc
== MEMCACHED_SUCCESS
);
168 callbacks
[0]= server_display_unsort_function
;
169 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
171 /* Now we sort old data! */
172 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
173 callbacks
[0]= server_display_function
;
174 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
177 memcached_free(local_memc
);
182 test_return
allocation_test(memcached_st
*not_used
)
185 memc
= memcached_create(NULL
);
187 memcached_free(memc
);
192 test_return
clone_test(memcached_st
*memc
)
197 clone
= memcached_clone(NULL
, NULL
);
199 memcached_free(clone
);
202 /* Can we init from null? */
205 clone
= memcached_clone(NULL
, memc
);
207 memcached_free(clone
);
210 /* Can we init from struct? */
212 memcached_st declared_clone
;
214 clone
= memcached_clone(&declared_clone
, NULL
);
216 memcached_free(clone
);
219 /* Can we init from struct? */
221 memcached_st declared_clone
;
223 clone
= memcached_clone(&declared_clone
, memc
);
225 memcached_free(clone
);
231 test_return
connection_test(memcached_st
*memc
)
235 rc
= memcached_server_add(memc
, "localhost", 0);
236 assert(rc
== MEMCACHED_SUCCESS
);
241 test_return
error_test(memcached_st
*memc
)
245 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
247 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
253 test_return
set_test(memcached_st
*memc
)
257 char *value
= "when we sanitize";
259 rc
= memcached_set(memc
, key
, strlen(key
),
260 value
, strlen(value
),
261 (time_t)0, (uint32_t)0);
262 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
267 test_return
append_test(memcached_st
*memc
)
275 rc
= memcached_flush(memc
, 0);
276 assert(rc
== MEMCACHED_SUCCESS
);
278 rc
= memcached_set(memc
, key
, strlen(key
),
279 value
, strlen(value
),
280 (time_t)0, (uint32_t)0);
281 assert(rc
== MEMCACHED_SUCCESS
);
283 rc
= memcached_append(memc
, key
, strlen(key
),
284 " the", strlen(" the"),
285 (time_t)0, (uint32_t)0);
286 assert(rc
== MEMCACHED_SUCCESS
);
288 rc
= memcached_append(memc
, key
, strlen(key
),
289 " people", strlen(" people"),
290 (time_t)0, (uint32_t)0);
291 assert(rc
== MEMCACHED_SUCCESS
);
293 value
= memcached_get(memc
, key
, strlen(key
),
294 &value_length
, &flags
, &rc
);
295 assert(!memcmp(value
, "we the people", strlen("we the people")));
296 assert(strlen("we the people") == value_length
);
297 assert(rc
== MEMCACHED_SUCCESS
);
303 test_return
append_binary_test(memcached_st
*memc
)
306 char *key
= "numbers";
307 unsigned int *store_ptr
;
308 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
314 rc
= memcached_flush(memc
, 0);
315 assert(rc
== MEMCACHED_SUCCESS
);
317 rc
= memcached_set(memc
,
320 (time_t)0, (uint32_t)0);
321 assert(rc
== MEMCACHED_SUCCESS
);
323 for (x
= 0; store_list
[x
] ; x
++)
325 rc
= memcached_append(memc
,
327 (char *)&store_list
[x
], sizeof(unsigned int),
328 (time_t)0, (uint32_t)0);
329 assert(rc
== MEMCACHED_SUCCESS
);
332 value
= memcached_get(memc
, key
, strlen(key
),
333 &value_length
, &flags
, &rc
);
334 assert((value_length
== (sizeof(unsigned int) * x
)));
335 assert(rc
== MEMCACHED_SUCCESS
);
337 store_ptr
= (unsigned int *)value
;
339 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
341 assert(*store_ptr
== store_list
[x
++]);
349 test_return
cas2_test(memcached_st
*memc
)
352 char *keys
[]= {"fudge", "son", "food"};
353 size_t key_length
[]= {5, 3, 4};
354 char *value
= "we the people";
355 size_t value_length
= strlen("we the people");
357 memcached_result_st results_obj
;
358 memcached_result_st
*results
;
361 rc
= memcached_flush(memc
, 0);
362 assert(rc
== MEMCACHED_SUCCESS
);
364 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
366 for (x
= 0; x
< 3; x
++)
368 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
369 keys
[x
], key_length
[x
],
370 (time_t)50, (uint32_t)9);
371 assert(rc
== MEMCACHED_SUCCESS
);
374 rc
= memcached_mget(memc
, keys
, key_length
, 3);
376 results
= memcached_result_create(memc
, &results_obj
);
378 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
380 assert(results
->cas
);
381 assert(rc
== MEMCACHED_SUCCESS
);
382 WATCHPOINT_ASSERT(memcached_result_cas(results
));
384 assert(!memcmp(value
, "we the people", strlen("we the people")));
385 assert(strlen("we the people") == value_length
);
386 assert(rc
== MEMCACHED_SUCCESS
);
388 memcached_result_free(&results_obj
);
393 test_return
cas_test(memcached_st
*memc
)
397 size_t key_length
= strlen("fun");
398 char *value
= "we the people";
399 size_t value_length
= strlen("we the people");
400 memcached_result_st results_obj
;
401 memcached_result_st
*results
;
404 rc
= memcached_flush(memc
, 0);
405 assert(rc
== MEMCACHED_SUCCESS
);
407 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
409 rc
= memcached_set(memc
, key
, strlen(key
),
410 value
, strlen(value
),
411 (time_t)0, (uint32_t)0);
412 assert(rc
== MEMCACHED_SUCCESS
);
414 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
416 results
= memcached_result_create(memc
, &results_obj
);
418 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
420 assert(rc
== MEMCACHED_SUCCESS
);
421 WATCHPOINT_ASSERT(memcached_result_cas(results
));
423 assert(!memcmp(value
, "we the people", strlen("we the people")));
424 assert(strlen("we the people") == value_length
);
425 assert(rc
== MEMCACHED_SUCCESS
);
427 rc
= memcached_cas(memc
, key
, key_length
,
428 "change the value", strlen("change the value"),
429 0, 0, memcached_result_cas(results
));
431 assert(rc
== MEMCACHED_SUCCESS
);
433 rc
= memcached_cas(memc
, key
, key_length
,
434 "change the value", strlen("change the value"),
437 assert(rc
== MEMCACHED_DATA_EXISTS
);
440 memcached_result_free(&results_obj
);
445 test_return
prepend_test(memcached_st
*memc
)
449 char *value
= "people";
453 rc
= memcached_flush(memc
, 0);
454 assert(rc
== MEMCACHED_SUCCESS
);
456 rc
= memcached_set(memc
, key
, strlen(key
),
457 value
, strlen(value
),
458 (time_t)0, (uint32_t)0);
459 assert(rc
== MEMCACHED_SUCCESS
);
461 rc
= memcached_prepend(memc
, key
, strlen(key
),
462 "the ", strlen("the "),
463 (time_t)0, (uint32_t)0);
464 assert(rc
== MEMCACHED_SUCCESS
);
466 rc
= memcached_prepend(memc
, key
, strlen(key
),
467 "we ", strlen("we "),
468 (time_t)0, (uint32_t)0);
469 assert(rc
== MEMCACHED_SUCCESS
);
471 value
= memcached_get(memc
, key
, strlen(key
),
472 &value_length
, &flags
, &rc
);
473 assert(!memcmp(value
, "we the people", strlen("we the people")));
474 assert(strlen("we the people") == value_length
);
475 assert(rc
== MEMCACHED_SUCCESS
);
482 Set the value, then quit to make sure it is flushed.
483 Come back in and test that add fails.
485 test_return
add_test(memcached_st
*memc
)
489 char *value
= "when we sanitize";
490 unsigned long long setting_value
;
492 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
494 rc
= memcached_set(memc
, key
, strlen(key
),
495 value
, strlen(value
),
496 (time_t)0, (uint32_t)0);
497 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
498 memcached_quit(memc
);
499 rc
= memcached_add(memc
, key
, strlen(key
),
500 value
, strlen(value
),
501 (time_t)0, (uint32_t)0);
503 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
505 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
507 assert(rc
== MEMCACHED_NOTSTORED
);
512 test_return
add_wrapper(memcached_st
*memc
)
516 for (x
= 0; x
< 10000; x
++)
522 test_return
replace_test(memcached_st
*memc
)
526 char *value
= "when we sanitize";
527 char *original
= "first we insert some data";
529 rc
= memcached_set(memc
, key
, strlen(key
),
530 original
, strlen(original
),
531 (time_t)0, (uint32_t)0);
532 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
534 rc
= memcached_replace(memc
, key
, strlen(key
),
535 value
, strlen(value
),
536 (time_t)0, (uint32_t)0);
537 assert(rc
== MEMCACHED_SUCCESS
);
542 test_return
delete_test(memcached_st
*memc
)
546 char *value
= "when we sanitize";
548 rc
= memcached_set(memc
, key
, strlen(key
),
549 value
, strlen(value
),
550 (time_t)0, (uint32_t)0);
551 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
553 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
554 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
559 test_return
flush_test(memcached_st
*memc
)
563 rc
= memcached_flush(memc
, 0);
564 assert(rc
== MEMCACHED_SUCCESS
);
569 memcached_return
server_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
573 return MEMCACHED_SUCCESS
;
576 test_return
memcached_server_cursor_test(memcached_st
*memc
)
578 char *context
= "foo bad";
579 memcached_server_function callbacks
[1];
581 callbacks
[0]= server_function
;
582 memcached_server_cursor(memc
, callbacks
, context
, 1);
587 test_return
bad_key_test(memcached_st
*memc
)
590 char *key
= "foo bad";
592 size_t string_length
;
597 clone
= memcached_clone(NULL
, memc
);
600 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
601 assert(rc
== MEMCACHED_SUCCESS
);
603 string
= memcached_get(clone
, key
, strlen(key
),
604 &string_length
, &flags
, &rc
);
605 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
606 assert(string_length
== 0);
610 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
611 assert(rc
== MEMCACHED_SUCCESS
);
612 string
= memcached_get(clone
, key
, strlen(key
),
613 &string_length
, &flags
, &rc
);
614 assert(rc
== MEMCACHED_NOTFOUND
);
615 assert(string_length
== 0);
618 /* Test multi key for bad keys */
620 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
621 size_t key_lengths
[] = { 7, 7, 7 };
623 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
624 assert(rc
== MEMCACHED_SUCCESS
);
626 rc
= memcached_mget(clone
, keys
, key_lengths
, 3);
627 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
629 rc
= memcached_mget_by_key(clone
, "foo daddy", 9, keys
, key_lengths
, 1);
630 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
633 memcached_free(clone
);
638 #define READ_THROUGH_VALUE "set for me"
639 memcached_return
read_through_trigger(memcached_st
*memc
,
640 char *key
, size_t key_length
,
641 memcached_result_st
*result
)
644 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
647 test_return
read_through(memcached_st
*memc
)
652 size_t string_length
;
655 string
= memcached_get(memc
, key
, strlen(key
),
656 &string_length
, &flags
, &rc
);
658 assert(rc
== MEMCACHED_NOTFOUND
);
659 assert(string_length
== 0);
662 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, read_through_trigger
);
663 assert(rc
== MEMCACHED_SUCCESS
);
665 string
= memcached_get(memc
, key
, strlen(key
),
666 &string_length
, &flags
, &rc
);
668 assert(rc
== MEMCACHED_SUCCESS
);
669 assert(string_length
== strlen(READ_THROUGH_VALUE
));
670 assert(!strcmp(READ_THROUGH_VALUE
, string
));
673 string
= memcached_get(memc
, key
, strlen(key
),
674 &string_length
, &flags
, &rc
);
676 assert(rc
== MEMCACHED_SUCCESS
);
677 assert(string_length
== strlen(READ_THROUGH_VALUE
));
678 assert(!strcmp(READ_THROUGH_VALUE
, string
));
684 memcached_return
delete_trigger(memcached_st
*ptr
, const char *key
, size_t key_length
)
688 return MEMCACHED_SUCCESS
;
691 test_return
delete_through(memcached_st
*memc
)
693 memcached_trigger_delete_key callback
;
696 callback
= delete_trigger
;
698 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, callback
);
699 assert(rc
== MEMCACHED_SUCCESS
);
704 test_return
get_test(memcached_st
*memc
)
709 size_t string_length
;
712 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
713 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
715 string
= memcached_get(memc
, key
, strlen(key
),
716 &string_length
, &flags
, &rc
);
718 assert(rc
== MEMCACHED_NOTFOUND
);
719 assert(string_length
== 0);
725 test_return
get_test2(memcached_st
*memc
)
729 char *value
= "when we sanitize";
731 size_t string_length
;
734 rc
= memcached_set(memc
, key
, strlen(key
),
735 value
, strlen(value
),
736 (time_t)0, (uint32_t)0);
737 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
739 string
= memcached_get(memc
, key
, strlen(key
),
740 &string_length
, &flags
, &rc
);
743 assert(rc
== MEMCACHED_SUCCESS
);
744 assert(string_length
== strlen(value
));
745 assert(!memcmp(string
, value
, string_length
));
752 test_return
set_test2(memcached_st
*memc
)
756 char *value
= "train in the brain";
757 size_t value_length
= strlen(value
);
760 for (x
= 0; x
< 10; x
++)
762 rc
= memcached_set(memc
, key
, strlen(key
),
764 (time_t)0, (uint32_t)0);
765 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
771 test_return
set_test3(memcached_st
*memc
)
776 size_t value_length
= 8191;
779 value
= (char*)malloc(value_length
);
782 for (x
= 0; x
< value_length
; x
++)
783 value
[x
] = (char) (x
% 127);
785 for (x
= 0; x
< 1; x
++)
787 rc
= memcached_set(memc
, key
, strlen(key
),
789 (time_t)0, (uint32_t)0);
790 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
798 test_return
get_test3(memcached_st
*memc
)
803 size_t value_length
= 8191;
805 size_t string_length
;
809 value
= (char*)malloc(value_length
);
812 for (x
= 0; x
< value_length
; x
++)
813 value
[x
] = (char) (x
% 127);
815 rc
= memcached_set(memc
, key
, strlen(key
),
817 (time_t)0, (uint32_t)0);
818 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
820 string
= memcached_get(memc
, key
, strlen(key
),
821 &string_length
, &flags
, &rc
);
823 assert(rc
== MEMCACHED_SUCCESS
);
825 assert(string_length
== value_length
);
826 assert(!memcmp(string
, value
, string_length
));
834 test_return
get_test4(memcached_st
*memc
)
839 size_t value_length
= 8191;
841 size_t string_length
;
845 value
= (char*)malloc(value_length
);
848 for (x
= 0; x
< value_length
; x
++)
849 value
[x
] = (char) (x
% 127);
851 rc
= memcached_set(memc
, key
, strlen(key
),
853 (time_t)0, (uint32_t)0);
854 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
856 for (x
= 0; x
< 10; x
++)
858 string
= memcached_get(memc
, key
, strlen(key
),
859 &string_length
, &flags
, &rc
);
861 assert(rc
== MEMCACHED_SUCCESS
);
863 assert(string_length
== value_length
);
864 assert(!memcmp(string
, value
, string_length
));
873 /* Do not copy the style of this code, I just access hosts to testthis function */
874 test_return
stats_servername_test(memcached_st
*memc
)
877 memcached_stat_st stat
;
878 rc
= memcached_stat_servername(&stat
, NULL
,
879 memc
->hosts
[0].hostname
,
880 memc
->hosts
[0].port
);
885 test_return
increment_test(memcached_st
*memc
)
892 rc
= memcached_set(memc
, key
, strlen(key
),
893 value
, strlen(value
),
894 (time_t)0, (uint32_t)0);
895 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
897 rc
= memcached_increment(memc
, key
, strlen(key
),
899 assert(rc
== MEMCACHED_SUCCESS
);
900 assert(new_number
== 1);
902 rc
= memcached_increment(memc
, key
, strlen(key
),
904 assert(rc
== MEMCACHED_SUCCESS
);
905 assert(new_number
== 2);
910 test_return
decrement_test(memcached_st
*memc
)
917 rc
= memcached_set(memc
, key
, strlen(key
),
918 value
, strlen(value
),
919 (time_t)0, (uint32_t)0);
920 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
922 rc
= memcached_decrement(memc
, key
, strlen(key
),
924 assert(rc
== MEMCACHED_SUCCESS
);
925 assert(new_number
== 2);
927 rc
= memcached_decrement(memc
, key
, strlen(key
),
929 assert(rc
== MEMCACHED_SUCCESS
);
930 assert(new_number
== 1);
935 test_return
quit_test(memcached_st
*memc
)
939 char *value
= "sanford and sun";
941 rc
= memcached_set(memc
, key
, strlen(key
),
942 value
, strlen(value
),
943 (time_t)10, (uint32_t)3);
944 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
945 memcached_quit(memc
);
947 rc
= memcached_set(memc
, key
, strlen(key
),
948 value
, strlen(value
),
949 (time_t)50, (uint32_t)9);
950 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
955 test_return
mget_result_test(memcached_st
*memc
)
958 char *keys
[]= {"fudge", "son", "food"};
959 size_t key_length
[]= {5, 3, 4};
962 memcached_result_st results_obj
;
963 memcached_result_st
*results
;
965 results
= memcached_result_create(memc
, &results_obj
);
967 assert(&results_obj
== results
);
969 /* We need to empty the server before continueing test */
970 rc
= memcached_flush(memc
, 0);
971 assert(rc
== MEMCACHED_SUCCESS
);
973 rc
= memcached_mget(memc
, keys
, key_length
, 3);
974 assert(rc
== MEMCACHED_SUCCESS
);
976 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
981 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
983 assert(rc
== MEMCACHED_END
);
985 for (x
= 0; x
< 3; x
++)
987 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
988 keys
[x
], key_length
[x
],
989 (time_t)50, (uint32_t)9);
990 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
993 rc
= memcached_mget(memc
, keys
, key_length
, 3);
994 assert(rc
== MEMCACHED_SUCCESS
);
996 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
999 assert(&results_obj
== results
);
1000 assert(rc
== MEMCACHED_SUCCESS
);
1001 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1002 assert(!memcmp(memcached_result_key_value(results
),
1003 memcached_result_value(results
),
1004 memcached_result_length(results
)));
1007 memcached_result_free(&results_obj
);
1012 test_return
mget_result_alloc_test(memcached_st
*memc
)
1014 memcached_return rc
;
1015 char *keys
[]= {"fudge", "son", "food"};
1016 size_t key_length
[]= {5, 3, 4};
1019 memcached_result_st
*results
;
1021 /* We need to empty the server before continueing test */
1022 rc
= memcached_flush(memc
, 0);
1023 assert(rc
== MEMCACHED_SUCCESS
);
1025 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1026 assert(rc
== MEMCACHED_SUCCESS
);
1028 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1033 assert(rc
== MEMCACHED_END
);
1035 for (x
= 0; x
< 3; x
++)
1037 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1038 keys
[x
], key_length
[x
],
1039 (time_t)50, (uint32_t)9);
1040 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1043 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1044 assert(rc
== MEMCACHED_SUCCESS
);
1047 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1050 assert(rc
== MEMCACHED_SUCCESS
);
1051 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1052 assert(!memcmp(memcached_result_key_value(results
),
1053 memcached_result_value(results
),
1054 memcached_result_length(results
)));
1055 memcached_result_free(results
);
1062 /* Count the results */
1063 unsigned int callback_counter(memcached_st
*ptr
, memcached_result_st
*result
, void *context
)
1065 unsigned int *counter
= (unsigned int *)context
;
1067 *counter
= *counter
+ 1;
1072 test_return
mget_result_function(memcached_st
*memc
)
1074 memcached_return rc
;
1075 char *keys
[]= {"fudge", "son", "food"};
1076 size_t key_length
[]= {5, 3, 4};
1078 unsigned int counter
;
1079 memcached_execute_function callbacks
[1];
1081 /* We need to empty the server before continueing test */
1082 rc
= memcached_flush(memc
, 0);
1083 for (x
= 0; x
< 3; x
++)
1085 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1086 keys
[x
], key_length
[x
],
1087 (time_t)50, (uint32_t)9);
1088 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1091 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1092 assert(rc
== MEMCACHED_SUCCESS
);
1094 callbacks
[0]= &callback_counter
;
1096 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1098 assert(counter
== 3);
1103 test_return
mget_test(memcached_st
*memc
)
1105 memcached_return rc
;
1106 char *keys
[]= {"fudge", "son", "food"};
1107 size_t key_length
[]= {5, 3, 4};
1111 char return_key
[MEMCACHED_MAX_KEY
];
1112 size_t return_key_length
;
1114 size_t return_value_length
;
1116 /* We need to empty the server before continueing test */
1117 rc
= memcached_flush(memc
, 0);
1118 assert(rc
== MEMCACHED_SUCCESS
);
1120 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1121 assert(rc
== MEMCACHED_SUCCESS
);
1123 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1124 &return_value_length
, &flags
, &rc
)) != NULL
)
1126 assert(return_value
);
1128 assert(!return_value
);
1129 assert(return_value_length
== 0);
1130 assert(rc
== MEMCACHED_END
);
1132 for (x
= 0; x
< 3; x
++)
1134 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1135 keys
[x
], key_length
[x
],
1136 (time_t)50, (uint32_t)9);
1137 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1140 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1141 assert(rc
== MEMCACHED_SUCCESS
);
1144 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1145 &return_value_length
, &flags
, &rc
)))
1147 assert(return_value
);
1148 assert(rc
== MEMCACHED_SUCCESS
);
1149 assert(return_key_length
== return_value_length
);
1150 assert(!memcmp(return_value
, return_key
, return_value_length
));
1158 test_return
get_stats_keys(memcached_st
*memc
)
1162 memcached_stat_st stat
;
1163 memcached_return rc
;
1165 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
1166 assert(rc
== MEMCACHED_SUCCESS
);
1167 for (ptr
= list
; *ptr
; ptr
++)
1176 test_return
version_string_test(memcached_st
*memc
)
1178 const char *version_string
;
1180 version_string
= memcached_lib_version();
1182 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1187 test_return
get_stats(memcached_st
*memc
)
1192 memcached_return rc
;
1193 memcached_stat_st
*stat
;
1195 stat
= memcached_stat(memc
, NULL
, &rc
);
1196 assert(rc
== MEMCACHED_SUCCESS
);
1198 assert(rc
== MEMCACHED_SUCCESS
);
1201 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1203 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
1204 assert(rc
== MEMCACHED_SUCCESS
);
1205 for (ptr
= list
; *ptr
; ptr
++);
1210 memcached_stat_free(NULL
, stat
);
1215 test_return
add_host_test(memcached_st
*memc
)
1218 memcached_server_st
*servers
;
1219 memcached_return rc
;
1220 char servername
[]= "0.example.com";
1222 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1224 assert(1 == memcached_server_list_count(servers
));
1226 for (x
= 2; x
< 20; x
++)
1228 char buffer
[SMALL_STRING_LEN
];
1230 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1231 servers
= memcached_server_list_append(servers
, buffer
, 401,
1233 assert(rc
== MEMCACHED_SUCCESS
);
1234 assert(x
== memcached_server_list_count(servers
));
1237 rc
= memcached_server_push(memc
, servers
);
1238 assert(rc
== MEMCACHED_SUCCESS
);
1239 rc
= memcached_server_push(memc
, servers
);
1240 assert(rc
== MEMCACHED_SUCCESS
);
1242 memcached_server_list_free(servers
);
1247 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
1249 return MEMCACHED_SUCCESS
;
1252 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
1254 return MEMCACHED_SUCCESS
;
1257 test_return
callback_test(memcached_st
*memc
)
1259 /* Test User Data */
1263 memcached_return rc
;
1265 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1266 assert(rc
== MEMCACHED_SUCCESS
);
1267 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1268 assert(*test_ptr
== x
);
1271 /* Test Clone Callback */
1273 memcached_clone_func temp_function
;
1274 memcached_return rc
;
1276 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
1277 assert(rc
== MEMCACHED_SUCCESS
);
1278 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1279 assert(temp_function
== clone_test_callback
);
1282 /* Test Cleanup Callback */
1284 memcached_cleanup_func temp_function
;
1285 memcached_return rc
;
1287 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1288 assert(rc
== MEMCACHED_SUCCESS
);
1289 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1290 assert(temp_function
== cleanup_test_callback
);
1296 /* We don't test the behavior itself, we test the switches */
1297 test_return
behavior_test(memcached_st
*memc
)
1299 unsigned long long value
;
1300 unsigned int set
= 1;
1302 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1303 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1306 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1307 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1310 set
= MEMCACHED_HASH_MD5
;
1311 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1312 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1313 assert(value
== MEMCACHED_HASH_MD5
);
1317 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1318 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1321 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1322 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1325 set
= MEMCACHED_HASH_DEFAULT
;
1326 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1327 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1328 assert(value
== MEMCACHED_HASH_DEFAULT
);
1330 set
= MEMCACHED_HASH_CRC
;
1331 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1332 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1333 assert(value
== MEMCACHED_HASH_CRC
);
1335 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1338 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1344 /* Test case provided by Cal Haldenbrand */
1345 test_return
user_supplied_bug1(memcached_st
*memc
)
1347 unsigned int setter
= 1;
1350 unsigned long long total
= 0;
1353 char randomstuff
[6 * 1024];
1354 memcached_return rc
;
1356 memset(randomstuff
, 0, 6 * 1024);
1358 /* We just keep looking at the same values over and over */
1361 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1362 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1366 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1370 size
= (rand() % ( 5 * 1024 ) ) + 400;
1371 memset(randomstuff
, 0, 6 * 1024);
1372 assert(size
< 6 * 1024); /* Being safe here */
1374 for (j
= 0 ; j
< size
;j
++)
1375 randomstuff
[j
] = (char) (rand() % 26) + 97;
1378 sprintf(key
, "%d", x
);
1379 rc
= memcached_set(memc
, key
, strlen(key
),
1380 randomstuff
, strlen(randomstuff
), 10, 0);
1381 /* If we fail, lets try again */
1382 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1383 rc
= memcached_set(memc
, key
, strlen(key
),
1384 randomstuff
, strlen(randomstuff
), 10, 0);
1385 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1391 /* Test case provided by Cal Haldenbrand */
1392 test_return
user_supplied_bug2(memcached_st
*memc
)
1395 unsigned int setter
;
1397 unsigned long long total
;
1400 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1401 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1403 setter
= 20 * 1024576;
1404 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1405 setter
= 20 * 1024576;
1406 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1407 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1408 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1410 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1413 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1415 memcached_return rc
= MEMCACHED_SUCCESS
;
1416 char buffer
[SMALL_STRING_LEN
];
1421 memset(buffer
, 0, SMALL_STRING_LEN
);
1423 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1424 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1425 &val_len
, &flags
, &rc
);
1426 if (rc
!= MEMCACHED_SUCCESS
)
1428 if (rc
== MEMCACHED_NOTFOUND
)
1432 WATCHPOINT_ERROR(rc
);
1446 /* Do a large mget() over all the keys we think exist */
1447 #define KEY_COUNT 3000 // * 1024576
1448 test_return
user_supplied_bug3(memcached_st
*memc
)
1450 memcached_return rc
;
1451 unsigned int setter
;
1454 size_t key_lengths
[KEY_COUNT
];
1457 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1458 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1460 setter
= 20 * 1024576;
1461 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1462 setter
= 20 * 1024576;
1463 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1464 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1465 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1468 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1470 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1471 for (x
= 0; x
< KEY_COUNT
; x
++)
1475 snprintf(buffer
, 30, "%u", x
);
1476 keys
[x
]= strdup(buffer
);
1477 key_lengths
[x
]= strlen(keys
[x
]);
1480 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1481 assert(rc
== MEMCACHED_SUCCESS
);
1483 /* Turn this into a help function */
1485 char return_key
[MEMCACHED_MAX_KEY
];
1486 size_t return_key_length
;
1488 size_t return_value_length
;
1491 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1492 &return_value_length
, &flags
, &rc
)))
1494 assert(return_value
);
1495 assert(rc
== MEMCACHED_SUCCESS
);
1500 for (x
= 0; x
< KEY_COUNT
; x
++)
1507 /* Make sure we behave properly if server list has no values */
1508 test_return
user_supplied_bug4(memcached_st
*memc
)
1510 memcached_return rc
;
1511 char *keys
[]= {"fudge", "son", "food"};
1512 size_t key_length
[]= {5, 3, 4};
1515 char return_key
[MEMCACHED_MAX_KEY
];
1516 size_t return_key_length
;
1518 size_t return_value_length
;
1520 /* Here we free everything before running a bunch of mget tests */
1522 memcached_server_list_free(memc
->hosts
);
1524 memc
->number_of_hosts
= 0;
1528 /* We need to empty the server before continueing test */
1529 rc
= memcached_flush(memc
, 0);
1530 assert(rc
== MEMCACHED_NO_SERVERS
);
1532 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1533 assert(rc
== MEMCACHED_NO_SERVERS
);
1535 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1536 &return_value_length
, &flags
, &rc
)) != NULL
)
1538 assert(return_value
);
1540 assert(!return_value
);
1541 assert(return_value_length
== 0);
1542 assert(rc
== MEMCACHED_NO_SERVERS
);
1544 for (x
= 0; x
< 3; x
++)
1546 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1547 keys
[x
], key_length
[x
],
1548 (time_t)50, (uint32_t)9);
1549 assert(rc
== MEMCACHED_NO_SERVERS
);
1552 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1553 assert(rc
== MEMCACHED_NO_SERVERS
);
1556 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1557 &return_value_length
, &flags
, &rc
)))
1559 assert(return_value
);
1560 assert(rc
== MEMCACHED_SUCCESS
);
1561 assert(return_key_length
== return_value_length
);
1562 assert(!memcmp(return_value
, return_key
, return_value_length
));
1570 #define VALUE_SIZE_BUG5 1048064
1571 test_return
user_supplied_bug5(memcached_st
*memc
)
1573 memcached_return rc
;
1574 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1575 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1576 char return_key
[MEMCACHED_MAX_KEY
];
1577 size_t return_key_length
;
1579 size_t value_length
;
1583 char insert_data
[VALUE_SIZE_BUG5
];
1585 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1586 insert_data
[x
]= rand();
1588 memcached_flush(memc
, 0);
1589 value
= memcached_get(memc
, keys
[0], key_length
[0],
1590 &value_length
, &flags
, &rc
);
1591 assert(value
== NULL
);
1592 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1595 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1596 &value_length
, &flags
, &rc
)))
1600 for (x
= 0; x
< 4; x
++)
1602 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1603 insert_data
, VALUE_SIZE_BUG5
,
1604 (time_t)0, (uint32_t)0);
1605 assert(rc
== MEMCACHED_SUCCESS
);
1608 for (x
= 0; x
< 10; x
++)
1610 value
= memcached_get(memc
, keys
[0], key_length
[0],
1611 &value_length
, &flags
, &rc
);
1615 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1617 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1618 &value_length
, &flags
, &rc
)))
1629 test_return
user_supplied_bug6(memcached_st
*memc
)
1631 memcached_return rc
;
1632 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1633 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1634 char return_key
[MEMCACHED_MAX_KEY
];
1635 size_t return_key_length
;
1637 size_t value_length
;
1641 char insert_data
[VALUE_SIZE_BUG5
];
1643 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1644 insert_data
[x
]= rand();
1646 memcached_flush(memc
, 0);
1647 value
= memcached_get(memc
, keys
[0], key_length
[0],
1648 &value_length
, &flags
, &rc
);
1649 assert(value
== NULL
);
1650 assert(rc
== MEMCACHED_NOTFOUND
);
1651 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1652 assert(rc
== MEMCACHED_SUCCESS
);
1655 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1656 &value_length
, &flags
, &rc
)))
1659 assert(rc
== MEMCACHED_END
);
1661 for (x
= 0; x
< 4; x
++)
1663 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1664 insert_data
, VALUE_SIZE_BUG5
,
1665 (time_t)0, (uint32_t)0);
1666 assert(rc
== MEMCACHED_SUCCESS
);
1669 for (x
= 0; x
< 2; x
++)
1671 value
= memcached_get(memc
, keys
[0], key_length
[0],
1672 &value_length
, &flags
, &rc
);
1676 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1677 assert(rc
== MEMCACHED_SUCCESS
);
1679 /* We test for purge of partial complete fetches */
1680 for (count
= 3; count
; count
--)
1682 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1683 &value_length
, &flags
, &rc
);
1684 assert(rc
== MEMCACHED_SUCCESS
);
1685 assert(!(memcmp(value
, insert_data
, value_length
)));
1686 assert(value_length
);
1694 test_return
user_supplied_bug8(memcached_st
*memc
)
1696 memcached_return rc
;
1698 memcached_st
*clone
;
1700 memcached_server_st
*servers
;
1701 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";
1703 servers
= memcached_servers_parse(server_list
);
1706 mine
= memcached_create(NULL
);
1707 rc
= memcached_server_push(mine
, servers
);
1708 assert(rc
== MEMCACHED_SUCCESS
);
1709 memcached_server_list_free(servers
);
1712 clone
= memcached_clone(NULL
, mine
);
1714 memcached_quit(mine
);
1715 memcached_quit(clone
);
1718 memcached_free(mine
);
1719 memcached_free(clone
);
1724 /* Test flag store/retrieve */
1725 test_return
user_supplied_bug7(memcached_st
*memc
)
1727 memcached_return rc
;
1728 char *keys
= "036790384900";
1729 size_t key_length
= strlen("036790384900");
1730 char return_key
[MEMCACHED_MAX_KEY
];
1731 size_t return_key_length
;
1733 size_t value_length
;
1736 char insert_data
[VALUE_SIZE_BUG5
];
1738 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1739 insert_data
[x
]= rand();
1741 memcached_flush(memc
, 0);
1744 rc
= memcached_set(memc
, keys
, key_length
,
1745 insert_data
, VALUE_SIZE_BUG5
,
1747 assert(rc
== MEMCACHED_SUCCESS
);
1750 value
= memcached_get(memc
, keys
, key_length
,
1751 &value_length
, &flags
, &rc
);
1752 assert(flags
== 245);
1756 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1759 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1760 &value_length
, &flags
, &rc
);
1761 assert(flags
== 245);
1769 test_return
user_supplied_bug9(memcached_st
*memc
)
1771 memcached_return rc
;
1772 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1773 size_t key_length
[3];
1778 char return_key
[MEMCACHED_MAX_KEY
];
1779 size_t return_key_length
;
1781 size_t return_value_length
;
1784 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1785 key_length
[1]= strlen("fudge&*@#");
1786 key_length
[2]= strlen("for^#@&$not");
1789 for (x
= 0; x
< 3; x
++)
1791 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1792 keys
[x
], key_length
[x
],
1793 (time_t)50, (uint32_t)9);
1794 assert(rc
== MEMCACHED_SUCCESS
);
1797 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1798 assert(rc
== MEMCACHED_SUCCESS
);
1800 /* We need to empty the server before continueing test */
1801 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1802 &return_value_length
, &flags
, &rc
)) != NULL
)
1804 assert(return_value
);
1813 /* We are testing with aggressive timeout to get failures */
1814 test_return
user_supplied_bug10(memcached_st
*memc
)
1818 size_t value_length
= 512;
1821 memcached_return rc
;
1822 unsigned int set
= 1;
1823 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1826 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1827 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1829 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1831 value
= (char*)malloc(value_length
* sizeof(char));
1833 for (x
= 0; x
< value_length
; x
++)
1834 value
[x
]= (char) (x
% 127);
1836 for (x
= 1; x
<= 100000; ++x
)
1838 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1840 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1842 if (rc
== MEMCACHED_WRITE_FAILURE
)
1847 memcached_free(mclone
);
1853 We are looking failures in the async protocol
1855 test_return
user_supplied_bug11(memcached_st
*memc
)
1859 size_t value_length
= 512;
1862 memcached_return rc
;
1863 unsigned int set
= 1;
1865 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1867 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1868 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1870 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1872 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1874 assert(timeout
== -1);
1876 value
= (char*)malloc(value_length
* sizeof(char));
1878 for (x
= 0; x
< value_length
; x
++)
1879 value
[x
]= (char) (x
% 127);
1881 for (x
= 1; x
<= 100000; ++x
)
1883 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1887 memcached_free(mclone
);
1893 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1895 test_return
user_supplied_bug12(memcached_st
*memc
)
1897 memcached_return rc
;
1899 size_t value_length
;
1901 uint64_t number_value
;
1903 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1904 &value_length
, &flags
, &rc
);
1905 assert(value
== NULL
);
1906 assert(rc
== MEMCACHED_NOTFOUND
);
1908 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1911 assert(value
== NULL
);
1912 assert(rc
== MEMCACHED_NOTFOUND
);
1914 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1916 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1917 &value_length
, &flags
, &rc
);
1919 assert(rc
== MEMCACHED_SUCCESS
);
1922 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1924 assert(number_value
== 2);
1925 assert(rc
== MEMCACHED_SUCCESS
);
1931 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1932 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1934 test_return
user_supplied_bug13(memcached_st
*memc
)
1936 char key
[] = "key34567890";
1938 memcached_return rc
;
1939 size_t overflowSize
;
1941 char commandFirst
[]= "set key34567890 0 0 ";
1942 char commandLast
[] = " \r\n"; /* first line of command sent to server */
1943 size_t commandLength
;
1946 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
1948 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
1950 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
1952 overflow
= malloc(testSize
);
1953 assert(overflow
!= NULL
);
1955 memset(overflow
, 'x', testSize
);
1956 rc
= memcached_set(memc
, key
, strlen(key
),
1957 overflow
, testSize
, 0, 0);
1958 assert(rc
== MEMCACHED_SUCCESS
);
1967 Test values of many different sizes
1968 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1969 set key34567890 0 0 8169 \r\n
1970 is sent followed by buffer of size 8169, followed by 8169
1972 test_return
user_supplied_bug14(memcached_st
*memc
)
1975 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1976 memcached_return rc
;
1979 size_t value_length
= 18000;
1981 size_t string_length
;
1984 size_t current_length
;
1986 value
= (char*)malloc(value_length
);
1989 for (x
= 0; x
< value_length
; x
++)
1990 value
[x
] = (char) (x
% 127);
1992 for (current_length
= 0; current_length
< value_length
; current_length
++)
1994 rc
= memcached_set(memc
, key
, strlen(key
),
1995 value
, current_length
,
1996 (time_t)0, (uint32_t)0);
1997 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1999 string
= memcached_get(memc
, key
, strlen(key
),
2000 &string_length
, &flags
, &rc
);
2002 assert(rc
== MEMCACHED_SUCCESS
);
2003 assert(string_length
== current_length
);
2004 assert(!memcmp(string
, value
, string_length
));
2015 Look for zero length value problems
2017 test_return
user_supplied_bug15(memcached_st
*memc
)
2020 memcached_return rc
;
2026 for (x
= 0; x
< 2; x
++)
2028 rc
= memcached_set(memc
, key
, strlen(key
),
2030 (time_t)0, (uint32_t)0);
2032 assert(rc
== MEMCACHED_SUCCESS
);
2034 value
= memcached_get(memc
, key
, strlen(key
),
2035 &length
, &flags
, &rc
);
2037 assert(rc
== MEMCACHED_SUCCESS
);
2038 assert(value
== NULL
);
2039 assert(length
== 0);
2042 value
= memcached_get(memc
, key
, strlen(key
),
2043 &length
, &flags
, &rc
);
2045 assert(rc
== MEMCACHED_SUCCESS
);
2046 assert(value
== NULL
);
2047 assert(length
== 0);
2054 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2055 test_return
user_supplied_bug16(memcached_st
*memc
)
2057 memcached_return rc
;
2063 rc
= memcached_set(memc
, key
, strlen(key
),
2065 (time_t)0, UINT32_MAX
);
2067 assert(rc
== MEMCACHED_SUCCESS
);
2069 value
= memcached_get(memc
, key
, strlen(key
),
2070 &length
, &flags
, &rc
);
2072 assert(rc
== MEMCACHED_SUCCESS
);
2073 assert(value
== NULL
);
2074 assert(length
== 0);
2075 assert(flags
== UINT32_MAX
);
2080 /* Check the validity of chinese key*/
2081 test_return
user_supplied_bug17(memcached_st
*memc
)
2083 memcached_return rc
;
2085 char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2090 rc
= memcached_set(memc
, key
, strlen(key
),
2091 value
, strlen(value
),
2094 assert(rc
== MEMCACHED_SUCCESS
);
2096 value2
= memcached_get(memc
, key
, strlen(key
),
2097 &length
, &flags
, &rc
);
2099 assert(length
==strlen(value
));
2100 assert(rc
== MEMCACHED_SUCCESS
);
2101 assert(memcmp(value
, value2
, length
)==0);
2107 test_return
result_static(memcached_st
*memc
)
2109 memcached_result_st result
;
2110 memcached_result_st
*result_ptr
;
2112 result_ptr
= memcached_result_create(memc
, &result
);
2113 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2115 memcached_result_free(&result
);
2120 test_return
result_alloc(memcached_st
*memc
)
2122 memcached_result_st
*result
;
2124 result
= memcached_result_create(memc
, NULL
);
2126 memcached_result_free(result
);
2131 test_return
string_static_null(memcached_st
*memc
)
2133 memcached_string_st string
;
2134 memcached_string_st
*string_ptr
;
2136 string_ptr
= memcached_string_create(memc
, &string
, 0);
2137 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2139 memcached_string_free(&string
);
2144 test_return
string_alloc_null(memcached_st
*memc
)
2146 memcached_string_st
*string
;
2148 string
= memcached_string_create(memc
, NULL
, 0);
2150 memcached_string_free(string
);
2155 test_return
string_alloc_with_size(memcached_st
*memc
)
2157 memcached_string_st
*string
;
2159 string
= memcached_string_create(memc
, NULL
, 1024);
2161 memcached_string_free(string
);
2166 test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2168 memcached_string_st
*string
;
2170 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
2171 assert(string
== NULL
);
2176 test_return
string_alloc_append(memcached_st
*memc
)
2179 char buffer
[SMALL_STRING_LEN
];
2180 memcached_string_st
*string
;
2182 /* Ring the bell! */
2183 memset(buffer
, 6, SMALL_STRING_LEN
);
2185 string
= memcached_string_create(memc
, NULL
, 100);
2188 for (x
= 0; x
< 1024; x
++)
2190 memcached_return rc
;
2191 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2192 assert(rc
== MEMCACHED_SUCCESS
);
2194 memcached_string_free(string
);
2199 test_return
string_alloc_append_toobig(memcached_st
*memc
)
2201 memcached_return rc
;
2203 char buffer
[SMALL_STRING_LEN
];
2204 memcached_string_st
*string
;
2206 /* Ring the bell! */
2207 memset(buffer
, 6, SMALL_STRING_LEN
);
2209 string
= memcached_string_create(memc
, NULL
, 100);
2212 for (x
= 0; x
< 1024; x
++)
2214 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2215 assert(rc
== MEMCACHED_SUCCESS
);
2217 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
2218 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2219 memcached_string_free(string
);
2224 test_return
cleanup_pairs(memcached_st
*memc
)
2226 pairs_free(global_pairs
);
2231 test_return
generate_pairs(memcached_st
*memc
)
2233 unsigned long long x
;
2234 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2235 global_count
= GLOBAL_COUNT
;
2237 for (x
= 0; x
< global_count
; x
++)
2239 global_keys
[x
]= global_pairs
[x
].key
;
2240 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2246 test_return
generate_large_pairs(memcached_st
*memc
)
2248 unsigned long long x
;
2249 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2250 global_count
= GLOBAL2_COUNT
;
2252 for (x
= 0; x
< global_count
; x
++)
2254 global_keys
[x
]= global_pairs
[x
].key
;
2255 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2261 test_return
generate_data(memcached_st
*memc
)
2263 execute_set(memc
, global_pairs
, global_count
);
2268 test_return
generate_data_with_stats(memcached_st
*memc
)
2270 memcached_stat_st
*stat_p
= NULL
;
2271 memcached_return rc
;
2273 execute_set(memc
, global_pairs
, global_count
);
2275 //TODO: hosts used size stats
2276 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2277 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; ++host_index
)
2279 printf("\nserver %d|%s|%d bytes: %lld\n", host_index
, (memc
->hosts
)[host_index
].hostname
, (memc
->hosts
)[host_index
].port
, (stat_p
+ host_index
)->bytes
);
2285 test_return
generate_buffer_data(memcached_st
*memc
)
2290 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2291 generate_data(memc
);
2296 test_return
get_read_count(memcached_st
*memc
)
2299 memcached_return rc
;
2300 memcached_st
*clone
;
2302 clone
= memcached_clone(NULL
, memc
);
2305 memcached_server_add(clone
, "localhost", 6666);
2309 size_t return_value_length
;
2313 for (x
= count
= 0; x
< global_count
; x
++)
2315 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2316 &return_value_length
, &flags
, &rc
);
2317 if (rc
== MEMCACHED_SUCCESS
)
2324 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2327 memcached_free(clone
);
2332 test_return
get_read(memcached_st
*memc
)
2335 memcached_return rc
;
2339 size_t return_value_length
;
2342 for (x
= 0; x
< global_count
; x
++)
2344 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2345 &return_value_length
, &flags
, &rc
);
2347 assert(return_value);
2348 assert(rc == MEMCACHED_SUCCESS);
2350 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2358 test_return
mget_read(memcached_st
*memc
)
2360 memcached_return rc
;
2362 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2363 assert(rc
== MEMCACHED_SUCCESS
);
2364 /* Turn this into a help function */
2366 char return_key
[MEMCACHED_MAX_KEY
];
2367 size_t return_key_length
;
2369 size_t return_value_length
;
2372 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2373 &return_value_length
, &flags
, &rc
)))
2375 assert(return_value
);
2376 assert(rc
== MEMCACHED_SUCCESS
);
2384 test_return
mget_read_result(memcached_st
*memc
)
2386 memcached_return rc
;
2388 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2389 assert(rc
== MEMCACHED_SUCCESS
);
2390 /* Turn this into a help function */
2392 memcached_result_st results_obj
;
2393 memcached_result_st
*results
;
2395 results
= memcached_result_create(memc
, &results_obj
);
2397 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2400 assert(rc
== MEMCACHED_SUCCESS
);
2403 memcached_result_free(&results_obj
);
2409 test_return
mget_read_function(memcached_st
*memc
)
2411 memcached_return rc
;
2412 unsigned int counter
;
2413 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
2415 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2416 assert(rc
== MEMCACHED_SUCCESS
);
2418 callbacks
[0]= &callback_counter
;
2420 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2425 test_return
delete_generate(memcached_st
*memc
)
2429 for (x
= 0; x
< global_count
; x
++)
2431 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2437 test_return
delete_buffer_generate(memcached_st
*memc
)
2443 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2445 for (x
= 0; x
< global_count
; x
++)
2447 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2453 test_return
free_data(memcached_st
*memc
)
2455 pairs_free(global_pairs
);
2460 test_return
add_host_test1(memcached_st
*memc
)
2463 memcached_return rc
;
2464 char servername
[]= "0.example.com";
2465 memcached_server_st
*servers
;
2467 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
2469 assert(1 == memcached_server_list_count(servers
));
2471 for (x
= 2; x
< 20; x
++)
2473 char buffer
[SMALL_STRING_LEN
];
2475 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2476 servers
= memcached_server_list_append(servers
, buffer
, 401,
2478 assert(rc
== MEMCACHED_SUCCESS
);
2479 assert(x
== memcached_server_list_count(servers
));
2482 rc
= memcached_server_push(memc
, servers
);
2483 assert(rc
== MEMCACHED_SUCCESS
);
2484 rc
= memcached_server_push(memc
, servers
);
2485 assert(rc
== MEMCACHED_SUCCESS
);
2487 memcached_server_list_free(servers
);
2492 memcached_return
pre_nonblock(memcached_st
*memc
)
2494 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2496 return MEMCACHED_SUCCESS
;
2499 memcached_return
pre_murmur(memcached_st
*memc
)
2501 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2503 return MEMCACHED_SUCCESS
;
2506 memcached_return
pre_md5(memcached_st
*memc
)
2508 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2510 return MEMCACHED_SUCCESS
;
2513 memcached_return
pre_crc(memcached_st
*memc
)
2515 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2517 return MEMCACHED_SUCCESS
;
2520 memcached_return
pre_hsieh(memcached_st
*memc
)
2522 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2524 return MEMCACHED_SUCCESS
;
2527 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2529 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2531 return MEMCACHED_SUCCESS
;
2534 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2536 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2538 return MEMCACHED_SUCCESS
;
2541 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2543 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2545 return MEMCACHED_SUCCESS
;
2548 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2550 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2552 return MEMCACHED_SUCCESS
;
2555 memcached_return
pre_behavior_ketama(memcached_st
*memc
)
2557 memcached_return rc
;
2560 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
2561 assert(rc
== MEMCACHED_SUCCESS
);
2563 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
2566 return MEMCACHED_SUCCESS
;
2569 memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
2571 memcached_return rc
;
2574 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2575 assert(rc
== MEMCACHED_SUCCESS
);
2577 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2580 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2581 assert(rc
== MEMCACHED_SUCCESS
);
2583 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2584 assert(value
== MEMCACHED_HASH_MD5
);
2585 return MEMCACHED_SUCCESS
;
2587 void my_free(memcached_st
*ptr
, void *mem
)
2592 void *my_malloc(memcached_st
*ptr
, const size_t size
)
2594 return malloc(size
);
2597 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2599 return realloc(mem
, size
);
2602 memcached_return
set_prefix(memcached_st
*memc
)
2604 memcached_return rc
;
2605 const char *key
= "mine";
2608 /* Make sure be default none exists */
2609 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2610 assert(rc
== MEMCACHED_FAILURE
);
2612 /* Test a clean set */
2613 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2614 assert(rc
== MEMCACHED_SUCCESS
);
2616 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2617 assert(memcmp(value
, key
, 4) == 0);
2618 assert(rc
== MEMCACHED_SUCCESS
);
2620 /* Test that we can turn it off */
2621 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2622 assert(rc
== MEMCACHED_SUCCESS
);
2624 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2625 assert(rc
== MEMCACHED_FAILURE
);
2627 /* Now setup for main test */
2628 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2629 assert(rc
== MEMCACHED_SUCCESS
);
2631 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2632 assert(rc
== MEMCACHED_SUCCESS
);
2633 assert(memcmp(value
, key
, 4) == 0);
2635 /* Set to Zero, and then Set to something too large */
2638 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2639 assert(rc
== MEMCACHED_SUCCESS
);
2641 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2642 assert(rc
== MEMCACHED_FAILURE
);
2643 assert(value
== NULL
);
2645 /* Test a long key for failure */
2646 long_key
= "Thisismorethentheallottednumberofcharacters";
2647 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2648 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2650 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2651 long_key
= "This is more then the allotted number of characters";
2652 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2653 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2655 /* Test for a bad prefix, but with a short key */
2656 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
2657 assert(rc
== MEMCACHED_SUCCESS
);
2659 long_key
= "dog cat";
2660 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2661 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2664 return MEMCACHED_SUCCESS
;
2667 memcached_return
set_memory_alloc(memcached_st
*memc
)
2670 memcached_malloc_function test_ptr
;
2671 memcached_return rc
;
2673 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2674 assert(rc
== MEMCACHED_SUCCESS
);
2675 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2676 assert(rc
== MEMCACHED_SUCCESS
);
2677 assert(test_ptr
== my_malloc
);
2681 memcached_realloc_function test_ptr
;
2682 memcached_return rc
;
2684 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2685 assert(rc
== MEMCACHED_SUCCESS
);
2686 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2687 assert(rc
== MEMCACHED_SUCCESS
);
2688 assert(test_ptr
== my_realloc
);
2692 memcached_free_function test_ptr
;
2693 memcached_return rc
;
2695 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2696 assert(rc
== MEMCACHED_SUCCESS
);
2697 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2698 assert(rc
== MEMCACHED_SUCCESS
);
2699 assert(test_ptr
== my_free
);
2702 return MEMCACHED_SUCCESS
;
2705 memcached_return
enable_consistent(memcached_st
*memc
)
2707 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2708 memcached_hash hash
;
2709 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2712 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2713 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2715 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2716 assert(hash
== MEMCACHED_HASH_HSIEH
);
2719 return MEMCACHED_SUCCESS
;
2722 memcached_return
enable_cas(memcached_st
*memc
)
2724 unsigned int set
= 1;
2726 memcached_version(memc
);
2728 if (memc
->hosts
[0].major_version
>= 1 &&
2729 memc
->hosts
[0].minor_version
>= 2 &&
2730 memc
->hosts
[0].micro_version
>= 4)
2732 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
2734 return MEMCACHED_SUCCESS
;
2737 return MEMCACHED_FAILURE
;
2740 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2742 memcached_version(memc
);
2744 if (memc
->hosts
[0].major_version
>= 1 &&
2745 memc
->hosts
[0].minor_version
>= 2 &&
2746 memc
->hosts
[0].micro_version
>= 4)
2747 return MEMCACHED_SUCCESS
;
2749 return MEMCACHED_FAILURE
;
2752 memcached_return
pre_unix_socket(memcached_st
*memc
)
2754 memcached_return rc
;
2757 memcached_server_list_free(memc
->hosts
);
2759 memc
->number_of_hosts
= 0;
2761 if (stat("/tmp/memcached.socket", &buf
))
2762 return MEMCACHED_FAILURE
;
2764 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2769 memcached_return
pre_udp(memcached_st
*memc
)
2771 memcached_return rc
;
2773 memcached_server_list_free(memc
->hosts
);
2775 memc
->number_of_hosts
= 0;
2778 return MEMCACHED_FAILURE
;
2780 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2785 memcached_return
pre_nodelay(memcached_st
*memc
)
2787 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2788 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2790 return MEMCACHED_SUCCESS
;
2793 memcached_return
poll_timeout(memcached_st
*memc
)
2799 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
2801 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2803 assert(timeout
== 100);
2805 return MEMCACHED_SUCCESS
;
2809 /* Clean the server before beginning testing */
2811 {"flush", 0, flush_test
},
2812 {"init", 0, init_test
},
2813 {"allocation", 0, allocation_test
},
2814 {"server_list_null_test", 0, server_list_null_test
},
2815 {"server_unsort", 0, server_unsort_test
},
2816 {"server_sort", 0, server_sort_test
},
2817 {"server_sort2", 0, server_sort2_test
},
2818 {"clone_test", 0, clone_test
},
2819 {"error", 0, error_test
},
2820 {"set", 0, set_test
},
2821 {"set2", 0, set_test2
},
2822 {"set3", 0, set_test3
},
2823 {"add", 1, add_test
},
2824 {"replace", 1, replace_test
},
2825 {"delete", 1, delete_test
},
2826 {"get", 1, get_test
},
2827 {"get2", 0, get_test2
},
2828 {"get3", 0, get_test3
},
2829 {"get4", 0, get_test4
},
2830 {"stats_servername", 0, stats_servername_test
},
2831 {"increment", 0, increment_test
},
2832 {"decrement", 0, decrement_test
},
2833 {"quit", 0, quit_test
},
2834 {"mget", 1, mget_test
},
2835 {"mget_result", 1, mget_result_test
},
2836 {"mget_result_alloc", 1, mget_result_alloc_test
},
2837 {"mget_result_function", 1, mget_result_function
},
2838 {"get_stats", 0, get_stats
},
2839 {"add_host_test", 0, add_host_test
},
2840 {"get_stats_keys", 0, get_stats_keys
},
2841 {"behavior_test", 0, get_stats_keys
},
2842 {"callback_test", 0, get_stats_keys
},
2843 {"version_string_test", 0, version_string_test
},
2844 {"bad_key", 1, bad_key_test
},
2845 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
2846 {"read_through", 1, read_through
},
2847 {"delete_through", 1, delete_through
},
2851 test_st async_tests
[] ={
2852 {"add", 1, add_wrapper
},
2856 test_st string_tests
[] ={
2857 {"string static with null", 0, string_static_null
},
2858 {"string alloc with null", 0, string_alloc_null
},
2859 {"string alloc with 1K", 0, string_alloc_with_size
},
2860 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2861 {"string append", 0, string_alloc_append
},
2862 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2866 test_st result_tests
[] ={
2867 {"result static", 0, result_static
},
2868 {"result alloc", 0, result_alloc
},
2872 test_st version_1_2_3
[] ={
2873 {"append", 0, append_test
},
2874 {"prepend", 0, prepend_test
},
2875 {"cas", 0, cas_test
},
2876 {"cas2", 0, cas2_test
},
2877 {"append_binary", 0, append_binary_test
},
2881 test_st user_tests
[] ={
2882 {"user_supplied_bug1", 0, user_supplied_bug1
},
2883 {"user_supplied_bug2", 0, user_supplied_bug2
},
2884 {"user_supplied_bug3", 0, user_supplied_bug3
},
2885 {"user_supplied_bug4", 0, user_supplied_bug4
},
2886 {"user_supplied_bug5", 1, user_supplied_bug5
},
2887 {"user_supplied_bug6", 1, user_supplied_bug6
},
2888 {"user_supplied_bug7", 1, user_supplied_bug7
},
2889 {"user_supplied_bug8", 1, user_supplied_bug8
},
2890 {"user_supplied_bug9", 1, user_supplied_bug9
},
2891 {"user_supplied_bug10", 1, user_supplied_bug10
},
2892 {"user_supplied_bug11", 1, user_supplied_bug11
},
2893 {"user_supplied_bug12", 1, user_supplied_bug12
},
2894 {"user_supplied_bug13", 1, user_supplied_bug13
},
2895 {"user_supplied_bug14", 1, user_supplied_bug14
},
2896 {"user_supplied_bug15", 1, user_supplied_bug15
},
2897 {"user_supplied_bug16", 1, user_supplied_bug16
},
2898 {"user_supplied_bug17", 1, user_supplied_bug17
},
2902 test_st generate_tests
[] ={
2903 {"generate_pairs", 1, generate_pairs
},
2904 {"generate_data", 1, generate_data
},
2905 {"get_read", 0, get_read
},
2906 {"delete_generate", 0, delete_generate
},
2907 {"generate_buffer_data", 1, generate_buffer_data
},
2908 {"delete_buffer", 0, delete_buffer_generate
},
2909 {"generate_data", 1, generate_data
},
2910 {"mget_read", 0, mget_read
},
2911 {"mget_read_result", 0, mget_read_result
},
2912 {"mget_read_function", 0, mget_read_function
},
2913 {"cleanup", 1, cleanup_pairs
},
2914 {"generate_large_pairs", 1, generate_large_pairs
},
2915 {"generate_data", 1, generate_data
},
2916 {"generate_buffer_data", 1, generate_buffer_data
},
2917 {"cleanup", 1, cleanup_pairs
},
2921 test_st consistent_tests
[] ={
2922 {"generate_pairs", 1, generate_pairs
},
2923 {"generate_data", 1, generate_data
},
2924 {"get_read", 0, get_read_count
},
2925 {"cleanup", 1, cleanup_pairs
},
2929 test_st consistent_weighted_tests
[] ={
2930 {"generate_pairs", 1, generate_pairs
},
2931 {"generate_data", 1, generate_data_with_stats
},
2932 {"get_read", 0, get_read_count
},
2933 {"cleanup", 1, cleanup_pairs
},
2937 collection_st collection
[] ={
2938 {"block", 0, 0, tests
},
2939 {"nonblock", pre_nonblock
, 0, tests
},
2940 {"nodelay", pre_nodelay
, 0, tests
},
2941 {"md5", pre_md5
, 0, tests
},
2942 {"crc", pre_crc
, 0, tests
},
2943 {"hsieh", pre_hsieh
, 0, tests
},
2944 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2945 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2946 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2947 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2948 {"ketama", pre_behavior_ketama
, 0, tests
},
2949 {"unix_socket", pre_unix_socket
, 0, tests
},
2950 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2951 {"poll_timeout", poll_timeout
, 0, tests
},
2952 {"gets", enable_cas
, 0, tests
},
2953 {"consistent", enable_consistent
, 0, tests
},
2954 {"memory_allocators", set_memory_alloc
, 0, tests
},
2955 {"prefix", set_prefix
, 0, tests
},
2956 // {"udp", pre_udp, 0, tests},
2957 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2958 {"string", 0, 0, string_tests
},
2959 {"result", 0, 0, result_tests
},
2960 {"async", pre_nonblock
, 0, async_tests
},
2961 {"user", 0, 0, user_tests
},
2962 {"generate", 0, 0, generate_tests
},
2963 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2964 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
2965 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2966 {"generate_md5", pre_md5
, 0, generate_tests
},
2967 {"generate_murmur", pre_murmur
, 0, generate_tests
},
2968 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2969 {"consistent_not", 0, 0, consistent_tests
},
2970 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
2971 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
2975 #define SERVERS_TO_CREATE 5
2977 void *world_create(void)
2979 server_startup_st
*construct
;
2981 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
2982 memset(construct
, 0, sizeof(server_startup_st
));
2983 construct
->count
= SERVERS_TO_CREATE
;
2985 server_startup(construct
);
2990 void world_destroy(void *p
)
2992 server_startup_st
*construct
= (server_startup_st
*)p
;
2993 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
2994 memcached_server_list_free(servers
);
2996 server_shutdown(construct
);
3000 void get_world(world_st
*world
)
3002 world
->collections
= collection
;
3003 world
->create
= world_create
;
3004 world
->destroy
= world_destroy
;