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 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3165 void *test_ptr
= NULL
;
3168 memcached_malloc_function malloc_cb
=
3169 (memcached_malloc_function
)my_malloc
;
3170 cb_ptr
= *(void **)&malloc_cb
;
3171 memcached_return rc
;
3173 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3174 assert(rc
== MEMCACHED_SUCCESS
);
3175 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3176 assert(rc
== MEMCACHED_SUCCESS
);
3177 assert(test_ptr
== cb_ptr
);
3181 memcached_realloc_function realloc_cb
=
3182 (memcached_realloc_function
)my_realloc
;
3183 cb_ptr
= *(void **)&realloc_cb
;
3184 memcached_return rc
;
3186 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3187 assert(rc
== MEMCACHED_SUCCESS
);
3188 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3189 assert(rc
== MEMCACHED_SUCCESS
);
3190 assert(test_ptr
== cb_ptr
);
3194 memcached_free_function free_cb
=
3195 (memcached_free_function
)my_free
;
3196 cb_ptr
= *(void **)&free_cb
;
3197 memcached_return rc
;
3199 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3200 assert(rc
== MEMCACHED_SUCCESS
);
3201 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3202 assert(rc
== MEMCACHED_SUCCESS
);
3203 assert(test_ptr
== cb_ptr
);
3205 return MEMCACHED_SUCCESS
;
3208 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3210 memcached_return rc
;
3211 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3212 my_realloc
, my_calloc
);
3213 assert(rc
== MEMCACHED_FAILURE
);
3215 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3216 my_realloc
, my_calloc
);
3218 memcached_malloc_function mem_malloc
;
3219 memcached_free_function mem_free
;
3220 memcached_realloc_function mem_realloc
;
3221 memcached_calloc_function mem_calloc
;
3222 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3223 &mem_realloc
, &mem_calloc
);
3225 assert(mem_malloc
== my_malloc
);
3226 assert(mem_realloc
== my_realloc
);
3227 assert(mem_calloc
== my_calloc
);
3228 assert(mem_free
== my_free
);
3230 return MEMCACHED_SUCCESS
;
3233 static memcached_return
enable_consistent(memcached_st
*memc
)
3235 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3236 memcached_hash hash
;
3237 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3238 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3239 return MEMCACHED_FAILURE
;
3241 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3242 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3244 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3245 assert(hash
== MEMCACHED_HASH_HSIEH
);
3248 return MEMCACHED_SUCCESS
;
3251 static memcached_return
enable_cas(memcached_st
*memc
)
3253 unsigned int set
= 1;
3255 memcached_version(memc
);
3257 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3258 || memc
->hosts
[0].minor_version
> 2)
3260 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3262 return MEMCACHED_SUCCESS
;
3265 return MEMCACHED_FAILURE
;
3268 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3270 memcached_version(memc
);
3272 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3273 || memc
->hosts
[0].minor_version
> 2)
3274 return MEMCACHED_SUCCESS
;
3276 return MEMCACHED_FAILURE
;
3279 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3281 memcached_return rc
;
3284 memcached_server_list_free(memc
->hosts
);
3286 memc
->number_of_hosts
= 0;
3288 if (stat("/tmp/memcached.socket", &buf
))
3289 return MEMCACHED_FAILURE
;
3291 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3296 static memcached_return
pre_nodelay(memcached_st
*memc
)
3298 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3299 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3301 return MEMCACHED_SUCCESS
;
3304 static memcached_return
pre_settimer(memcached_st
*memc
)
3306 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3307 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3309 return MEMCACHED_SUCCESS
;
3312 static memcached_return
poll_timeout(memcached_st
*memc
)
3318 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3320 timeout
= (int32_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3322 assert(timeout
== 100);
3324 return MEMCACHED_SUCCESS
;
3327 static test_return
noreply_test(memcached_st
*memc
)
3329 memcached_return ret
;
3330 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3331 assert(ret
== MEMCACHED_SUCCESS
);
3332 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3333 assert(ret
== MEMCACHED_SUCCESS
);
3334 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3335 assert(ret
== MEMCACHED_SUCCESS
);
3336 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3337 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3338 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3340 for (int count
=0; count
< 5; ++count
)
3342 for (int x
=0; x
< 100; ++x
)
3345 size_t len
= sprintf(key
, "%d", x
);
3349 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3352 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3355 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3358 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3361 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3364 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3368 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3369 ** API and is _ONLY_ done this way to verify that the library works the
3370 ** way it is supposed to do!!!!
3373 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3374 no_msg
+=memc
->hosts
[x
].cursor_active
;
3376 assert(no_msg
== 0);
3377 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3380 ** Now validate that all items was set properly!
3382 for (int x
=0; x
< 100; ++x
)
3385 size_t len
= sprintf(key
, "%d", x
);
3388 char* value
=memcached_get(memc
, key
, strlen(key
),
3389 &length
, &flags
, &ret
);
3390 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3393 case 0: /* FALLTHROUGH */
3394 case 1: /* FALLTHROUGH */
3396 assert(strncmp(value
, key
, len
) == 0);
3397 assert(len
== length
);
3400 assert(length
== len
* 2);
3403 assert(length
== len
* 3);
3410 /* Try setting an illegal cas value (should not return an error to
3411 * the caller (because we don't expect a return message from the server)
3413 char* keys
[]= {"0"};
3414 size_t lengths
[]= {1};
3417 memcached_result_st results_obj
;
3418 memcached_result_st
*results
;
3419 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3420 assert(ret
== MEMCACHED_SUCCESS
);
3422 results
= memcached_result_create(memc
, &results_obj
);
3424 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3426 assert(ret
== MEMCACHED_SUCCESS
);
3427 uint64_t cas
= memcached_result_cas(results
);
3428 memcached_result_free(&results_obj
);
3430 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3431 assert(ret
== MEMCACHED_SUCCESS
);
3434 * The item will have a new cas value, so try to set it again with the old
3435 * value. This should fail!
3437 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3438 assert(ret
== MEMCACHED_SUCCESS
);
3439 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3440 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3441 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3444 return TEST_SUCCESS
;
3447 static test_return
analyzer_test(memcached_st
*memc
)
3449 memcached_return rc
;
3450 memcached_stat_st
*stat
;
3451 memcached_analysis_st
*report
;
3453 stat
= memcached_stat(memc
, NULL
, &rc
);
3454 assert(rc
== MEMCACHED_SUCCESS
);
3457 report
= memcached_analyze(memc
, stat
, &rc
);
3458 assert(rc
== MEMCACHED_SUCCESS
);
3462 memcached_stat_free(NULL
, stat
);
3464 return TEST_SUCCESS
;
3467 /* Count the objects */
3468 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3469 const char *key
__attribute__((unused
)),
3470 size_t key_length
__attribute__((unused
)),
3473 uint32_t *counter
= (uint32_t *)context
;
3475 *counter
= *counter
+ 1;
3477 return MEMCACHED_SUCCESS
;
3480 static test_return
dump_test(memcached_st
*memc
)
3482 memcached_return rc
;
3483 uint32_t counter
= 0;
3484 memcached_dump_func callbacks
[1];
3485 test_return main_rc
;
3487 callbacks
[0]= &callback_dump_counter
;
3489 /* No support for Binary protocol yet */
3490 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3491 return TEST_SUCCESS
;
3493 main_rc
= set_test3(memc
);
3495 assert (main_rc
== TEST_SUCCESS
);
3497 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3498 assert(rc
== MEMCACHED_SUCCESS
);
3500 /* We may have more then 32 if our previous flush has not completed */
3501 assert(counter
>= 32);
3503 return TEST_SUCCESS
;
3506 #ifdef HAVE_LIBMEMCACHEDUTIL
3507 static void* connection_release(void *arg
) {
3509 memcached_pool_st
* pool
;
3514 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3518 static test_return
connection_pool_test(memcached_st
*memc
)
3520 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3521 assert(pool
!= NULL
);
3522 memcached_st
* mmc
[10];
3523 memcached_return rc
;
3525 for (int x
= 0; x
< 10; ++x
) {
3526 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3527 assert(mmc
[x
] != NULL
);
3528 assert(rc
== MEMCACHED_SUCCESS
);
3531 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3532 assert(rc
== MEMCACHED_SUCCESS
);
3536 memcached_pool_st
* pool
;
3538 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3539 pthread_create(&tid
, NULL
, connection_release
, &item
);
3540 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3541 assert(rc
== MEMCACHED_SUCCESS
);
3542 pthread_join(tid
, NULL
);
3543 assert(mmc
[9] == item
.mmc
);
3544 const char *key
= "key";
3545 size_t keylen
= strlen(key
);
3547 // verify that I can do ops with all connections
3548 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3549 assert(rc
== MEMCACHED_SUCCESS
);
3551 for (unsigned int x
= 0; x
< 10; ++x
) {
3552 uint64_t number_value
;
3553 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3554 assert(rc
== MEMCACHED_SUCCESS
);
3555 assert(number_value
== (x
+1));
3559 for (int x
= 0; x
< 10; ++x
)
3560 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3562 assert(memcached_pool_destroy(pool
) == memc
);
3563 return TEST_SUCCESS
;
3567 static test_return
replication_set_test(memcached_st
*memc
)
3569 memcached_return rc
;
3570 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3571 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3573 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3574 assert(rc
== MEMCACHED_SUCCESS
);
3577 ** "bubba" should now be stored on all of our servers. We don't have an
3578 ** easy to use API to address each individual server, so I'll just iterate
3579 ** through a bunch of "master keys" and I should most likely hit all of the
3582 for (int x
= 'a'; x
<= 'z'; ++x
)
3584 char key
[2]= { [0]= (char)x
};
3587 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3589 assert(rc
== MEMCACHED_SUCCESS
);
3590 assert(val
!= NULL
);
3594 memcached_free(clone
);
3596 return TEST_SUCCESS
;
3599 static test_return
replication_get_test(memcached_st
*memc
)
3601 memcached_return rc
;
3604 * Don't do the following in your code. I am abusing the internal details
3605 * within the library, and this is not a supported interface.
3606 * This is to verify correct behavior in the library
3608 for (int host
= 0; host
< memc
->number_of_hosts
; ++host
) {
3609 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3610 clone
->hosts
[host
].port
= 0;
3612 for (int x
= 'a'; x
<= 'z'; ++x
)
3614 char key
[2]= { [0]= (char)x
};
3617 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3619 assert(rc
== MEMCACHED_SUCCESS
);
3620 assert(val
!= NULL
);
3624 memcached_free(clone
);
3627 return TEST_SUCCESS
;
3630 static test_return
replication_mget_test(memcached_st
*memc
)
3632 memcached_return rc
;
3633 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3634 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3636 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3637 size_t len
[]= { 5, 4, 4, 4 };
3639 for (int x
=0; x
< 4; ++x
)
3641 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3642 assert(rc
== MEMCACHED_SUCCESS
);
3646 * Don't do the following in your code. I am abusing the internal details
3647 * within the library, and this is not a supported interface.
3648 * This is to verify correct behavior in the library
3650 memcached_result_st result_obj
;
3651 for (int host
= 0; host
< clone
->number_of_hosts
; ++host
)
3653 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3654 clone
->hosts
[host
].port
= 0;
3656 for (int x
= 'a'; x
<= 'z'; ++x
)
3658 char key
[2]= { [0]= (char)x
};
3660 rc
= memcached_mget_by_key(clone
, key
, 1, keys
, len
, 4);
3661 assert(rc
== MEMCACHED_SUCCESS
);
3663 memcached_result_st
*results
= memcached_result_create(clone
, &result_obj
);
3667 while ((results
= memcached_fetch_result(clone
, &result_obj
, &rc
)) != NULL
)
3672 memcached_result_free(&result_obj
);
3675 memcached_free(clone
);
3678 return TEST_SUCCESS
;
3681 static test_return
replication_delete_test(memcached_st
*memc
)
3683 memcached_return rc
;
3684 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3685 /* Delete the items from all of the servers except 1 */
3686 uint64_t repl
= memcached_behavior_get(memc
,
3687 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3688 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3690 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3691 size_t len
[]= { 5, 4, 4, 4 };
3693 for (int x
=0; x
< 4; ++x
)
3695 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
3696 assert(rc
== MEMCACHED_SUCCESS
);
3700 * Don't do the following in your code. I am abusing the internal details
3701 * within the library, and this is not a supported interface.
3702 * This is to verify correct behavior in the library
3704 int hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
3705 for (int x
= 0; x
< (repl
+ 1); ++x
) {
3706 clone
->hosts
[hash
].port
= 0;
3707 if (++hash
== clone
->number_of_hosts
)
3711 memcached_result_st result_obj
;
3712 for (int host
= 0; host
< clone
->number_of_hosts
; ++host
)
3714 for (int x
= 'a'; x
<= 'z'; ++x
)
3716 char key
[2]= { [0]= (char)x
};
3718 rc
= memcached_mget_by_key(clone
, key
, 1, keys
, len
, 4);
3719 assert(rc
== MEMCACHED_SUCCESS
);
3721 memcached_result_st
*results
= memcached_result_create(clone
, &result_obj
);
3725 while ((results
= memcached_fetch_result(clone
, &result_obj
, &rc
)) != NULL
)
3730 memcached_result_free(&result_obj
);
3733 memcached_free(clone
);
3735 return TEST_SUCCESS
;
3738 static void increment_request_id(uint16_t *id
)
3741 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
3745 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
3747 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
3748 assert(ids
!= NULL
);
3751 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3752 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
3757 static test_return
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
3760 memcached_server_st
*cur_server
= memc
->hosts
;
3761 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
3763 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3765 assert(cur_server
[x
].cursor_active
== 0);
3766 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
3768 free(expected_req_ids
);
3771 return TEST_SUCCESS
;
3775 ** There is a little bit of a hack here, instead of removing
3776 ** the servers, I just set num host to 0 and them add then new udp servers
3778 static memcached_return
init_udp(memcached_st
*memc
)
3780 memcached_version(memc
);
3781 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3782 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
3783 || memc
->hosts
[0].micro_version
< 6)
3784 return MEMCACHED_FAILURE
;
3786 uint32_t num_hosts
= memc
->number_of_hosts
;
3788 memcached_server_st servers
[num_hosts
];
3789 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
3790 for (x
= 0; x
< num_hosts
; x
++)
3791 memcached_server_free(&memc
->hosts
[x
]);
3793 memc
->number_of_hosts
= 0;
3794 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
3795 for (x
= 0; x
< num_hosts
; x
++)
3797 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
3798 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3801 return MEMCACHED_SUCCESS
;
3804 static memcached_return
binary_init_udp(memcached_st
*memc
)
3807 return init_udp(memc
);
3810 /* Make sure that I cant add a tcp server to a udp client */
3811 static test_return
add_tcp_server_udp_client_test(memcached_st
*memc
)
3813 memcached_server_st server
;
3814 memcached_server_clone(&server
, &memc
->hosts
[0]);
3815 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3816 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3817 return TEST_SUCCESS
;
3820 /* Make sure that I cant add a udp server to a tcp client */
3821 static test_return
add_udp_server_tcp_client_test(memcached_st
*memc
)
3823 memcached_server_st server
;
3824 memcached_server_clone(&server
, &memc
->hosts
[0]);
3825 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3827 memcached_st tcp_client
;
3828 memcached_create(&tcp_client
);
3829 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3830 return TEST_SUCCESS
;
3833 static test_return
set_udp_behavior_test(memcached_st
*memc
)
3836 memcached_quit(memc
);
3837 memc
->number_of_hosts
= 0;
3838 run_distribution(memc
);
3839 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
3840 assert(memc
->flags
& MEM_USE_UDP
);
3841 assert(memc
->flags
& MEM_NOREPLY
);;
3843 assert(memc
->number_of_hosts
== 0);
3845 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
3846 assert(!(memc
->flags
& MEM_USE_UDP
));
3847 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
3848 assert(!(memc
->flags
& MEM_NOREPLY
));
3849 return TEST_SUCCESS
;
3852 static test_return
udp_set_test(memcached_st
*memc
)
3855 unsigned int num_iters
= 1025; //request id rolls over at 1024
3856 for (x
= 0; x
< num_iters
;x
++)
3858 memcached_return rc
;
3860 char *value
= "when we sanitize";
3861 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3862 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
3863 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3864 rc
= memcached_set(memc
, key
, strlen(key
),
3865 value
, strlen(value
),
3866 (time_t)0, (uint32_t)0);
3867 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3868 /** NB, the check below assumes that if new write_ptr is less than
3869 * the original write_ptr that we have flushed. For large payloads, this
3870 * maybe an invalid assumption, but for the small payload we have it is OK
3872 if (rc
== MEMCACHED_SUCCESS
||
3873 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3874 increment_request_id(&expected_ids
[server_key
]);
3876 if (rc
== MEMCACHED_SUCCESS
)
3878 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3882 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3883 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3885 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3887 return TEST_SUCCESS
;
3890 static test_return
udp_buffered_set_test(memcached_st
*memc
)
3892 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3893 return udp_set_test(memc
);
3896 static test_return
udp_set_too_big_test(memcached_st
*memc
)
3898 memcached_return rc
;
3900 char value
[MAX_UDP_DATAGRAM_LENGTH
];
3901 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3902 rc
= memcached_set(memc
, key
, strlen(key
),
3903 value
, MAX_UDP_DATAGRAM_LENGTH
,
3904 (time_t)0, (uint32_t)0);
3905 assert(rc
== MEMCACHED_WRITE_FAILURE
);
3906 return post_udp_op_check(memc
,expected_ids
);
3909 static test_return
udp_delete_test(memcached_st
*memc
)
3912 unsigned int num_iters
= 1025; //request id rolls over at 1024
3913 for (x
= 0; x
< num_iters
;x
++)
3915 memcached_return rc
;
3917 uint16_t *expected_ids
=get_udp_request_ids(memc
);
3918 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3919 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3920 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
3921 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3922 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3923 increment_request_id(&expected_ids
[server_key
]);
3924 if (rc
== MEMCACHED_SUCCESS
)
3925 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3928 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3929 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3931 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3933 return TEST_SUCCESS
;
3936 static test_return
udp_buffered_delete_test(memcached_st
*memc
)
3938 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3939 return udp_delete_test(memc
);
3942 static test_return
udp_verbosity_test(memcached_st
*memc
)
3944 memcached_return rc
;
3945 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3947 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
3948 increment_request_id(&expected_ids
[x
]);
3950 rc
= memcached_verbosity(memc
,3);
3951 assert(rc
== MEMCACHED_SUCCESS
);
3952 return post_udp_op_check(memc
,expected_ids
);
3955 static test_return
udp_quit_test(memcached_st
*memc
)
3957 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3958 memcached_quit(memc
);
3959 return post_udp_op_check(memc
, expected_ids
);
3962 static test_return
udp_flush_test(memcached_st
*memc
)
3964 memcached_return rc
;
3965 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3967 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
3968 increment_request_id(&expected_ids
[x
]);
3970 rc
= memcached_flush(memc
,0);
3971 assert(rc
== MEMCACHED_SUCCESS
);
3972 return post_udp_op_check(memc
,expected_ids
);
3975 static test_return
udp_incr_test(memcached_st
*memc
)
3977 memcached_return rc
;
3980 rc
= memcached_set(memc
, key
, strlen(key
),
3981 value
, strlen(value
),
3982 (time_t)0, (uint32_t)0);
3984 assert(rc
== MEMCACHED_SUCCESS
);
3985 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3986 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3987 increment_request_id(&expected_ids
[server_key
]);
3989 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
3990 assert(rc
== MEMCACHED_SUCCESS
);
3991 return post_udp_op_check(memc
, expected_ids
);
3994 static test_return
udp_decr_test(memcached_st
*memc
)
3996 memcached_return rc
;
3999 rc
= memcached_set(memc
, key
, strlen(key
),
4000 value
, strlen(value
),
4001 (time_t)0, (uint32_t)0);
4003 assert(rc
== MEMCACHED_SUCCESS
);
4004 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4005 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4006 increment_request_id(&expected_ids
[server_key
]);
4008 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4009 assert(rc
== MEMCACHED_SUCCESS
);
4010 return post_udp_op_check(memc
, expected_ids
);
4014 static test_return
udp_stat_test(memcached_st
*memc
)
4016 memcached_stat_st
* rv
= NULL
;
4017 memcached_return rc
;
4019 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4020 rv
= memcached_stat(memc
, args
, &rc
);
4022 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4023 return post_udp_op_check(memc
, expected_ids
);
4026 static test_return
udp_version_test(memcached_st
*memc
)
4028 memcached_return rc
;
4029 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4030 rc
= memcached_version(memc
);
4031 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4032 return post_udp_op_check(memc
, expected_ids
);
4035 static test_return
udp_get_test(memcached_st
*memc
)
4037 memcached_return rc
;
4040 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4041 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4042 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4043 assert(val
== NULL
);
4044 return post_udp_op_check(memc
, expected_ids
);
4047 static test_return
udp_mixed_io_test(memcached_st
*memc
)
4050 test_st mixed_io_ops
[] ={
4051 {"udp_set_test", 0, udp_set_test
},
4052 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4053 {"udp_delete_test", 0, udp_delete_test
},
4054 {"udp_verbosity_test", 0, udp_verbosity_test
},
4055 {"udp_quit_test", 0, udp_quit_test
},
4056 {"udp_flush_test", 0, udp_flush_test
},
4057 {"udp_incr_test", 0, udp_incr_test
},
4058 {"udp_decr_test", 0, udp_decr_test
},
4059 {"udp_version_test", 0, udp_version_test
}
4062 for (x
= 0; x
< 500; x
++)
4064 current_op
= mixed_io_ops
[random() % 9];
4065 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4067 return TEST_SUCCESS
;
4070 static test_return
hsieh_avaibility_test (memcached_st
*memc
)
4072 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4073 #ifdef HAVE_HSIEH_HASH
4074 expected_rc
= MEMCACHED_SUCCESS
;
4076 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4077 (uint64_t)MEMCACHED_HASH_HSIEH
);
4078 assert(rc
== expected_rc
);
4079 return TEST_SUCCESS
;
4082 static char *list
[]=
4112 static test_return
md5_run (memcached_st
*memc
__attribute__((unused
)))
4116 uint32_t values
[]= { 3195025439, 2556848621, 3724893440, 3332385401, 245758794, 2550894432,
4117 121710495, 3053817768, 1250994555, 1862072655, 2631955953, 2951528551,
4118 1451250070, 2820856945, 2060845566, 3646985608, 2138080750, 217675895,
4119 2230934345, 1234361223, 3968582726, 2455685270, 1293568479, 199067604,
4123 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4127 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4128 assert(values
[x
] == hash_val
);
4131 return TEST_SUCCESS
;
4134 static test_return
crc_run (memcached_st
*memc
__attribute__((unused
)))
4138 uint32_t values
[]= { 10542, 22009, 14526, 19510, 19432, 10199, 20634, 9369, 11511, 10362,
4139 7893, 31289, 11313, 9354, 7621, 30628, 15218, 25967, 2695, 9380,
4140 17300, 28156, 9192, 20484, 16925 };
4142 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4146 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4147 assert(values
[x
] == hash_val
);
4150 return TEST_SUCCESS
;
4153 static test_return
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4157 uint32_t values
[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
4158 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
4159 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
4160 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
4163 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4167 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4168 assert(values
[x
] == hash_val
);
4171 return TEST_SUCCESS
;
4174 static test_return
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4178 uint32_t values
[]= { 1488911807, 2500855813, 1510099634, 1390325195, 3647689787, 3241528582,
4179 1669328060, 2604311949, 734810122, 1516407546, 560948863, 1767346780,
4180 561034892, 4156330026, 3716417003, 3475297030, 1518272172, 227211583,
4181 3938128828, 126112909, 3043416448, 3131561933, 1328739897, 2455664041,
4184 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4188 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4189 assert(values
[x
] == hash_val
);
4192 return TEST_SUCCESS
;
4195 static test_return
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4199 uint32_t values
[]= { 67176023, 1190179409, 2043204404, 3221866419, 2567703427, 3787535528, 4147287986,
4200 3500475733, 344481048, 3865235296, 2181839183, 119581266, 510234242, 4248244304,
4201 1362796839, 103389328, 1449620010, 182962511, 3554262370, 3206747549, 1551306158,
4202 4127558461, 1889140833, 2774173721, 1180552018 };
4205 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4209 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4210 assert(values
[x
] == hash_val
);
4213 return TEST_SUCCESS
;
4216 static test_return
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4220 uint32_t values
[]= { 280767167, 2421315013, 3072375666, 855001899, 459261019, 3521085446, 18738364,
4221 1625305005, 2162232970, 777243802, 3323728671, 132336572, 3654473228, 260679466,
4222 1169454059, 2698319462, 1062177260, 235516991, 2218399068, 405302637, 1128467232,
4223 3579622413, 2138539289, 96429129, 2877453236 };
4225 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4229 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4230 assert(values
[x
] == hash_val
);
4233 return TEST_SUCCESS
;
4236 static test_return
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4240 #ifdef HAVE_HSIEH_HASH
4241 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4242 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4243 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4244 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4247 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 };
4250 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4254 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4255 assert(values
[x
] == hash_val
);
4258 return TEST_SUCCESS
;
4261 static test_return
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4265 uint32_t values
[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
4266 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
4267 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
4268 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
4271 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4275 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4276 assert(values
[x
] == hash_val
);
4279 return TEST_SUCCESS
;
4282 static test_return
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4286 uint32_t values
[]= { 1442444624, 4253821186, 1885058256, 2120131735, 3261968576, 3515188778,
4287 4232909173, 4288625128, 1812047395, 3689182164, 2502979932, 1214050606,
4288 2415988847, 1494268927, 1025545760, 3920481083, 4153263658, 3824871822,
4289 3072759809, 798622255, 3065432577, 1453328165, 2691550971, 3408888387,
4293 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4297 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4298 assert(values
[x
] == hash_val
);
4301 return TEST_SUCCESS
;
4304 test_st udp_setup_server_tests
[] ={
4305 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
4306 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
4307 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
4311 test_st upd_io_tests
[] ={
4312 {"udp_set_test", 0, udp_set_test
},
4313 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
4314 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4315 {"udp_delete_test", 0, udp_delete_test
},
4316 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
4317 {"udp_verbosity_test", 0, udp_verbosity_test
},
4318 {"udp_quit_test", 0, udp_quit_test
},
4319 {"udp_flush_test", 0, udp_flush_test
},
4320 {"udp_incr_test", 0, udp_incr_test
},
4321 {"udp_decr_test", 0, udp_decr_test
},
4322 {"udp_stat_test", 0, udp_stat_test
},
4323 {"udp_version_test", 0, udp_version_test
},
4324 {"udp_get_test", 0, udp_get_test
},
4325 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
4329 /* Clean the server before beginning testing */
4331 {"flush", 0, flush_test
},
4332 {"init", 0, init_test
},
4333 {"allocation", 0, allocation_test
},
4334 {"server_list_null_test", 0, server_list_null_test
},
4335 {"server_unsort", 0, server_unsort_test
},
4336 {"server_sort", 0, server_sort_test
},
4337 {"server_sort2", 0, server_sort2_test
},
4338 {"clone_test", 0, clone_test
},
4339 {"connection_test", 0, connection_test
},
4340 {"callback_test", 0, callback_test
},
4341 {"behavior_test", 0, behavior_test
},
4342 {"userdata_test", 0, userdata_test
},
4343 {"error", 0, error_test
},
4344 {"set", 0, set_test
},
4345 {"set2", 0, set_test2
},
4346 {"set3", 0, set_test3
},
4347 {"dump", 1, dump_test
},
4348 {"add", 1, add_test
},
4349 {"replace", 1, replace_test
},
4350 {"delete", 1, delete_test
},
4351 {"get", 1, get_test
},
4352 {"get2", 0, get_test2
},
4353 {"get3", 0, get_test3
},
4354 {"get4", 0, get_test4
},
4355 {"partial mget", 0, get_test5
},
4356 {"stats_servername", 0, stats_servername_test
},
4357 {"increment", 0, increment_test
},
4358 {"increment_with_initial", 1, increment_with_initial_test
},
4359 {"decrement", 0, decrement_test
},
4360 {"decrement_with_initial", 1, decrement_with_initial_test
},
4361 {"quit", 0, quit_test
},
4362 {"mget", 1, mget_test
},
4363 {"mget_result", 1, mget_result_test
},
4364 {"mget_result_alloc", 1, mget_result_alloc_test
},
4365 {"mget_result_function", 1, mget_result_function
},
4366 {"get_stats", 0, get_stats
},
4367 {"add_host_test", 0, add_host_test
},
4368 {"add_host_test_1", 0, add_host_test1
},
4369 {"get_stats_keys", 0, get_stats_keys
},
4370 {"behavior_test", 0, get_stats_keys
},
4371 {"callback_test", 0, get_stats_keys
},
4372 {"version_string_test", 0, version_string_test
},
4373 {"bad_key", 1, bad_key_test
},
4374 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
4375 {"read_through", 1, read_through
},
4376 {"delete_through", 1, delete_through
},
4377 {"noreply", 1, noreply_test
},
4378 {"analyzer", 1, analyzer_test
},
4379 #ifdef HAVE_LIBMEMCACHEDUTIL
4380 {"connectionpool", 1, connection_pool_test
},
4385 test_st async_tests
[] ={
4386 {"add", 1, add_wrapper
},
4390 test_st string_tests
[] ={
4391 {"string static with null", 0, string_static_null
},
4392 {"string alloc with null", 0, string_alloc_null
},
4393 {"string alloc with 1K", 0, string_alloc_with_size
},
4394 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
4395 {"string append", 0, string_alloc_append
},
4396 {"string append failure (too big)", 0, string_alloc_append_toobig
},
4400 test_st result_tests
[] ={
4401 {"result static", 0, result_static
},
4402 {"result alloc", 0, result_alloc
},
4406 test_st version_1_2_3
[] ={
4407 {"append", 0, append_test
},
4408 {"prepend", 0, prepend_test
},
4409 {"cas", 0, cas_test
},
4410 {"cas2", 0, cas2_test
},
4411 {"append_binary", 0, append_binary_test
},
4415 test_st user_tests
[] ={
4416 {"user_supplied_bug1", 0, user_supplied_bug1
},
4417 {"user_supplied_bug2", 0, user_supplied_bug2
},
4418 {"user_supplied_bug3", 0, user_supplied_bug3
},
4419 {"user_supplied_bug4", 0, user_supplied_bug4
},
4420 {"user_supplied_bug5", 1, user_supplied_bug5
},
4421 {"user_supplied_bug6", 1, user_supplied_bug6
},
4422 {"user_supplied_bug7", 1, user_supplied_bug7
},
4423 {"user_supplied_bug8", 1, user_supplied_bug8
},
4424 {"user_supplied_bug9", 1, user_supplied_bug9
},
4425 {"user_supplied_bug10", 1, user_supplied_bug10
},
4426 {"user_supplied_bug11", 1, user_supplied_bug11
},
4427 {"user_supplied_bug12", 1, user_supplied_bug12
},
4428 {"user_supplied_bug13", 1, user_supplied_bug13
},
4429 {"user_supplied_bug14", 1, user_supplied_bug14
},
4430 {"user_supplied_bug15", 1, user_supplied_bug15
},
4431 {"user_supplied_bug16", 1, user_supplied_bug16
},
4434 ** It seems to be something weird with the character sets..
4435 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4436 ** guess I need to find out how this is supposed to work.. Perhaps I need
4437 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4438 ** so just disable the code for now...).
4440 {"user_supplied_bug17", 1, user_supplied_bug17
},
4442 {"user_supplied_bug18", 1, user_supplied_bug18
},
4443 {"user_supplied_bug19", 1, user_supplied_bug19
},
4444 {"user_supplied_bug20", 1, user_supplied_bug20
},
4448 test_st replication_tests
[]= {
4449 {"set", 1, replication_set_test
},
4450 {"get", 0, replication_get_test
},
4451 {"mget", 0, replication_mget_test
},
4452 {"delete", 0, replication_delete_test
},
4456 test_st generate_tests
[] ={
4457 {"generate_pairs", 1, generate_pairs
},
4458 {"generate_data", 1, generate_data
},
4459 {"get_read", 0, get_read
},
4460 {"delete_generate", 0, delete_generate
},
4461 {"generate_buffer_data", 1, generate_buffer_data
},
4462 {"delete_buffer", 0, delete_buffer_generate
},
4463 {"generate_data", 1, generate_data
},
4464 {"mget_read", 0, mget_read
},
4465 {"mget_read_result", 0, mget_read_result
},
4466 {"mget_read_function", 0, mget_read_function
},
4467 {"cleanup", 1, cleanup_pairs
},
4468 {"generate_large_pairs", 1, generate_large_pairs
},
4469 {"generate_data", 1, generate_data
},
4470 {"generate_buffer_data", 1, generate_buffer_data
},
4471 {"cleanup", 1, cleanup_pairs
},
4475 test_st consistent_tests
[] ={
4476 {"generate_pairs", 1, generate_pairs
},
4477 {"generate_data", 1, generate_data
},
4478 {"get_read", 0, get_read_count
},
4479 {"cleanup", 1, cleanup_pairs
},
4483 test_st consistent_weighted_tests
[] ={
4484 {"generate_pairs", 1, generate_pairs
},
4485 {"generate_data", 1, generate_data_with_stats
},
4486 {"get_read", 0, get_read_count
},
4487 {"cleanup", 1, cleanup_pairs
},
4491 test_st hsieh_availability
[] ={
4492 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
4496 test_st ketama_auto_eject_hosts
[] ={
4497 {"auto_eject_hosts", 1, auto_eject_hosts
},
4501 test_st hash_tests
[] ={
4502 {"md5", 0, md5_run
},
4503 {"crc", 0, crc_run
},
4504 {"fnv1_64", 0, fnv1_64_run
},
4505 {"fnv1a_64", 0, fnv1a_64_run
},
4506 {"fnv1_32", 0, fnv1_32_run
},
4507 {"fnv1a_32", 0, fnv1a_32_run
},
4508 {"hsieh", 0, hsieh_run
},
4509 {"murmur", 0, murmur_run
},
4510 {"jenkis", 0, jenkins_run
},
4514 collection_st collection
[] ={
4515 {"hsieh_availability",0,0,hsieh_availability
},
4516 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
4517 {"udp_io", init_udp
, 0, upd_io_tests
},
4518 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
4519 {"block", 0, 0, tests
},
4520 {"binary", pre_binary
, 0, tests
},
4521 {"nonblock", pre_nonblock
, 0, tests
},
4522 {"nodelay", pre_nodelay
, 0, tests
},
4523 {"settimer", pre_settimer
, 0, tests
},
4524 {"md5", pre_md5
, 0, tests
},
4525 {"crc", pre_crc
, 0, tests
},
4526 {"hsieh", pre_hsieh
, 0, tests
},
4527 {"jenkins", pre_jenkins
, 0, tests
},
4528 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
4529 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
4530 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
4531 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
4532 {"ketama", pre_behavior_ketama
, 0, tests
},
4533 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
4534 {"unix_socket", pre_unix_socket
, 0, tests
},
4535 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
4536 {"poll_timeout", poll_timeout
, 0, tests
},
4537 {"gets", enable_cas
, 0, tests
},
4538 {"consistent", enable_consistent
, 0, tests
},
4539 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
4540 {"memory_allocators", set_memory_alloc
, 0, tests
},
4541 {"prefix", set_prefix
, 0, tests
},
4542 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
4543 {"string", 0, 0, string_tests
},
4544 {"result", 0, 0, result_tests
},
4545 {"async", pre_nonblock
, 0, async_tests
},
4546 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
4547 {"user", 0, 0, user_tests
},
4548 {"generate", 0, 0, generate_tests
},
4549 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
4550 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
4551 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
4552 {"generate_md5", pre_md5
, 0, generate_tests
},
4553 {"generate_murmur", pre_murmur
, 0, generate_tests
},
4554 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
4555 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
4556 {"consistent_not", 0, 0, consistent_tests
},
4557 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
4558 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
4559 {"test_hashes", 0, 0, hash_tests
},
4560 {"replication", pre_replication
, 0, replication_tests
},
4564 #define SERVERS_TO_CREATE 5
4566 /* Prototypes for functions we will pass to test framework */
4567 void *world_create(void);
4568 void world_destroy(void *p
);
4570 void *world_create(void)
4572 server_startup_st
*construct
;
4574 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
4575 memset(construct
, 0, sizeof(server_startup_st
));
4576 construct
->count
= SERVERS_TO_CREATE
;
4578 server_startup(construct
);
4584 void world_destroy(void *p
)
4586 server_startup_st
*construct
= (server_startup_st
*)p
;
4587 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
4588 memcached_server_list_free(servers
);
4590 server_shutdown(construct
);
4594 void get_world(world_st
*world
)
4596 world
->collections
= collection
;
4597 world
->create
= world_create
;
4598 world
->destroy
= world_destroy
;