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
);
119 assert(local_memc
->hosts
[0].port
== 43043);
121 rc
= memcached_server_add(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042);
122 assert(rc
== MEMCACHED_SUCCESS
);
123 assert(local_memc
->hosts
[0].port
== 43042);
124 assert(local_memc
->hosts
[1].port
== 43043);
126 callbacks
[0]= server_display_function
;
127 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
130 memcached_free(local_memc
);
135 memcached_return
server_display_unsort_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
138 uint32_t x
= *((uint32_t *)(context
));
140 assert(test_ports
[x
] == server
->port
);
141 *((uint32_t *)(context
))= ++x
;
143 return MEMCACHED_SUCCESS
;
146 test_return
server_unsort_test(memcached_st
*ptr
)
149 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
150 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
152 memcached_server_function callbacks
[1];
153 memcached_st
*local_memc
;
155 local_memc
= memcached_create(NULL
);
158 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
160 test_ports
[x
]= random() % 64000;
161 rc
= memcached_server_add(local_memc
, "localhost", test_ports
[x
]);
162 assert(local_memc
->number_of_hosts
== x
+1);
163 assert(local_memc
->hosts
[0].count
== x
+1);
164 assert(rc
== MEMCACHED_SUCCESS
);
167 callbacks
[0]= server_display_unsort_function
;
168 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
170 /* Now we sort old data! */
171 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
172 callbacks
[0]= server_display_function
;
173 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
176 memcached_free(local_memc
);
181 test_return
allocation_test(memcached_st
*not_used
)
184 memc
= memcached_create(NULL
);
186 memcached_free(memc
);
191 test_return
clone_test(memcached_st
*memc
)
196 clone
= memcached_clone(NULL
, NULL
);
198 memcached_free(clone
);
201 /* Can we init from null? */
204 clone
= memcached_clone(NULL
, memc
);
206 memcached_free(clone
);
209 /* Can we init from struct? */
211 memcached_st declared_clone
;
213 clone
= memcached_clone(&declared_clone
, NULL
);
215 memcached_free(clone
);
218 /* Can we init from struct? */
220 memcached_st declared_clone
;
222 clone
= memcached_clone(&declared_clone
, memc
);
224 memcached_free(clone
);
230 test_return
connection_test(memcached_st
*memc
)
234 rc
= memcached_server_add(memc
, "localhost", 0);
235 assert(rc
== MEMCACHED_SUCCESS
);
240 test_return
error_test(memcached_st
*memc
)
244 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
246 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
252 test_return
set_test(memcached_st
*memc
)
256 char *value
= "when we sanitize";
258 rc
= memcached_set(memc
, key
, strlen(key
),
259 value
, strlen(value
),
260 (time_t)0, (uint32_t)0);
261 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
266 test_return
append_test(memcached_st
*memc
)
274 rc
= memcached_flush(memc
, 0);
275 assert(rc
== MEMCACHED_SUCCESS
);
277 rc
= memcached_set(memc
, key
, strlen(key
),
278 value
, strlen(value
),
279 (time_t)0, (uint32_t)0);
280 assert(rc
== MEMCACHED_SUCCESS
);
282 rc
= memcached_append(memc
, key
, strlen(key
),
283 " the", strlen(" the"),
284 (time_t)0, (uint32_t)0);
285 assert(rc
== MEMCACHED_SUCCESS
);
287 rc
= memcached_append(memc
, key
, strlen(key
),
288 " people", strlen(" people"),
289 (time_t)0, (uint32_t)0);
290 assert(rc
== MEMCACHED_SUCCESS
);
292 value
= memcached_get(memc
, key
, strlen(key
),
293 &value_length
, &flags
, &rc
);
294 assert(!memcmp(value
, "we the people", strlen("we the people")));
295 assert(strlen("we the people") == value_length
);
296 assert(rc
== MEMCACHED_SUCCESS
);
302 test_return
append_binary_test(memcached_st
*memc
)
305 char *key
= "numbers";
306 unsigned int *store_ptr
;
307 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
313 rc
= memcached_flush(memc
, 0);
314 assert(rc
== MEMCACHED_SUCCESS
);
316 rc
= memcached_set(memc
,
319 (time_t)0, (uint32_t)0);
320 assert(rc
== MEMCACHED_SUCCESS
);
322 for (x
= 0; store_list
[x
] ; x
++)
324 rc
= memcached_append(memc
,
326 (char *)&store_list
[x
], sizeof(unsigned int),
327 (time_t)0, (uint32_t)0);
328 assert(rc
== MEMCACHED_SUCCESS
);
331 value
= memcached_get(memc
, key
, strlen(key
),
332 &value_length
, &flags
, &rc
);
333 assert((value_length
== (sizeof(unsigned int) * x
)));
334 assert(rc
== MEMCACHED_SUCCESS
);
336 store_ptr
= (unsigned int *)value
;
338 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
340 assert(*store_ptr
== store_list
[x
++]);
348 test_return
cas2_test(memcached_st
*memc
)
351 char *keys
[]= {"fudge", "son", "food"};
352 size_t key_length
[]= {5, 3, 4};
353 char *value
= "we the people";
354 size_t value_length
= strlen("we the people");
356 memcached_result_st results_obj
;
357 memcached_result_st
*results
;
360 rc
= memcached_flush(memc
, 0);
361 assert(rc
== MEMCACHED_SUCCESS
);
363 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
365 for (x
= 0; x
< 3; x
++)
367 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
368 keys
[x
], key_length
[x
],
369 (time_t)50, (uint32_t)9);
370 assert(rc
== MEMCACHED_SUCCESS
);
373 rc
= memcached_mget(memc
, keys
, key_length
, 3);
375 results
= memcached_result_create(memc
, &results_obj
);
377 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
379 assert(results
->cas
);
380 assert(rc
== MEMCACHED_SUCCESS
);
381 WATCHPOINT_ASSERT(memcached_result_cas(results
));
383 assert(!memcmp(value
, "we the people", strlen("we the people")));
384 assert(strlen("we the people") == value_length
);
385 assert(rc
== MEMCACHED_SUCCESS
);
387 memcached_result_free(&results_obj
);
392 test_return
cas_test(memcached_st
*memc
)
396 size_t key_length
= strlen("fun");
397 char *value
= "we the people";
398 size_t value_length
= strlen("we the people");
399 memcached_result_st results_obj
;
400 memcached_result_st
*results
;
403 rc
= memcached_flush(memc
, 0);
404 assert(rc
== MEMCACHED_SUCCESS
);
406 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
408 rc
= memcached_set(memc
, key
, strlen(key
),
409 value
, strlen(value
),
410 (time_t)0, (uint32_t)0);
411 assert(rc
== MEMCACHED_SUCCESS
);
413 rc
= memcached_mget(memc
, &key
, &key_length
, 1);
415 results
= memcached_result_create(memc
, &results_obj
);
417 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
419 assert(rc
== MEMCACHED_SUCCESS
);
420 WATCHPOINT_ASSERT(memcached_result_cas(results
));
422 assert(!memcmp(value
, "we the people", strlen("we the people")));
423 assert(strlen("we the people") == value_length
);
424 assert(rc
== MEMCACHED_SUCCESS
);
426 rc
= memcached_cas(memc
, key
, key_length
,
427 "change the value", strlen("change the value"),
428 0, 0, memcached_result_cas(results
));
430 assert(rc
== MEMCACHED_SUCCESS
);
432 rc
= memcached_cas(memc
, key
, key_length
,
433 "change the value", strlen("change the value"),
436 assert(rc
== MEMCACHED_DATA_EXISTS
);
439 memcached_result_free(&results_obj
);
444 test_return
prepend_test(memcached_st
*memc
)
448 char *value
= "people";
452 rc
= memcached_flush(memc
, 0);
453 assert(rc
== MEMCACHED_SUCCESS
);
455 rc
= memcached_set(memc
, key
, strlen(key
),
456 value
, strlen(value
),
457 (time_t)0, (uint32_t)0);
458 assert(rc
== MEMCACHED_SUCCESS
);
460 rc
= memcached_prepend(memc
, key
, strlen(key
),
461 "the ", strlen("the "),
462 (time_t)0, (uint32_t)0);
463 assert(rc
== MEMCACHED_SUCCESS
);
465 rc
= memcached_prepend(memc
, key
, strlen(key
),
466 "we ", strlen("we "),
467 (time_t)0, (uint32_t)0);
468 assert(rc
== MEMCACHED_SUCCESS
);
470 value
= memcached_get(memc
, key
, strlen(key
),
471 &value_length
, &flags
, &rc
);
472 assert(!memcmp(value
, "we the people", strlen("we the people")));
473 assert(strlen("we the people") == value_length
);
474 assert(rc
== MEMCACHED_SUCCESS
);
481 Set the value, then quit to make sure it is flushed.
482 Come back in and test that add fails.
484 test_return
add_test(memcached_st
*memc
)
488 char *value
= "when we sanitize";
489 unsigned long long setting_value
;
491 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
493 rc
= memcached_set(memc
, key
, strlen(key
),
494 value
, strlen(value
),
495 (time_t)0, (uint32_t)0);
496 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
497 memcached_quit(memc
);
498 rc
= memcached_add(memc
, key
, strlen(key
),
499 value
, strlen(value
),
500 (time_t)0, (uint32_t)0);
502 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
504 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
506 assert(rc
== MEMCACHED_NOTSTORED
);
511 test_return
add_wrapper(memcached_st
*memc
)
515 for (x
= 0; x
< 10000; x
++)
521 test_return
replace_test(memcached_st
*memc
)
525 char *value
= "when we sanitize";
526 char *original
= "first we insert some data";
528 rc
= memcached_set(memc
, key
, strlen(key
),
529 original
, strlen(original
),
530 (time_t)0, (uint32_t)0);
531 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
533 rc
= memcached_replace(memc
, key
, strlen(key
),
534 value
, strlen(value
),
535 (time_t)0, (uint32_t)0);
536 assert(rc
== MEMCACHED_SUCCESS
);
541 test_return
delete_test(memcached_st
*memc
)
545 char *value
= "when we sanitize";
547 rc
= memcached_set(memc
, key
, strlen(key
),
548 value
, strlen(value
),
549 (time_t)0, (uint32_t)0);
550 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
552 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
553 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
558 test_return
flush_test(memcached_st
*memc
)
562 rc
= memcached_flush(memc
, 0);
563 assert(rc
== MEMCACHED_SUCCESS
);
568 memcached_return
server_function(memcached_st
*ptr
, memcached_server_st
*server
, void *context
)
572 return MEMCACHED_SUCCESS
;
575 test_return
memcached_server_cursor_test(memcached_st
*memc
)
577 char *context
= "foo bad";
578 memcached_server_function callbacks
[1];
580 callbacks
[0]= server_function
;
581 memcached_server_cursor(memc
, callbacks
, context
, 1);
586 test_return
bad_key_test(memcached_st
*memc
)
589 char *key
= "foo bad";
591 size_t string_length
;
596 clone
= memcached_clone(NULL
, memc
);
599 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
600 assert(rc
== MEMCACHED_SUCCESS
);
602 string
= memcached_get(clone
, key
, strlen(key
),
603 &string_length
, &flags
, &rc
);
604 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
605 assert(string_length
== 0);
609 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
610 assert(rc
== MEMCACHED_SUCCESS
);
611 string
= memcached_get(clone
, key
, strlen(key
),
612 &string_length
, &flags
, &rc
);
613 assert(rc
== MEMCACHED_NOTFOUND
);
614 assert(string_length
== 0);
617 /* Test multi key for bad keys */
619 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
620 size_t key_lengths
[] = { 7, 7, 7 };
622 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
623 assert(rc
== MEMCACHED_SUCCESS
);
625 rc
= memcached_mget(clone
, keys
, key_lengths
, 3);
626 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
628 rc
= memcached_mget_by_key(clone
, "foo daddy", 9, keys
, key_lengths
, 1);
629 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
632 memcached_free(clone
);
637 #define READ_THROUGH_VALUE "set for me"
638 memcached_return
read_through_trigger(memcached_st
*memc
,
639 char *key
, size_t key_length
,
640 memcached_result_st
*result
)
643 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
646 test_return
read_through(memcached_st
*memc
)
651 size_t string_length
;
654 string
= memcached_get(memc
, key
, strlen(key
),
655 &string_length
, &flags
, &rc
);
657 assert(rc
== MEMCACHED_NOTFOUND
);
658 assert(string_length
== 0);
661 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, read_through_trigger
);
662 assert(rc
== MEMCACHED_SUCCESS
);
664 string
= memcached_get(memc
, key
, strlen(key
),
665 &string_length
, &flags
, &rc
);
667 assert(rc
== MEMCACHED_SUCCESS
);
668 assert(string_length
== strlen(READ_THROUGH_VALUE
));
669 assert(!strcmp(READ_THROUGH_VALUE
, string
));
672 string
= memcached_get(memc
, key
, strlen(key
),
673 &string_length
, &flags
, &rc
);
675 assert(rc
== MEMCACHED_SUCCESS
);
676 assert(string_length
== strlen(READ_THROUGH_VALUE
));
677 assert(!strcmp(READ_THROUGH_VALUE
, string
));
683 memcached_return
delete_trigger(memcached_st
*ptr
, const char *key
, size_t key_length
)
687 return MEMCACHED_SUCCESS
;
690 test_return
delete_through(memcached_st
*memc
)
692 memcached_trigger_delete_key callback
;
695 callback
= delete_trigger
;
697 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, callback
);
698 assert(rc
== MEMCACHED_SUCCESS
);
703 test_return
get_test(memcached_st
*memc
)
708 size_t string_length
;
711 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
712 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
714 string
= memcached_get(memc
, key
, strlen(key
),
715 &string_length
, &flags
, &rc
);
717 assert(rc
== MEMCACHED_NOTFOUND
);
718 assert(string_length
== 0);
724 test_return
get_test2(memcached_st
*memc
)
728 char *value
= "when we sanitize";
730 size_t string_length
;
733 rc
= memcached_set(memc
, key
, strlen(key
),
734 value
, strlen(value
),
735 (time_t)0, (uint32_t)0);
736 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
738 string
= memcached_get(memc
, key
, strlen(key
),
739 &string_length
, &flags
, &rc
);
742 assert(rc
== MEMCACHED_SUCCESS
);
743 assert(string_length
== strlen(value
));
744 assert(!memcmp(string
, value
, string_length
));
751 test_return
set_test2(memcached_st
*memc
)
755 char *value
= "train in the brain";
756 size_t value_length
= strlen(value
);
759 for (x
= 0; x
< 10; x
++)
761 rc
= memcached_set(memc
, key
, strlen(key
),
763 (time_t)0, (uint32_t)0);
764 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
770 test_return
set_test3(memcached_st
*memc
)
775 size_t value_length
= 8191;
778 value
= (char*)malloc(value_length
);
781 for (x
= 0; x
< value_length
; x
++)
782 value
[x
] = (char) (x
% 127);
784 for (x
= 0; x
< 1; x
++)
786 rc
= memcached_set(memc
, key
, strlen(key
),
788 (time_t)0, (uint32_t)0);
789 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
797 test_return
get_test3(memcached_st
*memc
)
802 size_t value_length
= 8191;
804 size_t string_length
;
808 value
= (char*)malloc(value_length
);
811 for (x
= 0; x
< value_length
; x
++)
812 value
[x
] = (char) (x
% 127);
814 rc
= memcached_set(memc
, key
, strlen(key
),
816 (time_t)0, (uint32_t)0);
817 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
819 string
= memcached_get(memc
, key
, strlen(key
),
820 &string_length
, &flags
, &rc
);
822 assert(rc
== MEMCACHED_SUCCESS
);
824 assert(string_length
== value_length
);
825 assert(!memcmp(string
, value
, string_length
));
833 test_return
get_test4(memcached_st
*memc
)
838 size_t value_length
= 8191;
840 size_t string_length
;
844 value
= (char*)malloc(value_length
);
847 for (x
= 0; x
< value_length
; x
++)
848 value
[x
] = (char) (x
% 127);
850 rc
= memcached_set(memc
, key
, strlen(key
),
852 (time_t)0, (uint32_t)0);
853 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
855 for (x
= 0; x
< 10; x
++)
857 string
= memcached_get(memc
, key
, strlen(key
),
858 &string_length
, &flags
, &rc
);
860 assert(rc
== MEMCACHED_SUCCESS
);
862 assert(string_length
== value_length
);
863 assert(!memcmp(string
, value
, string_length
));
872 /* Do not copy the style of this code, I just access hosts to testthis function */
873 test_return
stats_servername_test(memcached_st
*memc
)
876 memcached_stat_st stat
;
877 rc
= memcached_stat_servername(&stat
, NULL
,
878 memc
->hosts
[0].hostname
,
879 memc
->hosts
[0].port
);
884 test_return
increment_test(memcached_st
*memc
)
891 rc
= memcached_set(memc
, key
, strlen(key
),
892 value
, strlen(value
),
893 (time_t)0, (uint32_t)0);
894 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
896 rc
= memcached_increment(memc
, key
, strlen(key
),
898 assert(rc
== MEMCACHED_SUCCESS
);
899 assert(new_number
== 1);
901 rc
= memcached_increment(memc
, key
, strlen(key
),
903 assert(rc
== MEMCACHED_SUCCESS
);
904 assert(new_number
== 2);
909 test_return
decrement_test(memcached_st
*memc
)
916 rc
= memcached_set(memc
, key
, strlen(key
),
917 value
, strlen(value
),
918 (time_t)0, (uint32_t)0);
919 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
921 rc
= memcached_decrement(memc
, key
, strlen(key
),
923 assert(rc
== MEMCACHED_SUCCESS
);
924 assert(new_number
== 2);
926 rc
= memcached_decrement(memc
, key
, strlen(key
),
928 assert(rc
== MEMCACHED_SUCCESS
);
929 assert(new_number
== 1);
934 test_return
quit_test(memcached_st
*memc
)
938 char *value
= "sanford and sun";
940 rc
= memcached_set(memc
, key
, strlen(key
),
941 value
, strlen(value
),
942 (time_t)10, (uint32_t)3);
943 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
944 memcached_quit(memc
);
946 rc
= memcached_set(memc
, key
, strlen(key
),
947 value
, strlen(value
),
948 (time_t)50, (uint32_t)9);
949 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
954 test_return
mget_result_test(memcached_st
*memc
)
957 char *keys
[]= {"fudge", "son", "food"};
958 size_t key_length
[]= {5, 3, 4};
961 memcached_result_st results_obj
;
962 memcached_result_st
*results
;
964 results
= memcached_result_create(memc
, &results_obj
);
966 assert(&results_obj
== results
);
968 /* We need to empty the server before continueing test */
969 rc
= memcached_flush(memc
, 0);
970 assert(rc
== MEMCACHED_SUCCESS
);
972 rc
= memcached_mget(memc
, keys
, key_length
, 3);
973 assert(rc
== MEMCACHED_SUCCESS
);
975 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
980 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
982 assert(rc
== MEMCACHED_END
);
984 for (x
= 0; x
< 3; x
++)
986 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
987 keys
[x
], key_length
[x
],
988 (time_t)50, (uint32_t)9);
989 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
992 rc
= memcached_mget(memc
, keys
, key_length
, 3);
993 assert(rc
== MEMCACHED_SUCCESS
);
995 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
998 assert(&results_obj
== results
);
999 assert(rc
== MEMCACHED_SUCCESS
);
1000 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1001 assert(!memcmp(memcached_result_key_value(results
),
1002 memcached_result_value(results
),
1003 memcached_result_length(results
)));
1006 memcached_result_free(&results_obj
);
1011 test_return
mget_result_alloc_test(memcached_st
*memc
)
1013 memcached_return rc
;
1014 char *keys
[]= {"fudge", "son", "food"};
1015 size_t key_length
[]= {5, 3, 4};
1018 memcached_result_st
*results
;
1020 /* We need to empty the server before continueing test */
1021 rc
= memcached_flush(memc
, 0);
1022 assert(rc
== MEMCACHED_SUCCESS
);
1024 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1025 assert(rc
== MEMCACHED_SUCCESS
);
1027 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1032 assert(rc
== MEMCACHED_END
);
1034 for (x
= 0; x
< 3; x
++)
1036 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1037 keys
[x
], key_length
[x
],
1038 (time_t)50, (uint32_t)9);
1039 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1042 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1043 assert(rc
== MEMCACHED_SUCCESS
);
1046 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1049 assert(rc
== MEMCACHED_SUCCESS
);
1050 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1051 assert(!memcmp(memcached_result_key_value(results
),
1052 memcached_result_value(results
),
1053 memcached_result_length(results
)));
1054 memcached_result_free(results
);
1061 /* Count the results */
1062 unsigned int callback_counter(memcached_st
*ptr
, memcached_result_st
*result
, void *context
)
1064 unsigned int *counter
= (unsigned int *)context
;
1066 *counter
= *counter
+ 1;
1071 test_return
mget_result_function(memcached_st
*memc
)
1073 memcached_return rc
;
1074 char *keys
[]= {"fudge", "son", "food"};
1075 size_t key_length
[]= {5, 3, 4};
1077 unsigned int counter
;
1078 memcached_execute_function callbacks
[1];
1080 /* We need to empty the server before continueing test */
1081 rc
= memcached_flush(memc
, 0);
1082 for (x
= 0; x
< 3; x
++)
1084 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1085 keys
[x
], key_length
[x
],
1086 (time_t)50, (uint32_t)9);
1087 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1090 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1091 assert(rc
== MEMCACHED_SUCCESS
);
1093 callbacks
[0]= &callback_counter
;
1095 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1097 assert(counter
== 3);
1102 test_return
mget_test(memcached_st
*memc
)
1104 memcached_return rc
;
1105 char *keys
[]= {"fudge", "son", "food"};
1106 size_t key_length
[]= {5, 3, 4};
1110 char return_key
[MEMCACHED_MAX_KEY
];
1111 size_t return_key_length
;
1113 size_t return_value_length
;
1115 /* We need to empty the server before continueing test */
1116 rc
= memcached_flush(memc
, 0);
1117 assert(rc
== MEMCACHED_SUCCESS
);
1119 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1120 assert(rc
== MEMCACHED_SUCCESS
);
1122 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1123 &return_value_length
, &flags
, &rc
)) != NULL
)
1125 assert(return_value
);
1127 assert(!return_value
);
1128 assert(return_value_length
== 0);
1129 assert(rc
== MEMCACHED_END
);
1131 for (x
= 0; x
< 3; x
++)
1133 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1134 keys
[x
], key_length
[x
],
1135 (time_t)50, (uint32_t)9);
1136 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1139 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1140 assert(rc
== MEMCACHED_SUCCESS
);
1143 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1144 &return_value_length
, &flags
, &rc
)))
1146 assert(return_value
);
1147 assert(rc
== MEMCACHED_SUCCESS
);
1148 assert(return_key_length
== return_value_length
);
1149 assert(!memcmp(return_value
, return_key
, return_value_length
));
1157 test_return
get_stats_keys(memcached_st
*memc
)
1161 memcached_stat_st stat
;
1162 memcached_return rc
;
1164 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
1165 assert(rc
== MEMCACHED_SUCCESS
);
1166 for (ptr
= list
; *ptr
; ptr
++)
1175 test_return
version_string_test(memcached_st
*memc
)
1177 const char *version_string
;
1179 version_string
= memcached_lib_version();
1181 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1186 test_return
get_stats(memcached_st
*memc
)
1191 memcached_return rc
;
1192 memcached_stat_st
*stat
;
1194 stat
= memcached_stat(memc
, NULL
, &rc
);
1195 assert(rc
== MEMCACHED_SUCCESS
);
1197 assert(rc
== MEMCACHED_SUCCESS
);
1200 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1202 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
1203 assert(rc
== MEMCACHED_SUCCESS
);
1204 for (ptr
= list
; *ptr
; ptr
++);
1209 memcached_stat_free(NULL
, stat
);
1214 test_return
add_host_test(memcached_st
*memc
)
1217 memcached_server_st
*servers
;
1218 memcached_return rc
;
1219 char servername
[]= "0.example.com";
1221 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1223 assert(1 == memcached_server_list_count(servers
));
1225 for (x
= 2; x
< 20; x
++)
1227 char buffer
[SMALL_STRING_LEN
];
1229 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1230 servers
= memcached_server_list_append(servers
, buffer
, 401,
1232 assert(rc
== MEMCACHED_SUCCESS
);
1233 assert(x
== memcached_server_list_count(servers
));
1236 rc
= memcached_server_push(memc
, servers
);
1237 assert(rc
== MEMCACHED_SUCCESS
);
1238 rc
= memcached_server_push(memc
, servers
);
1239 assert(rc
== MEMCACHED_SUCCESS
);
1241 memcached_server_list_free(servers
);
1246 memcached_return
clone_test_callback(memcached_st
*parent
, memcached_st
*clone
)
1248 return MEMCACHED_SUCCESS
;
1251 memcached_return
cleanup_test_callback(memcached_st
*ptr
)
1253 return MEMCACHED_SUCCESS
;
1256 test_return
callback_test(memcached_st
*memc
)
1258 /* Test User Data */
1262 memcached_return rc
;
1264 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1265 assert(rc
== MEMCACHED_SUCCESS
);
1266 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1267 assert(*test_ptr
== x
);
1270 /* Test Clone Callback */
1272 memcached_clone_func temp_function
;
1273 memcached_return rc
;
1275 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, clone_test_callback
);
1276 assert(rc
== MEMCACHED_SUCCESS
);
1277 temp_function
= (memcached_clone_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1278 assert(temp_function
== clone_test_callback
);
1281 /* Test Cleanup Callback */
1283 memcached_cleanup_func temp_function
;
1284 memcached_return rc
;
1286 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, cleanup_test_callback
);
1287 assert(rc
== MEMCACHED_SUCCESS
);
1288 temp_function
= (memcached_cleanup_func
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1289 assert(temp_function
== cleanup_test_callback
);
1295 /* We don't test the behavior itself, we test the switches */
1296 test_return
behavior_test(memcached_st
*memc
)
1298 unsigned long long value
;
1299 unsigned int set
= 1;
1301 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1302 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1305 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1306 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1309 set
= MEMCACHED_HASH_MD5
;
1310 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1311 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1312 assert(value
== MEMCACHED_HASH_MD5
);
1316 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1317 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1320 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1321 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1324 set
= MEMCACHED_HASH_DEFAULT
;
1325 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1326 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1327 assert(value
== MEMCACHED_HASH_DEFAULT
);
1329 set
= MEMCACHED_HASH_CRC
;
1330 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1331 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1332 assert(value
== MEMCACHED_HASH_CRC
);
1334 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1337 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1343 /* Test case provided by Cal Haldenbrand */
1344 test_return
user_supplied_bug1(memcached_st
*memc
)
1346 unsigned int setter
= 1;
1349 unsigned long long total
= 0;
1352 char randomstuff
[6 * 1024];
1353 memcached_return rc
;
1355 memset(randomstuff
, 0, 6 * 1024);
1357 /* We just keep looking at the same values over and over */
1360 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1361 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1365 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1369 size
= (rand() % ( 5 * 1024 ) ) + 400;
1370 memset(randomstuff
, 0, 6 * 1024);
1371 assert(size
< 6 * 1024); /* Being safe here */
1373 for (j
= 0 ; j
< size
;j
++)
1374 randomstuff
[j
] = (char) (rand() % 26) + 97;
1377 sprintf(key
, "%d", x
);
1378 rc
= memcached_set(memc
, key
, strlen(key
),
1379 randomstuff
, strlen(randomstuff
), 10, 0);
1380 /* If we fail, lets try again */
1381 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1382 rc
= memcached_set(memc
, key
, strlen(key
),
1383 randomstuff
, strlen(randomstuff
), 10, 0);
1384 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1390 /* Test case provided by Cal Haldenbrand */
1391 test_return
user_supplied_bug2(memcached_st
*memc
)
1394 unsigned int setter
;
1396 unsigned long long total
;
1399 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1400 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1402 setter
= 20 * 1024576;
1403 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1404 setter
= 20 * 1024576;
1405 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1406 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1407 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1409 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1412 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1414 memcached_return rc
= MEMCACHED_SUCCESS
;
1415 char buffer
[SMALL_STRING_LEN
];
1420 memset(buffer
, 0, SMALL_STRING_LEN
);
1422 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1423 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1424 &val_len
, &flags
, &rc
);
1425 if (rc
!= MEMCACHED_SUCCESS
)
1427 if (rc
== MEMCACHED_NOTFOUND
)
1431 WATCHPOINT_ERROR(rc
);
1445 /* Do a large mget() over all the keys we think exist */
1446 #define KEY_COUNT 3000 // * 1024576
1447 test_return
user_supplied_bug3(memcached_st
*memc
)
1449 memcached_return rc
;
1450 unsigned int setter
;
1453 size_t key_lengths
[KEY_COUNT
];
1456 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1457 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1459 setter
= 20 * 1024576;
1460 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1461 setter
= 20 * 1024576;
1462 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1463 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1464 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1467 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1469 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1470 for (x
= 0; x
< KEY_COUNT
; x
++)
1474 snprintf(buffer
, 30, "%u", x
);
1475 keys
[x
]= strdup(buffer
);
1476 key_lengths
[x
]= strlen(keys
[x
]);
1479 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1480 assert(rc
== MEMCACHED_SUCCESS
);
1482 /* Turn this into a help function */
1484 char return_key
[MEMCACHED_MAX_KEY
];
1485 size_t return_key_length
;
1487 size_t return_value_length
;
1490 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1491 &return_value_length
, &flags
, &rc
)))
1493 assert(return_value
);
1494 assert(rc
== MEMCACHED_SUCCESS
);
1499 for (x
= 0; x
< KEY_COUNT
; x
++)
1506 /* Make sure we behave properly if server list has no values */
1507 test_return
user_supplied_bug4(memcached_st
*memc
)
1509 memcached_return rc
;
1510 char *keys
[]= {"fudge", "son", "food"};
1511 size_t key_length
[]= {5, 3, 4};
1514 char return_key
[MEMCACHED_MAX_KEY
];
1515 size_t return_key_length
;
1517 size_t return_value_length
;
1519 /* Here we free everything before running a bunch of mget tests */
1521 memcached_server_list_free(memc
->hosts
);
1523 memc
->number_of_hosts
= 0;
1527 /* We need to empty the server before continueing test */
1528 rc
= memcached_flush(memc
, 0);
1529 assert(rc
== MEMCACHED_NO_SERVERS
);
1531 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1532 assert(rc
== MEMCACHED_NO_SERVERS
);
1534 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1535 &return_value_length
, &flags
, &rc
)) != NULL
)
1537 assert(return_value
);
1539 assert(!return_value
);
1540 assert(return_value_length
== 0);
1541 assert(rc
== MEMCACHED_NO_SERVERS
);
1543 for (x
= 0; x
< 3; x
++)
1545 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1546 keys
[x
], key_length
[x
],
1547 (time_t)50, (uint32_t)9);
1548 assert(rc
== MEMCACHED_NO_SERVERS
);
1551 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1552 assert(rc
== MEMCACHED_NO_SERVERS
);
1555 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1556 &return_value_length
, &flags
, &rc
)))
1558 assert(return_value
);
1559 assert(rc
== MEMCACHED_SUCCESS
);
1560 assert(return_key_length
== return_value_length
);
1561 assert(!memcmp(return_value
, return_key
, return_value_length
));
1569 #define VALUE_SIZE_BUG5 1048064
1570 test_return
user_supplied_bug5(memcached_st
*memc
)
1572 memcached_return rc
;
1573 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1574 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1575 char return_key
[MEMCACHED_MAX_KEY
];
1576 size_t return_key_length
;
1578 size_t value_length
;
1582 char insert_data
[VALUE_SIZE_BUG5
];
1584 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1585 insert_data
[x
]= rand();
1587 memcached_flush(memc
, 0);
1588 value
= memcached_get(memc
, keys
[0], key_length
[0],
1589 &value_length
, &flags
, &rc
);
1590 assert(value
== NULL
);
1591 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1594 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1595 &value_length
, &flags
, &rc
)))
1599 for (x
= 0; x
< 4; x
++)
1601 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1602 insert_data
, VALUE_SIZE_BUG5
,
1603 (time_t)0, (uint32_t)0);
1604 assert(rc
== MEMCACHED_SUCCESS
);
1607 for (x
= 0; x
< 10; x
++)
1609 value
= memcached_get(memc
, keys
[0], key_length
[0],
1610 &value_length
, &flags
, &rc
);
1614 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1616 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1617 &value_length
, &flags
, &rc
)))
1628 test_return
user_supplied_bug6(memcached_st
*memc
)
1630 memcached_return rc
;
1631 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1632 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1633 char return_key
[MEMCACHED_MAX_KEY
];
1634 size_t return_key_length
;
1636 size_t value_length
;
1640 char insert_data
[VALUE_SIZE_BUG5
];
1642 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1643 insert_data
[x
]= rand();
1645 memcached_flush(memc
, 0);
1646 value
= memcached_get(memc
, keys
[0], key_length
[0],
1647 &value_length
, &flags
, &rc
);
1648 assert(value
== NULL
);
1649 assert(rc
== MEMCACHED_NOTFOUND
);
1650 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1651 assert(rc
== MEMCACHED_SUCCESS
);
1654 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1655 &value_length
, &flags
, &rc
)))
1658 assert(rc
== MEMCACHED_END
);
1660 for (x
= 0; x
< 4; x
++)
1662 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1663 insert_data
, VALUE_SIZE_BUG5
,
1664 (time_t)0, (uint32_t)0);
1665 assert(rc
== MEMCACHED_SUCCESS
);
1668 for (x
= 0; x
< 2; x
++)
1670 value
= memcached_get(memc
, keys
[0], key_length
[0],
1671 &value_length
, &flags
, &rc
);
1675 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1676 assert(rc
== MEMCACHED_SUCCESS
);
1678 /* We test for purge of partial complete fetches */
1679 for (count
= 3; count
; count
--)
1681 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1682 &value_length
, &flags
, &rc
);
1683 assert(rc
== MEMCACHED_SUCCESS
);
1684 assert(!(memcmp(value
, insert_data
, value_length
)));
1685 assert(value_length
);
1693 test_return
user_supplied_bug8(memcached_st
*memc
)
1695 memcached_return rc
;
1697 memcached_st
*clone
;
1699 memcached_server_st
*servers
;
1700 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";
1702 servers
= memcached_servers_parse(server_list
);
1705 mine
= memcached_create(NULL
);
1706 rc
= memcached_server_push(mine
, servers
);
1707 assert(rc
== MEMCACHED_SUCCESS
);
1708 memcached_server_list_free(servers
);
1711 clone
= memcached_clone(NULL
, mine
);
1713 memcached_quit(mine
);
1714 memcached_quit(clone
);
1717 memcached_free(mine
);
1718 memcached_free(clone
);
1723 /* Test flag store/retrieve */
1724 test_return
user_supplied_bug7(memcached_st
*memc
)
1726 memcached_return rc
;
1727 char *keys
= "036790384900";
1728 size_t key_length
= strlen("036790384900");
1729 char return_key
[MEMCACHED_MAX_KEY
];
1730 size_t return_key_length
;
1732 size_t value_length
;
1735 char insert_data
[VALUE_SIZE_BUG5
];
1737 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1738 insert_data
[x
]= rand();
1740 memcached_flush(memc
, 0);
1743 rc
= memcached_set(memc
, keys
, key_length
,
1744 insert_data
, VALUE_SIZE_BUG5
,
1746 assert(rc
== MEMCACHED_SUCCESS
);
1749 value
= memcached_get(memc
, keys
, key_length
,
1750 &value_length
, &flags
, &rc
);
1751 assert(flags
== 245);
1755 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1758 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1759 &value_length
, &flags
, &rc
);
1760 assert(flags
== 245);
1768 test_return
user_supplied_bug9(memcached_st
*memc
)
1770 memcached_return rc
;
1771 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1772 size_t key_length
[3];
1777 char return_key
[MEMCACHED_MAX_KEY
];
1778 size_t return_key_length
;
1780 size_t return_value_length
;
1783 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
1784 key_length
[1]= strlen("fudge&*@#");
1785 key_length
[2]= strlen("for^#@&$not");
1788 for (x
= 0; x
< 3; x
++)
1790 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1791 keys
[x
], key_length
[x
],
1792 (time_t)50, (uint32_t)9);
1793 assert(rc
== MEMCACHED_SUCCESS
);
1796 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1797 assert(rc
== MEMCACHED_SUCCESS
);
1799 /* We need to empty the server before continueing test */
1800 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1801 &return_value_length
, &flags
, &rc
)) != NULL
)
1803 assert(return_value
);
1812 /* We are testing with aggressive timeout to get failures */
1813 test_return
user_supplied_bug10(memcached_st
*memc
)
1817 size_t value_length
= 512;
1820 memcached_return rc
;
1821 unsigned int set
= 1;
1822 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1825 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1826 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1828 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1830 value
= (char*)malloc(value_length
* sizeof(char));
1832 for (x
= 0; x
< value_length
; x
++)
1833 value
[x
]= (char) (x
% 127);
1835 for (x
= 1; x
<= 100000; ++x
)
1837 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1839 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_BUFFERED
);
1841 if (rc
== MEMCACHED_WRITE_FAILURE
)
1846 memcached_free(mclone
);
1852 We are looking failures in the async protocol
1854 test_return
user_supplied_bug11(memcached_st
*memc
)
1858 size_t value_length
= 512;
1861 memcached_return rc
;
1862 unsigned int set
= 1;
1864 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
1866 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1867 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1869 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
1871 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
1873 assert(timeout
== -1);
1875 value
= (char*)malloc(value_length
* sizeof(char));
1877 for (x
= 0; x
< value_length
; x
++)
1878 value
[x
]= (char) (x
% 127);
1880 for (x
= 1; x
<= 100000; ++x
)
1882 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
1886 memcached_free(mclone
);
1892 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1894 test_return
user_supplied_bug12(memcached_st
*memc
)
1896 memcached_return rc
;
1898 size_t value_length
;
1900 uint64_t number_value
;
1902 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1903 &value_length
, &flags
, &rc
);
1904 assert(value
== NULL
);
1905 assert(rc
== MEMCACHED_NOTFOUND
);
1907 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1910 assert(value
== NULL
);
1911 assert(rc
== MEMCACHED_NOTFOUND
);
1913 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1915 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
1916 &value_length
, &flags
, &rc
);
1918 assert(rc
== MEMCACHED_SUCCESS
);
1921 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
1923 assert(number_value
== 2);
1924 assert(rc
== MEMCACHED_SUCCESS
);
1930 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1931 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1933 test_return
user_supplied_bug13(memcached_st
*memc
)
1935 char key
[] = "key34567890";
1937 memcached_return rc
;
1938 size_t overflowSize
;
1940 char commandFirst
[]= "set key34567890 0 0 ";
1941 char commandLast
[] = " \r\n"; /* first line of command sent to server */
1942 size_t commandLength
;
1945 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
1947 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
1949 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
1951 overflow
= malloc(testSize
);
1952 assert(overflow
!= NULL
);
1954 memset(overflow
, 'x', testSize
);
1955 rc
= memcached_set(memc
, key
, strlen(key
),
1956 overflow
, testSize
, 0, 0);
1957 assert(rc
== MEMCACHED_SUCCESS
);
1966 Test values of many different sizes
1967 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1968 set key34567890 0 0 8169 \r\n
1969 is sent followed by buffer of size 8169, followed by 8169
1971 test_return
user_supplied_bug14(memcached_st
*memc
)
1974 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1975 memcached_return rc
;
1978 size_t value_length
= 18000;
1980 size_t string_length
;
1983 size_t current_length
;
1985 value
= (char*)malloc(value_length
);
1988 for (x
= 0; x
< value_length
; x
++)
1989 value
[x
] = (char) (x
% 127);
1991 for (current_length
= 0; current_length
< value_length
; current_length
++)
1993 rc
= memcached_set(memc
, key
, strlen(key
),
1994 value
, current_length
,
1995 (time_t)0, (uint32_t)0);
1996 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1998 string
= memcached_get(memc
, key
, strlen(key
),
1999 &string_length
, &flags
, &rc
);
2001 assert(rc
== MEMCACHED_SUCCESS
);
2002 assert(string_length
== current_length
);
2003 assert(!memcmp(string
, value
, string_length
));
2014 Look for zero length value problems
2016 test_return
user_supplied_bug15(memcached_st
*memc
)
2019 memcached_return rc
;
2025 for (x
= 0; x
< 2; x
++)
2027 rc
= memcached_set(memc
, key
, strlen(key
),
2029 (time_t)0, (uint32_t)0);
2031 assert(rc
== MEMCACHED_SUCCESS
);
2033 value
= memcached_get(memc
, key
, strlen(key
),
2034 &length
, &flags
, &rc
);
2036 assert(rc
== MEMCACHED_SUCCESS
);
2037 assert(value
== NULL
);
2038 assert(length
== 0);
2041 value
= memcached_get(memc
, key
, strlen(key
),
2042 &length
, &flags
, &rc
);
2044 assert(rc
== MEMCACHED_SUCCESS
);
2045 assert(value
== NULL
);
2046 assert(length
== 0);
2053 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2054 test_return
user_supplied_bug16(memcached_st
*memc
)
2056 memcached_return rc
;
2062 rc
= memcached_set(memc
, key
, strlen(key
),
2064 (time_t)0, UINT32_MAX
);
2066 assert(rc
== MEMCACHED_SUCCESS
);
2068 value
= memcached_get(memc
, key
, strlen(key
),
2069 &length
, &flags
, &rc
);
2071 assert(rc
== MEMCACHED_SUCCESS
);
2072 assert(value
== NULL
);
2073 assert(length
== 0);
2074 assert(flags
== UINT32_MAX
);
2079 test_return
result_static(memcached_st
*memc
)
2081 memcached_result_st result
;
2082 memcached_result_st
*result_ptr
;
2084 result_ptr
= memcached_result_create(memc
, &result
);
2085 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2087 memcached_result_free(&result
);
2092 test_return
result_alloc(memcached_st
*memc
)
2094 memcached_result_st
*result
;
2096 result
= memcached_result_create(memc
, NULL
);
2098 memcached_result_free(result
);
2103 test_return
string_static_null(memcached_st
*memc
)
2105 memcached_string_st string
;
2106 memcached_string_st
*string_ptr
;
2108 string_ptr
= memcached_string_create(memc
, &string
, 0);
2109 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
2111 memcached_string_free(&string
);
2116 test_return
string_alloc_null(memcached_st
*memc
)
2118 memcached_string_st
*string
;
2120 string
= memcached_string_create(memc
, NULL
, 0);
2122 memcached_string_free(string
);
2127 test_return
string_alloc_with_size(memcached_st
*memc
)
2129 memcached_string_st
*string
;
2131 string
= memcached_string_create(memc
, NULL
, 1024);
2133 memcached_string_free(string
);
2138 test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2140 memcached_string_st
*string
;
2142 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
2143 assert(string
== NULL
);
2148 test_return
string_alloc_append(memcached_st
*memc
)
2151 char buffer
[SMALL_STRING_LEN
];
2152 memcached_string_st
*string
;
2154 /* Ring the bell! */
2155 memset(buffer
, 6, SMALL_STRING_LEN
);
2157 string
= memcached_string_create(memc
, NULL
, 100);
2160 for (x
= 0; x
< 1024; x
++)
2162 memcached_return rc
;
2163 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2164 assert(rc
== MEMCACHED_SUCCESS
);
2166 memcached_string_free(string
);
2171 test_return
string_alloc_append_toobig(memcached_st
*memc
)
2173 memcached_return rc
;
2175 char buffer
[SMALL_STRING_LEN
];
2176 memcached_string_st
*string
;
2178 /* Ring the bell! */
2179 memset(buffer
, 6, SMALL_STRING_LEN
);
2181 string
= memcached_string_create(memc
, NULL
, 100);
2184 for (x
= 0; x
< 1024; x
++)
2186 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2187 assert(rc
== MEMCACHED_SUCCESS
);
2189 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
2190 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2191 memcached_string_free(string
);
2196 test_return
cleanup_pairs(memcached_st
*memc
)
2198 pairs_free(global_pairs
);
2203 test_return
generate_pairs(memcached_st
*memc
)
2205 unsigned long long x
;
2206 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2207 global_count
= GLOBAL_COUNT
;
2209 for (x
= 0; x
< global_count
; x
++)
2211 global_keys
[x
]= global_pairs
[x
].key
;
2212 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2218 test_return
generate_large_pairs(memcached_st
*memc
)
2220 unsigned long long x
;
2221 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2222 global_count
= GLOBAL2_COUNT
;
2224 for (x
= 0; x
< global_count
; x
++)
2226 global_keys
[x
]= global_pairs
[x
].key
;
2227 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2233 test_return
generate_data(memcached_st
*memc
)
2235 execute_set(memc
, global_pairs
, global_count
);
2240 test_return
generate_buffer_data(memcached_st
*memc
)
2245 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2246 generate_data(memc
);
2251 test_return
get_read_count(memcached_st
*memc
)
2254 memcached_return rc
;
2255 memcached_st
*clone
;
2257 clone
= memcached_clone(NULL
, memc
);
2260 memcached_server_add(clone
, "localhost", 6666);
2264 size_t return_value_length
;
2268 for (x
= count
= 0; x
< global_count
; x
++)
2270 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2271 &return_value_length
, &flags
, &rc
);
2272 if (rc
== MEMCACHED_SUCCESS
)
2279 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2282 memcached_free(clone
);
2287 test_return
get_read(memcached_st
*memc
)
2290 memcached_return rc
;
2294 size_t return_value_length
;
2297 for (x
= 0; x
< global_count
; x
++)
2299 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2300 &return_value_length
, &flags
, &rc
);
2302 assert(return_value);
2303 assert(rc == MEMCACHED_SUCCESS);
2305 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2313 test_return
mget_read(memcached_st
*memc
)
2315 memcached_return rc
;
2317 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2318 assert(rc
== MEMCACHED_SUCCESS
);
2319 /* Turn this into a help function */
2321 char return_key
[MEMCACHED_MAX_KEY
];
2322 size_t return_key_length
;
2324 size_t return_value_length
;
2327 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2328 &return_value_length
, &flags
, &rc
)))
2330 assert(return_value
);
2331 assert(rc
== MEMCACHED_SUCCESS
);
2339 test_return
mget_read_result(memcached_st
*memc
)
2341 memcached_return rc
;
2343 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2344 assert(rc
== MEMCACHED_SUCCESS
);
2345 /* Turn this into a help function */
2347 memcached_result_st results_obj
;
2348 memcached_result_st
*results
;
2350 results
= memcached_result_create(memc
, &results_obj
);
2352 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2355 assert(rc
== MEMCACHED_SUCCESS
);
2358 memcached_result_free(&results_obj
);
2364 test_return
mget_read_function(memcached_st
*memc
)
2366 memcached_return rc
;
2367 unsigned int counter
;
2368 unsigned int (*callbacks
[1])(memcached_st
*, memcached_result_st
*, void *);
2370 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2371 assert(rc
== MEMCACHED_SUCCESS
);
2373 callbacks
[0]= &callback_counter
;
2375 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2380 test_return
delete_generate(memcached_st
*memc
)
2384 for (x
= 0; x
< global_count
; x
++)
2386 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2392 test_return
delete_buffer_generate(memcached_st
*memc
)
2398 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2400 for (x
= 0; x
< global_count
; x
++)
2402 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2408 test_return
free_data(memcached_st
*memc
)
2410 pairs_free(global_pairs
);
2415 test_return
add_host_test1(memcached_st
*memc
)
2418 memcached_return rc
;
2419 char servername
[]= "0.example.com";
2420 memcached_server_st
*servers
;
2422 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
2424 assert(1 == memcached_server_list_count(servers
));
2426 for (x
= 2; x
< 20; x
++)
2428 char buffer
[SMALL_STRING_LEN
];
2430 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2431 servers
= memcached_server_list_append(servers
, buffer
, 401,
2433 assert(rc
== MEMCACHED_SUCCESS
);
2434 assert(x
== memcached_server_list_count(servers
));
2437 rc
= memcached_server_push(memc
, servers
);
2438 assert(rc
== MEMCACHED_SUCCESS
);
2439 rc
= memcached_server_push(memc
, servers
);
2440 assert(rc
== MEMCACHED_SUCCESS
);
2442 memcached_server_list_free(servers
);
2447 memcached_return
pre_nonblock(memcached_st
*memc
)
2449 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2451 return MEMCACHED_SUCCESS
;
2454 memcached_return
pre_murmur(memcached_st
*memc
)
2456 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2458 return MEMCACHED_SUCCESS
;
2461 memcached_return
pre_md5(memcached_st
*memc
)
2463 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2465 return MEMCACHED_SUCCESS
;
2468 memcached_return
pre_crc(memcached_st
*memc
)
2470 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2472 return MEMCACHED_SUCCESS
;
2475 memcached_return
pre_hsieh(memcached_st
*memc
)
2477 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2479 return MEMCACHED_SUCCESS
;
2482 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2484 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2486 return MEMCACHED_SUCCESS
;
2489 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2491 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2493 return MEMCACHED_SUCCESS
;
2496 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2498 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2500 return MEMCACHED_SUCCESS
;
2503 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2505 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2507 return MEMCACHED_SUCCESS
;
2510 memcached_return
pre_behavior_ketama(memcached_st
*memc
)
2512 memcached_return rc
;
2515 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
2516 assert(rc
== MEMCACHED_SUCCESS
);
2518 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
2521 return MEMCACHED_SUCCESS
;
2524 void my_free(memcached_st
*ptr
, void *mem
)
2529 void *my_malloc(memcached_st
*ptr
, const size_t size
)
2531 return malloc(size
);
2534 void *my_realloc(memcached_st
*ptr
, void *mem
, const size_t size
)
2536 return realloc(mem
, size
);
2539 memcached_return
set_prefix(memcached_st
*memc
)
2541 memcached_return rc
;
2542 const char *key
= "mine";
2545 /* Make sure be default none exists */
2546 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2547 assert(rc
== MEMCACHED_FAILURE
);
2549 /* Test a clean set */
2550 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2551 assert(rc
== MEMCACHED_SUCCESS
);
2553 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2554 assert(memcmp(value
, key
, 4) == 0);
2555 assert(rc
== MEMCACHED_SUCCESS
);
2557 /* Test that we can turn it off */
2558 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2559 assert(rc
== MEMCACHED_SUCCESS
);
2561 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2562 assert(rc
== MEMCACHED_FAILURE
);
2564 /* Now setup for main test */
2565 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
2566 assert(rc
== MEMCACHED_SUCCESS
);
2568 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2569 assert(rc
== MEMCACHED_SUCCESS
);
2570 assert(memcmp(value
, key
, 4) == 0);
2572 /* Set to Zero, and then Set to something too large */
2575 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
2576 assert(rc
== MEMCACHED_SUCCESS
);
2578 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
2579 assert(rc
== MEMCACHED_FAILURE
);
2580 assert(value
== NULL
);
2582 /* Test a long key for failure */
2583 long_key
= "Thisismorethentheallottednumberofcharacters";
2584 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2585 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2587 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2588 long_key
= "This is more then the allotted number of characters";
2589 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2590 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2592 /* Test for a bad prefix, but with a short key */
2593 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
2594 assert(rc
== MEMCACHED_SUCCESS
);
2596 long_key
= "dog cat";
2597 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
2598 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
2601 return MEMCACHED_SUCCESS
;
2604 memcached_return
set_memory_alloc(memcached_st
*memc
)
2607 memcached_malloc_function test_ptr
;
2608 memcached_return rc
;
2610 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &my_malloc
);
2611 assert(rc
== MEMCACHED_SUCCESS
);
2612 test_ptr
= (memcached_malloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
2613 assert(rc
== MEMCACHED_SUCCESS
);
2614 assert(test_ptr
== my_malloc
);
2618 memcached_realloc_function test_ptr
;
2619 memcached_return rc
;
2621 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &my_realloc
);
2622 assert(rc
== MEMCACHED_SUCCESS
);
2623 test_ptr
= (memcached_realloc_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
2624 assert(rc
== MEMCACHED_SUCCESS
);
2625 assert(test_ptr
== my_realloc
);
2629 memcached_free_function test_ptr
;
2630 memcached_return rc
;
2632 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, my_free
);
2633 assert(rc
== MEMCACHED_SUCCESS
);
2634 test_ptr
= (memcached_free_function
)memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
2635 assert(rc
== MEMCACHED_SUCCESS
);
2636 assert(test_ptr
== my_free
);
2639 return MEMCACHED_SUCCESS
;
2642 memcached_return
enable_consistent(memcached_st
*memc
)
2644 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
2645 memcached_hash hash
;
2646 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
2649 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
2650 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
2652 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2653 assert(hash
== MEMCACHED_HASH_HSIEH
);
2656 return MEMCACHED_SUCCESS
;
2659 memcached_return
enable_cas(memcached_st
*memc
)
2661 unsigned int set
= 1;
2663 memcached_version(memc
);
2665 if (memc
->hosts
[0].major_version
>= 1 &&
2666 memc
->hosts
[0].minor_version
>= 2 &&
2667 memc
->hosts
[0].micro_version
>= 4)
2669 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
2671 return MEMCACHED_SUCCESS
;
2674 return MEMCACHED_FAILURE
;
2677 memcached_return
check_for_1_2_3(memcached_st
*memc
)
2679 memcached_version(memc
);
2681 if (memc
->hosts
[0].major_version
>= 1 &&
2682 memc
->hosts
[0].minor_version
>= 2 &&
2683 memc
->hosts
[0].micro_version
>= 4)
2684 return MEMCACHED_SUCCESS
;
2686 return MEMCACHED_FAILURE
;
2689 memcached_return
pre_unix_socket(memcached_st
*memc
)
2691 memcached_return rc
;
2694 memcached_server_list_free(memc
->hosts
);
2696 memc
->number_of_hosts
= 0;
2698 if (stat("/tmp/memcached.socket", &buf
))
2699 return MEMCACHED_FAILURE
;
2701 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
2706 memcached_return
pre_udp(memcached_st
*memc
)
2708 memcached_return rc
;
2710 memcached_server_list_free(memc
->hosts
);
2712 memc
->number_of_hosts
= 0;
2715 return MEMCACHED_FAILURE
;
2717 rc
= memcached_server_add_udp(memc
, "localhost", MEMCACHED_DEFAULT_PORT
);
2722 memcached_return
pre_nodelay(memcached_st
*memc
)
2724 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2725 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
2727 return MEMCACHED_SUCCESS
;
2730 memcached_return
poll_timeout(memcached_st
*memc
)
2736 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
2738 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2740 assert(timeout
== 100);
2742 return MEMCACHED_SUCCESS
;
2746 /* Clean the server before beginning testing */
2748 {"flush", 0, flush_test
},
2749 {"init", 0, init_test
},
2750 {"allocation", 0, allocation_test
},
2751 {"server_list_null_test", 0, server_list_null_test
},
2752 {"server_unsort", 0, server_unsort_test
},
2753 {"server_sort", 0, server_sort_test
},
2754 {"server_sort2", 0, server_sort2_test
},
2755 {"clone_test", 0, clone_test
},
2756 {"error", 0, error_test
},
2757 {"set", 0, set_test
},
2758 {"set2", 0, set_test2
},
2759 {"set3", 0, set_test3
},
2760 {"add", 1, add_test
},
2761 {"replace", 1, replace_test
},
2762 {"delete", 1, delete_test
},
2763 {"get", 1, get_test
},
2764 {"get2", 0, get_test2
},
2765 {"get3", 0, get_test3
},
2766 {"get4", 0, get_test4
},
2767 {"stats_servername", 0, stats_servername_test
},
2768 {"increment", 0, increment_test
},
2769 {"decrement", 0, decrement_test
},
2770 {"quit", 0, quit_test
},
2771 {"mget", 1, mget_test
},
2772 {"mget_result", 1, mget_result_test
},
2773 {"mget_result_alloc", 1, mget_result_alloc_test
},
2774 {"mget_result_function", 1, mget_result_function
},
2775 {"get_stats", 0, get_stats
},
2776 {"add_host_test", 0, add_host_test
},
2777 {"get_stats_keys", 0, get_stats_keys
},
2778 {"behavior_test", 0, get_stats_keys
},
2779 {"callback_test", 0, get_stats_keys
},
2780 {"version_string_test", 0, version_string_test
},
2781 {"bad_key", 1, bad_key_test
},
2782 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
2783 {"read_through", 1, read_through
},
2784 {"delete_through", 1, delete_through
},
2788 test_st async_tests
[] ={
2789 {"add", 1, add_wrapper
},
2793 test_st string_tests
[] ={
2794 {"string static with null", 0, string_static_null
},
2795 {"string alloc with null", 0, string_alloc_null
},
2796 {"string alloc with 1K", 0, string_alloc_with_size
},
2797 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
2798 {"string append", 0, string_alloc_append
},
2799 {"string append failure (too big)", 0, string_alloc_append_toobig
},
2803 test_st result_tests
[] ={
2804 {"result static", 0, result_static
},
2805 {"result alloc", 0, result_alloc
},
2809 test_st version_1_2_3
[] ={
2810 {"append", 0, append_test
},
2811 {"prepend", 0, prepend_test
},
2812 {"cas", 0, cas_test
},
2813 {"cas2", 0, cas2_test
},
2814 {"append_binary", 0, append_binary_test
},
2818 test_st user_tests
[] ={
2819 {"user_supplied_bug1", 0, user_supplied_bug1
},
2820 {"user_supplied_bug2", 0, user_supplied_bug2
},
2821 {"user_supplied_bug3", 0, user_supplied_bug3
},
2822 {"user_supplied_bug4", 0, user_supplied_bug4
},
2823 {"user_supplied_bug5", 1, user_supplied_bug5
},
2824 {"user_supplied_bug6", 1, user_supplied_bug6
},
2825 {"user_supplied_bug7", 1, user_supplied_bug7
},
2826 {"user_supplied_bug8", 1, user_supplied_bug8
},
2827 {"user_supplied_bug9", 1, user_supplied_bug9
},
2828 {"user_supplied_bug10", 1, user_supplied_bug10
},
2829 {"user_supplied_bug11", 1, user_supplied_bug11
},
2830 {"user_supplied_bug12", 1, user_supplied_bug12
},
2831 {"user_supplied_bug13", 1, user_supplied_bug13
},
2832 {"user_supplied_bug14", 1, user_supplied_bug14
},
2833 {"user_supplied_bug15", 1, user_supplied_bug15
},
2834 {"user_supplied_bug16", 1, user_supplied_bug16
},
2838 test_st generate_tests
[] ={
2839 {"generate_pairs", 1, generate_pairs
},
2840 {"generate_data", 1, generate_data
},
2841 {"get_read", 0, get_read
},
2842 {"delete_generate", 0, delete_generate
},
2843 {"generate_buffer_data", 1, generate_buffer_data
},
2844 {"delete_buffer", 0, delete_buffer_generate
},
2845 {"generate_data", 1, generate_data
},
2846 {"mget_read", 0, mget_read
},
2847 {"mget_read_result", 0, mget_read_result
},
2848 {"mget_read_function", 0, mget_read_function
},
2849 {"cleanup", 1, cleanup_pairs
},
2850 {"generate_large_pairs", 1, generate_large_pairs
},
2851 {"generate_data", 1, generate_data
},
2852 {"generate_buffer_data", 1, generate_buffer_data
},
2853 {"cleanup", 1, cleanup_pairs
},
2857 test_st consistent_tests
[] ={
2858 {"generate_pairs", 1, generate_pairs
},
2859 {"generate_data", 1, generate_data
},
2860 {"get_read", 0, get_read_count
},
2861 {"cleanup", 1, cleanup_pairs
},
2865 collection_st collection
[] ={
2866 {"block", 0, 0, tests
},
2867 {"nonblock", pre_nonblock
, 0, tests
},
2868 {"nodelay", pre_nodelay
, 0, tests
},
2869 {"md5", pre_md5
, 0, tests
},
2870 {"crc", pre_crc
, 0, tests
},
2871 {"hsieh", pre_hsieh
, 0, tests
},
2872 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
2873 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
2874 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
2875 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
2876 {"ketama", pre_behavior_ketama
, 0, tests
},
2877 {"unix_socket", pre_unix_socket
, 0, tests
},
2878 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
2879 {"poll_timeout", poll_timeout
, 0, tests
},
2880 {"gets", enable_cas
, 0, tests
},
2881 {"consistent", enable_consistent
, 0, tests
},
2882 {"memory_allocators", set_memory_alloc
, 0, tests
},
2883 {"prefix", set_prefix
, 0, tests
},
2884 // {"udp", pre_udp, 0, tests},
2885 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
2886 {"string", 0, 0, string_tests
},
2887 {"result", 0, 0, result_tests
},
2888 {"async", pre_nonblock
, 0, async_tests
},
2889 {"user", 0, 0, user_tests
},
2890 {"generate", 0, 0, generate_tests
},
2891 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
2892 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
2893 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
2894 {"generate_md5", pre_md5
, 0, generate_tests
},
2895 {"generate_murmur", pre_murmur
, 0, generate_tests
},
2896 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
2897 {"consistent_not", 0, 0, consistent_tests
},
2898 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
2902 #define SERVERS_TO_CREATE 5
2904 void *world_create(void)
2906 server_startup_st
*construct
;
2908 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
2909 memset(construct
, 0, sizeof(server_startup_st
));
2910 construct
->count
= SERVERS_TO_CREATE
;
2912 server_startup(construct
);
2917 void world_destroy(void *p
)
2919 server_startup_st
*construct
= (server_startup_st
*)p
;
2920 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
2921 memcached_server_list_free(servers
);
2923 server_shutdown(construct
);
2927 void get_world(world_st
*world
)
2929 world
->collections
= collection
;
2930 world
->create
= world_create
;
2931 world
->destroy
= world_destroy
;