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 100000
28 #define GLOBAL2_COUNT 1000
29 static uint32_t global_count
;
31 static pairs_st
*global_pairs
;
32 static char *global_keys
[GLOBAL_COUNT
];
33 static size_t global_keys_length
[GLOBAL_COUNT
];
35 test_return
init_test(memcached_st
*not_used
)
39 (void)memcached_create(&memc
);
40 memcached_free(&memc
);
45 test_return
server_list_null_test(memcached_st
*ptr
)
47 memcached_server_st
*server_list
;
50 server_list
= memcached_server_list_append(NULL
, NULL
, 0, NULL
);
51 assert(server_list
== NULL
);
53 server_list
= memcached_server_list_append(NULL
, "localhost", 0, NULL
);
54 assert(server_list
== NULL
);
56 server_list
= memcached_server_list_append(NULL
, NULL
, 0, &rc
);
57 assert(server_list
== NULL
);
62 #define TEST_PORT_COUNT 7
63 uint32_t test_ports
[TEST_PORT_COUNT
];
65 memcached_return
server_display_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
68 uint32_t bigger
= *((uint32_t *)(context
));
69 assert(bigger
<= server
->port
);
70 *((uint32_t *)(context
))= server
->port
;
72 return MEMCACHED_SUCCESS
;
75 test_return
server_sort_test(memcached_st
*ptr
)
78 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
80 memcached_server_function callbacks
[1];
81 memcached_st
*local_memc
;
83 local_memc
= memcached_create(NULL
);
85 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
87 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
89 test_ports
[x
]= random() % 64000;
90 rc
= memcached_server_add(local_memc
, "localhost", test_ports
[x
]);
91 assert(local_memc
->number_of_hosts
== x
+1);
92 assert(local_memc
->hosts
[0].count
== x
+1);
93 assert(rc
== MEMCACHED_SUCCESS
);
96 callbacks
[0]= server_display_function
;
97 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
100 memcached_free(local_memc
);
105 test_return
server_sort2_test(memcached_st
*ptr
)
107 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
109 memcached_server_function callbacks
[1];
110 memcached_st
*local_memc
;
112 local_memc
= memcached_create(NULL
);
114 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
115 assert(rc
== MEMCACHED_SUCCESS
);
117 rc
= memcached_server_add(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043);
118 assert(rc
== MEMCACHED_SUCCESS
);
120 rc
= memcached_server_add(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042);
121 assert(rc
== MEMCACHED_SUCCESS
);
123 callbacks
[0]= server_display_function
;
124 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
127 memcached_free(local_memc
);
132 memcached_return
server_display_unsort_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
135 uint32_t x
= *((uint32_t *)(context
));
137 assert(test_ports
[x
] == server
->port
);
138 *((uint32_t *)(context
))= ++x
;
140 return MEMCACHED_SUCCESS
;
143 test_return
server_unsort_test(memcached_st
*ptr
)
146 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
147 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
149 memcached_server_function callbacks
[1];
150 memcached_st
*local_memc
;
152 local_memc
= memcached_create(NULL
);
155 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
157 test_ports
[x
]= random() % 64000;
158 rc
= memcached_server_add(local_memc
, "localhost", test_ports
[x
]);
159 assert(local_memc
->number_of_hosts
== x
+1);
160 assert(local_memc
->hosts
[0].count
== x
+1);
161 assert(rc
== MEMCACHED_SUCCESS
);
164 callbacks
[0]= server_display_unsort_function
;
165 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
167 /* Now we sort old data! */
168 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
169 callbacks
[0]= server_display_function
;
170 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
173 memcached_free(local_memc
);
178 test_return
allocation_test(memcached_st
*not_used
)
181 memc
= memcached_create(NULL
);
183 memcached_free(memc
);
188 test_return
clone_test(memcached_st
*memc
)
193 clone
= memcached_clone(NULL
, NULL
);
195 memcached_free(clone
);
198 /* Can we init from null? */
201 clone
= memcached_clone(NULL
, memc
);
203 memcached_free(clone
);
206 /* Can we init from struct? */
208 memcached_st declared_clone
;
210 clone
= memcached_clone(&declared_clone
, NULL
);
212 memcached_free(clone
);
215 /* Can we init from struct? */
217 memcached_st declared_clone
;
219 clone
= memcached_clone(&declared_clone
, memc
);
221 memcached_free(clone
);
227 test_return
connection_test(memcached_st
*memc
)
231 rc
= memcached_server_add(memc
, "localhost", 0);
232 assert(rc
== MEMCACHED_SUCCESS
);
237 test_return
error_test(memcached_st
*memc
)
241 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
243 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
249 test_return
set_test(memcached_st
*memc
)
253 char *value
= "when we sanitize";
255 rc
= memcached_set(memc
, key
, strlen(key
),
256 value
, strlen(value
),
257 (time_t)0, (uint32_t)0);
258 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
263 test_return
append_test(memcached_st
*memc
)
271 rc
= memcached_flush(memc
, 0);
272 assert(rc
== MEMCACHED_SUCCESS
);
274 rc
= memcached_set(memc
, key
, strlen(key
),
275 value
, strlen(value
),
276 (time_t)0, (uint32_t)0);
277 assert(rc
== MEMCACHED_SUCCESS
);
279 rc
= memcached_append(memc
, key
, strlen(key
),
280 " the", strlen(" the"),
281 (time_t)0, (uint32_t)0);
282 assert(rc
== MEMCACHED_SUCCESS
);
284 rc
= memcached_append(memc
, key
, strlen(key
),
285 " people", strlen(" people"),
286 (time_t)0, (uint32_t)0);
287 assert(rc
== MEMCACHED_SUCCESS
);
289 value
= memcached_get(memc
, key
, strlen(key
),
290 &value_length
, &flags
, &rc
);
291 assert(!memcmp(value
, "we the people", strlen("we the people")));
292 assert(strlen("we the people") == value_length
);
293 assert(rc
== MEMCACHED_SUCCESS
);
299 test_return
append_binary_test(memcached_st
*memc
)
302 char *key
= "numbers";
303 unsigned int *store_ptr
;
304 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
310 rc
= memcached_flush(memc
, 0);
311 assert(rc
== MEMCACHED_SUCCESS
);
313 rc
= memcached_set(memc
,
316 (time_t)0, (uint32_t)0);
317 assert(rc
== MEMCACHED_SUCCESS
);
319 for (x
= 0; store_list
[x
] ; x
++)
321 rc
= memcached_append(memc
,
323 (char *)&store_list
[x
], sizeof(unsigned int),
324 (time_t)0, (uint32_t)0);
325 assert(rc
== MEMCACHED_SUCCESS
);
328 value
= memcached_get(memc
, key
, strlen(key
),
329 &value_length
, &flags
, &rc
);
330 assert((value_length
== (sizeof(unsigned int) * x
)));
331 assert(rc
== MEMCACHED_SUCCESS
);
333 store_ptr
= (unsigned int *)value
;
335 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
337 assert(*store_ptr
== store_list
[x
++]);
345 test_return
cas2_test(memcached_st
*memc
)
348 char *keys
[]= {"fudge", "son", "food"};
349 size_t key_length
[]= {5, 3, 4};
350 char *value
= "we the people";
351 size_t value_length
= strlen("we the people");
353 memcached_result_st results_obj
;
354 memcached_result_st
*results
;
357 rc
= memcached_flush(memc
, 0);
358 assert(rc
== MEMCACHED_SUCCESS
);
360 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
362 for (x
= 0; x
< 3; x
++)
364 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
365 keys
[x
], key_length
[x
],
366 (time_t)50, (uint32_t)9);
367 assert(rc
== MEMCACHED_SUCCESS
);
370 rc
= memcached_mget(memc
, keys
, key_length
, 3);
372 results
= memcached_result_create(memc
, &results_obj
);
374 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
376 assert(results
->cas
);
377 assert(rc
== MEMCACHED_SUCCESS
);
378 WATCHPOINT_ASSERT(memcached_result_cas(results
));
380 assert(!memcmp(value
, "we the people", strlen("we the people")));
381 assert(strlen("we the people") == value_length
);
382 assert(rc
== MEMCACHED_SUCCESS
);
384 memcached_result_free(&results_obj
);
389 test_return
cas_test(memcached_st
*memc
)
393 size_t key_length
= strlen("fun");
394 char *value
= "we the people";
395 size_t value_length
= strlen("we the people");
396 memcached_result_st results_obj
;
397 memcached_result_st
*results
;
400 rc
= memcached_flush(memc
, 0);
401 assert(rc
== MEMCACHED_SUCCESS
);
403 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
405 rc
= memcached_set(memc
, key
, strlen(key
),
406 value
, strlen(value
),
407 (time_t)0, (uint32_t)0);
408 assert(rc
== MEMCACHED_SUCCESS
);
410 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
412 results
= memcached_result_create(memc
, &results_obj
);
414 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
416 assert(rc
== MEMCACHED_SUCCESS
);
417 WATCHPOINT_ASSERT(memcached_result_cas(results
));
419 assert(!memcmp(value
, "we the people", strlen("we the people")));
420 assert(strlen("we the people") == value_length
);
421 assert(rc
== MEMCACHED_SUCCESS
);
423 rc
= memcached_cas(memc
, key
, key_length
,
424 "change the value", strlen("change the value"),
425 0, 0, memcached_result_cas(results
));
427 assert(rc
== MEMCACHED_SUCCESS
);
429 rc
= memcached_cas(memc
, key
, key_length
,
430 "change the value", strlen("change the value"),
433 assert(rc
== MEMCACHED_DATA_EXISTS
);
436 memcached_result_free(&results_obj
);
441 test_return
prepend_test(memcached_st
*memc
)
445 char *value
= "people";
449 rc
= memcached_flush(memc
, 0);
450 assert(rc
== MEMCACHED_SUCCESS
);
452 rc
= memcached_set(memc
, key
, strlen(key
),
453 value
, strlen(value
),
454 (time_t)0, (uint32_t)0);
455 assert(rc
== MEMCACHED_SUCCESS
);
457 rc
= memcached_prepend(memc
, key
, strlen(key
),
458 "the ", strlen("the "),
459 (time_t)0, (uint32_t)0);
460 assert(rc
== MEMCACHED_SUCCESS
);
462 rc
= memcached_prepend(memc
, key
, strlen(key
),
463 "we ", strlen("we "),
464 (time_t)0, (uint32_t)0);
465 assert(rc
== MEMCACHED_SUCCESS
);
467 value
= memcached_get(memc
, key
, strlen(key
),
468 &value_length
, &flags
, &rc
);
469 assert(!memcmp(value
, "we the people", strlen("we the people")));
470 assert(strlen("we the people") == value_length
);
471 assert(rc
== MEMCACHED_SUCCESS
);
478 Set the value, then quit to make sure it is flushed.
479 Come back in and test that add fails.
481 test_return
add_test(memcached_st
*memc
)
485 char *value
= "when we sanitize";
486 unsigned long long setting_value
;
488 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
490 rc
= memcached_set(memc
, key
, strlen(key
),
491 value
, strlen(value
),
492 (time_t)0, (uint32_t)0);
493 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
494 memcached_quit(memc
);
495 rc
= memcached_add(memc
, key
, strlen(key
),
496 value
, strlen(value
),
497 (time_t)0, (uint32_t)0);
499 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
501 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
503 assert(rc
== MEMCACHED_NOTSTORED
);
508 test_return
add_wrapper(memcached_st
*memc
)
512 for (x
= 0; x
< 10000; x
++)
518 test_return
replace_test(memcached_st
*memc
)
522 char *value
= "when we sanitize";
523 char *original
= "first we insert some data";
525 rc
= memcached_set(memc
, key
, strlen(key
),
526 original
, strlen(original
),
527 (time_t)0, (uint32_t)0);
528 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
530 rc
= memcached_replace(memc
, key
, strlen(key
),
531 value
, strlen(value
),
532 (time_t)0, (uint32_t)0);
533 assert(rc
== MEMCACHED_SUCCESS
);
538 test_return
delete_test(memcached_st
*memc
)
542 char *value
= "when we sanitize";
544 rc
= memcached_set(memc
, key
, strlen(key
),
545 value
, strlen(value
),
546 (time_t)0, (uint32_t)0);
547 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
549 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
550 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
555 test_return
flush_test(memcached_st
*memc
)
559 rc
= memcached_flush(memc
, 0);
560 assert(rc
== MEMCACHED_SUCCESS
);
565 memcached_return
server_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
569 return MEMCACHED_SUCCESS
;
572 test_return
memcached_server_cursor_test(memcached_st
*memc
)
574 char *context
= "foo bad";
575 memcached_server_function callbacks
[1];
577 callbacks
[0]= server_function
;
578 memcached_server_cursor(memc
, callbacks
, context
, 1);
583 test_return
bad_key_test(memcached_st
*memc
)
586 char *key
= "foo bad";
588 size_t string_length
;
593 clone
= memcached_clone(NULL
, memc
);
596 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
597 assert(rc
== MEMCACHED_SUCCESS
);
599 string
= memcached_get(clone
, key
, strlen(key
),
600 &string_length
, &flags
, &rc
);
601 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
602 assert(string_length
== 0);
606 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
607 assert(rc
== MEMCACHED_SUCCESS
);
608 string
= memcached_get(clone
, key
, strlen(key
),
609 &string_length
, &flags
, &rc
);
610 assert(rc
== MEMCACHED_NOTFOUND
);
611 assert(string_length
== 0);
614 /* Test multi key for bad keys */
616 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
617 size_t key_lengths
[] = { 7, 7, 7 };
619 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
620 assert(rc
== MEMCACHED_SUCCESS
);
622 rc
= memcached_mget(clone
, keys
, key_lengths
, 3);
623 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
625 rc
= memcached_mget_by_key(clone
, "foo daddy", 9, keys
, key_lengths
, 1);
626 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
629 memcached_free(clone
);
634 #define READ_THROUGH_VALUE "set for me"
635 memcached_return
read_through_trigger(memcached_st
*memc
,
636 char *key
, size_t key_length
,
637 memcached_result_st
*result
)
640 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
643 test_return
read_through(memcached_st
*memc
)
648 size_t string_length
;
651 string
= memcached_get(memc
, key
, strlen(key
),
652 &string_length
, &flags
, &rc
);
654 assert(rc
== MEMCACHED_NOTFOUND
);
655 assert(string_length
== 0);
658 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, read_through_trigger
);
659 assert(rc
== MEMCACHED_SUCCESS
);
661 string
= memcached_get(memc
, key
, strlen(key
),
662 &string_length
, &flags
, &rc
);
664 assert(rc
== MEMCACHED_SUCCESS
);
665 assert(string_length
== strlen(READ_THROUGH_VALUE
));
666 assert(!strcmp(READ_THROUGH_VALUE
, string
));
669 string
= memcached_get(memc
, key
, strlen(key
),
670 &string_length
, &flags
, &rc
);
672 assert(rc
== MEMCACHED_SUCCESS
);
673 assert(string_length
== strlen(READ_THROUGH_VALUE
));
674 assert(!strcmp(READ_THROUGH_VALUE
, string
));
680 memcached_return
delete_trigger(memcached_st
*ptr
, const char *key
, size_t key_length
)
684 return MEMCACHED_SUCCESS
;
687 test_return
delete_through(memcached_st
*memc
)
689 memcached_trigger_delete_key callback
;
692 callback
= delete_trigger
;
694 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, callback
);
695 assert(rc
== MEMCACHED_SUCCESS
);
700 test_return
get_test(memcached_st
*memc
)
705 size_t string_length
;
708 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
709 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
711 string
= memcached_get(memc
, key
, strlen(key
),
712 &string_length
, &flags
, &rc
);
714 assert(rc
== MEMCACHED_NOTFOUND
);
715 assert(string_length
== 0);
721 test_return
get_test2(memcached_st
*memc
)
725 char *value
= "when we sanitize";
727 size_t string_length
;
730 rc
= memcached_set(memc
, key
, strlen(key
),
731 value
, strlen(value
),
732 (time_t)0, (uint32_t)0);
733 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
735 string
= memcached_get(memc
, key
, strlen(key
),
736 &string_length
, &flags
, &rc
);
739 assert(rc
== MEMCACHED_SUCCESS
);
740 assert(string_length
== strlen(value
));
741 assert(!memcmp(string
, value
, string_length
));
748 test_return
set_test2(memcached_st
*memc
)
752 char *value
= "train in the brain";
753 size_t value_length
= strlen(value
);
756 for (x
= 0; x
< 10; x
++)
758 rc
= memcached_set(memc
, key
, strlen(key
),
760 (time_t)0, (uint32_t)0);
761 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
767 test_return
set_test3(memcached_st
*memc
)
772 size_t value_length
= 8191;
775 value
= (char*)malloc(value_length
);
778 for (x
= 0; x
< value_length
; x
++)
779 value
[x
] = (char) (x
% 127);
781 for (x
= 0; x
< 1; x
++)
783 rc
= memcached_set(memc
, key
, strlen(key
),
785 (time_t)0, (uint32_t)0);
786 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
794 test_return
get_test3(memcached_st
*memc
)
799 size_t value_length
= 8191;
801 size_t string_length
;
805 value
= (char*)malloc(value_length
);
808 for (x
= 0; x
< value_length
; x
++)
809 value
[x
] = (char) (x
% 127);
811 rc
= memcached_set(memc
, key
, strlen(key
),
813 (time_t)0, (uint32_t)0);
814 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
816 string
= memcached_get(memc
, key
, strlen(key
),
817 &string_length
, &flags
, &rc
);
819 assert(rc
== MEMCACHED_SUCCESS
);
821 assert(string_length
== value_length
);
822 assert(!memcmp(string
, value
, string_length
));
830 test_return
get_test4(memcached_st
*memc
)
835 size_t value_length
= 8191;
837 size_t string_length
;
841 value
= (char*)malloc(value_length
);
844 for (x
= 0; x
< value_length
; x
++)
845 value
[x
] = (char) (x
% 127);
847 rc
= memcached_set(memc
, key
, strlen(key
),
849 (time_t)0, (uint32_t)0);
850 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
852 for (x
= 0; x
< 10; x
++)
854 string
= memcached_get(memc
, key
, strlen(key
),
855 &string_length
, &flags
, &rc
);
857 assert(rc
== MEMCACHED_SUCCESS
);
859 assert(string_length
== value_length
);
860 assert(!memcmp(string
, value
, string_length
));
869 /* Do not copy the style of this code, I just access hosts to testthis function */
870 test_return
stats_servername_test(memcached_st
*memc
)
873 memcached_stat_st stat
;
874 rc
= memcached_stat_servername(&stat
, NULL
,
875 memc
->hosts
[0].hostname
,
876 memc
->hosts
[0].port
);
881 test_return
increment_test(memcached_st
*memc
)
888 rc
= memcached_set(memc
, key
, strlen(key
),
889 value
, strlen(value
),
890 (time_t)0, (uint32_t)0);
891 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
893 rc
= memcached_increment(memc
, key
, strlen(key
),
895 assert(rc
== MEMCACHED_SUCCESS
);
896 assert(new_number
== 1);
898 rc
= memcached_increment(memc
, key
, strlen(key
),
900 assert(rc
== MEMCACHED_SUCCESS
);
901 assert(new_number
== 2);
906 test_return
decrement_test(memcached_st
*memc
)
913 rc
= memcached_set(memc
, key
, strlen(key
),
914 value
, strlen(value
),
915 (time_t)0, (uint32_t)0);
916 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
918 rc
= memcached_decrement(memc
, key
, strlen(key
),
920 assert(rc
== MEMCACHED_SUCCESS
);
921 assert(new_number
== 2);
923 rc
= memcached_decrement(memc
, key
, strlen(key
),
925 assert(rc
== MEMCACHED_SUCCESS
);
926 assert(new_number
== 1);
931 test_return
quit_test(memcached_st
*memc
)
935 char *value
= "sanford and sun";
937 rc
= memcached_set(memc
, key
, strlen(key
),
938 value
, strlen(value
),
939 (time_t)10, (uint32_t)3);
940 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
941 memcached_quit(memc
);
943 rc
= memcached_set(memc
, key
, strlen(key
),
944 value
, strlen(value
),
945 (time_t)50, (uint32_t)9);
946 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
951 test_return
mget_result_test(memcached_st
*memc
)
954 char *keys
[]= {"fudge", "son", "food"};
955 size_t key_length
[]= {5, 3, 4};
958 memcached_result_st results_obj
;
959 memcached_result_st
*results
;
961 results
= memcached_result_create(memc
, &results_obj
);
963 assert(&results_obj
== results
);
965 /* We need to empty the server before continueing test */
966 rc
= memcached_flush(memc
, 0);
967 assert(rc
== MEMCACHED_SUCCESS
);
969 rc
= memcached_mget(memc
, keys
, key_length
, 3);
970 assert(rc
== MEMCACHED_SUCCESS
);
972 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
977 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
979 assert(rc
== MEMCACHED_END
);
981 for (x
= 0; x
< 3; x
++)
983 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
984 keys
[x
], key_length
[x
],
985 (time_t)50, (uint32_t)9);
986 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
989 rc
= memcached_mget(memc
, keys
, key_length
, 3);
990 assert(rc
== MEMCACHED_SUCCESS
);
992 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
995 assert(&results_obj
== results
);
996 assert(rc
== MEMCACHED_SUCCESS
);
997 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
998 assert(!memcmp(memcached_result_key_value(results
),
999 memcached_result_value(results
),
1000 memcached_result_length(results
)));
1003 memcached_result_free(&results_obj
);
1008 test_return
mget_result_alloc_test(memcached_st
*memc
)
1010 memcached_return rc
;
1011 char *keys
[]= {"fudge", "son", "food"};
1012 size_t key_length
[]= {5, 3, 4};
1015 memcached_result_st
*results
;
1017 /* We need to empty the server before continueing test */
1018 rc
= memcached_flush(memc
, 0);
1019 assert(rc
== MEMCACHED_SUCCESS
);
1021 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1022 assert(rc
== MEMCACHED_SUCCESS
);
1024 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1029 assert(rc
== MEMCACHED_END
);
1031 for (x
= 0; x
< 3; x
++)
1033 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1034 keys
[x
], key_length
[x
],
1035 (time_t)50, (uint32_t)9);
1036 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1039 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1040 assert(rc
== MEMCACHED_SUCCESS
);
1043 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1046 assert(rc
== MEMCACHED_SUCCESS
);
1047 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1048 assert(!memcmp(memcached_result_key_value(results
),
1049 memcached_result_value(results
),
1050 memcached_result_length(results
)));
1051 memcached_result_free(results
);
1058 /* Count the results */
1059 unsigned int callback_counter(memcached_st
*ptr
, memcached_result_st
*result
, void *context
)
1061 unsigned int *counter
= (unsigned int *)context
;
1063 *counter
= *counter
+ 1;
1068 test_return
mget_result_function(memcached_st
*memc
)
1070 memcached_return rc
;
1071 char *keys
[]= {"fudge", "son", "food"};
1072 size_t key_length
[]= {5, 3, 4};
1074 unsigned int counter
;
1075 memcached_execute_function callbacks
[1];
1077 /* We need to empty the server before continueing test */
1078 rc
= memcached_flush(memc
, 0);
1079 for (x
= 0; x
< 3; x
++)
1081 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1082 keys
[x
], key_length
[x
],
1083 (time_t)50, (uint32_t)9);
1084 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1087 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1088 assert(rc
== MEMCACHED_SUCCESS
);
1090 callbacks
[0]= &callback_counter
;
1092 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1094 assert(counter
== 3);
1099 test_return
mget_test(memcached_st
*memc
)
1101 memcached_return rc
;
1102 char *keys
[]= {"fudge", "son", "food"};
1103 size_t key_length
[]= {5, 3, 4};
1107 char return_key
[MEMCACHED_MAX_KEY
];
1108 size_t return_key_length
;
1110 size_t return_value_length
;
1112 /* We need to empty the server before continueing test */
1113 rc
= memcached_flush(memc
, 0);
1114 assert(rc
== MEMCACHED_SUCCESS
);
1116 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1117 assert(rc
== MEMCACHED_SUCCESS
);
1119 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1120 &return_value_length
, &flags
, &rc
)) != NULL
)
1122 assert(return_value
);
1124 assert(!return_value
);
1125 assert(return_value_length
== 0);
1126 assert(rc
== MEMCACHED_END
);
1128 for (x
= 0; x
< 3; x
++)
1130 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1131 keys
[x
], key_length
[x
],
1132 (time_t)50, (uint32_t)9);
1133 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1136 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1137 assert(rc
== MEMCACHED_SUCCESS
);
1140 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1141 &return_value_length
, &flags
, &rc
)))
1143 assert(return_value
);
1144 assert(rc
== MEMCACHED_SUCCESS
);
1145 assert(return_key_length
== return_value_length
);
1146 assert(!memcmp(return_value
, return_key
, return_value_length
));
1154 test_return
get_stats_keys(memcached_st
*memc
)
1158 memcached_stat_st stat
;
1159 memcached_return rc
;
1161 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
1162 assert(rc
== MEMCACHED_SUCCESS
);
1163 for (ptr
= list
; *ptr
; ptr
++)
1172 test_return
version_string_test(memcached_st
*memc
)
1174 const char *version_string
;
1176 version_string
= memcached_lib_version();
1178 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1183 test_return
get_stats(memcached_st
*memc
)
1188 memcached_return rc
;
1189 memcached_stat_st
*stat
;
1191 stat
= memcached_stat(memc
, NULL
, &rc
);
1192 assert(rc
== MEMCACHED_SUCCESS
);
1194 assert(rc
== MEMCACHED_SUCCESS
);
1197 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1199 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
1200 assert(rc
== MEMCACHED_SUCCESS
);
1201 for (ptr
= list
; *ptr
; ptr
++);
1206 memcached_stat_free(NULL
, stat
);
1211 test_return
add_host_test(memcached_st
*memc
)
1214 memcached_server_st
*servers
;
1215 memcached_return rc
;
1216 char servername
[]= "0.example.com";
1218 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1220 assert(1 == memcached_server_list_count(servers
));
1222 for (x
= 2; x
< 20; x
++)
1224 char buffer
[SMALL_STRING_LEN
];
1226 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1227 servers
= memcached_server_list_append(servers
, buffer
, 401,
1229 assert(rc
== MEMCACHED_SUCCESS
);
1230 assert(x
== memcached_server_list_count(servers
));
1233 rc
= memcached_server_push(memc
, servers
);
1234 assert(rc
== MEMCACHED_SUCCESS
);
1235 rc
= memcached_server_push(memc
, servers
);
1236 assert(rc
== MEMCACHED_SUCCESS
);
1238 memcached_server_list_free(servers
);
1243 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
1245 return MEMCACHED_SUCCESS
;
1248 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
1250 return MEMCACHED_SUCCESS
;
1253 test_return
callback_test(memcached_st
*memc
)
1255 /* Test User Data */
1259 memcached_return rc
;
1261 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1262 assert(rc
== MEMCACHED_SUCCESS
);
1263 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1264 assert(*test_ptr
== x
);
1267 /* Test Clone Callback */
1269 memcached_clone_func temp_function
;
1270 memcached_return rc
;
1272 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
1273 assert(rc
== MEMCACHED_SUCCESS
);
1274 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1275 assert(temp_function
== clone_test_callback
);
1278 /* Test Cleanup Callback */
1280 memcached_cleanup_func temp_function
;
1281 memcached_return rc
;
1283 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1284 assert(rc
== MEMCACHED_SUCCESS
);
1285 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1286 assert(temp_function
== cleanup_test_callback
);
1292 /* We don't test the behavior itself, we test the switches */
1293 test_return
behavior_test(memcached_st
*memc
)
1295 unsigned long long value
;
1296 unsigned int set
= 1;
1298 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1299 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1302 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1303 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1306 set
= MEMCACHED_HASH_MD5
;
1307 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1308 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1309 assert(value
== MEMCACHED_HASH_MD5
);
1313 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1314 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1317 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1318 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1321 set
= MEMCACHED_HASH_DEFAULT
;
1322 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1323 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1324 assert(value
== MEMCACHED_HASH_DEFAULT
);
1326 set
= MEMCACHED_HASH_CRC
;
1327 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1328 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1329 assert(value
== MEMCACHED_HASH_CRC
);
1331 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1334 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1340 /* Test case provided by Cal Haldenbrand */
1341 test_return
user_supplied_bug1(memcached_st
*memc
)
1343 unsigned int setter
= 1;
1346 unsigned long long total
= 0;
1349 char randomstuff
[6 * 1024];
1350 memcached_return rc
;
1352 memset(randomstuff
, 0, 6 * 1024);
1354 /* We just keep looking at the same values over and over */
1357 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1358 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1362 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1366 size
= (rand() % ( 5 * 1024 ) ) + 400;
1367 memset(randomstuff
, 0, 6 * 1024);
1368 assert(size
< 6 * 1024); /* Being safe here */
1370 for (j
= 0 ; j
< size
;j
++)
1371 randomstuff
[j
] = (char) (rand() % 26) + 97;
1374 sprintf(key
, "%d", x
);
1375 rc
= memcached_set(memc
, key
, strlen(key
),
1376 randomstuff
, strlen(randomstuff
), 10, 0);
1377 /* If we fail, lets try again */
1378 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1379 rc
= memcached_set(memc
, key
, strlen(key
),
1380 randomstuff
, strlen(randomstuff
), 10, 0);
1381 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1387 /* Test case provided by Cal Haldenbrand */
1388 test_return
user_supplied_bug2(memcached_st
*memc
)
1391 unsigned int setter
;
1393 unsigned long long total
;
1396 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1397 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1399 setter
= 20 * 1024576;
1400 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1401 setter
= 20 * 1024576;
1402 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1403 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1404 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1406 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1409 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1411 memcached_return rc
= MEMCACHED_SUCCESS
;
1412 char buffer
[SMALL_STRING_LEN
];
1417 memset(buffer
, 0, SMALL_STRING_LEN
);
1419 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1420 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1421 &val_len
, &flags
, &rc
);
1422 if (rc
!= MEMCACHED_SUCCESS
)
1424 if (rc
== MEMCACHED_NOTFOUND
)
1428 WATCHPOINT_ERROR(rc
);
1442 /* Do a large mget() over all the keys we think exist */
1443 #define KEY_COUNT 3000 // * 1024576
1444 test_return
user_supplied_bug3(memcached_st
*memc
)
1446 memcached_return rc
;
1447 unsigned int setter
;
1450 size_t key_lengths
[KEY_COUNT
];
1453 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1454 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1456 setter
= 20 * 1024576;
1457 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1458 setter
= 20 * 1024576;
1459 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1460 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1461 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1464 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1466 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1467 for (x
= 0; x
< KEY_COUNT
; x
++)
1471 snprintf(buffer
, 30, "%u", x
);
1472 keys
[x
]= strdup(buffer
);
1473 key_lengths
[x
]= strlen(keys
[x
]);
1476 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1477 assert(rc
== MEMCACHED_SUCCESS
);
1479 /* Turn this into a help function */
1481 char return_key
[MEMCACHED_MAX_KEY
];
1482 size_t return_key_length
;
1484 size_t return_value_length
;
1487 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1488 &return_value_length
, &flags
, &rc
)))
1490 assert(return_value
);
1491 assert(rc
== MEMCACHED_SUCCESS
);
1496 for (x
= 0; x
< KEY_COUNT
; x
++)
1503 /* Make sure we behave properly if server list has no values */
1504 test_return
user_supplied_bug4(memcached_st
*memc
)
1506 memcached_return rc
;
1507 char *keys
[]= {"fudge", "son", "food"};
1508 size_t key_length
[]= {5, 3, 4};
1511 char return_key
[MEMCACHED_MAX_KEY
];
1512 size_t return_key_length
;
1514 size_t return_value_length
;
1516 /* Here we free everything before running a bunch of mget tests */
1518 memcached_server_list_free(memc
->hosts
);
1520 memc
->number_of_hosts
= 0;
1524 /* We need to empty the server before continueing test */
1525 rc
= memcached_flush(memc
, 0);
1526 assert(rc
== MEMCACHED_NO_SERVERS
);
1528 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1529 assert(rc
== MEMCACHED_NO_SERVERS
);
1531 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1532 &return_value_length
, &flags
, &rc
)) != NULL
)
1534 assert(return_value
);
1536 assert(!return_value
);
1537 assert(return_value_length
== 0);
1538 assert(rc
== MEMCACHED_NO_SERVERS
);
1540 for (x
= 0; x
< 3; x
++)
1542 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1543 keys
[x
], key_length
[x
],
1544 (time_t)50, (uint32_t)9);
1545 assert(rc
== MEMCACHED_NO_SERVERS
);
1548 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1549 assert(rc
== MEMCACHED_NO_SERVERS
);
1552 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1553 &return_value_length
, &flags
, &rc
)))
1555 assert(return_value
);
1556 assert(rc
== MEMCACHED_SUCCESS
);
1557 assert(return_key_length
== return_value_length
);
1558 assert(!memcmp(return_value
, return_key
, return_value_length
));
1566 #define VALUE_SIZE_BUG5 1048064
1567 test_return
user_supplied_bug5(memcached_st
*memc
)
1569 memcached_return rc
;
1570 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1571 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1572 char return_key
[MEMCACHED_MAX_KEY
];
1573 size_t return_key_length
;
1575 size_t value_length
;
1579 char insert_data
[VALUE_SIZE_BUG5
];
1581 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1582 insert_data
[x
]= rand();
1584 memcached_flush(memc
, 0);
1585 value
= memcached_get(memc
, keys
[0], key_length
[0],
1586 &value_length
, &flags
, &rc
);
1587 assert(value
== NULL
);
1588 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1591 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1592 &value_length
, &flags
, &rc
)))
1596 for (x
= 0; x
< 4; x
++)
1598 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1599 insert_data
, VALUE_SIZE_BUG5
,
1600 (time_t)0, (uint32_t)0);
1601 assert(rc
== MEMCACHED_SUCCESS
);
1604 for (x
= 0; x
< 10; x
++)
1606 value
= memcached_get(memc
, keys
[0], key_length
[0],
1607 &value_length
, &flags
, &rc
);
1611 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1613 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1614 &value_length
, &flags
, &rc
)))
1625 test_return
user_supplied_bug6(memcached_st
*memc
)
1627 memcached_return rc
;
1628 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1629 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1630 char return_key
[MEMCACHED_MAX_KEY
];
1631 size_t return_key_length
;
1633 size_t value_length
;
1637 char insert_data
[VALUE_SIZE_BUG5
];
1639 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1640 insert_data
[x
]= rand();
1642 memcached_flush(memc
, 0);
1643 value
= memcached_get(memc
, keys
[0], key_length
[0],
1644 &value_length
, &flags
, &rc
);
1645 assert(value
== NULL
);
1646 assert(rc
== MEMCACHED_NOTFOUND
);
1647 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1648 assert(rc
== MEMCACHED_SUCCESS
);
1651 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1652 &value_length
, &flags
, &rc
)))
1655 assert(rc
== MEMCACHED_END
);
1657 for (x
= 0; x
< 4; x
++)
1659 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1660 insert_data
, VALUE_SIZE_BUG5
,
1661 (time_t)0, (uint32_t)0);
1662 assert(rc
== MEMCACHED_SUCCESS
);
1665 for (x
= 0; x
< 2; x
++)
1667 value
= memcached_get(memc
, keys
[0], key_length
[0],
1668 &value_length
, &flags
, &rc
);
1672 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1673 assert(rc
== MEMCACHED_SUCCESS
);
1675 /* We test for purge of partial complete fetches */
1676 for (count
= 3; count
; count
--)
1678 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1679 &value_length
, &flags
, &rc
);
1680 assert(rc
== MEMCACHED_SUCCESS
);
1681 assert(!(memcmp(value
, insert_data
, value_length
)));
1682 assert(value_length
);
1690 test_return
user_supplied_bug8(memcached_st
*memc
)
1692 memcached_return rc
;
1694 memcached_st
*clone
;
1696 memcached_server_st
*servers
;
1697 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";
1699 servers
= memcached_servers_parse(server_list
);
1702 mine
= memcached_create(NULL
);
1703 rc
= memcached_server_push(mine
, servers
);
1704 assert(rc
== MEMCACHED_SUCCESS
);
1705 memcached_server_list_free(servers
);
1708 clone
= memcached_clone(NULL
, mine
);
1710 memcached_quit(mine
);
1711 memcached_quit(clone
);
1714 memcached_free(mine
);
1715 memcached_free(clone
);
1720 /* Test flag store/retrieve */
1721 test_return
user_supplied_bug7(memcached_st
*memc
)
1723 memcached_return rc
;
1724 char *keys
= "036790384900";
1725 size_t key_length
= strlen("036790384900");
1726 char return_key
[MEMCACHED_MAX_KEY
];
1727 size_t return_key_length
;
1729 size_t value_length
;
1732 char insert_data
[VALUE_SIZE_BUG5
];
1734 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1735 insert_data
[x
]= rand();
1737 memcached_flush(memc
, 0);
1740 rc
= memcached_set(memc
, keys
, key_length
,
1741 insert_data
, VALUE_SIZE_BUG5
,
1743 assert(rc
== MEMCACHED_SUCCESS
);
1746 value
= memcached_get(memc
, keys
, key_length
,
1747 &value_length
, &flags
, &rc
);
1748 assert(flags
== 245);
1752 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1755 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1756 &value_length
, &flags
, &rc
);
1757 assert(flags
== 245);
1765 test_return
user_supplied_bug9(memcached_st
*memc
)
1767 memcached_return rc
;
1768 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1769 size_t key_length
[3];
1774 char return_key
[MEMCACHED_MAX_KEY
];
1775 size_t return_key_length
;
1777 size_t return_value_length
;
1780 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1781 key_length
[1]= strlen("fudge&*@#");
1782 key_length
[2]= strlen("for^#@&$not");
1785 for (x
= 0; x
< 3; x
++)
1787 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1788 keys
[x
], key_length
[x
],
1789 (time_t)50, (uint32_t)9);
1790 assert(rc
== MEMCACHED_SUCCESS
);
1793 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1794 assert(rc
== MEMCACHED_SUCCESS
);
1796 /* We need to empty the server before continueing test */
1797 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1798 &return_value_length
, &flags
, &rc
)) != NULL
)
1800 assert(return_value
);
1809 /* We are testing with aggressive timeout to get failures */
1810 test_return
user_supplied_bug10(memcached_st
*memc
)
1814 size_t value_length
= 512;
1817 memcached_return rc
;
1818 unsigned int set
= 1;
1819 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1822 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1823 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1825 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1827 value
= (char*)malloc(value_length
* sizeof(char));
1829 for (x
= 0; x
< value_length
; x
++)
1830 value
[x
]= (char) (x
% 127);
1832 for (x
= 1; x
<= 100000; ++x
)
1834 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1836 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1838 if (rc
== MEMCACHED_WRITE_FAILURE
)
1843 memcached_free(mclone
);
1849 We are looking failures in the async protocol
1851 test_return
user_supplied_bug11(memcached_st
*memc
)
1855 size_t value_length
= 512;
1858 memcached_return rc
;
1859 unsigned int set
= 1;
1861 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1863 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1864 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1866 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1868 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1870 assert(timeout
== -1);
1872 value
= (char*)malloc(value_length
* sizeof(char));
1874 for (x
= 0; x
< value_length
; x
++)
1875 value
[x
]= (char) (x
% 127);
1877 for (x
= 1; x
<= 100000; ++x
)
1879 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1883 memcached_free(mclone
);
1889 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1891 test_return
user_supplied_bug12(memcached_st
*memc
)
1893 memcached_return rc
;
1895 size_t value_length
;
1897 uint64_t number_value
;
1899 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1900 &value_length
, &flags
, &rc
);
1901 assert(value
== NULL
);
1902 assert(rc
== MEMCACHED_NOTFOUND
);
1904 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1907 assert(value
== NULL
);
1908 assert(rc
== MEMCACHED_NOTFOUND
);
1910 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1912 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1913 &value_length
, &flags
, &rc
);
1915 assert(rc
== MEMCACHED_SUCCESS
);
1918 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1920 assert(number_value
== 2);
1921 assert(rc
== MEMCACHED_SUCCESS
);
1927 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1928 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1930 test_return
user_supplied_bug13(memcached_st
*memc
)
1932 char key
[] = "key34567890";
1934 memcached_return rc
;
1935 size_t overflowSize
;
1937 char commandFirst
[]= "set key34567890 0 0 ";
1938 char commandLast
[] = " \r\n"; /* first line of command sent to server */
1939 size_t commandLength
;
1942 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
1944 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
1946 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
1948 overflow
= malloc(testSize
);
1949 assert(overflow
!= NULL
);
1951 memset(overflow
, 'x', testSize
);
1952 rc
= memcached_set(memc
, key
, strlen(key
),
1953 overflow
, testSize
, 0, 0);
1954 assert(rc
== MEMCACHED_SUCCESS
);
1963 Test values of many different sizes
1964 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1965 set key34567890 0 0 8169 \r\n
1966 is sent followed by buffer of size 8169, followed by 8169
1968 test_return
user_supplied_bug14(memcached_st
*memc
)
1971 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1972 memcached_return rc
;
1975 size_t value_length
= 18000;
1977 size_t string_length
;
1980 size_t current_length
;
1982 value
= (char*)malloc(value_length
);
1985 for (x
= 0; x
< value_length
; x
++)
1986 value
[x
] = (char) (x
% 127);
1988 for (current_length
= 0; current_length
< value_length
; current_length
++)
1990 rc
= memcached_set(memc
, key
, strlen(key
),
1991 value
, current_length
,
1992 (time_t)0, (uint32_t)0);
1993 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1995 string
= memcached_get(memc
, key
, strlen(key
),
1996 &string_length
, &flags
, &rc
);
1998 assert(rc
== MEMCACHED_SUCCESS
);
1999 assert(string_length
== current_length
);
2000 assert(!memcmp(string
, value
, string_length
));
2011 Look for zero length value problems
2013 test_return
user_supplied_bug15(memcached_st
*memc
)
2016 memcached_return rc
;
2022 for (x
= 0; x
< 2; x
++)
2024 rc
= memcached_set(memc
, key
, strlen(key
),
2026 (time_t)0, (uint32_t)0);
2028 assert(rc
== MEMCACHED_SUCCESS
);
2030 value
= memcached_get(memc
, key
, strlen(key
),
2031 &length
, &flags
, &rc
);
2033 assert(rc
== MEMCACHED_SUCCESS
);
2034 assert(value
== NULL
);
2035 assert(length
== 0);
2038 value
= memcached_get(memc
, key
, strlen(key
),
2039 &length
, &flags
, &rc
);
2041 assert(rc
== MEMCACHED_SUCCESS
);
2042 assert(value
== NULL
);
2043 assert(length
== 0);
2050 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2051 test_return
user_supplied_bug16(memcached_st
*memc
)
2053 memcached_return rc
;
2059 rc
= memcached_set(memc
, key
, strlen(key
),
2061 (time_t)0, UINT32_MAX
);
2063 assert(rc
== MEMCACHED_SUCCESS
);
2065 value
= memcached_get(memc
, key
, strlen(key
),
2066 &length
, &flags
, &rc
);
2068 assert(rc
== MEMCACHED_SUCCESS
);
2069 assert(value
== NULL
);
2070 assert(length
== 0);
2071 assert(flags
== UINT32_MAX
);
2076 test_return
result_static(memcached_st
*memc
)
2078 memcached_result_st result
;
2079 memcached_result_st
*result_ptr
;
2081 result_ptr
= memcached_result_create(memc
, &result
);
2082 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2084 memcached_result_free(&result
);
2089 test_return
result_alloc(memcached_st
*memc
)
2091 memcached_result_st
*result
;
2093 result
= memcached_result_create(memc
, NULL
);
2095 memcached_result_free(result
);
2100 test_return
string_static_null(memcached_st
*memc
)
2102 memcached_string_st string
;
2103 memcached_string_st
*string_ptr
;
2105 string_ptr
= memcached_string_create(memc
, &string
, 0);
2106 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2108 memcached_string_free(&string
);
2113 test_return
string_alloc_null(memcached_st
*memc
)
2115 memcached_string_st
*string
;
2117 string
= memcached_string_create(memc
, NULL
, 0);
2119 memcached_string_free(string
);
2124 test_return
string_alloc_with_size(memcached_st
*memc
)
2126 memcached_string_st
*string
;
2128 string
= memcached_string_create(memc
, NULL
, 1024);
2130 memcached_string_free(string
);
2135 test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2137 memcached_string_st
*string
;
2139 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
2140 assert(string
== NULL
);
2145 test_return
string_alloc_append(memcached_st
*memc
)
2148 char buffer
[SMALL_STRING_LEN
];
2149 memcached_string_st
*string
;
2151 /* Ring the bell! */
2152 memset(buffer
, 6, SMALL_STRING_LEN
);
2154 string
= memcached_string_create(memc
, NULL
, 100);
2157 for (x
= 0; x
< 1024; x
++)
2159 memcached_return rc
;
2160 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2161 assert(rc
== MEMCACHED_SUCCESS
);
2163 memcached_string_free(string
);
2168 test_return
string_alloc_append_toobig(memcached_st
*memc
)
2170 memcached_return rc
;
2172 char buffer
[SMALL_STRING_LEN
];
2173 memcached_string_st
*string
;
2175 /* Ring the bell! */
2176 memset(buffer
, 6, SMALL_STRING_LEN
);
2178 string
= memcached_string_create(memc
, NULL
, 100);
2181 for (x
= 0; x
< 1024; x
++)
2183 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2184 assert(rc
== MEMCACHED_SUCCESS
);
2186 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
2187 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2188 memcached_string_free(string
);
2193 test_return
cleanup_pairs(memcached_st
*memc
)
2195 pairs_free(global_pairs
);
2200 test_return
generate_pairs(memcached_st
*memc
)
2202 unsigned long long x
;
2203 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2204 global_count
= GLOBAL_COUNT
;
2206 for (x
= 0; x
< global_count
; x
++)
2208 global_keys
[x
]= global_pairs
[x
].key
;
2209 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2215 test_return
generate_large_pairs(memcached_st
*memc
)
2217 unsigned long long x
;
2218 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2219 global_count
= GLOBAL2_COUNT
;
2221 for (x
= 0; x
< global_count
; x
++)
2223 global_keys
[x
]= global_pairs
[x
].key
;
2224 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2230 test_return
generate_data(memcached_st
*memc
)
2232 execute_set(memc
, global_pairs
, global_count
);
2237 test_return
generate_buffer_data(memcached_st
*memc
)
2242 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2243 generate_data(memc
);
2248 test_return
get_read_count(memcached_st
*memc
)
2251 memcached_return rc
;
2252 memcached_st
*clone
;
2254 clone
= memcached_clone(NULL
, memc
);
2257 memcached_server_add(clone
, "localhost", 6666);
2261 size_t return_value_length
;
2265 for (x
= count
= 0; x
< global_count
; x
++)
2267 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2268 &return_value_length
, &flags
, &rc
);
2269 if (rc
== MEMCACHED_SUCCESS
)
2276 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2279 memcached_free(clone
);
2284 test_return
get_read(memcached_st
*memc
)
2287 memcached_return rc
;
2291 size_t return_value_length
;
2294 for (x
= 0; x
< global_count
; x
++)
2296 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2297 &return_value_length
, &flags
, &rc
);
2299 assert(return_value);
2300 assert(rc == MEMCACHED_SUCCESS);
2302 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2310 test_return
mget_read(memcached_st
*memc
)
2312 memcached_return rc
;
2314 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2315 assert(rc
== MEMCACHED_SUCCESS
);
2316 /* Turn this into a help function */
2318 char return_key
[MEMCACHED_MAX_KEY
];
2319 size_t return_key_length
;
2321 size_t return_value_length
;
2324 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2325 &return_value_length
, &flags
, &rc
)))
2327 assert(return_value
);
2328 assert(rc
== MEMCACHED_SUCCESS
);
2336 test_return
mget_read_result(memcached_st
*memc
)
2338 memcached_return rc
;
2340 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2341 assert(rc
== MEMCACHED_SUCCESS
);
2342 /* Turn this into a help function */
2344 memcached_result_st results_obj
;
2345 memcached_result_st
*results
;
2347 results
= memcached_result_create(memc
, &results_obj
);
2349 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2352 assert(rc
== MEMCACHED_SUCCESS
);
2355 memcached_result_free(&results_obj
);
2361 test_return
mget_read_function(memcached_st
*memc
)
2363 memcached_return rc
;
2364 unsigned int counter
;
2365 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
2367 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2368 assert(rc
== MEMCACHED_SUCCESS
);
2370 callbacks
[0]= &callback_counter
;
2372 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2377 test_return
delete_generate(memcached_st
*memc
)
2381 for (x
= 0; x
< global_count
; x
++)
2383 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2389 test_return
delete_buffer_generate(memcached_st
*memc
)
2395 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2397 for (x
= 0; x
< global_count
; x
++)
2399 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2405 test_return
free_data(memcached_st
*memc
)
2407 pairs_free(global_pairs
);
2412 test_return
add_host_test1(memcached_st
*memc
)
2415 memcached_return rc
;
2416 char servername
[]= "0.example.com";
2417 memcached_server_st
*servers
;
2419 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
2421 assert(1 == memcached_server_list_count(servers
));
2423 for (x
= 2; x
< 20; x
++)
2425 char buffer
[SMALL_STRING_LEN
];
2427 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2428 servers
= memcached_server_list_append(servers
, buffer
, 401,
2430 assert(rc
== MEMCACHED_SUCCESS
);
2431 assert(x
== memcached_server_list_count(servers
));
2434 rc
= memcached_server_push(memc
, servers
);
2435 assert(rc
== MEMCACHED_SUCCESS
);
2436 rc
= memcached_server_push(memc
, servers
);
2437 assert(rc
== MEMCACHED_SUCCESS
);
2439 memcached_server_list_free(servers
);
2444 memcached_return
pre_nonblock(memcached_st
*memc
)
2446 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2448 return MEMCACHED_SUCCESS
;
2451 memcached_return
pre_murmur(memcached_st
*memc
)
2453 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2455 return MEMCACHED_SUCCESS
;
2458 memcached_return
pre_md5(memcached_st
*memc
)
2460 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2462 return MEMCACHED_SUCCESS
;
2465 memcached_return
pre_crc(memcached_st
*memc
)
2467 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2469 return MEMCACHED_SUCCESS
;
2472 memcached_return
pre_hsieh(memcached_st
*memc
)
2474 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2476 return MEMCACHED_SUCCESS
;
2479 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2481 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2483 return MEMCACHED_SUCCESS
;
2486 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2488 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2490 return MEMCACHED_SUCCESS
;
2493 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2495 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2497 return MEMCACHED_SUCCESS
;
2500 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2502 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2504 return MEMCACHED_SUCCESS
;
2507 memcached_return
pre_behavior_ketama(memcached_st
*memc
)
2509 memcached_return rc
;
2512 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
2513 assert(rc
== MEMCACHED_SUCCESS
);
2515 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
2518 return MEMCACHED_SUCCESS
;
2521 void my_free(memcached_st
*ptr
, void *mem
)
2526 void *my_malloc(memcached_st
*ptr
, const size_t size
)
2528 return malloc(size
);
2531 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2533 return realloc(mem
, size
);
2536 memcached_return
set_prefix(memcached_st
*memc
)
2538 memcached_return rc
;
2539 const char *key
= "mine";
2542 /* Make sure be default none exists */
2543 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2544 assert(rc
== MEMCACHED_FAILURE
);
2546 /* Test a clean set */
2547 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2548 assert(rc
== MEMCACHED_SUCCESS
);
2550 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2551 assert(memcmp(value
, key
, 4) == 0);
2552 assert(rc
== MEMCACHED_SUCCESS
);
2554 /* Test that we can turn it off */
2555 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2556 assert(rc
== MEMCACHED_SUCCESS
);
2558 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2559 assert(rc
== MEMCACHED_FAILURE
);
2561 /* Now setup for main test */
2562 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2563 assert(rc
== MEMCACHED_SUCCESS
);
2565 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2566 assert(rc
== MEMCACHED_SUCCESS
);
2567 assert(memcmp(value
, key
, 4) == 0);
2569 /* Set to Zero, and then Set to something too large */
2572 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2573 assert(rc
== MEMCACHED_SUCCESS
);
2575 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2576 assert(rc
== MEMCACHED_FAILURE
);
2577 assert(value
== NULL
);
2579 /* Test a long key for failure */
2580 long_key
= "Thisismorethentheallottednumberofcharacters";
2581 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2582 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2584 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2585 long_key
= "This is more then the allotted number of characters";
2586 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2587 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2589 /* Test for a bad prefix, but with a short key */
2590 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
2591 assert(rc
== MEMCACHED_SUCCESS
);
2593 long_key
= "dog cat";
2594 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2595 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2598 return MEMCACHED_SUCCESS
;
2601 memcached_return
set_memory_alloc(memcached_st
*memc
)
2604 memcached_malloc_function test_ptr
;
2605 memcached_return rc
;
2607 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2608 assert(rc
== MEMCACHED_SUCCESS
);
2609 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2610 assert(rc
== MEMCACHED_SUCCESS
);
2611 assert(test_ptr
== my_malloc
);
2615 memcached_realloc_function test_ptr
;
2616 memcached_return rc
;
2618 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2619 assert(rc
== MEMCACHED_SUCCESS
);
2620 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2621 assert(rc
== MEMCACHED_SUCCESS
);
2622 assert(test_ptr
== my_realloc
);
2626 memcached_free_function test_ptr
;
2627 memcached_return rc
;
2629 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2630 assert(rc
== MEMCACHED_SUCCESS
);
2631 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2632 assert(rc
== MEMCACHED_SUCCESS
);
2633 assert(test_ptr
== my_free
);
2636 return MEMCACHED_SUCCESS
;
2639 memcached_return
enable_wheel(memcached_st
*memc
)
2641 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL
;
2642 memcached_hash hash
;
2643 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2646 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2647 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL
);
2649 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2650 assert(hash
== MEMCACHED_HASH_HSIEH
);
2653 return MEMCACHED_SUCCESS
;
2656 memcached_return
enable_consistent(memcached_st
*memc
)
2658 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2659 memcached_hash hash
;
2660 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2663 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2664 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2666 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2667 assert(hash
== MEMCACHED_HASH_HSIEH
);
2670 return MEMCACHED_SUCCESS
;
2673 memcached_return
enable_cas(memcached_st
*memc
)
2675 unsigned int set
= 1;
2677 memcached_version(memc
);
2679 if (memc
->hosts
[0].major_version
>= 1 &&
2680 memc
->hosts
[0].minor_version
>= 2 &&
2681 memc
->hosts
[0].micro_version
>= 4)
2683 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
2685 return MEMCACHED_SUCCESS
;
2688 return MEMCACHED_FAILURE
;
2691 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2693 memcached_version(memc
);
2695 if (memc
->hosts
[0].major_version
>= 1 &&
2696 memc
->hosts
[0].minor_version
>= 2 &&
2697 memc
->hosts
[0].micro_version
>= 4)
2698 return MEMCACHED_SUCCESS
;
2700 return MEMCACHED_FAILURE
;
2703 memcached_return
pre_unix_socket(memcached_st
*memc
)
2705 memcached_return rc
;
2708 memcached_server_list_free(memc
->hosts
);
2710 memc
->number_of_hosts
= 0;
2712 if (stat("/tmp/memcached.socket", &buf
))
2713 return MEMCACHED_FAILURE
;
2715 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2720 memcached_return
pre_udp(memcached_st
*memc
)
2722 memcached_return rc
;
2724 memcached_server_list_free(memc
->hosts
);
2726 memc
->number_of_hosts
= 0;
2729 return MEMCACHED_FAILURE
;
2731 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2736 memcached_return
pre_nodelay(memcached_st
*memc
)
2738 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2739 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2741 return MEMCACHED_SUCCESS
;
2744 memcached_return
poll_timeout(memcached_st
*memc
)
2750 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
2752 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2754 assert(timeout
== 100);
2756 return MEMCACHED_SUCCESS
;
2760 /* Clean the server before beginning testing */
2762 {"flush", 0, flush_test
},
2763 {"init", 0, init_test
},
2764 {"allocation", 0, allocation_test
},
2765 {"server_list_null_test", 0, server_list_null_test
},
2766 {"server_unsort", 0, server_unsort_test
},
2767 {"server_sort", 0, server_sort_test
},
2768 {"server_sort2", 0, server_sort2_test
},
2769 {"clone_test", 0, clone_test
},
2770 {"error", 0, error_test
},
2771 {"set", 0, set_test
},
2772 {"set2", 0, set_test2
},
2773 {"set3", 0, set_test3
},
2774 {"add", 1, add_test
},
2775 {"replace", 1, replace_test
},
2776 {"delete", 1, delete_test
},
2777 {"get", 1, get_test
},
2778 {"get2", 0, get_test2
},
2779 {"get3", 0, get_test3
},
2780 {"get4", 0, get_test4
},
2781 {"stats_servername", 0, stats_servername_test
},
2782 {"increment", 0, increment_test
},
2783 {"decrement", 0, decrement_test
},
2784 {"quit", 0, quit_test
},
2785 {"mget", 1, mget_test
},
2786 {"mget_result", 1, mget_result_test
},
2787 {"mget_result_alloc", 1, mget_result_alloc_test
},
2788 {"mget_result_function", 1, mget_result_function
},
2789 {"get_stats", 0, get_stats
},
2790 {"add_host_test", 0, add_host_test
},
2791 {"get_stats_keys", 0, get_stats_keys
},
2792 {"behavior_test", 0, get_stats_keys
},
2793 {"callback_test", 0, get_stats_keys
},
2794 {"version_string_test", 0, version_string_test
},
2795 {"bad_key", 1, bad_key_test
},
2796 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
2797 {"read_through", 1, read_through
},
2798 {"delete_through", 1, delete_through
},
2802 test_st async_tests
[] ={
2803 {"add", 1, add_wrapper
},
2807 test_st string_tests
[] ={
2808 {"string static with null", 0, string_static_null
},
2809 {"string alloc with null", 0, string_alloc_null
},
2810 {"string alloc with 1K", 0, string_alloc_with_size
},
2811 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2812 {"string append", 0, string_alloc_append
},
2813 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2817 test_st result_tests
[] ={
2818 {"result static", 0, result_static
},
2819 {"result alloc", 0, result_alloc
},
2823 test_st version_1_2_3
[] ={
2824 {"append", 0, append_test
},
2825 {"prepend", 0, prepend_test
},
2826 {"cas", 0, cas_test
},
2827 {"cas2", 0, cas2_test
},
2828 {"append_binary", 0, append_binary_test
},
2832 test_st user_tests
[] ={
2833 {"user_supplied_bug1", 0, user_supplied_bug1
},
2834 {"user_supplied_bug2", 0, user_supplied_bug2
},
2835 {"user_supplied_bug3", 0, user_supplied_bug3
},
2836 {"user_supplied_bug4", 0, user_supplied_bug4
},
2837 {"user_supplied_bug5", 1, user_supplied_bug5
},
2838 {"user_supplied_bug6", 1, user_supplied_bug6
},
2839 {"user_supplied_bug7", 1, user_supplied_bug7
},
2840 {"user_supplied_bug8", 1, user_supplied_bug8
},
2841 {"user_supplied_bug9", 1, user_supplied_bug9
},
2842 {"user_supplied_bug10", 1, user_supplied_bug10
},
2843 {"user_supplied_bug11", 1, user_supplied_bug11
},
2844 {"user_supplied_bug12", 1, user_supplied_bug12
},
2845 {"user_supplied_bug13", 1, user_supplied_bug13
},
2846 {"user_supplied_bug14", 1, user_supplied_bug14
},
2847 {"user_supplied_bug15", 1, user_supplied_bug15
},
2848 {"user_supplied_bug16", 1, user_supplied_bug16
},
2852 test_st generate_tests
[] ={
2853 {"generate_pairs", 1, generate_pairs
},
2854 {"generate_data", 1, generate_data
},
2855 {"get_read", 0, get_read
},
2856 {"delete_generate", 0, delete_generate
},
2857 {"generate_buffer_data", 1, generate_buffer_data
},
2858 {"delete_buffer", 0, delete_buffer_generate
},
2859 {"generate_data", 1, generate_data
},
2860 {"mget_read", 0, mget_read
},
2861 {"mget_read_result", 0, mget_read_result
},
2862 {"mget_read_function", 0, mget_read_function
},
2863 {"cleanup", 1, cleanup_pairs
},
2864 {"generate_large_pairs", 1, generate_large_pairs
},
2865 {"generate_data", 1, generate_data
},
2866 {"generate_buffer_data", 1, generate_buffer_data
},
2867 {"cleanup", 1, cleanup_pairs
},
2871 test_st consistent_tests
[] ={
2872 {"generate_pairs", 1, generate_pairs
},
2873 {"generate_data", 1, generate_data
},
2874 {"get_read", 0, get_read_count
},
2875 {"cleanup", 1, cleanup_pairs
},
2879 collection_st collection
[] ={
2880 {"block", 0, 0, tests
},
2881 {"nonblock", pre_nonblock
, 0, tests
},
2882 {"nodelay", pre_nodelay
, 0, tests
},
2883 {"md5", pre_md5
, 0, tests
},
2884 {"crc", pre_crc
, 0, tests
},
2885 {"hsieh", pre_hsieh
, 0, tests
},
2886 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2887 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2888 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2889 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2890 {"ketama", pre_behavior_ketama
, 0, tests
},
2891 {"unix_socket", pre_unix_socket
, 0, tests
},
2892 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2893 {"poll_timeout", poll_timeout
, 0, tests
},
2894 {"gets", enable_cas
, 0, tests
},
2895 {"consistent", enable_consistent
, 0, tests
},
2896 {"wheel", enable_wheel
, 0, tests
},
2897 {"memory_allocators", set_memory_alloc
, 0, tests
},
2898 {"prefix", set_prefix
, 0, tests
},
2899 // {"udp", pre_udp, 0, tests},
2900 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2901 {"string", 0, 0, string_tests
},
2902 {"result", 0, 0, result_tests
},
2903 {"async", pre_nonblock
, 0, async_tests
},
2904 {"user", 0, 0, user_tests
},
2905 {"generate", 0, 0, generate_tests
},
2906 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2907 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
2908 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2909 {"generate_md5", pre_md5
, 0, generate_tests
},
2910 {"generate_murmur", pre_murmur
, 0, generate_tests
},
2911 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2912 {"consistent_not", 0, 0, consistent_tests
},
2913 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
2914 {"consistent_wheel", enable_wheel
, 0, consistent_tests
},
2918 #define SERVERS_TO_CREATE 5
2920 void *world_create(void)
2922 server_startup_st
*construct
;
2924 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
2925 memset(construct
, 0, sizeof(server_startup_st
));
2926 construct
->count
= SERVERS_TO_CREATE
;
2928 server_startup(construct
);
2933 void world_destroy(void *p
)
2935 server_startup_st
*construct
= (server_startup_st
*)p
;
2936 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
2937 memcached_server_list_free(servers
);
2939 server_shutdown(construct
);
2943 void get_world(world_st
*world
)
2945 world
->collections
= collection
;
2946 world
->create
= world_create
;
2947 world
->destroy
= world_destroy
;