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
|| rc
== MEMCACHED_DATA_EXISTS
);
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 /* Make sure zero length keys are marked as bad */
635 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
636 assert(rc
== MEMCACHED_SUCCESS
);
637 string
= memcached_get(clone
, key
, 0,
638 &string_length
, &flags
, &rc
);
639 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
640 assert(string_length
== 0);
643 memcached_free(clone
);
648 #define READ_THROUGH_VALUE "set for me"
649 memcached_return
read_through_trigger(memcached_st
*memc
,
650 char *key
, size_t key_length
,
651 memcached_result_st
*result
)
654 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
657 test_return
read_through(memcached_st
*memc
)
662 size_t string_length
;
665 string
= memcached_get(memc
, key
, strlen(key
),
666 &string_length
, &flags
, &rc
);
668 assert(rc
== MEMCACHED_NOTFOUND
);
669 assert(string_length
== 0);
672 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, read_through_trigger
);
673 assert(rc
== MEMCACHED_SUCCESS
);
675 string
= memcached_get(memc
, key
, strlen(key
),
676 &string_length
, &flags
, &rc
);
678 assert(rc
== MEMCACHED_SUCCESS
);
679 assert(string_length
== strlen(READ_THROUGH_VALUE
));
680 assert(!strcmp(READ_THROUGH_VALUE
, string
));
683 string
= memcached_get(memc
, key
, strlen(key
),
684 &string_length
, &flags
, &rc
);
686 assert(rc
== MEMCACHED_SUCCESS
);
687 assert(string_length
== strlen(READ_THROUGH_VALUE
));
688 assert(!strcmp(READ_THROUGH_VALUE
, string
));
694 memcached_return
delete_trigger(memcached_st
*ptr
, const char *key
, size_t key_length
)
698 return MEMCACHED_SUCCESS
;
701 test_return
delete_through(memcached_st
*memc
)
703 memcached_trigger_delete_key callback
;
706 callback
= delete_trigger
;
708 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, callback
);
709 assert(rc
== MEMCACHED_SUCCESS
);
714 test_return
get_test(memcached_st
*memc
)
719 size_t string_length
;
722 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
723 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
725 string
= memcached_get(memc
, key
, strlen(key
),
726 &string_length
, &flags
, &rc
);
728 assert(rc
== MEMCACHED_NOTFOUND
);
729 assert(string_length
== 0);
735 test_return
get_test2(memcached_st
*memc
)
739 char *value
= "when we sanitize";
741 size_t string_length
;
744 rc
= memcached_set(memc
, key
, strlen(key
),
745 value
, strlen(value
),
746 (time_t)0, (uint32_t)0);
747 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
749 string
= memcached_get(memc
, key
, strlen(key
),
750 &string_length
, &flags
, &rc
);
753 assert(rc
== MEMCACHED_SUCCESS
);
754 assert(string_length
== strlen(value
));
755 assert(!memcmp(string
, value
, string_length
));
762 test_return
set_test2(memcached_st
*memc
)
766 char *value
= "train in the brain";
767 size_t value_length
= strlen(value
);
770 for (x
= 0; x
< 10; x
++)
772 rc
= memcached_set(memc
, key
, strlen(key
),
774 (time_t)0, (uint32_t)0);
775 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
781 test_return
set_test3(memcached_st
*memc
)
786 size_t value_length
= 8191;
789 value
= (char*)malloc(value_length
);
792 for (x
= 0; x
< value_length
; x
++)
793 value
[x
] = (char) (x
% 127);
795 for (x
= 0; x
< 1; x
++)
797 rc
= memcached_set(memc
, key
, strlen(key
),
799 (time_t)0, (uint32_t)0);
800 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
808 test_return
get_test3(memcached_st
*memc
)
813 size_t value_length
= 8191;
815 size_t string_length
;
819 value
= (char*)malloc(value_length
);
822 for (x
= 0; x
< value_length
; x
++)
823 value
[x
] = (char) (x
% 127);
825 rc
= memcached_set(memc
, key
, strlen(key
),
827 (time_t)0, (uint32_t)0);
828 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
830 string
= memcached_get(memc
, key
, strlen(key
),
831 &string_length
, &flags
, &rc
);
833 assert(rc
== MEMCACHED_SUCCESS
);
835 assert(string_length
== value_length
);
836 assert(!memcmp(string
, value
, string_length
));
844 test_return
get_test4(memcached_st
*memc
)
849 size_t value_length
= 8191;
851 size_t string_length
;
855 value
= (char*)malloc(value_length
);
858 for (x
= 0; x
< value_length
; x
++)
859 value
[x
] = (char) (x
% 127);
861 rc
= memcached_set(memc
, key
, strlen(key
),
863 (time_t)0, (uint32_t)0);
864 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
866 for (x
= 0; x
< 10; x
++)
868 string
= memcached_get(memc
, key
, strlen(key
),
869 &string_length
, &flags
, &rc
);
871 assert(rc
== MEMCACHED_SUCCESS
);
873 assert(string_length
== value_length
);
874 assert(!memcmp(string
, value
, string_length
));
883 /* Do not copy the style of this code, I just access hosts to testthis function */
884 test_return
stats_servername_test(memcached_st
*memc
)
887 memcached_stat_st stat
;
888 rc
= memcached_stat_servername(&stat
, NULL
,
889 memc
->hosts
[0].hostname
,
890 memc
->hosts
[0].port
);
895 test_return
increment_test(memcached_st
*memc
)
902 rc
= memcached_set(memc
, key
, strlen(key
),
903 value
, strlen(value
),
904 (time_t)0, (uint32_t)0);
905 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
907 rc
= memcached_increment(memc
, key
, strlen(key
),
909 assert(rc
== MEMCACHED_SUCCESS
);
910 assert(new_number
== 1);
912 rc
= memcached_increment(memc
, key
, strlen(key
),
914 assert(rc
== MEMCACHED_SUCCESS
);
915 assert(new_number
== 2);
920 test_return
decrement_test(memcached_st
*memc
)
927 rc
= memcached_set(memc
, key
, strlen(key
),
928 value
, strlen(value
),
929 (time_t)0, (uint32_t)0);
930 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
932 rc
= memcached_decrement(memc
, key
, strlen(key
),
934 assert(rc
== MEMCACHED_SUCCESS
);
935 assert(new_number
== 2);
937 rc
= memcached_decrement(memc
, key
, strlen(key
),
939 assert(rc
== MEMCACHED_SUCCESS
);
940 assert(new_number
== 1);
945 test_return
quit_test(memcached_st
*memc
)
949 char *value
= "sanford and sun";
951 rc
= memcached_set(memc
, key
, strlen(key
),
952 value
, strlen(value
),
953 (time_t)10, (uint32_t)3);
954 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
955 memcached_quit(memc
);
957 rc
= memcached_set(memc
, key
, strlen(key
),
958 value
, strlen(value
),
959 (time_t)50, (uint32_t)9);
960 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
965 test_return
mget_result_test(memcached_st
*memc
)
968 char *keys
[]= {"fudge", "son", "food"};
969 size_t key_length
[]= {5, 3, 4};
972 memcached_result_st results_obj
;
973 memcached_result_st
*results
;
975 results
= memcached_result_create(memc
, &results_obj
);
977 assert(&results_obj
== results
);
979 /* We need to empty the server before continueing test */
980 rc
= memcached_flush(memc
, 0);
981 assert(rc
== MEMCACHED_SUCCESS
);
983 rc
= memcached_mget(memc
, keys
, key_length
, 3);
984 assert(rc
== MEMCACHED_SUCCESS
);
986 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
991 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
993 assert(rc
== MEMCACHED_END
);
995 for (x
= 0; x
< 3; x
++)
997 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
998 keys
[x
], key_length
[x
],
999 (time_t)50, (uint32_t)9);
1000 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1003 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1004 assert(rc
== MEMCACHED_SUCCESS
);
1006 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1009 assert(&results_obj
== results
);
1010 assert(rc
== MEMCACHED_SUCCESS
);
1011 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1012 assert(!memcmp(memcached_result_key_value(results
),
1013 memcached_result_value(results
),
1014 memcached_result_length(results
)));
1017 memcached_result_free(&results_obj
);
1022 test_return
mget_result_alloc_test(memcached_st
*memc
)
1024 memcached_return rc
;
1025 char *keys
[]= {"fudge", "son", "food"};
1026 size_t key_length
[]= {5, 3, 4};
1029 memcached_result_st
*results
;
1031 /* We need to empty the server before continueing test */
1032 rc
= memcached_flush(memc
, 0);
1033 assert(rc
== MEMCACHED_SUCCESS
);
1035 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1036 assert(rc
== MEMCACHED_SUCCESS
);
1038 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1043 assert(rc
== MEMCACHED_END
);
1045 for (x
= 0; x
< 3; x
++)
1047 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1048 keys
[x
], key_length
[x
],
1049 (time_t)50, (uint32_t)9);
1050 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1053 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1054 assert(rc
== MEMCACHED_SUCCESS
);
1057 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1060 assert(rc
== MEMCACHED_SUCCESS
);
1061 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1062 assert(!memcmp(memcached_result_key_value(results
),
1063 memcached_result_value(results
),
1064 memcached_result_length(results
)));
1065 memcached_result_free(results
);
1072 /* Count the results */
1073 unsigned int callback_counter(memcached_st
*ptr
, memcached_result_st
*result
, void *context
)
1075 unsigned int *counter
= (unsigned int *)context
;
1077 *counter
= *counter
+ 1;
1082 test_return
mget_result_function(memcached_st
*memc
)
1084 memcached_return rc
;
1085 char *keys
[]= {"fudge", "son", "food"};
1086 size_t key_length
[]= {5, 3, 4};
1088 unsigned int counter
;
1089 memcached_execute_function callbacks
[1];
1091 /* We need to empty the server before continueing test */
1092 rc
= memcached_flush(memc
, 0);
1093 for (x
= 0; x
< 3; x
++)
1095 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1096 keys
[x
], key_length
[x
],
1097 (time_t)50, (uint32_t)9);
1098 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1101 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1102 assert(rc
== MEMCACHED_SUCCESS
);
1104 callbacks
[0]= &callback_counter
;
1106 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1108 assert(counter
== 3);
1113 test_return
mget_test(memcached_st
*memc
)
1115 memcached_return rc
;
1116 char *keys
[]= {"fudge", "son", "food"};
1117 size_t key_length
[]= {5, 3, 4};
1121 char return_key
[MEMCACHED_MAX_KEY
];
1122 size_t return_key_length
;
1124 size_t return_value_length
;
1126 /* We need to empty the server before continueing test */
1127 rc
= memcached_flush(memc
, 0);
1128 assert(rc
== MEMCACHED_SUCCESS
);
1130 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1131 assert(rc
== MEMCACHED_SUCCESS
);
1133 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1134 &return_value_length
, &flags
, &rc
)) != NULL
)
1136 assert(return_value
);
1138 assert(!return_value
);
1139 assert(return_value_length
== 0);
1140 assert(rc
== MEMCACHED_END
);
1142 for (x
= 0; x
< 3; x
++)
1144 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1145 keys
[x
], key_length
[x
],
1146 (time_t)50, (uint32_t)9);
1147 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1150 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1151 assert(rc
== MEMCACHED_SUCCESS
);
1154 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1155 &return_value_length
, &flags
, &rc
)))
1157 assert(return_value
);
1158 assert(rc
== MEMCACHED_SUCCESS
);
1159 assert(return_key_length
== return_value_length
);
1160 assert(!memcmp(return_value
, return_key
, return_value_length
));
1168 test_return
get_stats_keys(memcached_st
*memc
)
1172 memcached_stat_st stat
;
1173 memcached_return rc
;
1175 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
1176 assert(rc
== MEMCACHED_SUCCESS
);
1177 for (ptr
= list
; *ptr
; ptr
++)
1186 test_return
version_string_test(memcached_st
*memc
)
1188 const char *version_string
;
1190 version_string
= memcached_lib_version();
1192 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1197 test_return
get_stats(memcached_st
*memc
)
1202 memcached_return rc
;
1203 memcached_stat_st
*stat
;
1205 stat
= memcached_stat(memc
, NULL
, &rc
);
1206 assert(rc
== MEMCACHED_SUCCESS
);
1208 assert(rc
== MEMCACHED_SUCCESS
);
1211 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1213 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
1214 assert(rc
== MEMCACHED_SUCCESS
);
1215 for (ptr
= list
; *ptr
; ptr
++);
1220 memcached_stat_free(NULL
, stat
);
1225 test_return
add_host_test(memcached_st
*memc
)
1228 memcached_server_st
*servers
;
1229 memcached_return rc
;
1230 char servername
[]= "0.example.com";
1232 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1234 assert(1 == memcached_server_list_count(servers
));
1236 for (x
= 2; x
< 20; x
++)
1238 char buffer
[SMALL_STRING_LEN
];
1240 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1241 servers
= memcached_server_list_append(servers
, buffer
, 401,
1243 assert(rc
== MEMCACHED_SUCCESS
);
1244 assert(x
== memcached_server_list_count(servers
));
1247 rc
= memcached_server_push(memc
, servers
);
1248 assert(rc
== MEMCACHED_SUCCESS
);
1249 rc
= memcached_server_push(memc
, servers
);
1250 assert(rc
== MEMCACHED_SUCCESS
);
1252 memcached_server_list_free(servers
);
1257 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
1259 return MEMCACHED_SUCCESS
;
1262 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
1264 return MEMCACHED_SUCCESS
;
1267 test_return
callback_test(memcached_st
*memc
)
1269 /* Test User Data */
1273 memcached_return rc
;
1275 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1276 assert(rc
== MEMCACHED_SUCCESS
);
1277 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1278 assert(*test_ptr
== x
);
1281 /* Test Clone Callback */
1283 memcached_clone_func temp_function
;
1284 memcached_return rc
;
1286 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
1287 assert(rc
== MEMCACHED_SUCCESS
);
1288 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1289 assert(temp_function
== clone_test_callback
);
1292 /* Test Cleanup Callback */
1294 memcached_cleanup_func temp_function
;
1295 memcached_return rc
;
1297 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1298 assert(rc
== MEMCACHED_SUCCESS
);
1299 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1300 assert(temp_function
== cleanup_test_callback
);
1306 /* We don't test the behavior itself, we test the switches */
1307 test_return
behavior_test(memcached_st
*memc
)
1309 unsigned long long value
;
1310 unsigned int set
= 1;
1312 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1313 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1316 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1317 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1320 set
= MEMCACHED_HASH_MD5
;
1321 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1322 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1323 assert(value
== MEMCACHED_HASH_MD5
);
1327 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1328 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1331 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1332 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1335 set
= MEMCACHED_HASH_DEFAULT
;
1336 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1337 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1338 assert(value
== MEMCACHED_HASH_DEFAULT
);
1340 set
= MEMCACHED_HASH_CRC
;
1341 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1342 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1343 assert(value
== MEMCACHED_HASH_CRC
);
1345 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1348 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1354 /* Test case provided by Cal Haldenbrand */
1355 test_return
user_supplied_bug1(memcached_st
*memc
)
1357 unsigned int setter
= 1;
1360 unsigned long long total
= 0;
1363 char randomstuff
[6 * 1024];
1364 memcached_return rc
;
1366 memset(randomstuff
, 0, 6 * 1024);
1368 /* We just keep looking at the same values over and over */
1371 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1372 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1376 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1380 size
= (rand() % ( 5 * 1024 ) ) + 400;
1381 memset(randomstuff
, 0, 6 * 1024);
1382 assert(size
< 6 * 1024); /* Being safe here */
1384 for (j
= 0 ; j
< size
;j
++)
1385 randomstuff
[j
] = (char) (rand() % 26) + 97;
1388 sprintf(key
, "%d", x
);
1389 rc
= memcached_set(memc
, key
, strlen(key
),
1390 randomstuff
, strlen(randomstuff
), 10, 0);
1391 /* If we fail, lets try again */
1392 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1393 rc
= memcached_set(memc
, key
, strlen(key
),
1394 randomstuff
, strlen(randomstuff
), 10, 0);
1395 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1401 /* Test case provided by Cal Haldenbrand */
1402 test_return
user_supplied_bug2(memcached_st
*memc
)
1405 unsigned int setter
;
1407 unsigned long long total
;
1410 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1411 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1413 setter
= 20 * 1024576;
1414 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1415 setter
= 20 * 1024576;
1416 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1417 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1418 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1420 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1423 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1425 memcached_return rc
= MEMCACHED_SUCCESS
;
1426 char buffer
[SMALL_STRING_LEN
];
1431 memset(buffer
, 0, SMALL_STRING_LEN
);
1433 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1434 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1435 &val_len
, &flags
, &rc
);
1436 if (rc
!= MEMCACHED_SUCCESS
)
1438 if (rc
== MEMCACHED_NOTFOUND
)
1442 WATCHPOINT_ERROR(rc
);
1456 /* Do a large mget() over all the keys we think exist */
1457 #define KEY_COUNT 3000 // * 1024576
1458 test_return
user_supplied_bug3(memcached_st
*memc
)
1460 memcached_return rc
;
1461 unsigned int setter
;
1464 size_t key_lengths
[KEY_COUNT
];
1467 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1468 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1470 setter
= 20 * 1024576;
1471 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1472 setter
= 20 * 1024576;
1473 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1474 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1475 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1478 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1480 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1481 for (x
= 0; x
< KEY_COUNT
; x
++)
1485 snprintf(buffer
, 30, "%u", x
);
1486 keys
[x
]= strdup(buffer
);
1487 key_lengths
[x
]= strlen(keys
[x
]);
1490 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1491 assert(rc
== MEMCACHED_SUCCESS
);
1493 /* Turn this into a help function */
1495 char return_key
[MEMCACHED_MAX_KEY
];
1496 size_t return_key_length
;
1498 size_t return_value_length
;
1501 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1502 &return_value_length
, &flags
, &rc
)))
1504 assert(return_value
);
1505 assert(rc
== MEMCACHED_SUCCESS
);
1510 for (x
= 0; x
< KEY_COUNT
; x
++)
1517 /* Make sure we behave properly if server list has no values */
1518 test_return
user_supplied_bug4(memcached_st
*memc
)
1520 memcached_return rc
;
1521 char *keys
[]= {"fudge", "son", "food"};
1522 size_t key_length
[]= {5, 3, 4};
1525 char return_key
[MEMCACHED_MAX_KEY
];
1526 size_t return_key_length
;
1528 size_t return_value_length
;
1530 /* Here we free everything before running a bunch of mget tests */
1532 memcached_server_list_free(memc
->hosts
);
1534 memc
->number_of_hosts
= 0;
1538 /* We need to empty the server before continueing test */
1539 rc
= memcached_flush(memc
, 0);
1540 assert(rc
== MEMCACHED_NO_SERVERS
);
1542 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1543 assert(rc
== MEMCACHED_NO_SERVERS
);
1545 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1546 &return_value_length
, &flags
, &rc
)) != NULL
)
1548 assert(return_value
);
1550 assert(!return_value
);
1551 assert(return_value_length
== 0);
1552 assert(rc
== MEMCACHED_NO_SERVERS
);
1554 for (x
= 0; x
< 3; x
++)
1556 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1557 keys
[x
], key_length
[x
],
1558 (time_t)50, (uint32_t)9);
1559 assert(rc
== MEMCACHED_NO_SERVERS
);
1562 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1563 assert(rc
== MEMCACHED_NO_SERVERS
);
1566 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1567 &return_value_length
, &flags
, &rc
)))
1569 assert(return_value
);
1570 assert(rc
== MEMCACHED_SUCCESS
);
1571 assert(return_key_length
== return_value_length
);
1572 assert(!memcmp(return_value
, return_key
, return_value_length
));
1580 #define VALUE_SIZE_BUG5 1048064
1581 test_return
user_supplied_bug5(memcached_st
*memc
)
1583 memcached_return rc
;
1584 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1585 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1586 char return_key
[MEMCACHED_MAX_KEY
];
1587 size_t return_key_length
;
1589 size_t value_length
;
1593 char insert_data
[VALUE_SIZE_BUG5
];
1595 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1596 insert_data
[x
]= rand();
1598 memcached_flush(memc
, 0);
1599 value
= memcached_get(memc
, keys
[0], key_length
[0],
1600 &value_length
, &flags
, &rc
);
1601 assert(value
== NULL
);
1602 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1605 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1606 &value_length
, &flags
, &rc
)))
1610 for (x
= 0; x
< 4; x
++)
1612 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1613 insert_data
, VALUE_SIZE_BUG5
,
1614 (time_t)0, (uint32_t)0);
1615 assert(rc
== MEMCACHED_SUCCESS
);
1618 for (x
= 0; x
< 10; x
++)
1620 value
= memcached_get(memc
, keys
[0], key_length
[0],
1621 &value_length
, &flags
, &rc
);
1625 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1627 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1628 &value_length
, &flags
, &rc
)))
1639 test_return
user_supplied_bug6(memcached_st
*memc
)
1641 memcached_return rc
;
1642 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1643 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1644 char return_key
[MEMCACHED_MAX_KEY
];
1645 size_t return_key_length
;
1647 size_t value_length
;
1651 char insert_data
[VALUE_SIZE_BUG5
];
1653 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1654 insert_data
[x
]= rand();
1656 memcached_flush(memc
, 0);
1657 value
= memcached_get(memc
, keys
[0], key_length
[0],
1658 &value_length
, &flags
, &rc
);
1659 assert(value
== NULL
);
1660 assert(rc
== MEMCACHED_NOTFOUND
);
1661 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1662 assert(rc
== MEMCACHED_SUCCESS
);
1665 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1666 &value_length
, &flags
, &rc
)))
1669 assert(rc
== MEMCACHED_END
);
1671 for (x
= 0; x
< 4; x
++)
1673 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1674 insert_data
, VALUE_SIZE_BUG5
,
1675 (time_t)0, (uint32_t)0);
1676 assert(rc
== MEMCACHED_SUCCESS
);
1679 for (x
= 0; x
< 2; x
++)
1681 value
= memcached_get(memc
, keys
[0], key_length
[0],
1682 &value_length
, &flags
, &rc
);
1686 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1687 assert(rc
== MEMCACHED_SUCCESS
);
1689 /* We test for purge of partial complete fetches */
1690 for (count
= 3; count
; count
--)
1692 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1693 &value_length
, &flags
, &rc
);
1694 assert(rc
== MEMCACHED_SUCCESS
);
1695 assert(!(memcmp(value
, insert_data
, value_length
)));
1696 assert(value_length
);
1704 test_return
user_supplied_bug8(memcached_st
*memc
)
1706 memcached_return rc
;
1708 memcached_st
*clone
;
1710 memcached_server_st
*servers
;
1711 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";
1713 servers
= memcached_servers_parse(server_list
);
1716 mine
= memcached_create(NULL
);
1717 rc
= memcached_server_push(mine
, servers
);
1718 assert(rc
== MEMCACHED_SUCCESS
);
1719 memcached_server_list_free(servers
);
1722 clone
= memcached_clone(NULL
, mine
);
1724 memcached_quit(mine
);
1725 memcached_quit(clone
);
1728 memcached_free(mine
);
1729 memcached_free(clone
);
1734 /* Test flag store/retrieve */
1735 test_return
user_supplied_bug7(memcached_st
*memc
)
1737 memcached_return rc
;
1738 char *keys
= "036790384900";
1739 size_t key_length
= strlen("036790384900");
1740 char return_key
[MEMCACHED_MAX_KEY
];
1741 size_t return_key_length
;
1743 size_t value_length
;
1746 char insert_data
[VALUE_SIZE_BUG5
];
1748 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1749 insert_data
[x
]= rand();
1751 memcached_flush(memc
, 0);
1754 rc
= memcached_set(memc
, keys
, key_length
,
1755 insert_data
, VALUE_SIZE_BUG5
,
1757 assert(rc
== MEMCACHED_SUCCESS
);
1760 value
= memcached_get(memc
, keys
, key_length
,
1761 &value_length
, &flags
, &rc
);
1762 assert(flags
== 245);
1766 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1769 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1770 &value_length
, &flags
, &rc
);
1771 assert(flags
== 245);
1779 test_return
user_supplied_bug9(memcached_st
*memc
)
1781 memcached_return rc
;
1782 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1783 size_t key_length
[3];
1788 char return_key
[MEMCACHED_MAX_KEY
];
1789 size_t return_key_length
;
1791 size_t return_value_length
;
1794 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1795 key_length
[1]= strlen("fudge&*@#");
1796 key_length
[2]= strlen("for^#@&$not");
1799 for (x
= 0; x
< 3; x
++)
1801 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1802 keys
[x
], key_length
[x
],
1803 (time_t)50, (uint32_t)9);
1804 assert(rc
== MEMCACHED_SUCCESS
);
1807 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1808 assert(rc
== MEMCACHED_SUCCESS
);
1810 /* We need to empty the server before continueing test */
1811 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1812 &return_value_length
, &flags
, &rc
)) != NULL
)
1814 assert(return_value
);
1823 /* We are testing with aggressive timeout to get failures */
1824 test_return
user_supplied_bug10(memcached_st
*memc
)
1828 size_t value_length
= 512;
1831 memcached_return rc
;
1832 unsigned int set
= 1;
1833 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1836 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1837 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1839 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1841 value
= (char*)malloc(value_length
* sizeof(char));
1843 for (x
= 0; x
< value_length
; x
++)
1844 value
[x
]= (char) (x
% 127);
1846 for (x
= 1; x
<= 100000; ++x
)
1848 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1850 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1852 if (rc
== MEMCACHED_WRITE_FAILURE
)
1857 memcached_free(mclone
);
1863 We are looking failures in the async protocol
1865 test_return
user_supplied_bug11(memcached_st
*memc
)
1869 size_t value_length
= 512;
1872 memcached_return rc
;
1873 unsigned int set
= 1;
1875 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1877 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1878 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1880 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1882 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1884 assert(timeout
== -1);
1886 value
= (char*)malloc(value_length
* sizeof(char));
1888 for (x
= 0; x
< value_length
; x
++)
1889 value
[x
]= (char) (x
% 127);
1891 for (x
= 1; x
<= 100000; ++x
)
1893 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1897 memcached_free(mclone
);
1903 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1905 test_return
user_supplied_bug12(memcached_st
*memc
)
1907 memcached_return rc
;
1909 size_t value_length
;
1911 uint64_t number_value
;
1913 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1914 &value_length
, &flags
, &rc
);
1915 assert(value
== NULL
);
1916 assert(rc
== MEMCACHED_NOTFOUND
);
1918 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1921 assert(value
== NULL
);
1922 assert(rc
== MEMCACHED_NOTFOUND
);
1924 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1926 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1927 &value_length
, &flags
, &rc
);
1929 assert(rc
== MEMCACHED_SUCCESS
);
1932 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1934 assert(number_value
== 2);
1935 assert(rc
== MEMCACHED_SUCCESS
);
1941 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1942 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1944 test_return
user_supplied_bug13(memcached_st
*memc
)
1946 char key
[] = "key34567890";
1948 memcached_return rc
;
1949 size_t overflowSize
;
1951 char commandFirst
[]= "set key34567890 0 0 ";
1952 char commandLast
[] = " \r\n"; /* first line of command sent to server */
1953 size_t commandLength
;
1956 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
1958 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
1960 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
1962 overflow
= malloc(testSize
);
1963 assert(overflow
!= NULL
);
1965 memset(overflow
, 'x', testSize
);
1966 rc
= memcached_set(memc
, key
, strlen(key
),
1967 overflow
, testSize
, 0, 0);
1968 assert(rc
== MEMCACHED_SUCCESS
);
1977 Test values of many different sizes
1978 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1979 set key34567890 0 0 8169 \r\n
1980 is sent followed by buffer of size 8169, followed by 8169
1982 test_return
user_supplied_bug14(memcached_st
*memc
)
1985 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1986 memcached_return rc
;
1989 size_t value_length
= 18000;
1991 size_t string_length
;
1994 size_t current_length
;
1996 value
= (char*)malloc(value_length
);
1999 for (x
= 0; x
< value_length
; x
++)
2000 value
[x
] = (char) (x
% 127);
2002 for (current_length
= 0; current_length
< value_length
; current_length
++)
2004 rc
= memcached_set(memc
, key
, strlen(key
),
2005 value
, current_length
,
2006 (time_t)0, (uint32_t)0);
2007 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2009 string
= memcached_get(memc
, key
, strlen(key
),
2010 &string_length
, &flags
, &rc
);
2012 assert(rc
== MEMCACHED_SUCCESS
);
2013 assert(string_length
== current_length
);
2014 assert(!memcmp(string
, value
, string_length
));
2025 Look for zero length value problems
2027 test_return
user_supplied_bug15(memcached_st
*memc
)
2030 memcached_return rc
;
2036 for (x
= 0; x
< 2; x
++)
2038 rc
= memcached_set(memc
, key
, strlen(key
),
2040 (time_t)0, (uint32_t)0);
2042 assert(rc
== MEMCACHED_SUCCESS
);
2044 value
= memcached_get(memc
, key
, strlen(key
),
2045 &length
, &flags
, &rc
);
2047 assert(rc
== MEMCACHED_SUCCESS
);
2048 assert(value
== NULL
);
2049 assert(length
== 0);
2052 value
= memcached_get(memc
, key
, strlen(key
),
2053 &length
, &flags
, &rc
);
2055 assert(rc
== MEMCACHED_SUCCESS
);
2056 assert(value
== NULL
);
2057 assert(length
== 0);
2064 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2065 test_return
user_supplied_bug16(memcached_st
*memc
)
2067 memcached_return rc
;
2073 rc
= memcached_set(memc
, key
, strlen(key
),
2075 (time_t)0, UINT32_MAX
);
2077 assert(rc
== MEMCACHED_SUCCESS
);
2079 value
= memcached_get(memc
, key
, strlen(key
),
2080 &length
, &flags
, &rc
);
2082 assert(rc
== MEMCACHED_SUCCESS
);
2083 assert(value
== NULL
);
2084 assert(length
== 0);
2085 assert(flags
== UINT32_MAX
);
2090 /* Check the validity of chinese key*/
2091 test_return
user_supplied_bug17(memcached_st
*memc
)
2093 memcached_return rc
;
2095 char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2100 rc
= memcached_set(memc
, key
, strlen(key
),
2101 value
, strlen(value
),
2104 assert(rc
== MEMCACHED_SUCCESS
);
2106 value2
= memcached_get(memc
, key
, strlen(key
),
2107 &length
, &flags
, &rc
);
2109 assert(length
==strlen(value
));
2110 assert(rc
== MEMCACHED_SUCCESS
);
2111 assert(memcmp(value
, value2
, length
)==0);
2118 test_return
result_static(memcached_st
*memc
)
2120 memcached_result_st result
;
2121 memcached_result_st
*result_ptr
;
2123 result_ptr
= memcached_result_create(memc
, &result
);
2124 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2126 memcached_result_free(&result
);
2131 test_return
result_alloc(memcached_st
*memc
)
2133 memcached_result_st
*result
;
2135 result
= memcached_result_create(memc
, NULL
);
2137 memcached_result_free(result
);
2142 test_return
string_static_null(memcached_st
*memc
)
2144 memcached_string_st string
;
2145 memcached_string_st
*string_ptr
;
2147 string_ptr
= memcached_string_create(memc
, &string
, 0);
2148 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2150 memcached_string_free(&string
);
2155 test_return
string_alloc_null(memcached_st
*memc
)
2157 memcached_string_st
*string
;
2159 string
= memcached_string_create(memc
, NULL
, 0);
2161 memcached_string_free(string
);
2166 test_return
string_alloc_with_size(memcached_st
*memc
)
2168 memcached_string_st
*string
;
2170 string
= memcached_string_create(memc
, NULL
, 1024);
2172 memcached_string_free(string
);
2177 test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2179 memcached_string_st
*string
;
2181 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
2182 assert(string
== NULL
);
2187 test_return
string_alloc_append(memcached_st
*memc
)
2190 char buffer
[SMALL_STRING_LEN
];
2191 memcached_string_st
*string
;
2193 /* Ring the bell! */
2194 memset(buffer
, 6, SMALL_STRING_LEN
);
2196 string
= memcached_string_create(memc
, NULL
, 100);
2199 for (x
= 0; x
< 1024; x
++)
2201 memcached_return rc
;
2202 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2203 assert(rc
== MEMCACHED_SUCCESS
);
2205 memcached_string_free(string
);
2210 test_return
string_alloc_append_toobig(memcached_st
*memc
)
2212 memcached_return rc
;
2214 char buffer
[SMALL_STRING_LEN
];
2215 memcached_string_st
*string
;
2217 /* Ring the bell! */
2218 memset(buffer
, 6, SMALL_STRING_LEN
);
2220 string
= memcached_string_create(memc
, NULL
, 100);
2223 for (x
= 0; x
< 1024; x
++)
2225 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2226 assert(rc
== MEMCACHED_SUCCESS
);
2228 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
2229 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2230 memcached_string_free(string
);
2235 test_return
cleanup_pairs(memcached_st
*memc
)
2237 pairs_free(global_pairs
);
2242 test_return
generate_pairs(memcached_st
*memc
)
2244 unsigned long long x
;
2245 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2246 global_count
= GLOBAL_COUNT
;
2248 for (x
= 0; x
< global_count
; x
++)
2250 global_keys
[x
]= global_pairs
[x
].key
;
2251 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2257 test_return
generate_large_pairs(memcached_st
*memc
)
2259 unsigned long long x
;
2260 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2261 global_count
= GLOBAL2_COUNT
;
2263 for (x
= 0; x
< global_count
; x
++)
2265 global_keys
[x
]= global_pairs
[x
].key
;
2266 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2272 test_return
generate_data(memcached_st
*memc
)
2274 execute_set(memc
, global_pairs
, global_count
);
2279 test_return
generate_data_with_stats(memcached_st
*memc
)
2281 memcached_stat_st
*stat_p
;
2282 memcached_return rc
;
2283 uint32_t host_index
= 0;
2284 execute_set(memc
, global_pairs
, global_count
);
2286 //TODO: hosts used size stats
2287 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2290 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2292 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, (memc
->hosts
)[host_index
].hostname
, (memc
->hosts
)[host_index
].port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
2295 memcached_stat_free(NULL
, stat_p
);
2300 test_return
generate_buffer_data(memcached_st
*memc
)
2305 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2306 generate_data(memc
);
2311 test_return
get_read_count(memcached_st
*memc
)
2314 memcached_return rc
;
2315 memcached_st
*clone
;
2317 clone
= memcached_clone(NULL
, memc
);
2320 memcached_server_add(clone
, "localhost", 6666);
2324 size_t return_value_length
;
2328 for (x
= count
= 0; x
< global_count
; x
++)
2330 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2331 &return_value_length
, &flags
, &rc
);
2332 if (rc
== MEMCACHED_SUCCESS
)
2339 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2342 memcached_free(clone
);
2347 test_return
get_read(memcached_st
*memc
)
2350 memcached_return rc
;
2354 size_t return_value_length
;
2357 for (x
= 0; x
< global_count
; x
++)
2359 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2360 &return_value_length
, &flags
, &rc
);
2362 assert(return_value);
2363 assert(rc == MEMCACHED_SUCCESS);
2365 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2373 test_return
mget_read(memcached_st
*memc
)
2375 memcached_return rc
;
2377 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2378 assert(rc
== MEMCACHED_SUCCESS
);
2379 /* Turn this into a help function */
2381 char return_key
[MEMCACHED_MAX_KEY
];
2382 size_t return_key_length
;
2384 size_t return_value_length
;
2387 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2388 &return_value_length
, &flags
, &rc
)))
2390 assert(return_value
);
2391 assert(rc
== MEMCACHED_SUCCESS
);
2399 test_return
mget_read_result(memcached_st
*memc
)
2401 memcached_return rc
;
2403 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2404 assert(rc
== MEMCACHED_SUCCESS
);
2405 /* Turn this into a help function */
2407 memcached_result_st results_obj
;
2408 memcached_result_st
*results
;
2410 results
= memcached_result_create(memc
, &results_obj
);
2412 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2415 assert(rc
== MEMCACHED_SUCCESS
);
2418 memcached_result_free(&results_obj
);
2424 test_return
mget_read_function(memcached_st
*memc
)
2426 memcached_return rc
;
2427 unsigned int counter
;
2428 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
2430 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2431 assert(rc
== MEMCACHED_SUCCESS
);
2433 callbacks
[0]= &callback_counter
;
2435 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2440 test_return
delete_generate(memcached_st
*memc
)
2444 for (x
= 0; x
< global_count
; x
++)
2446 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2452 test_return
delete_buffer_generate(memcached_st
*memc
)
2458 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2460 for (x
= 0; x
< global_count
; x
++)
2462 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2468 test_return
free_data(memcached_st
*memc
)
2470 pairs_free(global_pairs
);
2475 test_return
add_host_test1(memcached_st
*memc
)
2478 memcached_return rc
;
2479 char servername
[]= "0.example.com";
2480 memcached_server_st
*servers
;
2482 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
2484 assert(1 == memcached_server_list_count(servers
));
2486 for (x
= 2; x
< 20; x
++)
2488 char buffer
[SMALL_STRING_LEN
];
2490 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2491 servers
= memcached_server_list_append(servers
, buffer
, 401,
2493 assert(rc
== MEMCACHED_SUCCESS
);
2494 assert(x
== memcached_server_list_count(servers
));
2497 rc
= memcached_server_push(memc
, servers
);
2498 assert(rc
== MEMCACHED_SUCCESS
);
2499 rc
= memcached_server_push(memc
, servers
);
2500 assert(rc
== MEMCACHED_SUCCESS
);
2502 memcached_server_list_free(servers
);
2507 memcached_return
pre_nonblock(memcached_st
*memc
)
2509 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2511 return MEMCACHED_SUCCESS
;
2514 memcached_return
pre_murmur(memcached_st
*memc
)
2516 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2518 return MEMCACHED_SUCCESS
;
2521 memcached_return
pre_md5(memcached_st
*memc
)
2523 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2525 return MEMCACHED_SUCCESS
;
2528 memcached_return
pre_crc(memcached_st
*memc
)
2530 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2532 return MEMCACHED_SUCCESS
;
2535 memcached_return
pre_hsieh(memcached_st
*memc
)
2537 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2539 return MEMCACHED_SUCCESS
;
2542 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2544 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2546 return MEMCACHED_SUCCESS
;
2549 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2551 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2553 return MEMCACHED_SUCCESS
;
2556 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2558 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2560 return MEMCACHED_SUCCESS
;
2563 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2565 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2567 return MEMCACHED_SUCCESS
;
2570 memcached_return
pre_behavior_ketama(memcached_st
*memc
)
2572 memcached_return rc
;
2575 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
2576 assert(rc
== MEMCACHED_SUCCESS
);
2578 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
2581 return MEMCACHED_SUCCESS
;
2584 memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
2586 memcached_return rc
;
2589 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2590 assert(rc
== MEMCACHED_SUCCESS
);
2592 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2595 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2596 assert(rc
== MEMCACHED_SUCCESS
);
2598 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2599 assert(value
== MEMCACHED_HASH_MD5
);
2600 return MEMCACHED_SUCCESS
;
2603 memcached_return
pre_binary(memcached_st
*memc
)
2605 memcached_return rc
= MEMCACHED_FAILURE
;
2606 memcached_st
*clone
;
2608 clone
= memcached_clone(NULL
, memc
);
2610 // The memcached_version needs to be done on a clone, because the server
2611 // will not toggle protocol on an connection.
2612 memcached_version(clone
);
2614 if (clone
->hosts
[0].major_version
>= 1 && clone
->hosts
[0].minor_version
> 2)
2616 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2617 assert(rc
== MEMCACHED_SUCCESS
);
2618 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2621 memcached_free(clone
);
2625 void my_free(memcached_st
*ptr
, void *mem
)
2630 void *my_malloc(memcached_st
*ptr
, const size_t size
)
2632 return malloc(size
);
2635 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2637 return realloc(mem
, size
);
2640 memcached_return
set_prefix(memcached_st
*memc
)
2642 memcached_return rc
;
2643 const char *key
= "mine";
2646 /* Make sure be default none exists */
2647 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2648 assert(rc
== MEMCACHED_FAILURE
);
2650 /* Test a clean set */
2651 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2652 assert(rc
== MEMCACHED_SUCCESS
);
2654 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2655 assert(memcmp(value
, key
, 4) == 0);
2656 assert(rc
== MEMCACHED_SUCCESS
);
2658 /* Test that we can turn it off */
2659 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2660 assert(rc
== MEMCACHED_SUCCESS
);
2662 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2663 assert(rc
== MEMCACHED_FAILURE
);
2665 /* Now setup for main test */
2666 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2667 assert(rc
== MEMCACHED_SUCCESS
);
2669 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2670 assert(rc
== MEMCACHED_SUCCESS
);
2671 assert(memcmp(value
, key
, 4) == 0);
2673 /* Set to Zero, and then Set to something too large */
2676 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2677 assert(rc
== MEMCACHED_SUCCESS
);
2679 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2680 assert(rc
== MEMCACHED_FAILURE
);
2681 assert(value
== NULL
);
2683 /* Test a long key for failure */
2684 long_key
= "Thisismorethentheallottednumberofcharacters";
2685 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2686 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2688 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2689 long_key
= "This is more then the allotted number of characters";
2690 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2691 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2693 /* Test for a bad prefix, but with a short key */
2694 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
2695 assert(rc
== MEMCACHED_SUCCESS
);
2697 long_key
= "dog cat";
2698 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2699 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2702 return MEMCACHED_SUCCESS
;
2705 memcached_return
set_memory_alloc(memcached_st
*memc
)
2708 memcached_malloc_function test_ptr
;
2709 memcached_return rc
;
2711 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2712 assert(rc
== MEMCACHED_SUCCESS
);
2713 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2714 assert(rc
== MEMCACHED_SUCCESS
);
2715 assert(test_ptr
== my_malloc
);
2719 memcached_realloc_function test_ptr
;
2720 memcached_return rc
;
2722 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2723 assert(rc
== MEMCACHED_SUCCESS
);
2724 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2725 assert(rc
== MEMCACHED_SUCCESS
);
2726 assert(test_ptr
== my_realloc
);
2730 memcached_free_function test_ptr
;
2731 memcached_return rc
;
2733 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2734 assert(rc
== MEMCACHED_SUCCESS
);
2735 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2736 assert(rc
== MEMCACHED_SUCCESS
);
2737 assert(test_ptr
== my_free
);
2740 return MEMCACHED_SUCCESS
;
2743 memcached_return
enable_consistent(memcached_st
*memc
)
2745 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2746 memcached_hash hash
;
2747 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2750 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2751 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2753 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2754 assert(hash
== MEMCACHED_HASH_HSIEH
);
2757 return MEMCACHED_SUCCESS
;
2760 memcached_return
enable_cas(memcached_st
*memc
)
2762 unsigned int set
= 1;
2764 memcached_version(memc
);
2766 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
2767 || memc
->hosts
[0].minor_version
> 2)
2769 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
2771 return MEMCACHED_SUCCESS
;
2774 return MEMCACHED_FAILURE
;
2777 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2779 memcached_version(memc
);
2781 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
2782 || memc
->hosts
[0].minor_version
> 2)
2783 return MEMCACHED_SUCCESS
;
2785 return MEMCACHED_FAILURE
;
2788 memcached_return
pre_unix_socket(memcached_st
*memc
)
2790 memcached_return rc
;
2793 memcached_server_list_free(memc
->hosts
);
2795 memc
->number_of_hosts
= 0;
2797 if (stat("/tmp/memcached.socket", &buf
))
2798 return MEMCACHED_FAILURE
;
2800 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2805 memcached_return
pre_udp(memcached_st
*memc
)
2807 memcached_return rc
;
2809 memcached_server_list_free(memc
->hosts
);
2811 memc
->number_of_hosts
= 0;
2814 return MEMCACHED_FAILURE
;
2816 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2821 memcached_return
pre_nodelay(memcached_st
*memc
)
2823 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2824 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2826 return MEMCACHED_SUCCESS
;
2829 memcached_return
pre_settimer(memcached_st
*memc
)
2831 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
2832 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
2834 return MEMCACHED_SUCCESS
;
2837 memcached_return
poll_timeout(memcached_st
*memc
)
2843 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
2845 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2847 assert(timeout
== 100);
2849 return MEMCACHED_SUCCESS
;
2853 /* Clean the server before beginning testing */
2855 {"flush", 0, flush_test
},
2856 {"init", 0, init_test
},
2857 {"allocation", 0, allocation_test
},
2858 {"server_list_null_test", 0, server_list_null_test
},
2859 {"server_unsort", 0, server_unsort_test
},
2860 {"server_sort", 0, server_sort_test
},
2861 {"server_sort2", 0, server_sort2_test
},
2862 {"clone_test", 0, clone_test
},
2863 {"error", 0, error_test
},
2864 {"set", 0, set_test
},
2865 {"set2", 0, set_test2
},
2866 {"set3", 0, set_test3
},
2867 {"add", 1, add_test
},
2868 {"replace", 1, replace_test
},
2869 {"delete", 1, delete_test
},
2870 {"get", 1, get_test
},
2871 {"get2", 0, get_test2
},
2872 {"get3", 0, get_test3
},
2873 {"get4", 0, get_test4
},
2874 {"stats_servername", 0, stats_servername_test
},
2875 {"increment", 0, increment_test
},
2876 {"decrement", 0, decrement_test
},
2877 {"quit", 0, quit_test
},
2878 {"mget", 1, mget_test
},
2879 {"mget_result", 1, mget_result_test
},
2880 {"mget_result_alloc", 1, mget_result_alloc_test
},
2881 {"mget_result_function", 1, mget_result_function
},
2882 {"get_stats", 0, get_stats
},
2883 {"add_host_test", 0, add_host_test
},
2884 {"get_stats_keys", 0, get_stats_keys
},
2885 {"behavior_test", 0, get_stats_keys
},
2886 {"callback_test", 0, get_stats_keys
},
2887 {"version_string_test", 0, version_string_test
},
2888 {"bad_key", 1, bad_key_test
},
2889 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
2890 {"read_through", 1, read_through
},
2891 {"delete_through", 1, delete_through
},
2895 test_st async_tests
[] ={
2896 {"add", 1, add_wrapper
},
2900 test_st string_tests
[] ={
2901 {"string static with null", 0, string_static_null
},
2902 {"string alloc with null", 0, string_alloc_null
},
2903 {"string alloc with 1K", 0, string_alloc_with_size
},
2904 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2905 {"string append", 0, string_alloc_append
},
2906 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2910 test_st result_tests
[] ={
2911 {"result static", 0, result_static
},
2912 {"result alloc", 0, result_alloc
},
2916 test_st version_1_2_3
[] ={
2917 {"append", 0, append_test
},
2918 {"prepend", 0, prepend_test
},
2919 {"cas", 0, cas_test
},
2920 {"cas2", 0, cas2_test
},
2921 {"append_binary", 0, append_binary_test
},
2925 test_st user_tests
[] ={
2926 {"user_supplied_bug1", 0, user_supplied_bug1
},
2927 {"user_supplied_bug2", 0, user_supplied_bug2
},
2928 {"user_supplied_bug3", 0, user_supplied_bug3
},
2929 {"user_supplied_bug4", 0, user_supplied_bug4
},
2930 {"user_supplied_bug5", 1, user_supplied_bug5
},
2931 {"user_supplied_bug6", 1, user_supplied_bug6
},
2932 {"user_supplied_bug7", 1, user_supplied_bug7
},
2933 {"user_supplied_bug8", 1, user_supplied_bug8
},
2934 {"user_supplied_bug9", 1, user_supplied_bug9
},
2935 {"user_supplied_bug10", 1, user_supplied_bug10
},
2936 {"user_supplied_bug11", 1, user_supplied_bug11
},
2937 {"user_supplied_bug12", 1, user_supplied_bug12
},
2938 {"user_supplied_bug13", 1, user_supplied_bug13
},
2939 {"user_supplied_bug14", 1, user_supplied_bug14
},
2940 {"user_supplied_bug15", 1, user_supplied_bug15
},
2941 {"user_supplied_bug16", 1, user_supplied_bug16
},
2942 {"user_supplied_bug17", 1, user_supplied_bug17
},
2946 test_st generate_tests
[] ={
2947 {"generate_pairs", 1, generate_pairs
},
2948 {"generate_data", 1, generate_data
},
2949 {"get_read", 0, get_read
},
2950 {"delete_generate", 0, delete_generate
},
2951 {"generate_buffer_data", 1, generate_buffer_data
},
2952 {"delete_buffer", 0, delete_buffer_generate
},
2953 {"generate_data", 1, generate_data
},
2954 {"mget_read", 0, mget_read
},
2955 {"mget_read_result", 0, mget_read_result
},
2956 {"mget_read_function", 0, mget_read_function
},
2957 {"cleanup", 1, cleanup_pairs
},
2958 {"generate_large_pairs", 1, generate_large_pairs
},
2959 {"generate_data", 1, generate_data
},
2960 {"generate_buffer_data", 1, generate_buffer_data
},
2961 {"cleanup", 1, cleanup_pairs
},
2965 test_st consistent_tests
[] ={
2966 {"generate_pairs", 1, generate_pairs
},
2967 {"generate_data", 1, generate_data
},
2968 {"get_read", 0, get_read_count
},
2969 {"cleanup", 1, cleanup_pairs
},
2973 test_st consistent_weighted_tests
[] ={
2974 {"generate_pairs", 1, generate_pairs
},
2975 {"generate_data", 1, generate_data_with_stats
},
2976 {"get_read", 0, get_read_count
},
2977 {"cleanup", 1, cleanup_pairs
},
2981 collection_st collection
[] ={
2982 {"block", 0, 0, tests
},
2983 {"binary", pre_binary
, 0, tests
},
2984 {"nonblock", pre_nonblock
, 0, tests
},
2985 {"nodelay", pre_nodelay
, 0, tests
},
2986 {"settimer", pre_settimer
, 0, tests
},
2987 {"md5", pre_md5
, 0, tests
},
2988 {"crc", pre_crc
, 0, tests
},
2989 {"hsieh", pre_hsieh
, 0, tests
},
2990 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2991 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2992 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2993 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2994 {"ketama", pre_behavior_ketama
, 0, tests
},
2995 {"unix_socket", pre_unix_socket
, 0, tests
},
2996 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2997 {"poll_timeout", poll_timeout
, 0, tests
},
2998 {"gets", enable_cas
, 0, tests
},
2999 {"consistent", enable_consistent
, 0, tests
},
3000 {"memory_allocators", set_memory_alloc
, 0, tests
},
3001 {"prefix", set_prefix
, 0, tests
},
3002 // {"udp", pre_udp, 0, tests},
3003 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
3004 {"string", 0, 0, string_tests
},
3005 {"result", 0, 0, result_tests
},
3006 {"async", pre_nonblock
, 0, async_tests
},
3007 {"user", 0, 0, user_tests
},
3008 {"generate", 0, 0, generate_tests
},
3009 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
3010 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
3011 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
3012 {"generate_md5", pre_md5
, 0, generate_tests
},
3013 {"generate_murmur", pre_murmur
, 0, generate_tests
},
3014 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
3015 {"consistent_not", 0, 0, consistent_tests
},
3016 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
3017 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
3021 #define SERVERS_TO_CREATE 5
3023 void *world_create(void)
3025 server_startup_st
*construct
;
3027 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
3028 memset(construct
, 0, sizeof(server_startup_st
));
3029 construct
->count
= SERVERS_TO_CREATE
;
3031 server_startup(construct
);
3036 void world_destroy(void *p
)
3038 server_startup_st
*construct
= (server_startup_st
*)p
;
3039 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
3040 memcached_server_list_free(servers
);
3042 server_shutdown(construct
);
3046 void get_world(world_st
*world
)
3048 world
->collections
= collection
;
3049 world
->create
= world_create
;
3050 world
->destroy
= world_destroy
;