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 assert(server_list
== NULL
);
63 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
64 assert(server_list
== NULL
);
66 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
67 assert(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 assert in server_display_function */
90 memcached_server_function callbacks
[1];
91 memcached_st
*local_memc
;
93 local_memc
= memcached_create(NULL
);
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 assert(local_memc
->number_of_hosts
== x
+ 1);
102 assert(local_memc
->hosts
[0].count
== x
+1);
103 assert(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 assert in server_display_function */
119 memcached_server_function callbacks
[1];
120 memcached_st
*local_memc
;
122 local_memc
= memcached_create(NULL
);
124 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
125 assert(rc
== MEMCACHED_SUCCESS
);
127 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
128 assert(rc
== MEMCACHED_SUCCESS
);
129 assert(local_memc
->hosts
[0].port
== 43043);
131 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
132 assert(rc
== MEMCACHED_SUCCESS
);
133 assert(local_memc
->hosts
[0].port
== 43042);
134 assert(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 assert in server_display_function */
160 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
162 memcached_server_function callbacks
[1];
163 memcached_st
*local_memc
;
165 local_memc
= memcached_create(NULL
);
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 assert(local_memc
->number_of_hosts
== x
+1);
173 assert(local_memc
->hosts
[0].count
== x
+1);
174 assert(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
);
208 memcached_free(memc_clone
);
211 /* Can we init from null? */
213 memcached_st
*memc_clone
;
214 memc_clone
= memcached_clone(NULL
, memc
);
217 assert(memc_clone
->call_free
== memc
->call_free
);
218 assert(memc_clone
->call_malloc
== memc
->call_malloc
);
219 assert(memc_clone
->call_realloc
== memc
->call_realloc
);
220 assert(memc_clone
->call_calloc
== memc
->call_calloc
);
221 assert(memc_clone
->connect_timeout
== memc
->connect_timeout
);
222 assert(memc_clone
->delete_trigger
== memc
->delete_trigger
);
223 assert(memc_clone
->distribution
== memc
->distribution
);
224 assert(memc_clone
->flags
== memc
->flags
);
225 assert(memc_clone
->get_key_failure
== memc
->get_key_failure
);
226 assert(memc_clone
->hash
== memc
->hash
);
227 assert(memc_clone
->hash_continuum
== memc
->hash_continuum
);
228 assert(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
229 assert(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
230 assert(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
231 assert(memc_clone
->on_cleanup
== memc
->on_cleanup
);
232 assert(memc_clone
->on_clone
== memc
->on_clone
);
233 assert(memc_clone
->poll_timeout
== memc
->poll_timeout
);
234 assert(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
235 assert(memc_clone
->recv_size
== memc
->recv_size
);
236 assert(memc_clone
->retry_timeout
== memc
->retry_timeout
);
237 assert(memc_clone
->send_size
== memc
->send_size
);
238 assert(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
239 assert(memc_clone
->snd_timeout
== memc
->snd_timeout
);
240 assert(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
);
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
);
262 memcached_free(memc_clone
);
268 static test_return_t
userdata_test(memcached_st
*memc
)
271 assert(memcached_set_user_data(memc
, foo
) == NULL
);
272 assert(memcached_get_user_data(memc
) == foo
);
273 assert(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 assert(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 assert(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 assert(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 assert(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 assert(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 assert(rc
== MEMCACHED_SUCCESS
);
347 rc
= memcached_append(memc
, key
, strlen(key
),
348 " the", strlen(" the"),
349 (time_t)0, (uint32_t)0);
350 assert(rc
== MEMCACHED_SUCCESS
);
352 rc
= memcached_append(memc
, key
, strlen(key
),
353 " people", strlen(" people"),
354 (time_t)0, (uint32_t)0);
355 assert(rc
== MEMCACHED_SUCCESS
);
357 out_value
= memcached_get(memc
, key
, strlen(key
),
358 &value_length
, &flags
, &rc
);
359 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
360 assert(strlen("we the people") == value_length
);
361 assert(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 assert(rc
== MEMCACHED_SUCCESS
);
381 rc
= memcached_set(memc
,
384 (time_t)0, (uint32_t)0);
385 assert(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 assert(rc
== MEMCACHED_SUCCESS
);
396 value
= memcached_get(memc
, key
, strlen(key
),
397 &value_length
, &flags
, &rc
);
398 assert((value_length
== (sizeof(unsigned int) * x
)));
399 assert(rc
== MEMCACHED_SUCCESS
);
401 store_ptr
= (unsigned int *)value
;
403 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
405 assert(*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 assert(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 assert(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 assert(results
->cas
);
445 assert(rc
== MEMCACHED_SUCCESS
);
446 assert(memcached_result_cas(results
));
448 assert(!memcmp(value
, "we the people", strlen("we the people")));
449 assert(strlen("we the people") == value_length
);
450 assert(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 assert(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 assert(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 assert(rc
== MEMCACHED_SUCCESS
);
490 assert(memcached_result_cas(results
));
491 assert(!memcmp(value
, memcached_result_value(results
), value_length
));
492 assert(strlen(memcached_result_value(results
)) == value_length
);
493 assert(rc
== MEMCACHED_SUCCESS
);
494 uint64_t cas
= memcached_result_cas(results
);
497 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
498 assert(rc
== MEMCACHED_END
);
499 assert(results
== NULL
);
502 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
503 assert(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 assert(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 assert(rc
== MEMCACHED_SUCCESS
);
529 rc
= memcached_set(memc
, key
, strlen(key
),
530 value
, strlen(value
),
531 (time_t)0, (uint32_t)0);
532 assert(rc
== MEMCACHED_SUCCESS
);
534 rc
= memcached_prepend(memc
, key
, strlen(key
),
535 "the ", strlen("the "),
536 (time_t)0, (uint32_t)0);
537 assert(rc
== MEMCACHED_SUCCESS
);
539 rc
= memcached_prepend(memc
, key
, strlen(key
),
540 "we ", strlen("we "),
541 (time_t)0, (uint32_t)0);
542 assert(rc
== MEMCACHED_SUCCESS
);
544 out_value
= memcached_get(memc
, key
, strlen(key
),
545 &value_length
, &flags
, &rc
);
546 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
547 assert(strlen("we the people") == value_length
);
548 assert(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 assert(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 */
578 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
580 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
586 ** There was a problem of leaking filedescriptors in the initial release
587 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
588 ** systems it seems that the kernel is slow on reclaiming the resources
589 ** because the connects starts to time out (the test doesn't do much
590 ** anyway, so just loop 10 iterations)
592 static test_return_t
add_wrapper(memcached_st
*memc
)
595 unsigned int max
= 10000;
603 for (x
= 0; x
< max
; x
++)
609 static test_return_t
replace_test(memcached_st
*memc
)
612 const char *key
= "foo";
613 const char *value
= "when we sanitize";
614 const char *original
= "first we insert some data";
616 rc
= memcached_set(memc
, key
, strlen(key
),
617 original
, strlen(original
),
618 (time_t)0, (uint32_t)0);
619 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
621 rc
= memcached_replace(memc
, key
, strlen(key
),
622 value
, strlen(value
),
623 (time_t)0, (uint32_t)0);
624 assert(rc
== MEMCACHED_SUCCESS
);
629 static test_return_t
delete_test(memcached_st
*memc
)
632 const char *key
= "foo";
633 const char *value
= "when we sanitize";
635 rc
= memcached_set(memc
, key
, strlen(key
),
636 value
, strlen(value
),
637 (time_t)0, (uint32_t)0);
638 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
640 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
641 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
646 static test_return_t
flush_test(memcached_st
*memc
)
650 rc
= memcached_flush(memc
, 0);
651 assert(rc
== MEMCACHED_SUCCESS
);
656 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
657 memcached_server_st
*server
__attribute__((unused
)),
658 void *context
__attribute__((unused
)))
662 return MEMCACHED_SUCCESS
;
665 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
668 strcpy(context
, "foo bad");
669 memcached_server_function callbacks
[1];
671 callbacks
[0]= server_function
;
672 memcached_server_cursor(memc
, callbacks
, context
, 1);
676 static test_return_t
bad_key_test(memcached_st
*memc
)
679 const char *key
= "foo bad";
681 size_t string_length
;
683 memcached_st
*memc_clone
;
685 size_t max_keylen
= 0xffff;
687 memc_clone
= memcached_clone(NULL
, memc
);
690 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
691 assert(rc
== MEMCACHED_SUCCESS
);
693 /* All keys are valid in the binary protocol (except for length) */
694 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
696 string
= memcached_get(memc_clone
, key
, strlen(key
),
697 &string_length
, &flags
, &rc
);
698 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
699 assert(string_length
== 0);
703 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
704 assert(rc
== MEMCACHED_SUCCESS
);
705 string
= memcached_get(memc_clone
, key
, strlen(key
),
706 &string_length
, &flags
, &rc
);
707 assert(rc
== MEMCACHED_NOTFOUND
);
708 assert(string_length
== 0);
711 /* Test multi key for bad keys */
712 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
713 size_t key_lengths
[] = { 7, 7, 7 };
715 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
716 assert(rc
== MEMCACHED_SUCCESS
);
718 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
719 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
721 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
722 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
726 /* The following test should be moved to the end of this function when the
727 memcached server is updated to allow max size length of the keys in the
730 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
731 assert(rc
== MEMCACHED_SUCCESS
);
733 char *longkey
= malloc(max_keylen
+ 1);
736 memset(longkey
, 'a', max_keylen
+ 1);
737 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
738 &string_length
, &flags
, &rc
);
739 assert(rc
== MEMCACHED_NOTFOUND
);
740 assert(string_length
== 0);
743 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
744 &string_length
, &flags
, &rc
);
745 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
746 assert(string_length
== 0);
753 /* Make sure zero length keys are marked as bad */
755 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
756 assert(rc
== MEMCACHED_SUCCESS
);
757 string
= memcached_get(memc_clone
, key
, 0,
758 &string_length
, &flags
, &rc
);
759 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
760 assert(string_length
== 0);
763 memcached_free(memc_clone
);
768 #define READ_THROUGH_VALUE "set for me"
769 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
770 char *key
__attribute__((unused
)),
771 size_t key_length
__attribute__((unused
)),
772 memcached_result_st
*result
)
775 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
778 static test_return_t
read_through(memcached_st
*memc
)
781 const char *key
= "foo";
783 size_t string_length
;
785 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
787 string
= memcached_get(memc
, key
, strlen(key
),
788 &string_length
, &flags
, &rc
);
790 assert(rc
== MEMCACHED_NOTFOUND
);
791 assert(string_length
== 0);
794 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
796 assert(rc
== MEMCACHED_SUCCESS
);
798 string
= memcached_get(memc
, key
, strlen(key
),
799 &string_length
, &flags
, &rc
);
801 assert(rc
== MEMCACHED_SUCCESS
);
802 assert(string_length
== strlen(READ_THROUGH_VALUE
));
803 assert(!strcmp(READ_THROUGH_VALUE
, string
));
806 string
= memcached_get(memc
, key
, strlen(key
),
807 &string_length
, &flags
, &rc
);
809 assert(rc
== MEMCACHED_SUCCESS
);
810 assert(string_length
== strlen(READ_THROUGH_VALUE
));
811 assert(!strcmp(READ_THROUGH_VALUE
, string
));
817 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
819 size_t key_length
__attribute__((unused
)))
823 return MEMCACHED_SUCCESS
;
826 static test_return_t
delete_through(memcached_st
*memc
)
828 memcached_trigger_delete_key callback
;
831 callback
= (memcached_trigger_delete_key
)delete_trigger
;
833 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
834 assert(rc
== MEMCACHED_SUCCESS
);
839 static test_return_t
get_test(memcached_st
*memc
)
842 const char *key
= "foo";
844 size_t string_length
;
847 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
848 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
850 string
= memcached_get(memc
, key
, strlen(key
),
851 &string_length
, &flags
, &rc
);
853 assert(rc
== MEMCACHED_NOTFOUND
);
854 assert(string_length
== 0);
860 static test_return_t
get_test2(memcached_st
*memc
)
863 const char *key
= "foo";
864 const char *value
= "when we sanitize";
866 size_t string_length
;
869 rc
= memcached_set(memc
, key
, strlen(key
),
870 value
, strlen(value
),
871 (time_t)0, (uint32_t)0);
872 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
874 string
= memcached_get(memc
, key
, strlen(key
),
875 &string_length
, &flags
, &rc
);
878 assert(rc
== MEMCACHED_SUCCESS
);
879 assert(string_length
== strlen(value
));
880 assert(!memcmp(string
, value
, string_length
));
887 static test_return_t
set_test2(memcached_st
*memc
)
890 const char *key
= "foo";
891 const char *value
= "train in the brain";
892 size_t value_length
= strlen(value
);
895 for (x
= 0; x
< 10; x
++)
897 rc
= memcached_set(memc
, key
, strlen(key
),
899 (time_t)0, (uint32_t)0);
900 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
906 static test_return_t
set_test3(memcached_st
*memc
)
910 size_t value_length
= 8191;
913 value
= (char*)malloc(value_length
);
916 for (x
= 0; x
< value_length
; x
++)
917 value
[x
] = (char) (x
% 127);
919 /* The dump test relies on there being at least 32 items in memcached */
920 for (x
= 0; x
< 32; x
++)
924 sprintf(key
, "foo%u", x
);
926 rc
= memcached_set(memc
, key
, strlen(key
),
928 (time_t)0, (uint32_t)0);
929 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
937 static test_return_t
get_test3(memcached_st
*memc
)
940 const char *key
= "foo";
942 size_t value_length
= 8191;
944 size_t string_length
;
948 value
= (char*)malloc(value_length
);
951 for (x
= 0; x
< value_length
; x
++)
952 value
[x
] = (char) (x
% 127);
954 rc
= memcached_set(memc
, key
, strlen(key
),
956 (time_t)0, (uint32_t)0);
957 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
959 string
= memcached_get(memc
, key
, strlen(key
),
960 &string_length
, &flags
, &rc
);
962 assert(rc
== MEMCACHED_SUCCESS
);
964 assert(string_length
== value_length
);
965 assert(!memcmp(string
, value
, string_length
));
973 static test_return_t
get_test4(memcached_st
*memc
)
976 const char *key
= "foo";
978 size_t value_length
= 8191;
980 size_t string_length
;
984 value
= (char*)malloc(value_length
);
987 for (x
= 0; x
< value_length
; x
++)
988 value
[x
] = (char) (x
% 127);
990 rc
= memcached_set(memc
, key
, strlen(key
),
992 (time_t)0, (uint32_t)0);
993 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
995 for (x
= 0; x
< 10; x
++)
997 string
= memcached_get(memc
, key
, strlen(key
),
998 &string_length
, &flags
, &rc
);
1000 assert(rc
== MEMCACHED_SUCCESS
);
1002 assert(string_length
== value_length
);
1003 assert(!memcmp(string
, value
, string_length
));
1009 return TEST_SUCCESS
;
1013 * This test verifies that memcached_read_one_response doesn't try to
1014 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1015 * responses before you execute a storage command.
1017 static test_return_t
get_test5(memcached_st
*memc
)
1020 ** Request the same key twice, to ensure that we hash to the same server
1021 ** (so that we have multiple response values queued up) ;-)
1023 const char *keys
[]= { "key", "key" };
1024 size_t lengths
[]= { 3, 3 };
1028 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1029 keys
[0], lengths
[0], 0, 0);
1030 assert(rc
== MEMCACHED_SUCCESS
);
1031 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1033 memcached_result_st results_obj
;
1034 memcached_result_st
*results
;
1035 results
=memcached_result_create(memc
, &results_obj
);
1037 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1039 memcached_result_free(&results_obj
);
1041 /* Don't read out the second result, but issue a set instead.. */
1042 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1043 assert(rc
== MEMCACHED_SUCCESS
);
1045 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1046 &rlen
, &flags
, &rc
);
1047 assert(val
== NULL
);
1048 assert(rc
== MEMCACHED_NOTFOUND
);
1049 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1050 assert(val
!= NULL
);
1051 assert(rc
== MEMCACHED_SUCCESS
);
1054 return TEST_SUCCESS
;
1057 static test_return_t
mget_end(memcached_st
*memc
)
1059 const char *keys
[]= { "foo", "foo2" };
1060 size_t lengths
[]= { 3, 4 };
1061 const char *values
[]= { "fjord", "41" };
1063 memcached_return rc
;
1066 for (int i
= 0; i
< 2; i
++)
1068 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1069 (time_t)0, (uint32_t)0);
1070 assert(rc
== MEMCACHED_SUCCESS
);
1074 size_t string_length
;
1077 // retrieve both via mget
1078 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1079 assert(rc
== MEMCACHED_SUCCESS
);
1081 char key
[MEMCACHED_MAX_KEY
];
1084 // this should get both
1085 for (int i
= 0; i
< 2; i
++)
1087 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1089 assert(rc
== MEMCACHED_SUCCESS
);
1091 if (key_length
== 4)
1093 assert(string_length
== strlen(values
[val
]));
1094 assert(strncmp(values
[val
], string
, string_length
) == 0);
1098 // this should indicate end
1099 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1100 assert(rc
== MEMCACHED_END
);
1103 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1104 assert(rc
== MEMCACHED_SUCCESS
);
1106 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1107 assert(key_length
== lengths
[0]);
1108 assert(strncmp(keys
[0], key
, key_length
) == 0);
1109 assert(string_length
== strlen(values
[0]));
1110 assert(strncmp(values
[0], string
, string_length
) == 0);
1111 assert(rc
== MEMCACHED_SUCCESS
);
1114 // this should indicate end
1115 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1116 assert(rc
== MEMCACHED_END
);
1118 return TEST_SUCCESS
;
1121 /* Do not copy the style of this code, I just access hosts to testthis function */
1122 static test_return_t
stats_servername_test(memcached_st
*memc
)
1124 memcached_return rc
;
1125 memcached_stat_st memc_stat
;
1126 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1127 memc
->hosts
[0].hostname
,
1128 memc
->hosts
[0].port
);
1130 return TEST_SUCCESS
;
1133 static test_return_t
increment_test(memcached_st
*memc
)
1135 uint64_t new_number
;
1136 memcached_return rc
;
1137 const char *key
= "number";
1138 const char *value
= "0";
1140 rc
= memcached_set(memc
, key
, strlen(key
),
1141 value
, strlen(value
),
1142 (time_t)0, (uint32_t)0);
1143 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1145 rc
= memcached_increment(memc
, key
, strlen(key
),
1147 assert(rc
== MEMCACHED_SUCCESS
);
1148 assert(new_number
== 1);
1150 rc
= memcached_increment(memc
, key
, strlen(key
),
1152 assert(rc
== MEMCACHED_SUCCESS
);
1153 assert(new_number
== 2);
1155 return TEST_SUCCESS
;
1158 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1160 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1162 uint64_t new_number
;
1163 memcached_return rc
;
1164 const char *key
= "number";
1165 uint64_t initial
= 0;
1167 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1168 1, initial
, 0, &new_number
);
1169 assert(rc
== MEMCACHED_SUCCESS
);
1170 assert(new_number
== initial
);
1172 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1173 1, initial
, 0, &new_number
);
1174 assert(rc
== MEMCACHED_SUCCESS
);
1175 assert(new_number
== (initial
+ 1));
1177 return TEST_SUCCESS
;
1180 static test_return_t
decrement_test(memcached_st
*memc
)
1182 uint64_t new_number
;
1183 memcached_return rc
;
1184 const char *key
= "number";
1185 const char *value
= "3";
1187 rc
= memcached_set(memc
, key
, strlen(key
),
1188 value
, strlen(value
),
1189 (time_t)0, (uint32_t)0);
1190 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1192 rc
= memcached_decrement(memc
, key
, strlen(key
),
1194 assert(rc
== MEMCACHED_SUCCESS
);
1195 assert(new_number
== 2);
1197 rc
= memcached_decrement(memc
, key
, strlen(key
),
1199 assert(rc
== MEMCACHED_SUCCESS
);
1200 assert(new_number
== 1);
1202 return TEST_SUCCESS
;
1205 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1207 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1209 uint64_t new_number
;
1210 memcached_return rc
;
1211 const char *key
= "number";
1212 uint64_t initial
= 3;
1214 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1215 1, initial
, 0, &new_number
);
1216 assert(rc
== MEMCACHED_SUCCESS
);
1217 assert(new_number
== initial
);
1219 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1220 1, initial
, 0, &new_number
);
1221 assert(rc
== MEMCACHED_SUCCESS
);
1222 assert(new_number
== (initial
- 1));
1224 return TEST_SUCCESS
;
1227 static test_return_t
quit_test(memcached_st
*memc
)
1229 memcached_return rc
;
1230 const char *key
= "fudge";
1231 const char *value
= "sanford and sun";
1233 rc
= memcached_set(memc
, key
, strlen(key
),
1234 value
, strlen(value
),
1235 (time_t)10, (uint32_t)3);
1236 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1237 memcached_quit(memc
);
1239 rc
= memcached_set(memc
, key
, strlen(key
),
1240 value
, strlen(value
),
1241 (time_t)50, (uint32_t)9);
1242 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1244 return TEST_SUCCESS
;
1247 static test_return_t
mget_result_test(memcached_st
*memc
)
1249 memcached_return rc
;
1250 const char *keys
[]= {"fudge", "son", "food"};
1251 size_t key_length
[]= {5, 3, 4};
1254 memcached_result_st results_obj
;
1255 memcached_result_st
*results
;
1257 results
= memcached_result_create(memc
, &results_obj
);
1259 assert(&results_obj
== results
);
1261 /* We need to empty the server before continueing test */
1262 rc
= memcached_flush(memc
, 0);
1263 assert(rc
== MEMCACHED_SUCCESS
);
1265 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1266 assert(rc
== MEMCACHED_SUCCESS
);
1268 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1273 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1275 assert(rc
== MEMCACHED_END
);
1277 for (x
= 0; x
< 3; x
++)
1279 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1280 keys
[x
], key_length
[x
],
1281 (time_t)50, (uint32_t)9);
1282 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1285 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1286 assert(rc
== MEMCACHED_SUCCESS
);
1288 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1291 assert(&results_obj
== results
);
1292 assert(rc
== MEMCACHED_SUCCESS
);
1293 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1294 assert(!memcmp(memcached_result_key_value(results
),
1295 memcached_result_value(results
),
1296 memcached_result_length(results
)));
1299 memcached_result_free(&results_obj
);
1301 return TEST_SUCCESS
;
1304 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1306 memcached_return rc
;
1307 const char *keys
[]= {"fudge", "son", "food"};
1308 size_t key_length
[]= {5, 3, 4};
1311 memcached_result_st
*results
;
1313 /* We need to empty the server before continueing test */
1314 rc
= memcached_flush(memc
, 0);
1315 assert(rc
== MEMCACHED_SUCCESS
);
1317 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1318 assert(rc
== MEMCACHED_SUCCESS
);
1320 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1325 assert(rc
== MEMCACHED_END
);
1327 for (x
= 0; x
< 3; x
++)
1329 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1330 keys
[x
], key_length
[x
],
1331 (time_t)50, (uint32_t)9);
1332 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1335 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1336 assert(rc
== MEMCACHED_SUCCESS
);
1339 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1342 assert(rc
== MEMCACHED_SUCCESS
);
1343 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1344 assert(!memcmp(memcached_result_key_value(results
),
1345 memcached_result_value(results
),
1346 memcached_result_length(results
)));
1347 memcached_result_free(results
);
1351 return TEST_SUCCESS
;
1354 /* Count the results */
1355 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1356 memcached_result_st
*result
__attribute__((unused
)),
1359 unsigned int *counter
= (unsigned int *)context
;
1361 *counter
= *counter
+ 1;
1363 return MEMCACHED_SUCCESS
;
1366 static test_return_t
mget_result_function(memcached_st
*memc
)
1368 memcached_return rc
;
1369 const char *keys
[]= {"fudge", "son", "food"};
1370 size_t key_length
[]= {5, 3, 4};
1372 unsigned int counter
;
1373 memcached_execute_function callbacks
[1];
1375 /* We need to empty the server before continueing test */
1376 rc
= memcached_flush(memc
, 0);
1377 for (x
= 0; x
< 3; x
++)
1379 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1380 keys
[x
], key_length
[x
],
1381 (time_t)50, (uint32_t)9);
1382 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1385 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1386 assert(rc
== MEMCACHED_SUCCESS
);
1388 callbacks
[0]= &callback_counter
;
1390 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1392 assert(counter
== 3);
1394 return TEST_SUCCESS
;
1397 static test_return_t
mget_test(memcached_st
*memc
)
1399 memcached_return rc
;
1400 const char *keys
[]= {"fudge", "son", "food"};
1401 size_t key_length
[]= {5, 3, 4};
1405 char return_key
[MEMCACHED_MAX_KEY
];
1406 size_t return_key_length
;
1408 size_t return_value_length
;
1410 /* We need to empty the server before continueing test */
1411 rc
= memcached_flush(memc
, 0);
1412 assert(rc
== MEMCACHED_SUCCESS
);
1414 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1415 assert(rc
== MEMCACHED_SUCCESS
);
1417 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1418 &return_value_length
, &flags
, &rc
)) != NULL
)
1420 assert(return_value
);
1422 assert(!return_value
);
1423 assert(return_value_length
== 0);
1424 assert(rc
== MEMCACHED_END
);
1426 for (x
= 0; x
< 3; x
++)
1428 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1429 keys
[x
], key_length
[x
],
1430 (time_t)50, (uint32_t)9);
1431 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1434 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1435 assert(rc
== MEMCACHED_SUCCESS
);
1438 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1439 &return_value_length
, &flags
, &rc
)))
1441 assert(return_value
);
1442 assert(rc
== MEMCACHED_SUCCESS
);
1443 assert(return_key_length
== return_value_length
);
1444 assert(!memcmp(return_value
, return_key
, return_value_length
));
1449 return TEST_SUCCESS
;
1452 static test_return_t
mget_execute(memcached_st
*memc
)
1455 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1459 * I only want to hit _one_ server so I know the number of requests I'm
1460 * sending in the pipeline.
1462 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1463 memc
->number_of_hosts
= 1;
1465 int max_keys
= binary
? 20480 : 1;
1468 char **keys
= calloc((size_t)max_keys
, sizeof(char*));
1469 size_t *key_length
=calloc((size_t)max_keys
, sizeof(size_t));
1471 /* First add all of the items.. */
1472 char blob
[1024] = {0};
1473 memcached_return rc
;
1474 for (int x
= 0; x
< max_keys
; ++x
)
1477 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
1479 assert(keys
[x
] != NULL
);
1480 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1481 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1484 /* Try to get all of them with a large multiget */
1485 unsigned int counter
= 0;
1486 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
1487 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1488 (size_t)max_keys
, callbacks
, &counter
, 1);
1492 assert(rc
== MEMCACHED_SUCCESS
);
1494 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1495 assert(rc
== MEMCACHED_END
);
1497 /* Verify that we got all of the items */
1498 assert(counter
== (unsigned int)max_keys
);
1502 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
1503 assert(counter
== 0);
1506 /* Release all allocated resources */
1507 for (int x
= 0; x
< max_keys
; ++x
)
1512 memc
->number_of_hosts
= number_of_hosts
;
1513 return TEST_SUCCESS
;
1516 static test_return_t
get_stats_keys(memcached_st
*memc
)
1520 memcached_stat_st memc_stat
;
1521 memcached_return rc
;
1523 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1524 assert(rc
== MEMCACHED_SUCCESS
);
1525 for (ptr
= list
; *ptr
; ptr
++)
1531 return TEST_SUCCESS
;
1534 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1536 const char *version_string
;
1538 version_string
= memcached_lib_version();
1540 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1542 return TEST_SUCCESS
;
1545 static test_return_t
get_stats(memcached_st
*memc
)
1550 memcached_return rc
;
1551 memcached_stat_st
*memc_stat
;
1553 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1554 assert(rc
== MEMCACHED_SUCCESS
);
1556 assert(rc
== MEMCACHED_SUCCESS
);
1559 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1561 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1562 assert(rc
== MEMCACHED_SUCCESS
);
1563 for (ptr
= list
; *ptr
; ptr
++);
1568 memcached_stat_free(NULL
, memc_stat
);
1570 return TEST_SUCCESS
;
1573 static test_return_t
add_host_test(memcached_st
*memc
)
1576 memcached_server_st
*servers
;
1577 memcached_return rc
;
1578 char servername
[]= "0.example.com";
1580 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1582 assert(1 == memcached_server_list_count(servers
));
1584 for (x
= 2; x
< 20; x
++)
1586 char buffer
[SMALL_STRING_LEN
];
1588 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1589 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1591 assert(rc
== MEMCACHED_SUCCESS
);
1592 assert(x
== memcached_server_list_count(servers
));
1595 rc
= memcached_server_push(memc
, servers
);
1596 assert(rc
== MEMCACHED_SUCCESS
);
1597 rc
= memcached_server_push(memc
, servers
);
1598 assert(rc
== MEMCACHED_SUCCESS
);
1600 memcached_server_list_free(servers
);
1602 return TEST_SUCCESS
;
1605 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1607 return MEMCACHED_SUCCESS
;
1610 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1612 return MEMCACHED_SUCCESS
;
1615 static test_return_t
callback_test(memcached_st
*memc
)
1617 /* Test User Data */
1621 memcached_return rc
;
1623 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1624 assert(rc
== MEMCACHED_SUCCESS
);
1625 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1626 assert(*test_ptr
== x
);
1629 /* Test Clone Callback */
1631 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1632 void *clone_cb_ptr
= *(void **)&clone_cb
;
1633 void *temp_function
= NULL
;
1634 memcached_return rc
;
1636 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1638 assert(rc
== MEMCACHED_SUCCESS
);
1639 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1640 assert(temp_function
== clone_cb_ptr
);
1643 /* Test Cleanup Callback */
1645 memcached_cleanup_func cleanup_cb
=
1646 (memcached_cleanup_func
)cleanup_test_callback
;
1647 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1648 void *temp_function
= NULL
;
1649 memcached_return rc
;
1651 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1653 assert(rc
== MEMCACHED_SUCCESS
);
1654 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1655 assert(temp_function
== cleanup_cb_ptr
);
1658 return TEST_SUCCESS
;
1661 /* We don't test the behavior itself, we test the switches */
1662 static test_return_t
behavior_test(memcached_st
*memc
)
1667 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1668 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1671 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1672 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1675 set
= MEMCACHED_HASH_MD5
;
1676 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1677 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1678 assert(value
== MEMCACHED_HASH_MD5
);
1682 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1683 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1686 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1687 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1690 set
= MEMCACHED_HASH_DEFAULT
;
1691 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1692 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1693 assert(value
== MEMCACHED_HASH_DEFAULT
);
1695 set
= MEMCACHED_HASH_CRC
;
1696 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1697 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1698 assert(value
== MEMCACHED_HASH_CRC
);
1700 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1703 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1706 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1707 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1708 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1709 return TEST_SUCCESS
;
1712 static test_return_t
fetch_all_results(memcached_st
*memc
)
1714 memcached_return rc
= MEMCACHED_SUCCESS
;
1715 char return_key
[MEMCACHED_MAX_KEY
];
1716 size_t return_key_length
;
1718 size_t return_value_length
;
1721 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1722 &return_value_length
, &flags
, &rc
)))
1724 assert(return_value
);
1725 assert(rc
== MEMCACHED_SUCCESS
);
1729 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
1732 /* Test case provided by Cal Haldenbrand */
1733 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
1735 unsigned int setter
= 1;
1738 unsigned long long total
= 0;
1741 char randomstuff
[6 * 1024];
1742 memcached_return rc
;
1744 memset(randomstuff
, 0, 6 * 1024);
1746 /* We just keep looking at the same values over and over */
1749 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1750 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1754 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1758 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1759 memset(randomstuff
, 0, 6 * 1024);
1760 assert(size
< 6 * 1024); /* Being safe here */
1762 for (j
= 0 ; j
< size
;j
++)
1763 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1766 sprintf(key
, "%d", x
);
1767 rc
= memcached_set(memc
, key
, strlen(key
),
1768 randomstuff
, strlen(randomstuff
), 10, 0);
1769 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1770 /* If we fail, lets try again */
1771 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1772 rc
= memcached_set(memc
, key
, strlen(key
),
1773 randomstuff
, strlen(randomstuff
), 10, 0);
1774 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1777 return TEST_SUCCESS
;
1780 /* Test case provided by Cal Haldenbrand */
1781 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
1784 unsigned int setter
;
1786 unsigned long long total
;
1789 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1790 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1792 setter
= 20 * 1024576;
1793 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1794 setter
= 20 * 1024576;
1795 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1796 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1797 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1799 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1802 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1804 memcached_return rc
= MEMCACHED_SUCCESS
;
1805 char buffer
[SMALL_STRING_LEN
];
1810 memset(buffer
, 0, SMALL_STRING_LEN
);
1812 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1813 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1814 &val_len
, &flags
, &rc
);
1815 if (rc
!= MEMCACHED_SUCCESS
)
1817 if (rc
== MEMCACHED_NOTFOUND
)
1831 return TEST_SUCCESS
;
1834 /* Do a large mget() over all the keys we think exist */
1835 #define KEY_COUNT 3000 // * 1024576
1836 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
1838 memcached_return rc
;
1839 unsigned int setter
;
1842 size_t key_lengths
[KEY_COUNT
];
1845 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1846 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1848 setter
= 20 * 1024576;
1849 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1850 setter
= 20 * 1024576;
1851 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1852 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1853 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1856 keys
= calloc(KEY_COUNT
, sizeof(char *));
1858 for (x
= 0; x
< KEY_COUNT
; x
++)
1862 snprintf(buffer
, 30, "%u", x
);
1863 keys
[x
]= strdup(buffer
);
1864 key_lengths
[x
]= strlen(keys
[x
]);
1867 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
1868 assert(rc
== MEMCACHED_SUCCESS
);
1870 assert(fetch_all_results(memc
) == TEST_SUCCESS
);
1872 for (x
= 0; x
< KEY_COUNT
; x
++)
1876 return TEST_SUCCESS
;
1879 /* Make sure we behave properly if server list has no values */
1880 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
1882 memcached_return rc
;
1883 const char *keys
[]= {"fudge", "son", "food"};
1884 size_t key_length
[]= {5, 3, 4};
1887 char return_key
[MEMCACHED_MAX_KEY
];
1888 size_t return_key_length
;
1890 size_t return_value_length
;
1892 /* Here we free everything before running a bunch of mget tests */
1894 memcached_server_list_free(memc
->hosts
);
1896 memc
->number_of_hosts
= 0;
1900 /* We need to empty the server before continueing test */
1901 rc
= memcached_flush(memc
, 0);
1902 assert(rc
== MEMCACHED_NO_SERVERS
);
1904 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1905 assert(rc
== MEMCACHED_NO_SERVERS
);
1907 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1908 &return_value_length
, &flags
, &rc
)) != NULL
)
1910 assert(return_value
);
1912 assert(!return_value
);
1913 assert(return_value_length
== 0);
1914 assert(rc
== MEMCACHED_NO_SERVERS
);
1916 for (x
= 0; x
< 3; x
++)
1918 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1919 keys
[x
], key_length
[x
],
1920 (time_t)50, (uint32_t)9);
1921 assert(rc
== MEMCACHED_NO_SERVERS
);
1924 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1925 assert(rc
== MEMCACHED_NO_SERVERS
);
1928 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1929 &return_value_length
, &flags
, &rc
)))
1931 assert(return_value
);
1932 assert(rc
== MEMCACHED_SUCCESS
);
1933 assert(return_key_length
== return_value_length
);
1934 assert(!memcmp(return_value
, return_key
, return_value_length
));
1939 return TEST_SUCCESS
;
1942 #define VALUE_SIZE_BUG5 1048064
1943 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
1945 memcached_return rc
;
1946 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1947 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1948 char return_key
[MEMCACHED_MAX_KEY
];
1949 size_t return_key_length
;
1951 size_t value_length
;
1955 char insert_data
[VALUE_SIZE_BUG5
];
1957 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1958 insert_data
[x
]= (signed char)rand();
1960 memcached_flush(memc
, 0);
1961 value
= memcached_get(memc
, keys
[0], key_length
[0],
1962 &value_length
, &flags
, &rc
);
1963 assert(value
== NULL
);
1964 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1967 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1968 &value_length
, &flags
, &rc
)))
1972 for (x
= 0; x
< 4; x
++)
1974 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1975 insert_data
, VALUE_SIZE_BUG5
,
1976 (time_t)0, (uint32_t)0);
1977 assert(rc
== MEMCACHED_SUCCESS
);
1980 for (x
= 0; x
< 10; x
++)
1982 value
= memcached_get(memc
, keys
[0], key_length
[0],
1983 &value_length
, &flags
, &rc
);
1987 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1989 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1990 &value_length
, &flags
, &rc
)))
1998 return TEST_SUCCESS
;
2001 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2003 memcached_return rc
;
2004 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2005 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2006 char return_key
[MEMCACHED_MAX_KEY
];
2007 size_t return_key_length
;
2009 size_t value_length
;
2013 char insert_data
[VALUE_SIZE_BUG5
];
2015 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2016 insert_data
[x
]= (signed char)rand();
2018 memcached_flush(memc
, 0);
2019 value
= memcached_get(memc
, keys
[0], key_length
[0],
2020 &value_length
, &flags
, &rc
);
2021 assert(value
== NULL
);
2022 assert(rc
== MEMCACHED_NOTFOUND
);
2023 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2024 assert(rc
== MEMCACHED_SUCCESS
);
2027 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2028 &value_length
, &flags
, &rc
)))
2031 assert(rc
== MEMCACHED_END
);
2033 for (x
= 0; x
< 4; x
++)
2035 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2036 insert_data
, VALUE_SIZE_BUG5
,
2037 (time_t)0, (uint32_t)0);
2038 assert(rc
== MEMCACHED_SUCCESS
);
2041 for (x
= 0; x
< 2; x
++)
2043 value
= memcached_get(memc
, keys
[0], key_length
[0],
2044 &value_length
, &flags
, &rc
);
2048 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2049 assert(rc
== MEMCACHED_SUCCESS
);
2051 /* We test for purge of partial complete fetches */
2052 for (count
= 3; count
; count
--)
2054 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2055 &value_length
, &flags
, &rc
);
2056 assert(rc
== MEMCACHED_SUCCESS
);
2057 assert(!(memcmp(value
, insert_data
, value_length
)));
2058 assert(value_length
);
2063 return TEST_SUCCESS
;
2066 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2068 memcached_return rc
;
2070 memcached_st
*memc_clone
;
2072 memcached_server_st
*servers
;
2073 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";
2075 servers
= memcached_servers_parse(server_list
);
2078 mine
= memcached_create(NULL
);
2079 rc
= memcached_server_push(mine
, servers
);
2080 assert(rc
== MEMCACHED_SUCCESS
);
2081 memcached_server_list_free(servers
);
2084 memc_clone
= memcached_clone(NULL
, mine
);
2086 memcached_quit(mine
);
2087 memcached_quit(memc_clone
);
2090 memcached_free(mine
);
2091 memcached_free(memc_clone
);
2093 return TEST_SUCCESS
;
2096 /* Test flag store/retrieve */
2097 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2099 memcached_return rc
;
2100 const char *keys
= "036790384900";
2101 size_t key_length
= strlen(keys
);
2102 char return_key
[MEMCACHED_MAX_KEY
];
2103 size_t return_key_length
;
2105 size_t value_length
;
2108 char insert_data
[VALUE_SIZE_BUG5
];
2110 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2111 insert_data
[x
]= (signed char)rand();
2113 memcached_flush(memc
, 0);
2116 rc
= memcached_set(memc
, keys
, key_length
,
2117 insert_data
, VALUE_SIZE_BUG5
,
2119 assert(rc
== MEMCACHED_SUCCESS
);
2122 value
= memcached_get(memc
, keys
, key_length
,
2123 &value_length
, &flags
, &rc
);
2124 assert(flags
== 245);
2128 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2131 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2132 &value_length
, &flags
, &rc
);
2133 assert(flags
== 245);
2138 return TEST_SUCCESS
;
2141 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2143 memcached_return rc
;
2144 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2145 size_t key_length
[3];
2150 char return_key
[MEMCACHED_MAX_KEY
];
2151 size_t return_key_length
;
2153 size_t return_value_length
;
2156 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2157 key_length
[1]= strlen("fudge&*@#");
2158 key_length
[2]= strlen("for^#@&$not");
2161 for (x
= 0; x
< 3; x
++)
2163 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2164 keys
[x
], key_length
[x
],
2165 (time_t)50, (uint32_t)9);
2166 assert(rc
== MEMCACHED_SUCCESS
);
2169 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2170 assert(rc
== MEMCACHED_SUCCESS
);
2172 /* We need to empty the server before continueing test */
2173 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2174 &return_value_length
, &flags
, &rc
)) != NULL
)
2176 assert(return_value
);
2182 return TEST_SUCCESS
;
2185 /* We are testing with aggressive timeout to get failures */
2186 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2188 const char *key
= "foo";
2190 size_t value_length
= 512;
2193 memcached_return rc
;
2194 unsigned int set
= 1;
2195 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2198 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2199 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2201 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2204 value
= (char*)malloc(value_length
* sizeof(char));
2206 for (x
= 0; x
< value_length
; x
++)
2207 value
[x
]= (char) (x
% 127);
2209 for (x
= 1; x
<= 100000; ++x
)
2211 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2213 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2214 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2216 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2221 memcached_free(mclone
);
2223 return TEST_SUCCESS
;
2227 We are looking failures in the async protocol
2229 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2231 const char *key
= "foo";
2233 size_t value_length
= 512;
2236 memcached_return rc
;
2237 unsigned int set
= 1;
2239 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2241 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2242 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2244 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2247 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2249 assert(timeout
== -1);
2251 value
= (char*)malloc(value_length
* sizeof(char));
2253 for (x
= 0; x
< value_length
; x
++)
2254 value
[x
]= (char) (x
% 127);
2256 for (x
= 1; x
<= 100000; ++x
)
2258 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2262 memcached_free(mclone
);
2264 return TEST_SUCCESS
;
2268 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2270 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2272 memcached_return rc
;
2274 size_t value_length
;
2276 uint64_t number_value
;
2278 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2279 &value_length
, &flags
, &rc
);
2280 assert(value
== NULL
);
2281 assert(rc
== MEMCACHED_NOTFOUND
);
2283 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2286 assert(value
== NULL
);
2287 /* The binary protocol will set the key if it doesn't exist */
2288 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2289 assert(rc
== MEMCACHED_SUCCESS
);
2291 assert(rc
== MEMCACHED_NOTFOUND
);
2293 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2295 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2296 &value_length
, &flags
, &rc
);
2298 assert(rc
== MEMCACHED_SUCCESS
);
2301 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2303 assert(number_value
== 2);
2304 assert(rc
== MEMCACHED_SUCCESS
);
2306 return TEST_SUCCESS
;
2310 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2311 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2313 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2315 char key
[] = "key34567890";
2317 memcached_return rc
;
2318 size_t overflowSize
;
2320 char commandFirst
[]= "set key34567890 0 0 ";
2321 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2322 size_t commandLength
;
2325 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2327 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2329 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2331 overflow
= malloc(testSize
);
2332 assert(overflow
!= NULL
);
2334 memset(overflow
, 'x', testSize
);
2335 rc
= memcached_set(memc
, key
, strlen(key
),
2336 overflow
, testSize
, 0, 0);
2337 assert(rc
== MEMCACHED_SUCCESS
);
2341 return TEST_SUCCESS
;
2346 Test values of many different sizes
2347 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2348 set key34567890 0 0 8169 \r\n
2349 is sent followed by buffer of size 8169, followed by 8169
2351 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2354 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2355 memcached_return rc
;
2356 const char *key
= "foo";
2358 size_t value_length
= 18000;
2360 size_t string_length
;
2363 size_t current_length
;
2365 value
= (char*)malloc(value_length
);
2368 for (x
= 0; x
< value_length
; x
++)
2369 value
[x
] = (char) (x
% 127);
2371 for (current_length
= 0; current_length
< value_length
; current_length
++)
2373 rc
= memcached_set(memc
, key
, strlen(key
),
2374 value
, current_length
,
2375 (time_t)0, (uint32_t)0);
2376 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2378 string
= memcached_get(memc
, key
, strlen(key
),
2379 &string_length
, &flags
, &rc
);
2381 assert(rc
== MEMCACHED_SUCCESS
);
2382 assert(string_length
== current_length
);
2383 assert(!memcmp(string
, value
, string_length
));
2390 return TEST_SUCCESS
;
2394 Look for zero length value problems
2396 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2399 memcached_return rc
;
2400 const char *key
= "mykey";
2405 for (x
= 0; x
< 2; x
++)
2407 rc
= memcached_set(memc
, key
, strlen(key
),
2409 (time_t)0, (uint32_t)0);
2411 assert(rc
== MEMCACHED_SUCCESS
);
2413 value
= memcached_get(memc
, key
, strlen(key
),
2414 &length
, &flags
, &rc
);
2416 assert(rc
== MEMCACHED_SUCCESS
);
2417 assert(value
== NULL
);
2418 assert(length
== 0);
2421 value
= memcached_get(memc
, key
, strlen(key
),
2422 &length
, &flags
, &rc
);
2424 assert(rc
== MEMCACHED_SUCCESS
);
2425 assert(value
== NULL
);
2426 assert(length
== 0);
2430 return TEST_SUCCESS
;
2433 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2434 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2436 memcached_return rc
;
2437 const char *key
= "mykey";
2442 rc
= memcached_set(memc
, key
, strlen(key
),
2444 (time_t)0, UINT32_MAX
);
2446 assert(rc
== MEMCACHED_SUCCESS
);
2448 value
= memcached_get(memc
, key
, strlen(key
),
2449 &length
, &flags
, &rc
);
2451 assert(rc
== MEMCACHED_SUCCESS
);
2452 assert(value
== NULL
);
2453 assert(length
== 0);
2454 assert(flags
== UINT32_MAX
);
2456 return TEST_SUCCESS
;
2460 /* Check the validity of chinese key*/
2461 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2463 memcached_return rc
;
2464 const char *key
= "豆瓣";
2465 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2470 rc
= memcached_set(memc
, key
, strlen(key
),
2471 value
, strlen(value
),
2474 assert(rc
== MEMCACHED_SUCCESS
);
2476 value2
= memcached_get(memc
, key
, strlen(key
),
2477 &length
, &flags
, &rc
);
2479 assert(length
==strlen(value
));
2480 assert(rc
== MEMCACHED_SUCCESS
);
2481 assert(memcmp(value
, value2
, length
)==0);
2484 return TEST_SUCCESS
;
2492 static test_return_t
user_supplied_bug19(memcached_st
*memc
)
2495 memcached_server_st
*s
;
2496 memcached_return res
;
2500 m
= memcached_create(NULL
);
2501 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2502 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2504 s
= memcached_server_by_key(m
, "a", 1, &res
);
2505 memcached_server_free(s
);
2509 return TEST_SUCCESS
;
2512 /* CAS test from Andei */
2513 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2515 memcached_return status
;
2516 memcached_result_st
*result
, result_obj
;
2517 const char *key
= "abc";
2518 size_t key_len
= strlen("abc");
2519 const char *value
= "foobar";
2520 size_t value_len
= strlen(value
);
2522 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2524 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2525 assert(status
== MEMCACHED_SUCCESS
);
2527 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2528 assert(status
== MEMCACHED_SUCCESS
);
2530 result
= memcached_result_create(memc
, &result_obj
);
2533 memcached_result_create(memc
, &result_obj
);
2534 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2537 assert(status
== MEMCACHED_SUCCESS
);
2539 memcached_result_free(result
);
2541 return TEST_SUCCESS
;
2544 #include "ketama_test_cases.h"
2545 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2547 memcached_return rc
;
2550 memcached_server_st
*server_pool
;
2555 memc
= memcached_create(NULL
);
2558 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2559 assert(rc
== MEMCACHED_SUCCESS
);
2561 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2564 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2565 assert(rc
== MEMCACHED_SUCCESS
);
2567 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2568 assert(value
== MEMCACHED_HASH_MD5
);
2570 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");
2571 memcached_server_push(memc
, server_pool
);
2573 /* verify that the server list was parsed okay. */
2574 assert(memc
->number_of_hosts
== 8);
2575 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2576 assert(server_pool
[0].port
== 11211);
2577 assert(server_pool
[0].weight
== 600);
2578 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2579 assert(server_pool
[2].port
== 11211);
2580 assert(server_pool
[2].weight
== 200);
2581 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2582 assert(server_pool
[7].port
== 11211);
2583 assert(server_pool
[7].weight
== 100);
2585 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2586 * us test the boundary wraparound.
2588 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2590 /* verify the standard ketama set. */
2591 for (x
= 0; x
< 99; x
++)
2593 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2594 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2595 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2598 memcached_server_list_free(server_pool
);
2599 memcached_free(memc
);
2601 return TEST_SUCCESS
;
2604 /* Large mget() of missing keys with binary proto
2606 * If many binary quiet commands (such as getq's in an mget) fill the output
2607 * buffer and the server chooses not to respond, memcached_flush hangs. See
2608 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2611 /* sighandler_t function that always asserts false */
2612 static void fail(int unused
__attribute__((unused
)))
2618 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2620 memcached_return rc
;
2623 size_t* key_lengths
;
2624 void (*oldalarm
)(int);
2625 memcached_st
*memc_clone
;
2627 memc_clone
= memcached_clone(NULL
, memc
);
2630 /* only binproto uses getq for mget */
2631 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2633 /* empty the cache to ensure misses (hence non-responses) */
2634 rc
= memcached_flush(memc_clone
, 0);
2635 assert(rc
== MEMCACHED_SUCCESS
);
2637 key_lengths
= calloc(key_count
, sizeof(size_t));
2638 keys
= calloc(key_count
, sizeof(char *));
2640 for (x
= 0; x
< key_count
; x
++)
2644 snprintf(buffer
, 30, "%u", x
);
2645 keys
[x
]= strdup(buffer
);
2646 key_lengths
[x
]= strlen(keys
[x
]);
2649 oldalarm
= signal(SIGALRM
, fail
);
2652 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2653 assert(rc
== MEMCACHED_SUCCESS
);
2656 signal(SIGALRM
, oldalarm
);
2658 assert(fetch_all_results(memc
) == TEST_SUCCESS
);
2660 for (x
= 0; x
< key_count
; x
++)
2665 memcached_free(memc_clone
);
2667 return TEST_SUCCESS
;
2670 static memcached_return
pre_binary(memcached_st
*memc
);
2672 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
2674 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
2675 return TEST_SKIPPED
;
2679 /* should work as of r580 */
2680 rc
= _user_supplied_bug21(memc
, 10);
2681 assert(rc
== TEST_SUCCESS
);
2683 /* should fail as of r580 */
2684 rc
= _user_supplied_bug21(memc
, 1000);
2685 assert(rc
== TEST_SUCCESS
);
2687 return TEST_SUCCESS
;
2690 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
2694 memcached_return rc
;
2695 memcached_st
*memc
= memcached_create(NULL
);
2698 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2699 assert(rc
== MEMCACHED_SUCCESS
);
2701 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2704 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2705 assert(rc
== MEMCACHED_SUCCESS
);
2707 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2708 assert(value
== MEMCACHED_HASH_MD5
);
2710 /* server should be removed when in delay */
2711 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2712 assert(rc
== MEMCACHED_SUCCESS
);
2714 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2717 memcached_server_st
*server_pool
;
2718 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");
2719 memcached_server_push(memc
, server_pool
);
2721 /* verify that the server list was parsed okay. */
2722 assert(memc
->number_of_hosts
== 8);
2723 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2724 assert(server_pool
[0].port
== 11211);
2725 assert(server_pool
[0].weight
== 600);
2726 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2727 assert(server_pool
[2].port
== 11211);
2728 assert(server_pool
[2].weight
== 200);
2729 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2730 assert(server_pool
[7].port
== 11211);
2731 assert(server_pool
[7].weight
== 100);
2733 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2734 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2736 for (int x
= 0; x
< 99; x
++)
2738 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2739 assert(server_idx
!= 2);
2742 /* and re-added when it's back. */
2743 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2744 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2745 run_distribution(memc
);
2746 for (int x
= 0; x
< 99; x
++)
2748 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2749 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2750 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2753 memcached_server_list_free(server_pool
);
2754 memcached_free(memc
);
2756 return TEST_SUCCESS
;
2759 static test_return_t
result_static(memcached_st
*memc
)
2761 memcached_result_st result
;
2762 memcached_result_st
*result_ptr
;
2764 result_ptr
= memcached_result_create(memc
, &result
);
2765 assert(result
.is_allocated
== false);
2767 memcached_result_free(&result
);
2769 return TEST_SUCCESS
;
2772 static test_return_t
result_alloc(memcached_st
*memc
)
2774 memcached_result_st
*result
;
2776 result
= memcached_result_create(memc
, NULL
);
2778 memcached_result_free(result
);
2780 return TEST_SUCCESS
;
2783 static test_return_t
string_static_null(memcached_st
*memc
)
2785 memcached_string_st string
;
2786 memcached_string_st
*string_ptr
;
2788 string_ptr
= memcached_string_create(memc
, &string
, 0);
2789 assert(string
.is_allocated
== false);
2791 memcached_string_free(&string
);
2793 return TEST_SUCCESS
;
2796 static test_return_t
string_alloc_null(memcached_st
*memc
)
2798 memcached_string_st
*string
;
2800 string
= memcached_string_create(memc
, NULL
, 0);
2802 memcached_string_free(string
);
2804 return TEST_SUCCESS
;
2807 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
2809 memcached_string_st
*string
;
2811 string
= memcached_string_create(memc
, NULL
, 1024);
2813 memcached_string_free(string
);
2815 return TEST_SUCCESS
;
2818 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
2820 memcached_string_st
*string
;
2822 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2823 assert(string
== NULL
);
2825 return TEST_SUCCESS
;
2828 static test_return_t
string_alloc_append(memcached_st
*memc
)
2831 char buffer
[SMALL_STRING_LEN
];
2832 memcached_string_st
*string
;
2834 /* Ring the bell! */
2835 memset(buffer
, 6, SMALL_STRING_LEN
);
2837 string
= memcached_string_create(memc
, NULL
, 100);
2840 for (x
= 0; x
< 1024; x
++)
2842 memcached_return rc
;
2843 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2844 assert(rc
== MEMCACHED_SUCCESS
);
2846 memcached_string_free(string
);
2848 return TEST_SUCCESS
;
2851 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
2853 memcached_return rc
;
2855 char buffer
[SMALL_STRING_LEN
];
2856 memcached_string_st
*string
;
2858 /* Ring the bell! */
2859 memset(buffer
, 6, SMALL_STRING_LEN
);
2861 string
= memcached_string_create(memc
, NULL
, 100);
2864 for (x
= 0; x
< 1024; x
++)
2866 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2867 assert(rc
== MEMCACHED_SUCCESS
);
2869 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
2870 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2871 memcached_string_free(string
);
2873 return TEST_SUCCESS
;
2876 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2878 pairs_free(global_pairs
);
2880 return TEST_SUCCESS
;
2883 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2885 unsigned long long x
;
2886 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2887 global_count
= GLOBAL_COUNT
;
2889 for (x
= 0; x
< global_count
; x
++)
2891 global_keys
[x
]= global_pairs
[x
].key
;
2892 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2895 return TEST_SUCCESS
;
2898 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2900 unsigned long long x
;
2901 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2902 global_count
= GLOBAL2_COUNT
;
2904 for (x
= 0; x
< global_count
; x
++)
2906 global_keys
[x
]= global_pairs
[x
].key
;
2907 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2910 return TEST_SUCCESS
;
2913 static test_return_t
generate_data(memcached_st
*memc
)
2915 execute_set(memc
, global_pairs
, global_count
);
2917 return TEST_SUCCESS
;
2920 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
2922 memcached_stat_st
*stat_p
;
2923 memcached_return rc
;
2924 uint32_t host_index
= 0;
2925 execute_set(memc
, global_pairs
, global_count
);
2927 //TODO: hosts used size stats
2928 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2931 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2933 /* This test was changes so that "make test" would work properlly */
2935 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
);
2937 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
2940 memcached_stat_free(NULL
, stat_p
);
2942 return TEST_SUCCESS
;
2944 static test_return_t
generate_buffer_data(memcached_st
*memc
)
2949 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2950 generate_data(memc
);
2952 return TEST_SUCCESS
;
2955 static test_return_t
get_read_count(memcached_st
*memc
)
2958 memcached_return rc
;
2959 memcached_st
*memc_clone
;
2961 memc_clone
= memcached_clone(NULL
, memc
);
2964 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
2968 size_t return_value_length
;
2972 for (x
= count
= 0; x
< global_count
; x
++)
2974 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
2975 &return_value_length
, &flags
, &rc
);
2976 if (rc
== MEMCACHED_SUCCESS
)
2985 memcached_free(memc_clone
);
2987 return TEST_SUCCESS
;
2990 static test_return_t
get_read(memcached_st
*memc
)
2993 memcached_return rc
;
2997 size_t return_value_length
;
3000 for (x
= 0; x
< global_count
; x
++)
3002 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3003 &return_value_length
, &flags
, &rc
);
3005 assert(return_value);
3006 assert(rc == MEMCACHED_SUCCESS);
3008 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3013 return TEST_SUCCESS
;
3016 static test_return_t
mget_read(memcached_st
*memc
)
3018 memcached_return rc
;
3020 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3021 assert(rc
== MEMCACHED_SUCCESS
);
3022 assert(fetch_all_results(memc
) == TEST_SUCCESS
);
3024 return TEST_SUCCESS
;
3027 static test_return_t
mget_read_result(memcached_st
*memc
)
3029 memcached_return rc
;
3031 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3032 assert(rc
== MEMCACHED_SUCCESS
);
3033 /* Turn this into a help function */
3035 memcached_result_st results_obj
;
3036 memcached_result_st
*results
;
3038 results
= memcached_result_create(memc
, &results_obj
);
3040 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3043 assert(rc
== MEMCACHED_SUCCESS
);
3046 memcached_result_free(&results_obj
);
3049 return TEST_SUCCESS
;
3052 static test_return_t
mget_read_function(memcached_st
*memc
)
3054 memcached_return rc
;
3055 unsigned int counter
;
3056 memcached_execute_function callbacks
[1];
3058 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3059 assert(rc
== MEMCACHED_SUCCESS
);
3061 callbacks
[0]= &callback_counter
;
3063 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3065 return TEST_SUCCESS
;
3068 static test_return_t
delete_generate(memcached_st
*memc
)
3072 for (x
= 0; x
< global_count
; x
++)
3074 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3077 return TEST_SUCCESS
;
3080 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3086 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3088 for (x
= 0; x
< global_count
; x
++)
3090 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3093 return TEST_SUCCESS
;
3096 static test_return_t
add_host_test1(memcached_st
*memc
)
3099 memcached_return rc
;
3100 char servername
[]= "0.example.com";
3101 memcached_server_st
*servers
;
3103 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3105 assert(1 == memcached_server_list_count(servers
));
3107 for (x
= 2; x
< 20; x
++)
3109 char buffer
[SMALL_STRING_LEN
];
3111 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
3112 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3114 assert(rc
== MEMCACHED_SUCCESS
);
3115 assert(x
== memcached_server_list_count(servers
));
3118 rc
= memcached_server_push(memc
, servers
);
3119 assert(rc
== MEMCACHED_SUCCESS
);
3120 rc
= memcached_server_push(memc
, servers
);
3121 assert(rc
== MEMCACHED_SUCCESS
);
3123 memcached_server_list_free(servers
);
3125 return TEST_SUCCESS
;
3128 static memcached_return
pre_nonblock(memcached_st
*memc
)
3130 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3132 return MEMCACHED_SUCCESS
;
3135 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
3137 memcached_return rc
= MEMCACHED_FAILURE
;
3138 memcached_st
*memc_clone
;
3140 memc_clone
= memcached_clone(NULL
, memc
);
3142 // The memcached_version needs to be done on a clone, because the server
3143 // will not toggle protocol on an connection.
3144 memcached_version(memc_clone
);
3146 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3148 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3149 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3150 assert(rc
== MEMCACHED_SUCCESS
);
3151 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3154 memcached_free(memc_clone
);
3158 static memcached_return
pre_murmur(memcached_st
*memc
)
3160 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3162 return MEMCACHED_SUCCESS
;
3165 static memcached_return
pre_jenkins(memcached_st
*memc
)
3167 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3169 return MEMCACHED_SUCCESS
;
3173 static memcached_return
pre_md5(memcached_st
*memc
)
3175 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3177 return MEMCACHED_SUCCESS
;
3180 static memcached_return
pre_crc(memcached_st
*memc
)
3182 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3184 return MEMCACHED_SUCCESS
;
3187 static memcached_return
pre_hsieh(memcached_st
*memc
)
3189 #ifdef HAVE_HSIEH_HASH
3190 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3191 return MEMCACHED_SUCCESS
;
3194 return MEMCACHED_FAILURE
;
3198 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
3200 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
3202 return MEMCACHED_SUCCESS
;
3205 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
3207 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3209 return MEMCACHED_SUCCESS
;
3212 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
3214 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3216 return MEMCACHED_SUCCESS
;
3219 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
3221 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3223 return MEMCACHED_SUCCESS
;
3226 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3228 memcached_return rc
;
3231 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3232 assert(rc
== MEMCACHED_SUCCESS
);
3234 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3237 return MEMCACHED_SUCCESS
;
3240 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3242 memcached_return rc
;
3245 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3246 assert(rc
== MEMCACHED_SUCCESS
);
3248 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3251 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3252 assert(rc
== MEMCACHED_SUCCESS
);
3254 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3255 assert(value
== MEMCACHED_HASH_MD5
);
3256 return MEMCACHED_SUCCESS
;
3259 static memcached_return
pre_binary(memcached_st
*memc
)
3261 memcached_return rc
= MEMCACHED_FAILURE
;
3262 memcached_st
*memc_clone
;
3264 memc_clone
= memcached_clone(NULL
, memc
);
3266 // The memcached_version needs to be done on a clone, because the server
3267 // will not toggle protocol on an connection.
3268 memcached_version(memc_clone
);
3270 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3272 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3273 assert(rc
== MEMCACHED_SUCCESS
);
3274 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3277 memcached_free(memc_clone
);
3282 static memcached_return
pre_replication(memcached_st
*memc
)
3284 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
3285 return MEMCACHED_FAILURE
;
3288 * Make sure that we store the item on all servers
3289 * (master + replicas == number of servers)
3291 memcached_return rc
;
3292 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3293 memc
->number_of_hosts
- 1);
3294 assert(rc
== MEMCACHED_SUCCESS
);
3295 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3300 static memcached_return
pre_replication_noblock(memcached_st
*memc
)
3302 memcached_return rc
= MEMCACHED_FAILURE
;
3303 if (pre_replication(memc
) == MEMCACHED_SUCCESS
&&
3304 pre_nonblock(memc
) == MEMCACHED_SUCCESS
)
3305 rc
= MEMCACHED_SUCCESS
;
3310 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3315 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3317 void *ret
= malloc(size
);
3319 memset(ret
, 0xff, size
);
3324 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3326 return realloc(mem
, size
);
3329 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3331 return calloc(nelem
, size
);
3334 static memcached_return
set_prefix(memcached_st
*memc
)
3336 memcached_return rc
;
3337 const char *key
= "mine";
3340 /* Make sure be default none exists */
3341 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3342 assert(rc
== MEMCACHED_FAILURE
);
3344 /* Test a clean set */
3345 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3346 assert(rc
== MEMCACHED_SUCCESS
);
3348 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3349 assert(memcmp(value
, key
, 4) == 0);
3350 assert(rc
== MEMCACHED_SUCCESS
);
3352 /* Test that we can turn it off */
3353 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3354 assert(rc
== MEMCACHED_SUCCESS
);
3356 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3357 assert(rc
== MEMCACHED_FAILURE
);
3359 /* Now setup for main test */
3360 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3361 assert(rc
== MEMCACHED_SUCCESS
);
3363 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3364 assert(rc
== MEMCACHED_SUCCESS
);
3365 assert(memcmp(value
, key
, 4) == 0);
3367 /* Set to Zero, and then Set to something too large */
3370 memset(long_key
, 0, 255);
3372 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3373 assert(rc
== MEMCACHED_SUCCESS
);
3375 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3376 assert(rc
== MEMCACHED_FAILURE
);
3377 assert(value
== NULL
);
3379 /* Test a long key for failure */
3380 /* TODO, extend test to determine based on setting, what result should be */
3381 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3382 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3383 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3384 assert(rc
== MEMCACHED_SUCCESS
);
3386 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3387 strcpy(long_key
, "This is more then the allotted number of characters");
3388 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3389 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3391 /* Test for a bad prefix, but with a short key */
3392 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3393 assert(rc
== MEMCACHED_SUCCESS
);
3395 strcpy(long_key
, "dog cat");
3396 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3397 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3400 return MEMCACHED_SUCCESS
;
3403 #ifdef MEMCACHED_ENABLE_DEPRECATED
3404 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3406 void *test_ptr
= NULL
;
3409 memcached_malloc_function malloc_cb
=
3410 (memcached_malloc_function
)my_malloc
;
3411 cb_ptr
= *(void **)&malloc_cb
;
3412 memcached_return rc
;
3414 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3415 assert(rc
== MEMCACHED_SUCCESS
);
3416 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3417 assert(rc
== MEMCACHED_SUCCESS
);
3418 assert(test_ptr
== cb_ptr
);
3422 memcached_realloc_function realloc_cb
=
3423 (memcached_realloc_function
)my_realloc
;
3424 cb_ptr
= *(void **)&realloc_cb
;
3425 memcached_return rc
;
3427 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3428 assert(rc
== MEMCACHED_SUCCESS
);
3429 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3430 assert(rc
== MEMCACHED_SUCCESS
);
3431 assert(test_ptr
== cb_ptr
);
3435 memcached_free_function free_cb
=
3436 (memcached_free_function
)my_free
;
3437 cb_ptr
= *(void **)&free_cb
;
3438 memcached_return rc
;
3440 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3441 assert(rc
== MEMCACHED_SUCCESS
);
3442 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3443 assert(rc
== MEMCACHED_SUCCESS
);
3444 assert(test_ptr
== cb_ptr
);
3446 return MEMCACHED_SUCCESS
;
3450 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3452 memcached_return rc
;
3453 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3454 my_realloc
, my_calloc
);
3455 assert(rc
== MEMCACHED_FAILURE
);
3457 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3458 my_realloc
, my_calloc
);
3460 memcached_malloc_function mem_malloc
;
3461 memcached_free_function mem_free
;
3462 memcached_realloc_function mem_realloc
;
3463 memcached_calloc_function mem_calloc
;
3464 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3465 &mem_realloc
, &mem_calloc
);
3467 assert(mem_malloc
== my_malloc
);
3468 assert(mem_realloc
== my_realloc
);
3469 assert(mem_calloc
== my_calloc
);
3470 assert(mem_free
== my_free
);
3472 return MEMCACHED_SUCCESS
;
3475 static memcached_return
enable_consistent(memcached_st
*memc
)
3477 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3478 memcached_hash hash
;
3479 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3480 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3481 return MEMCACHED_FAILURE
;
3483 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3484 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3486 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3487 assert(hash
== MEMCACHED_HASH_HSIEH
);
3490 return MEMCACHED_SUCCESS
;
3493 static memcached_return
enable_cas(memcached_st
*memc
)
3495 unsigned int set
= 1;
3497 memcached_version(memc
);
3499 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3500 || memc
->hosts
[0].minor_version
> 2)
3502 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3504 return MEMCACHED_SUCCESS
;
3507 return MEMCACHED_FAILURE
;
3510 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3512 memcached_version(memc
);
3514 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3515 || memc
->hosts
[0].minor_version
> 2)
3516 return MEMCACHED_SUCCESS
;
3518 return MEMCACHED_FAILURE
;
3521 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3523 memcached_return rc
;
3526 memcached_server_list_free(memc
->hosts
);
3528 memc
->number_of_hosts
= 0;
3530 if (stat("/tmp/memcached.socket", &buf
))
3531 return MEMCACHED_FAILURE
;
3533 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3538 static memcached_return
pre_nodelay(memcached_st
*memc
)
3540 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3541 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3543 return MEMCACHED_SUCCESS
;
3546 static memcached_return
pre_settimer(memcached_st
*memc
)
3548 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3549 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3551 return MEMCACHED_SUCCESS
;
3554 static memcached_return
poll_timeout(memcached_st
*memc
)
3560 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3562 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3564 assert(timeout
== 100);
3566 return MEMCACHED_SUCCESS
;
3569 static test_return_t
noreply_test(memcached_st
*memc
)
3571 memcached_return ret
;
3572 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3573 assert(ret
== MEMCACHED_SUCCESS
);
3574 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3575 assert(ret
== MEMCACHED_SUCCESS
);
3576 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3577 assert(ret
== MEMCACHED_SUCCESS
);
3578 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3579 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3580 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3582 for (int count
=0; count
< 5; ++count
)
3584 for (int x
=0; x
< 100; ++x
)
3587 size_t len
= (size_t)sprintf(key
, "%d", x
);
3591 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3594 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3597 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3600 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3603 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3609 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3613 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3614 ** API and is _ONLY_ done this way to verify that the library works the
3615 ** way it is supposed to do!!!!
3618 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3619 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3621 assert(no_msg
== 0);
3622 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3625 ** Now validate that all items was set properly!
3627 for (int x
=0; x
< 100; ++x
)
3630 size_t len
= (size_t)sprintf(key
, "%d", x
);
3633 char* value
=memcached_get(memc
, key
, strlen(key
),
3634 &length
, &flags
, &ret
);
3635 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3638 case 0: /* FALLTHROUGH */
3639 case 1: /* FALLTHROUGH */
3641 assert(strncmp(value
, key
, len
) == 0);
3642 assert(len
== length
);
3645 assert(length
== len
* 2);
3648 assert(length
== len
* 3);
3658 /* Try setting an illegal cas value (should not return an error to
3659 * the caller (because we don't expect a return message from the server)
3661 const char* keys
[]= {"0"};
3662 size_t lengths
[]= {1};
3665 memcached_result_st results_obj
;
3666 memcached_result_st
*results
;
3667 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3668 assert(ret
== MEMCACHED_SUCCESS
);
3670 results
= memcached_result_create(memc
, &results_obj
);
3672 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3674 assert(ret
== MEMCACHED_SUCCESS
);
3675 uint64_t cas
= memcached_result_cas(results
);
3676 memcached_result_free(&results_obj
);
3678 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3679 assert(ret
== MEMCACHED_SUCCESS
);
3682 * The item will have a new cas value, so try to set it again with the old
3683 * value. This should fail!
3685 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3686 assert(ret
== MEMCACHED_SUCCESS
);
3687 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3688 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3689 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3692 return TEST_SUCCESS
;
3695 static test_return_t
analyzer_test(memcached_st
*memc
)
3697 memcached_return rc
;
3698 memcached_stat_st
*memc_stat
;
3699 memcached_analysis_st
*report
;
3701 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3702 assert(rc
== MEMCACHED_SUCCESS
);
3705 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3706 assert(rc
== MEMCACHED_SUCCESS
);
3710 memcached_stat_free(NULL
, memc_stat
);
3712 return TEST_SUCCESS
;
3715 /* Count the objects */
3716 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3717 const char *key
__attribute__((unused
)),
3718 size_t key_length
__attribute__((unused
)),
3721 uint32_t *counter
= (uint32_t *)context
;
3723 *counter
= *counter
+ 1;
3725 return MEMCACHED_SUCCESS
;
3728 static test_return_t
dump_test(memcached_st
*memc
)
3730 memcached_return rc
;
3731 uint32_t counter
= 0;
3732 memcached_dump_func callbacks
[1];
3733 test_return_t main_rc
;
3735 callbacks
[0]= &callback_dump_counter
;
3737 /* No support for Binary protocol yet */
3738 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3739 return TEST_SUCCESS
;
3741 main_rc
= set_test3(memc
);
3743 assert (main_rc
== TEST_SUCCESS
);
3745 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3746 assert(rc
== MEMCACHED_SUCCESS
);
3748 /* We may have more then 32 if our previous flush has not completed */
3749 assert(counter
>= 32);
3751 return TEST_SUCCESS
;
3754 #ifdef HAVE_LIBMEMCACHEDUTIL
3755 static void* connection_release(void *arg
) {
3757 memcached_pool_st
* pool
;
3762 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3766 static test_return_t
connection_pool_test(memcached_st
*memc
)
3768 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3769 assert(pool
!= NULL
);
3770 memcached_st
* mmc
[10];
3771 memcached_return rc
;
3773 for (int x
= 0; x
< 10; ++x
) {
3774 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3775 assert(mmc
[x
] != NULL
);
3776 assert(rc
== MEMCACHED_SUCCESS
);
3779 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3780 assert(rc
== MEMCACHED_SUCCESS
);
3784 memcached_pool_st
* pool
;
3786 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3787 pthread_create(&tid
, NULL
, connection_release
, &item
);
3788 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3789 assert(rc
== MEMCACHED_SUCCESS
);
3790 pthread_join(tid
, NULL
);
3791 assert(mmc
[9] == item
.mmc
);
3792 const char *key
= "key";
3793 size_t keylen
= strlen(key
);
3795 // verify that I can do ops with all connections
3796 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3797 assert(rc
== MEMCACHED_SUCCESS
);
3799 for (unsigned int x
= 0; x
< 10; ++x
) {
3800 uint64_t number_value
;
3801 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3802 assert(rc
== MEMCACHED_SUCCESS
);
3803 assert(number_value
== (x
+1));
3807 for (int x
= 0; x
< 10; ++x
)
3808 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3811 /* verify that I can set behaviors on the pool when I don't have all
3812 * of the connections in the pool. It should however be enabled
3813 * when I push the item into the pool
3815 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
3816 assert(mmc
[0] != NULL
);
3818 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
3819 assert(rc
== MEMCACHED_SUCCESS
);
3821 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
3822 assert(mmc
[1] != NULL
);
3824 assert(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
3825 assert(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
3826 assert(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
3828 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
3829 assert(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
3830 assert(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
3833 assert(memcached_pool_destroy(pool
) == memc
);
3834 return TEST_SUCCESS
;
3838 static test_return_t
replication_set_test(memcached_st
*memc
)
3840 memcached_return rc
;
3841 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3842 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3844 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3845 assert(rc
== MEMCACHED_SUCCESS
);
3848 ** We are using the quiet commands to store the replicas, so we need
3849 ** to ensure that all of them are processed before we can continue.
3850 ** In the test we go directly from storing the object to trying to
3851 ** receive the object from all of the different servers, so we
3852 ** could end up in a race condition (the memcached server hasn't yet
3853 ** processed the quiet command from the replication set when it process
3854 ** the request from the other client (created by the clone)). As a
3855 ** workaround for that we call memcached_quit to send the quit command
3856 ** to the server and wait for the response ;-) If you use the test code
3857 ** as an example for your own code, please note that you shouldn't need
3860 memcached_quit(memc
);
3863 ** "bubba" should now be stored on all of our servers. We don't have an
3864 ** easy to use API to address each individual server, so I'll just iterate
3865 ** through a bunch of "master keys" and I should most likely hit all of the
3868 for (int x
= 'a'; x
<= 'z'; ++x
)
3870 char key
[2]= { [0]= (char)x
};
3873 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3875 assert(rc
== MEMCACHED_SUCCESS
);
3876 assert(val
!= NULL
);
3880 memcached_free(memc_clone
);
3882 return TEST_SUCCESS
;
3885 static test_return_t
replication_get_test(memcached_st
*memc
)
3887 memcached_return rc
;
3890 * Don't do the following in your code. I am abusing the internal details
3891 * within the library, and this is not a supported interface.
3892 * This is to verify correct behavior in the library
3894 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
3896 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3897 memc_clone
->hosts
[host
].port
= 0;
3899 for (int x
= 'a'; x
<= 'z'; ++x
)
3901 char key
[2]= { [0]= (char)x
};
3904 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3906 assert(rc
== MEMCACHED_SUCCESS
);
3907 assert(val
!= NULL
);
3911 memcached_free(memc_clone
);
3914 return TEST_SUCCESS
;
3917 static test_return_t
replication_mget_test(memcached_st
*memc
)
3919 memcached_return rc
;
3920 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3921 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3923 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3924 size_t len
[]= { 5, 4, 4, 4 };
3926 for (int x
=0; x
< 4; ++x
)
3928 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3929 assert(rc
== MEMCACHED_SUCCESS
);
3933 ** We are using the quiet commands to store the replicas, so we need
3934 ** to ensure that all of them are processed before we can continue.
3935 ** In the test we go directly from storing the object to trying to
3936 ** receive the object from all of the different servers, so we
3937 ** could end up in a race condition (the memcached server hasn't yet
3938 ** processed the quiet command from the replication set when it process
3939 ** the request from the other client (created by the clone)). As a
3940 ** workaround for that we call memcached_quit to send the quit command
3941 ** to the server and wait for the response ;-) If you use the test code
3942 ** as an example for your own code, please note that you shouldn't need
3945 memcached_quit(memc
);
3948 * Don't do the following in your code. I am abusing the internal details
3949 * within the library, and this is not a supported interface.
3950 * This is to verify correct behavior in the library
3952 memcached_result_st result_obj
;
3953 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
3955 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
3956 new_clone
->hosts
[host
].port
= 0;
3958 for (int x
= 'a'; x
<= 'z'; ++x
)
3960 const char key
[2]= { [0]= (const char)x
};
3962 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
3963 assert(rc
== MEMCACHED_SUCCESS
);
3965 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
3969 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
3974 memcached_result_free(&result_obj
);
3977 memcached_free(new_clone
);
3980 memcached_free(memc_clone
);
3982 return TEST_SUCCESS
;
3985 static test_return_t
replication_delete_test(memcached_st
*memc
)
3987 memcached_return rc
;
3988 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3989 /* Delete the items from all of the servers except 1 */
3990 uint64_t repl
= memcached_behavior_get(memc
,
3991 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3992 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3994 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3995 size_t len
[]= { 5, 4, 4, 4 };
3997 for (int x
=0; x
< 4; ++x
)
3999 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4000 assert(rc
== MEMCACHED_SUCCESS
);
4004 * Don't do the following in your code. I am abusing the internal details
4005 * within the library, and this is not a supported interface.
4006 * This is to verify correct behavior in the library
4008 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4009 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4011 memc_clone
->hosts
[hash
].port
= 0;
4012 if (++hash
== memc_clone
->number_of_hosts
)
4016 memcached_result_st result_obj
;
4017 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4019 for (int x
= 'a'; x
<= 'z'; ++x
)
4021 const char key
[2]= { [0]= (const char)x
};
4023 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4024 assert(rc
== MEMCACHED_SUCCESS
);
4026 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4030 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4035 memcached_result_free(&result_obj
);
4038 memcached_free(memc_clone
);
4040 return TEST_SUCCESS
;
4043 static void increment_request_id(uint16_t *id
)
4046 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
4050 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
4052 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
4053 assert(ids
!= NULL
);
4056 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4057 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
4062 static test_return_t
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
4065 memcached_server_st
*cur_server
= memc
->hosts
;
4066 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
4068 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4070 assert(cur_server
[x
].cursor_active
== 0);
4071 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
4073 free(expected_req_ids
);
4076 return TEST_SUCCESS
;
4080 ** There is a little bit of a hack here, instead of removing
4081 ** the servers, I just set num host to 0 and them add then new udp servers
4083 static memcached_return
init_udp(memcached_st
*memc
)
4085 memcached_version(memc
);
4086 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
4087 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
4088 || memc
->hosts
[0].micro_version
< 6)
4089 return MEMCACHED_FAILURE
;
4091 uint32_t num_hosts
= memc
->number_of_hosts
;
4093 memcached_server_st servers
[num_hosts
];
4094 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
4095 for (x
= 0; x
< num_hosts
; x
++)
4096 memcached_server_free(&memc
->hosts
[x
]);
4098 memc
->number_of_hosts
= 0;
4099 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
4100 for (x
= 0; x
< num_hosts
; x
++)
4102 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
4103 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4106 return MEMCACHED_SUCCESS
;
4109 static memcached_return
binary_init_udp(memcached_st
*memc
)
4112 return init_udp(memc
);
4115 /* Make sure that I cant add a tcp server to a udp client */
4116 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
4118 memcached_server_st server
;
4119 memcached_server_clone(&server
, &memc
->hosts
[0]);
4120 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4121 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4122 return TEST_SUCCESS
;
4125 /* Make sure that I cant add a udp server to a tcp client */
4126 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
4128 memcached_server_st server
;
4129 memcached_server_clone(&server
, &memc
->hosts
[0]);
4130 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4132 memcached_st tcp_client
;
4133 memcached_create(&tcp_client
);
4134 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4135 return TEST_SUCCESS
;
4138 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
4141 memcached_quit(memc
);
4142 memc
->number_of_hosts
= 0;
4143 run_distribution(memc
);
4144 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
4145 assert(memc
->flags
& MEM_USE_UDP
);
4146 assert(memc
->flags
& MEM_NOREPLY
);;
4148 assert(memc
->number_of_hosts
== 0);
4150 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
4151 assert(!(memc
->flags
& MEM_USE_UDP
));
4152 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
4153 assert(!(memc
->flags
& MEM_NOREPLY
));
4154 return TEST_SUCCESS
;
4157 static test_return_t
udp_set_test(memcached_st
*memc
)
4160 unsigned int num_iters
= 1025; //request id rolls over at 1024
4161 for (x
= 0; x
< num_iters
;x
++)
4163 memcached_return rc
;
4164 const char *key
= "foo";
4165 const char *value
= "when we sanitize";
4166 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4167 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
4168 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4169 rc
= memcached_set(memc
, key
, strlen(key
),
4170 value
, strlen(value
),
4171 (time_t)0, (uint32_t)0);
4172 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4173 /** NB, the check below assumes that if new write_ptr is less than
4174 * the original write_ptr that we have flushed. For large payloads, this
4175 * maybe an invalid assumption, but for the small payload we have it is OK
4177 if (rc
== MEMCACHED_SUCCESS
||
4178 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4179 increment_request_id(&expected_ids
[server_key
]);
4181 if (rc
== MEMCACHED_SUCCESS
)
4183 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4187 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4188 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4190 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4192 return TEST_SUCCESS
;
4195 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
4197 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4198 return udp_set_test(memc
);
4201 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
4203 memcached_return rc
;
4204 const char *key
= "bar";
4205 char value
[MAX_UDP_DATAGRAM_LENGTH
];
4206 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4207 rc
= memcached_set(memc
, key
, strlen(key
),
4208 value
, MAX_UDP_DATAGRAM_LENGTH
,
4209 (time_t)0, (uint32_t)0);
4210 assert(rc
== MEMCACHED_WRITE_FAILURE
);
4211 return post_udp_op_check(memc
,expected_ids
);
4214 static test_return_t
udp_delete_test(memcached_st
*memc
)
4217 unsigned int num_iters
= 1025; //request id rolls over at 1024
4218 for (x
= 0; x
< num_iters
;x
++)
4220 memcached_return rc
;
4221 const char *key
= "foo";
4222 uint16_t *expected_ids
=get_udp_request_ids(memc
);
4223 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4224 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4225 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
4226 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4227 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4228 increment_request_id(&expected_ids
[server_key
]);
4229 if (rc
== MEMCACHED_SUCCESS
)
4230 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4233 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4234 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4236 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4238 return TEST_SUCCESS
;
4241 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
4243 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4244 return udp_delete_test(memc
);
4247 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
4249 memcached_return rc
;
4250 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4252 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4253 increment_request_id(&expected_ids
[x
]);
4255 rc
= memcached_verbosity(memc
,3);
4256 assert(rc
== MEMCACHED_SUCCESS
);
4257 return post_udp_op_check(memc
,expected_ids
);
4260 static test_return_t
udp_quit_test(memcached_st
*memc
)
4262 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4263 memcached_quit(memc
);
4264 return post_udp_op_check(memc
, expected_ids
);
4267 static test_return_t
udp_flush_test(memcached_st
*memc
)
4269 memcached_return rc
;
4270 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4272 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4273 increment_request_id(&expected_ids
[x
]);
4275 rc
= memcached_flush(memc
,0);
4276 assert(rc
== MEMCACHED_SUCCESS
);
4277 return post_udp_op_check(memc
,expected_ids
);
4280 static test_return_t
udp_incr_test(memcached_st
*memc
)
4282 memcached_return rc
;
4283 const char *key
= "incr";
4284 const char *value
= "1";
4285 rc
= memcached_set(memc
, key
, strlen(key
),
4286 value
, strlen(value
),
4287 (time_t)0, (uint32_t)0);
4289 assert(rc
== MEMCACHED_SUCCESS
);
4290 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4291 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4292 increment_request_id(&expected_ids
[server_key
]);
4294 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4295 assert(rc
== MEMCACHED_SUCCESS
);
4296 return post_udp_op_check(memc
, expected_ids
);
4299 static test_return_t
udp_decr_test(memcached_st
*memc
)
4301 memcached_return rc
;
4302 const char *key
= "decr";
4303 const char *value
= "1";
4304 rc
= memcached_set(memc
, key
, strlen(key
),
4305 value
, strlen(value
),
4306 (time_t)0, (uint32_t)0);
4308 assert(rc
== MEMCACHED_SUCCESS
);
4309 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4310 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4311 increment_request_id(&expected_ids
[server_key
]);
4313 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4314 assert(rc
== MEMCACHED_SUCCESS
);
4315 return post_udp_op_check(memc
, expected_ids
);
4319 static test_return_t
udp_stat_test(memcached_st
*memc
)
4321 memcached_stat_st
* rv
= NULL
;
4322 memcached_return rc
;
4324 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4325 rv
= memcached_stat(memc
, args
, &rc
);
4327 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4328 return post_udp_op_check(memc
, expected_ids
);
4331 static test_return_t
udp_version_test(memcached_st
*memc
)
4333 memcached_return rc
;
4334 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4335 rc
= memcached_version(memc
);
4336 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4337 return post_udp_op_check(memc
, expected_ids
);
4340 static test_return_t
udp_get_test(memcached_st
*memc
)
4342 memcached_return rc
;
4343 const char *key
= "foo";
4345 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4346 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4347 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4348 assert(val
== NULL
);
4349 return post_udp_op_check(memc
, expected_ids
);
4352 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
4355 test_st mixed_io_ops
[] ={
4356 {"udp_set_test", 0, udp_set_test
},
4357 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4358 {"udp_delete_test", 0, udp_delete_test
},
4359 {"udp_verbosity_test", 0, udp_verbosity_test
},
4360 {"udp_quit_test", 0, udp_quit_test
},
4361 {"udp_flush_test", 0, udp_flush_test
},
4362 {"udp_incr_test", 0, udp_incr_test
},
4363 {"udp_decr_test", 0, udp_decr_test
},
4364 {"udp_version_test", 0, udp_version_test
}
4367 for (x
= 0; x
< 500; x
++)
4369 current_op
= mixed_io_ops
[random() % 9];
4370 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4372 return TEST_SUCCESS
;
4375 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4377 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4378 #ifdef HAVE_HSIEH_HASH
4379 expected_rc
= MEMCACHED_SUCCESS
;
4381 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4382 (uint64_t)MEMCACHED_HASH_HSIEH
);
4383 assert(rc
== expected_rc
);
4384 return TEST_SUCCESS
;
4387 static const char *list
[]=
4417 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4421 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4422 245758794U, 2550894432U, 121710495U, 3053817768U,
4423 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4424 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4425 2138080750U, 217675895U, 2230934345U, 1234361223U,
4426 3968582726U, 2455685270U, 1293568479U, 199067604U,
4430 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4434 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4435 assert(values
[x
] == hash_val
);
4438 return TEST_SUCCESS
;
4441 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4445 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4446 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4447 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4448 17300U, 28156U, 9192U, 20484U, 16925U };
4450 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4454 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4455 assert(values
[x
] == hash_val
);
4458 return TEST_SUCCESS
;
4461 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4465 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4466 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4467 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4468 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4469 2815549194U, 2562818319U, 224996066U, 2680194749U,
4470 3035305390U, 246890365U, 2395624193U, 4145193337U,
4473 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4477 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4478 assert(values
[x
] == hash_val
);
4481 return TEST_SUCCESS
;
4484 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4488 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4489 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4490 734810122U, 1516407546U, 560948863U, 1767346780U,
4491 561034892U, 4156330026U, 3716417003U, 3475297030U,
4492 1518272172U, 227211583U, 3938128828U, 126112909U,
4493 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4496 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4500 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4501 assert(values
[x
] == hash_val
);
4504 return TEST_SUCCESS
;
4507 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4511 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4512 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4513 344481048U, 3865235296U, 2181839183U, 119581266U,
4514 510234242U, 4248244304U, 1362796839U, 103389328U,
4515 1449620010U, 182962511U, 3554262370U, 3206747549U,
4516 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4520 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4524 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4525 assert(values
[x
] == hash_val
);
4528 return TEST_SUCCESS
;
4531 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4535 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4536 459261019U, 3521085446U, 18738364U, 1625305005U,
4537 2162232970U, 777243802U, 3323728671U, 132336572U,
4538 3654473228U, 260679466U, 1169454059U, 2698319462U,
4539 1062177260U, 235516991U, 2218399068U, 405302637U,
4540 1128467232U, 3579622413U, 2138539289U, 96429129U,
4543 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4547 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4548 assert(values
[x
] == hash_val
);
4551 return TEST_SUCCESS
;
4554 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4558 #ifdef HAVE_HSIEH_HASH
4559 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4560 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4561 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4562 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4565 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 };
4568 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4572 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4573 assert(values
[x
] == hash_val
);
4576 return TEST_SUCCESS
;
4579 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4583 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4584 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4585 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4586 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4587 2815549194U, 2562818319U, 224996066U, 2680194749U,
4588 3035305390U, 246890365U, 2395624193U, 4145193337U,
4591 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4595 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4596 assert(values
[x
] == hash_val
);
4599 return TEST_SUCCESS
;
4602 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4606 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4607 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4608 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4609 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4610 4153263658U, 3824871822U, 3072759809U, 798622255U,
4611 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4615 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4619 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4620 assert(values
[x
] == hash_val
);
4623 return TEST_SUCCESS
;
4626 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4628 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
4629 return TEST_SKIPPED
;
4631 memcached_return ret
;
4632 const char *key
= "regression_bug_434484";
4633 size_t keylen
= strlen(key
);
4635 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4636 assert(ret
== MEMCACHED_NOTSTORED
);
4638 size_t size
= 2048 * 1024;
4639 void *data
= calloc(1, size
);
4640 assert(data
!= NULL
);
4641 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
4642 assert(ret
== MEMCACHED_E2BIG
);
4645 return TEST_SUCCESS
;
4648 static test_return_t
regression_bug_434843(memcached_st
*memc
)
4650 if (pre_binary(memc
) != MEMCACHED_SUCCESS
)
4651 return TEST_SKIPPED
;
4653 memcached_return rc
;
4654 unsigned int counter
= 0;
4655 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
4658 * I only want to hit only _one_ server so I know the number of requests I'm
4659 * sending in the pipleine to the server. Let's try to do a multiget of
4660 * 1024 (that should satisfy most users don't you think?). Future versions
4661 * will include a mget_execute function call if you need a higher number.
4663 uint32_t number_of_hosts
= memc
->number_of_hosts
;
4664 memc
->number_of_hosts
= 1;
4665 const size_t max_keys
= 1024;
4666 char **keys
= calloc(max_keys
, sizeof(char*));
4667 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
4669 for (int x
= 0; x
< (int)max_keys
; ++x
)
4672 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
4674 assert(keys
[x
] != NULL
);
4678 * Run two times.. the first time we should have 100% cache miss,
4679 * and the second time we should have 100% cache hits
4681 for (int y
= 0; y
< 2; ++y
)
4683 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
4684 assert(rc
== MEMCACHED_SUCCESS
);
4685 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
4688 /* The first iteration should give me a 100% cache miss. verify that*/
4689 assert(counter
== 0);
4690 char blob
[1024]= { 0 };
4691 for (int x
= 0; x
< (int)max_keys
; ++x
)
4693 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
4694 blob
, sizeof(blob
), 0, 0);
4695 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4700 /* Verify that we received all of the key/value pairs */
4701 assert(counter
== (unsigned int)max_keys
);
4705 /* Release allocated resources */
4706 for (size_t x
= 0; x
< max_keys
; ++x
)
4711 memc
->number_of_hosts
= number_of_hosts
;
4712 return TEST_SUCCESS
;
4715 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
4717 memcached_return rc
;
4718 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4719 assert(rc
== MEMCACHED_SUCCESS
);
4721 return regression_bug_434843(memc
);
4724 static test_return_t
regression_bug_421108(memcached_st
*memc
)
4726 memcached_return rc
;
4727 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4728 assert(rc
== MEMCACHED_SUCCESS
);
4730 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
4731 assert(rc
== MEMCACHED_SUCCESS
);
4732 assert(bytes
!= NULL
);
4733 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
4735 assert(rc
== MEMCACHED_SUCCESS
);
4736 assert(bytes_read
!= NULL
);
4738 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
4739 "bytes_written", &rc
);
4740 assert(rc
== MEMCACHED_SUCCESS
);
4741 assert(bytes_written
!= NULL
);
4743 assert(strcmp(bytes
, bytes_read
) != 0);
4744 assert(strcmp(bytes
, bytes_written
) != 0);
4746 /* Release allocated resources */
4749 free(bytes_written
);
4750 memcached_stat_free(NULL
, memc_stat
);
4751 return TEST_SUCCESS
;
4755 * The test case isn't obvious so I should probably document why
4756 * it works the way it does. Bug 442914 was caused by a bug
4757 * in the logic in memcached_purge (it did not handle the case
4758 * where the number of bytes sent was equal to the watermark).
4759 * In this test case, create messages so that we hit that case
4760 * and then disable noreply mode and issue a new command to
4761 * verify that it isn't stuck. If we change the format for the
4762 * delete command or the watermarks, we need to update this
4765 static test_return_t
regression_bug_442914(memcached_st
*memc
)
4767 memcached_return rc
;
4768 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4769 assert(rc
== MEMCACHED_SUCCESS
);
4770 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
4772 uint32_t number_of_hosts
= memc
->number_of_hosts
;
4773 memc
->number_of_hosts
= 1;
4778 for (int x
= 0; x
< 250; ++x
)
4780 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
4781 rc
= memcached_delete(memc
, k
, len
, 0);
4782 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4785 len
= (size_t)snprintf(k
, sizeof(k
), "%037u", 251);
4786 rc
= memcached_delete(memc
, k
, len
, 0);
4787 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4789 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
4790 assert(rc
== MEMCACHED_SUCCESS
);
4791 rc
= memcached_delete(memc
, k
, len
, 0);
4792 assert(rc
== MEMCACHED_NOTFOUND
);
4794 memc
->number_of_hosts
= number_of_hosts
;
4796 return TEST_SUCCESS
;
4799 static test_return_t
regression_bug_447342(memcached_st
*memc
)
4801 if (memc
->number_of_hosts
< 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
4802 return TEST_SKIPPED
;
4804 memcached_return rc
;
4806 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
4807 assert(rc
== MEMCACHED_SUCCESS
);
4809 const size_t max_keys
= 100;
4810 char **keys
= calloc(max_keys
, sizeof(char*));
4811 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
4813 for (int x
= 0; x
< (int)max_keys
; ++x
)
4816 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
4818 assert(keys
[x
] != NULL
);
4819 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
4820 assert(rc
== MEMCACHED_SUCCESS
);
4824 ** We are using the quiet commands to store the replicas, so we need
4825 ** to ensure that all of them are processed before we can continue.
4826 ** In the test we go directly from storing the object to trying to
4827 ** receive the object from all of the different servers, so we
4828 ** could end up in a race condition (the memcached server hasn't yet
4829 ** processed the quiet command from the replication set when it process
4830 ** the request from the other client (created by the clone)). As a
4831 ** workaround for that we call memcached_quit to send the quit command
4832 ** to the server and wait for the response ;-) If you use the test code
4833 ** as an example for your own code, please note that you shouldn't need
4836 memcached_quit(memc
);
4838 /* Verify that all messages are stored, and we didn't stuff too much
4841 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
4842 assert(rc
== MEMCACHED_SUCCESS
);
4844 unsigned int counter
= 0;
4845 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
4846 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
4847 /* Verify that we received all of the key/value pairs */
4848 assert(counter
== (unsigned int)max_keys
);
4850 memcached_quit(memc
);
4852 * Don't do the following in your code. I am abusing the internal details
4853 * within the library, and this is not a supported interface.
4854 * This is to verify correct behavior in the library. Fake that two servers
4857 unsigned int port0
= memc
->hosts
[0].port
;
4858 unsigned int port2
= memc
->hosts
[2].port
;
4859 memc
->hosts
[0].port
= 0;
4860 memc
->hosts
[2].port
= 0;
4862 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
4863 assert(rc
== MEMCACHED_SUCCESS
);
4866 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
4867 assert(counter
== (unsigned int)max_keys
);
4869 /* restore the memc handle */
4870 memc
->hosts
[0].port
= port0
;
4871 memc
->hosts
[2].port
= port2
;
4873 memcached_quit(memc
);
4875 /* Remove half of the objects */
4876 for (int x
= 0; x
< (int)max_keys
; ++x
)
4879 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
4880 assert(rc
== MEMCACHED_SUCCESS
);
4883 memcached_quit(memc
);
4884 memc
->hosts
[0].port
= 0;
4885 memc
->hosts
[2].port
= 0;
4887 /* now retry the command, this time we should have cache misses */
4888 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
4889 assert(rc
== MEMCACHED_SUCCESS
);
4892 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
4893 assert(counter
== (unsigned int)(max_keys
>> 1));
4895 /* Release allocated resources */
4896 for (size_t x
= 0; x
< max_keys
; ++x
)
4901 /* restore the memc handle */
4902 memc
->hosts
[0].port
= port0
;
4903 memc
->hosts
[2].port
= port2
;
4904 return TEST_SUCCESS
;
4907 static test_return_t
regression_bug_463297(memcached_st
*memc
)
4909 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4910 assert(memc_clone
!= NULL
);
4911 assert(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
4913 if (memc_clone
->hosts
[0].major_version
> 1 ||
4914 (memc_clone
->hosts
[0].major_version
== 1 &&
4915 memc_clone
->hosts
[0].minor_version
> 2))
4917 /* Binary protocol doesn't support deferred delete */
4918 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
4919 assert(bin_clone
!= NULL
);
4920 assert(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
4921 assert(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
4922 memcached_free(bin_clone
);
4924 memcached_quit(memc_clone
);
4926 /* If we know the server version, deferred delete should fail
4927 * with invalid arguments */
4928 assert(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
4930 /* If we don't know the server version, we should get a protocol error */
4931 memcached_return rc
= memcached_delete(memc
, "foo", 3, 1);
4932 /* but there is a bug in some of the memcached servers (1.4) that treats
4933 * the counter as noreply so it doesn't send the proper error message
4935 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
4937 /* And buffered mode should be disabled and we should get protocol error */
4938 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
4939 rc
= memcached_delete(memc
, "foo", 3, 1);
4940 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
4942 /* Same goes for noreply... */
4943 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
4944 rc
= memcached_delete(memc
, "foo", 3, 1);
4945 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
4947 /* but a normal request should go through (and be buffered) */
4948 assert((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
4949 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4951 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
4952 /* unbuffered noreply should be success */
4953 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
4954 /* unbuffered with reply should be not found... */
4955 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
4956 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
4959 memcached_free(memc_clone
);
4960 return TEST_SUCCESS
;
4964 /* Test memcached_server_get_last_disconnect
4965 * For a working server set, shall be NULL
4966 * For a set of non existing server, shall not be NULL
4968 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
4970 memcached_return rc
;
4971 memcached_server_st
*disconnected_server
;
4973 /* With the working set of server */
4974 const char *key
= "marmotte";
4975 const char *value
= "milka";
4977 rc
= memcached_set(memc
, key
, strlen(key
),
4978 value
, strlen(value
),
4979 (time_t)0, (uint32_t)0);
4980 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4982 disconnected_server
= memcached_server_get_last_disconnect(memc
);
4983 assert(disconnected_server
== NULL
);
4985 /* With a non existing server */
4987 memcached_server_st
*servers
;
4989 const char *server_list
= "localhost:9";
4991 servers
= memcached_servers_parse(server_list
);
4993 mine
= memcached_create(NULL
);
4994 rc
= memcached_server_push(mine
, servers
);
4995 assert(rc
== MEMCACHED_SUCCESS
);
4996 memcached_server_list_free(servers
);
4999 rc
= memcached_set(mine
, key
, strlen(key
),
5000 value
, strlen(value
),
5001 (time_t)0, (uint32_t)0);
5002 assert(rc
!= MEMCACHED_SUCCESS
);
5004 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5005 assert(disconnected_server
!= NULL
);
5006 assert(disconnected_server
->port
== 9);
5007 assert(strncmp(disconnected_server
->hostname
,"localhost",9) == 0);
5009 memcached_quit(mine
);
5010 memcached_free(mine
);
5012 return TEST_SUCCESS
;
5016 * This test ensures that the failure counter isn't incremented during
5017 * normal termination of the memcached instance.
5019 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5021 memcached_return rc
;
5023 /* Set value to force connection to the server */
5024 const char *key
= "marmotte";
5025 const char *value
= "milka";
5028 * Please note that I'm abusing the internal structures in libmemcached
5029 * in a non-portable way and you shouldn't be doing this. I'm only
5030 * doing this in order to verify that the library works the way it should
5032 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5033 memc
->number_of_hosts
= 1;
5035 /* Ensure that we are connected to the server by setting a value */
5036 rc
= memcached_set(memc
, key
, strlen(key
),
5037 value
, strlen(value
),
5038 (time_t)0, (uint32_t)0);
5039 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5042 /* The test is to see that the memcached_quit doesn't increase the
5043 * the server failure conter, so let's ensure that it is zero
5044 * before sending quit
5046 memc
->hosts
[0].server_failure_counter
= 0;
5048 memcached_quit(memc
);
5050 /* Verify that it memcached_quit didn't increment the failure counter
5051 * Please note that this isn't bullet proof, because an error could
5054 assert(memc
->hosts
[0].server_failure_counter
== 0);
5056 /* restore the instance */
5057 memc
->number_of_hosts
= number_of_hosts
;
5059 return TEST_SUCCESS
;
5062 test_st udp_setup_server_tests
[] ={
5063 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
5064 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
5065 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
5069 test_st upd_io_tests
[] ={
5070 {"udp_set_test", 0, udp_set_test
},
5071 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
5072 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
5073 {"udp_delete_test", 0, udp_delete_test
},
5074 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
5075 {"udp_verbosity_test", 0, udp_verbosity_test
},
5076 {"udp_quit_test", 0, udp_quit_test
},
5077 {"udp_flush_test", 0, udp_flush_test
},
5078 {"udp_incr_test", 0, udp_incr_test
},
5079 {"udp_decr_test", 0, udp_decr_test
},
5080 {"udp_stat_test", 0, udp_stat_test
},
5081 {"udp_version_test", 0, udp_version_test
},
5082 {"udp_get_test", 0, udp_get_test
},
5083 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
5087 /* Clean the server before beginning testing */
5089 {"flush", 0, flush_test
},
5090 {"init", 0, init_test
},
5091 {"allocation", 0, allocation_test
},
5092 {"server_list_null_test", 0, server_list_null_test
},
5093 {"server_unsort", 0, server_unsort_test
},
5094 {"server_sort", 0, server_sort_test
},
5095 {"server_sort2", 0, server_sort2_test
},
5096 {"clone_test", 0, clone_test
},
5097 {"connection_test", 0, connection_test
},
5098 {"callback_test", 0, callback_test
},
5099 {"behavior_test", 0, behavior_test
},
5100 {"userdata_test", 0, userdata_test
},
5101 {"error", 0, error_test
},
5102 {"set", 0, set_test
},
5103 {"set2", 0, set_test2
},
5104 {"set3", 0, set_test3
},
5105 {"dump", 1, dump_test
},
5106 {"add", 1, add_test
},
5107 {"replace", 1, replace_test
},
5108 {"delete", 1, delete_test
},
5109 {"get", 1, get_test
},
5110 {"get2", 0, get_test2
},
5111 {"get3", 0, get_test3
},
5112 {"get4", 0, get_test4
},
5113 {"partial mget", 0, get_test5
},
5114 {"stats_servername", 0, stats_servername_test
},
5115 {"increment", 0, increment_test
},
5116 {"increment_with_initial", 1, increment_with_initial_test
},
5117 {"decrement", 0, decrement_test
},
5118 {"decrement_with_initial", 1, decrement_with_initial_test
},
5119 {"quit", 0, quit_test
},
5120 {"mget", 1, mget_test
},
5121 {"mget_result", 1, mget_result_test
},
5122 {"mget_result_alloc", 1, mget_result_alloc_test
},
5123 {"mget_result_function", 1, mget_result_function
},
5124 {"mget_execute", 1, mget_execute
},
5125 {"mget_end", 0, mget_end
},
5126 {"get_stats", 0, get_stats
},
5127 {"add_host_test", 0, add_host_test
},
5128 {"add_host_test_1", 0, add_host_test1
},
5129 {"get_stats_keys", 0, get_stats_keys
},
5130 {"behavior_test", 0, get_stats_keys
},
5131 {"callback_test", 0, get_stats_keys
},
5132 {"version_string_test", 0, version_string_test
},
5133 {"bad_key", 1, bad_key_test
},
5134 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
5135 {"read_through", 1, read_through
},
5136 {"delete_through", 1, delete_through
},
5137 {"noreply", 1, noreply_test
},
5138 {"analyzer", 1, analyzer_test
},
5139 #ifdef HAVE_LIBMEMCACHEDUTIL
5140 {"connectionpool", 1, connection_pool_test
},
5142 {"test_get_last_disconnect", 1, test_get_last_disconnect
},
5146 test_st async_tests
[] ={
5147 {"add", 1, add_wrapper
},
5151 test_st string_tests
[] ={
5152 {"string static with null", 0, string_static_null
},
5153 {"string alloc with null", 0, string_alloc_null
},
5154 {"string alloc with 1K", 0, string_alloc_with_size
},
5155 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
5156 {"string append", 0, string_alloc_append
},
5157 {"string append failure (too big)", 0, string_alloc_append_toobig
},
5161 test_st result_tests
[] ={
5162 {"result static", 0, result_static
},
5163 {"result alloc", 0, result_alloc
},
5167 test_st version_1_2_3
[] ={
5168 {"append", 0, append_test
},
5169 {"prepend", 0, prepend_test
},
5170 {"cas", 0, cas_test
},
5171 {"cas2", 0, cas2_test
},
5172 {"append_binary", 0, append_binary_test
},
5176 test_st user_tests
[] ={
5177 {"user_supplied_bug1", 0, user_supplied_bug1
},
5178 {"user_supplied_bug2", 0, user_supplied_bug2
},
5179 {"user_supplied_bug3", 0, user_supplied_bug3
},
5180 {"user_supplied_bug4", 0, user_supplied_bug4
},
5181 {"user_supplied_bug5", 1, user_supplied_bug5
},
5182 {"user_supplied_bug6", 1, user_supplied_bug6
},
5183 {"user_supplied_bug7", 1, user_supplied_bug7
},
5184 {"user_supplied_bug8", 1, user_supplied_bug8
},
5185 {"user_supplied_bug9", 1, user_supplied_bug9
},
5186 {"user_supplied_bug10", 1, user_supplied_bug10
},
5187 {"user_supplied_bug11", 1, user_supplied_bug11
},
5188 {"user_supplied_bug12", 1, user_supplied_bug12
},
5189 {"user_supplied_bug13", 1, user_supplied_bug13
},
5190 {"user_supplied_bug14", 1, user_supplied_bug14
},
5191 {"user_supplied_bug15", 1, user_supplied_bug15
},
5192 {"user_supplied_bug16", 1, user_supplied_bug16
},
5195 ** It seems to be something weird with the character sets..
5196 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
5197 ** guess I need to find out how this is supposed to work.. Perhaps I need
5198 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
5199 ** so just disable the code for now...).
5201 {"user_supplied_bug17", 1, user_supplied_bug17
},
5203 {"user_supplied_bug18", 1, user_supplied_bug18
},
5204 {"user_supplied_bug19", 1, user_supplied_bug19
},
5205 {"user_supplied_bug20", 1, user_supplied_bug20
},
5206 {"user_supplied_bug21", 1, user_supplied_bug21
},
5207 {"wrong_failure_counter_test", 1, wrong_failure_counter_test
},
5211 test_st replication_tests
[]= {
5212 {"set", 1, replication_set_test
},
5213 {"get", 0, replication_get_test
},
5214 {"mget", 0, replication_mget_test
},
5215 {"delete", 0, replication_delete_test
},
5220 * The following test suite is used to verify that we don't introduce
5221 * regression bugs. If you want more information about the bug / test,
5222 * you should look in the bug report at
5223 * http://bugs.launchpad.net/libmemcached
5225 test_st regression_tests
[]= {
5226 {"lp:434484", 1, regression_bug_434484
},
5227 {"lp:434843", 1, regression_bug_434843
},
5228 {"lp:434843 buffered", 1, regression_bug_434843_buffered
},
5229 {"lp:421108", 1, regression_bug_421108
},
5230 {"lp:442914", 1, regression_bug_442914
},
5231 {"lp:447342", 1, regression_bug_447342
},
5232 {"lp:463297", 1, regression_bug_463297
},
5236 test_st generate_tests
[] ={
5237 {"generate_pairs", 1, generate_pairs
},
5238 {"generate_data", 1, generate_data
},
5239 {"get_read", 0, get_read
},
5240 {"delete_generate", 0, delete_generate
},
5241 {"generate_buffer_data", 1, generate_buffer_data
},
5242 {"delete_buffer", 0, delete_buffer_generate
},
5243 {"generate_data", 1, generate_data
},
5244 {"mget_read", 0, mget_read
},
5245 {"mget_read_result", 0, mget_read_result
},
5246 {"mget_read_function", 0, mget_read_function
},
5247 {"cleanup", 1, cleanup_pairs
},
5248 {"generate_large_pairs", 1, generate_large_pairs
},
5249 {"generate_data", 1, generate_data
},
5250 {"generate_buffer_data", 1, generate_buffer_data
},
5251 {"cleanup", 1, cleanup_pairs
},
5255 test_st consistent_tests
[] ={
5256 {"generate_pairs", 1, generate_pairs
},
5257 {"generate_data", 1, generate_data
},
5258 {"get_read", 0, get_read_count
},
5259 {"cleanup", 1, cleanup_pairs
},
5263 test_st consistent_weighted_tests
[] ={
5264 {"generate_pairs", 1, generate_pairs
},
5265 {"generate_data", 1, generate_data_with_stats
},
5266 {"get_read", 0, get_read_count
},
5267 {"cleanup", 1, cleanup_pairs
},
5271 test_st hsieh_availability
[] ={
5272 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
5276 test_st ketama_auto_eject_hosts
[] ={
5277 {"auto_eject_hosts", 1, auto_eject_hosts
},
5281 test_st hash_tests
[] ={
5282 {"md5", 0, md5_run
},
5283 {"crc", 0, crc_run
},
5284 {"fnv1_64", 0, fnv1_64_run
},
5285 {"fnv1a_64", 0, fnv1a_64_run
},
5286 {"fnv1_32", 0, fnv1_32_run
},
5287 {"fnv1a_32", 0, fnv1a_32_run
},
5288 {"hsieh", 0, hsieh_run
},
5289 {"murmur", 0, murmur_run
},
5290 {"jenkis", 0, jenkins_run
},
5294 collection_st collection
[] ={
5295 {"hsieh_availability",0,0,hsieh_availability
},
5296 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
5297 {"udp_io", init_udp
, 0, upd_io_tests
},
5298 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
5299 {"block", 0, 0, tests
},
5300 {"binary", pre_binary
, 0, tests
},
5301 {"nonblock", pre_nonblock
, 0, tests
},
5302 {"nodelay", pre_nodelay
, 0, tests
},
5303 {"settimer", pre_settimer
, 0, tests
},
5304 {"md5", pre_md5
, 0, tests
},
5305 {"crc", pre_crc
, 0, tests
},
5306 {"hsieh", pre_hsieh
, 0, tests
},
5307 {"jenkins", pre_jenkins
, 0, tests
},
5308 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
5309 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
5310 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
5311 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
5312 {"ketama", pre_behavior_ketama
, 0, tests
},
5313 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
5314 {"unix_socket", pre_unix_socket
, 0, tests
},
5315 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
5316 {"poll_timeout", poll_timeout
, 0, tests
},
5317 {"gets", enable_cas
, 0, tests
},
5318 {"consistent", enable_consistent
, 0, tests
},
5319 #ifdef MEMCACHED_ENABLE_DEPRECATED
5320 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
5322 {"memory_allocators", set_memory_alloc
, 0, tests
},
5323 {"prefix", set_prefix
, 0, tests
},
5324 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
5325 {"string", 0, 0, string_tests
},
5326 {"result", 0, 0, result_tests
},
5327 {"async", pre_nonblock
, 0, async_tests
},
5328 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
5329 {"user", 0, 0, user_tests
},
5330 {"generate", 0, 0, generate_tests
},
5331 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
5332 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
5333 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
5334 {"generate_md5", pre_md5
, 0, generate_tests
},
5335 {"generate_murmur", pre_murmur
, 0, generate_tests
},
5336 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
5337 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
5338 {"consistent_not", 0, 0, consistent_tests
},
5339 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
5340 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
5341 {"test_hashes", 0, 0, hash_tests
},
5342 {"replication", pre_replication
, 0, replication_tests
},
5343 {"replication_noblock", pre_replication_noblock
, 0, replication_tests
},
5344 {"regression", 0, 0, regression_tests
},
5348 #define SERVERS_TO_CREATE 5
5350 /* Prototypes for functions we will pass to test framework */
5351 void *world_create(void);
5352 void world_destroy(void *p
);
5354 void *world_create(void)
5356 server_startup_st
*construct
;
5358 construct
= calloc(sizeof(server_startup_st
), 1);
5359 construct
->count
= SERVERS_TO_CREATE
;
5361 server_startup(construct
);
5367 void world_destroy(void *p
)
5369 server_startup_st
*construct
= (server_startup_st
*)p
;
5370 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
5371 memcached_server_list_free(servers
);
5373 server_shutdown(construct
);
5377 void get_world(world_st
*world
)
5379 world
->collections
= collection
;
5380 world
->create
= world_create
;
5381 world
->destroy
= world_destroy
;