2 Sample test application.
5 #include "libmemcached/common.h"
12 #include <sys/types.h>
17 #include "clients/generator.h"
18 #include "clients/execute.h"
21 #define INT64_MAX LONG_MAX
24 #define INT32_MAX INT_MAX
30 #ifdef HAVE_LIBMEMCACHEDUTIL
32 #include "libmemcached/memcached_util.h"
35 #define GLOBAL_COUNT 10000
36 #define GLOBAL2_COUNT 100
37 #define SERVERS_TO_CREATE 5
38 static uint32_t global_count
;
40 static pairs_st
*global_pairs
;
41 static const char *global_keys
[GLOBAL_COUNT
];
42 static size_t global_keys_length
[GLOBAL_COUNT
];
44 static test_return
init_test(memcached_st
*not_used
__attribute__((unused
)))
48 (void)memcached_create(&memc
);
49 memcached_free(&memc
);
54 static test_return
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
56 memcached_server_st
*server_list
;
59 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
60 assert(server_list
== NULL
);
62 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
63 assert(server_list
== NULL
);
65 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
66 assert(server_list
== NULL
);
71 #define TEST_PORT_COUNT 7
72 uint32_t test_ports
[TEST_PORT_COUNT
];
74 static memcached_return
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
77 uint32_t bigger
= *((uint32_t *)(context
));
78 assert(bigger
<= server
->port
);
79 *((uint32_t *)(context
))= server
->port
;
81 return MEMCACHED_SUCCESS
;
84 static test_return
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
87 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
89 memcached_server_function callbacks
[1];
90 memcached_st
*local_memc
;
92 local_memc
= memcached_create(NULL
);
94 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
96 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
98 test_ports
[x
]= (uint32_t)random() % 64000;
99 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
100 assert(local_memc
->number_of_hosts
== x
+ 1);
101 assert(local_memc
->hosts
[0].count
== x
+1);
102 assert(rc
== MEMCACHED_SUCCESS
);
105 callbacks
[0]= server_display_function
;
106 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
109 memcached_free(local_memc
);
114 static test_return
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
116 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
118 memcached_server_function callbacks
[1];
119 memcached_st
*local_memc
;
121 local_memc
= memcached_create(NULL
);
123 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
124 assert(rc
== MEMCACHED_SUCCESS
);
126 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
127 assert(rc
== MEMCACHED_SUCCESS
);
128 assert(local_memc
->hosts
[0].port
== 43043);
130 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
131 assert(rc
== MEMCACHED_SUCCESS
);
132 assert(local_memc
->hosts
[0].port
== 43042);
133 assert(local_memc
->hosts
[1].port
== 43043);
135 callbacks
[0]= server_display_function
;
136 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
139 memcached_free(local_memc
);
144 static memcached_return
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
147 uint32_t x
= *((uint32_t *)(context
));
149 assert(test_ports
[x
] == server
->port
);
150 *((uint32_t *)(context
))= ++x
;
152 return MEMCACHED_SUCCESS
;
155 static test_return
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
158 uint32_t counter
= 0; /* Prime the value for the assert in server_display_function */
159 uint32_t bigger
= 0; /* Prime the value for the assert in server_display_function */
161 memcached_server_function callbacks
[1];
162 memcached_st
*local_memc
;
164 local_memc
= memcached_create(NULL
);
167 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
169 test_ports
[x
]= (uint32_t)(random() % 64000);
170 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
171 assert(local_memc
->number_of_hosts
== x
+1);
172 assert(local_memc
->hosts
[0].count
== x
+1);
173 assert(rc
== MEMCACHED_SUCCESS
);
176 callbacks
[0]= server_display_unsort_function
;
177 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
179 /* Now we sort old data! */
180 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
181 callbacks
[0]= server_display_function
;
182 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
185 memcached_free(local_memc
);
190 static test_return
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
193 memc
= memcached_create(NULL
);
195 memcached_free(memc
);
200 static test_return
clone_test(memcached_st
*memc
)
204 memcached_st
*memc_clone
;
205 memc_clone
= memcached_clone(NULL
, NULL
);
207 memcached_free(memc_clone
);
210 /* Can we init from null? */
212 memcached_st
*memc_clone
;
213 memc_clone
= memcached_clone(NULL
, memc
);
216 assert(memc_clone
->call_free
== memc
->call_free
);
217 assert(memc_clone
->call_malloc
== memc
->call_malloc
);
218 assert(memc_clone
->call_realloc
== memc
->call_realloc
);
219 assert(memc_clone
->call_calloc
== memc
->call_calloc
);
220 assert(memc_clone
->connect_timeout
== memc
->connect_timeout
);
221 assert(memc_clone
->delete_trigger
== memc
->delete_trigger
);
222 assert(memc_clone
->distribution
== memc
->distribution
);
223 assert(memc_clone
->flags
== memc
->flags
);
224 assert(memc_clone
->get_key_failure
== memc
->get_key_failure
);
225 assert(memc_clone
->hash
== memc
->hash
);
226 assert(memc_clone
->hash_continuum
== memc
->hash_continuum
);
227 assert(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
228 assert(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
229 assert(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
230 assert(memc_clone
->on_cleanup
== memc
->on_cleanup
);
231 assert(memc_clone
->on_clone
== memc
->on_clone
);
232 assert(memc_clone
->poll_timeout
== memc
->poll_timeout
);
233 assert(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
234 assert(memc_clone
->recv_size
== memc
->recv_size
);
235 assert(memc_clone
->retry_timeout
== memc
->retry_timeout
);
236 assert(memc_clone
->send_size
== memc
->send_size
);
237 assert(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
238 assert(memc_clone
->snd_timeout
== memc
->snd_timeout
);
239 assert(memc_clone
->user_data
== memc
->user_data
);
241 memcached_free(memc_clone
);
244 /* Can we init from struct? */
246 memcached_st declared_clone
;
247 memcached_st
*memc_clone
;
248 memset(&declared_clone
, 0 , sizeof(memcached_st
));
249 memc_clone
= memcached_clone(&declared_clone
, NULL
);
251 memcached_free(memc_clone
);
254 /* Can we init from struct? */
256 memcached_st declared_clone
;
257 memcached_st
*memc_clone
;
258 memset(&declared_clone
, 0 , sizeof(memcached_st
));
259 memc_clone
= memcached_clone(&declared_clone
, memc
);
261 memcached_free(memc_clone
);
267 static test_return
userdata_test(memcached_st
*memc
)
270 assert(memcached_set_user_data(memc
, foo
) == NULL
);
271 assert(memcached_get_user_data(memc
) == foo
);
272 assert(memcached_set_user_data(memc
, NULL
) == foo
);
277 static test_return
connection_test(memcached_st
*memc
)
281 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
282 assert(rc
== MEMCACHED_SUCCESS
);
287 static test_return
error_test(memcached_st
*memc
)
290 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
291 982370485U, 1263635348U, 4242906218U, 3829656100U,
292 1891735253U, 334139633U, 2257084983U, 3088286104U,
293 13199785U, 2542027183U, 1097051614U, 199566778U,
294 2748246961U, 2465192557U, 1664094137U, 2405439045U,
295 1842224848U, 692413798U, 3479807801U, 919913813U,
296 4269430871U, 610793021U, 527273862U, 1437122909U,
297 2300930706U, 2943759320U, 674306647U, 2400528935U,
298 54481931U, 4186304426U, 1741088401U, 2979625118U,
299 4159057246U, 3425930182U};
301 // You have updated the memcache_error messages but not updated docs/tests.
302 assert(MEMCACHED_MAXIMUM_RETURN
== 38);
303 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
306 const char *msg
= memcached_strerror(memc
, rc
);
307 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
308 MEMCACHED_HASH_JENKINS
);
309 assert(values
[rc
] == hash_val
);
315 static test_return
set_test(memcached_st
*memc
)
318 const char *key
= "foo";
319 const char *value
= "when we sanitize";
321 rc
= memcached_set(memc
, key
, strlen(key
),
322 value
, strlen(value
),
323 (time_t)0, (uint32_t)0);
324 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
329 static test_return
append_test(memcached_st
*memc
)
332 const char *key
= "fig";
333 const char *in_value
= "we";
334 char *out_value
= NULL
;
338 rc
= memcached_flush(memc
, 0);
339 assert(rc
== MEMCACHED_SUCCESS
);
341 rc
= memcached_set(memc
, key
, strlen(key
),
342 in_value
, strlen(in_value
),
343 (time_t)0, (uint32_t)0);
344 assert(rc
== MEMCACHED_SUCCESS
);
346 rc
= memcached_append(memc
, key
, strlen(key
),
347 " the", strlen(" the"),
348 (time_t)0, (uint32_t)0);
349 assert(rc
== MEMCACHED_SUCCESS
);
351 rc
= memcached_append(memc
, key
, strlen(key
),
352 " people", strlen(" people"),
353 (time_t)0, (uint32_t)0);
354 assert(rc
== MEMCACHED_SUCCESS
);
356 out_value
= memcached_get(memc
, key
, strlen(key
),
357 &value_length
, &flags
, &rc
);
358 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
359 assert(strlen("we the people") == value_length
);
360 assert(rc
== MEMCACHED_SUCCESS
);
366 static test_return
append_binary_test(memcached_st
*memc
)
369 const char *key
= "numbers";
370 unsigned int *store_ptr
;
371 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
377 rc
= memcached_flush(memc
, 0);
378 assert(rc
== MEMCACHED_SUCCESS
);
380 rc
= memcached_set(memc
,
383 (time_t)0, (uint32_t)0);
384 assert(rc
== MEMCACHED_SUCCESS
);
386 for (x
= 0; store_list
[x
] ; x
++)
388 rc
= memcached_append(memc
,
390 (char *)&store_list
[x
], sizeof(unsigned int),
391 (time_t)0, (uint32_t)0);
392 assert(rc
== MEMCACHED_SUCCESS
);
395 value
= memcached_get(memc
, key
, strlen(key
),
396 &value_length
, &flags
, &rc
);
397 assert((value_length
== (sizeof(unsigned int) * x
)));
398 assert(rc
== MEMCACHED_SUCCESS
);
400 store_ptr
= (unsigned int *)value
;
402 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
404 assert(*store_ptr
== store_list
[x
++]);
412 static test_return
cas2_test(memcached_st
*memc
)
415 const char *keys
[]= {"fudge", "son", "food"};
416 size_t key_length
[]= {5, 3, 4};
417 const char *value
= "we the people";
418 size_t value_length
= strlen("we the people");
420 memcached_result_st results_obj
;
421 memcached_result_st
*results
;
424 rc
= memcached_flush(memc
, 0);
425 assert(rc
== MEMCACHED_SUCCESS
);
427 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
429 for (x
= 0; x
< 3; x
++)
431 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
432 keys
[x
], key_length
[x
],
433 (time_t)50, (uint32_t)9);
434 assert(rc
== MEMCACHED_SUCCESS
);
437 rc
= memcached_mget(memc
, keys
, key_length
, 3);
439 results
= memcached_result_create(memc
, &results_obj
);
441 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
443 assert(results
->cas
);
444 assert(rc
== MEMCACHED_SUCCESS
);
445 assert(memcached_result_cas(results
));
447 assert(!memcmp(value
, "we the people", strlen("we the people")));
448 assert(strlen("we the people") == value_length
);
449 assert(rc
== MEMCACHED_SUCCESS
);
451 memcached_result_free(&results_obj
);
456 static test_return
cas_test(memcached_st
*memc
)
459 const char *key
= "fun";
460 size_t key_length
= strlen(key
);
461 const char *value
= "we the people";
462 const char* keys
[2] = { key
, NULL
};
463 size_t keylengths
[2] = { strlen(key
), 0 };
464 size_t value_length
= strlen(value
);
465 const char *value2
= "change the value";
466 size_t value2_length
= strlen(value2
);
468 memcached_result_st results_obj
;
469 memcached_result_st
*results
;
472 rc
= memcached_flush(memc
, 0);
473 assert(rc
== MEMCACHED_SUCCESS
);
475 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
477 rc
= memcached_set(memc
, key
, strlen(key
),
478 value
, strlen(value
),
479 (time_t)0, (uint32_t)0);
480 assert(rc
== MEMCACHED_SUCCESS
);
482 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
484 results
= memcached_result_create(memc
, &results_obj
);
486 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
488 assert(rc
== MEMCACHED_SUCCESS
);
489 assert(memcached_result_cas(results
));
490 assert(!memcmp(value
, memcached_result_value(results
), value_length
));
491 assert(strlen(memcached_result_value(results
)) == value_length
);
492 assert(rc
== MEMCACHED_SUCCESS
);
493 uint64_t cas
= memcached_result_cas(results
);
496 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
497 assert(rc
== MEMCACHED_END
);
498 assert(results
== NULL
);
501 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
502 assert(rc
== MEMCACHED_SUCCESS
);
505 * The item will have a new cas value, so try to set it again with the old
506 * value. This should fail!
508 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
509 assert(rc
== MEMCACHED_DATA_EXISTS
);
511 memcached_result_free(&results_obj
);
516 static test_return
prepend_test(memcached_st
*memc
)
519 const char *key
= "fig";
520 const char *value
= "people";
521 char *out_value
= NULL
;
525 rc
= memcached_flush(memc
, 0);
526 assert(rc
== MEMCACHED_SUCCESS
);
528 rc
= memcached_set(memc
, key
, strlen(key
),
529 value
, strlen(value
),
530 (time_t)0, (uint32_t)0);
531 assert(rc
== MEMCACHED_SUCCESS
);
533 rc
= memcached_prepend(memc
, key
, strlen(key
),
534 "the ", strlen("the "),
535 (time_t)0, (uint32_t)0);
536 assert(rc
== MEMCACHED_SUCCESS
);
538 rc
= memcached_prepend(memc
, key
, strlen(key
),
539 "we ", strlen("we "),
540 (time_t)0, (uint32_t)0);
541 assert(rc
== MEMCACHED_SUCCESS
);
543 out_value
= memcached_get(memc
, key
, strlen(key
),
544 &value_length
, &flags
, &rc
);
545 assert(!memcmp(out_value
, "we the people", strlen("we the people")));
546 assert(strlen("we the people") == value_length
);
547 assert(rc
== MEMCACHED_SUCCESS
);
554 Set the value, then quit to make sure it is flushed.
555 Come back in and test that add fails.
557 static test_return
add_test(memcached_st
*memc
)
560 const char *key
= "foo";
561 const char *value
= "when we sanitize";
562 unsigned long long setting_value
;
564 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
566 rc
= memcached_set(memc
, key
, strlen(key
),
567 value
, strlen(value
),
568 (time_t)0, (uint32_t)0);
569 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
570 memcached_quit(memc
);
571 rc
= memcached_add(memc
, key
, strlen(key
),
572 value
, strlen(value
),
573 (time_t)0, (uint32_t)0);
575 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
577 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
579 assert(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
585 ** There was a problem of leaking filedescriptors in the initial release
586 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
587 ** systems it seems that the kernel is slow on reclaiming the resources
588 ** because the connects starts to time out (the test doesn't do much
589 ** anyway, so just loop 10 iterations)
591 static test_return
add_wrapper(memcached_st
*memc
)
594 unsigned int max
= 10000;
599 for (x
= 0; x
< max
; x
++)
605 static test_return
replace_test(memcached_st
*memc
)
608 const char *key
= "foo";
609 const char *value
= "when we sanitize";
610 const char *original
= "first we insert some data";
612 rc
= memcached_set(memc
, key
, strlen(key
),
613 original
, strlen(original
),
614 (time_t)0, (uint32_t)0);
615 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
617 rc
= memcached_replace(memc
, key
, strlen(key
),
618 value
, strlen(value
),
619 (time_t)0, (uint32_t)0);
620 assert(rc
== MEMCACHED_SUCCESS
);
625 static test_return
delete_test(memcached_st
*memc
)
628 const char *key
= "foo";
629 const char *value
= "when we sanitize";
631 rc
= memcached_set(memc
, key
, strlen(key
),
632 value
, strlen(value
),
633 (time_t)0, (uint32_t)0);
634 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
636 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
637 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
642 static test_return
flush_test(memcached_st
*memc
)
646 rc
= memcached_flush(memc
, 0);
647 assert(rc
== MEMCACHED_SUCCESS
);
652 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
653 memcached_server_st
*server
__attribute__((unused
)),
654 void *context
__attribute__((unused
)))
658 return MEMCACHED_SUCCESS
;
661 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
664 strcpy(context
, "foo bad");
665 memcached_server_function callbacks
[1];
667 callbacks
[0]= server_function
;
668 memcached_server_cursor(memc
, callbacks
, context
, 1);
672 static test_return
bad_key_test(memcached_st
*memc
)
675 const char *key
= "foo bad";
677 size_t string_length
;
679 memcached_st
*memc_clone
;
681 size_t max_keylen
= 0xffff;
683 memc_clone
= memcached_clone(NULL
, memc
);
686 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
687 assert(rc
== MEMCACHED_SUCCESS
);
689 /* All keys are valid in the binary protocol (except for length) */
690 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
692 string
= memcached_get(memc_clone
, key
, strlen(key
),
693 &string_length
, &flags
, &rc
);
694 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
695 assert(string_length
== 0);
699 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
700 assert(rc
== MEMCACHED_SUCCESS
);
701 string
= memcached_get(memc_clone
, key
, strlen(key
),
702 &string_length
, &flags
, &rc
);
703 assert(rc
== MEMCACHED_NOTFOUND
);
704 assert(string_length
== 0);
707 /* Test multi key for bad keys */
708 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
709 size_t key_lengths
[] = { 7, 7, 7 };
711 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
712 assert(rc
== MEMCACHED_SUCCESS
);
714 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
715 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
717 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
718 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
722 /* The following test should be moved to the end of this function when the
723 memcached server is updated to allow max size length of the keys in the
726 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
727 assert(rc
== MEMCACHED_SUCCESS
);
729 char *longkey
= malloc(max_keylen
+ 1);
732 memset(longkey
, 'a', max_keylen
+ 1);
733 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
734 &string_length
, &flags
, &rc
);
735 assert(rc
== MEMCACHED_NOTFOUND
);
736 assert(string_length
== 0);
739 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
740 &string_length
, &flags
, &rc
);
741 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
742 assert(string_length
== 0);
749 /* Make sure zero length keys are marked as bad */
751 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
752 assert(rc
== MEMCACHED_SUCCESS
);
753 string
= memcached_get(memc_clone
, key
, 0,
754 &string_length
, &flags
, &rc
);
755 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
756 assert(string_length
== 0);
759 memcached_free(memc_clone
);
764 #define READ_THROUGH_VALUE "set for me"
765 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
766 char *key
__attribute__((unused
)),
767 size_t key_length
__attribute__((unused
)),
768 memcached_result_st
*result
)
771 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
774 static test_return
read_through(memcached_st
*memc
)
777 const char *key
= "foo";
779 size_t string_length
;
781 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
783 string
= memcached_get(memc
, key
, strlen(key
),
784 &string_length
, &flags
, &rc
);
786 assert(rc
== MEMCACHED_NOTFOUND
);
787 assert(string_length
== 0);
790 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
792 assert(rc
== MEMCACHED_SUCCESS
);
794 string
= memcached_get(memc
, key
, strlen(key
),
795 &string_length
, &flags
, &rc
);
797 assert(rc
== MEMCACHED_SUCCESS
);
798 assert(string_length
== strlen(READ_THROUGH_VALUE
));
799 assert(!strcmp(READ_THROUGH_VALUE
, string
));
802 string
= memcached_get(memc
, key
, strlen(key
),
803 &string_length
, &flags
, &rc
);
805 assert(rc
== MEMCACHED_SUCCESS
);
806 assert(string_length
== strlen(READ_THROUGH_VALUE
));
807 assert(!strcmp(READ_THROUGH_VALUE
, string
));
813 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
815 size_t key_length
__attribute__((unused
)))
819 return MEMCACHED_SUCCESS
;
822 static test_return
delete_through(memcached_st
*memc
)
824 memcached_trigger_delete_key callback
;
827 callback
= (memcached_trigger_delete_key
)delete_trigger
;
829 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
830 assert(rc
== MEMCACHED_SUCCESS
);
835 static test_return
get_test(memcached_st
*memc
)
838 const char *key
= "foo";
840 size_t string_length
;
843 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
844 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
846 string
= memcached_get(memc
, key
, strlen(key
),
847 &string_length
, &flags
, &rc
);
849 assert(rc
== MEMCACHED_NOTFOUND
);
850 assert(string_length
== 0);
856 static test_return
get_test2(memcached_st
*memc
)
859 const char *key
= "foo";
860 const char *value
= "when we sanitize";
862 size_t string_length
;
865 rc
= memcached_set(memc
, key
, strlen(key
),
866 value
, strlen(value
),
867 (time_t)0, (uint32_t)0);
868 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
870 string
= memcached_get(memc
, key
, strlen(key
),
871 &string_length
, &flags
, &rc
);
874 assert(rc
== MEMCACHED_SUCCESS
);
875 assert(string_length
== strlen(value
));
876 assert(!memcmp(string
, value
, string_length
));
883 static test_return
set_test2(memcached_st
*memc
)
886 const char *key
= "foo";
887 const char *value
= "train in the brain";
888 size_t value_length
= strlen(value
);
891 for (x
= 0; x
< 10; x
++)
893 rc
= memcached_set(memc
, key
, strlen(key
),
895 (time_t)0, (uint32_t)0);
896 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
902 static test_return
set_test3(memcached_st
*memc
)
906 size_t value_length
= 8191;
909 value
= (char*)malloc(value_length
);
912 for (x
= 0; x
< value_length
; x
++)
913 value
[x
] = (char) (x
% 127);
915 /* The dump test relies on there being at least 32 items in memcached */
916 for (x
= 0; x
< 32; x
++)
920 sprintf(key
, "foo%u", x
);
922 rc
= memcached_set(memc
, key
, strlen(key
),
924 (time_t)0, (uint32_t)0);
925 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
933 static test_return
get_test3(memcached_st
*memc
)
936 const char *key
= "foo";
938 size_t value_length
= 8191;
940 size_t string_length
;
944 value
= (char*)malloc(value_length
);
947 for (x
= 0; x
< value_length
; x
++)
948 value
[x
] = (char) (x
% 127);
950 rc
= memcached_set(memc
, key
, strlen(key
),
952 (time_t)0, (uint32_t)0);
953 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
955 string
= memcached_get(memc
, key
, strlen(key
),
956 &string_length
, &flags
, &rc
);
958 assert(rc
== MEMCACHED_SUCCESS
);
960 assert(string_length
== value_length
);
961 assert(!memcmp(string
, value
, string_length
));
969 static test_return
get_test4(memcached_st
*memc
)
972 const char *key
= "foo";
974 size_t value_length
= 8191;
976 size_t string_length
;
980 value
= (char*)malloc(value_length
);
983 for (x
= 0; x
< value_length
; x
++)
984 value
[x
] = (char) (x
% 127);
986 rc
= memcached_set(memc
, key
, strlen(key
),
988 (time_t)0, (uint32_t)0);
989 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
991 for (x
= 0; x
< 10; x
++)
993 string
= memcached_get(memc
, key
, strlen(key
),
994 &string_length
, &flags
, &rc
);
996 assert(rc
== MEMCACHED_SUCCESS
);
998 assert(string_length
== value_length
);
999 assert(!memcmp(string
, value
, string_length
));
1009 * This test verifies that memcached_read_one_response doesn't try to
1010 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1011 * responses before you execute a storage command.
1013 static test_return
get_test5(memcached_st
*memc
)
1016 ** Request the same key twice, to ensure that we hash to the same server
1017 ** (so that we have multiple response values queued up) ;-)
1019 const char *keys
[]= { "key", "key" };
1020 size_t lengths
[]= { 3, 3 };
1024 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1025 keys
[0], lengths
[0], 0, 0);
1026 assert(rc
== MEMCACHED_SUCCESS
);
1027 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1029 memcached_result_st results_obj
;
1030 memcached_result_st
*results
;
1031 results
=memcached_result_create(memc
, &results_obj
);
1033 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1035 memcached_result_free(&results_obj
);
1037 /* Don't read out the second result, but issue a set instead.. */
1038 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1039 assert(rc
== MEMCACHED_SUCCESS
);
1041 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1042 &rlen
, &flags
, &rc
);
1043 assert(val
== NULL
);
1044 assert(rc
== MEMCACHED_NOTFOUND
);
1045 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1046 assert(val
!= NULL
);
1047 assert(rc
== MEMCACHED_SUCCESS
);
1050 return TEST_SUCCESS
;
1053 /* Do not copy the style of this code, I just access hosts to testthis function */
1054 static test_return
stats_servername_test(memcached_st
*memc
)
1056 memcached_return rc
;
1057 memcached_stat_st memc_stat
;
1058 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1059 memc
->hosts
[0].hostname
,
1060 memc
->hosts
[0].port
);
1065 static test_return
increment_test(memcached_st
*memc
)
1067 uint64_t new_number
;
1068 memcached_return rc
;
1069 const char *key
= "number";
1070 const char *value
= "0";
1072 rc
= memcached_set(memc
, key
, strlen(key
),
1073 value
, strlen(value
),
1074 (time_t)0, (uint32_t)0);
1075 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1077 rc
= memcached_increment(memc
, key
, strlen(key
),
1079 assert(rc
== MEMCACHED_SUCCESS
);
1080 assert(new_number
== 1);
1082 rc
= memcached_increment(memc
, key
, strlen(key
),
1084 assert(rc
== MEMCACHED_SUCCESS
);
1085 assert(new_number
== 2);
1090 static test_return
increment_with_initial_test(memcached_st
*memc
)
1092 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1094 uint64_t new_number
;
1095 memcached_return rc
;
1096 const char *key
= "number";
1097 uint64_t initial
= 0;
1099 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1100 1, initial
, 0, &new_number
);
1101 assert(rc
== MEMCACHED_SUCCESS
);
1102 assert(new_number
== initial
);
1104 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1105 1, initial
, 0, &new_number
);
1106 assert(rc
== MEMCACHED_SUCCESS
);
1107 assert(new_number
== (initial
+ 1));
1112 static test_return
decrement_test(memcached_st
*memc
)
1114 uint64_t new_number
;
1115 memcached_return rc
;
1116 const char *key
= "number";
1117 const char *value
= "3";
1119 rc
= memcached_set(memc
, key
, strlen(key
),
1120 value
, strlen(value
),
1121 (time_t)0, (uint32_t)0);
1122 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1124 rc
= memcached_decrement(memc
, key
, strlen(key
),
1126 assert(rc
== MEMCACHED_SUCCESS
);
1127 assert(new_number
== 2);
1129 rc
= memcached_decrement(memc
, key
, strlen(key
),
1131 assert(rc
== MEMCACHED_SUCCESS
);
1132 assert(new_number
== 1);
1137 static test_return
decrement_with_initial_test(memcached_st
*memc
)
1139 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1141 uint64_t new_number
;
1142 memcached_return rc
;
1143 const char *key
= "number";
1144 uint64_t initial
= 3;
1146 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1147 1, initial
, 0, &new_number
);
1148 assert(rc
== MEMCACHED_SUCCESS
);
1149 assert(new_number
== initial
);
1151 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1152 1, initial
, 0, &new_number
);
1153 assert(rc
== MEMCACHED_SUCCESS
);
1154 assert(new_number
== (initial
- 1));
1159 static test_return
quit_test(memcached_st
*memc
)
1161 memcached_return rc
;
1162 const char *key
= "fudge";
1163 const char *value
= "sanford and sun";
1165 rc
= memcached_set(memc
, key
, strlen(key
),
1166 value
, strlen(value
),
1167 (time_t)10, (uint32_t)3);
1168 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1169 memcached_quit(memc
);
1171 rc
= memcached_set(memc
, key
, strlen(key
),
1172 value
, strlen(value
),
1173 (time_t)50, (uint32_t)9);
1174 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1179 static test_return
mget_result_test(memcached_st
*memc
)
1181 memcached_return rc
;
1182 const char *keys
[]= {"fudge", "son", "food"};
1183 size_t key_length
[]= {5, 3, 4};
1186 memcached_result_st results_obj
;
1187 memcached_result_st
*results
;
1189 results
= memcached_result_create(memc
, &results_obj
);
1191 assert(&results_obj
== results
);
1193 /* We need to empty the server before continueing test */
1194 rc
= memcached_flush(memc
, 0);
1195 assert(rc
== MEMCACHED_SUCCESS
);
1197 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1198 assert(rc
== MEMCACHED_SUCCESS
);
1200 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1205 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1207 assert(rc
== MEMCACHED_END
);
1209 for (x
= 0; x
< 3; x
++)
1211 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1212 keys
[x
], key_length
[x
],
1213 (time_t)50, (uint32_t)9);
1214 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1217 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1218 assert(rc
== MEMCACHED_SUCCESS
);
1220 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1223 assert(&results_obj
== results
);
1224 assert(rc
== MEMCACHED_SUCCESS
);
1225 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1226 assert(!memcmp(memcached_result_key_value(results
),
1227 memcached_result_value(results
),
1228 memcached_result_length(results
)));
1231 memcached_result_free(&results_obj
);
1236 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1238 memcached_return rc
;
1239 const char *keys
[]= {"fudge", "son", "food"};
1240 size_t key_length
[]= {5, 3, 4};
1243 memcached_result_st
*results
;
1245 /* We need to empty the server before continueing test */
1246 rc
= memcached_flush(memc
, 0);
1247 assert(rc
== MEMCACHED_SUCCESS
);
1249 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1250 assert(rc
== MEMCACHED_SUCCESS
);
1252 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1257 assert(rc
== MEMCACHED_END
);
1259 for (x
= 0; x
< 3; x
++)
1261 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1262 keys
[x
], key_length
[x
],
1263 (time_t)50, (uint32_t)9);
1264 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1267 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1268 assert(rc
== MEMCACHED_SUCCESS
);
1271 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1274 assert(rc
== MEMCACHED_SUCCESS
);
1275 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1276 assert(!memcmp(memcached_result_key_value(results
),
1277 memcached_result_value(results
),
1278 memcached_result_length(results
)));
1279 memcached_result_free(results
);
1286 /* Count the results */
1287 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1288 memcached_result_st
*result
__attribute__((unused
)),
1291 unsigned int *counter
= (unsigned int *)context
;
1293 *counter
= *counter
+ 1;
1295 return MEMCACHED_SUCCESS
;
1298 static test_return
mget_result_function(memcached_st
*memc
)
1300 memcached_return rc
;
1301 const char *keys
[]= {"fudge", "son", "food"};
1302 size_t key_length
[]= {5, 3, 4};
1304 unsigned int counter
;
1305 memcached_execute_function callbacks
[1];
1307 /* We need to empty the server before continueing test */
1308 rc
= memcached_flush(memc
, 0);
1309 for (x
= 0; x
< 3; x
++)
1311 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1312 keys
[x
], key_length
[x
],
1313 (time_t)50, (uint32_t)9);
1314 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1317 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1318 assert(rc
== MEMCACHED_SUCCESS
);
1320 callbacks
[0]= &callback_counter
;
1322 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1324 assert(counter
== 3);
1329 static test_return
mget_test(memcached_st
*memc
)
1331 memcached_return rc
;
1332 const char *keys
[]= {"fudge", "son", "food"};
1333 size_t key_length
[]= {5, 3, 4};
1337 char return_key
[MEMCACHED_MAX_KEY
];
1338 size_t return_key_length
;
1340 size_t return_value_length
;
1342 /* We need to empty the server before continueing test */
1343 rc
= memcached_flush(memc
, 0);
1344 assert(rc
== MEMCACHED_SUCCESS
);
1346 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1347 assert(rc
== MEMCACHED_SUCCESS
);
1349 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1350 &return_value_length
, &flags
, &rc
)) != NULL
)
1352 assert(return_value
);
1354 assert(!return_value
);
1355 assert(return_value_length
== 0);
1356 assert(rc
== MEMCACHED_END
);
1358 for (x
= 0; x
< 3; x
++)
1360 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1361 keys
[x
], key_length
[x
],
1362 (time_t)50, (uint32_t)9);
1363 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1366 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1367 assert(rc
== MEMCACHED_SUCCESS
);
1370 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1371 &return_value_length
, &flags
, &rc
)))
1373 assert(return_value
);
1374 assert(rc
== MEMCACHED_SUCCESS
);
1375 assert(return_key_length
== return_value_length
);
1376 assert(!memcmp(return_value
, return_key
, return_value_length
));
1384 static test_return
get_stats_keys(memcached_st
*memc
)
1388 memcached_stat_st memc_stat
;
1389 memcached_return rc
;
1391 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1392 assert(rc
== MEMCACHED_SUCCESS
);
1393 for (ptr
= list
; *ptr
; ptr
++)
1402 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1404 const char *version_string
;
1406 version_string
= memcached_lib_version();
1408 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1413 static test_return
get_stats(memcached_st
*memc
)
1418 memcached_return rc
;
1419 memcached_stat_st
*memc_stat
;
1421 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1422 assert(rc
== MEMCACHED_SUCCESS
);
1424 assert(rc
== MEMCACHED_SUCCESS
);
1427 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1429 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1430 assert(rc
== MEMCACHED_SUCCESS
);
1431 for (ptr
= list
; *ptr
; ptr
++);
1436 memcached_stat_free(NULL
, memc_stat
);
1441 static test_return
add_host_test(memcached_st
*memc
)
1444 memcached_server_st
*servers
;
1445 memcached_return rc
;
1446 char servername
[]= "0.example.com";
1448 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1450 assert(1 == memcached_server_list_count(servers
));
1452 for (x
= 2; x
< 20; x
++)
1454 char buffer
[SMALL_STRING_LEN
];
1456 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1457 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1459 assert(rc
== MEMCACHED_SUCCESS
);
1460 assert(x
== memcached_server_list_count(servers
));
1463 rc
= memcached_server_push(memc
, servers
);
1464 assert(rc
== MEMCACHED_SUCCESS
);
1465 rc
= memcached_server_push(memc
, servers
);
1466 assert(rc
== MEMCACHED_SUCCESS
);
1468 memcached_server_list_free(servers
);
1473 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1475 return MEMCACHED_SUCCESS
;
1478 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1480 return MEMCACHED_SUCCESS
;
1483 static test_return
callback_test(memcached_st
*memc
)
1485 /* Test User Data */
1489 memcached_return rc
;
1491 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1492 assert(rc
== MEMCACHED_SUCCESS
);
1493 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1494 assert(*test_ptr
== x
);
1497 /* Test Clone Callback */
1499 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1500 void *clone_cb_ptr
= *(void **)&clone_cb
;
1501 void *temp_function
= NULL
;
1502 memcached_return rc
;
1504 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1506 assert(rc
== MEMCACHED_SUCCESS
);
1507 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1508 assert(temp_function
== clone_cb_ptr
);
1511 /* Test Cleanup Callback */
1513 memcached_cleanup_func cleanup_cb
=
1514 (memcached_cleanup_func
)cleanup_test_callback
;
1515 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1516 void *temp_function
= NULL
;
1517 memcached_return rc
;
1519 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1521 assert(rc
== MEMCACHED_SUCCESS
);
1522 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1523 assert(temp_function
== cleanup_cb_ptr
);
1529 /* We don't test the behavior itself, we test the switches */
1530 static test_return
behavior_test(memcached_st
*memc
)
1535 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1536 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1539 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1540 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1543 set
= MEMCACHED_HASH_MD5
;
1544 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1545 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1546 assert(value
== MEMCACHED_HASH_MD5
);
1550 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1551 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1554 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1555 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1558 set
= MEMCACHED_HASH_DEFAULT
;
1559 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1560 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1561 assert(value
== MEMCACHED_HASH_DEFAULT
);
1563 set
= MEMCACHED_HASH_CRC
;
1564 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1565 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1566 assert(value
== MEMCACHED_HASH_CRC
);
1568 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1571 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1574 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1575 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1576 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1580 /* Test case provided by Cal Haldenbrand */
1581 static test_return
user_supplied_bug1(memcached_st
*memc
)
1583 unsigned int setter
= 1;
1586 unsigned long long total
= 0;
1589 char randomstuff
[6 * 1024];
1590 memcached_return rc
;
1592 memset(randomstuff
, 0, 6 * 1024);
1594 /* We just keep looking at the same values over and over */
1597 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1598 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1602 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1606 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1607 memset(randomstuff
, 0, 6 * 1024);
1608 assert(size
< 6 * 1024); /* Being safe here */
1610 for (j
= 0 ; j
< size
;j
++)
1611 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1614 sprintf(key
, "%d", x
);
1615 rc
= memcached_set(memc
, key
, strlen(key
),
1616 randomstuff
, strlen(randomstuff
), 10, 0);
1617 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1618 /* If we fail, lets try again */
1619 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1620 rc
= memcached_set(memc
, key
, strlen(key
),
1621 randomstuff
, strlen(randomstuff
), 10, 0);
1622 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1628 /* Test case provided by Cal Haldenbrand */
1629 static test_return
user_supplied_bug2(memcached_st
*memc
)
1632 unsigned int setter
;
1634 unsigned long long total
;
1637 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1638 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1640 setter
= 20 * 1024576;
1641 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1642 setter
= 20 * 1024576;
1643 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1644 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1645 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1647 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1650 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1652 memcached_return rc
= MEMCACHED_SUCCESS
;
1653 char buffer
[SMALL_STRING_LEN
];
1658 memset(buffer
, 0, SMALL_STRING_LEN
);
1660 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1661 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1662 &val_len
, &flags
, &rc
);
1663 if (rc
!= MEMCACHED_SUCCESS
)
1665 if (rc
== MEMCACHED_NOTFOUND
)
1682 /* Do a large mget() over all the keys we think exist */
1683 #define KEY_COUNT 3000 // * 1024576
1684 static test_return
user_supplied_bug3(memcached_st
*memc
)
1686 memcached_return rc
;
1687 unsigned int setter
;
1690 size_t key_lengths
[KEY_COUNT
];
1693 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1694 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1696 setter
= 20 * 1024576;
1697 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1698 setter
= 20 * 1024576;
1699 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1700 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1701 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1704 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1706 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1707 for (x
= 0; x
< KEY_COUNT
; x
++)
1711 snprintf(buffer
, 30, "%u", x
);
1712 keys
[x
]= strdup(buffer
);
1713 key_lengths
[x
]= strlen(keys
[x
]);
1716 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
1717 assert(rc
== MEMCACHED_SUCCESS
);
1719 /* Turn this into a help function */
1721 char return_key
[MEMCACHED_MAX_KEY
];
1722 size_t return_key_length
;
1724 size_t return_value_length
;
1727 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1728 &return_value_length
, &flags
, &rc
)))
1730 assert(return_value
);
1731 assert(rc
== MEMCACHED_SUCCESS
);
1736 for (x
= 0; x
< KEY_COUNT
; x
++)
1743 /* Make sure we behave properly if server list has no values */
1744 static test_return
user_supplied_bug4(memcached_st
*memc
)
1746 memcached_return rc
;
1747 const char *keys
[]= {"fudge", "son", "food"};
1748 size_t key_length
[]= {5, 3, 4};
1751 char return_key
[MEMCACHED_MAX_KEY
];
1752 size_t return_key_length
;
1754 size_t return_value_length
;
1756 /* Here we free everything before running a bunch of mget tests */
1758 memcached_server_list_free(memc
->hosts
);
1760 memc
->number_of_hosts
= 0;
1764 /* We need to empty the server before continueing test */
1765 rc
= memcached_flush(memc
, 0);
1766 assert(rc
== MEMCACHED_NO_SERVERS
);
1768 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1769 assert(rc
== MEMCACHED_NO_SERVERS
);
1771 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1772 &return_value_length
, &flags
, &rc
)) != NULL
)
1774 assert(return_value
);
1776 assert(!return_value
);
1777 assert(return_value_length
== 0);
1778 assert(rc
== MEMCACHED_NO_SERVERS
);
1780 for (x
= 0; x
< 3; x
++)
1782 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1783 keys
[x
], key_length
[x
],
1784 (time_t)50, (uint32_t)9);
1785 assert(rc
== MEMCACHED_NO_SERVERS
);
1788 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1789 assert(rc
== MEMCACHED_NO_SERVERS
);
1792 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1793 &return_value_length
, &flags
, &rc
)))
1795 assert(return_value
);
1796 assert(rc
== MEMCACHED_SUCCESS
);
1797 assert(return_key_length
== return_value_length
);
1798 assert(!memcmp(return_value
, return_key
, return_value_length
));
1806 #define VALUE_SIZE_BUG5 1048064
1807 static test_return
user_supplied_bug5(memcached_st
*memc
)
1809 memcached_return rc
;
1810 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1811 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1812 char return_key
[MEMCACHED_MAX_KEY
];
1813 size_t return_key_length
;
1815 size_t value_length
;
1819 char insert_data
[VALUE_SIZE_BUG5
];
1821 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1822 insert_data
[x
]= (signed char)rand();
1824 memcached_flush(memc
, 0);
1825 value
= memcached_get(memc
, keys
[0], key_length
[0],
1826 &value_length
, &flags
, &rc
);
1827 assert(value
== NULL
);
1828 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1831 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1832 &value_length
, &flags
, &rc
)))
1836 for (x
= 0; x
< 4; x
++)
1838 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1839 insert_data
, VALUE_SIZE_BUG5
,
1840 (time_t)0, (uint32_t)0);
1841 assert(rc
== MEMCACHED_SUCCESS
);
1844 for (x
= 0; x
< 10; x
++)
1846 value
= memcached_get(memc
, keys
[0], key_length
[0],
1847 &value_length
, &flags
, &rc
);
1851 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1853 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1854 &value_length
, &flags
, &rc
)))
1865 static test_return
user_supplied_bug6(memcached_st
*memc
)
1867 memcached_return rc
;
1868 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1869 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1870 char return_key
[MEMCACHED_MAX_KEY
];
1871 size_t return_key_length
;
1873 size_t value_length
;
1877 char insert_data
[VALUE_SIZE_BUG5
];
1879 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1880 insert_data
[x
]= (signed char)rand();
1882 memcached_flush(memc
, 0);
1883 value
= memcached_get(memc
, keys
[0], key_length
[0],
1884 &value_length
, &flags
, &rc
);
1885 assert(value
== NULL
);
1886 assert(rc
== MEMCACHED_NOTFOUND
);
1887 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1888 assert(rc
== MEMCACHED_SUCCESS
);
1891 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1892 &value_length
, &flags
, &rc
)))
1895 assert(rc
== MEMCACHED_END
);
1897 for (x
= 0; x
< 4; x
++)
1899 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1900 insert_data
, VALUE_SIZE_BUG5
,
1901 (time_t)0, (uint32_t)0);
1902 assert(rc
== MEMCACHED_SUCCESS
);
1905 for (x
= 0; x
< 2; x
++)
1907 value
= memcached_get(memc
, keys
[0], key_length
[0],
1908 &value_length
, &flags
, &rc
);
1912 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1913 assert(rc
== MEMCACHED_SUCCESS
);
1915 /* We test for purge of partial complete fetches */
1916 for (count
= 3; count
; count
--)
1918 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1919 &value_length
, &flags
, &rc
);
1920 assert(rc
== MEMCACHED_SUCCESS
);
1921 assert(!(memcmp(value
, insert_data
, value_length
)));
1922 assert(value_length
);
1930 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1932 memcached_return rc
;
1934 memcached_st
*memc_clone
;
1936 memcached_server_st
*servers
;
1937 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";
1939 servers
= memcached_servers_parse(server_list
);
1942 mine
= memcached_create(NULL
);
1943 rc
= memcached_server_push(mine
, servers
);
1944 assert(rc
== MEMCACHED_SUCCESS
);
1945 memcached_server_list_free(servers
);
1948 memc_clone
= memcached_clone(NULL
, mine
);
1950 memcached_quit(mine
);
1951 memcached_quit(memc_clone
);
1954 memcached_free(mine
);
1955 memcached_free(memc_clone
);
1960 /* Test flag store/retrieve */
1961 static test_return
user_supplied_bug7(memcached_st
*memc
)
1963 memcached_return rc
;
1964 const char *keys
= "036790384900";
1965 size_t key_length
= strlen(keys
);
1966 char return_key
[MEMCACHED_MAX_KEY
];
1967 size_t return_key_length
;
1969 size_t value_length
;
1972 char insert_data
[VALUE_SIZE_BUG5
];
1974 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1975 insert_data
[x
]= (signed char)rand();
1977 memcached_flush(memc
, 0);
1980 rc
= memcached_set(memc
, keys
, key_length
,
1981 insert_data
, VALUE_SIZE_BUG5
,
1983 assert(rc
== MEMCACHED_SUCCESS
);
1986 value
= memcached_get(memc
, keys
, key_length
,
1987 &value_length
, &flags
, &rc
);
1988 assert(flags
== 245);
1992 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
1995 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1996 &value_length
, &flags
, &rc
);
1997 assert(flags
== 245);
2005 static test_return
user_supplied_bug9(memcached_st
*memc
)
2007 memcached_return rc
;
2008 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2009 size_t key_length
[3];
2014 char return_key
[MEMCACHED_MAX_KEY
];
2015 size_t return_key_length
;
2017 size_t return_value_length
;
2020 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2021 key_length
[1]= strlen("fudge&*@#");
2022 key_length
[2]= strlen("for^#@&$not");
2025 for (x
= 0; x
< 3; x
++)
2027 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2028 keys
[x
], key_length
[x
],
2029 (time_t)50, (uint32_t)9);
2030 assert(rc
== MEMCACHED_SUCCESS
);
2033 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2034 assert(rc
== MEMCACHED_SUCCESS
);
2036 /* We need to empty the server before continueing test */
2037 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2038 &return_value_length
, &flags
, &rc
)) != NULL
)
2040 assert(return_value
);
2049 /* We are testing with aggressive timeout to get failures */
2050 static test_return
user_supplied_bug10(memcached_st
*memc
)
2052 const char *key
= "foo";
2054 size_t value_length
= 512;
2057 memcached_return rc
;
2058 unsigned int set
= 1;
2059 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2062 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2063 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2065 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2068 value
= (char*)malloc(value_length
* sizeof(char));
2070 for (x
= 0; x
< value_length
; x
++)
2071 value
[x
]= (char) (x
% 127);
2073 for (x
= 1; x
<= 100000; ++x
)
2075 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2077 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2078 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2080 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2085 memcached_free(mclone
);
2091 We are looking failures in the async protocol
2093 static test_return
user_supplied_bug11(memcached_st
*memc
)
2095 const char *key
= "foo";
2097 size_t value_length
= 512;
2100 memcached_return rc
;
2101 unsigned int set
= 1;
2103 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2105 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2106 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2108 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2111 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2113 assert(timeout
== -1);
2115 value
= (char*)malloc(value_length
* sizeof(char));
2117 for (x
= 0; x
< value_length
; x
++)
2118 value
[x
]= (char) (x
% 127);
2120 for (x
= 1; x
<= 100000; ++x
)
2122 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2126 memcached_free(mclone
);
2132 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2134 static test_return
user_supplied_bug12(memcached_st
*memc
)
2136 memcached_return rc
;
2138 size_t value_length
;
2140 uint64_t number_value
;
2142 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2143 &value_length
, &flags
, &rc
);
2144 assert(value
== NULL
);
2145 assert(rc
== MEMCACHED_NOTFOUND
);
2147 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2150 assert(value
== NULL
);
2151 /* The binary protocol will set the key if it doesn't exist */
2152 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2153 assert(rc
== MEMCACHED_SUCCESS
);
2155 assert(rc
== MEMCACHED_NOTFOUND
);
2157 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2159 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2160 &value_length
, &flags
, &rc
);
2162 assert(rc
== MEMCACHED_SUCCESS
);
2165 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2167 assert(number_value
== 2);
2168 assert(rc
== MEMCACHED_SUCCESS
);
2174 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2175 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2177 static test_return
user_supplied_bug13(memcached_st
*memc
)
2179 char key
[] = "key34567890";
2181 memcached_return rc
;
2182 size_t overflowSize
;
2184 char commandFirst
[]= "set key34567890 0 0 ";
2185 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2186 size_t commandLength
;
2189 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2191 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2193 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2195 overflow
= malloc(testSize
);
2196 assert(overflow
!= NULL
);
2198 memset(overflow
, 'x', testSize
);
2199 rc
= memcached_set(memc
, key
, strlen(key
),
2200 overflow
, testSize
, 0, 0);
2201 assert(rc
== MEMCACHED_SUCCESS
);
2210 Test values of many different sizes
2211 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2212 set key34567890 0 0 8169 \r\n
2213 is sent followed by buffer of size 8169, followed by 8169
2215 static test_return
user_supplied_bug14(memcached_st
*memc
)
2218 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2219 memcached_return rc
;
2220 const char *key
= "foo";
2222 size_t value_length
= 18000;
2224 size_t string_length
;
2227 size_t current_length
;
2229 value
= (char*)malloc(value_length
);
2232 for (x
= 0; x
< value_length
; x
++)
2233 value
[x
] = (char) (x
% 127);
2235 for (current_length
= 0; current_length
< value_length
; current_length
++)
2237 rc
= memcached_set(memc
, key
, strlen(key
),
2238 value
, current_length
,
2239 (time_t)0, (uint32_t)0);
2240 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2242 string
= memcached_get(memc
, key
, strlen(key
),
2243 &string_length
, &flags
, &rc
);
2245 assert(rc
== MEMCACHED_SUCCESS
);
2246 assert(string_length
== current_length
);
2247 assert(!memcmp(string
, value
, string_length
));
2258 Look for zero length value problems
2260 static test_return
user_supplied_bug15(memcached_st
*memc
)
2263 memcached_return rc
;
2264 const char *key
= "mykey";
2269 for (x
= 0; x
< 2; x
++)
2271 rc
= memcached_set(memc
, key
, strlen(key
),
2273 (time_t)0, (uint32_t)0);
2275 assert(rc
== MEMCACHED_SUCCESS
);
2277 value
= memcached_get(memc
, key
, strlen(key
),
2278 &length
, &flags
, &rc
);
2280 assert(rc
== MEMCACHED_SUCCESS
);
2281 assert(value
== NULL
);
2282 assert(length
== 0);
2285 value
= memcached_get(memc
, key
, strlen(key
),
2286 &length
, &flags
, &rc
);
2288 assert(rc
== MEMCACHED_SUCCESS
);
2289 assert(value
== NULL
);
2290 assert(length
== 0);
2297 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2298 static test_return
user_supplied_bug16(memcached_st
*memc
)
2300 memcached_return rc
;
2301 const char *key
= "mykey";
2306 rc
= memcached_set(memc
, key
, strlen(key
),
2308 (time_t)0, UINT32_MAX
);
2310 assert(rc
== MEMCACHED_SUCCESS
);
2312 value
= memcached_get(memc
, key
, strlen(key
),
2313 &length
, &flags
, &rc
);
2315 assert(rc
== MEMCACHED_SUCCESS
);
2316 assert(value
== NULL
);
2317 assert(length
== 0);
2318 assert(flags
== UINT32_MAX
);
2324 /* Check the validity of chinese key*/
2325 static test_return
user_supplied_bug17(memcached_st
*memc
)
2327 memcached_return rc
;
2328 const char *key
= "豆瓣";
2329 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2334 rc
= memcached_set(memc
, key
, strlen(key
),
2335 value
, strlen(value
),
2338 assert(rc
== MEMCACHED_SUCCESS
);
2340 value2
= memcached_get(memc
, key
, strlen(key
),
2341 &length
, &flags
, &rc
);
2343 assert(length
==strlen(value
));
2344 assert(rc
== MEMCACHED_SUCCESS
);
2345 assert(memcmp(value
, value2
, length
)==0);
2356 static test_return
user_supplied_bug19(memcached_st
*memc
)
2359 memcached_server_st
*s
;
2360 memcached_return res
;
2364 m
= memcached_create(NULL
);
2365 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2366 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2368 s
= memcached_server_by_key(m
, "a", 1, &res
);
2369 memcached_server_free(s
);
2376 /* CAS test from Andei */
2377 static test_return
user_supplied_bug20(memcached_st
*memc
)
2379 memcached_return status
;
2380 memcached_result_st
*result
, result_obj
;
2381 const char *key
= "abc";
2382 size_t key_len
= strlen("abc");
2383 const char *value
= "foobar";
2384 size_t value_len
= strlen(value
);
2386 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2388 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2389 assert(status
== MEMCACHED_SUCCESS
);
2391 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2392 assert(status
== MEMCACHED_SUCCESS
);
2394 result
= memcached_result_create(memc
, &result_obj
);
2397 memcached_result_create(memc
, &result_obj
);
2398 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2401 assert(status
== MEMCACHED_SUCCESS
);
2403 memcached_result_free(result
);
2408 #include "ketama_test_cases.h"
2409 static test_return
user_supplied_bug18(memcached_st
*trash
)
2411 memcached_return rc
;
2414 memcached_server_st
*server_pool
;
2419 memc
= memcached_create(NULL
);
2422 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2423 assert(rc
== MEMCACHED_SUCCESS
);
2425 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2428 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2429 assert(rc
== MEMCACHED_SUCCESS
);
2431 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2432 assert(value
== MEMCACHED_HASH_MD5
);
2434 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");
2435 memcached_server_push(memc
, server_pool
);
2437 /* verify that the server list was parsed okay. */
2438 assert(memc
->number_of_hosts
== 8);
2439 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2440 assert(server_pool
[0].port
== 11211);
2441 assert(server_pool
[0].weight
== 600);
2442 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2443 assert(server_pool
[2].port
== 11211);
2444 assert(server_pool
[2].weight
== 200);
2445 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2446 assert(server_pool
[7].port
== 11211);
2447 assert(server_pool
[7].weight
== 100);
2449 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2450 * us test the boundary wraparound.
2452 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2454 /* verify the standard ketama set. */
2455 for (x
= 0; x
< 99; x
++)
2457 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2458 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2459 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2462 memcached_server_list_free(server_pool
);
2463 memcached_free(memc
);
2468 static test_return
auto_eject_hosts(memcached_st
*trash
)
2472 memcached_return rc
;
2473 memcached_st
*memc
= memcached_create(NULL
);
2476 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2477 assert(rc
== MEMCACHED_SUCCESS
);
2479 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2482 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2483 assert(rc
== MEMCACHED_SUCCESS
);
2485 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2486 assert(value
== MEMCACHED_HASH_MD5
);
2488 /* server should be removed when in delay */
2489 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2490 assert(rc
== MEMCACHED_SUCCESS
);
2492 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2495 memcached_server_st
*server_pool
;
2496 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");
2497 memcached_server_push(memc
, server_pool
);
2499 /* verify that the server list was parsed okay. */
2500 assert(memc
->number_of_hosts
== 8);
2501 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2502 assert(server_pool
[0].port
== 11211);
2503 assert(server_pool
[0].weight
== 600);
2504 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2505 assert(server_pool
[2].port
== 11211);
2506 assert(server_pool
[2].weight
== 200);
2507 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2508 assert(server_pool
[7].port
== 11211);
2509 assert(server_pool
[7].weight
== 100);
2511 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2512 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2514 for (int x
= 0; x
< 99; x
++)
2516 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2517 assert(server_idx
!= 2);
2520 /* and re-added when it's back. */
2521 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2522 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2523 run_distribution(memc
);
2524 for (int x
= 0; x
< 99; x
++)
2526 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2527 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2528 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2531 memcached_server_list_free(server_pool
);
2532 memcached_free(memc
);
2534 return TEST_SUCCESS
;
2537 static test_return
result_static(memcached_st
*memc
)
2539 memcached_result_st result
;
2540 memcached_result_st
*result_ptr
;
2542 result_ptr
= memcached_result_create(memc
, &result
);
2543 assert(result
.is_allocated
== false);
2545 memcached_result_free(&result
);
2550 static test_return
result_alloc(memcached_st
*memc
)
2552 memcached_result_st
*result
;
2554 result
= memcached_result_create(memc
, NULL
);
2556 memcached_result_free(result
);
2561 static test_return
string_static_null(memcached_st
*memc
)
2563 memcached_string_st string
;
2564 memcached_string_st
*string_ptr
;
2566 string_ptr
= memcached_string_create(memc
, &string
, 0);
2567 assert(string
.is_allocated
== false);
2569 memcached_string_free(&string
);
2574 static test_return
string_alloc_null(memcached_st
*memc
)
2576 memcached_string_st
*string
;
2578 string
= memcached_string_create(memc
, NULL
, 0);
2580 memcached_string_free(string
);
2585 static test_return
string_alloc_with_size(memcached_st
*memc
)
2587 memcached_string_st
*string
;
2589 string
= memcached_string_create(memc
, NULL
, 1024);
2591 memcached_string_free(string
);
2596 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2598 memcached_string_st
*string
;
2600 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2601 assert(string
== NULL
);
2606 static test_return
string_alloc_append(memcached_st
*memc
)
2609 char buffer
[SMALL_STRING_LEN
];
2610 memcached_string_st
*string
;
2612 /* Ring the bell! */
2613 memset(buffer
, 6, SMALL_STRING_LEN
);
2615 string
= memcached_string_create(memc
, NULL
, 100);
2618 for (x
= 0; x
< 1024; x
++)
2620 memcached_return rc
;
2621 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2622 assert(rc
== MEMCACHED_SUCCESS
);
2624 memcached_string_free(string
);
2629 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2631 memcached_return rc
;
2633 char buffer
[SMALL_STRING_LEN
];
2634 memcached_string_st
*string
;
2636 /* Ring the bell! */
2637 memset(buffer
, 6, SMALL_STRING_LEN
);
2639 string
= memcached_string_create(memc
, NULL
, 100);
2642 for (x
= 0; x
< 1024; x
++)
2644 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2645 assert(rc
== MEMCACHED_SUCCESS
);
2647 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
2648 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2649 memcached_string_free(string
);
2654 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2656 pairs_free(global_pairs
);
2661 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2663 unsigned long long x
;
2664 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2665 global_count
= GLOBAL_COUNT
;
2667 for (x
= 0; x
< global_count
; x
++)
2669 global_keys
[x
]= global_pairs
[x
].key
;
2670 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2676 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2678 unsigned long long x
;
2679 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2680 global_count
= GLOBAL2_COUNT
;
2682 for (x
= 0; x
< global_count
; x
++)
2684 global_keys
[x
]= global_pairs
[x
].key
;
2685 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2691 static test_return
generate_data(memcached_st
*memc
)
2693 execute_set(memc
, global_pairs
, global_count
);
2698 static test_return
generate_data_with_stats(memcached_st
*memc
)
2700 memcached_stat_st
*stat_p
;
2701 memcached_return rc
;
2702 uint32_t host_index
= 0;
2703 execute_set(memc
, global_pairs
, global_count
);
2705 //TODO: hosts used size stats
2706 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2709 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2711 /* This test was changes so that "make test" would work properlly */
2713 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
);
2715 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
2718 memcached_stat_free(NULL
, stat_p
);
2722 static test_return
generate_buffer_data(memcached_st
*memc
)
2727 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2728 generate_data(memc
);
2733 static test_return
get_read_count(memcached_st
*memc
)
2736 memcached_return rc
;
2737 memcached_st
*memc_clone
;
2739 memc_clone
= memcached_clone(NULL
, memc
);
2742 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
2746 size_t return_value_length
;
2750 for (x
= count
= 0; x
< global_count
; x
++)
2752 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
2753 &return_value_length
, &flags
, &rc
);
2754 if (rc
== MEMCACHED_SUCCESS
)
2761 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2764 memcached_free(memc_clone
);
2769 static test_return
get_read(memcached_st
*memc
)
2772 memcached_return rc
;
2776 size_t return_value_length
;
2779 for (x
= 0; x
< global_count
; x
++)
2781 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2782 &return_value_length
, &flags
, &rc
);
2784 assert(return_value);
2785 assert(rc == MEMCACHED_SUCCESS);
2787 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2795 static test_return
mget_read(memcached_st
*memc
)
2797 memcached_return rc
;
2799 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2800 assert(rc
== MEMCACHED_SUCCESS
);
2801 /* Turn this into a help function */
2803 char return_key
[MEMCACHED_MAX_KEY
];
2804 size_t return_key_length
;
2806 size_t return_value_length
;
2809 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2810 &return_value_length
, &flags
, &rc
)))
2812 assert(return_value
);
2813 assert(rc
== MEMCACHED_SUCCESS
);
2821 static test_return
mget_read_result(memcached_st
*memc
)
2823 memcached_return rc
;
2825 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2826 assert(rc
== MEMCACHED_SUCCESS
);
2827 /* Turn this into a help function */
2829 memcached_result_st results_obj
;
2830 memcached_result_st
*results
;
2832 results
= memcached_result_create(memc
, &results_obj
);
2834 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2837 assert(rc
== MEMCACHED_SUCCESS
);
2840 memcached_result_free(&results_obj
);
2846 static test_return
mget_read_function(memcached_st
*memc
)
2848 memcached_return rc
;
2849 unsigned int counter
;
2850 memcached_execute_function callbacks
[1];
2852 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2853 assert(rc
== MEMCACHED_SUCCESS
);
2855 callbacks
[0]= &callback_counter
;
2857 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2862 static test_return
delete_generate(memcached_st
*memc
)
2866 for (x
= 0; x
< global_count
; x
++)
2868 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2874 static test_return
delete_buffer_generate(memcached_st
*memc
)
2880 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2882 for (x
= 0; x
< global_count
; x
++)
2884 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2890 static test_return
add_host_test1(memcached_st
*memc
)
2893 memcached_return rc
;
2894 char servername
[]= "0.example.com";
2895 memcached_server_st
*servers
;
2897 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2899 assert(1 == memcached_server_list_count(servers
));
2901 for (x
= 2; x
< 20; x
++)
2903 char buffer
[SMALL_STRING_LEN
];
2905 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2906 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2908 assert(rc
== MEMCACHED_SUCCESS
);
2909 assert(x
== memcached_server_list_count(servers
));
2912 rc
= memcached_server_push(memc
, servers
);
2913 assert(rc
== MEMCACHED_SUCCESS
);
2914 rc
= memcached_server_push(memc
, servers
);
2915 assert(rc
== MEMCACHED_SUCCESS
);
2917 memcached_server_list_free(servers
);
2922 static memcached_return
pre_nonblock(memcached_st
*memc
)
2924 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2926 return MEMCACHED_SUCCESS
;
2929 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
2931 memcached_return rc
= MEMCACHED_FAILURE
;
2932 memcached_st
*memc_clone
;
2934 memc_clone
= memcached_clone(NULL
, memc
);
2936 // The memcached_version needs to be done on a clone, because the server
2937 // will not toggle protocol on an connection.
2938 memcached_version(memc_clone
);
2940 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
2942 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2943 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2944 assert(rc
== MEMCACHED_SUCCESS
);
2945 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
2948 memcached_free(memc_clone
);
2952 static memcached_return
pre_murmur(memcached_st
*memc
)
2954 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
2956 return MEMCACHED_SUCCESS
;
2959 static memcached_return
pre_jenkins(memcached_st
*memc
)
2961 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
2963 return MEMCACHED_SUCCESS
;
2967 static memcached_return
pre_md5(memcached_st
*memc
)
2969 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
2971 return MEMCACHED_SUCCESS
;
2974 static memcached_return
pre_crc(memcached_st
*memc
)
2976 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
2978 return MEMCACHED_SUCCESS
;
2981 static memcached_return
pre_hsieh(memcached_st
*memc
)
2983 #ifdef HAVE_HSIEH_HASH
2984 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
2985 return MEMCACHED_SUCCESS
;
2988 return MEMCACHED_FAILURE
;
2992 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
2994 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
2996 return MEMCACHED_SUCCESS
;
2999 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
3001 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3003 return MEMCACHED_SUCCESS
;
3006 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
3008 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3010 return MEMCACHED_SUCCESS
;
3013 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
3015 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3017 return MEMCACHED_SUCCESS
;
3020 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3022 memcached_return rc
;
3025 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3026 assert(rc
== MEMCACHED_SUCCESS
);
3028 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3031 return MEMCACHED_SUCCESS
;
3034 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3036 memcached_return rc
;
3039 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3040 assert(rc
== MEMCACHED_SUCCESS
);
3042 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3045 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3046 assert(rc
== MEMCACHED_SUCCESS
);
3048 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3049 assert(value
== MEMCACHED_HASH_MD5
);
3050 return MEMCACHED_SUCCESS
;
3053 static memcached_return
pre_binary(memcached_st
*memc
)
3055 memcached_return rc
= MEMCACHED_FAILURE
;
3056 memcached_st
*memc_clone
;
3058 memc_clone
= memcached_clone(NULL
, memc
);
3060 // The memcached_version needs to be done on a clone, because the server
3061 // will not toggle protocol on an connection.
3062 memcached_version(memc_clone
);
3064 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3066 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3067 assert(rc
== MEMCACHED_SUCCESS
);
3068 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3071 memcached_free(memc_clone
);
3075 static memcached_return
pre_replication(memcached_st
*memc
)
3077 memcached_return rc
= MEMCACHED_FAILURE
;
3078 if (pre_binary(memc
) == MEMCACHED_SUCCESS
)
3081 * Make sure that we store the item on all servers
3082 * (master + replicas == number of servers)
3084 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3085 memc
->number_of_hosts
- 1);
3086 assert(rc
== MEMCACHED_SUCCESS
);
3087 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3093 static memcached_return
pre_replication_noblock(memcached_st
*memc
)
3095 memcached_return rc
= MEMCACHED_FAILURE
;
3096 if (pre_replication(memc
) == MEMCACHED_SUCCESS
&&
3097 pre_nonblock(memc
) == MEMCACHED_SUCCESS
)
3098 rc
= MEMCACHED_SUCCESS
;
3103 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3108 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3110 void *ret
= malloc(size
);
3112 memset(ret
, 0xff, size
);
3117 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3119 return realloc(mem
, size
);
3122 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3124 return calloc(nelem
, size
);
3127 static memcached_return
set_prefix(memcached_st
*memc
)
3129 memcached_return rc
;
3130 const char *key
= "mine";
3133 /* Make sure be default none exists */
3134 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3135 assert(rc
== MEMCACHED_FAILURE
);
3137 /* Test a clean set */
3138 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3139 assert(rc
== MEMCACHED_SUCCESS
);
3141 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3142 assert(memcmp(value
, key
, 4) == 0);
3143 assert(rc
== MEMCACHED_SUCCESS
);
3145 /* Test that we can turn it off */
3146 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3147 assert(rc
== MEMCACHED_SUCCESS
);
3149 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3150 assert(rc
== MEMCACHED_FAILURE
);
3152 /* Now setup for main test */
3153 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3154 assert(rc
== MEMCACHED_SUCCESS
);
3156 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3157 assert(rc
== MEMCACHED_SUCCESS
);
3158 assert(memcmp(value
, key
, 4) == 0);
3160 /* Set to Zero, and then Set to something too large */
3163 memset(long_key
, 0, 255);
3165 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3166 assert(rc
== MEMCACHED_SUCCESS
);
3168 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3169 assert(rc
== MEMCACHED_FAILURE
);
3170 assert(value
== NULL
);
3172 /* Test a long key for failure */
3173 /* TODO, extend test to determine based on setting, what result should be */
3174 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3175 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3176 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3177 assert(rc
== MEMCACHED_SUCCESS
);
3179 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3180 strcpy(long_key
, "This is more then the allotted number of characters");
3181 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3182 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3184 /* Test for a bad prefix, but with a short key */
3185 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3186 assert(rc
== MEMCACHED_SUCCESS
);
3188 strcpy(long_key
, "dog cat");
3189 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3190 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3193 return MEMCACHED_SUCCESS
;
3196 #ifdef MEMCACHED_ENABLE_DEPRECATED
3197 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3199 void *test_ptr
= NULL
;
3202 memcached_malloc_function malloc_cb
=
3203 (memcached_malloc_function
)my_malloc
;
3204 cb_ptr
= *(void **)&malloc_cb
;
3205 memcached_return rc
;
3207 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3208 assert(rc
== MEMCACHED_SUCCESS
);
3209 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3210 assert(rc
== MEMCACHED_SUCCESS
);
3211 assert(test_ptr
== cb_ptr
);
3215 memcached_realloc_function realloc_cb
=
3216 (memcached_realloc_function
)my_realloc
;
3217 cb_ptr
= *(void **)&realloc_cb
;
3218 memcached_return rc
;
3220 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3221 assert(rc
== MEMCACHED_SUCCESS
);
3222 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3223 assert(rc
== MEMCACHED_SUCCESS
);
3224 assert(test_ptr
== cb_ptr
);
3228 memcached_free_function free_cb
=
3229 (memcached_free_function
)my_free
;
3230 cb_ptr
= *(void **)&free_cb
;
3231 memcached_return rc
;
3233 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3234 assert(rc
== MEMCACHED_SUCCESS
);
3235 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3236 assert(rc
== MEMCACHED_SUCCESS
);
3237 assert(test_ptr
== cb_ptr
);
3239 return MEMCACHED_SUCCESS
;
3243 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3245 memcached_return rc
;
3246 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3247 my_realloc
, my_calloc
);
3248 assert(rc
== MEMCACHED_FAILURE
);
3250 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3251 my_realloc
, my_calloc
);
3253 memcached_malloc_function mem_malloc
;
3254 memcached_free_function mem_free
;
3255 memcached_realloc_function mem_realloc
;
3256 memcached_calloc_function mem_calloc
;
3257 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3258 &mem_realloc
, &mem_calloc
);
3260 assert(mem_malloc
== my_malloc
);
3261 assert(mem_realloc
== my_realloc
);
3262 assert(mem_calloc
== my_calloc
);
3263 assert(mem_free
== my_free
);
3265 return MEMCACHED_SUCCESS
;
3268 static memcached_return
enable_consistent(memcached_st
*memc
)
3270 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3271 memcached_hash hash
;
3272 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3273 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3274 return MEMCACHED_FAILURE
;
3276 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3277 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3279 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3280 assert(hash
== MEMCACHED_HASH_HSIEH
);
3283 return MEMCACHED_SUCCESS
;
3286 static memcached_return
enable_cas(memcached_st
*memc
)
3288 unsigned int set
= 1;
3290 memcached_version(memc
);
3292 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3293 || memc
->hosts
[0].minor_version
> 2)
3295 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3297 return MEMCACHED_SUCCESS
;
3300 return MEMCACHED_FAILURE
;
3303 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3305 memcached_version(memc
);
3307 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3308 || memc
->hosts
[0].minor_version
> 2)
3309 return MEMCACHED_SUCCESS
;
3311 return MEMCACHED_FAILURE
;
3314 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3316 memcached_return rc
;
3319 memcached_server_list_free(memc
->hosts
);
3321 memc
->number_of_hosts
= 0;
3323 if (stat("/tmp/memcached.socket", &buf
))
3324 return MEMCACHED_FAILURE
;
3326 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3331 static memcached_return
pre_nodelay(memcached_st
*memc
)
3333 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3334 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3336 return MEMCACHED_SUCCESS
;
3339 static memcached_return
pre_settimer(memcached_st
*memc
)
3341 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3342 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3344 return MEMCACHED_SUCCESS
;
3347 static memcached_return
poll_timeout(memcached_st
*memc
)
3353 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3355 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3357 assert(timeout
== 100);
3359 return MEMCACHED_SUCCESS
;
3362 static test_return
noreply_test(memcached_st
*memc
)
3364 memcached_return ret
;
3365 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3366 assert(ret
== MEMCACHED_SUCCESS
);
3367 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3368 assert(ret
== MEMCACHED_SUCCESS
);
3369 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3370 assert(ret
== MEMCACHED_SUCCESS
);
3371 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3372 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3373 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3375 for (int count
=0; count
< 5; ++count
)
3377 for (int x
=0; x
< 100; ++x
)
3380 size_t len
= (size_t)sprintf(key
, "%d", x
);
3384 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3387 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3390 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3393 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3396 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3402 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3406 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3407 ** API and is _ONLY_ done this way to verify that the library works the
3408 ** way it is supposed to do!!!!
3411 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3412 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3414 assert(no_msg
== 0);
3415 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3418 ** Now validate that all items was set properly!
3420 for (int x
=0; x
< 100; ++x
)
3423 size_t len
= (size_t)sprintf(key
, "%d", x
);
3426 char* value
=memcached_get(memc
, key
, strlen(key
),
3427 &length
, &flags
, &ret
);
3428 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3431 case 0: /* FALLTHROUGH */
3432 case 1: /* FALLTHROUGH */
3434 assert(strncmp(value
, key
, len
) == 0);
3435 assert(len
== length
);
3438 assert(length
== len
* 2);
3441 assert(length
== len
* 3);
3451 /* Try setting an illegal cas value (should not return an error to
3452 * the caller (because we don't expect a return message from the server)
3454 const char* keys
[]= {"0"};
3455 size_t lengths
[]= {1};
3458 memcached_result_st results_obj
;
3459 memcached_result_st
*results
;
3460 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3461 assert(ret
== MEMCACHED_SUCCESS
);
3463 results
= memcached_result_create(memc
, &results_obj
);
3465 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3467 assert(ret
== MEMCACHED_SUCCESS
);
3468 uint64_t cas
= memcached_result_cas(results
);
3469 memcached_result_free(&results_obj
);
3471 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3472 assert(ret
== MEMCACHED_SUCCESS
);
3475 * The item will have a new cas value, so try to set it again with the old
3476 * value. This should fail!
3478 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3479 assert(ret
== MEMCACHED_SUCCESS
);
3480 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3481 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3482 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3485 return TEST_SUCCESS
;
3488 static test_return
analyzer_test(memcached_st
*memc
)
3490 memcached_return rc
;
3491 memcached_stat_st
*memc_stat
;
3492 memcached_analysis_st
*report
;
3494 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3495 assert(rc
== MEMCACHED_SUCCESS
);
3498 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3499 assert(rc
== MEMCACHED_SUCCESS
);
3503 memcached_stat_free(NULL
, memc_stat
);
3505 return TEST_SUCCESS
;
3508 /* Count the objects */
3509 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3510 const char *key
__attribute__((unused
)),
3511 size_t key_length
__attribute__((unused
)),
3514 uint32_t *counter
= (uint32_t *)context
;
3516 *counter
= *counter
+ 1;
3518 return MEMCACHED_SUCCESS
;
3521 static test_return
dump_test(memcached_st
*memc
)
3523 memcached_return rc
;
3524 uint32_t counter
= 0;
3525 memcached_dump_func callbacks
[1];
3526 test_return main_rc
;
3528 callbacks
[0]= &callback_dump_counter
;
3530 /* No support for Binary protocol yet */
3531 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3532 return TEST_SUCCESS
;
3534 main_rc
= set_test3(memc
);
3536 assert (main_rc
== TEST_SUCCESS
);
3538 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3539 assert(rc
== MEMCACHED_SUCCESS
);
3541 /* We may have more then 32 if our previous flush has not completed */
3542 assert(counter
>= 32);
3544 return TEST_SUCCESS
;
3547 #ifdef HAVE_LIBMEMCACHEDUTIL
3548 static void* connection_release(void *arg
) {
3550 memcached_pool_st
* pool
;
3555 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3559 static test_return
connection_pool_test(memcached_st
*memc
)
3561 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3562 assert(pool
!= NULL
);
3563 memcached_st
* mmc
[10];
3564 memcached_return rc
;
3566 for (int x
= 0; x
< 10; ++x
) {
3567 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3568 assert(mmc
[x
] != NULL
);
3569 assert(rc
== MEMCACHED_SUCCESS
);
3572 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3573 assert(rc
== MEMCACHED_SUCCESS
);
3577 memcached_pool_st
* pool
;
3579 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3580 pthread_create(&tid
, NULL
, connection_release
, &item
);
3581 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3582 assert(rc
== MEMCACHED_SUCCESS
);
3583 pthread_join(tid
, NULL
);
3584 assert(mmc
[9] == item
.mmc
);
3585 const char *key
= "key";
3586 size_t keylen
= strlen(key
);
3588 // verify that I can do ops with all connections
3589 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3590 assert(rc
== MEMCACHED_SUCCESS
);
3592 for (unsigned int x
= 0; x
< 10; ++x
) {
3593 uint64_t number_value
;
3594 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3595 assert(rc
== MEMCACHED_SUCCESS
);
3596 assert(number_value
== (x
+1));
3600 for (int x
= 0; x
< 10; ++x
)
3601 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3603 assert(memcached_pool_destroy(pool
) == memc
);
3604 return TEST_SUCCESS
;
3608 static test_return
replication_set_test(memcached_st
*memc
)
3610 memcached_return rc
;
3611 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3612 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3614 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3615 assert(rc
== MEMCACHED_SUCCESS
);
3618 ** We are using the quiet commands to store the replicas, so we need
3619 ** to ensure that all of them are processed before we can continue.
3620 ** In the test we go directly from storing the object to trying to
3621 ** receive the object from all of the different servers, so we
3622 ** could end up in a race condition (the memcached server hasn't yet
3623 ** processed the quiet command from the replication set when it process
3624 ** the request from the other client (created by the clone)). As a
3625 ** workaround for that we call memcached_quit to send the quit command
3626 ** to the server and wait for the response ;-) If you use the test code
3627 ** as an example for your own code, please note that you shouldn't need
3630 memcached_quit(memc
);
3633 ** "bubba" should now be stored on all of our servers. We don't have an
3634 ** easy to use API to address each individual server, so I'll just iterate
3635 ** through a bunch of "master keys" and I should most likely hit all of the
3638 for (int x
= 'a'; x
<= 'z'; ++x
)
3640 char key
[2]= { [0]= (char)x
};
3643 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3645 assert(rc
== MEMCACHED_SUCCESS
);
3646 assert(val
!= NULL
);
3650 memcached_free(memc_clone
);
3652 return TEST_SUCCESS
;
3655 static test_return
replication_get_test(memcached_st
*memc
)
3657 memcached_return rc
;
3660 * Don't do the following in your code. I am abusing the internal details
3661 * within the library, and this is not a supported interface.
3662 * This is to verify correct behavior in the library
3664 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
3666 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3667 memc_clone
->hosts
[host
].port
= 0;
3669 for (int x
= 'a'; x
<= 'z'; ++x
)
3671 char key
[2]= { [0]= (char)x
};
3674 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3676 assert(rc
== MEMCACHED_SUCCESS
);
3677 assert(val
!= NULL
);
3681 memcached_free(memc_clone
);
3684 return TEST_SUCCESS
;
3687 static test_return
replication_mget_test(memcached_st
*memc
)
3689 memcached_return rc
;
3690 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3691 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3693 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3694 size_t len
[]= { 5, 4, 4, 4 };
3696 for (int x
=0; x
< 4; ++x
)
3698 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3699 assert(rc
== MEMCACHED_SUCCESS
);
3703 ** We are using the quiet commands to store the replicas, so we need
3704 ** to ensure that all of them are processed before we can continue.
3705 ** In the test we go directly from storing the object to trying to
3706 ** receive the object from all of the different servers, so we
3707 ** could end up in a race condition (the memcached server hasn't yet
3708 ** processed the quiet command from the replication set when it process
3709 ** the request from the other client (created by the clone)). As a
3710 ** workaround for that we call memcached_quit to send the quit command
3711 ** to the server and wait for the response ;-) If you use the test code
3712 ** as an example for your own code, please note that you shouldn't need
3715 memcached_quit(memc
);
3718 * Don't do the following in your code. I am abusing the internal details
3719 * within the library, and this is not a supported interface.
3720 * This is to verify correct behavior in the library
3722 memcached_result_st result_obj
;
3723 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
3725 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
3726 new_clone
->hosts
[host
].port
= 0;
3728 for (int x
= 'a'; x
<= 'z'; ++x
)
3730 const char key
[2]= { [0]= (const char)x
};
3732 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
3733 assert(rc
== MEMCACHED_SUCCESS
);
3735 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
3739 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
3744 memcached_result_free(&result_obj
);
3747 memcached_free(new_clone
);
3750 memcached_free(memc_clone
);
3752 return TEST_SUCCESS
;
3755 static test_return
replication_delete_test(memcached_st
*memc
)
3757 memcached_return rc
;
3758 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3759 /* Delete the items from all of the servers except 1 */
3760 uint64_t repl
= memcached_behavior_get(memc
,
3761 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3762 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3764 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3765 size_t len
[]= { 5, 4, 4, 4 };
3767 for (int x
=0; x
< 4; ++x
)
3769 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
3770 assert(rc
== MEMCACHED_SUCCESS
);
3774 * Don't do the following in your code. I am abusing the internal details
3775 * within the library, and this is not a supported interface.
3776 * This is to verify correct behavior in the library
3778 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
3779 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
3781 memc_clone
->hosts
[hash
].port
= 0;
3782 if (++hash
== memc_clone
->number_of_hosts
)
3786 memcached_result_st result_obj
;
3787 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
3789 for (int x
= 'a'; x
<= 'z'; ++x
)
3791 const char key
[2]= { [0]= (const char)x
};
3793 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
3794 assert(rc
== MEMCACHED_SUCCESS
);
3796 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
3800 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
3805 memcached_result_free(&result_obj
);
3808 memcached_free(memc_clone
);
3810 return TEST_SUCCESS
;
3813 static void increment_request_id(uint16_t *id
)
3816 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
3820 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
3822 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
3823 assert(ids
!= NULL
);
3826 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3827 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
3832 static test_return
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
3835 memcached_server_st
*cur_server
= memc
->hosts
;
3836 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
3838 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3840 assert(cur_server
[x
].cursor_active
== 0);
3841 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
3843 free(expected_req_ids
);
3846 return TEST_SUCCESS
;
3850 ** There is a little bit of a hack here, instead of removing
3851 ** the servers, I just set num host to 0 and them add then new udp servers
3853 static memcached_return
init_udp(memcached_st
*memc
)
3855 memcached_version(memc
);
3856 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3857 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
3858 || memc
->hosts
[0].micro_version
< 6)
3859 return MEMCACHED_FAILURE
;
3861 uint32_t num_hosts
= memc
->number_of_hosts
;
3863 memcached_server_st servers
[num_hosts
];
3864 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
3865 for (x
= 0; x
< num_hosts
; x
++)
3866 memcached_server_free(&memc
->hosts
[x
]);
3868 memc
->number_of_hosts
= 0;
3869 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
3870 for (x
= 0; x
< num_hosts
; x
++)
3872 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
3873 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3876 return MEMCACHED_SUCCESS
;
3879 static memcached_return
binary_init_udp(memcached_st
*memc
)
3882 return init_udp(memc
);
3885 /* Make sure that I cant add a tcp server to a udp client */
3886 static test_return
add_tcp_server_udp_client_test(memcached_st
*memc
)
3888 memcached_server_st server
;
3889 memcached_server_clone(&server
, &memc
->hosts
[0]);
3890 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3891 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3892 return TEST_SUCCESS
;
3895 /* Make sure that I cant add a udp server to a tcp client */
3896 static test_return
add_udp_server_tcp_client_test(memcached_st
*memc
)
3898 memcached_server_st server
;
3899 memcached_server_clone(&server
, &memc
->hosts
[0]);
3900 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3902 memcached_st tcp_client
;
3903 memcached_create(&tcp_client
);
3904 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3905 return TEST_SUCCESS
;
3908 static test_return
set_udp_behavior_test(memcached_st
*memc
)
3911 memcached_quit(memc
);
3912 memc
->number_of_hosts
= 0;
3913 run_distribution(memc
);
3914 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
3915 assert(memc
->flags
& MEM_USE_UDP
);
3916 assert(memc
->flags
& MEM_NOREPLY
);;
3918 assert(memc
->number_of_hosts
== 0);
3920 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
3921 assert(!(memc
->flags
& MEM_USE_UDP
));
3922 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
3923 assert(!(memc
->flags
& MEM_NOREPLY
));
3924 return TEST_SUCCESS
;
3927 static test_return
udp_set_test(memcached_st
*memc
)
3930 unsigned int num_iters
= 1025; //request id rolls over at 1024
3931 for (x
= 0; x
< num_iters
;x
++)
3933 memcached_return rc
;
3934 const char *key
= "foo";
3935 const char *value
= "when we sanitize";
3936 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3937 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
3938 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3939 rc
= memcached_set(memc
, key
, strlen(key
),
3940 value
, strlen(value
),
3941 (time_t)0, (uint32_t)0);
3942 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3943 /** NB, the check below assumes that if new write_ptr is less than
3944 * the original write_ptr that we have flushed. For large payloads, this
3945 * maybe an invalid assumption, but for the small payload we have it is OK
3947 if (rc
== MEMCACHED_SUCCESS
||
3948 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3949 increment_request_id(&expected_ids
[server_key
]);
3951 if (rc
== MEMCACHED_SUCCESS
)
3953 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3957 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
3958 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
3960 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
3962 return TEST_SUCCESS
;
3965 static test_return
udp_buffered_set_test(memcached_st
*memc
)
3967 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3968 return udp_set_test(memc
);
3971 static test_return
udp_set_too_big_test(memcached_st
*memc
)
3973 memcached_return rc
;
3974 const char *key
= "bar";
3975 char value
[MAX_UDP_DATAGRAM_LENGTH
];
3976 uint16_t *expected_ids
= get_udp_request_ids(memc
);
3977 rc
= memcached_set(memc
, key
, strlen(key
),
3978 value
, MAX_UDP_DATAGRAM_LENGTH
,
3979 (time_t)0, (uint32_t)0);
3980 assert(rc
== MEMCACHED_WRITE_FAILURE
);
3981 return post_udp_op_check(memc
,expected_ids
);
3984 static test_return
udp_delete_test(memcached_st
*memc
)
3987 unsigned int num_iters
= 1025; //request id rolls over at 1024
3988 for (x
= 0; x
< num_iters
;x
++)
3990 memcached_return rc
;
3991 const char *key
= "foo";
3992 uint16_t *expected_ids
=get_udp_request_ids(memc
);
3993 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
3994 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
3995 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
3996 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
3997 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
3998 increment_request_id(&expected_ids
[server_key
]);
3999 if (rc
== MEMCACHED_SUCCESS
)
4000 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4003 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4004 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4006 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4008 return TEST_SUCCESS
;
4011 static test_return
udp_buffered_delete_test(memcached_st
*memc
)
4013 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4014 return udp_delete_test(memc
);
4017 static test_return
udp_verbosity_test(memcached_st
*memc
)
4019 memcached_return rc
;
4020 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4022 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4023 increment_request_id(&expected_ids
[x
]);
4025 rc
= memcached_verbosity(memc
,3);
4026 assert(rc
== MEMCACHED_SUCCESS
);
4027 return post_udp_op_check(memc
,expected_ids
);
4030 static test_return
udp_quit_test(memcached_st
*memc
)
4032 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4033 memcached_quit(memc
);
4034 return post_udp_op_check(memc
, expected_ids
);
4037 static test_return
udp_flush_test(memcached_st
*memc
)
4039 memcached_return rc
;
4040 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4042 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4043 increment_request_id(&expected_ids
[x
]);
4045 rc
= memcached_flush(memc
,0);
4046 assert(rc
== MEMCACHED_SUCCESS
);
4047 return post_udp_op_check(memc
,expected_ids
);
4050 static test_return
udp_incr_test(memcached_st
*memc
)
4052 memcached_return rc
;
4053 const char *key
= "incr";
4054 const char *value
= "1";
4055 rc
= memcached_set(memc
, key
, strlen(key
),
4056 value
, strlen(value
),
4057 (time_t)0, (uint32_t)0);
4059 assert(rc
== MEMCACHED_SUCCESS
);
4060 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4061 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4062 increment_request_id(&expected_ids
[server_key
]);
4064 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4065 assert(rc
== MEMCACHED_SUCCESS
);
4066 return post_udp_op_check(memc
, expected_ids
);
4069 static test_return
udp_decr_test(memcached_st
*memc
)
4071 memcached_return rc
;
4072 const char *key
= "decr";
4073 const char *value
= "1";
4074 rc
= memcached_set(memc
, key
, strlen(key
),
4075 value
, strlen(value
),
4076 (time_t)0, (uint32_t)0);
4078 assert(rc
== MEMCACHED_SUCCESS
);
4079 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4080 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4081 increment_request_id(&expected_ids
[server_key
]);
4083 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4084 assert(rc
== MEMCACHED_SUCCESS
);
4085 return post_udp_op_check(memc
, expected_ids
);
4089 static test_return
udp_stat_test(memcached_st
*memc
)
4091 memcached_stat_st
* rv
= NULL
;
4092 memcached_return rc
;
4094 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4095 rv
= memcached_stat(memc
, args
, &rc
);
4097 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4098 return post_udp_op_check(memc
, expected_ids
);
4101 static test_return
udp_version_test(memcached_st
*memc
)
4103 memcached_return rc
;
4104 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4105 rc
= memcached_version(memc
);
4106 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4107 return post_udp_op_check(memc
, expected_ids
);
4110 static test_return
udp_get_test(memcached_st
*memc
)
4112 memcached_return rc
;
4113 const char *key
= "foo";
4115 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4116 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4117 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4118 assert(val
== NULL
);
4119 return post_udp_op_check(memc
, expected_ids
);
4122 static test_return
udp_mixed_io_test(memcached_st
*memc
)
4125 test_st mixed_io_ops
[] ={
4126 {"udp_set_test", 0, udp_set_test
},
4127 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4128 {"udp_delete_test", 0, udp_delete_test
},
4129 {"udp_verbosity_test", 0, udp_verbosity_test
},
4130 {"udp_quit_test", 0, udp_quit_test
},
4131 {"udp_flush_test", 0, udp_flush_test
},
4132 {"udp_incr_test", 0, udp_incr_test
},
4133 {"udp_decr_test", 0, udp_decr_test
},
4134 {"udp_version_test", 0, udp_version_test
}
4137 for (x
= 0; x
< 500; x
++)
4139 current_op
= mixed_io_ops
[random() % 9];
4140 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4142 return TEST_SUCCESS
;
4145 static test_return
hsieh_avaibility_test (memcached_st
*memc
)
4147 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4148 #ifdef HAVE_HSIEH_HASH
4149 expected_rc
= MEMCACHED_SUCCESS
;
4151 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4152 (uint64_t)MEMCACHED_HASH_HSIEH
);
4153 assert(rc
== expected_rc
);
4154 return TEST_SUCCESS
;
4157 static const char *list
[]=
4187 static test_return
md5_run (memcached_st
*memc
__attribute__((unused
)))
4191 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4192 245758794U, 2550894432U, 121710495U, 3053817768U,
4193 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4194 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4195 2138080750U, 217675895U, 2230934345U, 1234361223U,
4196 3968582726U, 2455685270U, 1293568479U, 199067604U,
4200 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4204 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4205 assert(values
[x
] == hash_val
);
4208 return TEST_SUCCESS
;
4211 static test_return
crc_run (memcached_st
*memc
__attribute__((unused
)))
4215 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4216 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4217 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4218 17300U, 28156U, 9192U, 20484U, 16925U };
4220 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4224 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4225 assert(values
[x
] == hash_val
);
4228 return TEST_SUCCESS
;
4231 static test_return
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4235 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4236 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4237 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4238 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4239 2815549194U, 2562818319U, 224996066U, 2680194749U,
4240 3035305390U, 246890365U, 2395624193U, 4145193337U,
4243 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4247 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4248 assert(values
[x
] == hash_val
);
4251 return TEST_SUCCESS
;
4254 static test_return
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4258 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4259 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4260 734810122U, 1516407546U, 560948863U, 1767346780U,
4261 561034892U, 4156330026U, 3716417003U, 3475297030U,
4262 1518272172U, 227211583U, 3938128828U, 126112909U,
4263 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4266 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4270 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4271 assert(values
[x
] == hash_val
);
4274 return TEST_SUCCESS
;
4277 static test_return
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4281 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4282 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4283 344481048U, 3865235296U, 2181839183U, 119581266U,
4284 510234242U, 4248244304U, 1362796839U, 103389328U,
4285 1449620010U, 182962511U, 3554262370U, 3206747549U,
4286 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4290 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4294 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4295 assert(values
[x
] == hash_val
);
4298 return TEST_SUCCESS
;
4301 static test_return
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4305 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4306 459261019U, 3521085446U, 18738364U, 1625305005U,
4307 2162232970U, 777243802U, 3323728671U, 132336572U,
4308 3654473228U, 260679466U, 1169454059U, 2698319462U,
4309 1062177260U, 235516991U, 2218399068U, 405302637U,
4310 1128467232U, 3579622413U, 2138539289U, 96429129U,
4313 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4317 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4318 assert(values
[x
] == hash_val
);
4321 return TEST_SUCCESS
;
4324 static test_return
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4328 #ifdef HAVE_HSIEH_HASH
4329 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4330 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4331 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4332 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4335 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 };
4338 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4342 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4343 assert(values
[x
] == hash_val
);
4346 return TEST_SUCCESS
;
4349 static test_return
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4353 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4354 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4355 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4356 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4357 2815549194U, 2562818319U, 224996066U, 2680194749U,
4358 3035305390U, 246890365U, 2395624193U, 4145193337U,
4361 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4365 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4366 assert(values
[x
] == hash_val
);
4369 return TEST_SUCCESS
;
4372 static test_return
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4376 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4377 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4378 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4379 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4380 4153263658U, 3824871822U, 3072759809U, 798622255U,
4381 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4385 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4389 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4390 assert(values
[x
] == hash_val
);
4393 return TEST_SUCCESS
;
4396 static test_return
regression_bug_434484(memcached_st
*memc
)
4398 if (pre_binary(memc
) != TEST_SUCCESS
)
4399 return TEST_SUCCESS
;
4401 memcached_return ret
;
4402 const char *key
= "regression_bug_434484";
4403 size_t keylen
= strlen(key
);
4405 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4406 assert(ret
== MEMCACHED_NOTSTORED
);
4408 size_t size
= 2048 * 1024;
4409 void *data
= malloc(size
);
4410 assert(data
!= NULL
);
4411 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
4412 assert(ret
== MEMCACHED_E2BIG
);
4415 return TEST_SUCCESS
;
4418 test_st udp_setup_server_tests
[] ={
4419 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
4420 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
4421 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
4425 test_st upd_io_tests
[] ={
4426 {"udp_set_test", 0, udp_set_test
},
4427 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
4428 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4429 {"udp_delete_test", 0, udp_delete_test
},
4430 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
4431 {"udp_verbosity_test", 0, udp_verbosity_test
},
4432 {"udp_quit_test", 0, udp_quit_test
},
4433 {"udp_flush_test", 0, udp_flush_test
},
4434 {"udp_incr_test", 0, udp_incr_test
},
4435 {"udp_decr_test", 0, udp_decr_test
},
4436 {"udp_stat_test", 0, udp_stat_test
},
4437 {"udp_version_test", 0, udp_version_test
},
4438 {"udp_get_test", 0, udp_get_test
},
4439 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
4443 /* Clean the server before beginning testing */
4445 {"flush", 0, flush_test
},
4446 {"init", 0, init_test
},
4447 {"allocation", 0, allocation_test
},
4448 {"server_list_null_test", 0, server_list_null_test
},
4449 {"server_unsort", 0, server_unsort_test
},
4450 {"server_sort", 0, server_sort_test
},
4451 {"server_sort2", 0, server_sort2_test
},
4452 {"clone_test", 0, clone_test
},
4453 {"connection_test", 0, connection_test
},
4454 {"callback_test", 0, callback_test
},
4455 {"behavior_test", 0, behavior_test
},
4456 {"userdata_test", 0, userdata_test
},
4457 {"error", 0, error_test
},
4458 {"set", 0, set_test
},
4459 {"set2", 0, set_test2
},
4460 {"set3", 0, set_test3
},
4461 {"dump", 1, dump_test
},
4462 {"add", 1, add_test
},
4463 {"replace", 1, replace_test
},
4464 {"delete", 1, delete_test
},
4465 {"get", 1, get_test
},
4466 {"get2", 0, get_test2
},
4467 {"get3", 0, get_test3
},
4468 {"get4", 0, get_test4
},
4469 {"partial mget", 0, get_test5
},
4470 {"stats_servername", 0, stats_servername_test
},
4471 {"increment", 0, increment_test
},
4472 {"increment_with_initial", 1, increment_with_initial_test
},
4473 {"decrement", 0, decrement_test
},
4474 {"decrement_with_initial", 1, decrement_with_initial_test
},
4475 {"quit", 0, quit_test
},
4476 {"mget", 1, mget_test
},
4477 {"mget_result", 1, mget_result_test
},
4478 {"mget_result_alloc", 1, mget_result_alloc_test
},
4479 {"mget_result_function", 1, mget_result_function
},
4480 {"get_stats", 0, get_stats
},
4481 {"add_host_test", 0, add_host_test
},
4482 {"add_host_test_1", 0, add_host_test1
},
4483 {"get_stats_keys", 0, get_stats_keys
},
4484 {"behavior_test", 0, get_stats_keys
},
4485 {"callback_test", 0, get_stats_keys
},
4486 {"version_string_test", 0, version_string_test
},
4487 {"bad_key", 1, bad_key_test
},
4488 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
4489 {"read_through", 1, read_through
},
4490 {"delete_through", 1, delete_through
},
4491 {"noreply", 1, noreply_test
},
4492 {"analyzer", 1, analyzer_test
},
4493 #ifdef HAVE_LIBMEMCACHEDUTIL
4494 {"connectionpool", 1, connection_pool_test
},
4499 test_st async_tests
[] ={
4500 {"add", 1, add_wrapper
},
4504 test_st string_tests
[] ={
4505 {"string static with null", 0, string_static_null
},
4506 {"string alloc with null", 0, string_alloc_null
},
4507 {"string alloc with 1K", 0, string_alloc_with_size
},
4508 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
4509 {"string append", 0, string_alloc_append
},
4510 {"string append failure (too big)", 0, string_alloc_append_toobig
},
4514 test_st result_tests
[] ={
4515 {"result static", 0, result_static
},
4516 {"result alloc", 0, result_alloc
},
4520 test_st version_1_2_3
[] ={
4521 {"append", 0, append_test
},
4522 {"prepend", 0, prepend_test
},
4523 {"cas", 0, cas_test
},
4524 {"cas2", 0, cas2_test
},
4525 {"append_binary", 0, append_binary_test
},
4529 test_st user_tests
[] ={
4530 {"user_supplied_bug1", 0, user_supplied_bug1
},
4531 {"user_supplied_bug2", 0, user_supplied_bug2
},
4532 {"user_supplied_bug3", 0, user_supplied_bug3
},
4533 {"user_supplied_bug4", 0, user_supplied_bug4
},
4534 {"user_supplied_bug5", 1, user_supplied_bug5
},
4535 {"user_supplied_bug6", 1, user_supplied_bug6
},
4536 {"user_supplied_bug7", 1, user_supplied_bug7
},
4537 {"user_supplied_bug8", 1, user_supplied_bug8
},
4538 {"user_supplied_bug9", 1, user_supplied_bug9
},
4539 {"user_supplied_bug10", 1, user_supplied_bug10
},
4540 {"user_supplied_bug11", 1, user_supplied_bug11
},
4541 {"user_supplied_bug12", 1, user_supplied_bug12
},
4542 {"user_supplied_bug13", 1, user_supplied_bug13
},
4543 {"user_supplied_bug14", 1, user_supplied_bug14
},
4544 {"user_supplied_bug15", 1, user_supplied_bug15
},
4545 {"user_supplied_bug16", 1, user_supplied_bug16
},
4548 ** It seems to be something weird with the character sets..
4549 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4550 ** guess I need to find out how this is supposed to work.. Perhaps I need
4551 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4552 ** so just disable the code for now...).
4554 {"user_supplied_bug17", 1, user_supplied_bug17
},
4556 {"user_supplied_bug18", 1, user_supplied_bug18
},
4557 {"user_supplied_bug19", 1, user_supplied_bug19
},
4558 {"user_supplied_bug20", 1, user_supplied_bug20
},
4562 test_st replication_tests
[]= {
4563 {"set", 1, replication_set_test
},
4564 {"get", 0, replication_get_test
},
4565 {"mget", 0, replication_mget_test
},
4566 {"delete", 0, replication_delete_test
},
4571 * The following test suite is used to verify that we don't introduce
4572 * regression bugs. If you want more information about the bug / test,
4573 * you should look in the bug report at
4574 * http://bugs.launchpad.net/libmemcached
4576 test_st regression_tests
[]= {
4577 {"lp:434484", 1, regression_bug_434484
},
4581 test_st generate_tests
[] ={
4582 {"generate_pairs", 1, generate_pairs
},
4583 {"generate_data", 1, generate_data
},
4584 {"get_read", 0, get_read
},
4585 {"delete_generate", 0, delete_generate
},
4586 {"generate_buffer_data", 1, generate_buffer_data
},
4587 {"delete_buffer", 0, delete_buffer_generate
},
4588 {"generate_data", 1, generate_data
},
4589 {"mget_read", 0, mget_read
},
4590 {"mget_read_result", 0, mget_read_result
},
4591 {"mget_read_function", 0, mget_read_function
},
4592 {"cleanup", 1, cleanup_pairs
},
4593 {"generate_large_pairs", 1, generate_large_pairs
},
4594 {"generate_data", 1, generate_data
},
4595 {"generate_buffer_data", 1, generate_buffer_data
},
4596 {"cleanup", 1, cleanup_pairs
},
4600 test_st consistent_tests
[] ={
4601 {"generate_pairs", 1, generate_pairs
},
4602 {"generate_data", 1, generate_data
},
4603 {"get_read", 0, get_read_count
},
4604 {"cleanup", 1, cleanup_pairs
},
4608 test_st consistent_weighted_tests
[] ={
4609 {"generate_pairs", 1, generate_pairs
},
4610 {"generate_data", 1, generate_data_with_stats
},
4611 {"get_read", 0, get_read_count
},
4612 {"cleanup", 1, cleanup_pairs
},
4616 test_st hsieh_availability
[] ={
4617 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
4621 test_st ketama_auto_eject_hosts
[] ={
4622 {"auto_eject_hosts", 1, auto_eject_hosts
},
4626 test_st hash_tests
[] ={
4627 {"md5", 0, md5_run
},
4628 {"crc", 0, crc_run
},
4629 {"fnv1_64", 0, fnv1_64_run
},
4630 {"fnv1a_64", 0, fnv1a_64_run
},
4631 {"fnv1_32", 0, fnv1_32_run
},
4632 {"fnv1a_32", 0, fnv1a_32_run
},
4633 {"hsieh", 0, hsieh_run
},
4634 {"murmur", 0, murmur_run
},
4635 {"jenkis", 0, jenkins_run
},
4639 collection_st collection
[] ={
4640 {"hsieh_availability",0,0,hsieh_availability
},
4641 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
4642 {"udp_io", init_udp
, 0, upd_io_tests
},
4643 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
4644 {"block", 0, 0, tests
},
4645 {"binary", pre_binary
, 0, tests
},
4646 {"nonblock", pre_nonblock
, 0, tests
},
4647 {"nodelay", pre_nodelay
, 0, tests
},
4648 {"settimer", pre_settimer
, 0, tests
},
4649 {"md5", pre_md5
, 0, tests
},
4650 {"crc", pre_crc
, 0, tests
},
4651 {"hsieh", pre_hsieh
, 0, tests
},
4652 {"jenkins", pre_jenkins
, 0, tests
},
4653 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
4654 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
4655 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
4656 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
4657 {"ketama", pre_behavior_ketama
, 0, tests
},
4658 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
4659 {"unix_socket", pre_unix_socket
, 0, tests
},
4660 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
4661 {"poll_timeout", poll_timeout
, 0, tests
},
4662 {"gets", enable_cas
, 0, tests
},
4663 {"consistent", enable_consistent
, 0, tests
},
4664 #ifdef MEMCACHED_ENABLE_DEPRECATED
4665 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
4667 {"memory_allocators", set_memory_alloc
, 0, tests
},
4668 {"prefix", set_prefix
, 0, tests
},
4669 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
4670 {"string", 0, 0, string_tests
},
4671 {"result", 0, 0, result_tests
},
4672 {"async", pre_nonblock
, 0, async_tests
},
4673 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
4674 {"user", 0, 0, user_tests
},
4675 {"generate", 0, 0, generate_tests
},
4676 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
4677 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
4678 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
4679 {"generate_md5", pre_md5
, 0, generate_tests
},
4680 {"generate_murmur", pre_murmur
, 0, generate_tests
},
4681 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
4682 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
4683 {"consistent_not", 0, 0, consistent_tests
},
4684 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
4685 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
4686 {"test_hashes", 0, 0, hash_tests
},
4687 {"replication", pre_replication
, 0, replication_tests
},
4688 {"replication_noblock", pre_replication_noblock
, 0, replication_tests
},
4689 {"regression", 0, 0, regression_tests
},
4693 #define SERVERS_TO_CREATE 5
4695 /* Prototypes for functions we will pass to test framework */
4696 void *world_create(void);
4697 void world_destroy(void *p
);
4699 void *world_create(void)
4701 server_startup_st
*construct
;
4703 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
4704 memset(construct
, 0, sizeof(server_startup_st
));
4705 construct
->count
= SERVERS_TO_CREATE
;
4707 server_startup(construct
);
4713 void world_destroy(void *p
)
4715 server_startup_st
*construct
= (server_startup_st
*)p
;
4716 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
4717 memcached_server_list_free(servers
);
4719 server_shutdown(construct
);
4723 void get_world(world_st
*world
)
4725 world
->collections
= collection
;
4726 world
->create
= world_create
;
4727 world
->destroy
= world_destroy
;