2 Sample test application.
5 /* TODO: I'm torn about this. The file seems like a functional test, which
6 * should use only the exported API of the library. On the other hand,
7 * something needs to test some of the internals - so we're going to turn on
8 * internal symbols here... but I'd like to come up with another way to test
11 #define BUILDING_LIBMEMCACHED 1
12 #include "libmemcached/common.h"
19 #include <sys/types.h>
24 #include "clients/generator.h"
25 #include "clients/execute.h"
28 #define INT64_MAX LONG_MAX
31 #define INT32_MAX INT_MAX
37 #ifdef HAVE_LIBMEMCACHEDUTIL
39 #include "libmemcached/memcached_util.h"
42 #define GLOBAL_COUNT 10000
43 #define GLOBAL2_COUNT 100
44 #define SERVERS_TO_CREATE 5
45 static uint32_t global_count
;
47 static pairs_st
*global_pairs
;
48 static char *global_keys
[GLOBAL_COUNT
];
49 static size_t global_keys_length
[GLOBAL_COUNT
];
51 static test_return
init_test(memcached_st
*not_used
__attribute__((unused
)))
55 (void)memcached_create(&memc
);
56 memcached_free(&memc
);
61 static test_return
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
63 memcached_server_st
*server_list
;
66 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
67 assert(server_list
== NULL
);
69 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
70 assert(server_list
== NULL
);
72 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
73 assert(server_list
== NULL
);
78 #define TEST_PORT_COUNT 7
79 uint32_t test_ports
[TEST_PORT_COUNT
];
81 static memcached_return
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
84 uint32_t bigger
= *((uint32_t *)(context
));
85 assert(bigger
<= server
->port
);
86 *((uint32_t *)(context
))= server
->port
;
88 return MEMCACHED_SUCCESS
;
91 static test_return
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
94 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
96 memcached_server_function callbacks
[1];
97 memcached_st
*local_memc
;
99 local_memc
= memcached_create(NULL
);
101 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
103 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
105 test_ports
[x
]= (uint32_t)random() % 64000;
106 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
107 assert(local_memc
->number_of_hosts
== x
+ 1);
108 assert(local_memc
->hosts
[0].count
== x
+1);
109 assert(rc
== MEMCACHED_SUCCESS
);
112 callbacks
[0]= server_display_function
;
113 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
116 memcached_free(local_memc
);
121 static test_return
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
123 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
125 memcached_server_function callbacks
[1];
126 memcached_st
*local_memc
;
128 local_memc
= memcached_create(NULL
);
130 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
131 assert(rc
== MEMCACHED_SUCCESS
);
133 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
134 assert(rc
== MEMCACHED_SUCCESS
);
135 assert(local_memc
->hosts
[0].port
== 43043);
137 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
138 assert(rc
== MEMCACHED_SUCCESS
);
139 assert(local_memc
->hosts
[0].port
== 43042);
140 assert(local_memc
->hosts
[1].port
== 43043);
142 callbacks
[0]= server_display_function
;
143 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
146 memcached_free(local_memc
);
151 static memcached_return
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
154 uint32_t x
= *((uint32_t *)(context
));
156 assert(test_ports
[x
] == server
->port
);
157 *((uint32_t *)(context
))= ++x
;
159 return MEMCACHED_SUCCESS
;
162 static test_return
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
165 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
166 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
168 memcached_server_function callbacks
[1];
169 memcached_st
*local_memc
;
171 local_memc
= memcached_create(NULL
);
174 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
176 test_ports
[x
]= (uint32_t)(random() % 64000);
177 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
178 assert(local_memc
->number_of_hosts
== x
+1);
179 assert(local_memc
->hosts
[0].count
== x
+1);
180 assert(rc
== MEMCACHED_SUCCESS
);
183 callbacks
[0]= server_display_unsort_function
;
184 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
186 /* Now we sort old data! */
187 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
188 callbacks
[0]= server_display_function
;
189 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
192 memcached_free(local_memc
);
197 static test_return
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
200 memc
= memcached_create(NULL
);
202 memcached_free(memc
);
207 static test_return
clone_test(memcached_st
*memc
)
211 memcached_st
*memc_clone
;
212 memc_clone
= memcached_clone(NULL
, NULL
);
214 memcached_free(memc_clone
);
217 /* Can we init from null? */
219 memcached_st
*memc_clone
;
220 memc_clone
= memcached_clone(NULL
, memc
);
223 assert(memc_clone
->call_free
== memc
->call_free
);
224 assert(memc_clone
->call_malloc
== memc
->call_malloc
);
225 assert(memc_clone
->call_realloc
== memc
->call_realloc
);
226 assert(memc_clone
->call_calloc
== memc
->call_calloc
);
227 assert(memc_clone
->connect_timeout
== memc
->connect_timeout
);
228 assert(memc_clone
->delete_trigger
== memc
->delete_trigger
);
229 assert(memc_clone
->distribution
== memc
->distribution
);
230 assert(memc_clone
->flags
== memc
->flags
);
231 assert(memc_clone
->get_key_failure
== memc
->get_key_failure
);
232 assert(memc_clone
->hash
== memc
->hash
);
233 assert(memc_clone
->hash_continuum
== memc
->hash_continuum
);
234 assert(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
235 assert(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
236 assert(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
237 assert(memc_clone
->on_cleanup
== memc
->on_cleanup
);
238 assert(memc_clone
->on_clone
== memc
->on_clone
);
239 assert(memc_clone
->poll_timeout
== memc
->poll_timeout
);
240 assert(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
241 assert(memc_clone
->recv_size
== memc
->recv_size
);
242 assert(memc_clone
->retry_timeout
== memc
->retry_timeout
);
243 assert(memc_clone
->send_size
== memc
->send_size
);
244 assert(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
245 assert(memc_clone
->snd_timeout
== memc
->snd_timeout
);
246 assert(memc_clone
->user_data
== memc
->user_data
);
248 memcached_free(memc_clone
);
251 /* Can we init from struct? */
253 memcached_st declared_clone
;
254 memcached_st
*memc_clone
;
255 memset(&declared_clone
, 0 , sizeof(memcached_st
));
256 memc_clone
= memcached_clone(&declared_clone
, NULL
);
258 memcached_free(memc_clone
);
261 /* Can we init from struct? */
263 memcached_st declared_clone
;
264 memcached_st
*memc_clone
;
265 memset(&declared_clone
, 0 , sizeof(memcached_st
));
266 memc_clone
= memcached_clone(&declared_clone
, memc
);
268 memcached_free(memc_clone
);
274 static test_return
userdata_test(memcached_st
*memc
)
277 assert(memcached_set_user_data(memc
, foo
) == NULL
);
278 assert(memcached_get_user_data(memc
) == foo
);
279 assert(memcached_set_user_data(memc
, NULL
) == foo
);
284 static test_return
connection_test(memcached_st
*memc
)
288 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
289 assert(rc
== MEMCACHED_SUCCESS
);
294 static test_return
error_test(memcached_st
*memc
)
298 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
300 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
306 static test_return
set_test(memcached_st
*memc
)
310 char *value
= "when we sanitize";
312 rc
= memcached_set(memc
, key
, strlen(key
),
313 value
, strlen(value
),
314 (time_t)0, (uint32_t)0);
315 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
320 static test_return
append_test(memcached_st
*memc
)
328 rc
= memcached_flush(memc
, 0);
329 assert(rc
== MEMCACHED_SUCCESS
);
331 rc
= memcached_set(memc
, key
, strlen(key
),
332 value
, strlen(value
),
333 (time_t)0, (uint32_t)0);
334 assert(rc
== MEMCACHED_SUCCESS
);
336 rc
= memcached_append(memc
, key
, strlen(key
),
337 " the", strlen(" the"),
338 (time_t)0, (uint32_t)0);
339 assert(rc
== MEMCACHED_SUCCESS
);
341 rc
= memcached_append(memc
, key
, strlen(key
),
342 " people", strlen(" people"),
343 (time_t)0, (uint32_t)0);
344 assert(rc
== MEMCACHED_SUCCESS
);
346 value
= memcached_get(memc
, key
, strlen(key
),
347 &value_length
, &flags
, &rc
);
348 assert(!memcmp(value
, "we the people", strlen("we the people")));
349 assert(strlen("we the people") == value_length
);
350 assert(rc
== MEMCACHED_SUCCESS
);
356 static test_return
append_binary_test(memcached_st
*memc
)
359 char *key
= "numbers";
360 unsigned int *store_ptr
;
361 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
367 rc
= memcached_flush(memc
, 0);
368 assert(rc
== MEMCACHED_SUCCESS
);
370 rc
= memcached_set(memc
,
373 (time_t)0, (uint32_t)0);
374 assert(rc
== MEMCACHED_SUCCESS
);
376 for (x
= 0; store_list
[x
] ; x
++)
378 rc
= memcached_append(memc
,
380 (char *)&store_list
[x
], sizeof(unsigned int),
381 (time_t)0, (uint32_t)0);
382 assert(rc
== MEMCACHED_SUCCESS
);
385 value
= memcached_get(memc
, key
, strlen(key
),
386 &value_length
, &flags
, &rc
);
387 assert((value_length
== (sizeof(unsigned int) * x
)));
388 assert(rc
== MEMCACHED_SUCCESS
);
390 store_ptr
= (unsigned int *)value
;
392 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
394 assert(*store_ptr
== store_list
[x
++]);
402 static test_return
cas2_test(memcached_st
*memc
)
405 char *keys
[]= {"fudge", "son", "food"};
406 size_t key_length
[]= {5, 3, 4};
407 char *value
= "we the people";
408 size_t value_length
= strlen("we the people");
410 memcached_result_st results_obj
;
411 memcached_result_st
*results
;
414 rc
= memcached_flush(memc
, 0);
415 assert(rc
== MEMCACHED_SUCCESS
);
417 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
419 for (x
= 0; x
< 3; x
++)
421 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
422 keys
[x
], key_length
[x
],
423 (time_t)50, (uint32_t)9);
424 assert(rc
== MEMCACHED_SUCCESS
);
427 rc
= memcached_mget(memc
, keys
, key_length
, 3);
429 results
= memcached_result_create(memc
, &results_obj
);
431 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
433 assert(results
->cas
);
434 assert(rc
== MEMCACHED_SUCCESS
);
435 assert(memcached_result_cas(results
));
437 assert(!memcmp(value
, "we the people", strlen("we the people")));
438 assert(strlen("we the people") == value_length
);
439 assert(rc
== MEMCACHED_SUCCESS
);
441 memcached_result_free(&results_obj
);
446 static test_return
cas_test(memcached_st
*memc
)
449 const char *key
= "fun";
450 size_t key_length
= strlen(key
);
451 const char *value
= "we the people";
452 char* keys
[2] = { (char*)key
, NULL
};
453 size_t keylengths
[2] = { strlen(key
), 0 };
454 size_t value_length
= strlen(value
);
455 const char *value2
= "change the value";
456 size_t value2_length
= strlen(value2
);
458 memcached_result_st results_obj
;
459 memcached_result_st
*results
;
462 rc
= memcached_flush(memc
, 0);
463 assert(rc
== MEMCACHED_SUCCESS
);
465 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
467 rc
= memcached_set(memc
, key
, strlen(key
),
468 value
, strlen(value
),
469 (time_t)0, (uint32_t)0);
470 assert(rc
== MEMCACHED_SUCCESS
);
472 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
474 results
= memcached_result_create(memc
, &results_obj
);
476 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
478 assert(rc
== MEMCACHED_SUCCESS
);
479 assert(memcached_result_cas(results
));
480 assert(!memcmp(value
, memcached_result_value(results
), value_length
));
481 assert(strlen(memcached_result_value(results
)) == value_length
);
482 assert(rc
== MEMCACHED_SUCCESS
);
483 uint64_t cas
= memcached_result_cas(results
);
486 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
487 assert(rc
== MEMCACHED_END
);
488 assert(results
== NULL
);
491 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
492 assert(rc
== MEMCACHED_SUCCESS
);
495 * The item will have a new cas value, so try to set it again with the old
496 * value. This should fail!
498 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
499 assert(rc
== MEMCACHED_DATA_EXISTS
);
501 memcached_result_free(&results_obj
);
506 static test_return
prepend_test(memcached_st
*memc
)
510 char *value
= "people";
514 rc
= memcached_flush(memc
, 0);
515 assert(rc
== MEMCACHED_SUCCESS
);
517 rc
= memcached_set(memc
, key
, strlen(key
),
518 value
, strlen(value
),
519 (time_t)0, (uint32_t)0);
520 assert(rc
== MEMCACHED_SUCCESS
);
522 rc
= memcached_prepend(memc
, key
, strlen(key
),
523 "the ", strlen("the "),
524 (time_t)0, (uint32_t)0);
525 assert(rc
== MEMCACHED_SUCCESS
);
527 rc
= memcached_prepend(memc
, key
, strlen(key
),
528 "we ", strlen("we "),
529 (time_t)0, (uint32_t)0);
530 assert(rc
== MEMCACHED_SUCCESS
);
532 value
= memcached_get(memc
, key
, strlen(key
),
533 &value_length
, &flags
, &rc
);
534 assert(!memcmp(value
, "we the people", strlen("we the people")));
535 assert(strlen("we the people") == value_length
);
536 assert(rc
== MEMCACHED_SUCCESS
);
543 Set the value, then quit to make sure it is flushed.
544 Come back in and test that add fails.
546 static test_return
add_test(memcached_st
*memc
)
550 char *value
= "when we sanitize";
551 unsigned long long setting_value
;
553 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
555 rc
= memcached_set(memc
, key
, strlen(key
),
556 value
, strlen(value
),
557 (time_t)0, (uint32_t)0);
558 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
559 memcached_quit(memc
);
560 rc
= memcached_add(memc
, key
, strlen(key
),
561 value
, strlen(value
),
562 (time_t)0, (uint32_t)0);
564 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
566 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
568 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
574 ** There was a problem of leaking filedescriptors in the initial release
575 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
576 ** systems it seems that the kernel is slow on reclaiming the resources
577 ** because the connects starts to time out (the test doesn't do much
578 ** anyway, so just loop 10 iterations)
580 static test_return
add_wrapper(memcached_st
*memc
)
583 unsigned int max
= 10000;
588 for (x
= 0; x
< max
; x
++)
594 static test_return
replace_test(memcached_st
*memc
)
598 char *value
= "when we sanitize";
599 char *original
= "first we insert some data";
601 rc
= memcached_set(memc
, key
, strlen(key
),
602 original
, strlen(original
),
603 (time_t)0, (uint32_t)0);
604 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
606 rc
= memcached_replace(memc
, key
, strlen(key
),
607 value
, strlen(value
),
608 (time_t)0, (uint32_t)0);
609 assert(rc
== MEMCACHED_SUCCESS
);
614 static test_return
delete_test(memcached_st
*memc
)
618 char *value
= "when we sanitize";
620 rc
= memcached_set(memc
, key
, strlen(key
),
621 value
, strlen(value
),
622 (time_t)0, (uint32_t)0);
623 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
625 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
626 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
631 static test_return
flush_test(memcached_st
*memc
)
635 rc
= memcached_flush(memc
, 0);
636 assert(rc
== MEMCACHED_SUCCESS
);
641 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
642 memcached_server_st
*server
__attribute__((unused
)),
643 void *context
__attribute__((unused
)))
647 return MEMCACHED_SUCCESS
;
650 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
652 char *context
= "foo bad";
653 memcached_server_function callbacks
[1];
655 callbacks
[0]= server_function
;
656 memcached_server_cursor(memc
, callbacks
, context
, 1);
661 static test_return
bad_key_test(memcached_st
*memc
)
664 char *key
= "foo bad";
666 size_t string_length
;
668 memcached_st
*memc_clone
;
670 size_t max_keylen
= 0xffff;
672 memc_clone
= memcached_clone(NULL
, memc
);
675 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
676 assert(rc
== MEMCACHED_SUCCESS
);
678 /* All keys are valid in the binary protocol (except for length) */
679 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
681 string
= memcached_get(memc_clone
, key
, strlen(key
),
682 &string_length
, &flags
, &rc
);
683 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
684 assert(string_length
== 0);
688 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
689 assert(rc
== MEMCACHED_SUCCESS
);
690 string
= memcached_get(memc_clone
, key
, strlen(key
),
691 &string_length
, &flags
, &rc
);
692 assert(rc
== MEMCACHED_NOTFOUND
);
693 assert(string_length
== 0);
696 /* Test multi key for bad keys */
697 char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
698 size_t key_lengths
[] = { 7, 7, 7 };
700 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
701 assert(rc
== MEMCACHED_SUCCESS
);
703 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
704 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
706 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
707 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
711 /* The following test should be moved to the end of this function when the
712 memcached server is updated to allow max size length of the keys in the
715 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
716 assert(rc
== MEMCACHED_SUCCESS
);
718 char *longkey
= malloc(max_keylen
+ 1);
721 memset(longkey
, 'a', max_keylen
+ 1);
722 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
723 &string_length
, &flags
, &rc
);
724 assert(rc
== MEMCACHED_NOTFOUND
);
725 assert(string_length
== 0);
728 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
729 &string_length
, &flags
, &rc
);
730 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
731 assert(string_length
== 0);
738 /* Make sure zero length keys are marked as bad */
740 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
741 assert(rc
== MEMCACHED_SUCCESS
);
742 string
= memcached_get(memc_clone
, key
, 0,
743 &string_length
, &flags
, &rc
);
744 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
745 assert(string_length
== 0);
748 memcached_free(memc_clone
);
753 #define READ_THROUGH_VALUE "set for me"
754 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
755 char *key
__attribute__((unused
)),
756 size_t key_length
__attribute__((unused
)),
757 memcached_result_st
*result
)
760 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
763 static test_return
read_through(memcached_st
*memc
)
768 size_t string_length
;
770 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
772 string
= memcached_get(memc
, key
, strlen(key
),
773 &string_length
, &flags
, &rc
);
775 assert(rc
== MEMCACHED_NOTFOUND
);
776 assert(string_length
== 0);
779 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
781 assert(rc
== MEMCACHED_SUCCESS
);
783 string
= memcached_get(memc
, key
, strlen(key
),
784 &string_length
, &flags
, &rc
);
786 assert(rc
== MEMCACHED_SUCCESS
);
787 assert(string_length
== strlen(READ_THROUGH_VALUE
));
788 assert(!strcmp(READ_THROUGH_VALUE
, string
));
791 string
= memcached_get(memc
, key
, strlen(key
),
792 &string_length
, &flags
, &rc
);
794 assert(rc
== MEMCACHED_SUCCESS
);
795 assert(string_length
== strlen(READ_THROUGH_VALUE
));
796 assert(!strcmp(READ_THROUGH_VALUE
, string
));
802 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
804 size_t key_length
__attribute__((unused
)))
808 return MEMCACHED_SUCCESS
;
811 static test_return
delete_through(memcached_st
*memc
)
813 memcached_trigger_delete_key callback
;
816 callback
= (memcached_trigger_delete_key
)delete_trigger
;
818 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
819 assert(rc
== MEMCACHED_SUCCESS
);
824 static test_return
get_test(memcached_st
*memc
)
829 size_t string_length
;
832 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
833 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
835 string
= memcached_get(memc
, key
, strlen(key
),
836 &string_length
, &flags
, &rc
);
838 assert(rc
== MEMCACHED_NOTFOUND
);
839 assert(string_length
== 0);
845 static test_return
get_test2(memcached_st
*memc
)
849 char *value
= "when we sanitize";
851 size_t string_length
;
854 rc
= memcached_set(memc
, key
, strlen(key
),
855 value
, strlen(value
),
856 (time_t)0, (uint32_t)0);
857 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
859 string
= memcached_get(memc
, key
, strlen(key
),
860 &string_length
, &flags
, &rc
);
863 assert(rc
== MEMCACHED_SUCCESS
);
864 assert(string_length
== strlen(value
));
865 assert(!memcmp(string
, value
, string_length
));
872 static test_return
set_test2(memcached_st
*memc
)
876 char *value
= "train in the brain";
877 size_t value_length
= strlen(value
);
880 for (x
= 0; x
< 10; x
++)
882 rc
= memcached_set(memc
, key
, strlen(key
),
884 (time_t)0, (uint32_t)0);
885 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
891 static test_return
set_test3(memcached_st
*memc
)
895 size_t value_length
= 8191;
898 value
= (char*)malloc(value_length
);
901 for (x
= 0; x
< value_length
; x
++)
902 value
[x
] = (char) (x
% 127);
904 /* The dump test relies on there being at least 32 items in memcached */
905 for (x
= 0; x
< 32; x
++)
909 sprintf(key
, "foo%u", x
);
911 rc
= memcached_set(memc
, key
, strlen(key
),
913 (time_t)0, (uint32_t)0);
914 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
922 static test_return
get_test3(memcached_st
*memc
)
927 size_t value_length
= 8191;
929 size_t string_length
;
933 value
= (char*)malloc(value_length
);
936 for (x
= 0; x
< value_length
; x
++)
937 value
[x
] = (char) (x
% 127);
939 rc
= memcached_set(memc
, key
, strlen(key
),
941 (time_t)0, (uint32_t)0);
942 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
944 string
= memcached_get(memc
, key
, strlen(key
),
945 &string_length
, &flags
, &rc
);
947 assert(rc
== MEMCACHED_SUCCESS
);
949 assert(string_length
== value_length
);
950 assert(!memcmp(string
, value
, string_length
));
958 static test_return
get_test4(memcached_st
*memc
)
963 size_t value_length
= 8191;
965 size_t string_length
;
969 value
= (char*)malloc(value_length
);
972 for (x
= 0; x
< value_length
; x
++)
973 value
[x
] = (char) (x
% 127);
975 rc
= memcached_set(memc
, key
, strlen(key
),
977 (time_t)0, (uint32_t)0);
978 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
980 for (x
= 0; x
< 10; x
++)
982 string
= memcached_get(memc
, key
, strlen(key
),
983 &string_length
, &flags
, &rc
);
985 assert(rc
== MEMCACHED_SUCCESS
);
987 assert(string_length
== value_length
);
988 assert(!memcmp(string
, value
, string_length
));
998 * This test verifies that memcached_read_one_response doesn't try to
999 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1000 * responses before you execute a storage command.
1002 static test_return
get_test5(memcached_st
*memc
)
1005 ** Request the same key twice, to ensure that we hash to the same server
1006 ** (so that we have multiple response values queued up) ;-)
1008 char *keys
[]= { "key", "key" };
1009 size_t lengths
[]= { 3, 3 };
1013 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1014 keys
[0], lengths
[0], 0, 0);
1015 assert(rc
== MEMCACHED_SUCCESS
);
1016 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1018 memcached_result_st results_obj
;
1019 memcached_result_st
*results
;
1020 results
=memcached_result_create(memc
, &results_obj
);
1022 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1024 memcached_result_free(&results_obj
);
1026 /* Don't read out the second result, but issue a set instead.. */
1027 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1028 assert(rc
== MEMCACHED_SUCCESS
);
1030 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1031 &rlen
, &flags
, &rc
);
1032 assert(val
== NULL
);
1033 assert(rc
== MEMCACHED_NOTFOUND
);
1034 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1035 assert(val
!= NULL
);
1036 assert(rc
== MEMCACHED_SUCCESS
);
1039 return TEST_SUCCESS
;
1042 /* Do not copy the style of this code, I just access hosts to testthis function */
1043 static test_return
stats_servername_test(memcached_st
*memc
)
1045 memcached_return rc
;
1046 memcached_stat_st memc_stat
;
1047 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1048 memc
->hosts
[0].hostname
,
1049 memc
->hosts
[0].port
);
1054 static test_return
increment_test(memcached_st
*memc
)
1056 uint64_t new_number
;
1057 memcached_return rc
;
1058 char *key
= "number";
1061 rc
= memcached_set(memc
, key
, strlen(key
),
1062 value
, strlen(value
),
1063 (time_t)0, (uint32_t)0);
1064 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1066 rc
= memcached_increment(memc
, key
, strlen(key
),
1068 assert(rc
== MEMCACHED_SUCCESS
);
1069 assert(new_number
== 1);
1071 rc
= memcached_increment(memc
, key
, strlen(key
),
1073 assert(rc
== MEMCACHED_SUCCESS
);
1074 assert(new_number
== 2);
1079 static test_return
increment_with_initial_test(memcached_st
*memc
)
1081 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1083 uint64_t new_number
;
1084 memcached_return rc
;
1085 char *key
= "number";
1086 uint64_t initial
= 0;
1088 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1089 1, initial
, 0, &new_number
);
1090 assert(rc
== MEMCACHED_SUCCESS
);
1091 assert(new_number
== initial
);
1093 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1094 1, initial
, 0, &new_number
);
1095 assert(rc
== MEMCACHED_SUCCESS
);
1096 assert(new_number
== (initial
+ 1));
1101 static test_return
decrement_test(memcached_st
*memc
)
1103 uint64_t new_number
;
1104 memcached_return rc
;
1105 char *key
= "number";
1108 rc
= memcached_set(memc
, key
, strlen(key
),
1109 value
, strlen(value
),
1110 (time_t)0, (uint32_t)0);
1111 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1113 rc
= memcached_decrement(memc
, key
, strlen(key
),
1115 assert(rc
== MEMCACHED_SUCCESS
);
1116 assert(new_number
== 2);
1118 rc
= memcached_decrement(memc
, key
, strlen(key
),
1120 assert(rc
== MEMCACHED_SUCCESS
);
1121 assert(new_number
== 1);
1126 static test_return
decrement_with_initial_test(memcached_st
*memc
)
1128 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1130 uint64_t new_number
;
1131 memcached_return rc
;
1132 char *key
= "number";
1133 uint64_t initial
= 3;
1135 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1136 1, initial
, 0, &new_number
);
1137 assert(rc
== MEMCACHED_SUCCESS
);
1138 assert(new_number
== initial
);
1140 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1141 1, initial
, 0, &new_number
);
1142 assert(rc
== MEMCACHED_SUCCESS
);
1143 assert(new_number
== (initial
- 1));
1148 static test_return
quit_test(memcached_st
*memc
)
1150 memcached_return rc
;
1152 char *value
= "sanford and sun";
1154 rc
= memcached_set(memc
, key
, strlen(key
),
1155 value
, strlen(value
),
1156 (time_t)10, (uint32_t)3);
1157 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1158 memcached_quit(memc
);
1160 rc
= memcached_set(memc
, key
, strlen(key
),
1161 value
, strlen(value
),
1162 (time_t)50, (uint32_t)9);
1163 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1168 static test_return
mget_result_test(memcached_st
*memc
)
1170 memcached_return rc
;
1171 char *keys
[]= {"fudge", "son", "food"};
1172 size_t key_length
[]= {5, 3, 4};
1175 memcached_result_st results_obj
;
1176 memcached_result_st
*results
;
1178 results
= memcached_result_create(memc
, &results_obj
);
1180 assert(&results_obj
== results
);
1182 /* We need to empty the server before continueing test */
1183 rc
= memcached_flush(memc
, 0);
1184 assert(rc
== MEMCACHED_SUCCESS
);
1186 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1187 assert(rc
== MEMCACHED_SUCCESS
);
1189 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1194 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1196 assert(rc
== MEMCACHED_END
);
1198 for (x
= 0; x
< 3; x
++)
1200 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1201 keys
[x
], key_length
[x
],
1202 (time_t)50, (uint32_t)9);
1203 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1206 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1207 assert(rc
== MEMCACHED_SUCCESS
);
1209 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1212 assert(&results_obj
== results
);
1213 assert(rc
== MEMCACHED_SUCCESS
);
1214 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1215 assert(!memcmp(memcached_result_key_value(results
),
1216 memcached_result_value(results
),
1217 memcached_result_length(results
)));
1220 memcached_result_free(&results_obj
);
1225 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1227 memcached_return rc
;
1228 char *keys
[]= {"fudge", "son", "food"};
1229 size_t key_length
[]= {5, 3, 4};
1232 memcached_result_st
*results
;
1234 /* We need to empty the server before continueing test */
1235 rc
= memcached_flush(memc
, 0);
1236 assert(rc
== MEMCACHED_SUCCESS
);
1238 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1239 assert(rc
== MEMCACHED_SUCCESS
);
1241 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1246 assert(rc
== MEMCACHED_END
);
1248 for (x
= 0; x
< 3; x
++)
1250 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1251 keys
[x
], key_length
[x
],
1252 (time_t)50, (uint32_t)9);
1253 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1256 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1257 assert(rc
== MEMCACHED_SUCCESS
);
1260 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1263 assert(rc
== MEMCACHED_SUCCESS
);
1264 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1265 assert(!memcmp(memcached_result_key_value(results
),
1266 memcached_result_value(results
),
1267 memcached_result_length(results
)));
1268 memcached_result_free(results
);
1275 /* Count the results */
1276 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1277 memcached_result_st
*result
__attribute__((unused
)),
1280 unsigned int *counter
= (unsigned int *)context
;
1282 *counter
= *counter
+ 1;
1284 return MEMCACHED_SUCCESS
;
1287 static test_return
mget_result_function(memcached_st
*memc
)
1289 memcached_return rc
;
1290 char *keys
[]= {"fudge", "son", "food"};
1291 size_t key_length
[]= {5, 3, 4};
1293 unsigned int counter
;
1294 memcached_execute_function callbacks
[1];
1296 /* We need to empty the server before continueing test */
1297 rc
= memcached_flush(memc
, 0);
1298 for (x
= 0; x
< 3; x
++)
1300 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1301 keys
[x
], key_length
[x
],
1302 (time_t)50, (uint32_t)9);
1303 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1306 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1307 assert(rc
== MEMCACHED_SUCCESS
);
1309 callbacks
[0]= &callback_counter
;
1311 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1313 assert(counter
== 3);
1318 static test_return
mget_test(memcached_st
*memc
)
1320 memcached_return rc
;
1321 char *keys
[]= {"fudge", "son", "food"};
1322 size_t key_length
[]= {5, 3, 4};
1326 char return_key
[MEMCACHED_MAX_KEY
];
1327 size_t return_key_length
;
1329 size_t return_value_length
;
1331 /* We need to empty the server before continueing test */
1332 rc
= memcached_flush(memc
, 0);
1333 assert(rc
== MEMCACHED_SUCCESS
);
1335 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1336 assert(rc
== MEMCACHED_SUCCESS
);
1338 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1339 &return_value_length
, &flags
, &rc
)) != NULL
)
1341 assert(return_value
);
1343 assert(!return_value
);
1344 assert(return_value_length
== 0);
1345 assert(rc
== MEMCACHED_END
);
1347 for (x
= 0; x
< 3; x
++)
1349 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1350 keys
[x
], key_length
[x
],
1351 (time_t)50, (uint32_t)9);
1352 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1355 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1356 assert(rc
== MEMCACHED_SUCCESS
);
1359 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1360 &return_value_length
, &flags
, &rc
)))
1362 assert(return_value
);
1363 assert(rc
== MEMCACHED_SUCCESS
);
1364 assert(return_key_length
== return_value_length
);
1365 assert(!memcmp(return_value
, return_key
, return_value_length
));
1373 static test_return
get_stats_keys(memcached_st
*memc
)
1377 memcached_stat_st memc_stat
;
1378 memcached_return rc
;
1380 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1381 assert(rc
== MEMCACHED_SUCCESS
);
1382 for (ptr
= list
; *ptr
; ptr
++)
1391 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1393 const char *version_string
;
1395 version_string
= memcached_lib_version();
1397 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1402 static test_return
get_stats(memcached_st
*memc
)
1407 memcached_return rc
;
1408 memcached_stat_st
*memc_stat
;
1410 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1411 assert(rc
== MEMCACHED_SUCCESS
);
1413 assert(rc
== MEMCACHED_SUCCESS
);
1416 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1418 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1419 assert(rc
== MEMCACHED_SUCCESS
);
1420 for (ptr
= list
; *ptr
; ptr
++);
1425 memcached_stat_free(NULL
, memc_stat
);
1430 static test_return
add_host_test(memcached_st
*memc
)
1433 memcached_server_st
*servers
;
1434 memcached_return rc
;
1435 char servername
[]= "0.example.com";
1437 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1439 assert(1 == memcached_server_list_count(servers
));
1441 for (x
= 2; x
< 20; x
++)
1443 char buffer
[SMALL_STRING_LEN
];
1445 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1446 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1448 assert(rc
== MEMCACHED_SUCCESS
);
1449 assert(x
== memcached_server_list_count(servers
));
1452 rc
= memcached_server_push(memc
, servers
);
1453 assert(rc
== MEMCACHED_SUCCESS
);
1454 rc
= memcached_server_push(memc
, servers
);
1455 assert(rc
== MEMCACHED_SUCCESS
);
1457 memcached_server_list_free(servers
);
1462 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1464 return MEMCACHED_SUCCESS
;
1467 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1469 return MEMCACHED_SUCCESS
;
1472 static test_return
callback_test(memcached_st
*memc
)
1474 /* Test User Data */
1478 memcached_return rc
;
1480 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1481 assert(rc
== MEMCACHED_SUCCESS
);
1482 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1483 assert(*test_ptr
== x
);
1486 /* Test Clone Callback */
1488 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1489 void *clone_cb_ptr
= *(void **)&clone_cb
;
1490 void *temp_function
= NULL
;
1491 memcached_return rc
;
1493 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1495 assert(rc
== MEMCACHED_SUCCESS
);
1496 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1497 assert(temp_function
== clone_cb_ptr
);
1500 /* Test Cleanup Callback */
1502 memcached_cleanup_func cleanup_cb
=
1503 (memcached_cleanup_func
)cleanup_test_callback
;
1504 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1505 void *temp_function
= NULL
;
1506 memcached_return rc
;
1508 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1510 assert(rc
== MEMCACHED_SUCCESS
);
1511 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1512 assert(temp_function
== cleanup_cb_ptr
);
1518 /* We don't test the behavior itself, we test the switches */
1519 static test_return
behavior_test(memcached_st
*memc
)
1524 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1525 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1528 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1529 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1532 set
= MEMCACHED_HASH_MD5
;
1533 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1534 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1535 assert(value
== MEMCACHED_HASH_MD5
);
1539 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1540 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1543 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1544 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1547 set
= MEMCACHED_HASH_DEFAULT
;
1548 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1549 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1550 assert(value
== MEMCACHED_HASH_DEFAULT
);
1552 set
= MEMCACHED_HASH_CRC
;
1553 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1554 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1555 assert(value
== MEMCACHED_HASH_CRC
);
1557 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1560 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1563 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1564 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1565 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1569 /* Test case provided by Cal Haldenbrand */
1570 static test_return
user_supplied_bug1(memcached_st
*memc
)
1572 unsigned int setter
= 1;
1575 unsigned long long total
= 0;
1578 char randomstuff
[6 * 1024];
1579 memcached_return rc
;
1581 memset(randomstuff
, 0, 6 * 1024);
1583 /* We just keep looking at the same values over and over */
1586 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1587 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1591 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1595 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1596 memset(randomstuff
, 0, 6 * 1024);
1597 assert(size
< 6 * 1024); /* Being safe here */
1599 for (j
= 0 ; j
< size
;j
++)
1600 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1603 sprintf(key
, "%d", x
);
1604 rc
= memcached_set(memc
, key
, strlen(key
),
1605 randomstuff
, strlen(randomstuff
), 10, 0);
1606 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1607 /* If we fail, lets try again */
1608 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1609 rc
= memcached_set(memc
, key
, strlen(key
),
1610 randomstuff
, strlen(randomstuff
), 10, 0);
1611 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1617 /* Test case provided by Cal Haldenbrand */
1618 static test_return
user_supplied_bug2(memcached_st
*memc
)
1621 unsigned int setter
;
1623 unsigned long long total
;
1626 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1627 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1629 setter
= 20 * 1024576;
1630 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1631 setter
= 20 * 1024576;
1632 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1633 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1634 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1636 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1639 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1641 memcached_return rc
= MEMCACHED_SUCCESS
;
1642 char buffer
[SMALL_STRING_LEN
];
1647 memset(buffer
, 0, SMALL_STRING_LEN
);
1649 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1650 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1651 &val_len
, &flags
, &rc
);
1652 if (rc
!= MEMCACHED_SUCCESS
)
1654 if (rc
== MEMCACHED_NOTFOUND
)
1671 /* Do a large mget() over all the keys we think exist */
1672 #define KEY_COUNT 3000 // * 1024576
1673 static test_return
user_supplied_bug3(memcached_st
*memc
)
1675 memcached_return rc
;
1676 unsigned int setter
;
1679 size_t key_lengths
[KEY_COUNT
];
1682 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1683 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1685 setter
= 20 * 1024576;
1686 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1687 setter
= 20 * 1024576;
1688 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1689 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1690 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1693 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1695 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1696 for (x
= 0; x
< KEY_COUNT
; x
++)
1700 snprintf(buffer
, 30, "%u", x
);
1701 keys
[x
]= strdup(buffer
);
1702 key_lengths
[x
]= strlen(keys
[x
]);
1705 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
1706 assert(rc
== MEMCACHED_SUCCESS
);
1708 /* Turn this into a help function */
1710 char return_key
[MEMCACHED_MAX_KEY
];
1711 size_t return_key_length
;
1713 size_t return_value_length
;
1716 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1717 &return_value_length
, &flags
, &rc
)))
1719 assert(return_value
);
1720 assert(rc
== MEMCACHED_SUCCESS
);
1725 for (x
= 0; x
< KEY_COUNT
; x
++)
1732 /* Make sure we behave properly if server list has no values */
1733 static test_return
user_supplied_bug4(memcached_st
*memc
)
1735 memcached_return rc
;
1736 char *keys
[]= {"fudge", "son", "food"};
1737 size_t key_length
[]= {5, 3, 4};
1740 char return_key
[MEMCACHED_MAX_KEY
];
1741 size_t return_key_length
;
1743 size_t return_value_length
;
1745 /* Here we free everything before running a bunch of mget tests */
1747 memcached_server_list_free(memc
->hosts
);
1749 memc
->number_of_hosts
= 0;
1753 /* We need to empty the server before continueing test */
1754 rc
= memcached_flush(memc
, 0);
1755 assert(rc
== MEMCACHED_NO_SERVERS
);
1757 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1758 assert(rc
== MEMCACHED_NO_SERVERS
);
1760 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1761 &return_value_length
, &flags
, &rc
)) != NULL
)
1763 assert(return_value
);
1765 assert(!return_value
);
1766 assert(return_value_length
== 0);
1767 assert(rc
== MEMCACHED_NO_SERVERS
);
1769 for (x
= 0; x
< 3; x
++)
1771 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1772 keys
[x
], key_length
[x
],
1773 (time_t)50, (uint32_t)9);
1774 assert(rc
== MEMCACHED_NO_SERVERS
);
1777 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1778 assert(rc
== MEMCACHED_NO_SERVERS
);
1781 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1782 &return_value_length
, &flags
, &rc
)))
1784 assert(return_value
);
1785 assert(rc
== MEMCACHED_SUCCESS
);
1786 assert(return_key_length
== return_value_length
);
1787 assert(!memcmp(return_value
, return_key
, return_value_length
));
1795 #define VALUE_SIZE_BUG5 1048064
1796 static test_return
user_supplied_bug5(memcached_st
*memc
)
1798 memcached_return rc
;
1799 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1800 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1801 char return_key
[MEMCACHED_MAX_KEY
];
1802 size_t return_key_length
;
1804 size_t value_length
;
1808 char insert_data
[VALUE_SIZE_BUG5
];
1810 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1811 insert_data
[x
]= (signed char)rand();
1813 memcached_flush(memc
, 0);
1814 value
= memcached_get(memc
, keys
[0], key_length
[0],
1815 &value_length
, &flags
, &rc
);
1816 assert(value
== NULL
);
1817 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1820 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1821 &value_length
, &flags
, &rc
)))
1825 for (x
= 0; x
< 4; x
++)
1827 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1828 insert_data
, VALUE_SIZE_BUG5
,
1829 (time_t)0, (uint32_t)0);
1830 assert(rc
== MEMCACHED_SUCCESS
);
1833 for (x
= 0; x
< 10; x
++)
1835 value
= memcached_get(memc
, keys
[0], key_length
[0],
1836 &value_length
, &flags
, &rc
);
1840 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1842 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1843 &value_length
, &flags
, &rc
)))
1854 static test_return
user_supplied_bug6(memcached_st
*memc
)
1856 memcached_return rc
;
1857 char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1858 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1859 char return_key
[MEMCACHED_MAX_KEY
];
1860 size_t return_key_length
;
1862 size_t value_length
;
1866 char insert_data
[VALUE_SIZE_BUG5
];
1868 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1869 insert_data
[x
]= (signed char)rand();
1871 memcached_flush(memc
, 0);
1872 value
= memcached_get(memc
, keys
[0], key_length
[0],
1873 &value_length
, &flags
, &rc
);
1874 assert(value
== NULL
);
1875 assert(rc
== MEMCACHED_NOTFOUND
);
1876 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1877 assert(rc
== MEMCACHED_SUCCESS
);
1880 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1881 &value_length
, &flags
, &rc
)))
1884 assert(rc
== MEMCACHED_END
);
1886 for (x
= 0; x
< 4; x
++)
1888 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1889 insert_data
, VALUE_SIZE_BUG5
,
1890 (time_t)0, (uint32_t)0);
1891 assert(rc
== MEMCACHED_SUCCESS
);
1894 for (x
= 0; x
< 2; x
++)
1896 value
= memcached_get(memc
, keys
[0], key_length
[0],
1897 &value_length
, &flags
, &rc
);
1901 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1902 assert(rc
== MEMCACHED_SUCCESS
);
1904 /* We test for purge of partial complete fetches */
1905 for (count
= 3; count
; count
--)
1907 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1908 &value_length
, &flags
, &rc
);
1909 assert(rc
== MEMCACHED_SUCCESS
);
1910 assert(!(memcmp(value
, insert_data
, value_length
)));
1911 assert(value_length
);
1919 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1921 memcached_return rc
;
1923 memcached_st
*memc_clone
;
1925 memcached_server_st
*servers
;
1926 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";
1928 servers
= memcached_servers_parse(server_list
);
1931 mine
= memcached_create(NULL
);
1932 rc
= memcached_server_push(mine
, servers
);
1933 assert(rc
== MEMCACHED_SUCCESS
);
1934 memcached_server_list_free(servers
);
1937 memc_clone
= memcached_clone(NULL
, mine
);
1939 memcached_quit(mine
);
1940 memcached_quit(memc_clone
);
1943 memcached_free(mine
);
1944 memcached_free(memc_clone
);
1949 /* Test flag store/retrieve */
1950 static test_return
user_supplied_bug7(memcached_st
*memc
)
1952 memcached_return rc
;
1953 char *keys
= "036790384900";
1954 size_t key_length
= strlen("036790384900");
1955 char return_key
[MEMCACHED_MAX_KEY
];
1956 size_t return_key_length
;
1958 size_t value_length
;
1961 char insert_data
[VALUE_SIZE_BUG5
];
1963 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1964 insert_data
[x
]= (signed char)rand();
1966 memcached_flush(memc
, 0);
1969 rc
= memcached_set(memc
, keys
, key_length
,
1970 insert_data
, VALUE_SIZE_BUG5
,
1972 assert(rc
== MEMCACHED_SUCCESS
);
1975 value
= memcached_get(memc
, keys
, key_length
,
1976 &value_length
, &flags
, &rc
);
1977 assert(flags
== 245);
1981 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1984 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1985 &value_length
, &flags
, &rc
);
1986 assert(flags
== 245);
1994 static test_return
user_supplied_bug9(memcached_st
*memc
)
1996 memcached_return rc
;
1997 char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1998 size_t key_length
[3];
2003 char return_key
[MEMCACHED_MAX_KEY
];
2004 size_t return_key_length
;
2006 size_t return_value_length
;
2009 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2010 key_length
[1]= strlen("fudge&*@#");
2011 key_length
[2]= strlen("for^#@&$not");
2014 for (x
= 0; x
< 3; x
++)
2016 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2017 keys
[x
], key_length
[x
],
2018 (time_t)50, (uint32_t)9);
2019 assert(rc
== MEMCACHED_SUCCESS
);
2022 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2023 assert(rc
== MEMCACHED_SUCCESS
);
2025 /* We need to empty the server before continueing test */
2026 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2027 &return_value_length
, &flags
, &rc
)) != NULL
)
2029 assert(return_value
);
2038 /* We are testing with aggressive timeout to get failures */
2039 static test_return
user_supplied_bug10(memcached_st
*memc
)
2043 size_t value_length
= 512;
2046 memcached_return rc
;
2047 unsigned int set
= 1;
2048 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2051 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2052 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2054 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2057 value
= (char*)malloc(value_length
* sizeof(char));
2059 for (x
= 0; x
< value_length
; x
++)
2060 value
[x
]= (char) (x
% 127);
2062 for (x
= 1; x
<= 100000; ++x
)
2064 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2066 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2067 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2069 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2074 memcached_free(mclone
);
2080 We are looking failures in the async protocol
2082 static test_return
user_supplied_bug11(memcached_st
*memc
)
2086 size_t value_length
= 512;
2089 memcached_return rc
;
2090 unsigned int set
= 1;
2092 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2094 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2095 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2097 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2100 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2102 assert(timeout
== -1);
2104 value
= (char*)malloc(value_length
* sizeof(char));
2106 for (x
= 0; x
< value_length
; x
++)
2107 value
[x
]= (char) (x
% 127);
2109 for (x
= 1; x
<= 100000; ++x
)
2111 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2115 memcached_free(mclone
);
2121 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2123 static test_return
user_supplied_bug12(memcached_st
*memc
)
2125 memcached_return rc
;
2127 size_t value_length
;
2129 uint64_t number_value
;
2131 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2132 &value_length
, &flags
, &rc
);
2133 assert(value
== NULL
);
2134 assert(rc
== MEMCACHED_NOTFOUND
);
2136 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2139 assert(value
== NULL
);
2140 /* The binary protocol will set the key if it doesn't exist */
2141 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2142 assert(rc
== MEMCACHED_SUCCESS
);
2144 assert(rc
== MEMCACHED_NOTFOUND
);
2146 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2148 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2149 &value_length
, &flags
, &rc
);
2151 assert(rc
== MEMCACHED_SUCCESS
);
2154 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2156 assert(number_value
== 2);
2157 assert(rc
== MEMCACHED_SUCCESS
);
2163 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2164 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2166 static test_return
user_supplied_bug13(memcached_st
*memc
)
2168 char key
[] = "key34567890";
2170 memcached_return rc
;
2171 size_t overflowSize
;
2173 char commandFirst
[]= "set key34567890 0 0 ";
2174 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2175 size_t commandLength
;
2178 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2180 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2182 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2184 overflow
= malloc(testSize
);
2185 assert(overflow
!= NULL
);
2187 memset(overflow
, 'x', testSize
);
2188 rc
= memcached_set(memc
, key
, strlen(key
),
2189 overflow
, testSize
, 0, 0);
2190 assert(rc
== MEMCACHED_SUCCESS
);
2199 Test values of many different sizes
2200 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2201 set key34567890 0 0 8169 \r\n
2202 is sent followed by buffer of size 8169, followed by 8169
2204 static test_return
user_supplied_bug14(memcached_st
*memc
)
2207 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2208 memcached_return rc
;
2211 size_t value_length
= 18000;
2213 size_t string_length
;
2216 size_t current_length
;
2218 value
= (char*)malloc(value_length
);
2221 for (x
= 0; x
< value_length
; x
++)
2222 value
[x
] = (char) (x
% 127);
2224 for (current_length
= 0; current_length
< value_length
; current_length
++)
2226 rc
= memcached_set(memc
, key
, strlen(key
),
2227 value
, current_length
,
2228 (time_t)0, (uint32_t)0);
2229 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2231 string
= memcached_get(memc
, key
, strlen(key
),
2232 &string_length
, &flags
, &rc
);
2234 assert(rc
== MEMCACHED_SUCCESS
);
2235 assert(string_length
== current_length
);
2236 assert(!memcmp(string
, value
, string_length
));
2247 Look for zero length value problems
2249 static test_return
user_supplied_bug15(memcached_st
*memc
)
2252 memcached_return rc
;
2258 for (x
= 0; x
< 2; x
++)
2260 rc
= memcached_set(memc
, key
, strlen(key
),
2262 (time_t)0, (uint32_t)0);
2264 assert(rc
== MEMCACHED_SUCCESS
);
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);
2274 value
= memcached_get(memc
, key
, strlen(key
),
2275 &length
, &flags
, &rc
);
2277 assert(rc
== MEMCACHED_SUCCESS
);
2278 assert(value
== NULL
);
2279 assert(length
== 0);
2286 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2287 static test_return
user_supplied_bug16(memcached_st
*memc
)
2289 memcached_return rc
;
2295 rc
= memcached_set(memc
, key
, strlen(key
),
2297 (time_t)0, UINT32_MAX
);
2299 assert(rc
== MEMCACHED_SUCCESS
);
2301 value
= memcached_get(memc
, key
, strlen(key
),
2302 &length
, &flags
, &rc
);
2304 assert(rc
== MEMCACHED_SUCCESS
);
2305 assert(value
== NULL
);
2306 assert(length
== 0);
2307 assert(flags
== UINT32_MAX
);
2312 /* Check the validity of chinese key*/
2313 static test_return
user_supplied_bug17(memcached_st
*memc
)
2315 memcached_return rc
;
2317 char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2322 rc
= memcached_set(memc
, key
, strlen(key
),
2323 value
, strlen(value
),
2326 assert(rc
== MEMCACHED_SUCCESS
);
2328 value2
= memcached_get(memc
, key
, strlen(key
),
2329 &length
, &flags
, &rc
);
2331 assert(length
==strlen(value
));
2332 assert(rc
== MEMCACHED_SUCCESS
);
2333 assert(memcmp(value
, value2
, length
)==0);
2343 static test_return
user_supplied_bug19(memcached_st
*memc
)
2346 memcached_server_st
*s
;
2347 memcached_return res
;
2351 m
= memcached_create(NULL
);
2352 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2353 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2355 s
= memcached_server_by_key(m
, "a", 1, &res
);
2356 memcached_server_free(s
);
2363 /* CAS test from Andei */
2364 static test_return
user_supplied_bug20(memcached_st
*memc
)
2366 memcached_return status
;
2367 memcached_result_st
*result
, result_obj
;
2369 size_t key_len
= strlen("abc");
2370 char *value
= "foobar";
2371 size_t value_len
= strlen(value
);
2373 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2375 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2376 assert(status
== MEMCACHED_SUCCESS
);
2378 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2379 assert(status
== MEMCACHED_SUCCESS
);
2381 result
= memcached_result_create(memc
, &result_obj
);
2384 memcached_result_create(memc
, &result_obj
);
2385 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2388 assert(status
== MEMCACHED_SUCCESS
);
2390 memcached_result_free(result
);
2395 #include "ketama_test_cases.h"
2396 static test_return
user_supplied_bug18(memcached_st
*trash
)
2398 memcached_return rc
;
2401 memcached_server_st
*server_pool
;
2406 memc
= memcached_create(NULL
);
2409 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2410 assert(rc
== MEMCACHED_SUCCESS
);
2412 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2415 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2416 assert(rc
== MEMCACHED_SUCCESS
);
2418 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2419 assert(value
== MEMCACHED_HASH_MD5
);
2421 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");
2422 memcached_server_push(memc
, server_pool
);
2424 /* verify that the server list was parsed okay. */
2425 assert(memc
->number_of_hosts
== 8);
2426 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2427 assert(server_pool
[0].port
== 11211);
2428 assert(server_pool
[0].weight
== 600);
2429 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2430 assert(server_pool
[2].port
== 11211);
2431 assert(server_pool
[2].weight
== 200);
2432 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2433 assert(server_pool
[7].port
== 11211);
2434 assert(server_pool
[7].weight
== 100);
2436 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2437 * us test the boundary wraparound.
2439 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2441 /* verify the standard ketama set. */
2442 for (x
= 0; x
< 99; x
++)
2444 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2445 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2446 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2449 memcached_server_list_free(server_pool
);
2450 memcached_free(memc
);
2455 static test_return
auto_eject_hosts(memcached_st
*trash
)
2459 memcached_return rc
;
2460 memcached_st
*memc
= memcached_create(NULL
);
2463 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2464 assert(rc
== MEMCACHED_SUCCESS
);
2466 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2469 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2470 assert(rc
== MEMCACHED_SUCCESS
);
2472 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2473 assert(value
== MEMCACHED_HASH_MD5
);
2475 /* server should be removed when in delay */
2476 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2477 assert(rc
== MEMCACHED_SUCCESS
);
2479 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2482 memcached_server_st
*server_pool
;
2483 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");
2484 memcached_server_push(memc
, server_pool
);
2486 /* verify that the server list was parsed okay. */
2487 assert(memc
->number_of_hosts
== 8);
2488 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2489 assert(server_pool
[0].port
== 11211);
2490 assert(server_pool
[0].weight
== 600);
2491 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2492 assert(server_pool
[2].port
== 11211);
2493 assert(server_pool
[2].weight
== 200);
2494 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2495 assert(server_pool
[7].port
== 11211);
2496 assert(server_pool
[7].weight
== 100);
2498 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2499 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2501 for (int x
= 0; x
< 99; x
++)
2503 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2504 assert(server_idx
!= 2);
2507 /* and re-added when it's back. */
2508 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2509 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2510 run_distribution(memc
);
2511 for (int x
= 0; x
< 99; x
++)
2513 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2514 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2515 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2518 memcached_server_list_free(server_pool
);
2519 memcached_free(memc
);
2521 return TEST_SUCCESS
;
2524 static test_return
result_static(memcached_st
*memc
)
2526 memcached_result_st result
;
2527 memcached_result_st
*result_ptr
;
2529 result_ptr
= memcached_result_create(memc
, &result
);
2530 assert(result
.is_allocated
== false);
2532 memcached_result_free(&result
);
2537 static test_return
result_alloc(memcached_st
*memc
)
2539 memcached_result_st
*result
;
2541 result
= memcached_result_create(memc
, NULL
);
2543 memcached_result_free(result
);
2548 static test_return
string_static_null(memcached_st
*memc
)
2550 memcached_string_st string
;
2551 memcached_string_st
*string_ptr
;
2553 string_ptr
= memcached_string_create(memc
, &string
, 0);
2554 assert(string
.is_allocated
== false);
2556 memcached_string_free(&string
);
2561 static test_return
string_alloc_null(memcached_st
*memc
)
2563 memcached_string_st
*string
;
2565 string
= memcached_string_create(memc
, NULL
, 0);
2567 memcached_string_free(string
);
2572 static test_return
string_alloc_with_size(memcached_st
*memc
)
2574 memcached_string_st
*string
;
2576 string
= memcached_string_create(memc
, NULL
, 1024);
2578 memcached_string_free(string
);
2583 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2585 memcached_string_st
*string
;
2587 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2588 assert(string
== NULL
);
2593 static test_return
string_alloc_append(memcached_st
*memc
)
2596 char buffer
[SMALL_STRING_LEN
];
2597 memcached_string_st
*string
;
2599 /* Ring the bell! */
2600 memset(buffer
, 6, SMALL_STRING_LEN
);
2602 string
= memcached_string_create(memc
, NULL
, 100);
2605 for (x
= 0; x
< 1024; x
++)
2607 memcached_return rc
;
2608 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2609 assert(rc
== MEMCACHED_SUCCESS
);
2611 memcached_string_free(string
);
2616 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2618 memcached_return rc
;
2620 char buffer
[SMALL_STRING_LEN
];
2621 memcached_string_st
*string
;
2623 /* Ring the bell! */
2624 memset(buffer
, 6, SMALL_STRING_LEN
);
2626 string
= memcached_string_create(memc
, NULL
, 100);
2629 for (x
= 0; x
< 1024; x
++)
2631 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2632 assert(rc
== MEMCACHED_SUCCESS
);
2634 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
2635 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2636 memcached_string_free(string
);
2641 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2643 pairs_free(global_pairs
);
2648 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2650 unsigned long long x
;
2651 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2652 global_count
= GLOBAL_COUNT
;
2654 for (x
= 0; x
< global_count
; x
++)
2656 global_keys
[x
]= global_pairs
[x
].key
;
2657 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2663 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2665 unsigned long long x
;
2666 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2667 global_count
= GLOBAL2_COUNT
;
2669 for (x
= 0; x
< global_count
; x
++)
2671 global_keys
[x
]= global_pairs
[x
].key
;
2672 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2678 static test_return
generate_data(memcached_st
*memc
)
2680 execute_set(memc
, global_pairs
, global_count
);
2685 static test_return
generate_data_with_stats(memcached_st
*memc
)
2687 memcached_stat_st
*stat_p
;
2688 memcached_return rc
;
2689 uint32_t host_index
= 0;
2690 execute_set(memc
, global_pairs
, global_count
);
2692 //TODO: hosts used size stats
2693 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2696 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2698 /* This test was changes so that "make test" would work properlly */
2700 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
);
2702 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
2705 memcached_stat_free(NULL
, stat_p
);
2709 static test_return
generate_buffer_data(memcached_st
*memc
)
2714 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2715 generate_data(memc
);
2720 static test_return
get_read_count(memcached_st
*memc
)
2723 memcached_return rc
;
2724 memcached_st
*memc_clone
;
2726 memc_clone
= memcached_clone(NULL
, memc
);
2729 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
2733 size_t return_value_length
;
2737 for (x
= count
= 0; x
< global_count
; x
++)
2739 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
2740 &return_value_length
, &flags
, &rc
);
2741 if (rc
== MEMCACHED_SUCCESS
)
2748 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2751 memcached_free(memc_clone
);
2756 static test_return
get_read(memcached_st
*memc
)
2759 memcached_return rc
;
2763 size_t return_value_length
;
2766 for (x
= 0; x
< global_count
; x
++)
2768 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2769 &return_value_length
, &flags
, &rc
);
2771 assert(return_value);
2772 assert(rc == MEMCACHED_SUCCESS);
2774 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2782 static test_return
mget_read(memcached_st
*memc
)
2784 memcached_return rc
;
2786 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2787 assert(rc
== MEMCACHED_SUCCESS
);
2788 /* Turn this into a help function */
2790 char return_key
[MEMCACHED_MAX_KEY
];
2791 size_t return_key_length
;
2793 size_t return_value_length
;
2796 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2797 &return_value_length
, &flags
, &rc
)))
2799 assert(return_value
);
2800 assert(rc
== MEMCACHED_SUCCESS
);
2808 static test_return
mget_read_result(memcached_st
*memc
)
2810 memcached_return rc
;
2812 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2813 assert(rc
== MEMCACHED_SUCCESS
);
2814 /* Turn this into a help function */
2816 memcached_result_st results_obj
;
2817 memcached_result_st
*results
;
2819 results
= memcached_result_create(memc
, &results_obj
);
2821 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2824 assert(rc
== MEMCACHED_SUCCESS
);
2827 memcached_result_free(&results_obj
);
2833 static test_return
mget_read_function(memcached_st
*memc
)
2835 memcached_return rc
;
2836 unsigned int counter
;
2837 memcached_execute_function callbacks
[1];
2839 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2840 assert(rc
== MEMCACHED_SUCCESS
);
2842 callbacks
[0]= &callback_counter
;
2844 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2849 static test_return
delete_generate(memcached_st
*memc
)
2853 for (x
= 0; x
< global_count
; x
++)
2855 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2861 static test_return
delete_buffer_generate(memcached_st
*memc
)
2867 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2869 for (x
= 0; x
< global_count
; x
++)
2871 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2877 static test_return
add_host_test1(memcached_st
*memc
)
2880 memcached_return rc
;
2881 char servername
[]= "0.example.com";
2882 memcached_server_st
*servers
;
2884 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2886 assert(1 == memcached_server_list_count(servers
));
2888 for (x
= 2; x
< 20; x
++)
2890 char buffer
[SMALL_STRING_LEN
];
2892 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2893 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2895 assert(rc
== MEMCACHED_SUCCESS
);
2896 assert(x
== memcached_server_list_count(servers
));
2899 rc
= memcached_server_push(memc
, servers
);
2900 assert(rc
== MEMCACHED_SUCCESS
);
2901 rc
= memcached_server_push(memc
, servers
);
2902 assert(rc
== MEMCACHED_SUCCESS
);
2904 memcached_server_list_free(servers
);
2909 static memcached_return
pre_nonblock(memcached_st
*memc
)
2911 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2913 return MEMCACHED_SUCCESS
;
2916 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
2918 memcached_return rc
= MEMCACHED_FAILURE
;
2919 memcached_st
*memc_clone
;
2921 memc_clone
= memcached_clone(NULL
, memc
);
2923 // The memcached_version needs to be done on a clone, because the server
2924 // will not toggle protocol on an connection.
2925 memcached_version(memc_clone
);
2927 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
2929 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2930 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2931 assert(rc
== MEMCACHED_SUCCESS
);
2932 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2935 memcached_free(memc_clone
);
2939 static memcached_return
pre_murmur(memcached_st
*memc
)
2941 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2943 return MEMCACHED_SUCCESS
;
2946 static memcached_return
pre_jenkins(memcached_st
*memc
)
2948 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
2950 return MEMCACHED_SUCCESS
;
2954 static memcached_return
pre_md5(memcached_st
*memc
)
2956 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2958 return MEMCACHED_SUCCESS
;
2961 static memcached_return
pre_crc(memcached_st
*memc
)
2963 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2965 return MEMCACHED_SUCCESS
;
2968 static memcached_return
pre_hsieh(memcached_st
*memc
)
2970 #ifdef HAVE_HSIEH_HASH
2971 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2972 return MEMCACHED_SUCCESS
;
2975 return MEMCACHED_FAILURE
;
2979 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2981 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2983 return MEMCACHED_SUCCESS
;
2986 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
2988 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
2990 return MEMCACHED_SUCCESS
;
2993 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
2995 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
2997 return MEMCACHED_SUCCESS
;
3000 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
3002 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3004 return MEMCACHED_SUCCESS
;
3007 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3009 memcached_return rc
;
3012 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3013 assert(rc
== MEMCACHED_SUCCESS
);
3015 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3018 return MEMCACHED_SUCCESS
;
3021 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3023 memcached_return rc
;
3026 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3027 assert(rc
== MEMCACHED_SUCCESS
);
3029 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3032 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3033 assert(rc
== MEMCACHED_SUCCESS
);
3035 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3036 assert(value
== MEMCACHED_HASH_MD5
);
3037 return MEMCACHED_SUCCESS
;
3040 static memcached_return
pre_binary(memcached_st
*memc
)
3042 memcached_return rc
= MEMCACHED_FAILURE
;
3043 memcached_st
*memc_clone
;
3045 memc_clone
= memcached_clone(NULL
, memc
);
3047 // The memcached_version needs to be done on a clone, because the server
3048 // will not toggle protocol on an connection.
3049 memcached_version(memc_clone
);
3051 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3053 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3054 assert(rc
== MEMCACHED_SUCCESS
);
3055 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3058 memcached_free(memc_clone
);
3062 static memcached_return
pre_replication(memcached_st
*memc
)
3064 memcached_return rc
= MEMCACHED_FAILURE
;
3065 if (pre_binary(memc
) == MEMCACHED_SUCCESS
)
3068 * Make sure that we store the item on all servers
3069 * (master + replicas == number of servers)
3071 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3072 memc
->number_of_hosts
- 1);
3073 assert(rc
== MEMCACHED_SUCCESS
);
3074 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3080 static memcached_return
pre_replication_noblock(memcached_st
*memc
)
3082 memcached_return rc
= MEMCACHED_FAILURE
;
3083 if (pre_replication(memc
) == MEMCACHED_SUCCESS
&&
3084 pre_nonblock(memc
) == MEMCACHED_SUCCESS
)
3085 rc
= MEMCACHED_SUCCESS
;
3090 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3095 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3097 void *ret
= malloc(size
);
3099 memset(ret
, 0xff, size
);
3104 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3106 return realloc(mem
, size
);
3109 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3111 return calloc(nelem
, size
);
3114 static memcached_return
set_prefix(memcached_st
*memc
)
3116 memcached_return rc
;
3117 const char *key
= "mine";
3120 /* Make sure be default none exists */
3121 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3122 assert(rc
== MEMCACHED_FAILURE
);
3124 /* Test a clean set */
3125 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3126 assert(rc
== MEMCACHED_SUCCESS
);
3128 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3129 assert(memcmp(value
, key
, 4) == 0);
3130 assert(rc
== MEMCACHED_SUCCESS
);
3132 /* Test that we can turn it off */
3133 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3134 assert(rc
== MEMCACHED_SUCCESS
);
3136 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3137 assert(rc
== MEMCACHED_FAILURE
);
3139 /* Now setup for main test */
3140 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3141 assert(rc
== MEMCACHED_SUCCESS
);
3143 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3144 assert(rc
== MEMCACHED_SUCCESS
);
3145 assert(memcmp(value
, key
, 4) == 0);
3147 /* Set to Zero, and then Set to something too large */
3150 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3151 assert(rc
== MEMCACHED_SUCCESS
);
3153 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3154 assert(rc
== MEMCACHED_FAILURE
);
3155 assert(value
== NULL
);
3157 /* Test a long key for failure */
3158 /* TODO, extend test to determine based on setting, what result should be */
3159 long_key
= "Thisismorethentheallottednumberofcharacters";
3160 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3161 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3162 assert(rc
== MEMCACHED_SUCCESS
);
3164 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3165 long_key
= "This is more then the allotted number of characters";
3166 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3167 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3169 /* Test for a bad prefix, but with a short key */
3170 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3171 assert(rc
== MEMCACHED_SUCCESS
);
3173 long_key
= "dog cat";
3174 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3175 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3178 return MEMCACHED_SUCCESS
;
3181 #ifdef MEMCACHED_ENABLE_DEPRECATED
3182 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3184 void *test_ptr
= NULL
;
3187 memcached_malloc_function malloc_cb
=
3188 (memcached_malloc_function
)my_malloc
;
3189 cb_ptr
= *(void **)&malloc_cb
;
3190 memcached_return rc
;
3192 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3193 assert(rc
== MEMCACHED_SUCCESS
);
3194 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3195 assert(rc
== MEMCACHED_SUCCESS
);
3196 assert(test_ptr
== cb_ptr
);
3200 memcached_realloc_function realloc_cb
=
3201 (memcached_realloc_function
)my_realloc
;
3202 cb_ptr
= *(void **)&realloc_cb
;
3203 memcached_return rc
;
3205 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3206 assert(rc
== MEMCACHED_SUCCESS
);
3207 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3208 assert(rc
== MEMCACHED_SUCCESS
);
3209 assert(test_ptr
== cb_ptr
);
3213 memcached_free_function free_cb
=
3214 (memcached_free_function
)my_free
;
3215 cb_ptr
= *(void **)&free_cb
;
3216 memcached_return rc
;
3218 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3219 assert(rc
== MEMCACHED_SUCCESS
);
3220 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3221 assert(rc
== MEMCACHED_SUCCESS
);
3222 assert(test_ptr
== cb_ptr
);
3224 return MEMCACHED_SUCCESS
;
3228 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3230 memcached_return rc
;
3231 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3232 my_realloc
, my_calloc
);
3233 assert(rc
== MEMCACHED_FAILURE
);
3235 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3236 my_realloc
, my_calloc
);
3238 memcached_malloc_function mem_malloc
;
3239 memcached_free_function mem_free
;
3240 memcached_realloc_function mem_realloc
;
3241 memcached_calloc_function mem_calloc
;
3242 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3243 &mem_realloc
, &mem_calloc
);
3245 assert(mem_malloc
== my_malloc
);
3246 assert(mem_realloc
== my_realloc
);
3247 assert(mem_calloc
== my_calloc
);
3248 assert(mem_free
== my_free
);
3250 return MEMCACHED_SUCCESS
;
3253 static memcached_return
enable_consistent(memcached_st
*memc
)
3255 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3256 memcached_hash hash
;
3257 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3258 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3259 return MEMCACHED_FAILURE
;
3261 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3262 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3264 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3265 assert(hash
== MEMCACHED_HASH_HSIEH
);
3268 return MEMCACHED_SUCCESS
;
3271 static memcached_return
enable_cas(memcached_st
*memc
)
3273 unsigned int set
= 1;
3275 memcached_version(memc
);
3277 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3278 || memc
->hosts
[0].minor_version
> 2)
3280 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3282 return MEMCACHED_SUCCESS
;
3285 return MEMCACHED_FAILURE
;
3288 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3290 memcached_version(memc
);
3292 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3293 || memc
->hosts
[0].minor_version
> 2)
3294 return MEMCACHED_SUCCESS
;
3296 return MEMCACHED_FAILURE
;
3299 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3301 memcached_return rc
;
3304 memcached_server_list_free(memc
->hosts
);
3306 memc
->number_of_hosts
= 0;
3308 if (stat("/tmp/memcached.socket", &buf
))
3309 return MEMCACHED_FAILURE
;
3311 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3316 static memcached_return
pre_nodelay(memcached_st
*memc
)
3318 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3319 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3321 return MEMCACHED_SUCCESS
;
3324 static memcached_return
pre_settimer(memcached_st
*memc
)
3326 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3327 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3329 return MEMCACHED_SUCCESS
;
3332 static memcached_return
poll_timeout(memcached_st
*memc
)
3338 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3340 timeout
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3342 assert(timeout
== 100);
3344 return MEMCACHED_SUCCESS
;
3347 static test_return
noreply_test(memcached_st
*memc
)
3349 memcached_return ret
;
3350 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3351 assert(ret
== MEMCACHED_SUCCESS
);
3352 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3353 assert(ret
== MEMCACHED_SUCCESS
);
3354 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3355 assert(ret
== MEMCACHED_SUCCESS
);
3356 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3357 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3358 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3360 for (int count
=0; count
< 5; ++count
)
3362 for (int x
=0; x
< 100; ++x
)
3365 size_t len
= (size_t)sprintf(key
, "%d", x
);
3369 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3372 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3375 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3378 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3381 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3387 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3391 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3392 ** API and is _ONLY_ done this way to verify that the library works the
3393 ** way it is supposed to do!!!!
3396 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3397 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3399 assert(no_msg
== 0);
3400 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3403 ** Now validate that all items was set properly!
3405 for (int x
=0; x
< 100; ++x
)
3408 size_t len
= (size_t)sprintf(key
, "%d", x
);
3411 char* value
=memcached_get(memc
, key
, strlen(key
),
3412 &length
, &flags
, &ret
);
3413 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3416 case 0: /* FALLTHROUGH */
3417 case 1: /* FALLTHROUGH */
3419 assert(strncmp(value
, key
, len
) == 0);
3420 assert(len
== length
);
3423 assert(length
== len
* 2);
3426 assert(length
== len
* 3);
3436 /* Try setting an illegal cas value (should not return an error to
3437 * the caller (because we don't expect a return message from the server)
3439 char* keys
[]= {"0"};
3440 size_t lengths
[]= {1};
3443 memcached_result_st results_obj
;
3444 memcached_result_st
*results
;
3445 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3446 assert(ret
== MEMCACHED_SUCCESS
);
3448 results
= memcached_result_create(memc
, &results_obj
);
3450 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3452 assert(ret
== MEMCACHED_SUCCESS
);
3453 uint64_t cas
= memcached_result_cas(results
);
3454 memcached_result_free(&results_obj
);
3456 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3457 assert(ret
== MEMCACHED_SUCCESS
);
3460 * The item will have a new cas value, so try to set it again with the old
3461 * value. This should fail!
3463 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3464 assert(ret
== MEMCACHED_SUCCESS
);
3465 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3466 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3467 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3470 return TEST_SUCCESS
;
3473 static test_return
analyzer_test(memcached_st
*memc
)
3475 memcached_return rc
;
3476 memcached_stat_st
*memc_stat
;
3477 memcached_analysis_st
*report
;
3479 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3480 assert(rc
== MEMCACHED_SUCCESS
);
3483 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3484 assert(rc
== MEMCACHED_SUCCESS
);
3488 memcached_stat_free(NULL
, memc_stat
);
3490 return TEST_SUCCESS
;
3493 /* Count the objects */
3494 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3495 const char *key
__attribute__((unused
)),
3496 size_t key_length
__attribute__((unused
)),
3499 uint32_t *counter
= (uint32_t *)context
;
3501 *counter
= *counter
+ 1;
3503 return MEMCACHED_SUCCESS
;
3506 static test_return
dump_test(memcached_st
*memc
)
3508 memcached_return rc
;
3509 uint32_t counter
= 0;
3510 memcached_dump_func callbacks
[1];
3511 test_return main_rc
;
3513 callbacks
[0]= &callback_dump_counter
;
3515 /* No support for Binary protocol yet */
3516 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3517 return TEST_SUCCESS
;
3519 main_rc
= set_test3(memc
);
3521 assert (main_rc
== TEST_SUCCESS
);
3523 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3524 assert(rc
== MEMCACHED_SUCCESS
);
3526 /* We may have more then 32 if our previous flush has not completed */
3527 assert(counter
>= 32);
3529 return TEST_SUCCESS
;
3532 #ifdef HAVE_LIBMEMCACHEDUTIL
3533 static void* connection_release(void *arg
) {
3535 memcached_pool_st
* pool
;
3540 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3544 static test_return
connection_pool_test(memcached_st
*memc
)
3546 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3547 assert(pool
!= NULL
);
3548 memcached_st
* mmc
[10];
3549 memcached_return rc
;
3551 for (int x
= 0; x
< 10; ++x
) {
3552 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3553 assert(mmc
[x
] != NULL
);
3554 assert(rc
== MEMCACHED_SUCCESS
);
3557 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3558 assert(rc
== MEMCACHED_SUCCESS
);
3562 memcached_pool_st
* pool
;
3564 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3565 pthread_create(&tid
, NULL
, connection_release
, &item
);
3566 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3567 assert(rc
== MEMCACHED_SUCCESS
);
3568 pthread_join(tid
, NULL
);
3569 assert(mmc
[9] == item
.mmc
);
3570 const char *key
= "key";
3571 size_t keylen
= strlen(key
);
3573 // verify that I can do ops with all connections
3574 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3575 assert(rc
== MEMCACHED_SUCCESS
);
3577 for (unsigned int x
= 0; x
< 10; ++x
) {
3578 uint64_t number_value
;
3579 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3580 assert(rc
== MEMCACHED_SUCCESS
);
3581 assert(number_value
== (x
+1));
3585 for (int x
= 0; x
< 10; ++x
)
3586 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3588 assert(memcached_pool_destroy(pool
) == memc
);
3589 return TEST_SUCCESS
;
3593 static test_return
replication_set_test(memcached_st
*memc
)
3595 memcached_return rc
;
3596 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3597 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3599 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3600 assert(rc
== MEMCACHED_SUCCESS
);
3603 ** We are using the quiet commands to store the replicas, so we need
3604 ** to ensure that all of them are processed before we can continue.
3605 ** In the test we go directly from storing the object to trying to
3606 ** receive the object from all of the different servers, so we
3607 ** could end up in a race condition (the memcached server hasn't yet
3608 ** processed the quiet command from the replication set when it process
3609 ** the request from the other client (created by the clone)). As a
3610 ** workaround for that we call memcached_quit to send the quit command
3611 ** to the server and wait for the response ;-) If you use the test code
3612 ** as an example for your own code, please note that you shouldn't need
3615 memcached_quit(memc
);
3618 ** "bubba" should now be stored on all of our servers. We don't have an
3619 ** easy to use API to address each individual server, so I'll just iterate
3620 ** through a bunch of "master keys" and I should most likely hit all of the
3623 for (int x
= 'a'; x
<= 'z'; ++x
)
3625 char key
[2]= { [0]= (char)x
};
3628 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3630 assert(rc
== MEMCACHED_SUCCESS
);
3631 assert(val
!= NULL
);
3635 memcached_free(clone
);
3637 return TEST_SUCCESS
;
3640 static test_return
replication_get_test(memcached_st
*memc
)
3642 memcached_return rc
;
3645 * Don't do the following in your code. I am abusing the internal details
3646 * within the library, and this is not a supported interface.
3647 * This is to verify correct behavior in the library
3649 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
3651 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3652 clone
->hosts
[host
].port
= 0;
3654 for (int x
= 'a'; x
<= 'z'; ++x
)
3656 char key
[2]= { [0]= (char)x
};
3659 char *val
= memcached_get_by_key(clone
, key
, 1, "bubba", 5,
3661 assert(rc
== MEMCACHED_SUCCESS
);
3662 assert(val
!= NULL
);
3666 memcached_free(clone
);
3669 return TEST_SUCCESS
;
3672 static test_return
replication_mget_test(memcached_st
*memc
)
3674 memcached_return rc
;
3675 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3676 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3678 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3679 size_t len
[]= { 5, 4, 4, 4 };
3681 for (int x
=0; x
< 4; ++x
)
3683 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3684 assert(rc
== MEMCACHED_SUCCESS
);
3688 ** We are using the quiet commands to store the replicas, so we need
3689 ** to ensure that all of them are processed before we can continue.
3690 ** In the test we go directly from storing the object to trying to
3691 ** receive the object from all of the different servers, so we
3692 ** could end up in a race condition (the memcached server hasn't yet
3693 ** processed the quiet command from the replication set when it process
3694 ** the request from the other client (created by the clone)). As a
3695 ** workaround for that we call memcached_quit to send the quit command
3696 ** to the server and wait for the response ;-) If you use the test code
3697 ** as an example for your own code, please note that you shouldn't need
3700 memcached_quit(memc
);
3703 * Don't do the following in your code. I am abusing the internal details
3704 * within the library, and this is not a supported interface.
3705 * This is to verify correct behavior in the library
3707 memcached_result_st result_obj
;
3708 for (uint32_t host
= 0; host
< clone
->number_of_hosts
; host
++)
3710 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
3711 new_clone
->hosts
[host
].port
= 0;
3713 for (int x
= 'a'; x
<= 'z'; ++x
)
3715 char key
[2]= { [0]= (char)x
};
3717 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
3718 assert(rc
== MEMCACHED_SUCCESS
);
3720 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
3724 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
3729 memcached_result_free(&result_obj
);
3732 memcached_free(new_clone
);
3735 return TEST_SUCCESS
;
3738 static test_return
replication_delete_test(memcached_st
*memc
)
3740 memcached_return rc
;
3741 memcached_st
*clone
= memcached_clone(NULL
, memc
);
3742 /* Delete the items from all of the servers except 1 */
3743 uint64_t repl
= memcached_behavior_get(memc
,
3744 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3745 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3747 char *keys
[]= { "bubba", "key1", "key2", "key3" };
3748 size_t len
[]= { 5, 4, 4, 4 };
3750 for (int x
=0; x
< 4; ++x
)
3752 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
3753 assert(rc
== MEMCACHED_SUCCESS
);
3757 * Don't do the following in your code. I am abusing the internal details
3758 * within the library, and this is not a supported interface.
3759 * This is to verify correct behavior in the library
3761 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
3762 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
3764 clone
->hosts
[hash
].port
= 0;
3765 if (++hash
== clone
->number_of_hosts
)
3769 memcached_result_st result_obj
;
3770 for (uint32_t host
= 0; host
< clone
->number_of_hosts
; ++host
)
3772 for (int x
= 'a'; x
<= 'z'; ++x
)
3774 char key
[2]= { [0]= (char)x
};
3776 rc
= memcached_mget_by_key(clone
, key
, 1, keys
, len
, 4);
3777 assert(rc
== MEMCACHED_SUCCESS
);
3779 memcached_result_st
*results
= memcached_result_create(clone
, &result_obj
);
3783 while ((results
= memcached_fetch_result(clone
, &result_obj
, &rc
)) != NULL
)
3788 memcached_result_free(&result_obj
);
3791 memcached_free(clone
);
3793 return TEST_SUCCESS
;
3796 static void increment_request_id(uint16_t *id
)
3799 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
3803 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
3805 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
3806 assert(ids
!= NULL
);
3809 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3810 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
3815 static test_return
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
3818 memcached_server_st
*cur_server
= memc
->hosts
;
3819 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
3821 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3823 assert(cur_server
[x
].cursor_active
== 0);
3824 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
3826 free(expected_req_ids
);
3829 return TEST_SUCCESS
;
3833 ** There is a little bit of a hack here, instead of removing
3834 ** the servers, I just set num host to 0 and them add then new udp servers
3836 static memcached_return
init_udp(memcached_st
*memc
)
3838 memcached_version(memc
);
3839 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3840 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
3841 || memc
->hosts
[0].micro_version
< 6)
3842 return MEMCACHED_FAILURE
;
3844 uint32_t num_hosts
= memc
->number_of_hosts
;
3846 memcached_server_st servers
[num_hosts
];
3847 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
3848 for (x
= 0; x
< num_hosts
; x
++)
3849 memcached_server_free(&memc
->hosts
[x
]);
3851 memc
->number_of_hosts
= 0;
3852 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
3853 for (x
= 0; x
< num_hosts
; x
++)
3855 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
3856 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3859 return MEMCACHED_SUCCESS
;
3862 static memcached_return
binary_init_udp(memcached_st
*memc
)
3865 return init_udp(memc
);
3868 /* Make sure that I cant add a tcp server to a udp client */
3869 static test_return
add_tcp_server_udp_client_test(memcached_st
*memc
)
3871 memcached_server_st server
;
3872 memcached_server_clone(&server
, &memc
->hosts
[0]);
3873 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3874 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3875 return TEST_SUCCESS
;
3878 /* Make sure that I cant add a udp server to a tcp client */
3879 static test_return
add_udp_server_tcp_client_test(memcached_st
*memc
)
3881 memcached_server_st server
;
3882 memcached_server_clone(&server
, &memc
->hosts
[0]);
3883 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3885 memcached_st tcp_client
;
3886 memcached_create(&tcp_client
);
3887 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3888 return TEST_SUCCESS
;
3891 static test_return
set_udp_behavior_test(memcached_st
*memc
)
3894 memcached_quit(memc
);
3895 memc
->number_of_hosts
= 0;
3896 run_distribution(memc
);
3897 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
3898 assert(memc
->flags
& MEM_USE_UDP
);
3899 assert(memc
->flags
& MEM_NOREPLY
);;
3901 assert(memc
->number_of_hosts
== 0);
3903 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
3904 assert(!(memc
->flags
& MEM_USE_UDP
));
3905 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
3906 assert(!(memc
->flags
& MEM_NOREPLY
));
3907 return TEST_SUCCESS
;
3910 static test_return
udp_set_test(memcached_st
*memc
)
3913 unsigned int num_iters
= 1025; //request id rolls over at 1024
3914 for (x
= 0; x
< num_iters
;x
++)
3916 memcached_return rc
;
3918 char *value
= "when we sanitize";
3919 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3920 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
3921 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3922 rc
= memcached_set(memc
, key
, strlen(key
),
3923 value
, strlen(value
),
3924 (time_t)0, (uint32_t)0);
3925 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3926 /** NB, the check below assumes that if new write_ptr is less than
3927 * the original write_ptr that we have flushed. For large payloads, this
3928 * maybe an invalid assumption, but for the small payload we have it is OK
3930 if (rc
== MEMCACHED_SUCCESS
||
3931 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3932 increment_request_id(&expected_ids
[server_key
]);
3934 if (rc
== MEMCACHED_SUCCESS
)
3936 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3940 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3941 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3943 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3945 return TEST_SUCCESS
;
3948 static test_return
udp_buffered_set_test(memcached_st
*memc
)
3950 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3951 return udp_set_test(memc
);
3954 static test_return
udp_set_too_big_test(memcached_st
*memc
)
3956 memcached_return rc
;
3958 char value
[MAX_UDP_DATAGRAM_LENGTH
];
3959 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3960 rc
= memcached_set(memc
, key
, strlen(key
),
3961 value
, MAX_UDP_DATAGRAM_LENGTH
,
3962 (time_t)0, (uint32_t)0);
3963 assert(rc
== MEMCACHED_WRITE_FAILURE
);
3964 return post_udp_op_check(memc
,expected_ids
);
3967 static test_return
udp_delete_test(memcached_st
*memc
)
3970 unsigned int num_iters
= 1025; //request id rolls over at 1024
3971 for (x
= 0; x
< num_iters
;x
++)
3973 memcached_return rc
;
3975 uint16_t *expected_ids
=get_udp_request_ids(memc
);
3976 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3977 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3978 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
3979 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3980 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3981 increment_request_id(&expected_ids
[server_key
]);
3982 if (rc
== MEMCACHED_SUCCESS
)
3983 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3986 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3987 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3989 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3991 return TEST_SUCCESS
;
3994 static test_return
udp_buffered_delete_test(memcached_st
*memc
)
3996 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3997 return udp_delete_test(memc
);
4000 static test_return
udp_verbosity_test(memcached_st
*memc
)
4002 memcached_return rc
;
4003 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4005 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4006 increment_request_id(&expected_ids
[x
]);
4008 rc
= memcached_verbosity(memc
,3);
4009 assert(rc
== MEMCACHED_SUCCESS
);
4010 return post_udp_op_check(memc
,expected_ids
);
4013 static test_return
udp_quit_test(memcached_st
*memc
)
4015 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4016 memcached_quit(memc
);
4017 return post_udp_op_check(memc
, expected_ids
);
4020 static test_return
udp_flush_test(memcached_st
*memc
)
4022 memcached_return rc
;
4023 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4025 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4026 increment_request_id(&expected_ids
[x
]);
4028 rc
= memcached_flush(memc
,0);
4029 assert(rc
== MEMCACHED_SUCCESS
);
4030 return post_udp_op_check(memc
,expected_ids
);
4033 static test_return
udp_incr_test(memcached_st
*memc
)
4035 memcached_return rc
;
4038 rc
= memcached_set(memc
, key
, strlen(key
),
4039 value
, strlen(value
),
4040 (time_t)0, (uint32_t)0);
4042 assert(rc
== MEMCACHED_SUCCESS
);
4043 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4044 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4045 increment_request_id(&expected_ids
[server_key
]);
4047 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4048 assert(rc
== MEMCACHED_SUCCESS
);
4049 return post_udp_op_check(memc
, expected_ids
);
4052 static test_return
udp_decr_test(memcached_st
*memc
)
4054 memcached_return rc
;
4057 rc
= memcached_set(memc
, key
, strlen(key
),
4058 value
, strlen(value
),
4059 (time_t)0, (uint32_t)0);
4061 assert(rc
== MEMCACHED_SUCCESS
);
4062 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4063 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4064 increment_request_id(&expected_ids
[server_key
]);
4066 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4067 assert(rc
== MEMCACHED_SUCCESS
);
4068 return post_udp_op_check(memc
, expected_ids
);
4072 static test_return
udp_stat_test(memcached_st
*memc
)
4074 memcached_stat_st
* rv
= NULL
;
4075 memcached_return rc
;
4077 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4078 rv
= memcached_stat(memc
, args
, &rc
);
4080 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4081 return post_udp_op_check(memc
, expected_ids
);
4084 static test_return
udp_version_test(memcached_st
*memc
)
4086 memcached_return rc
;
4087 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4088 rc
= memcached_version(memc
);
4089 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4090 return post_udp_op_check(memc
, expected_ids
);
4093 static test_return
udp_get_test(memcached_st
*memc
)
4095 memcached_return rc
;
4098 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4099 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4100 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4101 assert(val
== NULL
);
4102 return post_udp_op_check(memc
, expected_ids
);
4105 static test_return
udp_mixed_io_test(memcached_st
*memc
)
4108 test_st mixed_io_ops
[] ={
4109 {"udp_set_test", 0, udp_set_test
},
4110 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4111 {"udp_delete_test", 0, udp_delete_test
},
4112 {"udp_verbosity_test", 0, udp_verbosity_test
},
4113 {"udp_quit_test", 0, udp_quit_test
},
4114 {"udp_flush_test", 0, udp_flush_test
},
4115 {"udp_incr_test", 0, udp_incr_test
},
4116 {"udp_decr_test", 0, udp_decr_test
},
4117 {"udp_version_test", 0, udp_version_test
}
4120 for (x
= 0; x
< 500; x
++)
4122 current_op
= mixed_io_ops
[random() % 9];
4123 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4125 return TEST_SUCCESS
;
4128 static test_return
hsieh_avaibility_test (memcached_st
*memc
)
4130 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4131 #ifdef HAVE_HSIEH_HASH
4132 expected_rc
= MEMCACHED_SUCCESS
;
4134 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4135 (uint64_t)MEMCACHED_HASH_HSIEH
);
4136 assert(rc
== expected_rc
);
4137 return TEST_SUCCESS
;
4140 static char *list
[]=
4170 static test_return
md5_run (memcached_st
*memc
__attribute__((unused
)))
4174 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4175 245758794U, 2550894432U, 121710495U, 3053817768U,
4176 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4177 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4178 2138080750U, 217675895U, 2230934345U, 1234361223U,
4179 3968582726U, 2455685270U, 1293568479U, 199067604U,
4183 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4187 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4188 assert(values
[x
] == hash_val
);
4191 return TEST_SUCCESS
;
4194 static test_return
crc_run (memcached_st
*memc
__attribute__((unused
)))
4198 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4199 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4200 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4201 17300U, 28156U, 9192U, 20484U, 16925U };
4203 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4207 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4208 assert(values
[x
] == hash_val
);
4211 return TEST_SUCCESS
;
4214 static test_return
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4218 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4219 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4220 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4221 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4222 2815549194U, 2562818319U, 224996066U, 2680194749U,
4223 3035305390U, 246890365U, 2395624193U, 4145193337U,
4226 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4230 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4231 assert(values
[x
] == hash_val
);
4234 return TEST_SUCCESS
;
4237 static test_return
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4241 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4242 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4243 734810122U, 1516407546U, 560948863U, 1767346780U,
4244 561034892U, 4156330026U, 3716417003U, 3475297030U,
4245 1518272172U, 227211583U, 3938128828U, 126112909U,
4246 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4249 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4253 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4254 assert(values
[x
] == hash_val
);
4257 return TEST_SUCCESS
;
4260 static test_return
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4264 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4265 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4266 344481048U, 3865235296U, 2181839183U, 119581266U,
4267 510234242U, 4248244304U, 1362796839U, 103389328U,
4268 1449620010U, 182962511U, 3554262370U, 3206747549U,
4269 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4273 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4277 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4278 assert(values
[x
] == hash_val
);
4281 return TEST_SUCCESS
;
4284 static test_return
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4288 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4289 459261019U, 3521085446U, 18738364U, 1625305005U,
4290 2162232970U, 777243802U, 3323728671U, 132336572U,
4291 3654473228U, 260679466U, 1169454059U, 2698319462U,
4292 1062177260U, 235516991U, 2218399068U, 405302637U,
4293 1128467232U, 3579622413U, 2138539289U, 96429129U,
4296 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4300 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4301 assert(values
[x
] == hash_val
);
4304 return TEST_SUCCESS
;
4307 static test_return
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4311 #ifdef HAVE_HSIEH_HASH
4312 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4313 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4314 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4315 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4318 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 };
4321 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4325 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4326 assert(values
[x
] == hash_val
);
4329 return TEST_SUCCESS
;
4332 static test_return
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4336 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4337 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4338 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4339 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4340 2815549194U, 2562818319U, 224996066U, 2680194749U,
4341 3035305390U, 246890365U, 2395624193U, 4145193337U,
4344 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4348 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4349 assert(values
[x
] == hash_val
);
4352 return TEST_SUCCESS
;
4355 static test_return
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4359 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4360 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4361 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4362 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4363 4153263658U, 3824871822U, 3072759809U, 798622255U,
4364 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4368 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4372 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4373 assert(values
[x
] == hash_val
);
4376 return TEST_SUCCESS
;
4379 test_st udp_setup_server_tests
[] ={
4380 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
4381 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
4382 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
4386 test_st upd_io_tests
[] ={
4387 {"udp_set_test", 0, udp_set_test
},
4388 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
4389 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4390 {"udp_delete_test", 0, udp_delete_test
},
4391 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
4392 {"udp_verbosity_test", 0, udp_verbosity_test
},
4393 {"udp_quit_test", 0, udp_quit_test
},
4394 {"udp_flush_test", 0, udp_flush_test
},
4395 {"udp_incr_test", 0, udp_incr_test
},
4396 {"udp_decr_test", 0, udp_decr_test
},
4397 {"udp_stat_test", 0, udp_stat_test
},
4398 {"udp_version_test", 0, udp_version_test
},
4399 {"udp_get_test", 0, udp_get_test
},
4400 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
4404 /* Clean the server before beginning testing */
4406 {"flush", 0, flush_test
},
4407 {"init", 0, init_test
},
4408 {"allocation", 0, allocation_test
},
4409 {"server_list_null_test", 0, server_list_null_test
},
4410 {"server_unsort", 0, server_unsort_test
},
4411 {"server_sort", 0, server_sort_test
},
4412 {"server_sort2", 0, server_sort2_test
},
4413 {"clone_test", 0, clone_test
},
4414 {"connection_test", 0, connection_test
},
4415 {"callback_test", 0, callback_test
},
4416 {"behavior_test", 0, behavior_test
},
4417 {"userdata_test", 0, userdata_test
},
4418 {"error", 0, error_test
},
4419 {"set", 0, set_test
},
4420 {"set2", 0, set_test2
},
4421 {"set3", 0, set_test3
},
4422 {"dump", 1, dump_test
},
4423 {"add", 1, add_test
},
4424 {"replace", 1, replace_test
},
4425 {"delete", 1, delete_test
},
4426 {"get", 1, get_test
},
4427 {"get2", 0, get_test2
},
4428 {"get3", 0, get_test3
},
4429 {"get4", 0, get_test4
},
4430 {"partial mget", 0, get_test5
},
4431 {"stats_servername", 0, stats_servername_test
},
4432 {"increment", 0, increment_test
},
4433 {"increment_with_initial", 1, increment_with_initial_test
},
4434 {"decrement", 0, decrement_test
},
4435 {"decrement_with_initial", 1, decrement_with_initial_test
},
4436 {"quit", 0, quit_test
},
4437 {"mget", 1, mget_test
},
4438 {"mget_result", 1, mget_result_test
},
4439 {"mget_result_alloc", 1, mget_result_alloc_test
},
4440 {"mget_result_function", 1, mget_result_function
},
4441 {"get_stats", 0, get_stats
},
4442 {"add_host_test", 0, add_host_test
},
4443 {"add_host_test_1", 0, add_host_test1
},
4444 {"get_stats_keys", 0, get_stats_keys
},
4445 {"behavior_test", 0, get_stats_keys
},
4446 {"callback_test", 0, get_stats_keys
},
4447 {"version_string_test", 0, version_string_test
},
4448 {"bad_key", 1, bad_key_test
},
4449 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
4450 {"read_through", 1, read_through
},
4451 {"delete_through", 1, delete_through
},
4452 {"noreply", 1, noreply_test
},
4453 {"analyzer", 1, analyzer_test
},
4454 #ifdef HAVE_LIBMEMCACHEDUTIL
4455 {"connectionpool", 1, connection_pool_test
},
4460 test_st async_tests
[] ={
4461 {"add", 1, add_wrapper
},
4465 test_st string_tests
[] ={
4466 {"string static with null", 0, string_static_null
},
4467 {"string alloc with null", 0, string_alloc_null
},
4468 {"string alloc with 1K", 0, string_alloc_with_size
},
4469 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
4470 {"string append", 0, string_alloc_append
},
4471 {"string append failure (too big)", 0, string_alloc_append_toobig
},
4475 test_st result_tests
[] ={
4476 {"result static", 0, result_static
},
4477 {"result alloc", 0, result_alloc
},
4481 test_st version_1_2_3
[] ={
4482 {"append", 0, append_test
},
4483 {"prepend", 0, prepend_test
},
4484 {"cas", 0, cas_test
},
4485 {"cas2", 0, cas2_test
},
4486 {"append_binary", 0, append_binary_test
},
4490 test_st user_tests
[] ={
4491 {"user_supplied_bug1", 0, user_supplied_bug1
},
4492 {"user_supplied_bug2", 0, user_supplied_bug2
},
4493 {"user_supplied_bug3", 0, user_supplied_bug3
},
4494 {"user_supplied_bug4", 0, user_supplied_bug4
},
4495 {"user_supplied_bug5", 1, user_supplied_bug5
},
4496 {"user_supplied_bug6", 1, user_supplied_bug6
},
4497 {"user_supplied_bug7", 1, user_supplied_bug7
},
4498 {"user_supplied_bug8", 1, user_supplied_bug8
},
4499 {"user_supplied_bug9", 1, user_supplied_bug9
},
4500 {"user_supplied_bug10", 1, user_supplied_bug10
},
4501 {"user_supplied_bug11", 1, user_supplied_bug11
},
4502 {"user_supplied_bug12", 1, user_supplied_bug12
},
4503 {"user_supplied_bug13", 1, user_supplied_bug13
},
4504 {"user_supplied_bug14", 1, user_supplied_bug14
},
4505 {"user_supplied_bug15", 1, user_supplied_bug15
},
4506 {"user_supplied_bug16", 1, user_supplied_bug16
},
4509 ** It seems to be something weird with the character sets..
4510 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4511 ** guess I need to find out how this is supposed to work.. Perhaps I need
4512 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4513 ** so just disable the code for now...).
4515 {"user_supplied_bug17", 1, user_supplied_bug17
},
4517 {"user_supplied_bug18", 1, user_supplied_bug18
},
4518 {"user_supplied_bug19", 1, user_supplied_bug19
},
4519 {"user_supplied_bug20", 1, user_supplied_bug20
},
4523 test_st replication_tests
[]= {
4524 {"set", 1, replication_set_test
},
4525 {"get", 0, replication_get_test
},
4526 {"mget", 0, replication_mget_test
},
4527 {"delete", 0, replication_delete_test
},
4531 test_st generate_tests
[] ={
4532 {"generate_pairs", 1, generate_pairs
},
4533 {"generate_data", 1, generate_data
},
4534 {"get_read", 0, get_read
},
4535 {"delete_generate", 0, delete_generate
},
4536 {"generate_buffer_data", 1, generate_buffer_data
},
4537 {"delete_buffer", 0, delete_buffer_generate
},
4538 {"generate_data", 1, generate_data
},
4539 {"mget_read", 0, mget_read
},
4540 {"mget_read_result", 0, mget_read_result
},
4541 {"mget_read_function", 0, mget_read_function
},
4542 {"cleanup", 1, cleanup_pairs
},
4543 {"generate_large_pairs", 1, generate_large_pairs
},
4544 {"generate_data", 1, generate_data
},
4545 {"generate_buffer_data", 1, generate_buffer_data
},
4546 {"cleanup", 1, cleanup_pairs
},
4550 test_st consistent_tests
[] ={
4551 {"generate_pairs", 1, generate_pairs
},
4552 {"generate_data", 1, generate_data
},
4553 {"get_read", 0, get_read_count
},
4554 {"cleanup", 1, cleanup_pairs
},
4558 test_st consistent_weighted_tests
[] ={
4559 {"generate_pairs", 1, generate_pairs
},
4560 {"generate_data", 1, generate_data_with_stats
},
4561 {"get_read", 0, get_read_count
},
4562 {"cleanup", 1, cleanup_pairs
},
4566 test_st hsieh_availability
[] ={
4567 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
4571 test_st ketama_auto_eject_hosts
[] ={
4572 {"auto_eject_hosts", 1, auto_eject_hosts
},
4576 test_st hash_tests
[] ={
4577 {"md5", 0, md5_run
},
4578 {"crc", 0, crc_run
},
4579 {"fnv1_64", 0, fnv1_64_run
},
4580 {"fnv1a_64", 0, fnv1a_64_run
},
4581 {"fnv1_32", 0, fnv1_32_run
},
4582 {"fnv1a_32", 0, fnv1a_32_run
},
4583 {"hsieh", 0, hsieh_run
},
4584 {"murmur", 0, murmur_run
},
4585 {"jenkis", 0, jenkins_run
},
4589 collection_st collection
[] ={
4590 {"hsieh_availability",0,0,hsieh_availability
},
4591 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
4592 {"udp_io", init_udp
, 0, upd_io_tests
},
4593 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
4594 {"block", 0, 0, tests
},
4595 {"binary", pre_binary
, 0, tests
},
4596 {"nonblock", pre_nonblock
, 0, tests
},
4597 {"nodelay", pre_nodelay
, 0, tests
},
4598 {"settimer", pre_settimer
, 0, tests
},
4599 {"md5", pre_md5
, 0, tests
},
4600 {"crc", pre_crc
, 0, tests
},
4601 {"hsieh", pre_hsieh
, 0, tests
},
4602 {"jenkins", pre_jenkins
, 0, tests
},
4603 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
4604 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
4605 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
4606 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
4607 {"ketama", pre_behavior_ketama
, 0, tests
},
4608 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
4609 {"unix_socket", pre_unix_socket
, 0, tests
},
4610 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
4611 {"poll_timeout", poll_timeout
, 0, tests
},
4612 {"gets", enable_cas
, 0, tests
},
4613 {"consistent", enable_consistent
, 0, tests
},
4614 #ifdef MEMCACHED_ENABLE_DEPRECATED
4615 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
4617 {"memory_allocators", set_memory_alloc
, 0, tests
},
4618 {"prefix", set_prefix
, 0, tests
},
4619 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
4620 {"string", 0, 0, string_tests
},
4621 {"result", 0, 0, result_tests
},
4622 {"async", pre_nonblock
, 0, async_tests
},
4623 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
4624 {"user", 0, 0, user_tests
},
4625 {"generate", 0, 0, generate_tests
},
4626 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
4627 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
4628 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
4629 {"generate_md5", pre_md5
, 0, generate_tests
},
4630 {"generate_murmur", pre_murmur
, 0, generate_tests
},
4631 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
4632 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
4633 {"consistent_not", 0, 0, consistent_tests
},
4634 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
4635 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
4636 {"test_hashes", 0, 0, hash_tests
},
4637 {"replication", pre_replication
, 0, replication_tests
},
4638 {"replication_noblock", pre_replication_noblock
, 0, replication_tests
},
4642 #define SERVERS_TO_CREATE 5
4644 /* Prototypes for functions we will pass to test framework */
4645 void *world_create(void);
4646 void world_destroy(void *p
);
4648 void *world_create(void)
4650 server_startup_st
*construct
;
4652 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
4653 memset(construct
, 0, sizeof(server_startup_st
));
4654 construct
->count
= SERVERS_TO_CREATE
;
4656 server_startup(construct
);
4662 void world_destroy(void *p
)
4664 server_startup_st
*construct
= (server_startup_st
*)p
;
4665 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
4666 memcached_server_list_free(servers
);
4668 server_shutdown(construct
);
4672 void get_world(world_st
*world
)
4674 world
->collections
= collection
;
4675 world
->create
= world_create
;
4676 world
->destroy
= world_destroy
;