2 Sample test application.
4 #include "libmemcached/common.h"
11 #include <sys/types.h>
16 #include "clients/generator.h"
17 #include "clients/execute.h"
20 #define INT64_MAX LONG_MAX
23 #define INT32_MAX INT_MAX
29 #ifdef HAVE_LIBMEMCACHEDUTIL
31 #include "libmemcached/memcached_util.h"
34 #define GLOBAL_COUNT 10000
35 #define GLOBAL2_COUNT 100
36 #define SERVERS_TO_CREATE 5
37 static uint32_t global_count
;
39 static pairs_st
*global_pairs
;
40 static char *global_keys
[GLOBAL_COUNT
];
41 static size_t global_keys_length
[GLOBAL_COUNT
];
43 static test_return
init_test(memcached_st
*not_used
__attribute__((unused
)))
47 (void)memcached_create(&memc
);
48 memcached_free(&memc
);
53 static test_return
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
55 memcached_server_st
*server_list
;
58 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
59 assert(server_list
== NULL
);
61 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
62 assert(server_list
== NULL
);
64 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
65 assert(server_list
== NULL
);
70 #define TEST_PORT_COUNT 7
71 uint32_t test_ports
[TEST_PORT_COUNT
];
73 static memcached_return
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
76 uint32_t bigger
= *((uint32_t *)(context
));
77 assert(bigger
<= server
->port
);
78 *((uint32_t *)(context
))= server
->port
;
80 return MEMCACHED_SUCCESS
;
83 static test_return
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
86 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
88 memcached_server_function callbacks
[1];
89 memcached_st
*local_memc
;
91 local_memc
= memcached_create(NULL
);
93 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
95 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
97 test_ports
[x
]= (uint32_t)random() % 64000;
98 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
99 assert(local_memc
->number_of_hosts
== x
+ 1);
100 assert(local_memc
->hosts
[0].count
== x
+1);
101 assert(rc
== MEMCACHED_SUCCESS
);
104 callbacks
[0]= server_display_function
;
105 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
108 memcached_free(local_memc
);
113 static test_return
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
115 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
117 memcached_server_function callbacks
[1];
118 memcached_st
*local_memc
;
120 local_memc
= memcached_create(NULL
);
122 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
123 assert(rc
== MEMCACHED_SUCCESS
);
125 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
126 assert(rc
== MEMCACHED_SUCCESS
);
127 assert(local_memc
->hosts
[0].port
== 43043);
129 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
130 assert(rc
== MEMCACHED_SUCCESS
);
131 assert(local_memc
->hosts
[0].port
== 43042);
132 assert(local_memc
->hosts
[1].port
== 43043);
134 callbacks
[0]= server_display_function
;
135 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
138 memcached_free(local_memc
);
143 static memcached_return
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
146 uint32_t x
= *((uint32_t *)(context
));
148 assert(test_ports
[x
] == server
->port
);
149 *((uint32_t *)(context
))= ++x
;
151 return MEMCACHED_SUCCESS
;
154 static test_return
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
157 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
158 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
160 memcached_server_function callbacks
[1];
161 memcached_st
*local_memc
;
163 local_memc
= memcached_create(NULL
);
166 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
168 test_ports
[x
]= (uint32_t)(random() % 64000);
169 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
170 assert(local_memc
->number_of_hosts
== x
+1);
171 assert(local_memc
->hosts
[0].count
== x
+1);
172 assert(rc
== MEMCACHED_SUCCESS
);
175 callbacks
[0]= server_display_unsort_function
;
176 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
178 /* Now we sort old data! */
179 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
180 callbacks
[0]= server_display_function
;
181 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
184 memcached_free(local_memc
);
189 static test_return
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
192 memc
= memcached_create(NULL
);
194 memcached_free(memc
);
199 static test_return
clone_test(memcached_st
*memc
)
203 memcached_st
*memc_clone
;
204 memc_clone
= memcached_clone(NULL
, NULL
);
206 memcached_free(memc_clone
);
209 /* Can we init from null? */
211 memcached_st
*memc_clone
;
212 memc_clone
= memcached_clone(NULL
, memc
);
215 assert(memc_clone
->call_free
== memc
->call_free
);
216 assert(memc_clone
->call_malloc
== memc
->call_malloc
);
217 assert(memc_clone
->call_realloc
== memc
->call_realloc
);
218 assert(memc_clone
->call_calloc
== memc
->call_calloc
);
219 assert(memc_clone
->connect_timeout
== memc
->connect_timeout
);
220 assert(memc_clone
->delete_trigger
== memc
->delete_trigger
);
221 assert(memc_clone
->distribution
== memc
->distribution
);
222 assert(memc_clone
->flags
== memc
->flags
);
223 assert(memc_clone
->get_key_failure
== memc
->get_key_failure
);
224 assert(memc_clone
->hash
== memc
->hash
);
225 assert(memc_clone
->hash_continuum
== memc
->hash_continuum
);
226 assert(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
227 assert(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
228 assert(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
229 assert(memc_clone
->on_cleanup
== memc
->on_cleanup
);
230 assert(memc_clone
->on_clone
== memc
->on_clone
);
231 assert(memc_clone
->poll_timeout
== memc
->poll_timeout
);
232 assert(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
233 assert(memc_clone
->recv_size
== memc
->recv_size
);
234 assert(memc_clone
->retry_timeout
== memc
->retry_timeout
);
235 assert(memc_clone
->send_size
== memc
->send_size
);
236 assert(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
237 assert(memc_clone
->snd_timeout
== memc
->snd_timeout
);
238 assert(memc_clone
->user_data
== memc
->user_data
);
240 memcached_free(memc_clone
);
243 /* Can we init from struct? */
245 memcached_st declared_clone
;
246 memcached_st
*memc_clone
;
247 memset(&declared_clone
, 0 , sizeof(memcached_st
));
248 memc_clone
= memcached_clone(&declared_clone
, NULL
);
250 memcached_free(memc_clone
);
253 /* Can we init from struct? */
255 memcached_st declared_clone
;
256 memcached_st
*memc_clone
;
257 memset(&declared_clone
, 0 , sizeof(memcached_st
));
258 memc_clone
= memcached_clone(&declared_clone
, memc
);
260 memcached_free(memc_clone
);
266 static test_return
userdata_test(memcached_st
*memc
)
269 assert(memcached_set_user_data(memc
, foo
) == NULL
);
270 assert(memcached_get_user_data(memc
) == foo
);
271 assert(memcached_set_user_data(memc
, NULL
) == foo
);
276 static test_return
connection_test(memcached_st
*memc
)
280 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
281 assert(rc
== MEMCACHED_SUCCESS
);
286 static test_return
error_test(memcached_st
*memc
)
290 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
292 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
298 static test_return
set_test(memcached_st
*memc
)
302 char *value
= "when we sanitize";
304 rc
= memcached_set(memc
, key
, strlen(key
),
305 value
, strlen(value
),
306 (time_t)0, (uint32_t)0);
307 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
312 static test_return
append_test(memcached_st
*memc
)
320 rc
= memcached_flush(memc
, 0);
321 assert(rc
== MEMCACHED_SUCCESS
);
323 rc
= memcached_set(memc
, key
, strlen(key
),
324 value
, strlen(value
),
325 (time_t)0, (uint32_t)0);
326 assert(rc
== MEMCACHED_SUCCESS
);
328 rc
= memcached_append(memc
, key
, strlen(key
),
329 " the", strlen(" the"),
330 (time_t)0, (uint32_t)0);
331 assert(rc
== MEMCACHED_SUCCESS
);
333 rc
= memcached_append(memc
, key
, strlen(key
),
334 " people", strlen(" people"),
335 (time_t)0, (uint32_t)0);
336 assert(rc
== MEMCACHED_SUCCESS
);
338 value
= memcached_get(memc
, key
, strlen(key
),
339 &value_length
, &flags
, &rc
);
340 assert(!memcmp(value
, "we the people", strlen("we the people")));
341 assert(strlen("we the people") == value_length
);
342 assert(rc
== MEMCACHED_SUCCESS
);
348 static test_return
append_binary_test(memcached_st
*memc
)
351 char *key
= "numbers";
352 unsigned int *store_ptr
;
353 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
359 rc
= memcached_flush(memc
, 0);
360 assert(rc
== MEMCACHED_SUCCESS
);
362 rc
= memcached_set(memc
,
365 (time_t)0, (uint32_t)0);
366 assert(rc
== MEMCACHED_SUCCESS
);
368 for (x
= 0; store_list
[x
] ; x
++)
370 rc
= memcached_append(memc
,
372 (char *)&store_list
[x
], sizeof(unsigned int),
373 (time_t)0, (uint32_t)0);
374 assert(rc
== MEMCACHED_SUCCESS
);
377 value
= memcached_get(memc
, key
, strlen(key
),
378 &value_length
, &flags
, &rc
);
379 assert((value_length
== (sizeof(unsigned int) * x
)));
380 assert(rc
== MEMCACHED_SUCCESS
);
382 store_ptr
= (unsigned int *)value
;
384 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
386 assert(*store_ptr
== store_list
[x
++]);
394 static test_return
cas2_test(memcached_st
*memc
)
397 char *keys
[]= {"fudge", "son", "food"};
398 size_t key_length
[]= {5, 3, 4};
399 char *value
= "we the people";
400 size_t value_length
= strlen("we the people");
402 memcached_result_st results_obj
;
403 memcached_result_st
*results
;
406 rc
= memcached_flush(memc
, 0);
407 assert(rc
== MEMCACHED_SUCCESS
);
409 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
411 for (x
= 0; x
< 3; x
++)
413 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
414 keys
[x
], key_length
[x
],
415 (time_t)50, (uint32_t)9);
416 assert(rc
== MEMCACHED_SUCCESS
);
419 rc
= memcached_mget(memc
, keys
, key_length
, 3);
421 results
= memcached_result_create(memc
, &results_obj
);
423 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
425 assert(results
->cas
);
426 assert(rc
== MEMCACHED_SUCCESS
);
427 WATCHPOINT_ASSERT(memcached_result_cas(results
));
429 assert(!memcmp(value
, "we the people", strlen("we the people")));
430 assert(strlen("we the people") == value_length
);
431 assert(rc
== MEMCACHED_SUCCESS
);
433 memcached_result_free(&results_obj
);
438 static test_return
cas_test(memcached_st
*memc
)
441 const char *key
= "fun";
442 size_t key_length
= strlen(key
);
443 const char *value
= "we the people";
444 char* keys
[2] = { (char*)key
, NULL
};
445 size_t keylengths
[2] = { strlen(key
), 0 };
446 size_t value_length
= strlen(value
);
447 const char *value2
= "change the value";
448 size_t value2_length
= strlen(value2
);
450 memcached_result_st results_obj
;
451 memcached_result_st
*results
;
454 rc
= memcached_flush(memc
, 0);
455 assert(rc
== MEMCACHED_SUCCESS
);
457 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
459 rc
= memcached_set(memc
, key
, strlen(key
),
460 value
, strlen(value
),
461 (time_t)0, (uint32_t)0);
462 assert(rc
== MEMCACHED_SUCCESS
);
464 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
466 results
= memcached_result_create(memc
, &results_obj
);
468 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
470 assert(rc
== MEMCACHED_SUCCESS
);
471 WATCHPOINT_ASSERT(memcached_result_cas(results
));
472 assert(!memcmp(value
, memcached_result_value(results
), value_length
));
473 assert(strlen(memcached_result_value(results
)) == value_length
);
474 assert(rc
== MEMCACHED_SUCCESS
);
475 uint64_t cas
= memcached_result_cas(results
);
478 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
479 assert(rc
== MEMCACHED_END
);
480 assert(results
== NULL
);
483 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
484 assert(rc
== MEMCACHED_SUCCESS
);
487 * The item will have a new cas value, so try to set it again with the old
488 * value. This should fail!
490 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
491 assert(rc
== MEMCACHED_DATA_EXISTS
);
493 memcached_result_free(&results_obj
);
498 static test_return
prepend_test(memcached_st
*memc
)
502 char *value
= "people";
506 rc
= memcached_flush(memc
, 0);
507 assert(rc
== MEMCACHED_SUCCESS
);
509 rc
= memcached_set(memc
, key
, strlen(key
),
510 value
, strlen(value
),
511 (time_t)0, (uint32_t)0);
512 assert(rc
== MEMCACHED_SUCCESS
);
514 rc
= memcached_prepend(memc
, key
, strlen(key
),
515 "the ", strlen("the "),
516 (time_t)0, (uint32_t)0);
517 assert(rc
== MEMCACHED_SUCCESS
);
519 rc
= memcached_prepend(memc
, key
, strlen(key
),
520 "we ", strlen("we "),
521 (time_t)0, (uint32_t)0);
522 assert(rc
== MEMCACHED_SUCCESS
);
524 value
= memcached_get(memc
, key
, strlen(key
),
525 &value_length
, &flags
, &rc
);
526 assert(!memcmp(value
, "we the people", strlen("we the people")));
527 assert(strlen("we the people") == value_length
);
528 assert(rc
== MEMCACHED_SUCCESS
);
535 Set the value, then quit to make sure it is flushed.
536 Come back in and test that add fails.
538 static test_return
add_test(memcached_st
*memc
)
542 char *value
= "when we sanitize";
543 unsigned long long setting_value
;
545 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
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
);
551 memcached_quit(memc
);
552 rc
= memcached_add(memc
, key
, strlen(key
),
553 value
, strlen(value
),
554 (time_t)0, (uint32_t)0);
556 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
558 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
560 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
566 ** There was a problem of leaking filedescriptors in the initial release
567 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
568 ** systems it seems that the kernel is slow on reclaiming the resources
569 ** because the connects starts to time out (the test doesn't do much
570 ** anyway, so just loop 10 iterations)
572 static test_return
add_wrapper(memcached_st
*memc
)
575 unsigned int max
= 10000;
580 for (x
= 0; x
< max
; x
++)
586 static test_return
replace_test(memcached_st
*memc
)
590 char *value
= "when we sanitize";
591 char *original
= "first we insert some data";
593 rc
= memcached_set(memc
, key
, strlen(key
),
594 original
, strlen(original
),
595 (time_t)0, (uint32_t)0);
596 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
598 rc
= memcached_replace(memc
, key
, strlen(key
),
599 value
, strlen(value
),
600 (time_t)0, (uint32_t)0);
601 assert(rc
== MEMCACHED_SUCCESS
);
606 static test_return
delete_test(memcached_st
*memc
)
610 char *value
= "when we sanitize";
612 rc
= memcached_set(memc
, key
, strlen(key
),
613 value
, strlen(value
),
614 (time_t)0, (uint32_t)0);
615 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
617 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
618 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
623 static test_return
flush_test(memcached_st
*memc
)
627 rc
= memcached_flush(memc
, 0);
628 assert(rc
== MEMCACHED_SUCCESS
);
633 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
634 memcached_server_st
*server
__attribute__((unused
)),
635 void *context
__attribute__((unused
)))
639 return MEMCACHED_SUCCESS
;
642 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
644 char *context
= "foo bad";
645 memcached_server_function callbacks
[1];
647 callbacks
[0]= server_function
;
648 memcached_server_cursor(memc
, callbacks
, context
, 1);
653 static test_return
bad_key_test(memcached_st
*memc
)
656 char *key
= "foo bad";
658 size_t string_length
;
660 memcached_st
*memc_clone
;
662 size_t max_keylen
= 0xffff;
664 memc_clone
= memcached_clone(NULL
, memc
);
667 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
668 assert(rc
== MEMCACHED_SUCCESS
);
670 /* All keys are valid in the binary protocol (except for length) */
671 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
673 string
= memcached_get(memc_clone
, key
, strlen(key
),
674 &string_length
, &flags
, &rc
);
675 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
676 assert(string_length
== 0);
680 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
681 assert(rc
== MEMCACHED_SUCCESS
);
682 string
= memcached_get(memc_clone
, key
, strlen(key
),
683 &string_length
, &flags
, &rc
);
684 assert(rc
== MEMCACHED_NOTFOUND
);
685 assert(string_length
== 0);
688 /* Test multi key for bad keys */
689 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
690 size_t key_lengths
[] = { 7, 7, 7 };
692 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
693 assert(rc
== MEMCACHED_SUCCESS
);
695 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
696 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
698 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
699 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
703 /* The following test should be moved to the end of this function when the
704 memcached server is updated to allow max size length of the keys in the
707 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
708 assert(rc
== MEMCACHED_SUCCESS
);
710 char *longkey
= malloc(max_keylen
+ 1);
713 memset(longkey
, 'a', max_keylen
+ 1);
714 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
715 &string_length
, &flags
, &rc
);
716 assert(rc
== MEMCACHED_NOTFOUND
);
717 assert(string_length
== 0);
720 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
721 &string_length
, &flags
, &rc
);
722 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
723 assert(string_length
== 0);
730 /* Make sure zero length keys are marked as bad */
732 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
733 assert(rc
== MEMCACHED_SUCCESS
);
734 string
= memcached_get(memc_clone
, key
, 0,
735 &string_length
, &flags
, &rc
);
736 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
737 assert(string_length
== 0);
740 memcached_free(memc_clone
);
745 #define READ_THROUGH_VALUE "set for me"
746 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
747 char *key
__attribute__((unused
)),
748 size_t key_length
__attribute__((unused
)),
749 memcached_result_st
*result
)
752 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
755 static test_return
read_through(memcached_st
*memc
)
760 size_t string_length
;
762 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
764 string
= memcached_get(memc
, key
, strlen(key
),
765 &string_length
, &flags
, &rc
);
767 assert(rc
== MEMCACHED_NOTFOUND
);
768 assert(string_length
== 0);
771 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
773 assert(rc
== MEMCACHED_SUCCESS
);
775 string
= memcached_get(memc
, key
, strlen(key
),
776 &string_length
, &flags
, &rc
);
778 assert(rc
== MEMCACHED_SUCCESS
);
779 assert(string_length
== strlen(READ_THROUGH_VALUE
));
780 assert(!strcmp(READ_THROUGH_VALUE
, string
));
783 string
= memcached_get(memc
, key
, strlen(key
),
784 &string_length
, &flags
, &rc
);
786 assert(rc
== MEMCACHED_SUCCESS
);
787 assert(string_length
== strlen(READ_THROUGH_VALUE
));
788 assert(!strcmp(READ_THROUGH_VALUE
, string
));
794 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
796 size_t key_length
__attribute__((unused
)))
800 return MEMCACHED_SUCCESS
;
803 static test_return
delete_through(memcached_st
*memc
)
805 memcached_trigger_delete_key callback
;
808 callback
= (memcached_trigger_delete_key
)delete_trigger
;
810 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
811 assert(rc
== MEMCACHED_SUCCESS
);
816 static test_return
get_test(memcached_st
*memc
)
821 size_t string_length
;
824 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
825 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
827 string
= memcached_get(memc
, key
, strlen(key
),
828 &string_length
, &flags
, &rc
);
830 assert(rc
== MEMCACHED_NOTFOUND
);
831 assert(string_length
== 0);
837 static test_return
get_test2(memcached_st
*memc
)
841 char *value
= "when we sanitize";
843 size_t string_length
;
846 rc
= memcached_set(memc
, key
, strlen(key
),
847 value
, strlen(value
),
848 (time_t)0, (uint32_t)0);
849 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
851 string
= memcached_get(memc
, key
, strlen(key
),
852 &string_length
, &flags
, &rc
);
855 assert(rc
== MEMCACHED_SUCCESS
);
856 assert(string_length
== strlen(value
));
857 assert(!memcmp(string
, value
, string_length
));
864 static test_return
set_test2(memcached_st
*memc
)
868 char *value
= "train in the brain";
869 size_t value_length
= strlen(value
);
872 for (x
= 0; x
< 10; x
++)
874 rc
= memcached_set(memc
, key
, strlen(key
),
876 (time_t)0, (uint32_t)0);
877 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
883 static test_return
set_test3(memcached_st
*memc
)
887 size_t value_length
= 8191;
890 value
= (char*)malloc(value_length
);
893 for (x
= 0; x
< value_length
; x
++)
894 value
[x
] = (char) (x
% 127);
896 /* The dump test relies on there being at least 32 items in memcached */
897 for (x
= 0; x
< 32; x
++)
901 sprintf(key
, "foo%u", x
);
903 rc
= memcached_set(memc
, key
, strlen(key
),
905 (time_t)0, (uint32_t)0);
906 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
914 static test_return
get_test3(memcached_st
*memc
)
919 size_t value_length
= 8191;
921 size_t string_length
;
925 value
= (char*)malloc(value_length
);
928 for (x
= 0; x
< value_length
; x
++)
929 value
[x
] = (char) (x
% 127);
931 rc
= memcached_set(memc
, key
, strlen(key
),
933 (time_t)0, (uint32_t)0);
934 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
936 string
= memcached_get(memc
, key
, strlen(key
),
937 &string_length
, &flags
, &rc
);
939 assert(rc
== MEMCACHED_SUCCESS
);
941 assert(string_length
== value_length
);
942 assert(!memcmp(string
, value
, string_length
));
950 static test_return
get_test4(memcached_st
*memc
)
955 size_t value_length
= 8191;
957 size_t string_length
;
961 value
= (char*)malloc(value_length
);
964 for (x
= 0; x
< value_length
; x
++)
965 value
[x
] = (char) (x
% 127);
967 rc
= memcached_set(memc
, key
, strlen(key
),
969 (time_t)0, (uint32_t)0);
970 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
972 for (x
= 0; x
< 10; x
++)
974 string
= memcached_get(memc
, key
, strlen(key
),
975 &string_length
, &flags
, &rc
);
977 assert(rc
== MEMCACHED_SUCCESS
);
979 assert(string_length
== value_length
);
980 assert(!memcmp(string
, value
, string_length
));
990 * This test verifies that memcached_read_one_response doesn't try to
991 * dereference a NIL-pointer if you issue a multi-get and don't read out all
992 * responses before you execute a storage command.
994 static test_return
get_test5(memcached_st
*memc
)
997 ** Request the same key twice, to ensure that we hash to the same server
998 ** (so that we have multiple response values queued up) ;-)
1000 char *keys
[]= { "key", "key" };
1001 size_t lengths
[]= { 3, 3 };
1005 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1006 keys
[0], lengths
[0], 0, 0);
1007 assert(rc
== MEMCACHED_SUCCESS
);
1008 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1010 memcached_result_st results_obj
;
1011 memcached_result_st
*results
;
1012 results
=memcached_result_create(memc
, &results_obj
);
1014 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1016 memcached_result_free(&results_obj
);
1018 /* Don't read out the second result, but issue a set instead.. */
1019 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1020 assert(rc
== MEMCACHED_SUCCESS
);
1022 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1023 &rlen
, &flags
, &rc
);
1024 assert(val
== NULL
);
1025 assert(rc
== MEMCACHED_NOTFOUND
);
1026 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1027 assert(val
!= NULL
);
1028 assert(rc
== MEMCACHED_SUCCESS
);
1031 return TEST_SUCCESS
;
1034 /* Do not copy the style of this code, I just access hosts to testthis function */
1035 static test_return
stats_servername_test(memcached_st
*memc
)
1037 memcached_return rc
;
1038 memcached_stat_st memc_stat
;
1039 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1040 memc
->hosts
[0].hostname
,
1041 memc
->hosts
[0].port
);
1046 static test_return
increment_test(memcached_st
*memc
)
1048 uint64_t new_number
;
1049 memcached_return rc
;
1050 char *key
= "number";
1053 rc
= memcached_set(memc
, key
, strlen(key
),
1054 value
, strlen(value
),
1055 (time_t)0, (uint32_t)0);
1056 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1058 rc
= memcached_increment(memc
, key
, strlen(key
),
1060 assert(rc
== MEMCACHED_SUCCESS
);
1061 assert(new_number
== 1);
1063 rc
= memcached_increment(memc
, key
, strlen(key
),
1065 assert(rc
== MEMCACHED_SUCCESS
);
1066 assert(new_number
== 2);
1071 static test_return
increment_with_initial_test(memcached_st
*memc
)
1073 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1075 uint64_t new_number
;
1076 memcached_return rc
;
1077 char *key
= "number";
1078 uint64_t initial
= 0;
1080 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1081 1, initial
, 0, &new_number
);
1082 assert(rc
== MEMCACHED_SUCCESS
);
1083 assert(new_number
== initial
);
1085 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1086 1, initial
, 0, &new_number
);
1087 assert(rc
== MEMCACHED_SUCCESS
);
1088 assert(new_number
== (initial
+ 1));
1093 static test_return
decrement_test(memcached_st
*memc
)
1095 uint64_t new_number
;
1096 memcached_return rc
;
1097 char *key
= "number";
1100 rc
= memcached_set(memc
, key
, strlen(key
),
1101 value
, strlen(value
),
1102 (time_t)0, (uint32_t)0);
1103 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1105 rc
= memcached_decrement(memc
, key
, strlen(key
),
1107 assert(rc
== MEMCACHED_SUCCESS
);
1108 assert(new_number
== 2);
1110 rc
= memcached_decrement(memc
, key
, strlen(key
),
1112 assert(rc
== MEMCACHED_SUCCESS
);
1113 assert(new_number
== 1);
1118 static test_return
decrement_with_initial_test(memcached_st
*memc
)
1120 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1122 uint64_t new_number
;
1123 memcached_return rc
;
1124 char *key
= "number";
1125 uint64_t initial
= 3;
1127 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1128 1, initial
, 0, &new_number
);
1129 assert(rc
== MEMCACHED_SUCCESS
);
1130 assert(new_number
== initial
);
1132 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1133 1, initial
, 0, &new_number
);
1134 assert(rc
== MEMCACHED_SUCCESS
);
1135 assert(new_number
== (initial
- 1));
1140 static test_return
quit_test(memcached_st
*memc
)
1142 memcached_return rc
;
1144 char *value
= "sanford and sun";
1146 rc
= memcached_set(memc
, key
, strlen(key
),
1147 value
, strlen(value
),
1148 (time_t)10, (uint32_t)3);
1149 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1150 memcached_quit(memc
);
1152 rc
= memcached_set(memc
, key
, strlen(key
),
1153 value
, strlen(value
),
1154 (time_t)50, (uint32_t)9);
1155 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1160 static test_return
mget_result_test(memcached_st
*memc
)
1162 memcached_return rc
;
1163 char *keys
[]= {"fudge", "son", "food"};
1164 size_t key_length
[]= {5, 3, 4};
1167 memcached_result_st results_obj
;
1168 memcached_result_st
*results
;
1170 results
= memcached_result_create(memc
, &results_obj
);
1172 assert(&results_obj
== results
);
1174 /* We need to empty the server before continueing test */
1175 rc
= memcached_flush(memc
, 0);
1176 assert(rc
== MEMCACHED_SUCCESS
);
1178 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1179 assert(rc
== MEMCACHED_SUCCESS
);
1181 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1186 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1188 assert(rc
== MEMCACHED_END
);
1190 for (x
= 0; x
< 3; x
++)
1192 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1193 keys
[x
], key_length
[x
],
1194 (time_t)50, (uint32_t)9);
1195 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1198 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1199 assert(rc
== MEMCACHED_SUCCESS
);
1201 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1204 assert(&results_obj
== results
);
1205 assert(rc
== MEMCACHED_SUCCESS
);
1206 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1207 assert(!memcmp(memcached_result_key_value(results
),
1208 memcached_result_value(results
),
1209 memcached_result_length(results
)));
1212 memcached_result_free(&results_obj
);
1217 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1219 memcached_return rc
;
1220 char *keys
[]= {"fudge", "son", "food"};
1221 size_t key_length
[]= {5, 3, 4};
1224 memcached_result_st
*results
;
1226 /* We need to empty the server before continueing test */
1227 rc
= memcached_flush(memc
, 0);
1228 assert(rc
== MEMCACHED_SUCCESS
);
1230 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1231 assert(rc
== MEMCACHED_SUCCESS
);
1233 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1238 assert(rc
== MEMCACHED_END
);
1240 for (x
= 0; x
< 3; x
++)
1242 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1243 keys
[x
], key_length
[x
],
1244 (time_t)50, (uint32_t)9);
1245 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1248 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1249 assert(rc
== MEMCACHED_SUCCESS
);
1252 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1255 assert(rc
== MEMCACHED_SUCCESS
);
1256 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1257 assert(!memcmp(memcached_result_key_value(results
),
1258 memcached_result_value(results
),
1259 memcached_result_length(results
)));
1260 memcached_result_free(results
);
1267 /* Count the results */
1268 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1269 memcached_result_st
*result
__attribute__((unused
)),
1272 unsigned int *counter
= (unsigned int *)context
;
1274 *counter
= *counter
+ 1;
1276 return MEMCACHED_SUCCESS
;
1279 static test_return
mget_result_function(memcached_st
*memc
)
1281 memcached_return rc
;
1282 char *keys
[]= {"fudge", "son", "food"};
1283 size_t key_length
[]= {5, 3, 4};
1285 unsigned int counter
;
1286 memcached_execute_function callbacks
[1];
1288 /* We need to empty the server before continueing test */
1289 rc
= memcached_flush(memc
, 0);
1290 for (x
= 0; x
< 3; x
++)
1292 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1293 keys
[x
], key_length
[x
],
1294 (time_t)50, (uint32_t)9);
1295 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1298 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1299 assert(rc
== MEMCACHED_SUCCESS
);
1301 callbacks
[0]= &callback_counter
;
1303 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1305 assert(counter
== 3);
1310 static test_return
mget_test(memcached_st
*memc
)
1312 memcached_return rc
;
1313 char *keys
[]= {"fudge", "son", "food"};
1314 size_t key_length
[]= {5, 3, 4};
1318 char return_key
[MEMCACHED_MAX_KEY
];
1319 size_t return_key_length
;
1321 size_t return_value_length
;
1323 /* We need to empty the server before continueing test */
1324 rc
= memcached_flush(memc
, 0);
1325 assert(rc
== MEMCACHED_SUCCESS
);
1327 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1328 assert(rc
== MEMCACHED_SUCCESS
);
1330 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1331 &return_value_length
, &flags
, &rc
)) != NULL
)
1333 assert(return_value
);
1335 assert(!return_value
);
1336 assert(return_value_length
== 0);
1337 assert(rc
== MEMCACHED_END
);
1339 for (x
= 0; x
< 3; x
++)
1341 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1342 keys
[x
], key_length
[x
],
1343 (time_t)50, (uint32_t)9);
1344 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1347 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1348 assert(rc
== MEMCACHED_SUCCESS
);
1351 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1352 &return_value_length
, &flags
, &rc
)))
1354 assert(return_value
);
1355 assert(rc
== MEMCACHED_SUCCESS
);
1356 assert(return_key_length
== return_value_length
);
1357 assert(!memcmp(return_value
, return_key
, return_value_length
));
1365 static test_return
get_stats_keys(memcached_st
*memc
)
1369 memcached_stat_st memc_stat
;
1370 memcached_return rc
;
1372 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1373 assert(rc
== MEMCACHED_SUCCESS
);
1374 for (ptr
= list
; *ptr
; ptr
++)
1383 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1385 const char *version_string
;
1387 version_string
= memcached_lib_version();
1389 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1394 static test_return
get_stats(memcached_st
*memc
)
1399 memcached_return rc
;
1400 memcached_stat_st
*memc_stat
;
1402 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1403 assert(rc
== MEMCACHED_SUCCESS
);
1405 assert(rc
== MEMCACHED_SUCCESS
);
1408 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1410 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1411 assert(rc
== MEMCACHED_SUCCESS
);
1412 for (ptr
= list
; *ptr
; ptr
++);
1417 memcached_stat_free(NULL
, memc_stat
);
1422 static test_return
add_host_test(memcached_st
*memc
)
1425 memcached_server_st
*servers
;
1426 memcached_return rc
;
1427 char servername
[]= "0.example.com";
1429 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1431 assert(1 == memcached_server_list_count(servers
));
1433 for (x
= 2; x
< 20; x
++)
1435 char buffer
[SMALL_STRING_LEN
];
1437 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1438 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1440 assert(rc
== MEMCACHED_SUCCESS
);
1441 assert(x
== memcached_server_list_count(servers
));
1444 rc
= memcached_server_push(memc
, servers
);
1445 assert(rc
== MEMCACHED_SUCCESS
);
1446 rc
= memcached_server_push(memc
, servers
);
1447 assert(rc
== MEMCACHED_SUCCESS
);
1449 memcached_server_list_free(servers
);
1454 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1456 return MEMCACHED_SUCCESS
;
1459 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1461 return MEMCACHED_SUCCESS
;
1464 static test_return
callback_test(memcached_st
*memc
)
1466 /* Test User Data */
1470 memcached_return rc
;
1472 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1473 assert(rc
== MEMCACHED_SUCCESS
);
1474 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1475 assert(*test_ptr
== x
);
1478 /* Test Clone Callback */
1480 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1481 void *clone_cb_ptr
= *(void **)&clone_cb
;
1482 void *temp_function
= NULL
;
1483 memcached_return rc
;
1485 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1487 assert(rc
== MEMCACHED_SUCCESS
);
1488 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1489 assert(temp_function
== clone_cb_ptr
);
1492 /* Test Cleanup Callback */
1494 memcached_cleanup_func cleanup_cb
=
1495 (memcached_cleanup_func
)cleanup_test_callback
;
1496 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1497 void *temp_function
= NULL
;
1498 memcached_return rc
;
1500 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1502 assert(rc
== MEMCACHED_SUCCESS
);
1503 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1504 assert(temp_function
== cleanup_cb_ptr
);
1510 /* We don't test the behavior itself, we test the switches */
1511 static test_return
behavior_test(memcached_st
*memc
)
1516 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1517 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1520 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1521 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1524 set
= MEMCACHED_HASH_MD5
;
1525 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1526 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1527 assert(value
== MEMCACHED_HASH_MD5
);
1531 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1532 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1535 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1536 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1539 set
= MEMCACHED_HASH_DEFAULT
;
1540 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1541 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1542 assert(value
== MEMCACHED_HASH_DEFAULT
);
1544 set
= MEMCACHED_HASH_CRC
;
1545 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1546 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1547 assert(value
== MEMCACHED_HASH_CRC
);
1549 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1552 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1555 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1556 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1557 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1561 /* Test case provided by Cal Haldenbrand */
1562 static test_return
user_supplied_bug1(memcached_st
*memc
)
1564 unsigned int setter
= 1;
1567 unsigned long long total
= 0;
1570 char randomstuff
[6 * 1024];
1571 memcached_return rc
;
1573 memset(randomstuff
, 0, 6 * 1024);
1575 /* We just keep looking at the same values over and over */
1578 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1579 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1583 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1587 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1588 memset(randomstuff
, 0, 6 * 1024);
1589 assert(size
< 6 * 1024); /* Being safe here */
1591 for (j
= 0 ; j
< size
;j
++)
1592 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1595 sprintf(key
, "%d", x
);
1596 rc
= memcached_set(memc
, key
, strlen(key
),
1597 randomstuff
, strlen(randomstuff
), 10, 0);
1598 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1599 /* If we fail, lets try again */
1600 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1601 rc
= memcached_set(memc
, key
, strlen(key
),
1602 randomstuff
, strlen(randomstuff
), 10, 0);
1603 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1609 /* Test case provided by Cal Haldenbrand */
1610 static test_return
user_supplied_bug2(memcached_st
*memc
)
1613 unsigned int setter
;
1615 unsigned long long total
;
1618 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1619 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1621 setter
= 20 * 1024576;
1622 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1623 setter
= 20 * 1024576;
1624 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1625 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1626 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1628 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1631 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1633 memcached_return rc
= MEMCACHED_SUCCESS
;
1634 char buffer
[SMALL_STRING_LEN
];
1639 memset(buffer
, 0, SMALL_STRING_LEN
);
1641 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1642 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1643 &val_len
, &flags
, &rc
);
1644 if (rc
!= MEMCACHED_SUCCESS
)
1646 if (rc
== MEMCACHED_NOTFOUND
)
1650 WATCHPOINT_ERROR(rc
);
1664 /* Do a large mget() over all the keys we think exist */
1665 #define KEY_COUNT 3000 // * 1024576
1666 static test_return
user_supplied_bug3(memcached_st
*memc
)
1668 memcached_return rc
;
1669 unsigned int setter
;
1672 size_t key_lengths
[KEY_COUNT
];
1675 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1676 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1678 setter
= 20 * 1024576;
1679 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1680 setter
= 20 * 1024576;
1681 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1682 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1683 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1686 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1688 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1689 for (x
= 0; x
< KEY_COUNT
; x
++)
1693 snprintf(buffer
, 30, "%u", x
);
1694 keys
[x
]= strdup(buffer
);
1695 key_lengths
[x
]= strlen(keys
[x
]);
1698 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1699 assert(rc
== MEMCACHED_SUCCESS
);
1701 /* Turn this into a help function */
1703 char return_key
[MEMCACHED_MAX_KEY
];
1704 size_t return_key_length
;
1706 size_t return_value_length
;
1709 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1710 &return_value_length
, &flags
, &rc
)))
1712 assert(return_value
);
1713 assert(rc
== MEMCACHED_SUCCESS
);
1718 for (x
= 0; x
< KEY_COUNT
; x
++)
1725 /* Make sure we behave properly if server list has no values */
1726 static test_return
user_supplied_bug4(memcached_st
*memc
)
1728 memcached_return rc
;
1729 char *keys
[]= {"fudge", "son", "food"};
1730 size_t key_length
[]= {5, 3, 4};
1733 char return_key
[MEMCACHED_MAX_KEY
];
1734 size_t return_key_length
;
1736 size_t return_value_length
;
1738 /* Here we free everything before running a bunch of mget tests */
1740 memcached_server_list_free(memc
->hosts
);
1742 memc
->number_of_hosts
= 0;
1746 /* We need to empty the server before continueing test */
1747 rc
= memcached_flush(memc
, 0);
1748 assert(rc
== MEMCACHED_NO_SERVERS
);
1750 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1751 assert(rc
== MEMCACHED_NO_SERVERS
);
1753 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1754 &return_value_length
, &flags
, &rc
)) != NULL
)
1756 assert(return_value
);
1758 assert(!return_value
);
1759 assert(return_value_length
== 0);
1760 assert(rc
== MEMCACHED_NO_SERVERS
);
1762 for (x
= 0; x
< 3; x
++)
1764 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1765 keys
[x
], key_length
[x
],
1766 (time_t)50, (uint32_t)9);
1767 assert(rc
== MEMCACHED_NO_SERVERS
);
1770 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1771 assert(rc
== MEMCACHED_NO_SERVERS
);
1774 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1775 &return_value_length
, &flags
, &rc
)))
1777 assert(return_value
);
1778 assert(rc
== MEMCACHED_SUCCESS
);
1779 assert(return_key_length
== return_value_length
);
1780 assert(!memcmp(return_value
, return_key
, return_value_length
));
1788 #define VALUE_SIZE_BUG5 1048064
1789 static test_return
user_supplied_bug5(memcached_st
*memc
)
1791 memcached_return rc
;
1792 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1793 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1794 char return_key
[MEMCACHED_MAX_KEY
];
1795 size_t return_key_length
;
1797 size_t value_length
;
1801 char insert_data
[VALUE_SIZE_BUG5
];
1803 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1804 insert_data
[x
]= (signed char)rand();
1806 memcached_flush(memc
, 0);
1807 value
= memcached_get(memc
, keys
[0], key_length
[0],
1808 &value_length
, &flags
, &rc
);
1809 assert(value
== NULL
);
1810 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1813 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1814 &value_length
, &flags
, &rc
)))
1818 for (x
= 0; x
< 4; x
++)
1820 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1821 insert_data
, VALUE_SIZE_BUG5
,
1822 (time_t)0, (uint32_t)0);
1823 assert(rc
== MEMCACHED_SUCCESS
);
1826 for (x
= 0; x
< 10; x
++)
1828 value
= memcached_get(memc
, keys
[0], key_length
[0],
1829 &value_length
, &flags
, &rc
);
1833 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1835 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1836 &value_length
, &flags
, &rc
)))
1847 static test_return
user_supplied_bug6(memcached_st
*memc
)
1849 memcached_return rc
;
1850 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1851 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1852 char return_key
[MEMCACHED_MAX_KEY
];
1853 size_t return_key_length
;
1855 size_t value_length
;
1859 char insert_data
[VALUE_SIZE_BUG5
];
1861 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1862 insert_data
[x
]= (signed char)rand();
1864 memcached_flush(memc
, 0);
1865 value
= memcached_get(memc
, keys
[0], key_length
[0],
1866 &value_length
, &flags
, &rc
);
1867 assert(value
== NULL
);
1868 assert(rc
== MEMCACHED_NOTFOUND
);
1869 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1870 assert(rc
== MEMCACHED_SUCCESS
);
1873 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1874 &value_length
, &flags
, &rc
)))
1877 assert(rc
== MEMCACHED_END
);
1879 for (x
= 0; x
< 4; x
++)
1881 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1882 insert_data
, VALUE_SIZE_BUG5
,
1883 (time_t)0, (uint32_t)0);
1884 assert(rc
== MEMCACHED_SUCCESS
);
1887 for (x
= 0; x
< 2; x
++)
1889 value
= memcached_get(memc
, keys
[0], key_length
[0],
1890 &value_length
, &flags
, &rc
);
1894 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1895 assert(rc
== MEMCACHED_SUCCESS
);
1897 /* We test for purge of partial complete fetches */
1898 for (count
= 3; count
; count
--)
1900 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1901 &value_length
, &flags
, &rc
);
1902 assert(rc
== MEMCACHED_SUCCESS
);
1903 assert(!(memcmp(value
, insert_data
, value_length
)));
1904 assert(value_length
);
1912 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1914 memcached_return rc
;
1916 memcached_st
*memc_clone
;
1918 memcached_server_st
*servers
;
1919 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";
1921 servers
= memcached_servers_parse(server_list
);
1924 mine
= memcached_create(NULL
);
1925 rc
= memcached_server_push(mine
, servers
);
1926 assert(rc
== MEMCACHED_SUCCESS
);
1927 memcached_server_list_free(servers
);
1930 memc_clone
= memcached_clone(NULL
, mine
);
1932 memcached_quit(mine
);
1933 memcached_quit(memc_clone
);
1936 memcached_free(mine
);
1937 memcached_free(memc_clone
);
1942 /* Test flag store/retrieve */
1943 static test_return
user_supplied_bug7(memcached_st
*memc
)
1945 memcached_return rc
;
1946 char *keys
= "036790384900";
1947 size_t key_length
= strlen("036790384900");
1948 char return_key
[MEMCACHED_MAX_KEY
];
1949 size_t return_key_length
;
1951 size_t value_length
;
1954 char insert_data
[VALUE_SIZE_BUG5
];
1956 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1957 insert_data
[x
]= (signed char)rand();
1959 memcached_flush(memc
, 0);
1962 rc
= memcached_set(memc
, keys
, key_length
,
1963 insert_data
, VALUE_SIZE_BUG5
,
1965 assert(rc
== MEMCACHED_SUCCESS
);
1968 value
= memcached_get(memc
, keys
, key_length
,
1969 &value_length
, &flags
, &rc
);
1970 assert(flags
== 245);
1974 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1977 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1978 &value_length
, &flags
, &rc
);
1979 assert(flags
== 245);
1987 static test_return
user_supplied_bug9(memcached_st
*memc
)
1989 memcached_return rc
;
1990 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1991 size_t key_length
[3];
1996 char return_key
[MEMCACHED_MAX_KEY
];
1997 size_t return_key_length
;
1999 size_t return_value_length
;
2002 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2003 key_length
[1]= strlen("fudge&*@#");
2004 key_length
[2]= strlen("for^#@&$not");
2007 for (x
= 0; x
< 3; x
++)
2009 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2010 keys
[x
], key_length
[x
],
2011 (time_t)50, (uint32_t)9);
2012 assert(rc
== MEMCACHED_SUCCESS
);
2015 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2016 assert(rc
== MEMCACHED_SUCCESS
);
2018 /* We need to empty the server before continueing test */
2019 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2020 &return_value_length
, &flags
, &rc
)) != NULL
)
2022 assert(return_value
);
2031 /* We are testing with aggressive timeout to get failures */
2032 static test_return
user_supplied_bug10(memcached_st
*memc
)
2036 size_t value_length
= 512;
2039 memcached_return rc
;
2040 unsigned int set
= 1;
2041 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2044 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2045 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2047 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2050 value
= (char*)malloc(value_length
* sizeof(char));
2052 for (x
= 0; x
< value_length
; x
++)
2053 value
[x
]= (char) (x
% 127);
2055 for (x
= 1; x
<= 100000; ++x
)
2057 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2059 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2060 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2062 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2067 memcached_free(mclone
);
2073 We are looking failures in the async protocol
2075 static test_return
user_supplied_bug11(memcached_st
*memc
)
2079 size_t value_length
= 512;
2082 memcached_return rc
;
2083 unsigned int set
= 1;
2085 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2087 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2088 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2090 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2093 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2095 assert(timeout
== -1);
2097 value
= (char*)malloc(value_length
* sizeof(char));
2099 for (x
= 0; x
< value_length
; x
++)
2100 value
[x
]= (char) (x
% 127);
2102 for (x
= 1; x
<= 100000; ++x
)
2104 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2108 memcached_free(mclone
);
2114 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2116 static test_return
user_supplied_bug12(memcached_st
*memc
)
2118 memcached_return rc
;
2120 size_t value_length
;
2122 uint64_t number_value
;
2124 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2125 &value_length
, &flags
, &rc
);
2126 assert(value
== NULL
);
2127 assert(rc
== MEMCACHED_NOTFOUND
);
2129 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2132 assert(value
== NULL
);
2133 /* The binary protocol will set the key if it doesn't exist */
2134 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2135 assert(rc
== MEMCACHED_SUCCESS
);
2137 assert(rc
== MEMCACHED_NOTFOUND
);
2139 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2141 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2142 &value_length
, &flags
, &rc
);
2144 assert(rc
== MEMCACHED_SUCCESS
);
2147 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2149 assert(number_value
== 2);
2150 assert(rc
== MEMCACHED_SUCCESS
);
2156 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2157 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2159 static test_return
user_supplied_bug13(memcached_st
*memc
)
2161 char key
[] = "key34567890";
2163 memcached_return rc
;
2164 size_t overflowSize
;
2166 char commandFirst
[]= "set key34567890 0 0 ";
2167 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2168 size_t commandLength
;
2171 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2173 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2175 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2177 overflow
= malloc(testSize
);
2178 assert(overflow
!= NULL
);
2180 memset(overflow
, 'x', testSize
);
2181 rc
= memcached_set(memc
, key
, strlen(key
),
2182 overflow
, testSize
, 0, 0);
2183 assert(rc
== MEMCACHED_SUCCESS
);
2192 Test values of many different sizes
2193 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2194 set key34567890 0 0 8169 \r\n
2195 is sent followed by buffer of size 8169, followed by 8169
2197 static test_return
user_supplied_bug14(memcached_st
*memc
)
2200 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2201 memcached_return rc
;
2204 size_t value_length
= 18000;
2206 size_t string_length
;
2209 size_t current_length
;
2211 value
= (char*)malloc(value_length
);
2214 for (x
= 0; x
< value_length
; x
++)
2215 value
[x
] = (char) (x
% 127);
2217 for (current_length
= 0; current_length
< value_length
; current_length
++)
2219 rc
= memcached_set(memc
, key
, strlen(key
),
2220 value
, current_length
,
2221 (time_t)0, (uint32_t)0);
2222 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2224 string
= memcached_get(memc
, key
, strlen(key
),
2225 &string_length
, &flags
, &rc
);
2227 assert(rc
== MEMCACHED_SUCCESS
);
2228 assert(string_length
== current_length
);
2229 assert(!memcmp(string
, value
, string_length
));
2240 Look for zero length value problems
2242 static test_return
user_supplied_bug15(memcached_st
*memc
)
2245 memcached_return rc
;
2251 for (x
= 0; x
< 2; x
++)
2253 rc
= memcached_set(memc
, key
, strlen(key
),
2255 (time_t)0, (uint32_t)0);
2257 assert(rc
== MEMCACHED_SUCCESS
);
2259 value
= memcached_get(memc
, key
, strlen(key
),
2260 &length
, &flags
, &rc
);
2262 assert(rc
== MEMCACHED_SUCCESS
);
2263 assert(value
== NULL
);
2264 assert(length
== 0);
2267 value
= memcached_get(memc
, key
, strlen(key
),
2268 &length
, &flags
, &rc
);
2270 assert(rc
== MEMCACHED_SUCCESS
);
2271 assert(value
== NULL
);
2272 assert(length
== 0);
2279 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2280 static test_return
user_supplied_bug16(memcached_st
*memc
)
2282 memcached_return rc
;
2288 rc
= memcached_set(memc
, key
, strlen(key
),
2290 (time_t)0, UINT32_MAX
);
2292 assert(rc
== MEMCACHED_SUCCESS
);
2294 value
= memcached_get(memc
, key
, strlen(key
),
2295 &length
, &flags
, &rc
);
2297 assert(rc
== MEMCACHED_SUCCESS
);
2298 assert(value
== NULL
);
2299 assert(length
== 0);
2300 assert(flags
== UINT32_MAX
);
2305 /* Check the validity of chinese key*/
2306 static test_return
user_supplied_bug17(memcached_st
*memc
)
2308 memcached_return rc
;
2310 char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2315 rc
= memcached_set(memc
, key
, strlen(key
),
2316 value
, strlen(value
),
2319 assert(rc
== MEMCACHED_SUCCESS
);
2321 value2
= memcached_get(memc
, key
, strlen(key
),
2322 &length
, &flags
, &rc
);
2324 assert(length
==strlen(value
));
2325 assert(rc
== MEMCACHED_SUCCESS
);
2326 assert(memcmp(value
, value2
, length
)==0);
2336 static test_return
user_supplied_bug19(memcached_st
*memc
)
2339 memcached_server_st
*s
;
2340 memcached_return res
;
2344 m
= memcached_create(NULL
);
2345 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2346 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2348 s
= memcached_server_by_key(m
, "a", 1, &res
);
2349 memcached_server_free(s
);
2356 /* CAS test from Andei */
2357 static test_return
user_supplied_bug20(memcached_st
*memc
)
2359 memcached_return status
;
2360 memcached_result_st
*result
, result_obj
;
2362 size_t key_len
= strlen("abc");
2363 char *value
= "foobar";
2364 size_t value_len
= strlen(value
);
2366 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2368 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2369 assert(status
== MEMCACHED_SUCCESS
);
2371 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2372 assert(status
== MEMCACHED_SUCCESS
);
2374 result
= memcached_result_create(memc
, &result_obj
);
2377 memcached_result_create(memc
, &result_obj
);
2378 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2381 assert(status
== MEMCACHED_SUCCESS
);
2383 memcached_result_free(result
);
2388 #include "ketama_test_cases.h"
2389 static test_return
user_supplied_bug18(memcached_st
*trash
)
2391 memcached_return rc
;
2394 memcached_server_st
*server_pool
;
2399 memc
= memcached_create(NULL
);
2402 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2403 assert(rc
== MEMCACHED_SUCCESS
);
2405 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2408 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2409 assert(rc
== MEMCACHED_SUCCESS
);
2411 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2412 assert(value
== MEMCACHED_HASH_MD5
);
2414 server_pool
= memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
2415 memcached_server_push(memc
, server_pool
);
2417 /* verify that the server list was parsed okay. */
2418 assert(memc
->number_of_hosts
== 8);
2419 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2420 assert(server_pool
[0].port
== 11211);
2421 assert(server_pool
[0].weight
== 600);
2422 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2423 assert(server_pool
[2].port
== 11211);
2424 assert(server_pool
[2].weight
== 200);
2425 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2426 assert(server_pool
[7].port
== 11211);
2427 assert(server_pool
[7].weight
== 100);
2429 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2430 * us test the boundary wraparound.
2432 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2434 /* verify the standard ketama set. */
2435 for (x
= 0; x
< 99; x
++)
2437 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2438 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2439 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2442 memcached_server_list_free(server_pool
);
2443 memcached_free(memc
);
2448 static test_return
auto_eject_hosts(memcached_st
*trash
)
2452 memcached_return rc
;
2453 memcached_st
*memc
= memcached_create(NULL
);
2456 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2457 assert(rc
== MEMCACHED_SUCCESS
);
2459 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2462 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2463 assert(rc
== MEMCACHED_SUCCESS
);
2465 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2466 assert(value
== MEMCACHED_HASH_MD5
);
2468 /* server should be removed when in delay */
2469 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2470 assert(rc
== MEMCACHED_SUCCESS
);
2472 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2475 memcached_server_st
*server_pool
;
2476 server_pool
= memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
2477 memcached_server_push(memc
, server_pool
);
2479 /* verify that the server list was parsed okay. */
2480 assert(memc
->number_of_hosts
== 8);
2481 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2482 assert(server_pool
[0].port
== 11211);
2483 assert(server_pool
[0].weight
== 600);
2484 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2485 assert(server_pool
[2].port
== 11211);
2486 assert(server_pool
[2].weight
== 200);
2487 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2488 assert(server_pool
[7].port
== 11211);
2489 assert(server_pool
[7].weight
== 100);
2491 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2492 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2494 for (int x
= 0; x
< 99; x
++)
2496 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2497 assert(server_idx
!= 2);
2500 /* and re-added when it's back. */
2501 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2502 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2503 run_distribution(memc
);
2504 for (int x
= 0; x
< 99; x
++)
2506 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2507 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2508 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2511 memcached_server_list_free(server_pool
);
2512 memcached_free(memc
);
2514 return TEST_SUCCESS
;
2517 static test_return
result_static(memcached_st
*memc
)
2519 memcached_result_st result
;
2520 memcached_result_st
*result_ptr
;
2522 result_ptr
= memcached_result_create(memc
, &result
);
2523 assert(result
.is_allocated
== false);
2525 memcached_result_free(&result
);
2530 static test_return
result_alloc(memcached_st
*memc
)
2532 memcached_result_st
*result
;
2534 result
= memcached_result_create(memc
, NULL
);
2536 memcached_result_free(result
);
2541 static test_return
string_static_null(memcached_st
*memc
)
2543 memcached_string_st string
;
2544 memcached_string_st
*string_ptr
;
2546 string_ptr
= memcached_string_create(memc
, &string
, 0);
2547 assert(string
.is_allocated
== false);
2549 memcached_string_free(&string
);
2554 static test_return
string_alloc_null(memcached_st
*memc
)
2556 memcached_string_st
*string
;
2558 string
= memcached_string_create(memc
, NULL
, 0);
2560 memcached_string_free(string
);
2565 static test_return
string_alloc_with_size(memcached_st
*memc
)
2567 memcached_string_st
*string
;
2569 string
= memcached_string_create(memc
, NULL
, 1024);
2571 memcached_string_free(string
);
2576 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2578 memcached_string_st
*string
;
2580 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2581 assert(string
== NULL
);
2586 static test_return
string_alloc_append(memcached_st
*memc
)
2589 char buffer
[SMALL_STRING_LEN
];
2590 memcached_string_st
*string
;
2592 /* Ring the bell! */
2593 memset(buffer
, 6, SMALL_STRING_LEN
);
2595 string
= memcached_string_create(memc
, NULL
, 100);
2598 for (x
= 0; x
< 1024; x
++)
2600 memcached_return rc
;
2601 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2602 assert(rc
== MEMCACHED_SUCCESS
);
2604 memcached_string_free(string
);
2609 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2611 memcached_return rc
;
2613 char buffer
[SMALL_STRING_LEN
];
2614 memcached_string_st
*string
;
2616 /* Ring the bell! */
2617 memset(buffer
, 6, SMALL_STRING_LEN
);
2619 string
= memcached_string_create(memc
, NULL
, 100);
2622 for (x
= 0; x
< 1024; x
++)
2624 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2625 assert(rc
== MEMCACHED_SUCCESS
);
2627 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
2628 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2629 memcached_string_free(string
);
2634 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2636 pairs_free(global_pairs
);
2641 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2643 unsigned long long x
;
2644 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2645 global_count
= GLOBAL_COUNT
;
2647 for (x
= 0; x
< global_count
; x
++)
2649 global_keys
[x
]= global_pairs
[x
].key
;
2650 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2656 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2658 unsigned long long x
;
2659 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2660 global_count
= GLOBAL2_COUNT
;
2662 for (x
= 0; x
< global_count
; x
++)
2664 global_keys
[x
]= global_pairs
[x
].key
;
2665 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2671 static test_return
generate_data(memcached_st
*memc
)
2673 execute_set(memc
, global_pairs
, global_count
);
2678 static test_return
generate_data_with_stats(memcached_st
*memc
)
2680 memcached_stat_st
*stat_p
;
2681 memcached_return rc
;
2682 uint32_t host_index
= 0;
2683 execute_set(memc
, global_pairs
, global_count
);
2685 //TODO: hosts used size stats
2686 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2689 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2691 /* This test was changes so that "make test" would work properlly */
2693 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, (memc
->hosts
)[host_index
].hostname
, (memc
->hosts
)[host_index
].port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
2695 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
2698 memcached_stat_free(NULL
, stat_p
);
2702 static test_return
generate_buffer_data(memcached_st
*memc
)
2707 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2708 generate_data(memc
);
2713 static test_return
get_read_count(memcached_st
*memc
)
2716 memcached_return rc
;
2717 memcached_st
*memc_clone
;
2719 memc_clone
= memcached_clone(NULL
, memc
);
2722 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
2726 size_t return_value_length
;
2730 for (x
= count
= 0; x
< global_count
; x
++)
2732 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
2733 &return_value_length
, &flags
, &rc
);
2734 if (rc
== MEMCACHED_SUCCESS
)
2741 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2744 memcached_free(memc_clone
);
2749 static test_return
get_read(memcached_st
*memc
)
2752 memcached_return rc
;
2756 size_t return_value_length
;
2759 for (x
= 0; x
< global_count
; x
++)
2761 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2762 &return_value_length
, &flags
, &rc
);
2764 assert(return_value);
2765 assert(rc == MEMCACHED_SUCCESS);
2767 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2775 static test_return
mget_read(memcached_st
*memc
)
2777 memcached_return rc
;
2779 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2780 assert(rc
== MEMCACHED_SUCCESS
);
2781 /* Turn this into a help function */
2783 char return_key
[MEMCACHED_MAX_KEY
];
2784 size_t return_key_length
;
2786 size_t return_value_length
;
2789 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2790 &return_value_length
, &flags
, &rc
)))
2792 assert(return_value
);
2793 assert(rc
== MEMCACHED_SUCCESS
);
2801 static test_return
mget_read_result(memcached_st
*memc
)
2803 memcached_return rc
;
2805 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2806 assert(rc
== MEMCACHED_SUCCESS
);
2807 /* Turn this into a help function */
2809 memcached_result_st results_obj
;
2810 memcached_result_st
*results
;
2812 results
= memcached_result_create(memc
, &results_obj
);
2814 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2817 assert(rc
== MEMCACHED_SUCCESS
);
2820 memcached_result_free(&results_obj
);
2826 static test_return
mget_read_function(memcached_st
*memc
)
2828 memcached_return rc
;
2829 unsigned int counter
;
2830 memcached_execute_function callbacks
[1];
2832 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2833 assert(rc
== MEMCACHED_SUCCESS
);
2835 callbacks
[0]= &callback_counter
;
2837 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2842 static test_return
delete_generate(memcached_st
*memc
)
2846 for (x
= 0; x
< global_count
; x
++)
2848 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2854 static test_return
delete_buffer_generate(memcached_st
*memc
)
2860 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2862 for (x
= 0; x
< global_count
; x
++)
2864 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2870 static test_return
add_host_test1(memcached_st
*memc
)
2873 memcached_return rc
;
2874 char servername
[]= "0.example.com";
2875 memcached_server_st
*servers
;
2877 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2879 assert(1 == memcached_server_list_count(servers
));
2881 for (x
= 2; x
< 20; x
++)
2883 char buffer
[SMALL_STRING_LEN
];
2885 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2886 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2888 assert(rc
== MEMCACHED_SUCCESS
);
2889 assert(x
== memcached_server_list_count(servers
));
2892 rc
= memcached_server_push(memc
, servers
);
2893 assert(rc
== MEMCACHED_SUCCESS
);
2894 rc
= memcached_server_push(memc
, servers
);
2895 assert(rc
== MEMCACHED_SUCCESS
);
2897 memcached_server_list_free(servers
);
2902 static memcached_return
pre_nonblock(memcached_st
*memc
)
2904 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2906 return MEMCACHED_SUCCESS
;
2909 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
2911 memcached_return rc
= MEMCACHED_FAILURE
;
2912 memcached_st
*memc_clone
;
2914 memc_clone
= memcached_clone(NULL
, memc
);
2916 // The memcached_version needs to be done on a clone, because the server
2917 // will not toggle protocol on an connection.
2918 memcached_version(memc_clone
);
2920 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
2922 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2923 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2924 assert(rc
== MEMCACHED_SUCCESS
);
2925 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2928 memcached_free(memc_clone
);
2932 static memcached_return
pre_murmur(memcached_st
*memc
)
2934 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2936 return MEMCACHED_SUCCESS
;
2939 static memcached_return
pre_jenkins(memcached_st
*memc
)
2941 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
2943 return MEMCACHED_SUCCESS
;
2947 static memcached_return
pre_md5(memcached_st
*memc
)
2949 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2951 return MEMCACHED_SUCCESS
;
2954 static memcached_return
pre_crc(memcached_st
*memc
)
2956 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2958 return MEMCACHED_SUCCESS
;
2961 static memcached_return
pre_hsieh(memcached_st
*memc
)
2963 #ifdef HAVE_HSIEH_HASH
2964 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2965 return MEMCACHED_SUCCESS
;
2968 return MEMCACHED_FAILURE
;
2972 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2974 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2976 return MEMCACHED_SUCCESS
;
2979 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2981 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2983 return MEMCACHED_SUCCESS
;
2986 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2988 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2990 return MEMCACHED_SUCCESS
;
2993 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2995 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2997 return MEMCACHED_SUCCESS
;
3000 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3002 memcached_return rc
;
3005 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3006 assert(rc
== MEMCACHED_SUCCESS
);
3008 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3011 return MEMCACHED_SUCCESS
;
3014 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3016 memcached_return rc
;
3019 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3020 assert(rc
== MEMCACHED_SUCCESS
);
3022 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3025 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3026 assert(rc
== MEMCACHED_SUCCESS
);
3028 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3029 assert(value
== MEMCACHED_HASH_MD5
);
3030 return MEMCACHED_SUCCESS
;
3033 static memcached_return
pre_binary(memcached_st
*memc
)
3035 memcached_return rc
= MEMCACHED_FAILURE
;
3036 memcached_st
*memc_clone
;
3038 memc_clone
= memcached_clone(NULL
, memc
);
3040 // The memcached_version needs to be done on a clone, because the server
3041 // will not toggle protocol on an connection.
3042 memcached_version(memc_clone
);
3044 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3046 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3047 assert(rc
== MEMCACHED_SUCCESS
);
3048 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3051 memcached_free(memc_clone
);
3055 static memcached_return
pre_replication(memcached_st
*memc
)
3057 memcached_return rc
= MEMCACHED_FAILURE
;
3058 if (pre_binary(memc
) == MEMCACHED_SUCCESS
)
3061 * Make sure that we store the item on all servers
3062 * (master + replicas == number of servers)
3064 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3065 memc
->number_of_hosts
- 1);
3066 assert(rc
== MEMCACHED_SUCCESS
);
3067 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3073 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3078 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3080 void *ret
= malloc(size
);
3082 memset(ret
, 0xff, size
);
3087 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3089 return realloc(mem
, size
);
3092 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3094 return calloc(nelem
, size
);
3097 static memcached_return
set_prefix(memcached_st
*memc
)
3099 memcached_return rc
;
3100 const char *key
= "mine";
3103 /* Make sure be default none exists */
3104 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3105 assert(rc
== MEMCACHED_FAILURE
);
3107 /* Test a clean set */
3108 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3109 assert(rc
== MEMCACHED_SUCCESS
);
3111 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3112 assert(memcmp(value
, key
, 4) == 0);
3113 assert(rc
== MEMCACHED_SUCCESS
);
3115 /* Test that we can turn it off */
3116 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3117 assert(rc
== MEMCACHED_SUCCESS
);
3119 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3120 assert(rc
== MEMCACHED_FAILURE
);
3122 /* Now setup for main test */
3123 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3124 assert(rc
== MEMCACHED_SUCCESS
);
3126 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3127 assert(rc
== MEMCACHED_SUCCESS
);
3128 assert(memcmp(value
, key
, 4) == 0);
3130 /* Set to Zero, and then Set to something too large */
3133 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3134 assert(rc
== MEMCACHED_SUCCESS
);
3136 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3137 assert(rc
== MEMCACHED_FAILURE
);
3138 assert(value
== NULL
);
3140 /* Test a long key for failure */
3141 /* TODO, extend test to determine based on setting, what result should be */
3142 long_key
= "Thisismorethentheallottednumberofcharacters";
3143 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3144 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3145 assert(rc
== MEMCACHED_SUCCESS
);
3147 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3148 long_key
= "This is more then the allotted number of characters";
3149 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3150 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3152 /* Test for a bad prefix, but with a short key */
3153 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3154 assert(rc
== MEMCACHED_SUCCESS
);
3156 long_key
= "dog cat";
3157 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3158 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3161 return MEMCACHED_SUCCESS
;
3164 #ifdef MEMCACHED_ENABLE_DEPRECATED
3165 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3167 void *test_ptr
= NULL
;
3170 memcached_malloc_function malloc_cb
=
3171 (memcached_malloc_function
)my_malloc
;
3172 cb_ptr
= *(void **)&malloc_cb
;
3173 memcached_return rc
;
3175 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3176 assert(rc
== MEMCACHED_SUCCESS
);
3177 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3178 assert(rc
== MEMCACHED_SUCCESS
);
3179 assert(test_ptr
== cb_ptr
);
3183 memcached_realloc_function realloc_cb
=
3184 (memcached_realloc_function
)my_realloc
;
3185 cb_ptr
= *(void **)&realloc_cb
;
3186 memcached_return rc
;
3188 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3189 assert(rc
== MEMCACHED_SUCCESS
);
3190 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3191 assert(rc
== MEMCACHED_SUCCESS
);
3192 assert(test_ptr
== cb_ptr
);
3196 memcached_free_function free_cb
=
3197 (memcached_free_function
)my_free
;
3198 cb_ptr
= *(void **)&free_cb
;
3199 memcached_return rc
;
3201 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3202 assert(rc
== MEMCACHED_SUCCESS
);
3203 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3204 assert(rc
== MEMCACHED_SUCCESS
);
3205 assert(test_ptr
== cb_ptr
);
3207 return MEMCACHED_SUCCESS
;
3211 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3213 memcached_return rc
;
3214 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3215 my_realloc
, my_calloc
);
3216 assert(rc
== MEMCACHED_FAILURE
);
3218 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3219 my_realloc
, my_calloc
);
3221 memcached_malloc_function mem_malloc
;
3222 memcached_free_function mem_free
;
3223 memcached_realloc_function mem_realloc
;
3224 memcached_calloc_function mem_calloc
;
3225 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3226 &mem_realloc
, &mem_calloc
);
3228 assert(mem_malloc
== my_malloc
);
3229 assert(mem_realloc
== my_realloc
);
3230 assert(mem_calloc
== my_calloc
);
3231 assert(mem_free
== my_free
);
3233 return MEMCACHED_SUCCESS
;
3236 static memcached_return
enable_consistent(memcached_st
*memc
)
3238 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3239 memcached_hash hash
;
3240 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3241 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3242 return MEMCACHED_FAILURE
;
3244 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3245 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3247 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3248 assert(hash
== MEMCACHED_HASH_HSIEH
);
3251 return MEMCACHED_SUCCESS
;
3254 static memcached_return
enable_cas(memcached_st
*memc
)
3256 unsigned int set
= 1;
3258 memcached_version(memc
);
3260 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3261 || memc
->hosts
[0].minor_version
> 2)
3263 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3265 return MEMCACHED_SUCCESS
;
3268 return MEMCACHED_FAILURE
;
3271 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3273 memcached_version(memc
);
3275 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3276 || memc
->hosts
[0].minor_version
> 2)
3277 return MEMCACHED_SUCCESS
;
3279 return MEMCACHED_FAILURE
;
3282 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3284 memcached_return rc
;
3287 memcached_server_list_free(memc
->hosts
);
3289 memc
->number_of_hosts
= 0;
3291 if (stat("/tmp/memcached.socket", &buf
))
3292 return MEMCACHED_FAILURE
;
3294 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3299 static memcached_return
pre_nodelay(memcached_st
*memc
)
3301 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3302 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3304 return MEMCACHED_SUCCESS
;
3307 static memcached_return
pre_settimer(memcached_st
*memc
)
3309 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3310 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3312 return MEMCACHED_SUCCESS
;
3315 static memcached_return
poll_timeout(memcached_st
*memc
)
3321 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3323 timeout
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3325 assert(timeout
== 100);
3327 return MEMCACHED_SUCCESS
;
3330 static test_return
noreply_test(memcached_st
*memc
)
3332 memcached_return ret
;
3333 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3334 assert(ret
== MEMCACHED_SUCCESS
);
3335 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3336 assert(ret
== MEMCACHED_SUCCESS
);
3337 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3338 assert(ret
== MEMCACHED_SUCCESS
);
3339 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3340 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3341 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3343 for (int count
=0; count
< 5; ++count
)
3345 for (int x
=0; x
< 100; ++x
)
3348 size_t len
= (size_t)sprintf(key
, "%d", x
);
3352 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3355 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3358 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3361 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3364 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3370 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3374 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3375 ** API and is _ONLY_ done this way to verify that the library works the
3376 ** way it is supposed to do!!!!
3379 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3380 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3382 assert(no_msg
== 0);
3383 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3386 ** Now validate that all items was set properly!
3388 for (int x
=0; x
< 100; ++x
)
3391 size_t len
= (size_t)sprintf(key
, "%d", x
);
3394 char* value
=memcached_get(memc
, key
, strlen(key
),
3395 &length
, &flags
, &ret
);
3396 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3399 case 0: /* FALLTHROUGH */
3400 case 1: /* FALLTHROUGH */
3402 assert(strncmp(value
, key
, len
) == 0);
3403 assert(len
== length
);
3406 assert(length
== len
* 2);
3409 assert(length
== len
* 3);
3419 /* Try setting an illegal cas value (should not return an error to
3420 * the caller (because we don't expect a return message from the server)
3422 char* keys
[]= {"0"};
3423 size_t lengths
[]= {1};
3426 memcached_result_st results_obj
;
3427 memcached_result_st
*results
;
3428 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3429 assert(ret
== MEMCACHED_SUCCESS
);
3431 results
= memcached_result_create(memc
, &results_obj
);
3433 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3435 assert(ret
== MEMCACHED_SUCCESS
);
3436 uint64_t cas
= memcached_result_cas(results
);
3437 memcached_result_free(&results_obj
);
3439 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3440 assert(ret
== MEMCACHED_SUCCESS
);
3443 * The item will have a new cas value, so try to set it again with the old
3444 * value. This should fail!
3446 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3447 assert(ret
== MEMCACHED_SUCCESS
);
3448 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3449 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3450 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3453 return TEST_SUCCESS
;
3456 static test_return
analyzer_test(memcached_st
*memc
)
3458 memcached_return rc
;
3459 memcached_stat_st
*memc_stat
;
3460 memcached_analysis_st
*report
;
3462 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3463 assert(rc
== MEMCACHED_SUCCESS
);
3466 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3467 assert(rc
== MEMCACHED_SUCCESS
);
3471 memcached_stat_free(NULL
, memc_stat
);
3473 return TEST_SUCCESS
;
3476 /* Count the objects */
3477 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3478 const char *key
__attribute__((unused
)),
3479 size_t key_length
__attribute__((unused
)),
3482 uint32_t *counter
= (uint32_t *)context
;
3484 *counter
= *counter
+ 1;
3486 return MEMCACHED_SUCCESS
;
3489 static test_return
dump_test(memcached_st
*memc
)
3491 memcached_return rc
;
3492 uint32_t counter
= 0;
3493 memcached_dump_func callbacks
[1];
3494 test_return main_rc
;
3496 callbacks
[0]= &callback_dump_counter
;
3498 /* No support for Binary protocol yet */
3499 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3500 return TEST_SUCCESS
;
3502 main_rc
= set_test3(memc
);
3504 assert (main_rc
== TEST_SUCCESS
);
3506 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3507 assert(rc
== MEMCACHED_SUCCESS
);
3509 /* We may have more then 32 if our previous flush has not completed */
3510 assert(counter
>= 32);
3512 return TEST_SUCCESS
;
3515 #ifdef HAVE_LIBMEMCACHEDUTIL
3516 static void* connection_release(void *arg
) {
3518 memcached_pool_st
* pool
;
3523 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3527 static test_return
connection_pool_test(memcached_st
*memc
)
3529 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3530 assert(pool
!= NULL
);
3531 memcached_st
* mmc
[10];
3532 memcached_return rc
;
3534 for (int x
= 0; x
< 10; ++x
) {
3535 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3536 assert(mmc
[x
] != NULL
);
3537 assert(rc
== MEMCACHED_SUCCESS
);
3540 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3541 assert(rc
== MEMCACHED_SUCCESS
);
3545 memcached_pool_st
* pool
;
3547 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3548 pthread_create(&tid
, NULL
, connection_release
, &item
);
3549 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3550 assert(rc
== MEMCACHED_SUCCESS
);
3551 pthread_join(tid
, NULL
);
3552 assert(mmc
[9] == item
.mmc
);
3553 const char *key
= "key";
3554 size_t keylen
= strlen(key
);
3556 // verify that I can do ops with all connections
3557 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3558 assert(rc
== MEMCACHED_SUCCESS
);
3560 for (unsigned int x
= 0; x
< 10; ++x
) {
3561 uint64_t number_value
;
3562 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3563 assert(rc
== MEMCACHED_SUCCESS
);
3564 assert(number_value
== (x
+1));
3568 for (int x
= 0; x
< 10; ++x
)
3569 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3571 assert(memcached_pool_destroy(pool
) == memc
);
3572 return TEST_SUCCESS
;
3576 static test_return
replication_set_test(memcached_st
*memc
)
3578 memcached_return rc
;
3579 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3580 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3582 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3583 assert(rc
== MEMCACHED_SUCCESS
);
3586 ** "bubba" should now be stored on all of our servers. We don't have an
3587 ** easy to use API to address each individual server, so I'll just iterate
3588 ** through a bunch of "master keys" and I should most likely hit all of the
3591 for (int x
= 'a'; x
<= 'z'; ++x
)
3593 char key
[2]= { [0]= (char)x
};
3596 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3598 assert(rc
== MEMCACHED_SUCCESS
);
3599 assert(val
!= NULL
);
3603 memcached_free(clone
);
3605 return TEST_SUCCESS
;
3608 static test_return
replication_get_test(memcached_st
*memc
)
3610 memcached_return rc
;
3613 * Don't do the following in your code. I am abusing the internal details
3614 * within the library, and this is not a supported interface.
3615 * This is to verify correct behavior in the library
3617 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
3619 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3620 clone
->hosts
[host
].port
= 0;
3622 for (int x
= 'a'; x
<= 'z'; ++x
)
3624 char key
[2]= { [0]= (char)x
};
3627 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3629 assert(rc
== MEMCACHED_SUCCESS
);
3630 assert(val
!= NULL
);
3634 memcached_free(clone
);
3637 return TEST_SUCCESS
;
3640 static test_return
replication_mget_test(memcached_st
*memc
)
3642 memcached_return rc
;
3643 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3644 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3646 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3647 size_t len
[]= { 5, 4, 4, 4 };
3649 for (int x
=0; x
< 4; ++x
)
3651 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3652 assert(rc
== MEMCACHED_SUCCESS
);
3656 * Don't do the following in your code. I am abusing the internal details
3657 * within the library, and this is not a supported interface.
3658 * This is to verify correct behavior in the library
3660 memcached_result_st result_obj
;
3661 for (uint32_t host
= 0; host
< clone
->number_of_hosts
; host
++)
3663 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
3664 new_clone
->hosts
[host
].port
= 0;
3666 for (int x
= 'a'; x
<= 'z'; ++x
)
3668 char key
[2]= { [0]= (char)x
};
3670 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
3671 assert(rc
== MEMCACHED_SUCCESS
);
3673 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
3677 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
3682 memcached_result_free(&result_obj
);
3685 memcached_free(new_clone
);
3688 return TEST_SUCCESS
;
3691 static test_return
replication_delete_test(memcached_st
*memc
)
3693 memcached_return rc
;
3694 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3695 /* Delete the items from all of the servers except 1 */
3696 uint64_t repl
= memcached_behavior_get(memc
,
3697 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3698 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3700 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3701 size_t len
[]= { 5, 4, 4, 4 };
3703 for (int x
=0; x
< 4; ++x
)
3705 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
3706 assert(rc
== MEMCACHED_SUCCESS
);
3710 * Don't do the following in your code. I am abusing the internal details
3711 * within the library, and this is not a supported interface.
3712 * This is to verify correct behavior in the library
3714 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
3715 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
3717 clone
->hosts
[hash
].port
= 0;
3718 if (++hash
== clone
->number_of_hosts
)
3722 memcached_result_st result_obj
;
3723 for (uint32_t host
= 0; host
< clone
->number_of_hosts
; ++host
)
3725 for (int x
= 'a'; x
<= 'z'; ++x
)
3727 char key
[2]= { [0]= (char)x
};
3729 rc
= memcached_mget_by_key(clone
, key
, 1, keys
, len
, 4);
3730 assert(rc
== MEMCACHED_SUCCESS
);
3732 memcached_result_st
*results
= memcached_result_create(clone
, &result_obj
);
3736 while ((results
= memcached_fetch_result(clone
, &result_obj
, &rc
)) != NULL
)
3741 memcached_result_free(&result_obj
);
3744 memcached_free(clone
);
3746 return TEST_SUCCESS
;
3749 static void increment_request_id(uint16_t *id
)
3752 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
3756 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
3758 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
3759 assert(ids
!= NULL
);
3762 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3763 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
3768 static test_return
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
3771 memcached_server_st
*cur_server
= memc
->hosts
;
3772 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
3774 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3776 assert(cur_server
[x
].cursor_active
== 0);
3777 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
3779 free(expected_req_ids
);
3782 return TEST_SUCCESS
;
3786 ** There is a little bit of a hack here, instead of removing
3787 ** the servers, I just set num host to 0 and them add then new udp servers
3789 static memcached_return
init_udp(memcached_st
*memc
)
3791 memcached_version(memc
);
3792 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3793 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
3794 || memc
->hosts
[0].micro_version
< 6)
3795 return MEMCACHED_FAILURE
;
3797 uint32_t num_hosts
= memc
->number_of_hosts
;
3799 memcached_server_st servers
[num_hosts
];
3800 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
3801 for (x
= 0; x
< num_hosts
; x
++)
3802 memcached_server_free(&memc
->hosts
[x
]);
3804 memc
->number_of_hosts
= 0;
3805 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
3806 for (x
= 0; x
< num_hosts
; x
++)
3808 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
3809 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3812 return MEMCACHED_SUCCESS
;
3815 static memcached_return
binary_init_udp(memcached_st
*memc
)
3818 return init_udp(memc
);
3821 /* Make sure that I cant add a tcp server to a udp client */
3822 static test_return
add_tcp_server_udp_client_test(memcached_st
*memc
)
3824 memcached_server_st server
;
3825 memcached_server_clone(&server
, &memc
->hosts
[0]);
3826 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3827 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3828 return TEST_SUCCESS
;
3831 /* Make sure that I cant add a udp server to a tcp client */
3832 static test_return
add_udp_server_tcp_client_test(memcached_st
*memc
)
3834 memcached_server_st server
;
3835 memcached_server_clone(&server
, &memc
->hosts
[0]);
3836 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3838 memcached_st tcp_client
;
3839 memcached_create(&tcp_client
);
3840 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3841 return TEST_SUCCESS
;
3844 static test_return
set_udp_behavior_test(memcached_st
*memc
)
3847 memcached_quit(memc
);
3848 memc
->number_of_hosts
= 0;
3849 run_distribution(memc
);
3850 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
3851 assert(memc
->flags
& MEM_USE_UDP
);
3852 assert(memc
->flags
& MEM_NOREPLY
);;
3854 assert(memc
->number_of_hosts
== 0);
3856 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
3857 assert(!(memc
->flags
& MEM_USE_UDP
));
3858 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
3859 assert(!(memc
->flags
& MEM_NOREPLY
));
3860 return TEST_SUCCESS
;
3863 static test_return
udp_set_test(memcached_st
*memc
)
3866 unsigned int num_iters
= 1025; //request id rolls over at 1024
3867 for (x
= 0; x
< num_iters
;x
++)
3869 memcached_return rc
;
3871 char *value
= "when we sanitize";
3872 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3873 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
3874 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3875 rc
= memcached_set(memc
, key
, strlen(key
),
3876 value
, strlen(value
),
3877 (time_t)0, (uint32_t)0);
3878 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3879 /** NB, the check below assumes that if new write_ptr is less than
3880 * the original write_ptr that we have flushed. For large payloads, this
3881 * maybe an invalid assumption, but for the small payload we have it is OK
3883 if (rc
== MEMCACHED_SUCCESS
||
3884 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3885 increment_request_id(&expected_ids
[server_key
]);
3887 if (rc
== MEMCACHED_SUCCESS
)
3889 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3893 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3894 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3896 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3898 return TEST_SUCCESS
;
3901 static test_return
udp_buffered_set_test(memcached_st
*memc
)
3903 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3904 return udp_set_test(memc
);
3907 static test_return
udp_set_too_big_test(memcached_st
*memc
)
3909 memcached_return rc
;
3911 char value
[MAX_UDP_DATAGRAM_LENGTH
];
3912 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3913 rc
= memcached_set(memc
, key
, strlen(key
),
3914 value
, MAX_UDP_DATAGRAM_LENGTH
,
3915 (time_t)0, (uint32_t)0);
3916 assert(rc
== MEMCACHED_WRITE_FAILURE
);
3917 return post_udp_op_check(memc
,expected_ids
);
3920 static test_return
udp_delete_test(memcached_st
*memc
)
3923 unsigned int num_iters
= 1025; //request id rolls over at 1024
3924 for (x
= 0; x
< num_iters
;x
++)
3926 memcached_return rc
;
3928 uint16_t *expected_ids
=get_udp_request_ids(memc
);
3929 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3930 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3931 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
3932 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3933 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3934 increment_request_id(&expected_ids
[server_key
]);
3935 if (rc
== MEMCACHED_SUCCESS
)
3936 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3939 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3940 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3942 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3944 return TEST_SUCCESS
;
3947 static test_return
udp_buffered_delete_test(memcached_st
*memc
)
3949 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3950 return udp_delete_test(memc
);
3953 static test_return
udp_verbosity_test(memcached_st
*memc
)
3955 memcached_return rc
;
3956 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3958 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
3959 increment_request_id(&expected_ids
[x
]);
3961 rc
= memcached_verbosity(memc
,3);
3962 assert(rc
== MEMCACHED_SUCCESS
);
3963 return post_udp_op_check(memc
,expected_ids
);
3966 static test_return
udp_quit_test(memcached_st
*memc
)
3968 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3969 memcached_quit(memc
);
3970 return post_udp_op_check(memc
, expected_ids
);
3973 static test_return
udp_flush_test(memcached_st
*memc
)
3975 memcached_return rc
;
3976 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3978 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
3979 increment_request_id(&expected_ids
[x
]);
3981 rc
= memcached_flush(memc
,0);
3982 assert(rc
== MEMCACHED_SUCCESS
);
3983 return post_udp_op_check(memc
,expected_ids
);
3986 static test_return
udp_incr_test(memcached_st
*memc
)
3988 memcached_return rc
;
3991 rc
= memcached_set(memc
, key
, strlen(key
),
3992 value
, strlen(value
),
3993 (time_t)0, (uint32_t)0);
3995 assert(rc
== MEMCACHED_SUCCESS
);
3996 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3997 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3998 increment_request_id(&expected_ids
[server_key
]);
4000 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4001 assert(rc
== MEMCACHED_SUCCESS
);
4002 return post_udp_op_check(memc
, expected_ids
);
4005 static test_return
udp_decr_test(memcached_st
*memc
)
4007 memcached_return rc
;
4010 rc
= memcached_set(memc
, key
, strlen(key
),
4011 value
, strlen(value
),
4012 (time_t)0, (uint32_t)0);
4014 assert(rc
== MEMCACHED_SUCCESS
);
4015 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4016 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4017 increment_request_id(&expected_ids
[server_key
]);
4019 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4020 assert(rc
== MEMCACHED_SUCCESS
);
4021 return post_udp_op_check(memc
, expected_ids
);
4025 static test_return
udp_stat_test(memcached_st
*memc
)
4027 memcached_stat_st
* rv
= NULL
;
4028 memcached_return rc
;
4030 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4031 rv
= memcached_stat(memc
, args
, &rc
);
4033 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4034 return post_udp_op_check(memc
, expected_ids
);
4037 static test_return
udp_version_test(memcached_st
*memc
)
4039 memcached_return rc
;
4040 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4041 rc
= memcached_version(memc
);
4042 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4043 return post_udp_op_check(memc
, expected_ids
);
4046 static test_return
udp_get_test(memcached_st
*memc
)
4048 memcached_return rc
;
4051 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4052 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4053 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4054 assert(val
== NULL
);
4055 return post_udp_op_check(memc
, expected_ids
);
4058 static test_return
udp_mixed_io_test(memcached_st
*memc
)
4061 test_st mixed_io_ops
[] ={
4062 {"udp_set_test", 0, udp_set_test
},
4063 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4064 {"udp_delete_test", 0, udp_delete_test
},
4065 {"udp_verbosity_test", 0, udp_verbosity_test
},
4066 {"udp_quit_test", 0, udp_quit_test
},
4067 {"udp_flush_test", 0, udp_flush_test
},
4068 {"udp_incr_test", 0, udp_incr_test
},
4069 {"udp_decr_test", 0, udp_decr_test
},
4070 {"udp_version_test", 0, udp_version_test
}
4073 for (x
= 0; x
< 500; x
++)
4075 current_op
= mixed_io_ops
[random() % 9];
4076 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4078 return TEST_SUCCESS
;
4081 static test_return
hsieh_avaibility_test (memcached_st
*memc
)
4083 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4084 #ifdef HAVE_HSIEH_HASH
4085 expected_rc
= MEMCACHED_SUCCESS
;
4087 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4088 (uint64_t)MEMCACHED_HASH_HSIEH
);
4089 assert(rc
== expected_rc
);
4090 return TEST_SUCCESS
;
4093 static char *list
[]=
4123 static test_return
md5_run (memcached_st
*memc
__attribute__((unused
)))
4127 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4128 245758794U, 2550894432U, 121710495U, 3053817768U,
4129 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4130 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4131 2138080750U, 217675895U, 2230934345U, 1234361223U,
4132 3968582726U, 2455685270U, 1293568479U, 199067604U,
4136 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4140 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4141 assert(values
[x
] == hash_val
);
4144 return TEST_SUCCESS
;
4147 static test_return
crc_run (memcached_st
*memc
__attribute__((unused
)))
4151 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4152 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4153 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4154 17300U, 28156U, 9192U, 20484U, 16925U };
4156 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4160 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4161 assert(values
[x
] == hash_val
);
4164 return TEST_SUCCESS
;
4167 static test_return
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4171 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4172 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4173 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4174 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4175 2815549194U, 2562818319U, 224996066U, 2680194749U,
4176 3035305390U, 246890365U, 2395624193U, 4145193337U,
4179 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4183 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4184 assert(values
[x
] == hash_val
);
4187 return TEST_SUCCESS
;
4190 static test_return
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4194 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4195 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4196 734810122U, 1516407546U, 560948863U, 1767346780U,
4197 561034892U, 4156330026U, 3716417003U, 3475297030U,
4198 1518272172U, 227211583U, 3938128828U, 126112909U,
4199 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4202 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4206 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4207 assert(values
[x
] == hash_val
);
4210 return TEST_SUCCESS
;
4213 static test_return
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4217 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4218 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4219 344481048U, 3865235296U, 2181839183U, 119581266U,
4220 510234242U, 4248244304U, 1362796839U, 103389328U,
4221 1449620010U, 182962511U, 3554262370U, 3206747549U,
4222 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4226 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4230 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4231 assert(values
[x
] == hash_val
);
4234 return TEST_SUCCESS
;
4237 static test_return
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4241 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4242 459261019U, 3521085446U, 18738364U, 1625305005U,
4243 2162232970U, 777243802U, 3323728671U, 132336572U,
4244 3654473228U, 260679466U, 1169454059U, 2698319462U,
4245 1062177260U, 235516991U, 2218399068U, 405302637U,
4246 1128467232U, 3579622413U, 2138539289U, 96429129U,
4249 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4253 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4254 assert(values
[x
] == hash_val
);
4257 return TEST_SUCCESS
;
4260 static test_return
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4264 #ifdef HAVE_HSIEH_HASH
4265 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4266 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4267 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4268 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4271 uint32_t values
[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
4274 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4278 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4279 assert(values
[x
] == hash_val
);
4282 return TEST_SUCCESS
;
4285 static test_return
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4289 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4290 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4291 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4292 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4293 2815549194U, 2562818319U, 224996066U, 2680194749U,
4294 3035305390U, 246890365U, 2395624193U, 4145193337U,
4297 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4301 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4302 assert(values
[x
] == hash_val
);
4305 return TEST_SUCCESS
;
4308 static test_return
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4312 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4313 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4314 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4315 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4316 4153263658U, 3824871822U, 3072759809U, 798622255U,
4317 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4321 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4325 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4326 assert(values
[x
] == hash_val
);
4329 return TEST_SUCCESS
;
4332 test_st udp_setup_server_tests
[] ={
4333 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
4334 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
4335 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
4339 test_st upd_io_tests
[] ={
4340 {"udp_set_test", 0, udp_set_test
},
4341 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
4342 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4343 {"udp_delete_test", 0, udp_delete_test
},
4344 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
4345 {"udp_verbosity_test", 0, udp_verbosity_test
},
4346 {"udp_quit_test", 0, udp_quit_test
},
4347 {"udp_flush_test", 0, udp_flush_test
},
4348 {"udp_incr_test", 0, udp_incr_test
},
4349 {"udp_decr_test", 0, udp_decr_test
},
4350 {"udp_stat_test", 0, udp_stat_test
},
4351 {"udp_version_test", 0, udp_version_test
},
4352 {"udp_get_test", 0, udp_get_test
},
4353 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
4357 /* Clean the server before beginning testing */
4359 {"flush", 0, flush_test
},
4360 {"init", 0, init_test
},
4361 {"allocation", 0, allocation_test
},
4362 {"server_list_null_test", 0, server_list_null_test
},
4363 {"server_unsort", 0, server_unsort_test
},
4364 {"server_sort", 0, server_sort_test
},
4365 {"server_sort2", 0, server_sort2_test
},
4366 {"clone_test", 0, clone_test
},
4367 {"connection_test", 0, connection_test
},
4368 {"callback_test", 0, callback_test
},
4369 {"behavior_test", 0, behavior_test
},
4370 {"userdata_test", 0, userdata_test
},
4371 {"error", 0, error_test
},
4372 {"set", 0, set_test
},
4373 {"set2", 0, set_test2
},
4374 {"set3", 0, set_test3
},
4375 {"dump", 1, dump_test
},
4376 {"add", 1, add_test
},
4377 {"replace", 1, replace_test
},
4378 {"delete", 1, delete_test
},
4379 {"get", 1, get_test
},
4380 {"get2", 0, get_test2
},
4381 {"get3", 0, get_test3
},
4382 {"get4", 0, get_test4
},
4383 {"partial mget", 0, get_test5
},
4384 {"stats_servername", 0, stats_servername_test
},
4385 {"increment", 0, increment_test
},
4386 {"increment_with_initial", 1, increment_with_initial_test
},
4387 {"decrement", 0, decrement_test
},
4388 {"decrement_with_initial", 1, decrement_with_initial_test
},
4389 {"quit", 0, quit_test
},
4390 {"mget", 1, mget_test
},
4391 {"mget_result", 1, mget_result_test
},
4392 {"mget_result_alloc", 1, mget_result_alloc_test
},
4393 {"mget_result_function", 1, mget_result_function
},
4394 {"get_stats", 0, get_stats
},
4395 {"add_host_test", 0, add_host_test
},
4396 {"add_host_test_1", 0, add_host_test1
},
4397 {"get_stats_keys", 0, get_stats_keys
},
4398 {"behavior_test", 0, get_stats_keys
},
4399 {"callback_test", 0, get_stats_keys
},
4400 {"version_string_test", 0, version_string_test
},
4401 {"bad_key", 1, bad_key_test
},
4402 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
4403 {"read_through", 1, read_through
},
4404 {"delete_through", 1, delete_through
},
4405 {"noreply", 1, noreply_test
},
4406 {"analyzer", 1, analyzer_test
},
4407 #ifdef HAVE_LIBMEMCACHEDUTIL
4408 {"connectionpool", 1, connection_pool_test
},
4413 test_st async_tests
[] ={
4414 {"add", 1, add_wrapper
},
4418 test_st string_tests
[] ={
4419 {"string static with null", 0, string_static_null
},
4420 {"string alloc with null", 0, string_alloc_null
},
4421 {"string alloc with 1K", 0, string_alloc_with_size
},
4422 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
4423 {"string append", 0, string_alloc_append
},
4424 {"string append failure (too big)", 0, string_alloc_append_toobig
},
4428 test_st result_tests
[] ={
4429 {"result static", 0, result_static
},
4430 {"result alloc", 0, result_alloc
},
4434 test_st version_1_2_3
[] ={
4435 {"append", 0, append_test
},
4436 {"prepend", 0, prepend_test
},
4437 {"cas", 0, cas_test
},
4438 {"cas2", 0, cas2_test
},
4439 {"append_binary", 0, append_binary_test
},
4443 test_st user_tests
[] ={
4444 {"user_supplied_bug1", 0, user_supplied_bug1
},
4445 {"user_supplied_bug2", 0, user_supplied_bug2
},
4446 {"user_supplied_bug3", 0, user_supplied_bug3
},
4447 {"user_supplied_bug4", 0, user_supplied_bug4
},
4448 {"user_supplied_bug5", 1, user_supplied_bug5
},
4449 {"user_supplied_bug6", 1, user_supplied_bug6
},
4450 {"user_supplied_bug7", 1, user_supplied_bug7
},
4451 {"user_supplied_bug8", 1, user_supplied_bug8
},
4452 {"user_supplied_bug9", 1, user_supplied_bug9
},
4453 {"user_supplied_bug10", 1, user_supplied_bug10
},
4454 {"user_supplied_bug11", 1, user_supplied_bug11
},
4455 {"user_supplied_bug12", 1, user_supplied_bug12
},
4456 {"user_supplied_bug13", 1, user_supplied_bug13
},
4457 {"user_supplied_bug14", 1, user_supplied_bug14
},
4458 {"user_supplied_bug15", 1, user_supplied_bug15
},
4459 {"user_supplied_bug16", 1, user_supplied_bug16
},
4462 ** It seems to be something weird with the character sets..
4463 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4464 ** guess I need to find out how this is supposed to work.. Perhaps I need
4465 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4466 ** so just disable the code for now...).
4468 {"user_supplied_bug17", 1, user_supplied_bug17
},
4470 {"user_supplied_bug18", 1, user_supplied_bug18
},
4471 {"user_supplied_bug19", 1, user_supplied_bug19
},
4472 {"user_supplied_bug20", 1, user_supplied_bug20
},
4476 test_st replication_tests
[]= {
4477 {"set", 1, replication_set_test
},
4478 {"get", 0, replication_get_test
},
4479 {"mget", 0, replication_mget_test
},
4480 {"delete", 0, replication_delete_test
},
4484 test_st generate_tests
[] ={
4485 {"generate_pairs", 1, generate_pairs
},
4486 {"generate_data", 1, generate_data
},
4487 {"get_read", 0, get_read
},
4488 {"delete_generate", 0, delete_generate
},
4489 {"generate_buffer_data", 1, generate_buffer_data
},
4490 {"delete_buffer", 0, delete_buffer_generate
},
4491 {"generate_data", 1, generate_data
},
4492 {"mget_read", 0, mget_read
},
4493 {"mget_read_result", 0, mget_read_result
},
4494 {"mget_read_function", 0, mget_read_function
},
4495 {"cleanup", 1, cleanup_pairs
},
4496 {"generate_large_pairs", 1, generate_large_pairs
},
4497 {"generate_data", 1, generate_data
},
4498 {"generate_buffer_data", 1, generate_buffer_data
},
4499 {"cleanup", 1, cleanup_pairs
},
4503 test_st consistent_tests
[] ={
4504 {"generate_pairs", 1, generate_pairs
},
4505 {"generate_data", 1, generate_data
},
4506 {"get_read", 0, get_read_count
},
4507 {"cleanup", 1, cleanup_pairs
},
4511 test_st consistent_weighted_tests
[] ={
4512 {"generate_pairs", 1, generate_pairs
},
4513 {"generate_data", 1, generate_data_with_stats
},
4514 {"get_read", 0, get_read_count
},
4515 {"cleanup", 1, cleanup_pairs
},
4519 test_st hsieh_availability
[] ={
4520 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
4524 test_st ketama_auto_eject_hosts
[] ={
4525 {"auto_eject_hosts", 1, auto_eject_hosts
},
4529 test_st hash_tests
[] ={
4530 {"md5", 0, md5_run
},
4531 {"crc", 0, crc_run
},
4532 {"fnv1_64", 0, fnv1_64_run
},
4533 {"fnv1a_64", 0, fnv1a_64_run
},
4534 {"fnv1_32", 0, fnv1_32_run
},
4535 {"fnv1a_32", 0, fnv1a_32_run
},
4536 {"hsieh", 0, hsieh_run
},
4537 {"murmur", 0, murmur_run
},
4538 {"jenkis", 0, jenkins_run
},
4542 collection_st collection
[] ={
4543 {"hsieh_availability",0,0,hsieh_availability
},
4544 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
4545 {"udp_io", init_udp
, 0, upd_io_tests
},
4546 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
4547 {"block", 0, 0, tests
},
4548 {"binary", pre_binary
, 0, tests
},
4549 {"nonblock", pre_nonblock
, 0, tests
},
4550 {"nodelay", pre_nodelay
, 0, tests
},
4551 {"settimer", pre_settimer
, 0, tests
},
4552 {"md5", pre_md5
, 0, tests
},
4553 {"crc", pre_crc
, 0, tests
},
4554 {"hsieh", pre_hsieh
, 0, tests
},
4555 {"jenkins", pre_jenkins
, 0, tests
},
4556 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
4557 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
4558 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
4559 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
4560 {"ketama", pre_behavior_ketama
, 0, tests
},
4561 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
4562 {"unix_socket", pre_unix_socket
, 0, tests
},
4563 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
4564 {"poll_timeout", poll_timeout
, 0, tests
},
4565 {"gets", enable_cas
, 0, tests
},
4566 {"consistent", enable_consistent
, 0, tests
},
4567 #ifdef MEMCACHED_ENABLE_DEPRECATED
4568 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
4570 {"memory_allocators", set_memory_alloc
, 0, tests
},
4571 {"prefix", set_prefix
, 0, tests
},
4572 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
4573 {"string", 0, 0, string_tests
},
4574 {"result", 0, 0, result_tests
},
4575 {"async", pre_nonblock
, 0, async_tests
},
4576 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
4577 {"user", 0, 0, user_tests
},
4578 {"generate", 0, 0, generate_tests
},
4579 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
4580 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
4581 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
4582 {"generate_md5", pre_md5
, 0, generate_tests
},
4583 {"generate_murmur", pre_murmur
, 0, generate_tests
},
4584 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
4585 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
4586 {"consistent_not", 0, 0, consistent_tests
},
4587 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
4588 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
4589 {"test_hashes", 0, 0, hash_tests
},
4590 {"replication", pre_replication
, 0, replication_tests
},
4594 #define SERVERS_TO_CREATE 5
4596 /* Prototypes for functions we will pass to test framework */
4597 void *world_create(void);
4598 void world_destroy(void *p
);
4600 void *world_create(void)
4602 server_startup_st
*construct
;
4604 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
4605 memset(construct
, 0, sizeof(server_startup_st
));
4606 construct
->count
= SERVERS_TO_CREATE
;
4608 server_startup(construct
);
4614 void world_destroy(void *p
)
4616 server_startup_st
*construct
= (server_startup_st
*)p
;
4617 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
4618 memcached_server_list_free(servers
);
4620 server_shutdown(construct
);
4624 void get_world(world_st
*world
)
4626 world
->collections
= collection
;
4627 world
->create
= world_create
;
4628 world
->destroy
= world_destroy
;