2 Sample test application.
5 #include "libmemcached/common.h"
12 #include <sys/types.h>
18 #include "clients/generator.h"
19 #include "clients/execute.h"
22 #define INT64_MAX LONG_MAX
25 #define INT32_MAX INT_MAX
31 #ifdef HAVE_LIBMEMCACHEDUTIL
33 #include "libmemcached/memcached_util.h"
36 #define GLOBAL_COUNT 10000
37 #define GLOBAL2_COUNT 100
38 #define SERVERS_TO_CREATE 5
39 static uint32_t global_count
;
41 static pairs_st
*global_pairs
;
42 static const char *global_keys
[GLOBAL_COUNT
];
43 static size_t global_keys_length
[GLOBAL_COUNT
];
45 static test_return_t
init_test(memcached_st
*not_used
__attribute__((unused
)))
49 (void)memcached_create(&memc
);
50 memcached_free(&memc
);
55 static test_return_t
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
57 memcached_server_st
*server_list
;
60 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
61 test_truth(server_list
== NULL
);
63 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
64 test_truth(server_list
== NULL
);
66 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
67 test_truth(server_list
== NULL
);
72 #define TEST_PORT_COUNT 7
73 uint32_t test_ports
[TEST_PORT_COUNT
];
75 static memcached_return
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
78 uint32_t bigger
= *((uint32_t *)(context
));
79 assert(bigger
<= server
->port
);
80 *((uint32_t *)(context
))= server
->port
;
82 return MEMCACHED_SUCCESS
;
85 static test_return_t
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
88 uint32_t bigger
= 0; /* Prime the value for the test_truth in server_display_function */
90 memcached_server_function callbacks
[1];
91 memcached_st
*local_memc
;
93 local_memc
= memcached_create(NULL
);
94 test_truth(local_memc
);
95 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
97 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
99 test_ports
[x
]= (uint32_t)random() % 64000;
100 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
101 test_truth(local_memc
->number_of_hosts
== x
+ 1);
102 test_truth(local_memc
->hosts
[0].count
== x
+1);
103 test_truth(rc
== MEMCACHED_SUCCESS
);
106 callbacks
[0]= server_display_function
;
107 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
110 memcached_free(local_memc
);
115 static test_return_t
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
117 uint32_t bigger
= 0; /* Prime the value for the test_truth in server_display_function */
119 memcached_server_function callbacks
[1];
120 memcached_st
*local_memc
;
122 local_memc
= memcached_create(NULL
);
123 test_truth(local_memc
);
124 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
125 test_truth(rc
== MEMCACHED_SUCCESS
);
127 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
128 test_truth(rc
== MEMCACHED_SUCCESS
);
129 test_truth(local_memc
->hosts
[0].port
== 43043);
131 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
132 test_truth(rc
== MEMCACHED_SUCCESS
);
133 test_truth(local_memc
->hosts
[0].port
== 43042);
134 test_truth(local_memc
->hosts
[1].port
== 43043);
136 callbacks
[0]= server_display_function
;
137 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
140 memcached_free(local_memc
);
145 static memcached_return
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
148 uint32_t x
= *((uint32_t *)(context
));
150 assert(test_ports
[x
] == server
->port
);
151 *((uint32_t *)(context
))= ++x
;
153 return MEMCACHED_SUCCESS
;
156 static test_return_t
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
159 uint32_t counter
= 0; /* Prime the value for the test_truth in server_display_function */
160 uint32_t bigger
= 0; /* Prime the value for the test_truth in server_display_function */
162 memcached_server_function callbacks
[1];
163 memcached_st
*local_memc
;
165 local_memc
= memcached_create(NULL
);
166 test_truth(local_memc
);
168 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
170 test_ports
[x
]= (uint32_t)(random() % 64000);
171 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
172 test_truth(local_memc
->number_of_hosts
== x
+1);
173 test_truth(local_memc
->hosts
[0].count
== x
+1);
174 test_truth(rc
== MEMCACHED_SUCCESS
);
177 callbacks
[0]= server_display_unsort_function
;
178 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
180 /* Now we sort old data! */
181 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
182 callbacks
[0]= server_display_function
;
183 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
186 memcached_free(local_memc
);
191 static test_return_t
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
194 memc
= memcached_create(NULL
);
196 memcached_free(memc
);
201 static test_return_t
clone_test(memcached_st
*memc
)
205 memcached_st
*memc_clone
;
206 memc_clone
= memcached_clone(NULL
, NULL
);
207 test_truth(memc_clone
);
208 memcached_free(memc_clone
);
211 /* Can we init from null? */
213 memcached_st
*memc_clone
;
214 memc_clone
= memcached_clone(NULL
, memc
);
215 test_truth(memc_clone
);
217 test_truth(memc_clone
->call_free
== memc
->call_free
);
218 test_truth(memc_clone
->call_malloc
== memc
->call_malloc
);
219 test_truth(memc_clone
->call_realloc
== memc
->call_realloc
);
220 test_truth(memc_clone
->call_calloc
== memc
->call_calloc
);
221 test_truth(memc_clone
->connect_timeout
== memc
->connect_timeout
);
222 test_truth(memc_clone
->delete_trigger
== memc
->delete_trigger
);
223 test_truth(memc_clone
->distribution
== memc
->distribution
);
224 test_truth(memc_clone
->flags
== memc
->flags
);
225 test_truth(memc_clone
->get_key_failure
== memc
->get_key_failure
);
226 test_truth(memc_clone
->hash
== memc
->hash
);
227 test_truth(memc_clone
->hash_continuum
== memc
->hash_continuum
);
228 test_truth(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
229 test_truth(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
230 test_truth(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
231 test_truth(memc_clone
->on_cleanup
== memc
->on_cleanup
);
232 test_truth(memc_clone
->on_clone
== memc
->on_clone
);
233 test_truth(memc_clone
->poll_timeout
== memc
->poll_timeout
);
234 test_truth(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
235 test_truth(memc_clone
->recv_size
== memc
->recv_size
);
236 test_truth(memc_clone
->retry_timeout
== memc
->retry_timeout
);
237 test_truth(memc_clone
->send_size
== memc
->send_size
);
238 test_truth(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
239 test_truth(memc_clone
->snd_timeout
== memc
->snd_timeout
);
240 test_truth(memc_clone
->user_data
== memc
->user_data
);
242 memcached_free(memc_clone
);
245 /* Can we init from struct? */
247 memcached_st declared_clone
;
248 memcached_st
*memc_clone
;
249 memset(&declared_clone
, 0 , sizeof(memcached_st
));
250 memc_clone
= memcached_clone(&declared_clone
, NULL
);
251 test_truth(memc_clone
);
252 memcached_free(memc_clone
);
255 /* Can we init from struct? */
257 memcached_st declared_clone
;
258 memcached_st
*memc_clone
;
259 memset(&declared_clone
, 0 , sizeof(memcached_st
));
260 memc_clone
= memcached_clone(&declared_clone
, memc
);
261 test_truth(memc_clone
);
262 memcached_free(memc_clone
);
268 static test_return_t
userdata_test(memcached_st
*memc
)
271 test_truth(memcached_set_user_data(memc
, foo
) == NULL
);
272 test_truth(memcached_get_user_data(memc
) == foo
);
273 test_truth(memcached_set_user_data(memc
, NULL
) == foo
);
278 static test_return_t
connection_test(memcached_st
*memc
)
282 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
283 test_truth(rc
== MEMCACHED_SUCCESS
);
288 static test_return_t
error_test(memcached_st
*memc
)
291 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
292 982370485U, 1263635348U, 4242906218U, 3829656100U,
293 1891735253U, 334139633U, 2257084983U, 3088286104U,
294 13199785U, 2542027183U, 1097051614U, 199566778U,
295 2748246961U, 2465192557U, 1664094137U, 2405439045U,
296 1842224848U, 692413798U, 3479807801U, 919913813U,
297 4269430871U, 610793021U, 527273862U, 1437122909U,
298 2300930706U, 2943759320U, 674306647U, 2400528935U,
299 54481931U, 4186304426U, 1741088401U, 2979625118U,
300 4159057246U, 3425930182U, 2593724503U};
302 // You have updated the memcache_error messages but not updated docs/tests.
303 test_truth(MEMCACHED_MAXIMUM_RETURN
== 39);
304 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
307 const char *msg
= memcached_strerror(memc
, rc
);
308 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
309 MEMCACHED_HASH_JENKINS
);
310 test_truth(values
[rc
] == hash_val
);
316 static test_return_t
set_test(memcached_st
*memc
)
319 const char *key
= "foo";
320 const char *value
= "when we sanitize";
322 rc
= memcached_set(memc
, key
, strlen(key
),
323 value
, strlen(value
),
324 (time_t)0, (uint32_t)0);
325 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
330 static test_return_t
append_test(memcached_st
*memc
)
333 const char *key
= "fig";
334 const char *in_value
= "we";
335 char *out_value
= NULL
;
339 rc
= memcached_flush(memc
, 0);
340 test_truth(rc
== MEMCACHED_SUCCESS
);
342 rc
= memcached_set(memc
, key
, strlen(key
),
343 in_value
, strlen(in_value
),
344 (time_t)0, (uint32_t)0);
345 test_truth(rc
== MEMCACHED_SUCCESS
);
347 rc
= memcached_append(memc
, key
, strlen(key
),
348 " the", strlen(" the"),
349 (time_t)0, (uint32_t)0);
350 test_truth(rc
== MEMCACHED_SUCCESS
);
352 rc
= memcached_append(memc
, key
, strlen(key
),
353 " people", strlen(" people"),
354 (time_t)0, (uint32_t)0);
355 test_truth(rc
== MEMCACHED_SUCCESS
);
357 out_value
= memcached_get(memc
, key
, strlen(key
),
358 &value_length
, &flags
, &rc
);
359 test_truth(!memcmp(out_value
, "we the people", strlen("we the people")));
360 test_truth(strlen("we the people") == value_length
);
361 test_truth(rc
== MEMCACHED_SUCCESS
);
367 static test_return_t
append_binary_test(memcached_st
*memc
)
370 const char *key
= "numbers";
371 unsigned int *store_ptr
;
372 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
378 rc
= memcached_flush(memc
, 0);
379 test_truth(rc
== MEMCACHED_SUCCESS
);
381 rc
= memcached_set(memc
,
384 (time_t)0, (uint32_t)0);
385 test_truth(rc
== MEMCACHED_SUCCESS
);
387 for (x
= 0; store_list
[x
] ; x
++)
389 rc
= memcached_append(memc
,
391 (char *)&store_list
[x
], sizeof(unsigned int),
392 (time_t)0, (uint32_t)0);
393 test_truth(rc
== MEMCACHED_SUCCESS
);
396 value
= memcached_get(memc
, key
, strlen(key
),
397 &value_length
, &flags
, &rc
);
398 test_truth((value_length
== (sizeof(unsigned int) * x
)));
399 test_truth(rc
== MEMCACHED_SUCCESS
);
401 store_ptr
= (unsigned int *)value
;
403 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
405 test_truth(*store_ptr
== store_list
[x
++]);
413 static test_return_t
cas2_test(memcached_st
*memc
)
416 const char *keys
[]= {"fudge", "son", "food"};
417 size_t key_length
[]= {5, 3, 4};
418 const char *value
= "we the people";
419 size_t value_length
= strlen("we the people");
421 memcached_result_st results_obj
;
422 memcached_result_st
*results
;
425 rc
= memcached_flush(memc
, 0);
426 test_truth(rc
== MEMCACHED_SUCCESS
);
428 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
430 for (x
= 0; x
< 3; x
++)
432 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
433 keys
[x
], key_length
[x
],
434 (time_t)50, (uint32_t)9);
435 test_truth(rc
== MEMCACHED_SUCCESS
);
438 rc
= memcached_mget(memc
, keys
, key_length
, 3);
440 results
= memcached_result_create(memc
, &results_obj
);
442 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
444 test_truth(results
->cas
);
445 test_truth(rc
== MEMCACHED_SUCCESS
);
446 test_truth(memcached_result_cas(results
));
448 test_truth(!memcmp(value
, "we the people", strlen("we the people")));
449 test_truth(strlen("we the people") == value_length
);
450 test_truth(rc
== MEMCACHED_SUCCESS
);
452 memcached_result_free(&results_obj
);
457 static test_return_t
cas_test(memcached_st
*memc
)
460 const char *key
= "fun";
461 size_t key_length
= strlen(key
);
462 const char *value
= "we the people";
463 const char* keys
[2] = { key
, NULL
};
464 size_t keylengths
[2] = { strlen(key
), 0 };
465 size_t value_length
= strlen(value
);
466 const char *value2
= "change the value";
467 size_t value2_length
= strlen(value2
);
469 memcached_result_st results_obj
;
470 memcached_result_st
*results
;
473 rc
= memcached_flush(memc
, 0);
474 test_truth(rc
== MEMCACHED_SUCCESS
);
476 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
478 rc
= memcached_set(memc
, key
, strlen(key
),
479 value
, strlen(value
),
480 (time_t)0, (uint32_t)0);
481 test_truth(rc
== MEMCACHED_SUCCESS
);
483 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
485 results
= memcached_result_create(memc
, &results_obj
);
487 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
489 test_truth(rc
== MEMCACHED_SUCCESS
);
490 test_truth(memcached_result_cas(results
));
491 test_truth(!memcmp(value
, memcached_result_value(results
), value_length
));
492 test_truth(strlen(memcached_result_value(results
)) == value_length
);
493 test_truth(rc
== MEMCACHED_SUCCESS
);
494 uint64_t cas
= memcached_result_cas(results
);
497 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
498 test_truth(rc
== MEMCACHED_END
);
499 test_truth(results
== NULL
);
502 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
503 test_truth(rc
== MEMCACHED_SUCCESS
);
506 * The item will have a new cas value, so try to set it again with the old
507 * value. This should fail!
509 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
510 test_truth(rc
== MEMCACHED_DATA_EXISTS
);
512 memcached_result_free(&results_obj
);
517 static test_return_t
prepend_test(memcached_st
*memc
)
520 const char *key
= "fig";
521 const char *value
= "people";
522 char *out_value
= NULL
;
526 rc
= memcached_flush(memc
, 0);
527 test_truth(rc
== MEMCACHED_SUCCESS
);
529 rc
= memcached_set(memc
, key
, strlen(key
),
530 value
, strlen(value
),
531 (time_t)0, (uint32_t)0);
532 test_truth(rc
== MEMCACHED_SUCCESS
);
534 rc
= memcached_prepend(memc
, key
, strlen(key
),
535 "the ", strlen("the "),
536 (time_t)0, (uint32_t)0);
537 test_truth(rc
== MEMCACHED_SUCCESS
);
539 rc
= memcached_prepend(memc
, key
, strlen(key
),
540 "we ", strlen("we "),
541 (time_t)0, (uint32_t)0);
542 test_truth(rc
== MEMCACHED_SUCCESS
);
544 out_value
= memcached_get(memc
, key
, strlen(key
),
545 &value_length
, &flags
, &rc
);
546 test_truth(!memcmp(out_value
, "we the people", strlen("we the people")));
547 test_truth(strlen("we the people") == value_length
);
548 test_truth(rc
== MEMCACHED_SUCCESS
);
555 Set the value, then quit to make sure it is flushed.
556 Come back in and test that add fails.
558 static test_return_t
add_test(memcached_st
*memc
)
561 const char *key
= "foo";
562 const char *value
= "when we sanitize";
563 unsigned long long setting_value
;
565 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
567 rc
= memcached_set(memc
, key
, strlen(key
),
568 value
, strlen(value
),
569 (time_t)0, (uint32_t)0);
570 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
571 memcached_quit(memc
);
572 rc
= memcached_add(memc
, key
, strlen(key
),
573 value
, strlen(value
),
574 (time_t)0, (uint32_t)0);
576 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
579 test_truth(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
583 test_truth(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
590 ** There was a problem of leaking filedescriptors in the initial release
591 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
592 ** systems it seems that the kernel is slow on reclaiming the resources
593 ** because the connects starts to time out (the test doesn't do much
594 ** anyway, so just loop 10 iterations)
596 static test_return_t
add_wrapper(memcached_st
*memc
)
599 unsigned int max
= 10000;
607 for (x
= 0; x
< max
; x
++)
613 static test_return_t
replace_test(memcached_st
*memc
)
616 const char *key
= "foo";
617 const char *value
= "when we sanitize";
618 const char *original
= "first we insert some data";
620 rc
= memcached_set(memc
, key
, strlen(key
),
621 original
, strlen(original
),
622 (time_t)0, (uint32_t)0);
623 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
625 rc
= memcached_replace(memc
, key
, strlen(key
),
626 value
, strlen(value
),
627 (time_t)0, (uint32_t)0);
628 test_truth(rc
== MEMCACHED_SUCCESS
);
633 static test_return_t
delete_test(memcached_st
*memc
)
636 const char *key
= "foo";
637 const char *value
= "when we sanitize";
639 rc
= memcached_set(memc
, key
, strlen(key
),
640 value
, strlen(value
),
641 (time_t)0, (uint32_t)0);
642 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
644 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
645 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
650 static test_return_t
flush_test(memcached_st
*memc
)
654 rc
= memcached_flush(memc
, 0);
655 test_truth(rc
== MEMCACHED_SUCCESS
);
660 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
661 memcached_server_st
*server
__attribute__((unused
)),
662 void *context
__attribute__((unused
)))
666 return MEMCACHED_SUCCESS
;
669 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
672 strcpy(context
, "foo bad");
673 memcached_server_function callbacks
[1];
675 callbacks
[0]= server_function
;
676 memcached_server_cursor(memc
, callbacks
, context
, 1);
680 static test_return_t
bad_key_test(memcached_st
*memc
)
683 const char *key
= "foo bad";
685 size_t string_length
;
687 memcached_st
*memc_clone
;
689 size_t max_keylen
= 0xffff;
691 memc_clone
= memcached_clone(NULL
, memc
);
692 test_truth(memc_clone
);
694 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
695 test_truth(rc
== MEMCACHED_SUCCESS
);
697 /* All keys are valid in the binary protocol (except for length) */
698 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
700 string
= memcached_get(memc_clone
, key
, strlen(key
),
701 &string_length
, &flags
, &rc
);
702 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
703 test_truth(string_length
== 0);
707 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
708 test_truth(rc
== MEMCACHED_SUCCESS
);
709 string
= memcached_get(memc_clone
, key
, strlen(key
),
710 &string_length
, &flags
, &rc
);
711 test_truth(rc
== MEMCACHED_NOTFOUND
);
712 test_truth(string_length
== 0);
715 /* Test multi key for bad keys */
716 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
717 size_t key_lengths
[] = { 7, 7, 7 };
719 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
720 test_truth(rc
== MEMCACHED_SUCCESS
);
722 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
723 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
725 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
726 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
730 /* The following test should be moved to the end of this function when the
731 memcached server is updated to allow max size length of the keys in the
734 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
735 test_truth(rc
== MEMCACHED_SUCCESS
);
737 char *longkey
= malloc(max_keylen
+ 1);
740 memset(longkey
, 'a', max_keylen
+ 1);
741 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
742 &string_length
, &flags
, &rc
);
743 test_truth(rc
== MEMCACHED_NOTFOUND
);
744 test_truth(string_length
== 0);
747 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
748 &string_length
, &flags
, &rc
);
749 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
750 test_truth(string_length
== 0);
757 /* Make sure zero length keys are marked as bad */
759 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
760 test_truth(rc
== MEMCACHED_SUCCESS
);
761 string
= memcached_get(memc_clone
, key
, 0,
762 &string_length
, &flags
, &rc
);
763 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
764 test_truth(string_length
== 0);
767 memcached_free(memc_clone
);
772 #define READ_THROUGH_VALUE "set for me"
773 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
774 char *key
__attribute__((unused
)),
775 size_t key_length
__attribute__((unused
)),
776 memcached_result_st
*result
)
779 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
782 static test_return_t
read_through(memcached_st
*memc
)
785 const char *key
= "foo";
787 size_t string_length
;
789 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
791 string
= memcached_get(memc
, key
, strlen(key
),
792 &string_length
, &flags
, &rc
);
794 test_truth(rc
== MEMCACHED_NOTFOUND
);
795 test_truth(string_length
== 0);
798 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
800 test_truth(rc
== MEMCACHED_SUCCESS
);
802 string
= memcached_get(memc
, key
, strlen(key
),
803 &string_length
, &flags
, &rc
);
805 test_truth(rc
== MEMCACHED_SUCCESS
);
806 test_truth(string_length
== strlen(READ_THROUGH_VALUE
));
807 test_truth(!strcmp(READ_THROUGH_VALUE
, string
));
810 string
= memcached_get(memc
, key
, strlen(key
),
811 &string_length
, &flags
, &rc
);
813 test_truth(rc
== MEMCACHED_SUCCESS
);
814 test_truth(string_length
== strlen(READ_THROUGH_VALUE
));
815 test_truth(!strcmp(READ_THROUGH_VALUE
, string
));
821 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
823 size_t key_length
__attribute__((unused
)))
827 return MEMCACHED_SUCCESS
;
830 static test_return_t
delete_through(memcached_st
*memc
)
832 memcached_trigger_delete_key callback
;
835 callback
= (memcached_trigger_delete_key
)delete_trigger
;
837 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
838 test_truth(rc
== MEMCACHED_SUCCESS
);
843 static test_return_t
get_test(memcached_st
*memc
)
846 const char *key
= "foo";
848 size_t string_length
;
851 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
852 test_truth(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
854 string
= memcached_get(memc
, key
, strlen(key
),
855 &string_length
, &flags
, &rc
);
857 test_truth(rc
== MEMCACHED_NOTFOUND
);
858 test_truth(string_length
== 0);
864 static test_return_t
get_test2(memcached_st
*memc
)
867 const char *key
= "foo";
868 const char *value
= "when we sanitize";
870 size_t string_length
;
873 rc
= memcached_set(memc
, key
, strlen(key
),
874 value
, strlen(value
),
875 (time_t)0, (uint32_t)0);
876 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
878 string
= memcached_get(memc
, key
, strlen(key
),
879 &string_length
, &flags
, &rc
);
882 test_truth(rc
== MEMCACHED_SUCCESS
);
883 test_truth(string_length
== strlen(value
));
884 test_truth(!memcmp(string
, value
, string_length
));
891 static test_return_t
set_test2(memcached_st
*memc
)
894 const char *key
= "foo";
895 const char *value
= "train in the brain";
896 size_t value_length
= strlen(value
);
899 for (x
= 0; x
< 10; x
++)
901 rc
= memcached_set(memc
, key
, strlen(key
),
903 (time_t)0, (uint32_t)0);
904 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
910 static test_return_t
set_test3(memcached_st
*memc
)
914 size_t value_length
= 8191;
917 value
= (char*)malloc(value_length
);
920 for (x
= 0; x
< value_length
; x
++)
921 value
[x
] = (char) (x
% 127);
923 /* The dump test relies on there being at least 32 items in memcached */
924 for (x
= 0; x
< 32; x
++)
928 sprintf(key
, "foo%u", x
);
930 rc
= memcached_set(memc
, key
, strlen(key
),
932 (time_t)0, (uint32_t)0);
933 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
941 static test_return_t
get_test3(memcached_st
*memc
)
944 const char *key
= "foo";
946 size_t value_length
= 8191;
948 size_t string_length
;
952 value
= (char*)malloc(value_length
);
955 for (x
= 0; x
< value_length
; x
++)
956 value
[x
] = (char) (x
% 127);
958 rc
= memcached_set(memc
, key
, strlen(key
),
960 (time_t)0, (uint32_t)0);
961 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
963 string
= memcached_get(memc
, key
, strlen(key
),
964 &string_length
, &flags
, &rc
);
966 test_truth(rc
== MEMCACHED_SUCCESS
);
968 test_truth(string_length
== value_length
);
969 test_truth(!memcmp(string
, value
, string_length
));
977 static test_return_t
get_test4(memcached_st
*memc
)
980 const char *key
= "foo";
982 size_t value_length
= 8191;
984 size_t string_length
;
988 value
= (char*)malloc(value_length
);
991 for (x
= 0; x
< value_length
; x
++)
992 value
[x
] = (char) (x
% 127);
994 rc
= memcached_set(memc
, key
, strlen(key
),
996 (time_t)0, (uint32_t)0);
997 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
999 for (x
= 0; x
< 10; x
++)
1001 string
= memcached_get(memc
, key
, strlen(key
),
1002 &string_length
, &flags
, &rc
);
1004 test_truth(rc
== MEMCACHED_SUCCESS
);
1006 test_truth(string_length
== value_length
);
1007 test_truth(!memcmp(string
, value
, string_length
));
1013 return TEST_SUCCESS
;
1017 * This test verifies that memcached_read_one_response doesn't try to
1018 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1019 * responses before you execute a storage command.
1021 static test_return_t
get_test5(memcached_st
*memc
)
1024 ** Request the same key twice, to ensure that we hash to the same server
1025 ** (so that we have multiple response values queued up) ;-)
1027 const char *keys
[]= { "key", "key" };
1028 size_t lengths
[]= { 3, 3 };
1032 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1033 keys
[0], lengths
[0], 0, 0);
1034 test_truth(rc
== MEMCACHED_SUCCESS
);
1035 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1037 memcached_result_st results_obj
;
1038 memcached_result_st
*results
;
1039 results
=memcached_result_create(memc
, &results_obj
);
1040 test_truth(results
);
1041 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1042 test_truth(results
);
1043 memcached_result_free(&results_obj
);
1045 /* Don't read out the second result, but issue a set instead.. */
1046 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1047 test_truth(rc
== MEMCACHED_SUCCESS
);
1049 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1050 &rlen
, &flags
, &rc
);
1051 test_truth(val
== NULL
);
1052 test_truth(rc
== MEMCACHED_NOTFOUND
);
1053 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1054 test_truth(val
!= NULL
);
1055 test_truth(rc
== MEMCACHED_SUCCESS
);
1058 return TEST_SUCCESS
;
1061 static test_return_t
mget_end(memcached_st
*memc
)
1063 const char *keys
[]= { "foo", "foo2" };
1064 size_t lengths
[]= { 3, 4 };
1065 const char *values
[]= { "fjord", "41" };
1067 memcached_return rc
;
1070 for (int i
= 0; i
< 2; i
++)
1072 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1073 (time_t)0, (uint32_t)0);
1074 test_truth(rc
== MEMCACHED_SUCCESS
);
1078 size_t string_length
;
1081 // retrieve both via mget
1082 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1083 test_truth(rc
== MEMCACHED_SUCCESS
);
1085 char key
[MEMCACHED_MAX_KEY
];
1088 // this should get both
1089 for (int i
= 0; i
< 2; i
++)
1091 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1093 test_truth(rc
== MEMCACHED_SUCCESS
);
1095 if (key_length
== 4)
1097 test_truth(string_length
== strlen(values
[val
]));
1098 test_truth(strncmp(values
[val
], string
, string_length
) == 0);
1102 // this should indicate end
1103 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1104 test_truth(rc
== MEMCACHED_END
);
1107 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1108 test_truth(rc
== MEMCACHED_SUCCESS
);
1110 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1111 test_truth(key_length
== lengths
[0]);
1112 test_truth(strncmp(keys
[0], key
, key_length
) == 0);
1113 test_truth(string_length
== strlen(values
[0]));
1114 test_truth(strncmp(values
[0], string
, string_length
) == 0);
1115 test_truth(rc
== MEMCACHED_SUCCESS
);
1118 // this should indicate end
1119 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1120 test_truth(rc
== MEMCACHED_END
);
1122 return TEST_SUCCESS
;
1125 /* Do not copy the style of this code, I just access hosts to testthis function */
1126 static test_return_t
stats_servername_test(memcached_st
*memc
)
1128 memcached_return rc
;
1129 memcached_stat_st memc_stat
;
1130 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1131 memc
->hosts
[0].hostname
,
1132 memc
->hosts
[0].port
);
1134 return TEST_SUCCESS
;
1137 static test_return_t
increment_test(memcached_st
*memc
)
1139 uint64_t new_number
;
1140 memcached_return rc
;
1141 const char *key
= "number";
1142 const char *value
= "0";
1144 rc
= memcached_set(memc
, key
, strlen(key
),
1145 value
, strlen(value
),
1146 (time_t)0, (uint32_t)0);
1147 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1149 rc
= memcached_increment(memc
, key
, strlen(key
),
1151 test_truth(rc
== MEMCACHED_SUCCESS
);
1152 test_truth(new_number
== 1);
1154 rc
= memcached_increment(memc
, key
, strlen(key
),
1156 test_truth(rc
== MEMCACHED_SUCCESS
);
1157 test_truth(new_number
== 2);
1159 return TEST_SUCCESS
;
1162 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1164 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1166 uint64_t new_number
;
1167 memcached_return rc
;
1168 const char *key
= "number";
1169 uint64_t initial
= 0;
1171 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1172 1, initial
, 0, &new_number
);
1173 test_truth(rc
== MEMCACHED_SUCCESS
);
1174 test_truth(new_number
== initial
);
1176 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1177 1, initial
, 0, &new_number
);
1178 test_truth(rc
== MEMCACHED_SUCCESS
);
1179 test_truth(new_number
== (initial
+ 1));
1181 return TEST_SUCCESS
;
1184 static test_return_t
decrement_test(memcached_st
*memc
)
1186 uint64_t new_number
;
1187 memcached_return rc
;
1188 const char *key
= "number";
1189 const char *value
= "3";
1191 rc
= memcached_set(memc
, key
, strlen(key
),
1192 value
, strlen(value
),
1193 (time_t)0, (uint32_t)0);
1194 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1196 rc
= memcached_decrement(memc
, key
, strlen(key
),
1198 test_truth(rc
== MEMCACHED_SUCCESS
);
1199 test_truth(new_number
== 2);
1201 rc
= memcached_decrement(memc
, key
, strlen(key
),
1203 test_truth(rc
== MEMCACHED_SUCCESS
);
1204 test_truth(new_number
== 1);
1206 return TEST_SUCCESS
;
1209 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1211 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1213 uint64_t new_number
;
1214 memcached_return rc
;
1215 const char *key
= "number";
1216 uint64_t initial
= 3;
1218 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1219 1, initial
, 0, &new_number
);
1220 test_truth(rc
== MEMCACHED_SUCCESS
);
1221 test_truth(new_number
== initial
);
1223 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1224 1, initial
, 0, &new_number
);
1225 test_truth(rc
== MEMCACHED_SUCCESS
);
1226 test_truth(new_number
== (initial
- 1));
1228 return TEST_SUCCESS
;
1231 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1233 uint64_t new_number
;
1234 memcached_return rc
;
1235 const char *master_key
= "foo";
1236 const char *key
= "number";
1237 const char *value
= "0";
1239 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1241 value
, strlen(value
),
1242 (time_t)0, (uint32_t)0);
1243 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1245 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1247 test_truth(rc
== MEMCACHED_SUCCESS
);
1248 test_truth(new_number
== 1);
1250 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1252 test_truth(rc
== MEMCACHED_SUCCESS
);
1253 test_truth(new_number
== 2);
1255 return TEST_SUCCESS
;
1258 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1260 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1262 uint64_t new_number
;
1263 memcached_return rc
;
1264 const char *master_key
= "foo";
1265 const char *key
= "number";
1266 uint64_t initial
= 0;
1268 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1270 1, initial
, 0, &new_number
);
1271 test_truth(rc
== MEMCACHED_SUCCESS
);
1272 test_truth(new_number
== initial
);
1274 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1276 1, initial
, 0, &new_number
);
1277 test_truth(rc
== MEMCACHED_SUCCESS
);
1278 test_truth(new_number
== (initial
+ 1));
1280 return TEST_SUCCESS
;
1283 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1285 uint64_t new_number
;
1286 memcached_return rc
;
1287 const char *master_key
= "foo";
1288 const char *key
= "number";
1289 const char *value
= "3";
1291 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1293 value
, strlen(value
),
1294 (time_t)0, (uint32_t)0);
1295 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1297 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1300 test_truth(rc
== MEMCACHED_SUCCESS
);
1301 test_truth(new_number
== 2);
1303 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1306 test_truth(rc
== MEMCACHED_SUCCESS
);
1307 test_truth(new_number
== 1);
1309 return TEST_SUCCESS
;
1312 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1314 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1316 uint64_t new_number
;
1317 memcached_return rc
;
1318 const char *master_key
= "foo";
1319 const char *key
= "number";
1320 uint64_t initial
= 3;
1322 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1324 1, initial
, 0, &new_number
);
1325 test_truth(rc
== MEMCACHED_SUCCESS
);
1326 test_truth(new_number
== initial
);
1328 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1330 1, initial
, 0, &new_number
);
1331 test_truth(rc
== MEMCACHED_SUCCESS
);
1332 test_truth(new_number
== (initial
- 1));
1334 return TEST_SUCCESS
;
1337 static test_return_t
quit_test(memcached_st
*memc
)
1339 memcached_return rc
;
1340 const char *key
= "fudge";
1341 const char *value
= "sanford and sun";
1343 rc
= memcached_set(memc
, key
, strlen(key
),
1344 value
, strlen(value
),
1345 (time_t)10, (uint32_t)3);
1346 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1347 memcached_quit(memc
);
1349 rc
= memcached_set(memc
, key
, strlen(key
),
1350 value
, strlen(value
),
1351 (time_t)50, (uint32_t)9);
1352 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1354 return TEST_SUCCESS
;
1357 static test_return_t
mget_result_test(memcached_st
*memc
)
1359 memcached_return rc
;
1360 const char *keys
[]= {"fudge", "son", "food"};
1361 size_t key_length
[]= {5, 3, 4};
1364 memcached_result_st results_obj
;
1365 memcached_result_st
*results
;
1367 results
= memcached_result_create(memc
, &results_obj
);
1368 test_truth(results
);
1369 test_truth(&results_obj
== results
);
1371 /* We need to empty the server before continueing test */
1372 rc
= memcached_flush(memc
, 0);
1373 test_truth(rc
== MEMCACHED_SUCCESS
);
1375 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1376 test_truth(rc
== MEMCACHED_SUCCESS
);
1378 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1380 test_truth(results
);
1383 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1384 test_truth(!results
);
1385 test_truth(rc
== MEMCACHED_END
);
1387 for (x
= 0; x
< 3; x
++)
1389 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1390 keys
[x
], key_length
[x
],
1391 (time_t)50, (uint32_t)9);
1392 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1395 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1396 test_truth(rc
== MEMCACHED_SUCCESS
);
1398 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1400 test_truth(results
);
1401 test_truth(&results_obj
== results
);
1402 test_truth(rc
== MEMCACHED_SUCCESS
);
1403 test_truth(memcached_result_key_length(results
) == memcached_result_length(results
));
1404 test_truth(!memcmp(memcached_result_key_value(results
),
1405 memcached_result_value(results
),
1406 memcached_result_length(results
)));
1409 memcached_result_free(&results_obj
);
1411 return TEST_SUCCESS
;
1414 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1416 memcached_return rc
;
1417 const char *keys
[]= {"fudge", "son", "food"};
1418 size_t key_length
[]= {5, 3, 4};
1421 memcached_result_st
*results
;
1423 /* We need to empty the server before continueing test */
1424 rc
= memcached_flush(memc
, 0);
1425 test_truth(rc
== MEMCACHED_SUCCESS
);
1427 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1428 test_truth(rc
== MEMCACHED_SUCCESS
);
1430 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1432 test_truth(results
);
1434 test_truth(!results
);
1435 test_truth(rc
== MEMCACHED_END
);
1437 for (x
= 0; x
< 3; x
++)
1439 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1440 keys
[x
], key_length
[x
],
1441 (time_t)50, (uint32_t)9);
1442 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1445 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1446 test_truth(rc
== MEMCACHED_SUCCESS
);
1449 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1451 test_truth(results
);
1452 test_truth(rc
== MEMCACHED_SUCCESS
);
1453 test_truth(memcached_result_key_length(results
) == memcached_result_length(results
));
1454 test_truth(!memcmp(memcached_result_key_value(results
),
1455 memcached_result_value(results
),
1456 memcached_result_length(results
)));
1457 memcached_result_free(results
);
1461 return TEST_SUCCESS
;
1464 /* Count the results */
1465 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1466 memcached_result_st
*result
__attribute__((unused
)),
1469 unsigned int *counter
= (unsigned int *)context
;
1471 *counter
= *counter
+ 1;
1473 return MEMCACHED_SUCCESS
;
1476 static test_return_t
mget_result_function(memcached_st
*memc
)
1478 memcached_return rc
;
1479 const char *keys
[]= {"fudge", "son", "food"};
1480 size_t key_length
[]= {5, 3, 4};
1482 unsigned int counter
;
1483 memcached_execute_function callbacks
[1];
1485 /* We need to empty the server before continueing test */
1486 rc
= memcached_flush(memc
, 0);
1487 for (x
= 0; x
< 3; x
++)
1489 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1490 keys
[x
], key_length
[x
],
1491 (time_t)50, (uint32_t)9);
1492 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1495 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1496 test_truth(rc
== MEMCACHED_SUCCESS
);
1498 callbacks
[0]= &callback_counter
;
1500 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1502 test_truth(counter
== 3);
1504 return TEST_SUCCESS
;
1507 static test_return_t
mget_test(memcached_st
*memc
)
1509 memcached_return rc
;
1510 const char *keys
[]= {"fudge", "son", "food"};
1511 size_t key_length
[]= {5, 3, 4};
1515 char return_key
[MEMCACHED_MAX_KEY
];
1516 size_t return_key_length
;
1518 size_t return_value_length
;
1520 /* We need to empty the server before continueing test */
1521 rc
= memcached_flush(memc
, 0);
1522 test_truth(rc
== MEMCACHED_SUCCESS
);
1524 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1525 test_truth(rc
== MEMCACHED_SUCCESS
);
1527 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1528 &return_value_length
, &flags
, &rc
)) != NULL
)
1530 test_truth(return_value
);
1532 test_truth(!return_value
);
1533 test_truth(return_value_length
== 0);
1534 test_truth(rc
== MEMCACHED_END
);
1536 for (x
= 0; x
< 3; x
++)
1538 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1539 keys
[x
], key_length
[x
],
1540 (time_t)50, (uint32_t)9);
1541 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1544 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1545 test_truth(rc
== MEMCACHED_SUCCESS
);
1548 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1549 &return_value_length
, &flags
, &rc
)))
1551 test_truth(return_value
);
1552 test_truth(rc
== MEMCACHED_SUCCESS
);
1553 test_truth(return_key_length
== return_value_length
);
1554 test_truth(!memcmp(return_value
, return_key
, return_value_length
));
1559 return TEST_SUCCESS
;
1562 static test_return_t
mget_execute(memcached_st
*memc
)
1565 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1569 * I only want to hit _one_ server so I know the number of requests I'm
1570 * sending in the pipeline.
1572 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1573 memc
->number_of_hosts
= 1;
1575 int max_keys
= binary
? 20480 : 1;
1578 char **keys
= calloc((size_t)max_keys
, sizeof(char*));
1579 size_t *key_length
=calloc((size_t)max_keys
, sizeof(size_t));
1581 /* First add all of the items.. */
1582 char blob
[1024] = {0};
1583 memcached_return rc
;
1584 for (int x
= 0; x
< max_keys
; ++x
)
1587 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
1589 test_truth(keys
[x
] != NULL
);
1590 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1591 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1594 /* Try to get all of them with a large multiget */
1595 unsigned int counter
= 0;
1596 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
1597 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1598 (size_t)max_keys
, callbacks
, &counter
, 1);
1602 test_truth(rc
== MEMCACHED_SUCCESS
);
1604 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1605 test_truth(rc
== MEMCACHED_END
);
1607 /* Verify that we got all of the items */
1608 test_truth(counter
== (unsigned int)max_keys
);
1612 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
1613 test_truth(counter
== 0);
1616 /* Release all allocated resources */
1617 for (int x
= 0; x
< max_keys
; ++x
)
1622 memc
->number_of_hosts
= number_of_hosts
;
1623 return TEST_SUCCESS
;
1626 static test_return_t
get_stats_keys(memcached_st
*memc
)
1630 memcached_stat_st memc_stat
;
1631 memcached_return rc
;
1633 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1634 test_truth(rc
== MEMCACHED_SUCCESS
);
1635 for (ptr
= list
; *ptr
; ptr
++)
1641 return TEST_SUCCESS
;
1644 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1646 const char *version_string
;
1648 version_string
= memcached_lib_version();
1650 test_truth(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1652 return TEST_SUCCESS
;
1655 static test_return_t
get_stats(memcached_st
*memc
)
1660 memcached_return rc
;
1661 memcached_stat_st
*memc_stat
;
1663 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1664 test_truth(rc
== MEMCACHED_SUCCESS
);
1666 test_truth(rc
== MEMCACHED_SUCCESS
);
1667 test_truth(memc_stat
);
1669 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1671 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1672 test_truth(rc
== MEMCACHED_SUCCESS
);
1673 for (ptr
= list
; *ptr
; ptr
++);
1678 memcached_stat_free(NULL
, memc_stat
);
1680 return TEST_SUCCESS
;
1683 static test_return_t
add_host_test(memcached_st
*memc
)
1686 memcached_server_st
*servers
;
1687 memcached_return rc
;
1688 char servername
[]= "0.example.com";
1690 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1691 test_truth(servers
);
1692 test_truth(1 == memcached_server_list_count(servers
));
1694 for (x
= 2; x
< 20; x
++)
1696 char buffer
[SMALL_STRING_LEN
];
1698 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1699 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1701 test_truth(rc
== MEMCACHED_SUCCESS
);
1702 test_truth(x
== memcached_server_list_count(servers
));
1705 rc
= memcached_server_push(memc
, servers
);
1706 test_truth(rc
== MEMCACHED_SUCCESS
);
1707 rc
= memcached_server_push(memc
, servers
);
1708 test_truth(rc
== MEMCACHED_SUCCESS
);
1710 memcached_server_list_free(servers
);
1712 return TEST_SUCCESS
;
1715 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1717 return MEMCACHED_SUCCESS
;
1720 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1722 return MEMCACHED_SUCCESS
;
1725 static test_return_t
callback_test(memcached_st
*memc
)
1727 /* Test User Data */
1731 memcached_return rc
;
1733 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1734 test_truth(rc
== MEMCACHED_SUCCESS
);
1735 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1736 test_truth(*test_ptr
== x
);
1739 /* Test Clone Callback */
1741 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1742 void *clone_cb_ptr
= *(void **)&clone_cb
;
1743 void *temp_function
= NULL
;
1744 memcached_return rc
;
1746 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1748 test_truth(rc
== MEMCACHED_SUCCESS
);
1749 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1750 test_truth(temp_function
== clone_cb_ptr
);
1753 /* Test Cleanup Callback */
1755 memcached_cleanup_func cleanup_cb
=
1756 (memcached_cleanup_func
)cleanup_test_callback
;
1757 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1758 void *temp_function
= NULL
;
1759 memcached_return rc
;
1761 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1763 test_truth(rc
== MEMCACHED_SUCCESS
);
1764 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1765 test_truth(temp_function
== cleanup_cb_ptr
);
1768 return TEST_SUCCESS
;
1771 /* We don't test the behavior itself, we test the switches */
1772 static test_return_t
behavior_test(memcached_st
*memc
)
1777 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1778 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1779 test_truth(value
== 1);
1781 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1782 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1783 test_truth(value
== 1);
1785 set
= MEMCACHED_HASH_MD5
;
1786 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1787 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1788 test_truth(value
== MEMCACHED_HASH_MD5
);
1792 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1793 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1794 test_truth(value
== 0);
1796 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1797 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1798 test_truth(value
== 0);
1800 set
= MEMCACHED_HASH_DEFAULT
;
1801 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1802 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1803 test_truth(value
== MEMCACHED_HASH_DEFAULT
);
1805 set
= MEMCACHED_HASH_CRC
;
1806 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1807 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1808 test_truth(value
== MEMCACHED_HASH_CRC
);
1810 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1811 test_truth(value
> 0);
1813 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1814 test_truth(value
> 0);
1816 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1817 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1818 test_truth((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1819 return TEST_SUCCESS
;
1822 static test_return_t
fetch_all_results(memcached_st
*memc
)
1824 memcached_return rc
= MEMCACHED_SUCCESS
;
1825 char return_key
[MEMCACHED_MAX_KEY
];
1826 size_t return_key_length
;
1828 size_t return_value_length
;
1831 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1832 &return_value_length
, &flags
, &rc
)))
1834 test_truth(return_value
);
1835 test_truth(rc
== MEMCACHED_SUCCESS
);
1839 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
1842 /* Test case provided by Cal Haldenbrand */
1843 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
1845 unsigned int setter
= 1;
1848 unsigned long long total
= 0;
1851 char randomstuff
[6 * 1024];
1852 memcached_return rc
;
1854 memset(randomstuff
, 0, 6 * 1024);
1856 /* We just keep looking at the same values over and over */
1859 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1860 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1864 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1868 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1869 memset(randomstuff
, 0, 6 * 1024);
1870 test_truth(size
< 6 * 1024); /* Being safe here */
1872 for (j
= 0 ; j
< size
;j
++)
1873 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1876 sprintf(key
, "%d", x
);
1877 rc
= memcached_set(memc
, key
, strlen(key
),
1878 randomstuff
, strlen(randomstuff
), 10, 0);
1879 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1880 /* If we fail, lets try again */
1881 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1882 rc
= memcached_set(memc
, key
, strlen(key
),
1883 randomstuff
, strlen(randomstuff
), 10, 0);
1884 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1887 return TEST_SUCCESS
;
1890 /* Test case provided by Cal Haldenbrand */
1891 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
1894 unsigned int setter
;
1896 unsigned long long total
;
1899 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1900 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1902 setter
= 20 * 1024576;
1903 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1904 setter
= 20 * 1024576;
1905 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1906 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1907 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1909 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1912 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1914 memcached_return rc
= MEMCACHED_SUCCESS
;
1915 char buffer
[SMALL_STRING_LEN
];
1920 memset(buffer
, 0, SMALL_STRING_LEN
);
1922 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1923 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1924 &val_len
, &flags
, &rc
);
1925 if (rc
!= MEMCACHED_SUCCESS
)
1927 if (rc
== MEMCACHED_NOTFOUND
)
1941 return TEST_SUCCESS
;
1944 /* Do a large mget() over all the keys we think exist */
1945 #define KEY_COUNT 3000 // * 1024576
1946 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
1948 memcached_return rc
;
1949 unsigned int setter
;
1952 size_t key_lengths
[KEY_COUNT
];
1955 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1956 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1958 setter
= 20 * 1024576;
1959 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1960 setter
= 20 * 1024576;
1961 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1962 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1963 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1966 keys
= calloc(KEY_COUNT
, sizeof(char *));
1968 for (x
= 0; x
< KEY_COUNT
; x
++)
1972 snprintf(buffer
, 30, "%u", x
);
1973 keys
[x
]= strdup(buffer
);
1974 key_lengths
[x
]= strlen(keys
[x
]);
1977 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
1978 test_truth(rc
== MEMCACHED_SUCCESS
);
1980 test_truth(fetch_all_results(memc
) == TEST_SUCCESS
);
1982 for (x
= 0; x
< KEY_COUNT
; x
++)
1986 return TEST_SUCCESS
;
1989 /* Make sure we behave properly if server list has no values */
1990 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
1992 memcached_return rc
;
1993 const char *keys
[]= {"fudge", "son", "food"};
1994 size_t key_length
[]= {5, 3, 4};
1997 char return_key
[MEMCACHED_MAX_KEY
];
1998 size_t return_key_length
;
2000 size_t return_value_length
;
2002 /* Here we free everything before running a bunch of mget tests */
2004 memcached_server_list_free(memc
->hosts
);
2006 memc
->number_of_hosts
= 0;
2010 /* We need to empty the server before continueing test */
2011 rc
= memcached_flush(memc
, 0);
2012 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2014 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2015 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2017 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2018 &return_value_length
, &flags
, &rc
)) != NULL
)
2020 test_truth(return_value
);
2022 test_truth(!return_value
);
2023 test_truth(return_value_length
== 0);
2024 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2026 for (x
= 0; x
< 3; x
++)
2028 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2029 keys
[x
], key_length
[x
],
2030 (time_t)50, (uint32_t)9);
2031 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2034 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2035 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2038 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2039 &return_value_length
, &flags
, &rc
)))
2041 test_truth(return_value
);
2042 test_truth(rc
== MEMCACHED_SUCCESS
);
2043 test_truth(return_key_length
== return_value_length
);
2044 test_truth(!memcmp(return_value
, return_key
, return_value_length
));
2049 return TEST_SUCCESS
;
2052 #define VALUE_SIZE_BUG5 1048064
2053 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2055 memcached_return rc
;
2056 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2057 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2058 char return_key
[MEMCACHED_MAX_KEY
];
2059 size_t return_key_length
;
2061 size_t value_length
;
2065 char insert_data
[VALUE_SIZE_BUG5
];
2067 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2068 insert_data
[x
]= (signed char)rand();
2070 memcached_flush(memc
, 0);
2071 value
= memcached_get(memc
, keys
[0], key_length
[0],
2072 &value_length
, &flags
, &rc
);
2073 test_truth(value
== NULL
);
2074 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2077 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2078 &value_length
, &flags
, &rc
)))
2080 test_truth(count
== 0);
2082 for (x
= 0; x
< 4; x
++)
2084 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2085 insert_data
, VALUE_SIZE_BUG5
,
2086 (time_t)0, (uint32_t)0);
2087 test_truth(rc
== MEMCACHED_SUCCESS
);
2090 for (x
= 0; x
< 10; x
++)
2092 value
= memcached_get(memc
, keys
[0], key_length
[0],
2093 &value_length
, &flags
, &rc
);
2097 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2099 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2100 &value_length
, &flags
, &rc
)))
2105 test_truth(count
== 4);
2108 return TEST_SUCCESS
;
2111 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2113 memcached_return rc
;
2114 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2115 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2116 char return_key
[MEMCACHED_MAX_KEY
];
2117 size_t return_key_length
;
2119 size_t value_length
;
2123 char insert_data
[VALUE_SIZE_BUG5
];
2125 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2126 insert_data
[x
]= (signed char)rand();
2128 memcached_flush(memc
, 0);
2129 value
= memcached_get(memc
, keys
[0], key_length
[0],
2130 &value_length
, &flags
, &rc
);
2131 test_truth(value
== NULL
);
2132 test_truth(rc
== MEMCACHED_NOTFOUND
);
2133 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2134 test_truth(rc
== MEMCACHED_SUCCESS
);
2137 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2138 &value_length
, &flags
, &rc
)))
2140 test_truth(count
== 0);
2141 test_truth(rc
== MEMCACHED_END
);
2143 for (x
= 0; x
< 4; x
++)
2145 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2146 insert_data
, VALUE_SIZE_BUG5
,
2147 (time_t)0, (uint32_t)0);
2148 test_truth(rc
== MEMCACHED_SUCCESS
);
2151 for (x
= 0; x
< 2; x
++)
2153 value
= memcached_get(memc
, keys
[0], key_length
[0],
2154 &value_length
, &flags
, &rc
);
2158 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2159 test_truth(rc
== MEMCACHED_SUCCESS
);
2161 /* We test for purge of partial complete fetches */
2162 for (count
= 3; count
; count
--)
2164 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2165 &value_length
, &flags
, &rc
);
2166 test_truth(rc
== MEMCACHED_SUCCESS
);
2167 test_truth(!(memcmp(value
, insert_data
, value_length
)));
2168 test_truth(value_length
);
2173 return TEST_SUCCESS
;
2176 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2178 memcached_return rc
;
2180 memcached_st
*memc_clone
;
2182 memcached_server_st
*servers
;
2183 const 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";
2185 servers
= memcached_servers_parse(server_list
);
2186 test_truth(servers
);
2188 mine
= memcached_create(NULL
);
2189 rc
= memcached_server_push(mine
, servers
);
2190 test_truth(rc
== MEMCACHED_SUCCESS
);
2191 memcached_server_list_free(servers
);
2194 memc_clone
= memcached_clone(NULL
, mine
);
2196 memcached_quit(mine
);
2197 memcached_quit(memc_clone
);
2200 memcached_free(mine
);
2201 memcached_free(memc_clone
);
2203 return TEST_SUCCESS
;
2206 /* Test flag store/retrieve */
2207 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2209 memcached_return rc
;
2210 const char *keys
= "036790384900";
2211 size_t key_length
= strlen(keys
);
2212 char return_key
[MEMCACHED_MAX_KEY
];
2213 size_t return_key_length
;
2215 size_t value_length
;
2218 char insert_data
[VALUE_SIZE_BUG5
];
2220 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2221 insert_data
[x
]= (signed char)rand();
2223 memcached_flush(memc
, 0);
2226 rc
= memcached_set(memc
, keys
, key_length
,
2227 insert_data
, VALUE_SIZE_BUG5
,
2229 test_truth(rc
== MEMCACHED_SUCCESS
);
2232 value
= memcached_get(memc
, keys
, key_length
,
2233 &value_length
, &flags
, &rc
);
2234 test_truth(flags
== 245);
2238 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2241 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2242 &value_length
, &flags
, &rc
);
2243 test_truth(flags
== 245);
2248 return TEST_SUCCESS
;
2251 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2253 memcached_return rc
;
2254 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2255 size_t key_length
[3];
2260 char return_key
[MEMCACHED_MAX_KEY
];
2261 size_t return_key_length
;
2263 size_t return_value_length
;
2266 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2267 key_length
[1]= strlen("fudge&*@#");
2268 key_length
[2]= strlen("for^#@&$not");
2271 for (x
= 0; x
< 3; x
++)
2273 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2274 keys
[x
], key_length
[x
],
2275 (time_t)50, (uint32_t)9);
2276 test_truth(rc
== MEMCACHED_SUCCESS
);
2279 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2280 test_truth(rc
== MEMCACHED_SUCCESS
);
2282 /* We need to empty the server before continueing test */
2283 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2284 &return_value_length
, &flags
, &rc
)) != NULL
)
2286 test_truth(return_value
);
2290 test_truth(count
== 3);
2292 return TEST_SUCCESS
;
2295 /* We are testing with aggressive timeout to get failures */
2296 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2298 const char *key
= "foo";
2300 size_t value_length
= 512;
2303 memcached_return rc
;
2304 unsigned int set
= 1;
2305 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2308 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2309 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2311 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2314 value
= (char*)malloc(value_length
* sizeof(char));
2316 for (x
= 0; x
< value_length
; x
++)
2317 value
[x
]= (char) (x
% 127);
2319 for (x
= 1; x
<= 100000; ++x
)
2321 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2323 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2324 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2326 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2331 memcached_free(mclone
);
2333 return TEST_SUCCESS
;
2337 We are looking failures in the async protocol
2339 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2341 const char *key
= "foo";
2343 size_t value_length
= 512;
2346 memcached_return rc
;
2347 unsigned int set
= 1;
2349 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2351 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2352 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2354 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2357 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2359 test_truth(timeout
== -1);
2361 value
= (char*)malloc(value_length
* sizeof(char));
2363 for (x
= 0; x
< value_length
; x
++)
2364 value
[x
]= (char) (x
% 127);
2366 for (x
= 1; x
<= 100000; ++x
)
2368 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2372 memcached_free(mclone
);
2374 return TEST_SUCCESS
;
2378 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2380 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2382 memcached_return rc
;
2384 size_t value_length
;
2386 uint64_t number_value
;
2388 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2389 &value_length
, &flags
, &rc
);
2390 test_truth(value
== NULL
);
2391 test_truth(rc
== MEMCACHED_NOTFOUND
);
2393 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2396 test_truth(value
== NULL
);
2397 /* The binary protocol will set the key if it doesn't exist */
2398 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2400 test_truth(rc
== MEMCACHED_SUCCESS
);
2404 test_truth(rc
== MEMCACHED_NOTFOUND
);
2407 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2409 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2410 &value_length
, &flags
, &rc
);
2412 test_truth(rc
== MEMCACHED_SUCCESS
);
2415 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2417 test_truth(number_value
== 2);
2418 test_truth(rc
== MEMCACHED_SUCCESS
);
2420 return TEST_SUCCESS
;
2424 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2425 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2427 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2429 char key
[] = "key34567890";
2431 memcached_return rc
;
2432 size_t overflowSize
;
2434 char commandFirst
[]= "set key34567890 0 0 ";
2435 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2436 size_t commandLength
;
2439 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2441 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2443 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2445 overflow
= malloc(testSize
);
2446 test_truth(overflow
!= NULL
);
2448 memset(overflow
, 'x', testSize
);
2449 rc
= memcached_set(memc
, key
, strlen(key
),
2450 overflow
, testSize
, 0, 0);
2451 test_truth(rc
== MEMCACHED_SUCCESS
);
2455 return TEST_SUCCESS
;
2460 Test values of many different sizes
2461 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2462 set key34567890 0 0 8169 \r\n
2463 is sent followed by buffer of size 8169, followed by 8169
2465 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2468 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2469 memcached_return rc
;
2470 const char *key
= "foo";
2472 size_t value_length
= 18000;
2474 size_t string_length
;
2477 size_t current_length
;
2479 value
= (char*)malloc(value_length
);
2482 for (x
= 0; x
< value_length
; x
++)
2483 value
[x
] = (char) (x
% 127);
2485 for (current_length
= 0; current_length
< value_length
; current_length
++)
2487 rc
= memcached_set(memc
, key
, strlen(key
),
2488 value
, current_length
,
2489 (time_t)0, (uint32_t)0);
2490 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2492 string
= memcached_get(memc
, key
, strlen(key
),
2493 &string_length
, &flags
, &rc
);
2495 test_truth(rc
== MEMCACHED_SUCCESS
);
2496 test_truth(string_length
== current_length
);
2497 test_truth(!memcmp(string
, value
, string_length
));
2504 return TEST_SUCCESS
;
2508 Look for zero length value problems
2510 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2513 memcached_return rc
;
2514 const char *key
= "mykey";
2519 for (x
= 0; x
< 2; x
++)
2521 rc
= memcached_set(memc
, key
, strlen(key
),
2523 (time_t)0, (uint32_t)0);
2525 test_truth(rc
== MEMCACHED_SUCCESS
);
2527 value
= memcached_get(memc
, key
, strlen(key
),
2528 &length
, &flags
, &rc
);
2530 test_truth(rc
== MEMCACHED_SUCCESS
);
2531 test_truth(value
== NULL
);
2532 test_truth(length
== 0);
2533 test_truth(flags
== 0);
2535 value
= memcached_get(memc
, key
, strlen(key
),
2536 &length
, &flags
, &rc
);
2538 test_truth(rc
== MEMCACHED_SUCCESS
);
2539 test_truth(value
== NULL
);
2540 test_truth(length
== 0);
2541 test_truth(flags
== 0);
2544 return TEST_SUCCESS
;
2547 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2548 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2550 memcached_return rc
;
2551 const char *key
= "mykey";
2556 rc
= memcached_set(memc
, key
, strlen(key
),
2558 (time_t)0, UINT32_MAX
);
2560 test_truth(rc
== MEMCACHED_SUCCESS
);
2562 value
= memcached_get(memc
, key
, strlen(key
),
2563 &length
, &flags
, &rc
);
2565 test_truth(rc
== MEMCACHED_SUCCESS
);
2566 test_truth(value
== NULL
);
2567 test_truth(length
== 0);
2568 test_truth(flags
== UINT32_MAX
);
2570 return TEST_SUCCESS
;
2574 /* Check the validity of chinese key*/
2575 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2577 memcached_return rc
;
2578 const char *key
= "豆瓣";
2579 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2584 rc
= memcached_set(memc
, key
, strlen(key
),
2585 value
, strlen(value
),
2588 test_truth(rc
== MEMCACHED_SUCCESS
);
2590 value2
= memcached_get(memc
, key
, strlen(key
),
2591 &length
, &flags
, &rc
);
2593 test_truth(length
==strlen(value
));
2594 test_truth(rc
== MEMCACHED_SUCCESS
);
2595 test_truth(memcmp(value
, value2
, length
)==0);
2598 return TEST_SUCCESS
;
2606 static test_return_t
user_supplied_bug19(memcached_st
*memc
)
2609 memcached_server_st
*s
;
2610 memcached_return res
;
2614 m
= memcached_create(NULL
);
2615 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2616 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2618 s
= memcached_server_by_key(m
, "a", 1, &res
);
2619 memcached_server_free(s
);
2623 return TEST_SUCCESS
;
2626 /* CAS test from Andei */
2627 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2629 memcached_return status
;
2630 memcached_result_st
*result
, result_obj
;
2631 const char *key
= "abc";
2632 size_t key_len
= strlen("abc");
2633 const char *value
= "foobar";
2634 size_t value_len
= strlen(value
);
2636 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2638 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2639 test_truth(status
== MEMCACHED_SUCCESS
);
2641 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2642 test_truth(status
== MEMCACHED_SUCCESS
);
2644 result
= memcached_result_create(memc
, &result_obj
);
2647 memcached_result_create(memc
, &result_obj
);
2648 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2651 test_truth(status
== MEMCACHED_SUCCESS
);
2653 memcached_result_free(result
);
2655 return TEST_SUCCESS
;
2658 #include "ketama_test_cases.h"
2659 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2661 memcached_return rc
;
2664 memcached_server_st
*server_pool
;
2669 memc
= memcached_create(NULL
);
2672 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2673 test_truth(rc
== MEMCACHED_SUCCESS
);
2675 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2676 test_truth(value
== 1);
2678 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2679 test_truth(rc
== MEMCACHED_SUCCESS
);
2681 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2682 test_truth(value
== MEMCACHED_HASH_MD5
);
2684 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");
2685 memcached_server_push(memc
, server_pool
);
2687 /* verify that the server list was parsed okay. */
2688 test_truth(memc
->number_of_hosts
== 8);
2689 test_truth(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2690 test_truth(server_pool
[0].port
== 11211);
2691 test_truth(server_pool
[0].weight
== 600);
2692 test_truth(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2693 test_truth(server_pool
[2].port
== 11211);
2694 test_truth(server_pool
[2].weight
== 200);
2695 test_truth(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2696 test_truth(server_pool
[7].port
== 11211);
2697 test_truth(server_pool
[7].weight
== 100);
2699 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2700 * us test the boundary wraparound.
2702 test_truth(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2704 /* verify the standard ketama set. */
2705 for (x
= 0; x
< 99; x
++)
2707 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2708 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2709 test_truth(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
2712 memcached_server_list_free(server_pool
);
2713 memcached_free(memc
);
2715 return TEST_SUCCESS
;
2718 /* Large mget() of missing keys with binary proto
2720 * If many binary quiet commands (such as getq's in an mget) fill the output
2721 * buffer and the server chooses not to respond, memcached_flush hangs. See
2722 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2725 /* sighandler_t function that always asserts false */
2726 static void fail(int unused
__attribute__((unused
)))
2732 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2734 memcached_return rc
;
2737 size_t* key_lengths
;
2738 void (*oldalarm
)(int);
2739 memcached_st
*memc_clone
;
2741 memc_clone
= memcached_clone(NULL
, memc
);
2742 test_truth(memc_clone
);
2744 /* only binproto uses getq for mget */
2745 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2747 /* empty the cache to ensure misses (hence non-responses) */
2748 rc
= memcached_flush(memc_clone
, 0);
2749 test_truth(rc
== MEMCACHED_SUCCESS
);
2751 key_lengths
= calloc(key_count
, sizeof(size_t));
2752 keys
= calloc(key_count
, sizeof(char *));
2754 for (x
= 0; x
< key_count
; x
++)
2758 snprintf(buffer
, 30, "%u", x
);
2759 keys
[x
]= strdup(buffer
);
2760 key_lengths
[x
]= strlen(keys
[x
]);
2763 oldalarm
= signal(SIGALRM
, fail
);
2766 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2767 test_truth(rc
== MEMCACHED_SUCCESS
);
2770 signal(SIGALRM
, oldalarm
);
2772 test_truth(fetch_all_results(memc
) == TEST_SUCCESS
);
2774 for (x
= 0; x
< key_count
; x
++)
2779 memcached_free(memc_clone
);
2781 return TEST_SUCCESS
;
2784 static memcached_return
pre_binary(memcached_st
*memc
);
2786 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
2788 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
2789 return TEST_SKIPPED
;
2793 /* should work as of r580 */
2794 rc
= _user_supplied_bug21(memc
, 10);
2795 test_truth(rc
== TEST_SUCCESS
);
2797 /* should fail as of r580 */
2798 rc
= _user_supplied_bug21(memc
, 1000);
2799 test_truth(rc
== TEST_SUCCESS
);
2801 return TEST_SUCCESS
;
2804 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
2808 memcached_return rc
;
2809 memcached_st
*memc
= memcached_create(NULL
);
2812 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2813 test_truth(rc
== MEMCACHED_SUCCESS
);
2815 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2816 test_truth(value
== 1);
2818 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2819 test_truth(rc
== MEMCACHED_SUCCESS
);
2821 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2822 test_truth(value
== MEMCACHED_HASH_MD5
);
2824 /* server should be removed when in delay */
2825 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2826 test_truth(rc
== MEMCACHED_SUCCESS
);
2828 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2829 test_truth(value
== 1);
2831 memcached_server_st
*server_pool
;
2832 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");
2833 memcached_server_push(memc
, server_pool
);
2835 /* verify that the server list was parsed okay. */
2836 test_truth(memc
->number_of_hosts
== 8);
2837 test_truth(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2838 test_truth(server_pool
[0].port
== 11211);
2839 test_truth(server_pool
[0].weight
== 600);
2840 test_truth(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2841 test_truth(server_pool
[2].port
== 11211);
2842 test_truth(server_pool
[2].weight
== 200);
2843 test_truth(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2844 test_truth(server_pool
[7].port
== 11211);
2845 test_truth(server_pool
[7].weight
== 100);
2847 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2848 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2850 for (int x
= 0; x
< 99; x
++)
2852 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2853 test_truth(server_idx
!= 2);
2856 /* and re-added when it's back. */
2857 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2858 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2859 run_distribution(memc
);
2860 for (int x
= 0; x
< 99; x
++)
2862 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2863 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2864 test_truth(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
2867 memcached_server_list_free(server_pool
);
2868 memcached_free(memc
);
2870 return TEST_SUCCESS
;
2873 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
2877 memcached_return rc
;
2878 memcached_st
*memc
= memcached_create(NULL
);
2882 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2883 test_truth(rc
== MEMCACHED_SUCCESS
);
2885 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2886 test_truth(value
== 1);
2888 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2889 test_truth(rc
== MEMCACHED_SUCCESS
);
2891 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2892 test_truth(value
== MEMCACHED_HASH_MD5
);
2895 test_truth(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
,
2896 MEMCACHED_KETAMA_COMPAT_SPY
) == MEMCACHED_SUCCESS
);
2898 memcached_server_st
*server_pool
;
2899 server_pool
= memcached_servers_parse("10.0.1.1:11211,10.0.1.2:11211,10.0.1.3:11211,10.0.1.4:11211,10.0.1.5:11211,10.0.1.6:11211,10.0.1.7:11211,10.0.1.8:11211,192.168.1.1:11211,192.168.100.1:11211");
2900 memcached_server_push(memc
, server_pool
);
2903 if ((fp
= fopen("ketama_keys.txt", "w")))
2907 printf("cannot write to file ketama_keys.txt");
2908 return TEST_FAILURE
;
2911 for (int x
= 0; x
< 10000; x
++)
2914 sprintf(key
, "%d", x
);
2916 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
2917 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2918 unsigned int port
= memc
->hosts
[server_idx
].port
;
2919 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
2922 memcached_server_list_free(server_pool
);
2923 memcached_free(memc
);
2925 return TEST_SUCCESS
;
2929 static test_return_t
result_static(memcached_st
*memc
)
2931 memcached_result_st result
;
2932 memcached_result_st
*result_ptr
;
2934 result_ptr
= memcached_result_create(memc
, &result
);
2935 test_truth(result
.is_allocated
== false);
2936 test_truth(result_ptr
);
2937 memcached_result_free(&result
);
2939 return TEST_SUCCESS
;
2942 static test_return_t
result_alloc(memcached_st
*memc
)
2944 memcached_result_st
*result
;
2946 result
= memcached_result_create(memc
, NULL
);
2948 memcached_result_free(result
);
2950 return TEST_SUCCESS
;
2953 static test_return_t
string_static_null(memcached_st
*memc
)
2955 memcached_string_st string
;
2956 memcached_string_st
*string_ptr
;
2958 string_ptr
= memcached_string_create(memc
, &string
, 0);
2959 test_truth(string
.is_allocated
== false);
2960 test_truth(string_ptr
);
2961 memcached_string_free(&string
);
2963 return TEST_SUCCESS
;
2966 static test_return_t
string_alloc_null(memcached_st
*memc
)
2968 memcached_string_st
*string
;
2970 string
= memcached_string_create(memc
, NULL
, 0);
2972 memcached_string_free(string
);
2974 return TEST_SUCCESS
;
2977 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
2979 memcached_string_st
*string
;
2981 string
= memcached_string_create(memc
, NULL
, 1024);
2983 memcached_string_free(string
);
2985 return TEST_SUCCESS
;
2988 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
2990 memcached_string_st
*string
;
2992 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2993 test_truth(string
== NULL
);
2995 return TEST_SUCCESS
;
2998 static test_return_t
string_alloc_append(memcached_st
*memc
)
3001 char buffer
[SMALL_STRING_LEN
];
3002 memcached_string_st
*string
;
3004 /* Ring the bell! */
3005 memset(buffer
, 6, SMALL_STRING_LEN
);
3007 string
= memcached_string_create(memc
, NULL
, 100);
3010 for (x
= 0; x
< 1024; x
++)
3012 memcached_return rc
;
3013 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3014 test_truth(rc
== MEMCACHED_SUCCESS
);
3016 memcached_string_free(string
);
3018 return TEST_SUCCESS
;
3021 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3023 memcached_return rc
;
3025 char buffer
[SMALL_STRING_LEN
];
3026 memcached_string_st
*string
;
3028 /* Ring the bell! */
3029 memset(buffer
, 6, SMALL_STRING_LEN
);
3031 string
= memcached_string_create(memc
, NULL
, 100);
3034 for (x
= 0; x
< 1024; x
++)
3036 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3037 test_truth(rc
== MEMCACHED_SUCCESS
);
3039 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3040 test_truth(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3041 memcached_string_free(string
);
3043 return TEST_SUCCESS
;
3046 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3048 pairs_free(global_pairs
);
3050 return TEST_SUCCESS
;
3053 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3055 unsigned long long x
;
3056 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3057 global_count
= GLOBAL_COUNT
;
3059 for (x
= 0; x
< global_count
; x
++)
3061 global_keys
[x
]= global_pairs
[x
].key
;
3062 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3065 return TEST_SUCCESS
;
3068 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3070 unsigned long long x
;
3071 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3072 global_count
= GLOBAL2_COUNT
;
3074 for (x
= 0; x
< global_count
; x
++)
3076 global_keys
[x
]= global_pairs
[x
].key
;
3077 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3080 return TEST_SUCCESS
;
3083 static test_return_t
generate_data(memcached_st
*memc
)
3085 execute_set(memc
, global_pairs
, global_count
);
3087 return TEST_SUCCESS
;
3090 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3092 memcached_stat_st
*stat_p
;
3093 memcached_return rc
;
3094 uint32_t host_index
= 0;
3095 execute_set(memc
, global_pairs
, global_count
);
3097 //TODO: hosts used size stats
3098 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3101 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3103 /* This test was changes so that "make test" would work properlly */
3105 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
);
3107 test_truth((unsigned long long)(stat_p
+ host_index
)->bytes
);
3110 memcached_stat_free(NULL
, stat_p
);
3112 return TEST_SUCCESS
;
3114 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3119 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3120 generate_data(memc
);
3122 return TEST_SUCCESS
;
3125 static test_return_t
get_read_count(memcached_st
*memc
)
3128 memcached_return rc
;
3129 memcached_st
*memc_clone
;
3131 memc_clone
= memcached_clone(NULL
, memc
);
3132 test_truth(memc_clone
);
3134 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3138 size_t return_value_length
;
3142 for (x
= count
= 0; x
< global_count
; x
++)
3144 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3145 &return_value_length
, &flags
, &rc
);
3146 if (rc
== MEMCACHED_SUCCESS
)
3155 memcached_free(memc_clone
);
3157 return TEST_SUCCESS
;
3160 static test_return_t
get_read(memcached_st
*memc
)
3163 memcached_return rc
;
3167 size_t return_value_length
;
3170 for (x
= 0; x
< global_count
; x
++)
3172 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3173 &return_value_length
, &flags
, &rc
);
3175 test_truth(return_value);
3176 test_truth(rc == MEMCACHED_SUCCESS);
3178 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3183 return TEST_SUCCESS
;
3186 static test_return_t
mget_read(memcached_st
*memc
)
3188 memcached_return rc
;
3190 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3191 test_truth(rc
== MEMCACHED_SUCCESS
);
3192 test_truth(fetch_all_results(memc
) == TEST_SUCCESS
);
3194 return TEST_SUCCESS
;
3197 static test_return_t
mget_read_result(memcached_st
*memc
)
3199 memcached_return rc
;
3201 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3202 test_truth(rc
== MEMCACHED_SUCCESS
);
3203 /* Turn this into a help function */
3205 memcached_result_st results_obj
;
3206 memcached_result_st
*results
;
3208 results
= memcached_result_create(memc
, &results_obj
);
3210 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3212 test_truth(results
);
3213 test_truth(rc
== MEMCACHED_SUCCESS
);
3216 memcached_result_free(&results_obj
);
3219 return TEST_SUCCESS
;
3222 static test_return_t
mget_read_function(memcached_st
*memc
)
3224 memcached_return rc
;
3225 unsigned int counter
;
3226 memcached_execute_function callbacks
[1];
3228 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3229 test_truth(rc
== MEMCACHED_SUCCESS
);
3231 callbacks
[0]= &callback_counter
;
3233 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3235 return TEST_SUCCESS
;
3238 static test_return_t
delete_generate(memcached_st
*memc
)
3242 for (x
= 0; x
< global_count
; x
++)
3244 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3247 return TEST_SUCCESS
;
3250 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3256 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3258 for (x
= 0; x
< global_count
; x
++)
3260 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3263 return TEST_SUCCESS
;
3266 static test_return_t
add_host_test1(memcached_st
*memc
)
3269 memcached_return rc
;
3270 char servername
[]= "0.example.com";
3271 memcached_server_st
*servers
;
3273 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3274 test_truth(servers
);
3275 test_truth(1 == memcached_server_list_count(servers
));
3277 for (x
= 2; x
< 20; x
++)
3279 char buffer
[SMALL_STRING_LEN
];
3281 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
3282 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3284 test_truth(rc
== MEMCACHED_SUCCESS
);
3285 test_truth(x
== memcached_server_list_count(servers
));
3288 rc
= memcached_server_push(memc
, servers
);
3289 test_truth(rc
== MEMCACHED_SUCCESS
);
3290 rc
= memcached_server_push(memc
, servers
);
3291 test_truth(rc
== MEMCACHED_SUCCESS
);
3293 memcached_server_list_free(servers
);
3295 return TEST_SUCCESS
;
3298 static memcached_return
pre_nonblock(memcached_st
*memc
)
3300 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3302 return MEMCACHED_SUCCESS
;
3305 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
3307 memcached_return rc
= MEMCACHED_FAILURE
;
3308 memcached_st
*memc_clone
;
3310 memc_clone
= memcached_clone(NULL
, memc
);
3312 // The memcached_version needs to be done on a clone, because the server
3313 // will not toggle protocol on an connection.
3314 memcached_version(memc_clone
);
3316 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3318 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3319 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3320 assert(rc
== MEMCACHED_SUCCESS
);
3321 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3324 memcached_free(memc_clone
);
3328 static memcached_return
pre_murmur(memcached_st
*memc
)
3330 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3332 return MEMCACHED_SUCCESS
;
3335 static memcached_return
pre_jenkins(memcached_st
*memc
)
3337 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3339 return MEMCACHED_SUCCESS
;
3343 static memcached_return
pre_md5(memcached_st
*memc
)
3345 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3347 return MEMCACHED_SUCCESS
;
3350 static memcached_return
pre_crc(memcached_st
*memc
)
3352 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3354 return MEMCACHED_SUCCESS
;
3357 static memcached_return
pre_hsieh(memcached_st
*memc
)
3359 #ifdef HAVE_HSIEH_HASH
3360 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3361 return MEMCACHED_SUCCESS
;
3364 return MEMCACHED_FAILURE
;
3368 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
3370 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
3372 return MEMCACHED_SUCCESS
;
3375 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
3377 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3379 return MEMCACHED_SUCCESS
;
3382 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
3384 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3386 return MEMCACHED_SUCCESS
;
3389 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
3391 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3393 return MEMCACHED_SUCCESS
;
3396 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3398 memcached_return rc
;
3401 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3402 assert(rc
== MEMCACHED_SUCCESS
);
3404 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3407 return MEMCACHED_SUCCESS
;
3410 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3412 memcached_return rc
;
3415 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3416 assert(rc
== MEMCACHED_SUCCESS
);
3418 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3421 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3422 assert(rc
== MEMCACHED_SUCCESS
);
3424 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3425 assert(value
== MEMCACHED_HASH_MD5
);
3426 return MEMCACHED_SUCCESS
;
3429 static memcached_return
pre_binary(memcached_st
*memc
)
3431 memcached_return rc
= MEMCACHED_FAILURE
;
3432 memcached_st
*memc_clone
;
3434 memc_clone
= memcached_clone(NULL
, memc
);
3436 // The memcached_version needs to be done on a clone, because the server
3437 // will not toggle protocol on an connection.
3438 memcached_version(memc_clone
);
3440 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3442 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3443 assert(rc
== MEMCACHED_SUCCESS
);
3444 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3447 memcached_free(memc_clone
);
3452 static memcached_return
pre_replication(memcached_st
*memc
)
3454 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
3455 return MEMCACHED_FAILURE
;
3458 * Make sure that we store the item on all servers
3459 * (master + replicas == number of servers)
3461 memcached_return rc
;
3462 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3463 memc
->number_of_hosts
- 1);
3464 assert(rc
== MEMCACHED_SUCCESS
);
3465 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3470 static memcached_return
pre_replication_noblock(memcached_st
*memc
)
3472 memcached_return rc
= MEMCACHED_FAILURE
;
3473 if (pre_replication(memc
) == MEMCACHED_SUCCESS
&&
3474 pre_nonblock(memc
) == MEMCACHED_SUCCESS
)
3475 rc
= MEMCACHED_SUCCESS
;
3480 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3485 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3487 void *ret
= malloc(size
);
3489 memset(ret
, 0xff, size
);
3494 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3496 return realloc(mem
, size
);
3499 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3501 return calloc(nelem
, size
);
3504 static memcached_return
set_prefix(memcached_st
*memc
)
3506 memcached_return rc
;
3507 const char *key
= "mine";
3510 /* Make sure be default none exists */
3511 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3512 assert(rc
== MEMCACHED_FAILURE
);
3514 /* Test a clean set */
3515 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3516 assert(rc
== MEMCACHED_SUCCESS
);
3518 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3519 assert(memcmp(value
, key
, 4) == 0);
3520 assert(rc
== MEMCACHED_SUCCESS
);
3522 /* Test that we can turn it off */
3523 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3524 assert(rc
== MEMCACHED_SUCCESS
);
3526 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3527 assert(rc
== MEMCACHED_FAILURE
);
3529 /* Now setup for main test */
3530 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3531 assert(rc
== MEMCACHED_SUCCESS
);
3533 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3534 assert(rc
== MEMCACHED_SUCCESS
);
3535 assert(memcmp(value
, key
, 4) == 0);
3537 /* Set to Zero, and then Set to something too large */
3540 memset(long_key
, 0, 255);
3542 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3543 assert(rc
== MEMCACHED_SUCCESS
);
3545 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3546 assert(rc
== MEMCACHED_FAILURE
);
3547 assert(value
== NULL
);
3549 /* Test a long key for failure */
3550 /* TODO, extend test to determine based on setting, what result should be */
3551 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3552 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3553 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3554 assert(rc
== MEMCACHED_SUCCESS
);
3556 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3557 strcpy(long_key
, "This is more then the allotted number of characters");
3558 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3559 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3561 /* Test for a bad prefix, but with a short key */
3562 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3563 assert(rc
== MEMCACHED_SUCCESS
);
3565 strcpy(long_key
, "dog cat");
3566 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3567 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3570 return MEMCACHED_SUCCESS
;
3573 #ifdef MEMCACHED_ENABLE_DEPRECATED
3574 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3576 void *test_ptr
= NULL
;
3579 memcached_malloc_function malloc_cb
=
3580 (memcached_malloc_function
)my_malloc
;
3581 cb_ptr
= *(void **)&malloc_cb
;
3582 memcached_return rc
;
3584 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3585 assert(rc
== MEMCACHED_SUCCESS
);
3586 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3587 assert(rc
== MEMCACHED_SUCCESS
);
3588 assert(test_ptr
== cb_ptr
);
3592 memcached_realloc_function realloc_cb
=
3593 (memcached_realloc_function
)my_realloc
;
3594 cb_ptr
= *(void **)&realloc_cb
;
3595 memcached_return rc
;
3597 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3598 assert(rc
== MEMCACHED_SUCCESS
);
3599 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3600 assert(rc
== MEMCACHED_SUCCESS
);
3601 assert(test_ptr
== cb_ptr
);
3605 memcached_free_function free_cb
=
3606 (memcached_free_function
)my_free
;
3607 cb_ptr
= *(void **)&free_cb
;
3608 memcached_return rc
;
3610 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3611 assert(rc
== MEMCACHED_SUCCESS
);
3612 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3613 assert(rc
== MEMCACHED_SUCCESS
);
3614 assert(test_ptr
== cb_ptr
);
3616 return MEMCACHED_SUCCESS
;
3620 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3622 memcached_return rc
;
3623 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3624 my_realloc
, my_calloc
);
3625 assert(rc
== MEMCACHED_FAILURE
);
3627 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3628 my_realloc
, my_calloc
);
3630 memcached_malloc_function mem_malloc
;
3631 memcached_free_function mem_free
;
3632 memcached_realloc_function mem_realloc
;
3633 memcached_calloc_function mem_calloc
;
3634 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3635 &mem_realloc
, &mem_calloc
);
3637 assert(mem_malloc
== my_malloc
);
3638 assert(mem_realloc
== my_realloc
);
3639 assert(mem_calloc
== my_calloc
);
3640 assert(mem_free
== my_free
);
3642 return MEMCACHED_SUCCESS
;
3645 static memcached_return
enable_consistent(memcached_st
*memc
)
3647 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3648 memcached_hash hash
;
3649 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3650 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3651 return MEMCACHED_FAILURE
;
3653 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3654 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3656 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3657 assert(hash
== MEMCACHED_HASH_HSIEH
);
3660 return MEMCACHED_SUCCESS
;
3663 static memcached_return
enable_cas(memcached_st
*memc
)
3665 unsigned int set
= 1;
3667 memcached_version(memc
);
3669 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3670 || memc
->hosts
[0].minor_version
> 2)
3672 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3674 return MEMCACHED_SUCCESS
;
3677 return MEMCACHED_FAILURE
;
3680 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3682 memcached_version(memc
);
3684 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3685 || memc
->hosts
[0].minor_version
> 2)
3686 return MEMCACHED_SUCCESS
;
3688 return MEMCACHED_FAILURE
;
3691 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3693 memcached_return rc
;
3696 memcached_server_list_free(memc
->hosts
);
3698 memc
->number_of_hosts
= 0;
3700 if (stat("/tmp/memcached.socket", &buf
))
3701 return MEMCACHED_FAILURE
;
3703 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3708 static memcached_return
pre_nodelay(memcached_st
*memc
)
3710 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3711 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3713 return MEMCACHED_SUCCESS
;
3716 static memcached_return
pre_settimer(memcached_st
*memc
)
3718 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3719 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3721 return MEMCACHED_SUCCESS
;
3724 static memcached_return
poll_timeout(memcached_st
*memc
)
3730 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3732 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3734 assert(timeout
== 100);
3736 return MEMCACHED_SUCCESS
;
3739 static test_return_t
noreply_test(memcached_st
*memc
)
3741 memcached_return ret
;
3742 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3743 test_truth(ret
== MEMCACHED_SUCCESS
);
3744 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3745 test_truth(ret
== MEMCACHED_SUCCESS
);
3746 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3747 test_truth(ret
== MEMCACHED_SUCCESS
);
3748 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3749 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3750 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3752 for (int count
=0; count
< 5; ++count
)
3754 for (int x
=0; x
< 100; ++x
)
3757 size_t len
= (size_t)sprintf(key
, "%d", x
);
3761 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3764 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3767 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3770 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3773 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3779 test_truth(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3783 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3784 ** API and is _ONLY_ done this way to verify that the library works the
3785 ** way it is supposed to do!!!!
3788 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3789 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3791 test_truth(no_msg
== 0);
3792 test_truth(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3795 ** Now validate that all items was set properly!
3797 for (int x
=0; x
< 100; ++x
)
3800 size_t len
= (size_t)sprintf(key
, "%d", x
);
3803 char* value
=memcached_get(memc
, key
, strlen(key
),
3804 &length
, &flags
, &ret
);
3805 test_truth(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3808 case 0: /* FALLTHROUGH */
3809 case 1: /* FALLTHROUGH */
3811 test_truth(strncmp(value
, key
, len
) == 0);
3812 test_truth(len
== length
);
3815 test_truth(length
== len
* 2);
3818 test_truth(length
== len
* 3);
3828 /* Try setting an illegal cas value (should not return an error to
3829 * the caller (because we don't expect a return message from the server)
3831 const char* keys
[]= {"0"};
3832 size_t lengths
[]= {1};
3835 memcached_result_st results_obj
;
3836 memcached_result_st
*results
;
3837 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3838 test_truth(ret
== MEMCACHED_SUCCESS
);
3840 results
= memcached_result_create(memc
, &results_obj
);
3841 test_truth(results
);
3842 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3843 test_truth(results
);
3844 test_truth(ret
== MEMCACHED_SUCCESS
);
3845 uint64_t cas
= memcached_result_cas(results
);
3846 memcached_result_free(&results_obj
);
3848 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3849 test_truth(ret
== MEMCACHED_SUCCESS
);
3852 * The item will have a new cas value, so try to set it again with the old
3853 * value. This should fail!
3855 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3856 test_truth(ret
== MEMCACHED_SUCCESS
);
3857 test_truth(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3858 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3859 test_truth(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3862 return TEST_SUCCESS
;
3865 static test_return_t
analyzer_test(memcached_st
*memc
)
3867 memcached_return rc
;
3868 memcached_stat_st
*memc_stat
;
3869 memcached_analysis_st
*report
;
3871 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3872 test_truth(rc
== MEMCACHED_SUCCESS
);
3873 test_truth(memc_stat
);
3875 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3876 test_truth(rc
== MEMCACHED_SUCCESS
);
3880 memcached_stat_free(NULL
, memc_stat
);
3882 return TEST_SUCCESS
;
3885 /* Count the objects */
3886 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3887 const char *key
__attribute__((unused
)),
3888 size_t key_length
__attribute__((unused
)),
3891 uint32_t *counter
= (uint32_t *)context
;
3893 *counter
= *counter
+ 1;
3895 return MEMCACHED_SUCCESS
;
3898 static test_return_t
dump_test(memcached_st
*memc
)
3900 memcached_return rc
;
3901 uint32_t counter
= 0;
3902 memcached_dump_func callbacks
[1];
3903 test_return_t main_rc
;
3905 callbacks
[0]= &callback_dump_counter
;
3907 /* No support for Binary protocol yet */
3908 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3909 return TEST_SUCCESS
;
3911 main_rc
= set_test3(memc
);
3913 test_truth (main_rc
== TEST_SUCCESS
);
3915 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3916 test_truth(rc
== MEMCACHED_SUCCESS
);
3918 /* We may have more then 32 if our previous flush has not completed */
3919 test_truth(counter
>= 32);
3921 return TEST_SUCCESS
;
3924 #ifdef HAVE_LIBMEMCACHEDUTIL
3925 static void* connection_release(void *arg
)
3928 memcached_pool_st
* pool
;
3933 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3937 static test_return_t
connection_pool_test(memcached_st
*memc
)
3939 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3940 test_truth(pool
!= NULL
);
3941 memcached_st
* mmc
[10];
3942 memcached_return rc
;
3944 for (int x
= 0; x
< 10; ++x
) {
3945 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3946 test_truth(mmc
[x
] != NULL
);
3947 test_truth(rc
== MEMCACHED_SUCCESS
);
3950 test_truth(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3951 test_truth(rc
== MEMCACHED_SUCCESS
);
3955 memcached_pool_st
* pool
;
3957 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3958 pthread_create(&tid
, NULL
, connection_release
, &item
);
3959 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3960 test_truth(rc
== MEMCACHED_SUCCESS
);
3961 pthread_join(tid
, NULL
);
3962 test_truth(mmc
[9] == item
.mmc
);
3963 const char *key
= "key";
3964 size_t keylen
= strlen(key
);
3966 // verify that I can do ops with all connections
3967 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3968 test_truth(rc
== MEMCACHED_SUCCESS
);
3970 for (unsigned int x
= 0; x
< 10; ++x
) {
3971 uint64_t number_value
;
3972 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3973 test_truth(rc
== MEMCACHED_SUCCESS
);
3974 test_truth(number_value
== (x
+1));
3978 for (int x
= 0; x
< 10; ++x
)
3979 test_truth(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3982 /* verify that I can set behaviors on the pool when I don't have all
3983 * of the connections in the pool. It should however be enabled
3984 * when I push the item into the pool
3986 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
3987 test_truth(mmc
[0] != NULL
);
3989 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
3990 test_truth(rc
== MEMCACHED_SUCCESS
);
3992 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
3993 test_truth(mmc
[1] != NULL
);
3995 test_truth(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
3996 test_truth(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
3997 test_truth(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
3999 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4000 test_truth(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4001 test_truth(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4004 test_truth(memcached_pool_destroy(pool
) == memc
);
4005 return TEST_SUCCESS
;
4009 static test_return_t
replication_set_test(memcached_st
*memc
)
4011 memcached_return rc
;
4012 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4013 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4015 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4016 test_truth(rc
== MEMCACHED_SUCCESS
);
4019 ** We are using the quiet commands to store the replicas, so we need
4020 ** to ensure that all of them are processed before we can continue.
4021 ** In the test we go directly from storing the object to trying to
4022 ** receive the object from all of the different servers, so we
4023 ** could end up in a race condition (the memcached server hasn't yet
4024 ** processed the quiet command from the replication set when it process
4025 ** the request from the other client (created by the clone)). As a
4026 ** workaround for that we call memcached_quit to send the quit command
4027 ** to the server and wait for the response ;-) If you use the test code
4028 ** as an example for your own code, please note that you shouldn't need
4031 memcached_quit(memc
);
4034 ** "bubba" should now be stored on all of our servers. We don't have an
4035 ** easy to use API to address each individual server, so I'll just iterate
4036 ** through a bunch of "master keys" and I should most likely hit all of the
4039 for (int x
= 'a'; x
<= 'z'; ++x
)
4041 char key
[2]= { [0]= (char)x
};
4044 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4046 test_truth(rc
== MEMCACHED_SUCCESS
);
4047 test_truth(val
!= NULL
);
4051 memcached_free(memc_clone
);
4053 return TEST_SUCCESS
;
4056 static test_return_t
replication_get_test(memcached_st
*memc
)
4058 memcached_return rc
;
4061 * Don't do the following in your code. I am abusing the internal details
4062 * within the library, and this is not a supported interface.
4063 * This is to verify correct behavior in the library
4065 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
4067 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4068 memc_clone
->hosts
[host
].port
= 0;
4070 for (int x
= 'a'; x
<= 'z'; ++x
)
4072 char key
[2]= { [0]= (char)x
};
4075 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4077 test_truth(rc
== MEMCACHED_SUCCESS
);
4078 test_truth(val
!= NULL
);
4082 memcached_free(memc_clone
);
4085 return TEST_SUCCESS
;
4088 static test_return_t
replication_mget_test(memcached_st
*memc
)
4090 memcached_return rc
;
4091 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4092 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4094 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4095 size_t len
[]= { 5, 4, 4, 4 };
4097 for (int x
=0; x
< 4; ++x
)
4099 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4100 test_truth(rc
== MEMCACHED_SUCCESS
);
4104 ** We are using the quiet commands to store the replicas, so we need
4105 ** to ensure that all of them are processed before we can continue.
4106 ** In the test we go directly from storing the object to trying to
4107 ** receive the object from all of the different servers, so we
4108 ** could end up in a race condition (the memcached server hasn't yet
4109 ** processed the quiet command from the replication set when it process
4110 ** the request from the other client (created by the clone)). As a
4111 ** workaround for that we call memcached_quit to send the quit command
4112 ** to the server and wait for the response ;-) If you use the test code
4113 ** as an example for your own code, please note that you shouldn't need
4116 memcached_quit(memc
);
4119 * Don't do the following in your code. I am abusing the internal details
4120 * within the library, and this is not a supported interface.
4121 * This is to verify correct behavior in the library
4123 memcached_result_st result_obj
;
4124 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4126 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4127 new_clone
->hosts
[host
].port
= 0;
4129 for (int x
= 'a'; x
<= 'z'; ++x
)
4131 const char key
[2]= { [0]= (const char)x
};
4133 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4134 test_truth(rc
== MEMCACHED_SUCCESS
);
4136 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4137 test_truth(results
);
4140 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4144 test_truth(hits
== 4);
4145 memcached_result_free(&result_obj
);
4148 memcached_free(new_clone
);
4151 memcached_free(memc_clone
);
4153 return TEST_SUCCESS
;
4156 static test_return_t
replication_delete_test(memcached_st
*memc
)
4158 memcached_return rc
;
4159 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4160 /* Delete the items from all of the servers except 1 */
4161 uint64_t repl
= memcached_behavior_get(memc
,
4162 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4163 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4165 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4166 size_t len
[]= { 5, 4, 4, 4 };
4168 for (int x
=0; x
< 4; ++x
)
4170 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4171 test_truth(rc
== MEMCACHED_SUCCESS
);
4175 * Don't do the following in your code. I am abusing the internal details
4176 * within the library, and this is not a supported interface.
4177 * This is to verify correct behavior in the library
4179 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4180 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4182 memc_clone
->hosts
[hash
].port
= 0;
4183 if (++hash
== memc_clone
->number_of_hosts
)
4187 memcached_result_st result_obj
;
4188 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4190 for (int x
= 'a'; x
<= 'z'; ++x
)
4192 const char key
[2]= { [0]= (const char)x
};
4194 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4195 test_truth(rc
== MEMCACHED_SUCCESS
);
4197 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4198 test_truth(results
);
4201 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4205 test_truth(hits
== 4);
4206 memcached_result_free(&result_obj
);
4209 memcached_free(memc_clone
);
4211 return TEST_SUCCESS
;
4214 static void increment_request_id(uint16_t *id
)
4217 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
4221 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
4223 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
4224 assert(ids
!= NULL
);
4227 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4228 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
4233 static test_return_t
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
4236 memcached_server_st
*cur_server
= memc
->hosts
;
4237 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
4239 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4241 test_truth(cur_server
[x
].cursor_active
== 0);
4242 test_truth(cur_req_ids
[x
] == expected_req_ids
[x
]);
4244 free(expected_req_ids
);
4247 return TEST_SUCCESS
;
4251 ** There is a little bit of a hack here, instead of removing
4252 ** the servers, I just set num host to 0 and them add then new udp servers
4254 static memcached_return
init_udp(memcached_st
*memc
)
4256 memcached_version(memc
);
4257 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
4258 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
4259 || memc
->hosts
[0].micro_version
< 6)
4260 return MEMCACHED_FAILURE
;
4262 uint32_t num_hosts
= memc
->number_of_hosts
;
4264 memcached_server_st servers
[num_hosts
];
4265 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
4266 for (x
= 0; x
< num_hosts
; x
++)
4267 memcached_server_free(&memc
->hosts
[x
]);
4269 memc
->number_of_hosts
= 0;
4270 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
4271 for (x
= 0; x
< num_hosts
; x
++)
4273 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
4274 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4277 return MEMCACHED_SUCCESS
;
4280 static memcached_return
binary_init_udp(memcached_st
*memc
)
4283 return init_udp(memc
);
4286 /* Make sure that I cant add a tcp server to a udp client */
4287 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
4289 memcached_server_st server
;
4290 memcached_server_clone(&server
, &memc
->hosts
[0]);
4291 test_truth(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4292 test_truth(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4293 return TEST_SUCCESS
;
4296 /* Make sure that I cant add a udp server to a tcp client */
4297 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
4299 memcached_server_st server
;
4300 memcached_server_clone(&server
, &memc
->hosts
[0]);
4301 test_truth(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4303 memcached_st tcp_client
;
4304 memcached_create(&tcp_client
);
4305 test_truth(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4306 return TEST_SUCCESS
;
4309 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
4312 memcached_quit(memc
);
4313 memc
->number_of_hosts
= 0;
4314 run_distribution(memc
);
4315 test_truth(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
4316 test_truth(memc
->flags
& MEM_USE_UDP
);
4317 test_truth(memc
->flags
& MEM_NOREPLY
);;
4319 test_truth(memc
->number_of_hosts
== 0);
4321 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
4322 test_truth(!(memc
->flags
& MEM_USE_UDP
));
4323 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
4324 test_truth(!(memc
->flags
& MEM_NOREPLY
));
4325 return TEST_SUCCESS
;
4328 static test_return_t
udp_set_test(memcached_st
*memc
)
4331 unsigned int num_iters
= 1025; //request id rolls over at 1024
4332 for (x
= 0; x
< num_iters
;x
++)
4334 memcached_return rc
;
4335 const char *key
= "foo";
4336 const char *value
= "when we sanitize";
4337 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4338 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
4339 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4340 rc
= memcached_set(memc
, key
, strlen(key
),
4341 value
, strlen(value
),
4342 (time_t)0, (uint32_t)0);
4343 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4344 /** NB, the check below assumes that if new write_ptr is less than
4345 * the original write_ptr that we have flushed. For large payloads, this
4346 * maybe an invalid assumption, but for the small payload we have it is OK
4348 if (rc
== MEMCACHED_SUCCESS
||
4349 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4350 increment_request_id(&expected_ids
[server_key
]);
4352 if (rc
== MEMCACHED_SUCCESS
)
4354 test_truth(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4358 test_truth(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4359 test_truth(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4361 test_truth(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4363 return TEST_SUCCESS
;
4366 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
4368 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4369 return udp_set_test(memc
);
4372 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
4374 memcached_return rc
;
4375 const char *key
= "bar";
4376 char value
[MAX_UDP_DATAGRAM_LENGTH
];
4377 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4378 rc
= memcached_set(memc
, key
, strlen(key
),
4379 value
, MAX_UDP_DATAGRAM_LENGTH
,
4380 (time_t)0, (uint32_t)0);
4381 test_truth(rc
== MEMCACHED_WRITE_FAILURE
);
4382 return post_udp_op_check(memc
,expected_ids
);
4385 static test_return_t
udp_delete_test(memcached_st
*memc
)
4388 unsigned int num_iters
= 1025; //request id rolls over at 1024
4389 for (x
= 0; x
< num_iters
;x
++)
4391 memcached_return rc
;
4392 const char *key
= "foo";
4393 uint16_t *expected_ids
=get_udp_request_ids(memc
);
4394 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4395 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4396 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
4397 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4398 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4399 increment_request_id(&expected_ids
[server_key
]);
4400 if (rc
== MEMCACHED_SUCCESS
)
4402 test_truth(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4406 test_truth(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4407 test_truth(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4409 test_truth(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4411 return TEST_SUCCESS
;
4414 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
4416 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4417 return udp_delete_test(memc
);
4420 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
4422 memcached_return rc
;
4423 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4425 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4426 increment_request_id(&expected_ids
[x
]);
4428 rc
= memcached_verbosity(memc
,3);
4429 test_truth(rc
== MEMCACHED_SUCCESS
);
4430 return post_udp_op_check(memc
,expected_ids
);
4433 static test_return_t
udp_quit_test(memcached_st
*memc
)
4435 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4436 memcached_quit(memc
);
4437 return post_udp_op_check(memc
, expected_ids
);
4440 static test_return_t
udp_flush_test(memcached_st
*memc
)
4442 memcached_return rc
;
4443 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4445 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4446 increment_request_id(&expected_ids
[x
]);
4448 rc
= memcached_flush(memc
,0);
4449 test_truth(rc
== MEMCACHED_SUCCESS
);
4450 return post_udp_op_check(memc
,expected_ids
);
4453 static test_return_t
udp_incr_test(memcached_st
*memc
)
4455 memcached_return rc
;
4456 const char *key
= "incr";
4457 const char *value
= "1";
4458 rc
= memcached_set(memc
, key
, strlen(key
),
4459 value
, strlen(value
),
4460 (time_t)0, (uint32_t)0);
4462 test_truth(rc
== MEMCACHED_SUCCESS
);
4463 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4464 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4465 increment_request_id(&expected_ids
[server_key
]);
4467 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4468 test_truth(rc
== MEMCACHED_SUCCESS
);
4469 return post_udp_op_check(memc
, expected_ids
);
4472 static test_return_t
udp_decr_test(memcached_st
*memc
)
4474 memcached_return rc
;
4475 const char *key
= "decr";
4476 const char *value
= "1";
4477 rc
= memcached_set(memc
, key
, strlen(key
),
4478 value
, strlen(value
),
4479 (time_t)0, (uint32_t)0);
4481 test_truth(rc
== MEMCACHED_SUCCESS
);
4482 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4483 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4484 increment_request_id(&expected_ids
[server_key
]);
4486 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4487 test_truth(rc
== MEMCACHED_SUCCESS
);
4488 return post_udp_op_check(memc
, expected_ids
);
4492 static test_return_t
udp_stat_test(memcached_st
*memc
)
4494 memcached_stat_st
* rv
= NULL
;
4495 memcached_return rc
;
4497 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4498 rv
= memcached_stat(memc
, args
, &rc
);
4500 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
4501 return post_udp_op_check(memc
, expected_ids
);
4504 static test_return_t
udp_version_test(memcached_st
*memc
)
4506 memcached_return rc
;
4507 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4508 rc
= memcached_version(memc
);
4509 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
4510 return post_udp_op_check(memc
, expected_ids
);
4513 static test_return_t
udp_get_test(memcached_st
*memc
)
4515 memcached_return rc
;
4516 const char *key
= "foo";
4518 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4519 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4520 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
4521 test_truth(val
== NULL
);
4522 return post_udp_op_check(memc
, expected_ids
);
4525 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
4528 test_st mixed_io_ops
[] ={
4529 {"udp_set_test", 0, udp_set_test
},
4530 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4531 {"udp_delete_test", 0, udp_delete_test
},
4532 {"udp_verbosity_test", 0, udp_verbosity_test
},
4533 {"udp_quit_test", 0, udp_quit_test
},
4534 {"udp_flush_test", 0, udp_flush_test
},
4535 {"udp_incr_test", 0, udp_incr_test
},
4536 {"udp_decr_test", 0, udp_decr_test
},
4537 {"udp_version_test", 0, udp_version_test
}
4540 for (x
= 0; x
< 500; x
++)
4542 current_op
= mixed_io_ops
[random() % 9];
4543 test_truth(current_op
.function(memc
) == TEST_SUCCESS
);
4545 return TEST_SUCCESS
;
4548 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4550 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4551 #ifdef HAVE_HSIEH_HASH
4552 expected_rc
= MEMCACHED_SUCCESS
;
4554 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4555 (uint64_t)MEMCACHED_HASH_HSIEH
);
4556 test_truth(rc
== expected_rc
);
4557 return TEST_SUCCESS
;
4560 static const char *list
[]=
4590 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4594 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4595 245758794U, 2550894432U, 121710495U, 3053817768U,
4596 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4597 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4598 2138080750U, 217675895U, 2230934345U, 1234361223U,
4599 3968582726U, 2455685270U, 1293568479U, 199067604U,
4603 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4607 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4608 test_truth(values
[x
] == hash_val
);
4611 return TEST_SUCCESS
;
4614 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4618 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4619 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4620 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4621 17300U, 28156U, 9192U, 20484U, 16925U };
4623 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4627 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4628 assert(values
[x
] == hash_val
);
4631 return TEST_SUCCESS
;
4634 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4638 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4639 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4640 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4641 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4642 2815549194U, 2562818319U, 224996066U, 2680194749U,
4643 3035305390U, 246890365U, 2395624193U, 4145193337U,
4646 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4650 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4651 assert(values
[x
] == hash_val
);
4654 return TEST_SUCCESS
;
4657 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4661 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4662 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4663 734810122U, 1516407546U, 560948863U, 1767346780U,
4664 561034892U, 4156330026U, 3716417003U, 3475297030U,
4665 1518272172U, 227211583U, 3938128828U, 126112909U,
4666 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4669 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4673 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4674 assert(values
[x
] == hash_val
);
4677 return TEST_SUCCESS
;
4680 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4684 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4685 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4686 344481048U, 3865235296U, 2181839183U, 119581266U,
4687 510234242U, 4248244304U, 1362796839U, 103389328U,
4688 1449620010U, 182962511U, 3554262370U, 3206747549U,
4689 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4693 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4697 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4698 assert(values
[x
] == hash_val
);
4701 return TEST_SUCCESS
;
4704 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4708 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4709 459261019U, 3521085446U, 18738364U, 1625305005U,
4710 2162232970U, 777243802U, 3323728671U, 132336572U,
4711 3654473228U, 260679466U, 1169454059U, 2698319462U,
4712 1062177260U, 235516991U, 2218399068U, 405302637U,
4713 1128467232U, 3579622413U, 2138539289U, 96429129U,
4716 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4720 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4721 assert(values
[x
] == hash_val
);
4724 return TEST_SUCCESS
;
4727 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4731 #ifdef HAVE_HSIEH_HASH
4732 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4733 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4734 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4735 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4738 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 };
4741 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4745 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4746 assert(values
[x
] == hash_val
);
4749 return TEST_SUCCESS
;
4752 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4756 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4757 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4758 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4759 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4760 2815549194U, 2562818319U, 224996066U, 2680194749U,
4761 3035305390U, 246890365U, 2395624193U, 4145193337U,
4764 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4768 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4769 assert(values
[x
] == hash_val
);
4772 return TEST_SUCCESS
;
4775 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4779 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4780 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4781 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4782 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4783 4153263658U, 3824871822U, 3072759809U, 798622255U,
4784 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4788 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4792 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4793 assert(values
[x
] == hash_val
);
4796 return TEST_SUCCESS
;
4800 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4802 memcached_return rc
;
4805 memcached_server_st
*server_pool
;
4810 memc
= memcached_create(NULL
);
4813 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4814 assert(rc
== MEMCACHED_SUCCESS
);
4816 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4819 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
,
4820 MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED
) == MEMCACHED_SUCCESS
);
4822 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
) ==
4823 MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED
);
4825 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");
4826 memcached_server_push(memc
, server_pool
);
4828 /* verify that the server list was parsed okay. */
4829 assert(memc
->number_of_hosts
== 8);
4830 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
4831 assert(server_pool
[0].port
== 11211);
4832 assert(server_pool
[0].weight
== 600);
4833 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
4834 assert(server_pool
[2].port
== 11211);
4835 assert(server_pool
[2].weight
== 200);
4836 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
4837 assert(server_pool
[7].port
== 11211);
4838 assert(server_pool
[7].weight
== 100);
4840 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4841 * us test the boundary wraparound.
4843 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4845 /* verify the standard ketama set. */
4846 for (x
= 0; x
< 99; x
++)
4848 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4849 char *hostname
= memc
->hosts
[server_idx
].hostname
;
4850 assert(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
4853 memcached_server_list_free(server_pool
);
4854 memcached_free(memc
);
4856 return TEST_SUCCESS
;
4859 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4861 memcached_return rc
;
4864 memcached_server_st
*server_pool
;
4869 memc
= memcached_create(NULL
);
4872 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4873 assert(rc
== MEMCACHED_SUCCESS
);
4875 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4878 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
,
4879 MEMCACHED_KETAMA_COMPAT_SPY
) == MEMCACHED_SUCCESS
);
4881 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE
) ==
4882 MEMCACHED_KETAMA_COMPAT_SPY
);
4884 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");
4885 memcached_server_push(memc
, server_pool
);
4887 /* verify that the server list was parsed okay. */
4888 assert(memc
->number_of_hosts
== 8);
4889 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
4890 assert(server_pool
[0].port
== 11211);
4891 assert(server_pool
[0].weight
== 600);
4892 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
4893 assert(server_pool
[2].port
== 11211);
4894 assert(server_pool
[2].weight
== 200);
4895 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
4896 assert(server_pool
[7].port
== 11211);
4897 assert(server_pool
[7].weight
== 100);
4899 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4900 * us test the boundary wraparound.
4902 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4904 /* verify the standard ketama set. */
4905 for (x
= 0; x
< 99; x
++)
4907 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4908 char *hostname
= memc
->hosts
[server_idx
].hostname
;
4909 assert(strcmp(hostname
, ketama_test_cases_spy
[x
].server
) == 0);
4912 memcached_server_list_free(server_pool
);
4913 memcached_free(memc
);
4915 return TEST_SUCCESS
;
4918 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4920 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
4921 return TEST_SKIPPED
;
4923 memcached_return ret
;
4924 const char *key
= "regression_bug_434484";
4925 size_t keylen
= strlen(key
);
4927 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4928 assert(ret
== MEMCACHED_NOTSTORED
);
4930 size_t size
= 2048 * 1024;
4931 void *data
= calloc(1, size
);
4932 assert(data
!= NULL
);
4933 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
4934 assert(ret
== MEMCACHED_E2BIG
);
4937 return TEST_SUCCESS
;
4940 static test_return_t
regression_bug_434843(memcached_st
*memc
)
4942 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
4943 return TEST_SKIPPED
;
4945 memcached_return rc
;
4946 unsigned int counter
= 0;
4947 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
4950 * I only want to hit only _one_ server so I know the number of requests I'm
4951 * sending in the pipleine to the server. Let's try to do a multiget of
4952 * 1024 (that should satisfy most users don't you think?). Future versions
4953 * will include a mget_execute function call if you need a higher number.
4955 uint32_t number_of_hosts
= memc
->number_of_hosts
;
4956 memc
->number_of_hosts
= 1;
4957 const size_t max_keys
= 1024;
4958 char **keys
= calloc(max_keys
, sizeof(char*));
4959 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
4961 for (int x
= 0; x
< (int)max_keys
; ++x
)
4964 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
4966 assert(keys
[x
] != NULL
);
4970 * Run two times.. the first time we should have 100% cache miss,
4971 * and the second time we should have 100% cache hits
4973 for (int y
= 0; y
< 2; ++y
)
4975 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
4976 assert(rc
== MEMCACHED_SUCCESS
);
4977 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
4980 /* The first iteration should give me a 100% cache miss. verify that*/
4981 assert(counter
== 0);
4982 char blob
[1024]= { 0 };
4983 for (int x
= 0; x
< (int)max_keys
; ++x
)
4985 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
4986 blob
, sizeof(blob
), 0, 0);
4987 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4992 /* Verify that we received all of the key/value pairs */
4993 assert(counter
== (unsigned int)max_keys
);
4997 /* Release allocated resources */
4998 for (size_t x
= 0; x
< max_keys
; ++x
)
5003 memc
->number_of_hosts
= number_of_hosts
;
5004 return TEST_SUCCESS
;
5007 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5009 memcached_return rc
;
5010 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5011 assert(rc
== MEMCACHED_SUCCESS
);
5013 return regression_bug_434843(memc
);
5016 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5018 memcached_return rc
;
5019 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5020 assert(rc
== MEMCACHED_SUCCESS
);
5022 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5023 assert(rc
== MEMCACHED_SUCCESS
);
5024 assert(bytes
!= NULL
);
5025 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5027 assert(rc
== MEMCACHED_SUCCESS
);
5028 assert(bytes_read
!= NULL
);
5030 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5031 "bytes_written", &rc
);
5032 assert(rc
== MEMCACHED_SUCCESS
);
5033 assert(bytes_written
!= NULL
);
5035 assert(strcmp(bytes
, bytes_read
) != 0);
5036 assert(strcmp(bytes
, bytes_written
) != 0);
5038 /* Release allocated resources */
5041 free(bytes_written
);
5042 memcached_stat_free(NULL
, memc_stat
);
5043 return TEST_SUCCESS
;
5047 * The test case isn't obvious so I should probably document why
5048 * it works the way it does. Bug 442914 was caused by a bug
5049 * in the logic in memcached_purge (it did not handle the case
5050 * where the number of bytes sent was equal to the watermark).
5051 * In this test case, create messages so that we hit that case
5052 * and then disable noreply mode and issue a new command to
5053 * verify that it isn't stuck. If we change the format for the
5054 * delete command or the watermarks, we need to update this
5057 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5059 memcached_return rc
;
5060 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5061 assert(rc
== MEMCACHED_SUCCESS
);
5062 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5064 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5065 memc
->number_of_hosts
= 1;
5070 for (int x
= 0; x
< 250; ++x
)
5072 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5073 rc
= memcached_delete(memc
, k
, len
, 0);
5074 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5077 len
= (size_t)snprintf(k
, sizeof(k
), "%037u", 251);
5078 rc
= memcached_delete(memc
, k
, len
, 0);
5079 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5081 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5082 assert(rc
== MEMCACHED_SUCCESS
);
5083 rc
= memcached_delete(memc
, k
, len
, 0);
5084 assert(rc
== MEMCACHED_NOTFOUND
);
5086 memc
->number_of_hosts
= number_of_hosts
;
5088 return TEST_SUCCESS
;
5091 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5093 if (memc
->number_of_hosts
< 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5094 return TEST_SKIPPED
;
5096 memcached_return rc
;
5098 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5099 assert(rc
== MEMCACHED_SUCCESS
);
5101 const size_t max_keys
= 100;
5102 char **keys
= calloc(max_keys
, sizeof(char*));
5103 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5105 for (int x
= 0; x
< (int)max_keys
; ++x
)
5108 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
5110 assert(keys
[x
] != NULL
);
5111 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5112 assert(rc
== MEMCACHED_SUCCESS
);
5116 ** We are using the quiet commands to store the replicas, so we need
5117 ** to ensure that all of them are processed before we can continue.
5118 ** In the test we go directly from storing the object to trying to
5119 ** receive the object from all of the different servers, so we
5120 ** could end up in a race condition (the memcached server hasn't yet
5121 ** processed the quiet command from the replication set when it process
5122 ** the request from the other client (created by the clone)). As a
5123 ** workaround for that we call memcached_quit to send the quit command
5124 ** to the server and wait for the response ;-) If you use the test code
5125 ** as an example for your own code, please note that you shouldn't need
5128 memcached_quit(memc
);
5130 /* Verify that all messages are stored, and we didn't stuff too much
5133 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5134 assert(rc
== MEMCACHED_SUCCESS
);
5136 unsigned int counter
= 0;
5137 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5138 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5139 /* Verify that we received all of the key/value pairs */
5140 assert(counter
== (unsigned int)max_keys
);
5142 memcached_quit(memc
);
5144 * Don't do the following in your code. I am abusing the internal details
5145 * within the library, and this is not a supported interface.
5146 * This is to verify correct behavior in the library. Fake that two servers
5149 unsigned int port0
= memc
->hosts
[0].port
;
5150 unsigned int port2
= memc
->hosts
[2].port
;
5151 memc
->hosts
[0].port
= 0;
5152 memc
->hosts
[2].port
= 0;
5154 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5155 assert(rc
== MEMCACHED_SUCCESS
);
5158 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5159 assert(counter
== (unsigned int)max_keys
);
5161 /* restore the memc handle */
5162 memc
->hosts
[0].port
= port0
;
5163 memc
->hosts
[2].port
= port2
;
5165 memcached_quit(memc
);
5167 /* Remove half of the objects */
5168 for (int x
= 0; x
< (int)max_keys
; ++x
)
5171 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5172 assert(rc
== MEMCACHED_SUCCESS
);
5175 memcached_quit(memc
);
5176 memc
->hosts
[0].port
= 0;
5177 memc
->hosts
[2].port
= 0;
5179 /* now retry the command, this time we should have cache misses */
5180 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5181 assert(rc
== MEMCACHED_SUCCESS
);
5184 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5185 assert(counter
== (unsigned int)(max_keys
>> 1));
5187 /* Release allocated resources */
5188 for (size_t x
= 0; x
< max_keys
; ++x
)
5193 /* restore the memc handle */
5194 memc
->hosts
[0].port
= port0
;
5195 memc
->hosts
[2].port
= port2
;
5196 return TEST_SUCCESS
;
5199 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5201 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5202 assert(memc_clone
!= NULL
);
5203 assert(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5205 if (memc_clone
->hosts
[0].major_version
> 1 ||
5206 (memc_clone
->hosts
[0].major_version
== 1 &&
5207 memc_clone
->hosts
[0].minor_version
> 2))
5209 /* Binary protocol doesn't support deferred delete */
5210 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5211 assert(bin_clone
!= NULL
);
5212 assert(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5213 assert(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5214 memcached_free(bin_clone
);
5216 memcached_quit(memc_clone
);
5218 /* If we know the server version, deferred delete should fail
5219 * with invalid arguments */
5220 assert(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5222 /* If we don't know the server version, we should get a protocol error */
5223 memcached_return rc
= memcached_delete(memc
, "foo", 3, 1);
5224 /* but there is a bug in some of the memcached servers (1.4) that treats
5225 * the counter as noreply so it doesn't send the proper error message
5227 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5229 /* And buffered mode should be disabled and we should get protocol error */
5230 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5231 rc
= memcached_delete(memc
, "foo", 3, 1);
5232 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5234 /* Same goes for noreply... */
5235 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5236 rc
= memcached_delete(memc
, "foo", 3, 1);
5237 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5239 /* but a normal request should go through (and be buffered) */
5240 assert((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5241 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5243 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5244 /* unbuffered noreply should be success */
5245 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5246 /* unbuffered with reply should be not found... */
5247 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5248 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5251 memcached_free(memc_clone
);
5252 return TEST_SUCCESS
;
5256 /* Test memcached_server_get_last_disconnect
5257 * For a working server set, shall be NULL
5258 * For a set of non existing server, shall not be NULL
5260 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5262 memcached_return rc
;
5263 memcached_server_st
*disconnected_server
;
5265 /* With the working set of server */
5266 const char *key
= "marmotte";
5267 const char *value
= "milka";
5269 rc
= memcached_set(memc
, key
, strlen(key
),
5270 value
, strlen(value
),
5271 (time_t)0, (uint32_t)0);
5272 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5274 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5275 assert(disconnected_server
== NULL
);
5277 /* With a non existing server */
5279 memcached_server_st
*servers
;
5281 const char *server_list
= "localhost:9";
5283 servers
= memcached_servers_parse(server_list
);
5285 mine
= memcached_create(NULL
);
5286 rc
= memcached_server_push(mine
, servers
);
5287 assert(rc
== MEMCACHED_SUCCESS
);
5288 memcached_server_list_free(servers
);
5291 rc
= memcached_set(mine
, key
, strlen(key
),
5292 value
, strlen(value
),
5293 (time_t)0, (uint32_t)0);
5294 assert(rc
!= MEMCACHED_SUCCESS
);
5296 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5297 assert(disconnected_server
!= NULL
);
5298 assert(disconnected_server
->port
== 9);
5299 assert(strncmp(disconnected_server
->hostname
,"localhost",9) == 0);
5301 memcached_quit(mine
);
5302 memcached_free(mine
);
5304 return TEST_SUCCESS
;
5308 * This test ensures that the failure counter isn't incremented during
5309 * normal termination of the memcached instance.
5311 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5313 memcached_return rc
;
5315 /* Set value to force connection to the server */
5316 const char *key
= "marmotte";
5317 const char *value
= "milka";
5320 * Please note that I'm abusing the internal structures in libmemcached
5321 * in a non-portable way and you shouldn't be doing this. I'm only
5322 * doing this in order to verify that the library works the way it should
5324 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5325 memc
->number_of_hosts
= 1;
5327 /* Ensure that we are connected to the server by setting a value */
5328 rc
= memcached_set(memc
, key
, strlen(key
),
5329 value
, strlen(value
),
5330 (time_t)0, (uint32_t)0);
5331 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5334 /* The test is to see that the memcached_quit doesn't increase the
5335 * the server failure conter, so let's ensure that it is zero
5336 * before sending quit
5338 memc
->hosts
[0].server_failure_counter
= 0;
5340 memcached_quit(memc
);
5342 /* Verify that it memcached_quit didn't increment the failure counter
5343 * Please note that this isn't bullet proof, because an error could
5346 assert(memc
->hosts
[0].server_failure_counter
== 0);
5348 /* restore the instance */
5349 memc
->number_of_hosts
= number_of_hosts
;
5351 return TEST_SUCCESS
;
5354 test_st udp_setup_server_tests
[] ={
5355 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
5356 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
5357 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
5361 test_st upd_io_tests
[] ={
5362 {"udp_set_test", 0, udp_set_test
},
5363 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
5364 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
5365 {"udp_delete_test", 0, udp_delete_test
},
5366 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
5367 {"udp_verbosity_test", 0, udp_verbosity_test
},
5368 {"udp_quit_test", 0, udp_quit_test
},
5369 {"udp_flush_test", 0, udp_flush_test
},
5370 {"udp_incr_test", 0, udp_incr_test
},
5371 {"udp_decr_test", 0, udp_decr_test
},
5372 {"udp_stat_test", 0, udp_stat_test
},
5373 {"udp_version_test", 0, udp_version_test
},
5374 {"udp_get_test", 0, udp_get_test
},
5375 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
5379 /* Clean the server before beginning testing */
5381 {"flush", 0, flush_test
},
5382 {"init", 0, init_test
},
5383 {"allocation", 0, allocation_test
},
5384 {"server_list_null_test", 0, server_list_null_test
},
5385 {"server_unsort", 0, server_unsort_test
},
5386 {"server_sort", 0, server_sort_test
},
5387 {"server_sort2", 0, server_sort2_test
},
5388 {"clone_test", 0, clone_test
},
5389 {"connection_test", 0, connection_test
},
5390 {"callback_test", 0, callback_test
},
5391 {"behavior_test", 0, behavior_test
},
5392 {"userdata_test", 0, userdata_test
},
5393 {"error", 0, error_test
},
5394 {"set", 0, set_test
},
5395 {"set2", 0, set_test2
},
5396 {"set3", 0, set_test3
},
5397 {"dump", 1, dump_test
},
5398 {"add", 1, add_test
},
5399 {"replace", 1, replace_test
},
5400 {"delete", 1, delete_test
},
5401 {"get", 1, get_test
},
5402 {"get2", 0, get_test2
},
5403 {"get3", 0, get_test3
},
5404 {"get4", 0, get_test4
},
5405 {"partial mget", 0, get_test5
},
5406 {"stats_servername", 0, stats_servername_test
},
5407 {"increment", 0, increment_test
},
5408 {"increment_with_initial", 1, increment_with_initial_test
},
5409 {"decrement", 0, decrement_test
},
5410 {"decrement_with_initial", 1, decrement_with_initial_test
},
5411 {"increment_by_key", 0, increment_by_key_test
},
5412 {"increment_with_initial_by_key", 1, increment_with_initial_by_key_test
},
5413 {"decrement_by_key", 0, decrement_by_key_test
},
5414 {"decrement_with_initial_by_key", 1, decrement_with_initial_by_key_test
},
5415 {"quit", 0, quit_test
},
5416 {"mget", 1, mget_test
},
5417 {"mget_result", 1, mget_result_test
},
5418 {"mget_result_alloc", 1, mget_result_alloc_test
},
5419 {"mget_result_function", 1, mget_result_function
},
5420 {"mget_execute", 1, mget_execute
},
5421 {"mget_end", 0, mget_end
},
5422 {"get_stats", 0, get_stats
},
5423 {"add_host_test", 0, add_host_test
},
5424 {"add_host_test_1", 0, add_host_test1
},
5425 {"get_stats_keys", 0, get_stats_keys
},
5426 {"behavior_test", 0, get_stats_keys
},
5427 {"callback_test", 0, get_stats_keys
},
5428 {"version_string_test", 0, version_string_test
},
5429 {"bad_key", 1, bad_key_test
},
5430 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
5431 {"read_through", 1, read_through
},
5432 {"delete_through", 1, delete_through
},
5433 {"noreply", 1, noreply_test
},
5434 {"analyzer", 1, analyzer_test
},
5435 #ifdef HAVE_LIBMEMCACHEDUTIL
5436 {"connectionpool", 1, connection_pool_test
},
5438 {"test_get_last_disconnect", 1, test_get_last_disconnect
},
5442 test_st async_tests
[] ={
5443 {"add", 1, add_wrapper
},
5447 test_st string_tests
[] ={
5448 {"string static with null", 0, string_static_null
},
5449 {"string alloc with null", 0, string_alloc_null
},
5450 {"string alloc with 1K", 0, string_alloc_with_size
},
5451 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
5452 {"string append", 0, string_alloc_append
},
5453 {"string append failure (too big)", 0, string_alloc_append_toobig
},
5457 test_st result_tests
[] ={
5458 {"result static", 0, result_static
},
5459 {"result alloc", 0, result_alloc
},
5463 test_st version_1_2_3
[] ={
5464 {"append", 0, append_test
},
5465 {"prepend", 0, prepend_test
},
5466 {"cas", 0, cas_test
},
5467 {"cas2", 0, cas2_test
},
5468 {"append_binary", 0, append_binary_test
},
5472 test_st user_tests
[] ={
5473 {"user_supplied_bug1", 0, user_supplied_bug1
},
5474 {"user_supplied_bug2", 0, user_supplied_bug2
},
5475 {"user_supplied_bug3", 0, user_supplied_bug3
},
5476 {"user_supplied_bug4", 0, user_supplied_bug4
},
5477 {"user_supplied_bug5", 1, user_supplied_bug5
},
5478 {"user_supplied_bug6", 1, user_supplied_bug6
},
5479 {"user_supplied_bug7", 1, user_supplied_bug7
},
5480 {"user_supplied_bug8", 1, user_supplied_bug8
},
5481 {"user_supplied_bug9", 1, user_supplied_bug9
},
5482 {"user_supplied_bug10", 1, user_supplied_bug10
},
5483 {"user_supplied_bug11", 1, user_supplied_bug11
},
5484 {"user_supplied_bug12", 1, user_supplied_bug12
},
5485 {"user_supplied_bug13", 1, user_supplied_bug13
},
5486 {"user_supplied_bug14", 1, user_supplied_bug14
},
5487 {"user_supplied_bug15", 1, user_supplied_bug15
},
5488 {"user_supplied_bug16", 1, user_supplied_bug16
},
5491 ** It seems to be something weird with the character sets..
5492 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
5493 ** guess I need to find out how this is supposed to work.. Perhaps I need
5494 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
5495 ** so just disable the code for now...).
5497 {"user_supplied_bug17", 1, user_supplied_bug17
},
5499 {"user_supplied_bug18", 1, user_supplied_bug18
},
5500 {"user_supplied_bug19", 1, user_supplied_bug19
},
5501 {"user_supplied_bug20", 1, user_supplied_bug20
},
5502 {"user_supplied_bug21", 1, user_supplied_bug21
},
5503 {"wrong_failure_counter_test", 1, wrong_failure_counter_test
},
5507 test_st replication_tests
[]= {
5508 {"set", 1, replication_set_test
},
5509 {"get", 0, replication_get_test
},
5510 {"mget", 0, replication_mget_test
},
5511 {"delete", 0, replication_delete_test
},
5516 * The following test suite is used to verify that we don't introduce
5517 * regression bugs. If you want more information about the bug / test,
5518 * you should look in the bug report at
5519 * http://bugs.launchpad.net/libmemcached
5521 test_st regression_tests
[]= {
5522 {"lp:434484", 1, regression_bug_434484
},
5523 {"lp:434843", 1, regression_bug_434843
},
5524 {"lp:434843 buffered", 1, regression_bug_434843_buffered
},
5525 {"lp:421108", 1, regression_bug_421108
},
5526 {"lp:442914", 1, regression_bug_442914
},
5527 {"lp:447342", 1, regression_bug_447342
},
5528 {"lp:463297", 1, regression_bug_463297
},
5532 test_st ketama_compatibility
[]= {
5533 {"libmemcached", 1, ketama_compatibility_libmemcached
},
5534 {"spymemcached", 1, ketama_compatibility_spymemcached
},
5538 test_st generate_tests
[] ={
5539 {"generate_pairs", 1, generate_pairs
},
5540 {"generate_data", 1, generate_data
},
5541 {"get_read", 0, get_read
},
5542 {"delete_generate", 0, delete_generate
},
5543 {"generate_buffer_data", 1, generate_buffer_data
},
5544 {"delete_buffer", 0, delete_buffer_generate
},
5545 {"generate_data", 1, generate_data
},
5546 {"mget_read", 0, mget_read
},
5547 {"mget_read_result", 0, mget_read_result
},
5548 {"mget_read_function", 0, mget_read_function
},
5549 {"cleanup", 1, cleanup_pairs
},
5550 {"generate_large_pairs", 1, generate_large_pairs
},
5551 {"generate_data", 1, generate_data
},
5552 {"generate_buffer_data", 1, generate_buffer_data
},
5553 {"cleanup", 1, cleanup_pairs
},
5557 test_st consistent_tests
[] ={
5558 {"generate_pairs", 1, generate_pairs
},
5559 {"generate_data", 1, generate_data
},
5560 {"get_read", 0, get_read_count
},
5561 {"cleanup", 1, cleanup_pairs
},
5565 test_st consistent_weighted_tests
[] ={
5566 {"generate_pairs", 1, generate_pairs
},
5567 {"generate_data", 1, generate_data_with_stats
},
5568 {"get_read", 0, get_read_count
},
5569 {"cleanup", 1, cleanup_pairs
},
5573 test_st hsieh_availability
[] ={
5574 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
5578 test_st ketama_auto_eject_hosts
[] ={
5579 {"auto_eject_hosts", 1, auto_eject_hosts
},
5580 {"output_ketama_weighted_keys", 1, output_ketama_weighted_keys
},
5584 test_st hash_tests
[] ={
5585 {"md5", 0, md5_run
},
5586 {"crc", 0, crc_run
},
5587 {"fnv1_64", 0, fnv1_64_run
},
5588 {"fnv1a_64", 0, fnv1a_64_run
},
5589 {"fnv1_32", 0, fnv1_32_run
},
5590 {"fnv1a_32", 0, fnv1a_32_run
},
5591 {"hsieh", 0, hsieh_run
},
5592 {"murmur", 0, murmur_run
},
5593 {"jenkis", 0, jenkins_run
},
5597 collection_st collection
[] ={
5598 {"hsieh_availability",0,0,hsieh_availability
},
5599 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
5600 {"udp_io", init_udp
, 0, upd_io_tests
},
5601 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
5602 {"block", 0, 0, tests
},
5603 {"binary", pre_binary
, 0, tests
},
5604 {"nonblock", pre_nonblock
, 0, tests
},
5605 {"nodelay", pre_nodelay
, 0, tests
},
5606 {"settimer", pre_settimer
, 0, tests
},
5607 {"md5", pre_md5
, 0, tests
},
5608 {"crc", pre_crc
, 0, tests
},
5609 {"hsieh", pre_hsieh
, 0, tests
},
5610 {"jenkins", pre_jenkins
, 0, tests
},
5611 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
5612 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
5613 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
5614 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
5615 {"ketama", pre_behavior_ketama
, 0, tests
},
5616 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
5617 {"unix_socket", pre_unix_socket
, 0, tests
},
5618 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
5619 {"poll_timeout", poll_timeout
, 0, tests
},
5620 {"gets", enable_cas
, 0, tests
},
5621 {"consistent", enable_consistent
, 0, tests
},
5622 #ifdef MEMCACHED_ENABLE_DEPRECATED
5623 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
5625 {"memory_allocators", set_memory_alloc
, 0, tests
},
5626 {"prefix", set_prefix
, 0, tests
},
5627 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
5628 {"string", 0, 0, string_tests
},
5629 {"result", 0, 0, result_tests
},
5630 {"async", pre_nonblock
, 0, async_tests
},
5631 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
5632 {"user", 0, 0, user_tests
},
5633 {"generate", 0, 0, generate_tests
},
5634 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
5635 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
5636 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
5637 {"generate_md5", pre_md5
, 0, generate_tests
},
5638 {"generate_murmur", pre_murmur
, 0, generate_tests
},
5639 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
5640 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
5641 {"consistent_not", 0, 0, consistent_tests
},
5642 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
5643 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
5644 {"ketama_compat", 0, 0, ketama_compatibility
},
5645 {"test_hashes", 0, 0, hash_tests
},
5646 {"replication", pre_replication
, 0, replication_tests
},
5647 {"replication_noblock", pre_replication_noblock
, 0, replication_tests
},
5648 {"regression", 0, 0, regression_tests
},
5652 #define SERVERS_TO_CREATE 5
5654 /* Prototypes for functions we will pass to test framework */
5655 void *world_create(void);
5656 void world_destroy(void *p
);
5658 void *world_create(void)
5660 server_startup_st
*construct
;
5662 construct
= calloc(sizeof(server_startup_st
), 1);
5663 construct
->count
= SERVERS_TO_CREATE
;
5665 server_startup(construct
);
5671 void world_destroy(void *p
)
5673 server_startup_st
*construct
= (server_startup_st
*)p
;
5674 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
5675 memcached_server_list_free(servers
);
5677 server_shutdown(construct
);
5681 void get_world(world_st
*world
)
5683 world
->collections
= collection
;
5684 world
->create
= world_create
;
5685 world
->destroy
= world_destroy
;