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);
2117 test_return
result_static(memcached_st
*memc
)
2119 memcached_result_st result
;
2120 memcached_result_st
*result_ptr
;
2122 result_ptr
= memcached_result_create(memc
, &result
);
2123 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2125 memcached_result_free(&result
);
2130 test_return
result_alloc(memcached_st
*memc
)
2132 memcached_result_st
*result
;
2134 result
= memcached_result_create(memc
, NULL
);
2136 memcached_result_free(result
);
2141 test_return
string_static_null(memcached_st
*memc
)
2143 memcached_string_st string
;
2144 memcached_string_st
*string_ptr
;
2146 string_ptr
= memcached_string_create(memc
, &string
, 0);
2147 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2149 memcached_string_free(&string
);
2154 test_return
string_alloc_null(memcached_st
*memc
)
2156 memcached_string_st
*string
;
2158 string
= memcached_string_create(memc
, NULL
, 0);
2160 memcached_string_free(string
);
2165 test_return
string_alloc_with_size(memcached_st
*memc
)
2167 memcached_string_st
*string
;
2169 string
= memcached_string_create(memc
, NULL
, 1024);
2171 memcached_string_free(string
);
2176 test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2178 memcached_string_st
*string
;
2180 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
2181 assert(string
== NULL
);
2186 test_return
string_alloc_append(memcached_st
*memc
)
2189 char buffer
[SMALL_STRING_LEN
];
2190 memcached_string_st
*string
;
2192 /* Ring the bell! */
2193 memset(buffer
, 6, SMALL_STRING_LEN
);
2195 string
= memcached_string_create(memc
, NULL
, 100);
2198 for (x
= 0; x
< 1024; x
++)
2200 memcached_return rc
;
2201 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2202 assert(rc
== MEMCACHED_SUCCESS
);
2204 memcached_string_free(string
);
2209 test_return
string_alloc_append_toobig(memcached_st
*memc
)
2211 memcached_return rc
;
2213 char buffer
[SMALL_STRING_LEN
];
2214 memcached_string_st
*string
;
2216 /* Ring the bell! */
2217 memset(buffer
, 6, SMALL_STRING_LEN
);
2219 string
= memcached_string_create(memc
, NULL
, 100);
2222 for (x
= 0; x
< 1024; x
++)
2224 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2225 assert(rc
== MEMCACHED_SUCCESS
);
2227 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
2228 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2229 memcached_string_free(string
);
2234 test_return
cleanup_pairs(memcached_st
*memc
)
2236 pairs_free(global_pairs
);
2241 test_return
generate_pairs(memcached_st
*memc
)
2243 unsigned long long x
;
2244 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2245 global_count
= GLOBAL_COUNT
;
2247 for (x
= 0; x
< global_count
; x
++)
2249 global_keys
[x
]= global_pairs
[x
].key
;
2250 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2256 test_return
generate_large_pairs(memcached_st
*memc
)
2258 unsigned long long x
;
2259 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2260 global_count
= GLOBAL2_COUNT
;
2262 for (x
= 0; x
< global_count
; x
++)
2264 global_keys
[x
]= global_pairs
[x
].key
;
2265 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2271 test_return
generate_data(memcached_st
*memc
)
2273 execute_set(memc
, global_pairs
, global_count
);
2278 test_return
generate_data_with_stats(memcached_st
*memc
)
2280 memcached_stat_st
*stat_p
= NULL
;
2281 memcached_return rc
;
2283 execute_set(memc
, global_pairs
, global_count
);
2285 //TODO: hosts used size stats
2286 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2287 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; ++host_index
)
2289 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
);
2295 test_return
generate_buffer_data(memcached_st
*memc
)
2300 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2301 generate_data(memc
);
2306 test_return
get_read_count(memcached_st
*memc
)
2309 memcached_return rc
;
2310 memcached_st
*clone
;
2312 clone
= memcached_clone(NULL
, memc
);
2315 memcached_server_add(clone
, "localhost", 6666);
2319 size_t return_value_length
;
2323 for (x
= count
= 0; x
< global_count
; x
++)
2325 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2326 &return_value_length
, &flags
, &rc
);
2327 if (rc
== MEMCACHED_SUCCESS
)
2334 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2337 memcached_free(clone
);
2342 test_return
get_read(memcached_st
*memc
)
2345 memcached_return rc
;
2349 size_t return_value_length
;
2352 for (x
= 0; x
< global_count
; x
++)
2354 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2355 &return_value_length
, &flags
, &rc
);
2357 assert(return_value);
2358 assert(rc == MEMCACHED_SUCCESS);
2360 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2368 test_return
mget_read(memcached_st
*memc
)
2370 memcached_return rc
;
2372 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2373 assert(rc
== MEMCACHED_SUCCESS
);
2374 /* Turn this into a help function */
2376 char return_key
[MEMCACHED_MAX_KEY
];
2377 size_t return_key_length
;
2379 size_t return_value_length
;
2382 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2383 &return_value_length
, &flags
, &rc
)))
2385 assert(return_value
);
2386 assert(rc
== MEMCACHED_SUCCESS
);
2394 test_return
mget_read_result(memcached_st
*memc
)
2396 memcached_return rc
;
2398 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2399 assert(rc
== MEMCACHED_SUCCESS
);
2400 /* Turn this into a help function */
2402 memcached_result_st results_obj
;
2403 memcached_result_st
*results
;
2405 results
= memcached_result_create(memc
, &results_obj
);
2407 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2410 assert(rc
== MEMCACHED_SUCCESS
);
2413 memcached_result_free(&results_obj
);
2419 test_return
mget_read_function(memcached_st
*memc
)
2421 memcached_return rc
;
2422 unsigned int counter
;
2423 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
2425 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2426 assert(rc
== MEMCACHED_SUCCESS
);
2428 callbacks
[0]= &callback_counter
;
2430 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2435 test_return
delete_generate(memcached_st
*memc
)
2439 for (x
= 0; x
< global_count
; x
++)
2441 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2447 test_return
delete_buffer_generate(memcached_st
*memc
)
2453 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2455 for (x
= 0; x
< global_count
; x
++)
2457 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2463 test_return
free_data(memcached_st
*memc
)
2465 pairs_free(global_pairs
);
2470 test_return
add_host_test1(memcached_st
*memc
)
2473 memcached_return rc
;
2474 char servername
[]= "0.example.com";
2475 memcached_server_st
*servers
;
2477 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
2479 assert(1 == memcached_server_list_count(servers
));
2481 for (x
= 2; x
< 20; x
++)
2483 char buffer
[SMALL_STRING_LEN
];
2485 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2486 servers
= memcached_server_list_append(servers
, buffer
, 401,
2488 assert(rc
== MEMCACHED_SUCCESS
);
2489 assert(x
== memcached_server_list_count(servers
));
2492 rc
= memcached_server_push(memc
, servers
);
2493 assert(rc
== MEMCACHED_SUCCESS
);
2494 rc
= memcached_server_push(memc
, servers
);
2495 assert(rc
== MEMCACHED_SUCCESS
);
2497 memcached_server_list_free(servers
);
2502 memcached_return
pre_nonblock(memcached_st
*memc
)
2504 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2506 return MEMCACHED_SUCCESS
;
2509 memcached_return
pre_murmur(memcached_st
*memc
)
2511 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2513 return MEMCACHED_SUCCESS
;
2516 memcached_return
pre_md5(memcached_st
*memc
)
2518 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2520 return MEMCACHED_SUCCESS
;
2523 memcached_return
pre_crc(memcached_st
*memc
)
2525 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2527 return MEMCACHED_SUCCESS
;
2530 memcached_return
pre_hsieh(memcached_st
*memc
)
2532 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2534 return MEMCACHED_SUCCESS
;
2537 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2539 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2541 return MEMCACHED_SUCCESS
;
2544 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2546 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2548 return MEMCACHED_SUCCESS
;
2551 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2553 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2555 return MEMCACHED_SUCCESS
;
2558 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2560 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2562 return MEMCACHED_SUCCESS
;
2565 memcached_return
pre_behavior_ketama(memcached_st
*memc
)
2567 memcached_return rc
;
2570 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
2571 assert(rc
== MEMCACHED_SUCCESS
);
2573 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
2576 return MEMCACHED_SUCCESS
;
2579 memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
2581 memcached_return rc
;
2584 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2585 assert(rc
== MEMCACHED_SUCCESS
);
2587 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2590 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2591 assert(rc
== MEMCACHED_SUCCESS
);
2593 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2594 assert(value
== MEMCACHED_HASH_MD5
);
2595 return MEMCACHED_SUCCESS
;
2598 memcached_return
pre_binary(memcached_st
*memc
)
2600 memcached_return rc
;
2602 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2603 assert(rc
== MEMCACHED_SUCCESS
);
2605 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2607 return MEMCACHED_SUCCESS
;
2610 void my_free(memcached_st
*ptr
, void *mem
)
2615 void *my_malloc(memcached_st
*ptr
, const size_t size
)
2617 return malloc(size
);
2620 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2622 return realloc(mem
, size
);
2625 memcached_return
set_prefix(memcached_st
*memc
)
2627 memcached_return rc
;
2628 const char *key
= "mine";
2631 /* Make sure be default none exists */
2632 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2633 assert(rc
== MEMCACHED_FAILURE
);
2635 /* Test a clean set */
2636 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2637 assert(rc
== MEMCACHED_SUCCESS
);
2639 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2640 assert(memcmp(value
, key
, 4) == 0);
2641 assert(rc
== MEMCACHED_SUCCESS
);
2643 /* Test that we can turn it off */
2644 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2645 assert(rc
== MEMCACHED_SUCCESS
);
2647 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2648 assert(rc
== MEMCACHED_FAILURE
);
2650 /* Now setup for main test */
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(rc
== MEMCACHED_SUCCESS
);
2656 assert(memcmp(value
, key
, 4) == 0);
2658 /* Set to Zero, and then Set to something too large */
2661 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2662 assert(rc
== MEMCACHED_SUCCESS
);
2664 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2665 assert(rc
== MEMCACHED_FAILURE
);
2666 assert(value
== NULL
);
2668 /* Test a long key for failure */
2669 long_key
= "Thisismorethentheallottednumberofcharacters";
2670 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2671 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2673 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2674 long_key
= "This is more then the allotted number of characters";
2675 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2676 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2678 /* Test for a bad prefix, but with a short key */
2679 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
2680 assert(rc
== MEMCACHED_SUCCESS
);
2682 long_key
= "dog cat";
2683 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2684 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2687 return MEMCACHED_SUCCESS
;
2690 memcached_return
set_memory_alloc(memcached_st
*memc
)
2693 memcached_malloc_function test_ptr
;
2694 memcached_return rc
;
2696 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2697 assert(rc
== MEMCACHED_SUCCESS
);
2698 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2699 assert(rc
== MEMCACHED_SUCCESS
);
2700 assert(test_ptr
== my_malloc
);
2704 memcached_realloc_function test_ptr
;
2705 memcached_return rc
;
2707 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2708 assert(rc
== MEMCACHED_SUCCESS
);
2709 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2710 assert(rc
== MEMCACHED_SUCCESS
);
2711 assert(test_ptr
== my_realloc
);
2715 memcached_free_function test_ptr
;
2716 memcached_return rc
;
2718 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2719 assert(rc
== MEMCACHED_SUCCESS
);
2720 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2721 assert(rc
== MEMCACHED_SUCCESS
);
2722 assert(test_ptr
== my_free
);
2725 return MEMCACHED_SUCCESS
;
2728 memcached_return
enable_consistent(memcached_st
*memc
)
2730 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2731 memcached_hash hash
;
2732 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2735 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2736 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2738 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2739 assert(hash
== MEMCACHED_HASH_HSIEH
);
2742 return MEMCACHED_SUCCESS
;
2745 memcached_return
enable_cas(memcached_st
*memc
)
2747 unsigned int set
= 1;
2749 memcached_version(memc
);
2751 if (memc
->hosts
[0].major_version
>= 1 &&
2752 (memc
->hosts
[0].minor_version
== 2 &&
2753 memc
->hosts
[0].micro_version
>= 4) || memc
->hosts
[0].minor_version
> 2)
2755 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
2757 return MEMCACHED_SUCCESS
;
2760 return MEMCACHED_FAILURE
;
2763 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2765 memcached_version(memc
);
2767 if (memc
->hosts
[0].major_version
>= 1 &&
2768 (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4)
2769 || memc
->hosts
[0].minor_version
> 2)
2770 return MEMCACHED_SUCCESS
;
2772 return MEMCACHED_FAILURE
;
2775 memcached_return
pre_unix_socket(memcached_st
*memc
)
2777 memcached_return rc
;
2780 memcached_server_list_free(memc
->hosts
);
2782 memc
->number_of_hosts
= 0;
2784 if (stat("/tmp/memcached.socket", &buf
))
2785 return MEMCACHED_FAILURE
;
2787 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2792 memcached_return
pre_udp(memcached_st
*memc
)
2794 memcached_return rc
;
2796 memcached_server_list_free(memc
->hosts
);
2798 memc
->number_of_hosts
= 0;
2801 return MEMCACHED_FAILURE
;
2803 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2808 memcached_return
pre_nodelay(memcached_st
*memc
)
2810 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2811 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2813 return MEMCACHED_SUCCESS
;
2816 memcached_return
poll_timeout(memcached_st
*memc
)
2822 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
2824 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2826 assert(timeout
== 100);
2828 return MEMCACHED_SUCCESS
;
2832 /* Clean the server before beginning testing */
2834 {"flush", 0, flush_test
},
2835 {"init", 0, init_test
},
2836 {"allocation", 0, allocation_test
},
2837 {"server_list_null_test", 0, server_list_null_test
},
2838 {"server_unsort", 0, server_unsort_test
},
2839 {"server_sort", 0, server_sort_test
},
2840 {"server_sort2", 0, server_sort2_test
},
2841 {"clone_test", 0, clone_test
},
2842 {"error", 0, error_test
},
2843 {"set", 0, set_test
},
2844 {"set2", 0, set_test2
},
2845 {"set3", 0, set_test3
},
2846 {"add", 1, add_test
},
2847 {"replace", 1, replace_test
},
2848 {"delete", 1, delete_test
},
2849 {"get", 1, get_test
},
2850 {"get2", 0, get_test2
},
2851 {"get3", 0, get_test3
},
2852 {"get4", 0, get_test4
},
2853 {"stats_servername", 0, stats_servername_test
},
2854 {"increment", 0, increment_test
},
2855 {"decrement", 0, decrement_test
},
2856 {"quit", 0, quit_test
},
2857 {"mget", 1, mget_test
},
2858 {"mget_result", 1, mget_result_test
},
2859 {"mget_result_alloc", 1, mget_result_alloc_test
},
2860 {"mget_result_function", 1, mget_result_function
},
2861 {"get_stats", 0, get_stats
},
2862 {"add_host_test", 0, add_host_test
},
2863 {"get_stats_keys", 0, get_stats_keys
},
2864 {"behavior_test", 0, get_stats_keys
},
2865 {"callback_test", 0, get_stats_keys
},
2866 {"version_string_test", 0, version_string_test
},
2867 {"bad_key", 1, bad_key_test
},
2868 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
2869 {"read_through", 1, read_through
},
2870 {"delete_through", 1, delete_through
},
2874 test_st async_tests
[] ={
2875 {"add", 1, add_wrapper
},
2879 test_st string_tests
[] ={
2880 {"string static with null", 0, string_static_null
},
2881 {"string alloc with null", 0, string_alloc_null
},
2882 {"string alloc with 1K", 0, string_alloc_with_size
},
2883 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2884 {"string append", 0, string_alloc_append
},
2885 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2889 test_st result_tests
[] ={
2890 {"result static", 0, result_static
},
2891 {"result alloc", 0, result_alloc
},
2895 test_st version_1_2_3
[] ={
2896 {"append", 0, append_test
},
2897 {"prepend", 0, prepend_test
},
2898 {"cas", 0, cas_test
},
2899 {"cas2", 0, cas2_test
},
2900 {"append_binary", 0, append_binary_test
},
2904 test_st user_tests
[] ={
2905 {"user_supplied_bug1", 0, user_supplied_bug1
},
2906 {"user_supplied_bug2", 0, user_supplied_bug2
},
2907 {"user_supplied_bug3", 0, user_supplied_bug3
},
2908 {"user_supplied_bug4", 0, user_supplied_bug4
},
2909 {"user_supplied_bug5", 1, user_supplied_bug5
},
2910 {"user_supplied_bug6", 1, user_supplied_bug6
},
2911 {"user_supplied_bug7", 1, user_supplied_bug7
},
2912 {"user_supplied_bug8", 1, user_supplied_bug8
},
2913 {"user_supplied_bug9", 1, user_supplied_bug9
},
2914 {"user_supplied_bug10", 1, user_supplied_bug10
},
2915 {"user_supplied_bug11", 1, user_supplied_bug11
},
2916 {"user_supplied_bug12", 1, user_supplied_bug12
},
2917 {"user_supplied_bug13", 1, user_supplied_bug13
},
2918 {"user_supplied_bug14", 1, user_supplied_bug14
},
2919 {"user_supplied_bug15", 1, user_supplied_bug15
},
2920 {"user_supplied_bug16", 1, user_supplied_bug16
},
2921 {"user_supplied_bug17", 1, user_supplied_bug17
},
2925 test_st generate_tests
[] ={
2926 {"generate_pairs", 1, generate_pairs
},
2927 {"generate_data", 1, generate_data
},
2928 {"get_read", 0, get_read
},
2929 {"delete_generate", 0, delete_generate
},
2930 {"generate_buffer_data", 1, generate_buffer_data
},
2931 {"delete_buffer", 0, delete_buffer_generate
},
2932 {"generate_data", 1, generate_data
},
2933 {"mget_read", 0, mget_read
},
2934 {"mget_read_result", 0, mget_read_result
},
2935 {"mget_read_function", 0, mget_read_function
},
2936 {"cleanup", 1, cleanup_pairs
},
2937 {"generate_large_pairs", 1, generate_large_pairs
},
2938 {"generate_data", 1, generate_data
},
2939 {"generate_buffer_data", 1, generate_buffer_data
},
2940 {"cleanup", 1, cleanup_pairs
},
2944 test_st consistent_tests
[] ={
2945 {"generate_pairs", 1, generate_pairs
},
2946 {"generate_data", 1, generate_data
},
2947 {"get_read", 0, get_read_count
},
2948 {"cleanup", 1, cleanup_pairs
},
2952 test_st consistent_weighted_tests
[] ={
2953 {"generate_pairs", 1, generate_pairs
},
2954 {"generate_data", 1, generate_data_with_stats
},
2955 {"get_read", 0, get_read_count
},
2956 {"cleanup", 1, cleanup_pairs
},
2960 collection_st collection
[] ={
2961 {"block", 0, 0, tests
},
2962 {"binary", pre_binary
, 0, tests
},
2963 {"nonblock", pre_nonblock
, 0, tests
},
2964 {"nodelay", pre_nodelay
, 0, tests
},
2965 {"md5", pre_md5
, 0, tests
},
2966 {"crc", pre_crc
, 0, tests
},
2967 {"hsieh", pre_hsieh
, 0, tests
},
2968 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2969 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2970 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2971 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2972 {"ketama", pre_behavior_ketama
, 0, tests
},
2973 {"unix_socket", pre_unix_socket
, 0, tests
},
2974 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2975 {"poll_timeout", poll_timeout
, 0, tests
},
2976 {"gets", enable_cas
, 0, tests
},
2977 {"consistent", enable_consistent
, 0, tests
},
2978 {"memory_allocators", set_memory_alloc
, 0, tests
},
2979 {"prefix", set_prefix
, 0, tests
},
2980 // {"udp", pre_udp, 0, tests},
2981 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2982 {"string", 0, 0, string_tests
},
2983 {"result", 0, 0, result_tests
},
2984 {"async", pre_nonblock
, 0, async_tests
},
2985 {"user", 0, 0, user_tests
},
2986 {"generate", 0, 0, generate_tests
},
2987 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2988 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
2989 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2990 {"generate_md5", pre_md5
, 0, generate_tests
},
2991 {"generate_murmur", pre_murmur
, 0, generate_tests
},
2992 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2993 {"consistent_not", 0, 0, consistent_tests
},
2994 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
2995 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
2999 #define SERVERS_TO_CREATE 5
3001 void *world_create(void)
3003 server_startup_st
*construct
;
3005 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
3006 memset(construct
, 0, sizeof(server_startup_st
));
3007 construct
->count
= SERVERS_TO_CREATE
;
3009 server_startup(construct
);
3014 void world_destroy(void *p
)
3016 server_startup_st
*construct
= (server_startup_st
*)p
;
3017 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
3018 memcached_server_list_free(servers
);
3020 server_shutdown(construct
);
3024 void get_world(world_st
*world
)
3026 world
->collections
= collection
;
3027 world
->create
= world_create
;
3028 world
->destroy
= world_destroy
;