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
]= 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
]= 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
)
204 clone
= memcached_clone(NULL
, NULL
);
206 memcached_free(clone
);
209 /* Can we init from null? */
212 clone
= memcached_clone(NULL
, memc
);
215 assert(clone
->call_free
== memc
->call_free
);
216 assert(clone
->call_malloc
== memc
->call_malloc
);
217 assert(clone
->call_realloc
== memc
->call_realloc
);
218 assert(clone
->call_calloc
== memc
->call_calloc
);
219 assert(clone
->connect_timeout
== memc
->connect_timeout
);
220 assert(clone
->delete_trigger
== memc
->delete_trigger
);
221 assert(clone
->distribution
== memc
->distribution
);
222 assert(clone
->flags
== memc
->flags
);
223 assert(clone
->get_key_failure
== memc
->get_key_failure
);
224 assert(clone
->hash
== memc
->hash
);
225 assert(clone
->hash_continuum
== memc
->hash_continuum
);
226 assert(clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
227 assert(clone
->io_msg_watermark
== memc
->io_msg_watermark
);
228 assert(clone
->io_key_prefetch
== memc
->io_key_prefetch
);
229 assert(clone
->on_cleanup
== memc
->on_cleanup
);
230 assert(clone
->on_clone
== memc
->on_clone
);
231 assert(clone
->poll_timeout
== memc
->poll_timeout
);
232 assert(clone
->rcv_timeout
== memc
->rcv_timeout
);
233 assert(clone
->recv_size
== memc
->recv_size
);
234 assert(clone
->retry_timeout
== memc
->retry_timeout
);
235 assert(clone
->send_size
== memc
->send_size
);
236 assert(clone
->server_failure_limit
== memc
->server_failure_limit
);
237 assert(clone
->snd_timeout
== memc
->snd_timeout
);
238 assert(clone
->user_data
== memc
->user_data
);
239 assert(clone
->number_of_replicas
== memc
->number_of_replicas
);
241 memcached_free(clone
);
244 /* Can we init from struct? */
246 memcached_st declared_clone
;
248 memset(&declared_clone
, 0 , sizeof(memcached_st
));
249 clone
= memcached_clone(&declared_clone
, NULL
);
251 memcached_free(clone
);
254 /* Can we init from struct? */
256 memcached_st declared_clone
;
258 memset(&declared_clone
, 0 , sizeof(memcached_st
));
259 clone
= memcached_clone(&declared_clone
, memc
);
261 memcached_free(clone
);
267 static test_return
userdata_test(memcached_st
*memc
)
270 assert(memcached_set_user_data(memc
, foo
) == NULL
);
271 assert(memcached_get_user_data(memc
) == foo
);
272 assert(memcached_set_user_data(memc
, NULL
) == foo
);
277 static test_return
connection_test(memcached_st
*memc
)
281 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
282 assert(rc
== MEMCACHED_SUCCESS
);
287 static test_return
error_test(memcached_st
*memc
)
291 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
293 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
299 static test_return
set_test(memcached_st
*memc
)
303 char *value
= "when we sanitize";
305 rc
= memcached_set(memc
, key
, strlen(key
),
306 value
, strlen(value
),
307 (time_t)0, (uint32_t)0);
308 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
313 static test_return
append_test(memcached_st
*memc
)
321 rc
= memcached_flush(memc
, 0);
322 assert(rc
== MEMCACHED_SUCCESS
);
324 rc
= memcached_set(memc
, key
, strlen(key
),
325 value
, strlen(value
),
326 (time_t)0, (uint32_t)0);
327 assert(rc
== MEMCACHED_SUCCESS
);
329 rc
= memcached_append(memc
, key
, strlen(key
),
330 " the", strlen(" the"),
331 (time_t)0, (uint32_t)0);
332 assert(rc
== MEMCACHED_SUCCESS
);
334 rc
= memcached_append(memc
, key
, strlen(key
),
335 " people", strlen(" people"),
336 (time_t)0, (uint32_t)0);
337 assert(rc
== MEMCACHED_SUCCESS
);
339 value
= memcached_get(memc
, key
, strlen(key
),
340 &value_length
, &flags
, &rc
);
341 assert(!memcmp(value
, "we the people", strlen("we the people")));
342 assert(strlen("we the people") == value_length
);
343 assert(rc
== MEMCACHED_SUCCESS
);
349 static test_return
append_binary_test(memcached_st
*memc
)
352 char *key
= "numbers";
353 unsigned int *store_ptr
;
354 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
360 rc
= memcached_flush(memc
, 0);
361 assert(rc
== MEMCACHED_SUCCESS
);
363 rc
= memcached_set(memc
,
366 (time_t)0, (uint32_t)0);
367 assert(rc
== MEMCACHED_SUCCESS
);
369 for (x
= 0; store_list
[x
] ; x
++)
371 rc
= memcached_append(memc
,
373 (char *)&store_list
[x
], sizeof(unsigned int),
374 (time_t)0, (uint32_t)0);
375 assert(rc
== MEMCACHED_SUCCESS
);
378 value
= memcached_get(memc
, key
, strlen(key
),
379 &value_length
, &flags
, &rc
);
380 assert((value_length
== (sizeof(unsigned int) * x
)));
381 assert(rc
== MEMCACHED_SUCCESS
);
383 store_ptr
= (unsigned int *)value
;
385 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
387 assert(*store_ptr
== store_list
[x
++]);
395 static test_return
cas2_test(memcached_st
*memc
)
398 char *keys
[]= {"fudge", "son", "food"};
399 size_t key_length
[]= {5, 3, 4};
400 char *value
= "we the people";
401 size_t value_length
= strlen("we the people");
403 memcached_result_st results_obj
;
404 memcached_result_st
*results
;
407 rc
= memcached_flush(memc
, 0);
408 assert(rc
== MEMCACHED_SUCCESS
);
410 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
412 for (x
= 0; x
< 3; x
++)
414 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
415 keys
[x
], key_length
[x
],
416 (time_t)50, (uint32_t)9);
417 assert(rc
== MEMCACHED_SUCCESS
);
420 rc
= memcached_mget(memc
, keys
, key_length
, 3);
422 results
= memcached_result_create(memc
, &results_obj
);
424 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
426 assert(results
->cas
);
427 assert(rc
== MEMCACHED_SUCCESS
);
428 WATCHPOINT_ASSERT(memcached_result_cas(results
));
430 assert(!memcmp(value
, "we the people", strlen("we the people")));
431 assert(strlen("we the people") == value_length
);
432 assert(rc
== MEMCACHED_SUCCESS
);
434 memcached_result_free(&results_obj
);
439 static test_return
cas_test(memcached_st
*memc
)
442 const char *key
= "fun";
443 size_t key_length
= strlen(key
);
444 const char *value
= "we the people";
445 char* keys
[2] = { (char*)key
, NULL
};
446 size_t keylengths
[2] = { strlen(key
), 0 };
447 size_t value_length
= strlen(value
);
448 const char *value2
= "change the value";
449 size_t value2_length
= strlen(value2
);
451 memcached_result_st results_obj
;
452 memcached_result_st
*results
;
455 rc
= memcached_flush(memc
, 0);
456 assert(rc
== MEMCACHED_SUCCESS
);
458 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
460 rc
= memcached_set(memc
, key
, strlen(key
),
461 value
, strlen(value
),
462 (time_t)0, (uint32_t)0);
463 assert(rc
== MEMCACHED_SUCCESS
);
465 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
467 results
= memcached_result_create(memc
, &results_obj
);
469 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
471 assert(rc
== MEMCACHED_SUCCESS
);
472 WATCHPOINT_ASSERT(memcached_result_cas(results
));
473 assert(!memcmp(value
, memcached_result_value(results
), value_length
));
474 assert(strlen(memcached_result_value(results
)) == value_length
);
475 assert(rc
== MEMCACHED_SUCCESS
);
476 uint64_t cas
= memcached_result_cas(results
);
479 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
480 assert(rc
== MEMCACHED_END
);
481 assert(results
== NULL
);
484 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
485 assert(rc
== MEMCACHED_SUCCESS
);
488 * The item will have a new cas value, so try to set it again with the old
489 * value. This should fail!
491 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
492 assert(rc
== MEMCACHED_DATA_EXISTS
);
494 memcached_result_free(&results_obj
);
499 static test_return
prepend_test(memcached_st
*memc
)
503 char *value
= "people";
507 rc
= memcached_flush(memc
, 0);
508 assert(rc
== MEMCACHED_SUCCESS
);
510 rc
= memcached_set(memc
, key
, strlen(key
),
511 value
, strlen(value
),
512 (time_t)0, (uint32_t)0);
513 assert(rc
== MEMCACHED_SUCCESS
);
515 rc
= memcached_prepend(memc
, key
, strlen(key
),
516 "the ", strlen("the "),
517 (time_t)0, (uint32_t)0);
518 assert(rc
== MEMCACHED_SUCCESS
);
520 rc
= memcached_prepend(memc
, key
, strlen(key
),
521 "we ", strlen("we "),
522 (time_t)0, (uint32_t)0);
523 assert(rc
== MEMCACHED_SUCCESS
);
525 value
= memcached_get(memc
, key
, strlen(key
),
526 &value_length
, &flags
, &rc
);
527 assert(!memcmp(value
, "we the people", strlen("we the people")));
528 assert(strlen("we the people") == value_length
);
529 assert(rc
== MEMCACHED_SUCCESS
);
536 Set the value, then quit to make sure it is flushed.
537 Come back in and test that add fails.
539 static test_return
add_test(memcached_st
*memc
)
543 char *value
= "when we sanitize";
544 unsigned long long setting_value
;
546 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
548 rc
= memcached_set(memc
, key
, strlen(key
),
549 value
, strlen(value
),
550 (time_t)0, (uint32_t)0);
551 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
552 memcached_quit(memc
);
553 rc
= memcached_add(memc
, key
, strlen(key
),
554 value
, strlen(value
),
555 (time_t)0, (uint32_t)0);
557 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
559 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
561 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
567 ** There was a problem of leaking filedescriptors in the initial release
568 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
569 ** systems it seems that the kernel is slow on reclaiming the resources
570 ** because the connects starts to time out (the test doesn't do much
571 ** anyway, so just loop 10 iterations)
573 static test_return
add_wrapper(memcached_st
*memc
)
576 unsigned int max
= 10000;
581 for (x
= 0; x
< max
; x
++)
587 static test_return
replace_test(memcached_st
*memc
)
591 char *value
= "when we sanitize";
592 char *original
= "first we insert some data";
594 rc
= memcached_set(memc
, key
, strlen(key
),
595 original
, strlen(original
),
596 (time_t)0, (uint32_t)0);
597 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
599 rc
= memcached_replace(memc
, key
, strlen(key
),
600 value
, strlen(value
),
601 (time_t)0, (uint32_t)0);
602 assert(rc
== MEMCACHED_SUCCESS
);
607 static test_return
delete_test(memcached_st
*memc
)
611 char *value
= "when we sanitize";
613 rc
= memcached_set(memc
, key
, strlen(key
),
614 value
, strlen(value
),
615 (time_t)0, (uint32_t)0);
616 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
618 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
619 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
624 static test_return
flush_test(memcached_st
*memc
)
628 rc
= memcached_flush(memc
, 0);
629 assert(rc
== MEMCACHED_SUCCESS
);
634 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
635 memcached_server_st
*server
__attribute__((unused
)),
636 void *context
__attribute__((unused
)))
640 return MEMCACHED_SUCCESS
;
643 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
645 char *context
= "foo bad";
646 memcached_server_function callbacks
[1];
648 callbacks
[0]= server_function
;
649 memcached_server_cursor(memc
, callbacks
, context
, 1);
654 static test_return
bad_key_test(memcached_st
*memc
)
657 char *key
= "foo bad";
659 size_t string_length
;
663 size_t max_keylen
= 0xffff;
665 clone
= memcached_clone(NULL
, memc
);
668 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
669 assert(rc
== MEMCACHED_SUCCESS
);
671 /* All keys are valid in the binary protocol (except for length) */
672 if (memcached_behavior_get(clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
674 string
= memcached_get(clone
, key
, strlen(key
),
675 &string_length
, &flags
, &rc
);
676 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
677 assert(string_length
== 0);
681 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
682 assert(rc
== MEMCACHED_SUCCESS
);
683 string
= memcached_get(clone
, key
, strlen(key
),
684 &string_length
, &flags
, &rc
);
685 assert(rc
== MEMCACHED_NOTFOUND
);
686 assert(string_length
== 0);
689 /* Test multi key for bad keys */
690 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
691 size_t key_lengths
[] = { 7, 7, 7 };
693 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
694 assert(rc
== MEMCACHED_SUCCESS
);
696 rc
= memcached_mget(clone
, keys
, key_lengths
, 3);
697 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
699 rc
= memcached_mget_by_key(clone
, "foo daddy", 9, keys
, key_lengths
, 1);
700 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
704 /* The following test should be moved to the end of this function when the
705 memcached server is updated to allow max size length of the keys in the
708 rc
= memcached_callback_set(clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
709 assert(rc
== MEMCACHED_SUCCESS
);
711 char *longkey
= malloc(max_keylen
+ 1);
714 memset(longkey
, 'a', max_keylen
+ 1);
715 string
= memcached_get(clone
, longkey
, max_keylen
,
716 &string_length
, &flags
, &rc
);
717 assert(rc
== MEMCACHED_NOTFOUND
);
718 assert(string_length
== 0);
721 string
= memcached_get(clone
, longkey
, max_keylen
+ 1,
722 &string_length
, &flags
, &rc
);
723 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
724 assert(string_length
== 0);
731 /* Make sure zero length keys are marked as bad */
733 rc
= memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
734 assert(rc
== MEMCACHED_SUCCESS
);
735 string
= memcached_get(clone
, key
, 0,
736 &string_length
, &flags
, &rc
);
737 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
738 assert(string_length
== 0);
741 memcached_free(clone
);
746 #define READ_THROUGH_VALUE "set for me"
747 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
748 char *key
__attribute__((unused
)),
749 size_t key_length
__attribute__((unused
)),
750 memcached_result_st
*result
)
753 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
756 static test_return
read_through(memcached_st
*memc
)
761 size_t string_length
;
763 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
765 string
= memcached_get(memc
, key
, strlen(key
),
766 &string_length
, &flags
, &rc
);
768 assert(rc
== MEMCACHED_NOTFOUND
);
769 assert(string_length
== 0);
772 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
774 assert(rc
== MEMCACHED_SUCCESS
);
776 string
= memcached_get(memc
, key
, strlen(key
),
777 &string_length
, &flags
, &rc
);
779 assert(rc
== MEMCACHED_SUCCESS
);
780 assert(string_length
== strlen(READ_THROUGH_VALUE
));
781 assert(!strcmp(READ_THROUGH_VALUE
, string
));
784 string
= memcached_get(memc
, key
, strlen(key
),
785 &string_length
, &flags
, &rc
);
787 assert(rc
== MEMCACHED_SUCCESS
);
788 assert(string_length
== strlen(READ_THROUGH_VALUE
));
789 assert(!strcmp(READ_THROUGH_VALUE
, string
));
795 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
797 size_t key_length
__attribute__((unused
)))
801 return MEMCACHED_SUCCESS
;
804 static test_return
delete_through(memcached_st
*memc
)
806 memcached_trigger_delete_key callback
;
809 callback
= (memcached_trigger_delete_key
)delete_trigger
;
811 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
812 assert(rc
== MEMCACHED_SUCCESS
);
817 static test_return
get_test(memcached_st
*memc
)
822 size_t string_length
;
825 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
826 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
828 string
= memcached_get(memc
, key
, strlen(key
),
829 &string_length
, &flags
, &rc
);
831 assert(rc
== MEMCACHED_NOTFOUND
);
832 assert(string_length
== 0);
838 static test_return
get_test2(memcached_st
*memc
)
842 char *value
= "when we sanitize";
844 size_t string_length
;
847 rc
= memcached_set(memc
, key
, strlen(key
),
848 value
, strlen(value
),
849 (time_t)0, (uint32_t)0);
850 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
852 string
= memcached_get(memc
, key
, strlen(key
),
853 &string_length
, &flags
, &rc
);
856 assert(rc
== MEMCACHED_SUCCESS
);
857 assert(string_length
== strlen(value
));
858 assert(!memcmp(string
, value
, string_length
));
865 static test_return
set_test2(memcached_st
*memc
)
869 char *value
= "train in the brain";
870 size_t value_length
= strlen(value
);
873 for (x
= 0; x
< 10; x
++)
875 rc
= memcached_set(memc
, key
, strlen(key
),
877 (time_t)0, (uint32_t)0);
878 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
884 static test_return
set_test3(memcached_st
*memc
)
888 size_t value_length
= 8191;
891 value
= (char*)malloc(value_length
);
894 for (x
= 0; x
< value_length
; x
++)
895 value
[x
] = (char) (x
% 127);
897 /* The dump test relies on there being at least 32 items in memcached */
898 for (x
= 0; x
< 32; x
++)
902 sprintf(key
, "foo%u", x
);
904 rc
= memcached_set(memc
, key
, strlen(key
),
906 (time_t)0, (uint32_t)0);
907 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
915 static test_return
get_test3(memcached_st
*memc
)
920 size_t value_length
= 8191;
922 size_t string_length
;
926 value
= (char*)malloc(value_length
);
929 for (x
= 0; x
< value_length
; x
++)
930 value
[x
] = (char) (x
% 127);
932 rc
= memcached_set(memc
, key
, strlen(key
),
934 (time_t)0, (uint32_t)0);
935 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
937 string
= memcached_get(memc
, key
, strlen(key
),
938 &string_length
, &flags
, &rc
);
940 assert(rc
== MEMCACHED_SUCCESS
);
942 assert(string_length
== value_length
);
943 assert(!memcmp(string
, value
, string_length
));
951 static test_return
get_test4(memcached_st
*memc
)
956 size_t value_length
= 8191;
958 size_t string_length
;
962 value
= (char*)malloc(value_length
);
965 for (x
= 0; x
< value_length
; x
++)
966 value
[x
] = (char) (x
% 127);
968 rc
= memcached_set(memc
, key
, strlen(key
),
970 (time_t)0, (uint32_t)0);
971 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
973 for (x
= 0; x
< 10; x
++)
975 string
= memcached_get(memc
, key
, strlen(key
),
976 &string_length
, &flags
, &rc
);
978 assert(rc
== MEMCACHED_SUCCESS
);
980 assert(string_length
== value_length
);
981 assert(!memcmp(string
, value
, string_length
));
991 * This test verifies that memcached_read_one_response doesn't try to
992 * dereference a NIL-pointer if you issue a multi-get and don't read out all
993 * responses before you execute a storage command.
995 static test_return
get_test5(memcached_st
*memc
)
998 ** Request the same key twice, to ensure that we hash to the same server
999 ** (so that we have multiple response values queued up) ;-)
1001 char *keys
[]= { "key", "key" };
1002 size_t lengths
[]= { 3, 3 };
1006 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1007 keys
[0], lengths
[0], 0, 0);
1008 assert(rc
== MEMCACHED_SUCCESS
);
1009 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1011 memcached_result_st results_obj
;
1012 memcached_result_st
*results
;
1013 results
=memcached_result_create(memc
, &results_obj
);
1015 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1017 memcached_result_free(&results_obj
);
1019 /* Don't read out the second result, but issue a set instead.. */
1020 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1021 assert(rc
== MEMCACHED_SUCCESS
);
1023 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1024 &rlen
, &flags
, &rc
);
1025 assert(val
== NULL
);
1026 assert(rc
== MEMCACHED_NOTFOUND
);
1027 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1028 assert(val
!= NULL
);
1029 assert(rc
== MEMCACHED_SUCCESS
);
1032 return TEST_SUCCESS
;
1035 /* Do not copy the style of this code, I just access hosts to testthis function */
1036 static test_return
stats_servername_test(memcached_st
*memc
)
1038 memcached_return rc
;
1039 memcached_stat_st stat
;
1040 rc
= memcached_stat_servername(&stat
, NULL
,
1041 memc
->hosts
[0].hostname
,
1042 memc
->hosts
[0].port
);
1047 static test_return
increment_test(memcached_st
*memc
)
1049 uint64_t new_number
;
1050 memcached_return rc
;
1051 char *key
= "number";
1054 rc
= memcached_set(memc
, key
, strlen(key
),
1055 value
, strlen(value
),
1056 (time_t)0, (uint32_t)0);
1057 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1059 rc
= memcached_increment(memc
, key
, strlen(key
),
1061 assert(rc
== MEMCACHED_SUCCESS
);
1062 assert(new_number
== 1);
1064 rc
= memcached_increment(memc
, key
, strlen(key
),
1066 assert(rc
== MEMCACHED_SUCCESS
);
1067 assert(new_number
== 2);
1072 static test_return
increment_with_initial_test(memcached_st
*memc
)
1074 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1076 uint64_t new_number
;
1077 memcached_return rc
;
1078 char *key
= "number";
1079 uint64_t initial
= 0;
1081 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1082 1, initial
, 0, &new_number
);
1083 assert(rc
== MEMCACHED_SUCCESS
);
1084 assert(new_number
== initial
);
1086 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1087 1, initial
, 0, &new_number
);
1088 assert(rc
== MEMCACHED_SUCCESS
);
1089 assert(new_number
== (initial
+ 1));
1094 static test_return
decrement_test(memcached_st
*memc
)
1096 uint64_t new_number
;
1097 memcached_return rc
;
1098 char *key
= "number";
1101 rc
= memcached_set(memc
, key
, strlen(key
),
1102 value
, strlen(value
),
1103 (time_t)0, (uint32_t)0);
1104 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1106 rc
= memcached_decrement(memc
, key
, strlen(key
),
1108 assert(rc
== MEMCACHED_SUCCESS
);
1109 assert(new_number
== 2);
1111 rc
= memcached_decrement(memc
, key
, strlen(key
),
1113 assert(rc
== MEMCACHED_SUCCESS
);
1114 assert(new_number
== 1);
1119 static test_return
decrement_with_initial_test(memcached_st
*memc
)
1121 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1123 uint64_t new_number
;
1124 memcached_return rc
;
1125 char *key
= "number";
1126 uint64_t initial
= 3;
1128 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1129 1, initial
, 0, &new_number
);
1130 assert(rc
== MEMCACHED_SUCCESS
);
1131 assert(new_number
== initial
);
1133 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1134 1, initial
, 0, &new_number
);
1135 assert(rc
== MEMCACHED_SUCCESS
);
1136 assert(new_number
== (initial
- 1));
1141 static test_return
quit_test(memcached_st
*memc
)
1143 memcached_return rc
;
1145 char *value
= "sanford and sun";
1147 rc
= memcached_set(memc
, key
, strlen(key
),
1148 value
, strlen(value
),
1149 (time_t)10, (uint32_t)3);
1150 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1151 memcached_quit(memc
);
1153 rc
= memcached_set(memc
, key
, strlen(key
),
1154 value
, strlen(value
),
1155 (time_t)50, (uint32_t)9);
1156 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1161 static test_return
mget_result_test(memcached_st
*memc
)
1163 memcached_return rc
;
1164 char *keys
[]= {"fudge", "son", "food"};
1165 size_t key_length
[]= {5, 3, 4};
1168 memcached_result_st results_obj
;
1169 memcached_result_st
*results
;
1171 results
= memcached_result_create(memc
, &results_obj
);
1173 assert(&results_obj
== results
);
1175 /* We need to empty the server before continueing test */
1176 rc
= memcached_flush(memc
, 0);
1177 assert(rc
== MEMCACHED_SUCCESS
);
1179 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1180 assert(rc
== MEMCACHED_SUCCESS
);
1182 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1187 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1189 assert(rc
== MEMCACHED_END
);
1191 for (x
= 0; x
< 3; x
++)
1193 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1194 keys
[x
], key_length
[x
],
1195 (time_t)50, (uint32_t)9);
1196 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1199 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1200 assert(rc
== MEMCACHED_SUCCESS
);
1202 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1205 assert(&results_obj
== results
);
1206 assert(rc
== MEMCACHED_SUCCESS
);
1207 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1208 assert(!memcmp(memcached_result_key_value(results
),
1209 memcached_result_value(results
),
1210 memcached_result_length(results
)));
1213 memcached_result_free(&results_obj
);
1218 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1220 memcached_return rc
;
1221 char *keys
[]= {"fudge", "son", "food"};
1222 size_t key_length
[]= {5, 3, 4};
1225 memcached_result_st
*results
;
1227 /* We need to empty the server before continueing test */
1228 rc
= memcached_flush(memc
, 0);
1229 assert(rc
== MEMCACHED_SUCCESS
);
1231 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1232 assert(rc
== MEMCACHED_SUCCESS
);
1234 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1239 assert(rc
== MEMCACHED_END
);
1241 for (x
= 0; x
< 3; x
++)
1243 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1244 keys
[x
], key_length
[x
],
1245 (time_t)50, (uint32_t)9);
1246 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1249 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1250 assert(rc
== MEMCACHED_SUCCESS
);
1253 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1256 assert(rc
== MEMCACHED_SUCCESS
);
1257 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1258 assert(!memcmp(memcached_result_key_value(results
),
1259 memcached_result_value(results
),
1260 memcached_result_length(results
)));
1261 memcached_result_free(results
);
1268 /* Count the results */
1269 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1270 memcached_result_st
*result
__attribute__((unused
)),
1273 unsigned int *counter
= (unsigned int *)context
;
1275 *counter
= *counter
+ 1;
1277 return MEMCACHED_SUCCESS
;
1280 static test_return
mget_result_function(memcached_st
*memc
)
1282 memcached_return rc
;
1283 char *keys
[]= {"fudge", "son", "food"};
1284 size_t key_length
[]= {5, 3, 4};
1286 unsigned int counter
;
1287 memcached_execute_function callbacks
[1];
1289 /* We need to empty the server before continueing test */
1290 rc
= memcached_flush(memc
, 0);
1291 for (x
= 0; x
< 3; x
++)
1293 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1294 keys
[x
], key_length
[x
],
1295 (time_t)50, (uint32_t)9);
1296 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1299 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1300 assert(rc
== MEMCACHED_SUCCESS
);
1302 callbacks
[0]= &callback_counter
;
1304 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1306 assert(counter
== 3);
1311 static test_return
mget_test(memcached_st
*memc
)
1313 memcached_return rc
;
1314 char *keys
[]= {"fudge", "son", "food"};
1315 size_t key_length
[]= {5, 3, 4};
1319 char return_key
[MEMCACHED_MAX_KEY
];
1320 size_t return_key_length
;
1322 size_t return_value_length
;
1324 /* We need to empty the server before continueing test */
1325 rc
= memcached_flush(memc
, 0);
1326 assert(rc
== MEMCACHED_SUCCESS
);
1328 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1329 assert(rc
== MEMCACHED_SUCCESS
);
1331 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1332 &return_value_length
, &flags
, &rc
)) != NULL
)
1334 assert(return_value
);
1336 assert(!return_value
);
1337 assert(return_value_length
== 0);
1338 assert(rc
== MEMCACHED_END
);
1340 for (x
= 0; x
< 3; x
++)
1342 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1343 keys
[x
], key_length
[x
],
1344 (time_t)50, (uint32_t)9);
1345 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1348 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1349 assert(rc
== MEMCACHED_SUCCESS
);
1352 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1353 &return_value_length
, &flags
, &rc
)))
1355 assert(return_value
);
1356 assert(rc
== MEMCACHED_SUCCESS
);
1357 assert(return_key_length
== return_value_length
);
1358 assert(!memcmp(return_value
, return_key
, return_value_length
));
1366 static test_return
get_stats_keys(memcached_st
*memc
)
1370 memcached_stat_st stat
;
1371 memcached_return rc
;
1373 list
= memcached_stat_get_keys(memc
, &stat
, &rc
);
1374 assert(rc
== MEMCACHED_SUCCESS
);
1375 for (ptr
= list
; *ptr
; ptr
++)
1384 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1386 const char *version_string
;
1388 version_string
= memcached_lib_version();
1390 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1395 static test_return
get_stats(memcached_st
*memc
)
1400 memcached_return rc
;
1401 memcached_stat_st
*stat
;
1403 stat
= memcached_stat(memc
, NULL
, &rc
);
1404 assert(rc
== MEMCACHED_SUCCESS
);
1406 assert(rc
== MEMCACHED_SUCCESS
);
1409 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1411 list
= memcached_stat_get_keys(memc
, stat
+x
, &rc
);
1412 assert(rc
== MEMCACHED_SUCCESS
);
1413 for (ptr
= list
; *ptr
; ptr
++);
1418 memcached_stat_free(NULL
, stat
);
1423 static test_return
add_host_test(memcached_st
*memc
)
1426 memcached_server_st
*servers
;
1427 memcached_return rc
;
1428 char servername
[]= "0.example.com";
1430 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1432 assert(1 == memcached_server_list_count(servers
));
1434 for (x
= 2; x
< 20; x
++)
1436 char buffer
[SMALL_STRING_LEN
];
1438 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1439 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1441 assert(rc
== MEMCACHED_SUCCESS
);
1442 assert(x
== memcached_server_list_count(servers
));
1445 rc
= memcached_server_push(memc
, servers
);
1446 assert(rc
== MEMCACHED_SUCCESS
);
1447 rc
= memcached_server_push(memc
, servers
);
1448 assert(rc
== MEMCACHED_SUCCESS
);
1450 memcached_server_list_free(servers
);
1455 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*clone
__attribute__((unused
)))
1457 return MEMCACHED_SUCCESS
;
1460 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1462 return MEMCACHED_SUCCESS
;
1465 static test_return
callback_test(memcached_st
*memc
)
1467 /* Test User Data */
1471 memcached_return rc
;
1473 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1474 assert(rc
== MEMCACHED_SUCCESS
);
1475 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1476 assert(*test_ptr
== x
);
1479 /* Test Clone Callback */
1481 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1482 void *clone_cb_ptr
= *(void **)&clone_cb
;
1483 void *temp_function
= NULL
;
1484 memcached_return rc
;
1486 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1488 assert(rc
== MEMCACHED_SUCCESS
);
1489 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1490 assert(temp_function
== clone_cb_ptr
);
1493 /* Test Cleanup Callback */
1495 memcached_cleanup_func cleanup_cb
=
1496 (memcached_cleanup_func
)cleanup_test_callback
;
1497 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1498 void *temp_function
= NULL
;
1499 memcached_return rc
;
1501 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1503 assert(rc
== MEMCACHED_SUCCESS
);
1504 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1505 assert(temp_function
== cleanup_cb_ptr
);
1511 /* We don't test the behavior itself, we test the switches */
1512 static test_return
behavior_test(memcached_st
*memc
)
1517 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1518 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1521 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1522 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1525 set
= MEMCACHED_HASH_MD5
;
1526 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1527 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1528 assert(value
== MEMCACHED_HASH_MD5
);
1532 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1533 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1536 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1537 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1540 set
= MEMCACHED_HASH_DEFAULT
;
1541 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1542 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1543 assert(value
== MEMCACHED_HASH_DEFAULT
);
1545 set
= MEMCACHED_HASH_CRC
;
1546 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1547 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1548 assert(value
== MEMCACHED_HASH_CRC
);
1550 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1553 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1556 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1557 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1558 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1562 /* Test case provided by Cal Haldenbrand */
1563 static test_return
user_supplied_bug1(memcached_st
*memc
)
1565 unsigned int setter
= 1;
1568 unsigned long long total
= 0;
1571 char randomstuff
[6 * 1024];
1572 memcached_return rc
;
1574 memset(randomstuff
, 0, 6 * 1024);
1576 /* We just keep looking at the same values over and over */
1579 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1580 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1584 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1588 size
= (rand() % ( 5 * 1024 ) ) + 400;
1589 memset(randomstuff
, 0, 6 * 1024);
1590 assert(size
< 6 * 1024); /* Being safe here */
1592 for (j
= 0 ; j
< size
;j
++)
1593 randomstuff
[j
] = (char) (rand() % 26) + 97;
1596 sprintf(key
, "%d", x
);
1597 rc
= memcached_set(memc
, key
, strlen(key
),
1598 randomstuff
, strlen(randomstuff
), 10, 0);
1599 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1600 /* If we fail, lets try again */
1601 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1602 rc
= memcached_set(memc
, key
, strlen(key
),
1603 randomstuff
, strlen(randomstuff
), 10, 0);
1604 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1610 /* Test case provided by Cal Haldenbrand */
1611 static test_return
user_supplied_bug2(memcached_st
*memc
)
1614 unsigned int setter
;
1616 unsigned long long total
;
1619 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1620 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1622 setter
= 20 * 1024576;
1623 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1624 setter
= 20 * 1024576;
1625 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1626 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1627 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1629 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1632 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1634 memcached_return rc
= MEMCACHED_SUCCESS
;
1635 char buffer
[SMALL_STRING_LEN
];
1640 memset(buffer
, 0, SMALL_STRING_LEN
);
1642 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1643 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1644 &val_len
, &flags
, &rc
);
1645 if (rc
!= MEMCACHED_SUCCESS
)
1647 if (rc
== MEMCACHED_NOTFOUND
)
1651 WATCHPOINT_ERROR(rc
);
1665 /* Do a large mget() over all the keys we think exist */
1666 #define KEY_COUNT 3000 // * 1024576
1667 static test_return
user_supplied_bug3(memcached_st
*memc
)
1669 memcached_return rc
;
1670 unsigned int setter
;
1673 size_t key_lengths
[KEY_COUNT
];
1676 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1677 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1679 setter
= 20 * 1024576;
1680 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1681 setter
= 20 * 1024576;
1682 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1683 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1684 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1687 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1689 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1690 for (x
= 0; x
< KEY_COUNT
; x
++)
1694 snprintf(buffer
, 30, "%u", x
);
1695 keys
[x
]= strdup(buffer
);
1696 key_lengths
[x
]= strlen(keys
[x
]);
1699 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1700 assert(rc
== MEMCACHED_SUCCESS
);
1702 /* Turn this into a help function */
1704 char return_key
[MEMCACHED_MAX_KEY
];
1705 size_t return_key_length
;
1707 size_t return_value_length
;
1710 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1711 &return_value_length
, &flags
, &rc
)))
1713 assert(return_value
);
1714 assert(rc
== MEMCACHED_SUCCESS
);
1719 for (x
= 0; x
< KEY_COUNT
; x
++)
1726 /* Make sure we behave properly if server list has no values */
1727 static test_return
user_supplied_bug4(memcached_st
*memc
)
1729 memcached_return rc
;
1730 char *keys
[]= {"fudge", "son", "food"};
1731 size_t key_length
[]= {5, 3, 4};
1734 char return_key
[MEMCACHED_MAX_KEY
];
1735 size_t return_key_length
;
1737 size_t return_value_length
;
1739 /* Here we free everything before running a bunch of mget tests */
1741 memcached_server_list_free(memc
->hosts
);
1743 memc
->number_of_hosts
= 0;
1747 /* We need to empty the server before continueing test */
1748 rc
= memcached_flush(memc
, 0);
1749 assert(rc
== MEMCACHED_NO_SERVERS
);
1751 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1752 assert(rc
== MEMCACHED_NO_SERVERS
);
1754 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1755 &return_value_length
, &flags
, &rc
)) != NULL
)
1757 assert(return_value
);
1759 assert(!return_value
);
1760 assert(return_value_length
== 0);
1761 assert(rc
== MEMCACHED_NO_SERVERS
);
1763 for (x
= 0; x
< 3; x
++)
1765 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1766 keys
[x
], key_length
[x
],
1767 (time_t)50, (uint32_t)9);
1768 assert(rc
== MEMCACHED_NO_SERVERS
);
1771 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1772 assert(rc
== MEMCACHED_NO_SERVERS
);
1775 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1776 &return_value_length
, &flags
, &rc
)))
1778 assert(return_value
);
1779 assert(rc
== MEMCACHED_SUCCESS
);
1780 assert(return_key_length
== return_value_length
);
1781 assert(!memcmp(return_value
, return_key
, return_value_length
));
1789 #define VALUE_SIZE_BUG5 1048064
1790 static test_return
user_supplied_bug5(memcached_st
*memc
)
1792 memcached_return rc
;
1793 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1794 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1795 char return_key
[MEMCACHED_MAX_KEY
];
1796 size_t return_key_length
;
1798 size_t value_length
;
1802 char insert_data
[VALUE_SIZE_BUG5
];
1804 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1805 insert_data
[x
]= rand();
1807 memcached_flush(memc
, 0);
1808 value
= memcached_get(memc
, keys
[0], key_length
[0],
1809 &value_length
, &flags
, &rc
);
1810 assert(value
== NULL
);
1811 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1814 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1815 &value_length
, &flags
, &rc
)))
1819 for (x
= 0; x
< 4; x
++)
1821 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1822 insert_data
, VALUE_SIZE_BUG5
,
1823 (time_t)0, (uint32_t)0);
1824 assert(rc
== MEMCACHED_SUCCESS
);
1827 for (x
= 0; x
< 10; x
++)
1829 value
= memcached_get(memc
, keys
[0], key_length
[0],
1830 &value_length
, &flags
, &rc
);
1834 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1836 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1837 &value_length
, &flags
, &rc
)))
1848 static test_return
user_supplied_bug6(memcached_st
*memc
)
1850 memcached_return rc
;
1851 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1852 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1853 char return_key
[MEMCACHED_MAX_KEY
];
1854 size_t return_key_length
;
1856 size_t value_length
;
1860 char insert_data
[VALUE_SIZE_BUG5
];
1862 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1863 insert_data
[x
]= rand();
1865 memcached_flush(memc
, 0);
1866 value
= memcached_get(memc
, keys
[0], key_length
[0],
1867 &value_length
, &flags
, &rc
);
1868 assert(value
== NULL
);
1869 assert(rc
== MEMCACHED_NOTFOUND
);
1870 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1871 assert(rc
== MEMCACHED_SUCCESS
);
1874 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1875 &value_length
, &flags
, &rc
)))
1878 assert(rc
== MEMCACHED_END
);
1880 for (x
= 0; x
< 4; x
++)
1882 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1883 insert_data
, VALUE_SIZE_BUG5
,
1884 (time_t)0, (uint32_t)0);
1885 assert(rc
== MEMCACHED_SUCCESS
);
1888 for (x
= 0; x
< 2; x
++)
1890 value
= memcached_get(memc
, keys
[0], key_length
[0],
1891 &value_length
, &flags
, &rc
);
1895 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1896 assert(rc
== MEMCACHED_SUCCESS
);
1898 /* We test for purge of partial complete fetches */
1899 for (count
= 3; count
; count
--)
1901 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1902 &value_length
, &flags
, &rc
);
1903 assert(rc
== MEMCACHED_SUCCESS
);
1904 assert(!(memcmp(value
, insert_data
, value_length
)));
1905 assert(value_length
);
1913 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1915 memcached_return rc
;
1917 memcached_st
*clone
;
1919 memcached_server_st
*servers
;
1920 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";
1922 servers
= memcached_servers_parse(server_list
);
1925 mine
= memcached_create(NULL
);
1926 rc
= memcached_server_push(mine
, servers
);
1927 assert(rc
== MEMCACHED_SUCCESS
);
1928 memcached_server_list_free(servers
);
1931 clone
= memcached_clone(NULL
, mine
);
1933 memcached_quit(mine
);
1934 memcached_quit(clone
);
1937 memcached_free(mine
);
1938 memcached_free(clone
);
1943 /* Test flag store/retrieve */
1944 static test_return
user_supplied_bug7(memcached_st
*memc
)
1946 memcached_return rc
;
1947 char *keys
= "036790384900";
1948 size_t key_length
= strlen("036790384900");
1949 char return_key
[MEMCACHED_MAX_KEY
];
1950 size_t return_key_length
;
1952 size_t value_length
;
1955 char insert_data
[VALUE_SIZE_BUG5
];
1957 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1958 insert_data
[x
]= rand();
1960 memcached_flush(memc
, 0);
1963 rc
= memcached_set(memc
, keys
, key_length
,
1964 insert_data
, VALUE_SIZE_BUG5
,
1966 assert(rc
== MEMCACHED_SUCCESS
);
1969 value
= memcached_get(memc
, keys
, key_length
,
1970 &value_length
, &flags
, &rc
);
1971 assert(flags
== 245);
1975 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1978 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1979 &value_length
, &flags
, &rc
);
1980 assert(flags
== 245);
1988 static test_return
user_supplied_bug9(memcached_st
*memc
)
1990 memcached_return rc
;
1991 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1992 size_t key_length
[3];
1997 char return_key
[MEMCACHED_MAX_KEY
];
1998 size_t return_key_length
;
2000 size_t return_value_length
;
2003 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2004 key_length
[1]= strlen("fudge&*@#");
2005 key_length
[2]= strlen("for^#@&$not");
2008 for (x
= 0; x
< 3; x
++)
2010 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2011 keys
[x
], key_length
[x
],
2012 (time_t)50, (uint32_t)9);
2013 assert(rc
== MEMCACHED_SUCCESS
);
2016 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2017 assert(rc
== MEMCACHED_SUCCESS
);
2019 /* We need to empty the server before continueing test */
2020 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2021 &return_value_length
, &flags
, &rc
)) != NULL
)
2023 assert(return_value
);
2032 /* We are testing with aggressive timeout to get failures */
2033 static test_return
user_supplied_bug10(memcached_st
*memc
)
2037 size_t value_length
= 512;
2040 memcached_return rc
;
2041 unsigned int set
= 1;
2042 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2045 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2046 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2048 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 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
, timeout
);
2092 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2094 assert(timeout
== -1);
2096 value
= (char*)malloc(value_length
* sizeof(char));
2098 for (x
= 0; x
< value_length
; x
++)
2099 value
[x
]= (char) (x
% 127);
2101 for (x
= 1; x
<= 100000; ++x
)
2103 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2107 memcached_free(mclone
);
2113 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2115 static test_return
user_supplied_bug12(memcached_st
*memc
)
2117 memcached_return rc
;
2119 size_t value_length
;
2121 uint64_t number_value
;
2123 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2124 &value_length
, &flags
, &rc
);
2125 assert(value
== NULL
);
2126 assert(rc
== MEMCACHED_NOTFOUND
);
2128 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2131 assert(value
== NULL
);
2132 /* The binary protocol will set the key if it doesn't exist */
2133 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2134 assert(rc
== MEMCACHED_SUCCESS
);
2136 assert(rc
== MEMCACHED_NOTFOUND
);
2138 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2140 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2141 &value_length
, &flags
, &rc
);
2143 assert(rc
== MEMCACHED_SUCCESS
);
2146 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2148 assert(number_value
== 2);
2149 assert(rc
== MEMCACHED_SUCCESS
);
2155 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2156 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2158 static test_return
user_supplied_bug13(memcached_st
*memc
)
2160 char key
[] = "key34567890";
2162 memcached_return rc
;
2163 size_t overflowSize
;
2165 char commandFirst
[]= "set key34567890 0 0 ";
2166 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2167 size_t commandLength
;
2170 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2172 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2174 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2176 overflow
= malloc(testSize
);
2177 assert(overflow
!= NULL
);
2179 memset(overflow
, 'x', testSize
);
2180 rc
= memcached_set(memc
, key
, strlen(key
),
2181 overflow
, testSize
, 0, 0);
2182 assert(rc
== MEMCACHED_SUCCESS
);
2191 Test values of many different sizes
2192 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2193 set key34567890 0 0 8169 \r\n
2194 is sent followed by buffer of size 8169, followed by 8169
2196 static test_return
user_supplied_bug14(memcached_st
*memc
)
2199 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2200 memcached_return rc
;
2203 size_t value_length
= 18000;
2205 size_t string_length
;
2208 size_t current_length
;
2210 value
= (char*)malloc(value_length
);
2213 for (x
= 0; x
< value_length
; x
++)
2214 value
[x
] = (char) (x
% 127);
2216 for (current_length
= 0; current_length
< value_length
; current_length
++)
2218 rc
= memcached_set(memc
, key
, strlen(key
),
2219 value
, current_length
,
2220 (time_t)0, (uint32_t)0);
2221 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2223 string
= memcached_get(memc
, key
, strlen(key
),
2224 &string_length
, &flags
, &rc
);
2226 assert(rc
== MEMCACHED_SUCCESS
);
2227 assert(string_length
== current_length
);
2228 assert(!memcmp(string
, value
, string_length
));
2239 Look for zero length value problems
2241 static test_return
user_supplied_bug15(memcached_st
*memc
)
2244 memcached_return rc
;
2250 for (x
= 0; x
< 2; x
++)
2252 rc
= memcached_set(memc
, key
, strlen(key
),
2254 (time_t)0, (uint32_t)0);
2256 assert(rc
== MEMCACHED_SUCCESS
);
2258 value
= memcached_get(memc
, key
, strlen(key
),
2259 &length
, &flags
, &rc
);
2261 assert(rc
== MEMCACHED_SUCCESS
);
2262 assert(value
== NULL
);
2263 assert(length
== 0);
2266 value
= memcached_get(memc
, key
, strlen(key
),
2267 &length
, &flags
, &rc
);
2269 assert(rc
== MEMCACHED_SUCCESS
);
2270 assert(value
== NULL
);
2271 assert(length
== 0);
2278 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2279 static test_return
user_supplied_bug16(memcached_st
*memc
)
2281 memcached_return rc
;
2287 rc
= memcached_set(memc
, key
, strlen(key
),
2289 (time_t)0, UINT32_MAX
);
2291 assert(rc
== MEMCACHED_SUCCESS
);
2293 value
= memcached_get(memc
, key
, strlen(key
),
2294 &length
, &flags
, &rc
);
2296 assert(rc
== MEMCACHED_SUCCESS
);
2297 assert(value
== NULL
);
2298 assert(length
== 0);
2299 assert(flags
== UINT32_MAX
);
2304 /* Check the validity of chinese key*/
2305 static test_return
user_supplied_bug17(memcached_st
*memc
)
2307 memcached_return rc
;
2309 char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2314 rc
= memcached_set(memc
, key
, strlen(key
),
2315 value
, strlen(value
),
2318 assert(rc
== MEMCACHED_SUCCESS
);
2320 value2
= memcached_get(memc
, key
, strlen(key
),
2321 &length
, &flags
, &rc
);
2323 assert(length
==strlen(value
));
2324 assert(rc
== MEMCACHED_SUCCESS
);
2325 assert(memcmp(value
, value2
, length
)==0);
2335 static test_return
user_supplied_bug19(memcached_st
*memc
)
2338 memcached_server_st
*s
;
2339 memcached_return res
;
2343 m
= memcached_create(NULL
);
2344 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2345 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2347 s
= memcached_server_by_key(m
, "a", 1, &res
);
2348 memcached_server_free(s
);
2355 /* CAS test from Andei */
2356 static test_return
user_supplied_bug20(memcached_st
*memc
)
2358 memcached_return status
;
2359 memcached_result_st
*result
, result_obj
;
2361 size_t key_len
= strlen("abc");
2362 char *value
= "foobar";
2363 size_t value_len
= strlen(value
);
2365 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2367 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2368 assert(status
== MEMCACHED_SUCCESS
);
2370 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2371 assert(status
== MEMCACHED_SUCCESS
);
2373 result
= memcached_result_create(memc
, &result_obj
);
2376 memcached_result_create(memc
, &result_obj
);
2377 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2380 assert(status
== MEMCACHED_SUCCESS
);
2382 memcached_result_free(result
);
2387 #include "ketama_test_cases.h"
2388 static test_return
user_supplied_bug18(memcached_st
*trash
)
2390 memcached_return rc
;
2393 memcached_server_st
*server_pool
;
2398 memc
= memcached_create(NULL
);
2401 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2402 assert(rc
== MEMCACHED_SUCCESS
);
2404 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2407 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2408 assert(rc
== MEMCACHED_SUCCESS
);
2410 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2411 assert(value
== MEMCACHED_HASH_MD5
);
2413 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");
2414 memcached_server_push(memc
, server_pool
);
2416 /* verify that the server list was parsed okay. */
2417 assert(memc
->number_of_hosts
== 8);
2418 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2419 assert(server_pool
[0].port
== 11211);
2420 assert(server_pool
[0].weight
== 600);
2421 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2422 assert(server_pool
[2].port
== 11211);
2423 assert(server_pool
[2].weight
== 200);
2424 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2425 assert(server_pool
[7].port
== 11211);
2426 assert(server_pool
[7].weight
== 100);
2428 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2429 * us test the boundary wraparound.
2431 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2433 /* verify the standard ketama set. */
2434 for (x
= 0; x
< 99; x
++)
2436 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2437 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2438 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2441 memcached_server_list_free(server_pool
);
2442 memcached_free(memc
);
2447 static test_return
auto_eject_hosts(memcached_st
*trash
)
2451 memcached_return rc
;
2452 memcached_st
*memc
= memcached_create(NULL
);
2455 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2456 assert(rc
== MEMCACHED_SUCCESS
);
2458 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2461 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2462 assert(rc
== MEMCACHED_SUCCESS
);
2464 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2465 assert(value
== MEMCACHED_HASH_MD5
);
2467 /* server should be removed when in delay */
2468 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2469 assert(rc
== MEMCACHED_SUCCESS
);
2471 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2474 memcached_server_st
*server_pool
;
2475 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");
2476 memcached_server_push(memc
, server_pool
);
2478 /* verify that the server list was parsed okay. */
2479 assert(memc
->number_of_hosts
== 8);
2480 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2481 assert(server_pool
[0].port
== 11211);
2482 assert(server_pool
[0].weight
== 600);
2483 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2484 assert(server_pool
[2].port
== 11211);
2485 assert(server_pool
[2].weight
== 200);
2486 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2487 assert(server_pool
[7].port
== 11211);
2488 assert(server_pool
[7].weight
== 100);
2490 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2491 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2493 for (int x
= 0; x
< 99; x
++)
2495 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2496 assert(server_idx
!= 2);
2499 /* and re-added when it's back. */
2500 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2501 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2502 run_distribution(memc
);
2503 for (int x
= 0; x
< 99; x
++)
2505 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2506 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2507 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2510 memcached_server_list_free(server_pool
);
2511 memcached_free(memc
);
2513 return TEST_SUCCESS
;
2516 static test_return
result_static(memcached_st
*memc
)
2518 memcached_result_st result
;
2519 memcached_result_st
*result_ptr
;
2521 result_ptr
= memcached_result_create(memc
, &result
);
2522 assert(result
.is_allocated
== false);
2524 memcached_result_free(&result
);
2529 static test_return
result_alloc(memcached_st
*memc
)
2531 memcached_result_st
*result
;
2533 result
= memcached_result_create(memc
, NULL
);
2535 memcached_result_free(result
);
2540 static test_return
string_static_null(memcached_st
*memc
)
2542 memcached_string_st string
;
2543 memcached_string_st
*string_ptr
;
2545 string_ptr
= memcached_string_create(memc
, &string
, 0);
2546 assert(string
.is_allocated
== false);
2548 memcached_string_free(&string
);
2553 static test_return
string_alloc_null(memcached_st
*memc
)
2555 memcached_string_st
*string
;
2557 string
= memcached_string_create(memc
, NULL
, 0);
2559 memcached_string_free(string
);
2564 static test_return
string_alloc_with_size(memcached_st
*memc
)
2566 memcached_string_st
*string
;
2568 string
= memcached_string_create(memc
, NULL
, 1024);
2570 memcached_string_free(string
);
2575 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2577 memcached_string_st
*string
;
2579 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2580 assert(string
== NULL
);
2585 static test_return
string_alloc_append(memcached_st
*memc
)
2588 char buffer
[SMALL_STRING_LEN
];
2589 memcached_string_st
*string
;
2591 /* Ring the bell! */
2592 memset(buffer
, 6, SMALL_STRING_LEN
);
2594 string
= memcached_string_create(memc
, NULL
, 100);
2597 for (x
= 0; x
< 1024; x
++)
2599 memcached_return rc
;
2600 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2601 assert(rc
== MEMCACHED_SUCCESS
);
2603 memcached_string_free(string
);
2608 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2610 memcached_return rc
;
2612 char buffer
[SMALL_STRING_LEN
];
2613 memcached_string_st
*string
;
2615 /* Ring the bell! */
2616 memset(buffer
, 6, SMALL_STRING_LEN
);
2618 string
= memcached_string_create(memc
, NULL
, 100);
2621 for (x
= 0; x
< 1024; x
++)
2623 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2624 assert(rc
== MEMCACHED_SUCCESS
);
2626 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
2627 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2628 memcached_string_free(string
);
2633 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2635 pairs_free(global_pairs
);
2640 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2642 unsigned long long x
;
2643 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2644 global_count
= GLOBAL_COUNT
;
2646 for (x
= 0; x
< global_count
; x
++)
2648 global_keys
[x
]= global_pairs
[x
].key
;
2649 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2655 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2657 unsigned long long x
;
2658 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2659 global_count
= GLOBAL2_COUNT
;
2661 for (x
= 0; x
< global_count
; x
++)
2663 global_keys
[x
]= global_pairs
[x
].key
;
2664 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2670 static test_return
generate_data(memcached_st
*memc
)
2672 execute_set(memc
, global_pairs
, global_count
);
2677 static test_return
generate_data_with_stats(memcached_st
*memc
)
2679 memcached_stat_st
*stat_p
;
2680 memcached_return rc
;
2681 uint32_t host_index
= 0;
2682 execute_set(memc
, global_pairs
, global_count
);
2684 //TODO: hosts used size stats
2685 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2688 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2690 /* This test was changes so that "make test" would work properlly */
2692 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
);
2694 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
2697 memcached_stat_free(NULL
, stat_p
);
2701 static test_return
generate_buffer_data(memcached_st
*memc
)
2706 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2707 generate_data(memc
);
2712 static test_return
get_read_count(memcached_st
*memc
)
2715 memcached_return rc
;
2716 memcached_st
*clone
;
2718 clone
= memcached_clone(NULL
, memc
);
2721 memcached_server_add_with_weight(clone
, "localhost", 6666, 0);
2725 size_t return_value_length
;
2729 for (x
= count
= 0; x
< global_count
; x
++)
2731 return_value
= memcached_get(clone
, global_keys
[x
], global_keys_length
[x
],
2732 &return_value_length
, &flags
, &rc
);
2733 if (rc
== MEMCACHED_SUCCESS
)
2740 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2743 memcached_free(clone
);
2748 static test_return
get_read(memcached_st
*memc
)
2751 memcached_return rc
;
2755 size_t return_value_length
;
2758 for (x
= 0; x
< global_count
; x
++)
2760 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2761 &return_value_length
, &flags
, &rc
);
2763 assert(return_value);
2764 assert(rc == MEMCACHED_SUCCESS);
2766 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2774 static test_return
mget_read(memcached_st
*memc
)
2776 memcached_return rc
;
2778 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2779 assert(rc
== MEMCACHED_SUCCESS
);
2780 /* Turn this into a help function */
2782 char return_key
[MEMCACHED_MAX_KEY
];
2783 size_t return_key_length
;
2785 size_t return_value_length
;
2788 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2789 &return_value_length
, &flags
, &rc
)))
2791 assert(return_value
);
2792 assert(rc
== MEMCACHED_SUCCESS
);
2800 static test_return
mget_read_result(memcached_st
*memc
)
2802 memcached_return rc
;
2804 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2805 assert(rc
== MEMCACHED_SUCCESS
);
2806 /* Turn this into a help function */
2808 memcached_result_st results_obj
;
2809 memcached_result_st
*results
;
2811 results
= memcached_result_create(memc
, &results_obj
);
2813 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2816 assert(rc
== MEMCACHED_SUCCESS
);
2819 memcached_result_free(&results_obj
);
2825 static test_return
mget_read_function(memcached_st
*memc
)
2827 memcached_return rc
;
2828 unsigned int counter
;
2829 memcached_execute_function callbacks
[1];
2831 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2832 assert(rc
== MEMCACHED_SUCCESS
);
2834 callbacks
[0]= &callback_counter
;
2836 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2841 static test_return
delete_generate(memcached_st
*memc
)
2845 for (x
= 0; x
< global_count
; x
++)
2847 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2853 static test_return
delete_buffer_generate(memcached_st
*memc
)
2859 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2861 for (x
= 0; x
< global_count
; x
++)
2863 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2869 static test_return
add_host_test1(memcached_st
*memc
)
2872 memcached_return rc
;
2873 char servername
[]= "0.example.com";
2874 memcached_server_st
*servers
;
2876 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2878 assert(1 == memcached_server_list_count(servers
));
2880 for (x
= 2; x
< 20; x
++)
2882 char buffer
[SMALL_STRING_LEN
];
2884 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2885 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2887 assert(rc
== MEMCACHED_SUCCESS
);
2888 assert(x
== memcached_server_list_count(servers
));
2891 rc
= memcached_server_push(memc
, servers
);
2892 assert(rc
== MEMCACHED_SUCCESS
);
2893 rc
= memcached_server_push(memc
, servers
);
2894 assert(rc
== MEMCACHED_SUCCESS
);
2896 memcached_server_list_free(servers
);
2901 static memcached_return
pre_nonblock(memcached_st
*memc
)
2903 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2905 return MEMCACHED_SUCCESS
;
2908 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
2910 memcached_return rc
= MEMCACHED_FAILURE
;
2911 memcached_st
*clone
;
2913 clone
= memcached_clone(NULL
, memc
);
2915 // The memcached_version needs to be done on a clone, because the server
2916 // will not toggle protocol on an connection.
2917 memcached_version(clone
);
2919 if (clone
->hosts
[0].major_version
>= 1 && clone
->hosts
[0].minor_version
> 2)
2921 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2922 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2923 assert(rc
== MEMCACHED_SUCCESS
);
2924 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2927 memcached_free(clone
);
2931 static memcached_return
pre_murmur(memcached_st
*memc
)
2933 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2935 return MEMCACHED_SUCCESS
;
2938 static memcached_return
pre_jenkins(memcached_st
*memc
)
2940 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
2942 return MEMCACHED_SUCCESS
;
2946 static memcached_return
pre_md5(memcached_st
*memc
)
2948 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2950 return MEMCACHED_SUCCESS
;
2953 static memcached_return
pre_crc(memcached_st
*memc
)
2955 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2957 return MEMCACHED_SUCCESS
;
2960 static memcached_return
pre_hsieh(memcached_st
*memc
)
2962 #ifdef HAVE_HSIEH_HASH
2963 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2964 return MEMCACHED_SUCCESS
;
2967 return MEMCACHED_FAILURE
;
2971 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2973 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2975 return MEMCACHED_SUCCESS
;
2978 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2980 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2982 return MEMCACHED_SUCCESS
;
2985 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2987 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2989 return MEMCACHED_SUCCESS
;
2992 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
2994 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
2996 return MEMCACHED_SUCCESS
;
2999 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3001 memcached_return rc
;
3004 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3005 assert(rc
== MEMCACHED_SUCCESS
);
3007 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3010 return MEMCACHED_SUCCESS
;
3013 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3015 memcached_return rc
;
3018 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3019 assert(rc
== MEMCACHED_SUCCESS
);
3021 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3024 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3025 assert(rc
== MEMCACHED_SUCCESS
);
3027 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3028 assert(value
== MEMCACHED_HASH_MD5
);
3029 return MEMCACHED_SUCCESS
;
3032 static memcached_return
pre_binary(memcached_st
*memc
)
3034 memcached_return rc
= MEMCACHED_FAILURE
;
3035 memcached_st
*clone
;
3037 clone
= memcached_clone(NULL
, memc
);
3039 // The memcached_version needs to be done on a clone, because the server
3040 // will not toggle protocol on an connection.
3041 memcached_version(clone
);
3043 if (clone
->hosts
[0].major_version
>= 1 && clone
->hosts
[0].minor_version
> 2)
3045 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3046 assert(rc
== MEMCACHED_SUCCESS
);
3047 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3050 memcached_free(clone
);
3054 static memcached_return
pre_replication(memcached_st
*memc
)
3056 memcached_return rc
= MEMCACHED_FAILURE
;
3057 if (pre_binary(memc
) == MEMCACHED_SUCCESS
)
3060 * Make sure that we store the item on all servers
3061 * (master + replicas == number of servers)
3063 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3064 memc
->number_of_hosts
- 1);
3065 assert(rc
== MEMCACHED_SUCCESS
);
3066 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3072 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3077 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3079 void *ret
= malloc(size
);
3081 memset(ret
, 0xff, size
);
3086 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3088 return realloc(mem
, size
);
3091 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3093 return calloc(nelem
, size
);
3096 static memcached_return
set_prefix(memcached_st
*memc
)
3098 memcached_return rc
;
3099 const char *key
= "mine";
3102 /* Make sure be default none exists */
3103 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3104 assert(rc
== MEMCACHED_FAILURE
);
3106 /* Test a clean set */
3107 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3108 assert(rc
== MEMCACHED_SUCCESS
);
3110 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3111 assert(memcmp(value
, key
, 4) == 0);
3112 assert(rc
== MEMCACHED_SUCCESS
);
3114 /* Test that we can turn it off */
3115 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3116 assert(rc
== MEMCACHED_SUCCESS
);
3118 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3119 assert(rc
== MEMCACHED_FAILURE
);
3121 /* Now setup for main test */
3122 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3123 assert(rc
== MEMCACHED_SUCCESS
);
3125 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3126 assert(rc
== MEMCACHED_SUCCESS
);
3127 assert(memcmp(value
, key
, 4) == 0);
3129 /* Set to Zero, and then Set to something too large */
3132 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3133 assert(rc
== MEMCACHED_SUCCESS
);
3135 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3136 assert(rc
== MEMCACHED_FAILURE
);
3137 assert(value
== NULL
);
3139 /* Test a long key for failure */
3140 /* TODO, extend test to determine based on setting, what result should be */
3141 long_key
= "Thisismorethentheallottednumberofcharacters";
3142 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3143 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3144 assert(rc
== MEMCACHED_SUCCESS
);
3146 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3147 long_key
= "This is more then the allotted number of characters";
3148 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3149 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3151 /* Test for a bad prefix, but with a short key */
3152 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3153 assert(rc
== MEMCACHED_SUCCESS
);
3155 long_key
= "dog cat";
3156 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3157 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3160 return MEMCACHED_SUCCESS
;
3163 #ifdef MEMCACHED_ENABLE_DEPRECATED
3164 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3166 void *test_ptr
= NULL
;
3169 memcached_malloc_function malloc_cb
=
3170 (memcached_malloc_function
)my_malloc
;
3171 cb_ptr
= *(void **)&malloc_cb
;
3172 memcached_return rc
;
3174 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3175 assert(rc
== MEMCACHED_SUCCESS
);
3176 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3177 assert(rc
== MEMCACHED_SUCCESS
);
3178 assert(test_ptr
== cb_ptr
);
3182 memcached_realloc_function realloc_cb
=
3183 (memcached_realloc_function
)my_realloc
;
3184 cb_ptr
= *(void **)&realloc_cb
;
3185 memcached_return rc
;
3187 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3188 assert(rc
== MEMCACHED_SUCCESS
);
3189 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3190 assert(rc
== MEMCACHED_SUCCESS
);
3191 assert(test_ptr
== cb_ptr
);
3195 memcached_free_function free_cb
=
3196 (memcached_free_function
)my_free
;
3197 cb_ptr
= *(void **)&free_cb
;
3198 memcached_return rc
;
3200 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3201 assert(rc
== MEMCACHED_SUCCESS
);
3202 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3203 assert(rc
== MEMCACHED_SUCCESS
);
3204 assert(test_ptr
== cb_ptr
);
3206 return MEMCACHED_SUCCESS
;
3210 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3212 memcached_return rc
;
3213 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3214 my_realloc
, my_calloc
);
3215 assert(rc
== MEMCACHED_FAILURE
);
3217 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3218 my_realloc
, my_calloc
);
3220 memcached_malloc_function mem_malloc
;
3221 memcached_free_function mem_free
;
3222 memcached_realloc_function mem_realloc
;
3223 memcached_calloc_function mem_calloc
;
3224 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3225 &mem_realloc
, &mem_calloc
);
3227 assert(mem_malloc
== my_malloc
);
3228 assert(mem_realloc
== my_realloc
);
3229 assert(mem_calloc
== my_calloc
);
3230 assert(mem_free
== my_free
);
3232 return MEMCACHED_SUCCESS
;
3235 static memcached_return
enable_consistent(memcached_st
*memc
)
3237 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3238 memcached_hash hash
;
3239 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3240 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3241 return MEMCACHED_FAILURE
;
3243 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3244 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3246 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3247 assert(hash
== MEMCACHED_HASH_HSIEH
);
3250 return MEMCACHED_SUCCESS
;
3253 static memcached_return
enable_cas(memcached_st
*memc
)
3255 unsigned int set
= 1;
3257 memcached_version(memc
);
3259 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3260 || memc
->hosts
[0].minor_version
> 2)
3262 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3264 return MEMCACHED_SUCCESS
;
3267 return MEMCACHED_FAILURE
;
3270 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3272 memcached_version(memc
);
3274 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3275 || memc
->hosts
[0].minor_version
> 2)
3276 return MEMCACHED_SUCCESS
;
3278 return MEMCACHED_FAILURE
;
3281 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3283 memcached_return rc
;
3286 memcached_server_list_free(memc
->hosts
);
3288 memc
->number_of_hosts
= 0;
3290 if (stat("/tmp/memcached.socket", &buf
))
3291 return MEMCACHED_FAILURE
;
3293 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3298 static memcached_return
pre_nodelay(memcached_st
*memc
)
3300 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3301 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3303 return MEMCACHED_SUCCESS
;
3306 static memcached_return
pre_settimer(memcached_st
*memc
)
3308 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3309 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3311 return MEMCACHED_SUCCESS
;
3314 static memcached_return
poll_timeout(memcached_st
*memc
)
3320 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3322 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3324 assert(timeout
== 100);
3326 return MEMCACHED_SUCCESS
;
3329 static test_return
noreply_test(memcached_st
*memc
)
3331 memcached_return ret
;
3332 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3333 assert(ret
== MEMCACHED_SUCCESS
);
3334 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3335 assert(ret
== MEMCACHED_SUCCESS
);
3336 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3337 assert(ret
== MEMCACHED_SUCCESS
);
3338 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3339 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3340 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3342 for (int count
=0; count
< 5; ++count
)
3344 for (int x
=0; x
< 100; ++x
)
3347 size_t len
= sprintf(key
, "%d", x
);
3351 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3354 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3357 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3360 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3363 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3366 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3370 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3371 ** API and is _ONLY_ done this way to verify that the library works the
3372 ** way it is supposed to do!!!!
3375 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3376 no_msg
+=memc
->hosts
[x
].cursor_active
;
3378 assert(no_msg
== 0);
3379 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3382 ** Now validate that all items was set properly!
3384 for (int x
=0; x
< 100; ++x
)
3387 size_t len
= sprintf(key
, "%d", x
);
3390 char* value
=memcached_get(memc
, key
, strlen(key
),
3391 &length
, &flags
, &ret
);
3392 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3395 case 0: /* FALLTHROUGH */
3396 case 1: /* FALLTHROUGH */
3398 assert(strncmp(value
, key
, len
) == 0);
3399 assert(len
== length
);
3402 assert(length
== len
* 2);
3405 assert(length
== len
* 3);
3412 /* Try setting an illegal cas value (should not return an error to
3413 * the caller (because we don't expect a return message from the server)
3415 char* keys
[]= {"0"};
3416 size_t lengths
[]= {1};
3419 memcached_result_st results_obj
;
3420 memcached_result_st
*results
;
3421 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3422 assert(ret
== MEMCACHED_SUCCESS
);
3424 results
= memcached_result_create(memc
, &results_obj
);
3426 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3428 assert(ret
== MEMCACHED_SUCCESS
);
3429 uint64_t cas
= memcached_result_cas(results
);
3430 memcached_result_free(&results_obj
);
3432 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3433 assert(ret
== MEMCACHED_SUCCESS
);
3436 * The item will have a new cas value, so try to set it again with the old
3437 * value. This should fail!
3439 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3440 assert(ret
== MEMCACHED_SUCCESS
);
3441 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3442 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3443 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3446 return TEST_SUCCESS
;
3449 static test_return
analyzer_test(memcached_st
*memc
)
3451 memcached_return rc
;
3452 memcached_stat_st
*stat
;
3453 memcached_analysis_st
*report
;
3455 stat
= memcached_stat(memc
, NULL
, &rc
);
3456 assert(rc
== MEMCACHED_SUCCESS
);
3459 report
= memcached_analyze(memc
, stat
, &rc
);
3460 assert(rc
== MEMCACHED_SUCCESS
);
3464 memcached_stat_free(NULL
, stat
);
3466 return TEST_SUCCESS
;
3469 /* Count the objects */
3470 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3471 const char *key
__attribute__((unused
)),
3472 size_t key_length
__attribute__((unused
)),
3475 uint32_t *counter
= (uint32_t *)context
;
3477 *counter
= *counter
+ 1;
3479 return MEMCACHED_SUCCESS
;
3482 static test_return
dump_test(memcached_st
*memc
)
3484 memcached_return rc
;
3485 uint32_t counter
= 0;
3486 memcached_dump_func callbacks
[1];
3487 test_return main_rc
;
3489 callbacks
[0]= &callback_dump_counter
;
3491 /* No support for Binary protocol yet */
3492 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3493 return TEST_SUCCESS
;
3495 main_rc
= set_test3(memc
);
3497 assert (main_rc
== TEST_SUCCESS
);
3499 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3500 assert(rc
== MEMCACHED_SUCCESS
);
3502 /* We may have more then 32 if our previous flush has not completed */
3503 assert(counter
>= 32);
3505 return TEST_SUCCESS
;
3508 #ifdef HAVE_LIBMEMCACHEDUTIL
3509 static void* connection_release(void *arg
) {
3511 memcached_pool_st
* pool
;
3516 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3520 static test_return
connection_pool_test(memcached_st
*memc
)
3522 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3523 assert(pool
!= NULL
);
3524 memcached_st
* mmc
[10];
3525 memcached_return rc
;
3527 for (int x
= 0; x
< 10; ++x
) {
3528 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3529 assert(mmc
[x
] != NULL
);
3530 assert(rc
== MEMCACHED_SUCCESS
);
3533 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3534 assert(rc
== MEMCACHED_SUCCESS
);
3538 memcached_pool_st
* pool
;
3540 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3541 pthread_create(&tid
, NULL
, connection_release
, &item
);
3542 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3543 assert(rc
== MEMCACHED_SUCCESS
);
3544 pthread_join(tid
, NULL
);
3545 assert(mmc
[9] == item
.mmc
);
3546 const char *key
= "key";
3547 size_t keylen
= strlen(key
);
3549 // verify that I can do ops with all connections
3550 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3551 assert(rc
== MEMCACHED_SUCCESS
);
3553 for (unsigned int x
= 0; x
< 10; ++x
) {
3554 uint64_t number_value
;
3555 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3556 assert(rc
== MEMCACHED_SUCCESS
);
3557 assert(number_value
== (x
+1));
3561 for (int x
= 0; x
< 10; ++x
)
3562 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3564 assert(memcached_pool_destroy(pool
) == memc
);
3565 return TEST_SUCCESS
;
3569 static test_return
replication_set_test(memcached_st
*memc
)
3571 memcached_return rc
;
3572 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3573 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3575 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3576 assert(rc
== MEMCACHED_SUCCESS
);
3579 ** "bubba" should now be stored on all of our servers. We don't have an
3580 ** easy to use API to address each individual server, so I'll just iterate
3581 ** through a bunch of "master keys" and I should most likely hit all of the
3584 for (int x
= 'a'; x
<= 'z'; ++x
)
3586 char key
[2]= { [0]= (char)x
};
3589 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3591 assert(rc
== MEMCACHED_SUCCESS
);
3592 assert(val
!= NULL
);
3596 memcached_free(clone
);
3598 return TEST_SUCCESS
;
3601 static test_return
replication_get_test(memcached_st
*memc
)
3603 memcached_return rc
;
3606 * Don't do the following in your code. I am abusing the internal details
3607 * within the library, and this is not a supported interface.
3608 * This is to verify correct behavior in the library
3610 for (int host
= 0; host
< memc
->number_of_hosts
; ++host
) {
3611 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3612 clone
->hosts
[host
].port
= 0;
3614 for (int x
= 'a'; x
<= 'z'; ++x
)
3616 char key
[2]= { [0]= (char)x
};
3619 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3621 assert(rc
== MEMCACHED_SUCCESS
);
3622 assert(val
!= NULL
);
3626 memcached_free(clone
);
3629 return TEST_SUCCESS
;
3632 static test_return
replication_mget_test(memcached_st
*memc
)
3634 memcached_return rc
;
3635 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3636 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3638 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3639 size_t len
[]= { 5, 4, 4, 4 };
3641 for (int x
=0; x
< 4; ++x
)
3643 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3644 assert(rc
== MEMCACHED_SUCCESS
);
3648 * Don't do the following in your code. I am abusing the internal details
3649 * within the library, and this is not a supported interface.
3650 * This is to verify correct behavior in the library
3652 memcached_result_st result_obj
;
3653 for (int host
= 0; host
< clone
->number_of_hosts
; ++host
)
3655 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3656 clone
->hosts
[host
].port
= 0;
3658 for (int x
= 'a'; x
<= 'z'; ++x
)
3660 char key
[2]= { [0]= (char)x
};
3662 rc
= memcached_mget_by_key(clone
, key
, 1, keys
, len
, 4);
3663 assert(rc
== MEMCACHED_SUCCESS
);
3665 memcached_result_st
*results
= memcached_result_create(clone
, &result_obj
);
3669 while ((results
= memcached_fetch_result(clone
, &result_obj
, &rc
)) != NULL
)
3674 memcached_result_free(&result_obj
);
3677 memcached_free(clone
);
3680 return TEST_SUCCESS
;
3683 static test_return
replication_delete_test(memcached_st
*memc
)
3685 memcached_return rc
;
3686 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3687 /* Delete the items from all of the servers except 1 */
3688 uint64_t repl
= memcached_behavior_get(memc
,
3689 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3690 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3692 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3693 size_t len
[]= { 5, 4, 4, 4 };
3695 for (int x
=0; x
< 4; ++x
)
3697 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
3698 assert(rc
== MEMCACHED_SUCCESS
);
3702 * Don't do the following in your code. I am abusing the internal details
3703 * within the library, and this is not a supported interface.
3704 * This is to verify correct behavior in the library
3706 int hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
3707 for (int x
= 0; x
< (repl
+ 1); ++x
) {
3708 clone
->hosts
[hash
].port
= 0;
3709 if (++hash
== clone
->number_of_hosts
)
3713 memcached_result_st result_obj
;
3714 for (int host
= 0; host
< clone
->number_of_hosts
; ++host
)
3716 for (int x
= 'a'; x
<= 'z'; ++x
)
3718 char key
[2]= { [0]= (char)x
};
3720 rc
= memcached_mget_by_key(clone
, key
, 1, keys
, len
, 4);
3721 assert(rc
== MEMCACHED_SUCCESS
);
3723 memcached_result_st
*results
= memcached_result_create(clone
, &result_obj
);
3727 while ((results
= memcached_fetch_result(clone
, &result_obj
, &rc
)) != NULL
)
3732 memcached_result_free(&result_obj
);
3735 memcached_free(clone
);
3737 return TEST_SUCCESS
;
3740 static void increment_request_id(uint16_t *id
)
3743 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
3747 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
3749 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
3750 assert(ids
!= NULL
);
3753 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3754 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
3759 static test_return
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
3762 memcached_server_st
*cur_server
= memc
->hosts
;
3763 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
3765 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3767 assert(cur_server
[x
].cursor_active
== 0);
3768 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
3770 free(expected_req_ids
);
3773 return TEST_SUCCESS
;
3777 ** There is a little bit of a hack here, instead of removing
3778 ** the servers, I just set num host to 0 and them add then new udp servers
3780 static memcached_return
init_udp(memcached_st
*memc
)
3782 memcached_version(memc
);
3783 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3784 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
3785 || memc
->hosts
[0].micro_version
< 6)
3786 return MEMCACHED_FAILURE
;
3788 uint32_t num_hosts
= memc
->number_of_hosts
;
3790 memcached_server_st servers
[num_hosts
];
3791 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
3792 for (x
= 0; x
< num_hosts
; x
++)
3793 memcached_server_free(&memc
->hosts
[x
]);
3795 memc
->number_of_hosts
= 0;
3796 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
3797 for (x
= 0; x
< num_hosts
; x
++)
3799 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
3800 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3803 return MEMCACHED_SUCCESS
;
3806 static memcached_return
binary_init_udp(memcached_st
*memc
)
3809 return init_udp(memc
);
3812 /* Make sure that I cant add a tcp server to a udp client */
3813 static test_return
add_tcp_server_udp_client_test(memcached_st
*memc
)
3815 memcached_server_st server
;
3816 memcached_server_clone(&server
, &memc
->hosts
[0]);
3817 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3818 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3819 return TEST_SUCCESS
;
3822 /* Make sure that I cant add a udp server to a tcp client */
3823 static test_return
add_udp_server_tcp_client_test(memcached_st
*memc
)
3825 memcached_server_st server
;
3826 memcached_server_clone(&server
, &memc
->hosts
[0]);
3827 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3829 memcached_st tcp_client
;
3830 memcached_create(&tcp_client
);
3831 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3832 return TEST_SUCCESS
;
3835 static test_return
set_udp_behavior_test(memcached_st
*memc
)
3838 memcached_quit(memc
);
3839 memc
->number_of_hosts
= 0;
3840 run_distribution(memc
);
3841 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
3842 assert(memc
->flags
& MEM_USE_UDP
);
3843 assert(memc
->flags
& MEM_NOREPLY
);;
3845 assert(memc
->number_of_hosts
== 0);
3847 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
3848 assert(!(memc
->flags
& MEM_USE_UDP
));
3849 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
3850 assert(!(memc
->flags
& MEM_NOREPLY
));
3851 return TEST_SUCCESS
;
3854 static test_return
udp_set_test(memcached_st
*memc
)
3857 unsigned int num_iters
= 1025; //request id rolls over at 1024
3858 for (x
= 0; x
< num_iters
;x
++)
3860 memcached_return rc
;
3862 char *value
= "when we sanitize";
3863 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3864 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
3865 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3866 rc
= memcached_set(memc
, key
, strlen(key
),
3867 value
, strlen(value
),
3868 (time_t)0, (uint32_t)0);
3869 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3870 /** NB, the check below assumes that if new write_ptr is less than
3871 * the original write_ptr that we have flushed. For large payloads, this
3872 * maybe an invalid assumption, but for the small payload we have it is OK
3874 if (rc
== MEMCACHED_SUCCESS
||
3875 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3876 increment_request_id(&expected_ids
[server_key
]);
3878 if (rc
== MEMCACHED_SUCCESS
)
3880 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3884 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3885 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3887 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3889 return TEST_SUCCESS
;
3892 static test_return
udp_buffered_set_test(memcached_st
*memc
)
3894 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3895 return udp_set_test(memc
);
3898 static test_return
udp_set_too_big_test(memcached_st
*memc
)
3900 memcached_return rc
;
3902 char value
[MAX_UDP_DATAGRAM_LENGTH
];
3903 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3904 rc
= memcached_set(memc
, key
, strlen(key
),
3905 value
, MAX_UDP_DATAGRAM_LENGTH
,
3906 (time_t)0, (uint32_t)0);
3907 assert(rc
== MEMCACHED_WRITE_FAILURE
);
3908 return post_udp_op_check(memc
,expected_ids
);
3911 static test_return
udp_delete_test(memcached_st
*memc
)
3914 unsigned int num_iters
= 1025; //request id rolls over at 1024
3915 for (x
= 0; x
< num_iters
;x
++)
3917 memcached_return rc
;
3919 uint16_t *expected_ids
=get_udp_request_ids(memc
);
3920 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3921 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3922 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
3923 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3924 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3925 increment_request_id(&expected_ids
[server_key
]);
3926 if (rc
== MEMCACHED_SUCCESS
)
3927 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3930 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3931 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3933 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3935 return TEST_SUCCESS
;
3938 static test_return
udp_buffered_delete_test(memcached_st
*memc
)
3940 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3941 return udp_delete_test(memc
);
3944 static test_return
udp_verbosity_test(memcached_st
*memc
)
3946 memcached_return rc
;
3947 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3949 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
3950 increment_request_id(&expected_ids
[x
]);
3952 rc
= memcached_verbosity(memc
,3);
3953 assert(rc
== MEMCACHED_SUCCESS
);
3954 return post_udp_op_check(memc
,expected_ids
);
3957 static test_return
udp_quit_test(memcached_st
*memc
)
3959 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3960 memcached_quit(memc
);
3961 return post_udp_op_check(memc
, expected_ids
);
3964 static test_return
udp_flush_test(memcached_st
*memc
)
3966 memcached_return rc
;
3967 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3969 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
3970 increment_request_id(&expected_ids
[x
]);
3972 rc
= memcached_flush(memc
,0);
3973 assert(rc
== MEMCACHED_SUCCESS
);
3974 return post_udp_op_check(memc
,expected_ids
);
3977 static test_return
udp_incr_test(memcached_st
*memc
)
3979 memcached_return rc
;
3982 rc
= memcached_set(memc
, key
, strlen(key
),
3983 value
, strlen(value
),
3984 (time_t)0, (uint32_t)0);
3986 assert(rc
== MEMCACHED_SUCCESS
);
3987 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3988 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3989 increment_request_id(&expected_ids
[server_key
]);
3991 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
3992 assert(rc
== MEMCACHED_SUCCESS
);
3993 return post_udp_op_check(memc
, expected_ids
);
3996 static test_return
udp_decr_test(memcached_st
*memc
)
3998 memcached_return rc
;
4001 rc
= memcached_set(memc
, key
, strlen(key
),
4002 value
, strlen(value
),
4003 (time_t)0, (uint32_t)0);
4005 assert(rc
== MEMCACHED_SUCCESS
);
4006 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4007 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4008 increment_request_id(&expected_ids
[server_key
]);
4010 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4011 assert(rc
== MEMCACHED_SUCCESS
);
4012 return post_udp_op_check(memc
, expected_ids
);
4016 static test_return
udp_stat_test(memcached_st
*memc
)
4018 memcached_stat_st
* rv
= NULL
;
4019 memcached_return rc
;
4021 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4022 rv
= memcached_stat(memc
, args
, &rc
);
4024 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4025 return post_udp_op_check(memc
, expected_ids
);
4028 static test_return
udp_version_test(memcached_st
*memc
)
4030 memcached_return rc
;
4031 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4032 rc
= memcached_version(memc
);
4033 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4034 return post_udp_op_check(memc
, expected_ids
);
4037 static test_return
udp_get_test(memcached_st
*memc
)
4039 memcached_return rc
;
4042 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4043 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4044 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4045 assert(val
== NULL
);
4046 return post_udp_op_check(memc
, expected_ids
);
4049 static test_return
udp_mixed_io_test(memcached_st
*memc
)
4052 test_st mixed_io_ops
[] ={
4053 {"udp_set_test", 0, udp_set_test
},
4054 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4055 {"udp_delete_test", 0, udp_delete_test
},
4056 {"udp_verbosity_test", 0, udp_verbosity_test
},
4057 {"udp_quit_test", 0, udp_quit_test
},
4058 {"udp_flush_test", 0, udp_flush_test
},
4059 {"udp_incr_test", 0, udp_incr_test
},
4060 {"udp_decr_test", 0, udp_decr_test
},
4061 {"udp_version_test", 0, udp_version_test
}
4064 for (x
= 0; x
< 500; x
++)
4066 current_op
= mixed_io_ops
[random() % 9];
4067 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4069 return TEST_SUCCESS
;
4072 static test_return
hsieh_avaibility_test (memcached_st
*memc
)
4074 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4075 #ifdef HAVE_HSIEH_HASH
4076 expected_rc
= MEMCACHED_SUCCESS
;
4078 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4079 (uint64_t)MEMCACHED_HASH_HSIEH
);
4080 assert(rc
== expected_rc
);
4081 return TEST_SUCCESS
;
4084 static char *list
[]=
4114 static test_return
md5_run (memcached_st
*memc
__attribute__((unused
)))
4118 uint32_t values
[]= { 3195025439, 2556848621, 3724893440, 3332385401, 245758794, 2550894432,
4119 121710495, 3053817768, 1250994555, 1862072655, 2631955953, 2951528551,
4120 1451250070, 2820856945, 2060845566, 3646985608, 2138080750, 217675895,
4121 2230934345, 1234361223, 3968582726, 2455685270, 1293568479, 199067604,
4125 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4129 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4130 assert(values
[x
] == hash_val
);
4133 return TEST_SUCCESS
;
4136 static test_return
crc_run (memcached_st
*memc
__attribute__((unused
)))
4140 uint32_t values
[]= { 10542, 22009, 14526, 19510, 19432, 10199, 20634, 9369, 11511, 10362,
4141 7893, 31289, 11313, 9354, 7621, 30628, 15218, 25967, 2695, 9380,
4142 17300, 28156, 9192, 20484, 16925 };
4144 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4148 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4149 assert(values
[x
] == hash_val
);
4152 return TEST_SUCCESS
;
4155 static test_return
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4159 uint32_t values
[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
4160 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
4161 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
4162 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
4165 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4169 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4170 assert(values
[x
] == hash_val
);
4173 return TEST_SUCCESS
;
4176 static test_return
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4180 uint32_t values
[]= { 1488911807, 2500855813, 1510099634, 1390325195, 3647689787, 3241528582,
4181 1669328060, 2604311949, 734810122, 1516407546, 560948863, 1767346780,
4182 561034892, 4156330026, 3716417003, 3475297030, 1518272172, 227211583,
4183 3938128828, 126112909, 3043416448, 3131561933, 1328739897, 2455664041,
4186 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4190 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4191 assert(values
[x
] == hash_val
);
4194 return TEST_SUCCESS
;
4197 static test_return
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4201 uint32_t values
[]= { 67176023, 1190179409, 2043204404, 3221866419, 2567703427, 3787535528, 4147287986,
4202 3500475733, 344481048, 3865235296, 2181839183, 119581266, 510234242, 4248244304,
4203 1362796839, 103389328, 1449620010, 182962511, 3554262370, 3206747549, 1551306158,
4204 4127558461, 1889140833, 2774173721, 1180552018 };
4207 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4211 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4212 assert(values
[x
] == hash_val
);
4215 return TEST_SUCCESS
;
4218 static test_return
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4222 uint32_t values
[]= { 280767167, 2421315013, 3072375666, 855001899, 459261019, 3521085446, 18738364,
4223 1625305005, 2162232970, 777243802, 3323728671, 132336572, 3654473228, 260679466,
4224 1169454059, 2698319462, 1062177260, 235516991, 2218399068, 405302637, 1128467232,
4225 3579622413, 2138539289, 96429129, 2877453236 };
4227 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4231 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4232 assert(values
[x
] == hash_val
);
4235 return TEST_SUCCESS
;
4238 static test_return
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4242 #ifdef HAVE_HSIEH_HASH
4243 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4244 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4245 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4246 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4249 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 };
4252 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4256 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4257 assert(values
[x
] == hash_val
);
4260 return TEST_SUCCESS
;
4263 static test_return
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4267 uint32_t values
[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
4268 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
4269 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
4270 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
4273 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4277 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4278 assert(values
[x
] == hash_val
);
4281 return TEST_SUCCESS
;
4284 static test_return
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4288 uint32_t values
[]= { 1442444624, 4253821186, 1885058256, 2120131735, 3261968576, 3515188778,
4289 4232909173, 4288625128, 1812047395, 3689182164, 2502979932, 1214050606,
4290 2415988847, 1494268927, 1025545760, 3920481083, 4153263658, 3824871822,
4291 3072759809, 798622255, 3065432577, 1453328165, 2691550971, 3408888387,
4295 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4299 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4300 assert(values
[x
] == hash_val
);
4303 return TEST_SUCCESS
;
4306 test_st udp_setup_server_tests
[] ={
4307 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
4308 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
4309 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
4313 test_st upd_io_tests
[] ={
4314 {"udp_set_test", 0, udp_set_test
},
4315 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
4316 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4317 {"udp_delete_test", 0, udp_delete_test
},
4318 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
4319 {"udp_verbosity_test", 0, udp_verbosity_test
},
4320 {"udp_quit_test", 0, udp_quit_test
},
4321 {"udp_flush_test", 0, udp_flush_test
},
4322 {"udp_incr_test", 0, udp_incr_test
},
4323 {"udp_decr_test", 0, udp_decr_test
},
4324 {"udp_stat_test", 0, udp_stat_test
},
4325 {"udp_version_test", 0, udp_version_test
},
4326 {"udp_get_test", 0, udp_get_test
},
4327 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
4331 /* Clean the server before beginning testing */
4333 {"flush", 0, flush_test
},
4334 {"init", 0, init_test
},
4335 {"allocation", 0, allocation_test
},
4336 {"server_list_null_test", 0, server_list_null_test
},
4337 {"server_unsort", 0, server_unsort_test
},
4338 {"server_sort", 0, server_sort_test
},
4339 {"server_sort2", 0, server_sort2_test
},
4340 {"clone_test", 0, clone_test
},
4341 {"connection_test", 0, connection_test
},
4342 {"callback_test", 0, callback_test
},
4343 {"behavior_test", 0, behavior_test
},
4344 {"userdata_test", 0, userdata_test
},
4345 {"error", 0, error_test
},
4346 {"set", 0, set_test
},
4347 {"set2", 0, set_test2
},
4348 {"set3", 0, set_test3
},
4349 {"dump", 1, dump_test
},
4350 {"add", 1, add_test
},
4351 {"replace", 1, replace_test
},
4352 {"delete", 1, delete_test
},
4353 {"get", 1, get_test
},
4354 {"get2", 0, get_test2
},
4355 {"get3", 0, get_test3
},
4356 {"get4", 0, get_test4
},
4357 {"partial mget", 0, get_test5
},
4358 {"stats_servername", 0, stats_servername_test
},
4359 {"increment", 0, increment_test
},
4360 {"increment_with_initial", 1, increment_with_initial_test
},
4361 {"decrement", 0, decrement_test
},
4362 {"decrement_with_initial", 1, decrement_with_initial_test
},
4363 {"quit", 0, quit_test
},
4364 {"mget", 1, mget_test
},
4365 {"mget_result", 1, mget_result_test
},
4366 {"mget_result_alloc", 1, mget_result_alloc_test
},
4367 {"mget_result_function", 1, mget_result_function
},
4368 {"get_stats", 0, get_stats
},
4369 {"add_host_test", 0, add_host_test
},
4370 {"add_host_test_1", 0, add_host_test1
},
4371 {"get_stats_keys", 0, get_stats_keys
},
4372 {"behavior_test", 0, get_stats_keys
},
4373 {"callback_test", 0, get_stats_keys
},
4374 {"version_string_test", 0, version_string_test
},
4375 {"bad_key", 1, bad_key_test
},
4376 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
4377 {"read_through", 1, read_through
},
4378 {"delete_through", 1, delete_through
},
4379 {"noreply", 1, noreply_test
},
4380 {"analyzer", 1, analyzer_test
},
4381 #ifdef HAVE_LIBMEMCACHEDUTIL
4382 {"connectionpool", 1, connection_pool_test
},
4387 test_st async_tests
[] ={
4388 {"add", 1, add_wrapper
},
4392 test_st string_tests
[] ={
4393 {"string static with null", 0, string_static_null
},
4394 {"string alloc with null", 0, string_alloc_null
},
4395 {"string alloc with 1K", 0, string_alloc_with_size
},
4396 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
4397 {"string append", 0, string_alloc_append
},
4398 {"string append failure (too big)", 0, string_alloc_append_toobig
},
4402 test_st result_tests
[] ={
4403 {"result static", 0, result_static
},
4404 {"result alloc", 0, result_alloc
},
4408 test_st version_1_2_3
[] ={
4409 {"append", 0, append_test
},
4410 {"prepend", 0, prepend_test
},
4411 {"cas", 0, cas_test
},
4412 {"cas2", 0, cas2_test
},
4413 {"append_binary", 0, append_binary_test
},
4417 test_st user_tests
[] ={
4418 {"user_supplied_bug1", 0, user_supplied_bug1
},
4419 {"user_supplied_bug2", 0, user_supplied_bug2
},
4420 {"user_supplied_bug3", 0, user_supplied_bug3
},
4421 {"user_supplied_bug4", 0, user_supplied_bug4
},
4422 {"user_supplied_bug5", 1, user_supplied_bug5
},
4423 {"user_supplied_bug6", 1, user_supplied_bug6
},
4424 {"user_supplied_bug7", 1, user_supplied_bug7
},
4425 {"user_supplied_bug8", 1, user_supplied_bug8
},
4426 {"user_supplied_bug9", 1, user_supplied_bug9
},
4427 {"user_supplied_bug10", 1, user_supplied_bug10
},
4428 {"user_supplied_bug11", 1, user_supplied_bug11
},
4429 {"user_supplied_bug12", 1, user_supplied_bug12
},
4430 {"user_supplied_bug13", 1, user_supplied_bug13
},
4431 {"user_supplied_bug14", 1, user_supplied_bug14
},
4432 {"user_supplied_bug15", 1, user_supplied_bug15
},
4433 {"user_supplied_bug16", 1, user_supplied_bug16
},
4436 ** It seems to be something weird with the character sets..
4437 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4438 ** guess I need to find out how this is supposed to work.. Perhaps I need
4439 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4440 ** so just disable the code for now...).
4442 {"user_supplied_bug17", 1, user_supplied_bug17
},
4444 {"user_supplied_bug18", 1, user_supplied_bug18
},
4445 {"user_supplied_bug19", 1, user_supplied_bug19
},
4446 {"user_supplied_bug20", 1, user_supplied_bug20
},
4450 test_st replication_tests
[]= {
4451 {"set", 1, replication_set_test
},
4452 {"get", 0, replication_get_test
},
4453 {"mget", 0, replication_mget_test
},
4454 {"delete", 0, replication_delete_test
},
4458 test_st generate_tests
[] ={
4459 {"generate_pairs", 1, generate_pairs
},
4460 {"generate_data", 1, generate_data
},
4461 {"get_read", 0, get_read
},
4462 {"delete_generate", 0, delete_generate
},
4463 {"generate_buffer_data", 1, generate_buffer_data
},
4464 {"delete_buffer", 0, delete_buffer_generate
},
4465 {"generate_data", 1, generate_data
},
4466 {"mget_read", 0, mget_read
},
4467 {"mget_read_result", 0, mget_read_result
},
4468 {"mget_read_function", 0, mget_read_function
},
4469 {"cleanup", 1, cleanup_pairs
},
4470 {"generate_large_pairs", 1, generate_large_pairs
},
4471 {"generate_data", 1, generate_data
},
4472 {"generate_buffer_data", 1, generate_buffer_data
},
4473 {"cleanup", 1, cleanup_pairs
},
4477 test_st consistent_tests
[] ={
4478 {"generate_pairs", 1, generate_pairs
},
4479 {"generate_data", 1, generate_data
},
4480 {"get_read", 0, get_read_count
},
4481 {"cleanup", 1, cleanup_pairs
},
4485 test_st consistent_weighted_tests
[] ={
4486 {"generate_pairs", 1, generate_pairs
},
4487 {"generate_data", 1, generate_data_with_stats
},
4488 {"get_read", 0, get_read_count
},
4489 {"cleanup", 1, cleanup_pairs
},
4493 test_st hsieh_availability
[] ={
4494 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
4498 test_st ketama_auto_eject_hosts
[] ={
4499 {"auto_eject_hosts", 1, auto_eject_hosts
},
4503 test_st hash_tests
[] ={
4504 {"md5", 0, md5_run
},
4505 {"crc", 0, crc_run
},
4506 {"fnv1_64", 0, fnv1_64_run
},
4507 {"fnv1a_64", 0, fnv1a_64_run
},
4508 {"fnv1_32", 0, fnv1_32_run
},
4509 {"fnv1a_32", 0, fnv1a_32_run
},
4510 {"hsieh", 0, hsieh_run
},
4511 {"murmur", 0, murmur_run
},
4512 {"jenkis", 0, jenkins_run
},
4516 collection_st collection
[] ={
4517 {"hsieh_availability",0,0,hsieh_availability
},
4518 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
4519 {"udp_io", init_udp
, 0, upd_io_tests
},
4520 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
4521 {"block", 0, 0, tests
},
4522 {"binary", pre_binary
, 0, tests
},
4523 {"nonblock", pre_nonblock
, 0, tests
},
4524 {"nodelay", pre_nodelay
, 0, tests
},
4525 {"settimer", pre_settimer
, 0, tests
},
4526 {"md5", pre_md5
, 0, tests
},
4527 {"crc", pre_crc
, 0, tests
},
4528 {"hsieh", pre_hsieh
, 0, tests
},
4529 {"jenkins", pre_jenkins
, 0, tests
},
4530 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
4531 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
4532 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
4533 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
4534 {"ketama", pre_behavior_ketama
, 0, tests
},
4535 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
4536 {"unix_socket", pre_unix_socket
, 0, tests
},
4537 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
4538 {"poll_timeout", poll_timeout
, 0, tests
},
4539 {"gets", enable_cas
, 0, tests
},
4540 {"consistent", enable_consistent
, 0, tests
},
4541 #ifdef MEMCACHED_ENABLE_DEPRECATED
4542 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
4544 {"memory_allocators", set_memory_alloc
, 0, tests
},
4545 {"prefix", set_prefix
, 0, tests
},
4546 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
4547 {"string", 0, 0, string_tests
},
4548 {"result", 0, 0, result_tests
},
4549 {"async", pre_nonblock
, 0, async_tests
},
4550 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
4551 {"user", 0, 0, user_tests
},
4552 {"generate", 0, 0, generate_tests
},
4553 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
4554 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
4555 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
4556 {"generate_md5", pre_md5
, 0, generate_tests
},
4557 {"generate_murmur", pre_murmur
, 0, generate_tests
},
4558 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
4559 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
4560 {"consistent_not", 0, 0, consistent_tests
},
4561 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
4562 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
4563 {"test_hashes", 0, 0, hash_tests
},
4564 {"replication", pre_replication
, 0, replication_tests
},
4568 #define SERVERS_TO_CREATE 5
4570 /* Prototypes for functions we will pass to test framework */
4571 void *world_create(void);
4572 void world_destroy(void *p
);
4574 void *world_create(void)
4576 server_startup_st
*construct
;
4578 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
4579 memset(construct
, 0, sizeof(server_startup_st
));
4580 construct
->count
= SERVERS_TO_CREATE
;
4582 server_startup(construct
);
4588 void world_destroy(void *p
)
4590 server_startup_st
*construct
= (server_startup_st
*)p
;
4591 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
4592 memcached_server_list_free(servers
);
4594 server_shutdown(construct
);
4598 void get_world(world_st
*world
)
4600 world
->collections
= collection
;
4601 world
->create
= world_create
;
4602 world
->destroy
= world_destroy
;