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;
602 for (x
= 0; x
< max
; x
++)
608 static test_return
replace_test(memcached_st
*memc
)
611 const char *key
= "foo";
612 const char *value
= "when we sanitize";
613 const char *original
= "first we insert some data";
615 rc
= memcached_set(memc
, key
, strlen(key
),
616 original
, strlen(original
),
617 (time_t)0, (uint32_t)0);
618 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
620 rc
= memcached_replace(memc
, key
, strlen(key
),
621 value
, strlen(value
),
622 (time_t)0, (uint32_t)0);
623 assert(rc
== MEMCACHED_SUCCESS
);
628 static test_return
delete_test(memcached_st
*memc
)
631 const char *key
= "foo";
632 const char *value
= "when we sanitize";
634 rc
= memcached_set(memc
, key
, strlen(key
),
635 value
, strlen(value
),
636 (time_t)0, (uint32_t)0);
637 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
639 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
640 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
645 static test_return
flush_test(memcached_st
*memc
)
649 rc
= memcached_flush(memc
, 0);
650 assert(rc
== MEMCACHED_SUCCESS
);
655 static memcached_return
server_function(memcached_st
*ptr
__attribute__((unused
)),
656 memcached_server_st
*server
__attribute__((unused
)),
657 void *context
__attribute__((unused
)))
661 return MEMCACHED_SUCCESS
;
664 static test_return
memcached_server_cursor_test(memcached_st
*memc
)
667 strcpy(context
, "foo bad");
668 memcached_server_function callbacks
[1];
670 callbacks
[0]= server_function
;
671 memcached_server_cursor(memc
, callbacks
, context
, 1);
675 static test_return
bad_key_test(memcached_st
*memc
)
678 const char *key
= "foo bad";
680 size_t string_length
;
682 memcached_st
*memc_clone
;
684 size_t max_keylen
= 0xffff;
686 memc_clone
= memcached_clone(NULL
, memc
);
689 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
690 assert(rc
== MEMCACHED_SUCCESS
);
692 /* All keys are valid in the binary protocol (except for length) */
693 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
695 string
= memcached_get(memc_clone
, key
, strlen(key
),
696 &string_length
, &flags
, &rc
);
697 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
698 assert(string_length
== 0);
702 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
703 assert(rc
== MEMCACHED_SUCCESS
);
704 string
= memcached_get(memc_clone
, key
, strlen(key
),
705 &string_length
, &flags
, &rc
);
706 assert(rc
== MEMCACHED_NOTFOUND
);
707 assert(string_length
== 0);
710 /* Test multi key for bad keys */
711 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
712 size_t key_lengths
[] = { 7, 7, 7 };
714 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
715 assert(rc
== MEMCACHED_SUCCESS
);
717 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
718 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
720 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
721 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
725 /* The following test should be moved to the end of this function when the
726 memcached server is updated to allow max size length of the keys in the
729 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
730 assert(rc
== MEMCACHED_SUCCESS
);
732 char *longkey
= malloc(max_keylen
+ 1);
735 memset(longkey
, 'a', max_keylen
+ 1);
736 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
737 &string_length
, &flags
, &rc
);
738 assert(rc
== MEMCACHED_NOTFOUND
);
739 assert(string_length
== 0);
742 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
743 &string_length
, &flags
, &rc
);
744 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
745 assert(string_length
== 0);
752 /* Make sure zero length keys are marked as bad */
754 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
755 assert(rc
== MEMCACHED_SUCCESS
);
756 string
= memcached_get(memc_clone
, key
, 0,
757 &string_length
, &flags
, &rc
);
758 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
759 assert(string_length
== 0);
762 memcached_free(memc_clone
);
767 #define READ_THROUGH_VALUE "set for me"
768 static memcached_return
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
769 char *key
__attribute__((unused
)),
770 size_t key_length
__attribute__((unused
)),
771 memcached_result_st
*result
)
774 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
777 static test_return
read_through(memcached_st
*memc
)
780 const char *key
= "foo";
782 size_t string_length
;
784 memcached_trigger_key cb
= (memcached_trigger_key
)read_through_trigger
;
786 string
= memcached_get(memc
, key
, strlen(key
),
787 &string_length
, &flags
, &rc
);
789 assert(rc
== MEMCACHED_NOTFOUND
);
790 assert(string_length
== 0);
793 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
795 assert(rc
== MEMCACHED_SUCCESS
);
797 string
= memcached_get(memc
, key
, strlen(key
),
798 &string_length
, &flags
, &rc
);
800 assert(rc
== MEMCACHED_SUCCESS
);
801 assert(string_length
== strlen(READ_THROUGH_VALUE
));
802 assert(!strcmp(READ_THROUGH_VALUE
, string
));
805 string
= memcached_get(memc
, key
, strlen(key
),
806 &string_length
, &flags
, &rc
);
808 assert(rc
== MEMCACHED_SUCCESS
);
809 assert(string_length
== strlen(READ_THROUGH_VALUE
));
810 assert(!strcmp(READ_THROUGH_VALUE
, string
));
816 static memcached_return
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
818 size_t key_length
__attribute__((unused
)))
822 return MEMCACHED_SUCCESS
;
825 static test_return
delete_through(memcached_st
*memc
)
827 memcached_trigger_delete_key callback
;
830 callback
= (memcached_trigger_delete_key
)delete_trigger
;
832 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
833 assert(rc
== MEMCACHED_SUCCESS
);
838 static test_return
get_test(memcached_st
*memc
)
841 const char *key
= "foo";
843 size_t string_length
;
846 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
847 assert(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
849 string
= memcached_get(memc
, key
, strlen(key
),
850 &string_length
, &flags
, &rc
);
852 assert(rc
== MEMCACHED_NOTFOUND
);
853 assert(string_length
== 0);
859 static test_return
get_test2(memcached_st
*memc
)
862 const char *key
= "foo";
863 const char *value
= "when we sanitize";
865 size_t string_length
;
868 rc
= memcached_set(memc
, key
, strlen(key
),
869 value
, strlen(value
),
870 (time_t)0, (uint32_t)0);
871 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
873 string
= memcached_get(memc
, key
, strlen(key
),
874 &string_length
, &flags
, &rc
);
877 assert(rc
== MEMCACHED_SUCCESS
);
878 assert(string_length
== strlen(value
));
879 assert(!memcmp(string
, value
, string_length
));
886 static test_return
set_test2(memcached_st
*memc
)
889 const char *key
= "foo";
890 const char *value
= "train in the brain";
891 size_t value_length
= strlen(value
);
894 for (x
= 0; x
< 10; x
++)
896 rc
= memcached_set(memc
, key
, strlen(key
),
898 (time_t)0, (uint32_t)0);
899 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
905 static test_return
set_test3(memcached_st
*memc
)
909 size_t value_length
= 8191;
912 value
= (char*)malloc(value_length
);
915 for (x
= 0; x
< value_length
; x
++)
916 value
[x
] = (char) (x
% 127);
918 /* The dump test relies on there being at least 32 items in memcached */
919 for (x
= 0; x
< 32; x
++)
923 sprintf(key
, "foo%u", x
);
925 rc
= memcached_set(memc
, key
, strlen(key
),
927 (time_t)0, (uint32_t)0);
928 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
936 static test_return
get_test3(memcached_st
*memc
)
939 const char *key
= "foo";
941 size_t value_length
= 8191;
943 size_t string_length
;
947 value
= (char*)malloc(value_length
);
950 for (x
= 0; x
< value_length
; x
++)
951 value
[x
] = (char) (x
% 127);
953 rc
= memcached_set(memc
, key
, strlen(key
),
955 (time_t)0, (uint32_t)0);
956 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
958 string
= memcached_get(memc
, key
, strlen(key
),
959 &string_length
, &flags
, &rc
);
961 assert(rc
== MEMCACHED_SUCCESS
);
963 assert(string_length
== value_length
);
964 assert(!memcmp(string
, value
, string_length
));
972 static test_return
get_test4(memcached_st
*memc
)
975 const char *key
= "foo";
977 size_t value_length
= 8191;
979 size_t string_length
;
983 value
= (char*)malloc(value_length
);
986 for (x
= 0; x
< value_length
; x
++)
987 value
[x
] = (char) (x
% 127);
989 rc
= memcached_set(memc
, key
, strlen(key
),
991 (time_t)0, (uint32_t)0);
992 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
994 for (x
= 0; x
< 10; x
++)
996 string
= memcached_get(memc
, key
, strlen(key
),
997 &string_length
, &flags
, &rc
);
999 assert(rc
== MEMCACHED_SUCCESS
);
1001 assert(string_length
== value_length
);
1002 assert(!memcmp(string
, value
, string_length
));
1008 return TEST_SUCCESS
;
1012 * This test verifies that memcached_read_one_response doesn't try to
1013 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1014 * responses before you execute a storage command.
1016 static test_return
get_test5(memcached_st
*memc
)
1019 ** Request the same key twice, to ensure that we hash to the same server
1020 ** (so that we have multiple response values queued up) ;-)
1022 const char *keys
[]= { "key", "key" };
1023 size_t lengths
[]= { 3, 3 };
1027 memcached_return rc
= memcached_set(memc
, keys
[0], lengths
[0],
1028 keys
[0], lengths
[0], 0, 0);
1029 assert(rc
== MEMCACHED_SUCCESS
);
1030 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1032 memcached_result_st results_obj
;
1033 memcached_result_st
*results
;
1034 results
=memcached_result_create(memc
, &results_obj
);
1036 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1038 memcached_result_free(&results_obj
);
1040 /* Don't read out the second result, but issue a set instead.. */
1041 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1042 assert(rc
== MEMCACHED_SUCCESS
);
1044 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1045 &rlen
, &flags
, &rc
);
1046 assert(val
== NULL
);
1047 assert(rc
== MEMCACHED_NOTFOUND
);
1048 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1049 assert(val
!= NULL
);
1050 assert(rc
== MEMCACHED_SUCCESS
);
1053 return TEST_SUCCESS
;
1056 static test_return
mget_end(memcached_st
*memc
)
1058 const char *keys
[]= { "foo", "foo2" };
1059 size_t lengths
[]= { 3, 4 };
1060 const char *values
[]= { "fjord", "41" };
1062 memcached_return rc
;
1065 for (int i
= 0; i
< 2; i
++)
1067 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1068 (time_t)0, (uint32_t)0);
1069 assert(rc
== MEMCACHED_SUCCESS
);
1073 size_t string_length
;
1076 // retrieve both via mget
1077 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1078 assert(rc
== MEMCACHED_SUCCESS
);
1080 char key
[MEMCACHED_MAX_KEY
];
1083 // this should get both
1084 for (int i
= 0; i
< 2; i
++)
1086 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1088 assert(rc
== MEMCACHED_SUCCESS
);
1090 if (key_length
== 4)
1092 assert(string_length
== strlen(values
[val
]));
1093 assert(strncmp(values
[val
], string
, string_length
) == 0);
1097 // this should indicate end
1098 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1099 assert(rc
== MEMCACHED_END
);
1102 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1103 assert(rc
== MEMCACHED_SUCCESS
);
1105 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1106 assert(key_length
== lengths
[0]);
1107 assert(strncmp(keys
[0], key
, key_length
) == 0);
1108 assert(string_length
== strlen(values
[0]));
1109 assert(strncmp(values
[0], string
, string_length
) == 0);
1110 assert(rc
== MEMCACHED_SUCCESS
);
1113 // this should indicate end
1114 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1115 assert(rc
== MEMCACHED_END
);
1117 return TEST_SUCCESS
;
1120 /* Do not copy the style of this code, I just access hosts to testthis function */
1121 static test_return
stats_servername_test(memcached_st
*memc
)
1123 memcached_return rc
;
1124 memcached_stat_st memc_stat
;
1125 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1126 memc
->hosts
[0].hostname
,
1127 memc
->hosts
[0].port
);
1129 return TEST_SUCCESS
;
1132 static test_return
increment_test(memcached_st
*memc
)
1134 uint64_t new_number
;
1135 memcached_return rc
;
1136 const char *key
= "number";
1137 const char *value
= "0";
1139 rc
= memcached_set(memc
, key
, strlen(key
),
1140 value
, strlen(value
),
1141 (time_t)0, (uint32_t)0);
1142 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1144 rc
= memcached_increment(memc
, key
, strlen(key
),
1146 assert(rc
== MEMCACHED_SUCCESS
);
1147 assert(new_number
== 1);
1149 rc
= memcached_increment(memc
, key
, strlen(key
),
1151 assert(rc
== MEMCACHED_SUCCESS
);
1152 assert(new_number
== 2);
1154 return TEST_SUCCESS
;
1157 static test_return
increment_with_initial_test(memcached_st
*memc
)
1159 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1161 uint64_t new_number
;
1162 memcached_return rc
;
1163 const char *key
= "number";
1164 uint64_t initial
= 0;
1166 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1167 1, initial
, 0, &new_number
);
1168 assert(rc
== MEMCACHED_SUCCESS
);
1169 assert(new_number
== initial
);
1171 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1172 1, initial
, 0, &new_number
);
1173 assert(rc
== MEMCACHED_SUCCESS
);
1174 assert(new_number
== (initial
+ 1));
1176 return TEST_SUCCESS
;
1179 static test_return
decrement_test(memcached_st
*memc
)
1181 uint64_t new_number
;
1182 memcached_return rc
;
1183 const char *key
= "number";
1184 const char *value
= "3";
1186 rc
= memcached_set(memc
, key
, strlen(key
),
1187 value
, strlen(value
),
1188 (time_t)0, (uint32_t)0);
1189 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1191 rc
= memcached_decrement(memc
, key
, strlen(key
),
1193 assert(rc
== MEMCACHED_SUCCESS
);
1194 assert(new_number
== 2);
1196 rc
= memcached_decrement(memc
, key
, strlen(key
),
1198 assert(rc
== MEMCACHED_SUCCESS
);
1199 assert(new_number
== 1);
1201 return TEST_SUCCESS
;
1204 static test_return
decrement_with_initial_test(memcached_st
*memc
)
1206 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1208 uint64_t new_number
;
1209 memcached_return rc
;
1210 const char *key
= "number";
1211 uint64_t initial
= 3;
1213 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1214 1, initial
, 0, &new_number
);
1215 assert(rc
== MEMCACHED_SUCCESS
);
1216 assert(new_number
== initial
);
1218 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1219 1, initial
, 0, &new_number
);
1220 assert(rc
== MEMCACHED_SUCCESS
);
1221 assert(new_number
== (initial
- 1));
1223 return TEST_SUCCESS
;
1226 static test_return
quit_test(memcached_st
*memc
)
1228 memcached_return rc
;
1229 const char *key
= "fudge";
1230 const char *value
= "sanford and sun";
1232 rc
= memcached_set(memc
, key
, strlen(key
),
1233 value
, strlen(value
),
1234 (time_t)10, (uint32_t)3);
1235 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1236 memcached_quit(memc
);
1238 rc
= memcached_set(memc
, key
, strlen(key
),
1239 value
, strlen(value
),
1240 (time_t)50, (uint32_t)9);
1241 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1243 return TEST_SUCCESS
;
1246 static test_return
mget_result_test(memcached_st
*memc
)
1248 memcached_return rc
;
1249 const char *keys
[]= {"fudge", "son", "food"};
1250 size_t key_length
[]= {5, 3, 4};
1253 memcached_result_st results_obj
;
1254 memcached_result_st
*results
;
1256 results
= memcached_result_create(memc
, &results_obj
);
1258 assert(&results_obj
== results
);
1260 /* We need to empty the server before continueing test */
1261 rc
= memcached_flush(memc
, 0);
1262 assert(rc
== MEMCACHED_SUCCESS
);
1264 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1265 assert(rc
== MEMCACHED_SUCCESS
);
1267 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1272 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1274 assert(rc
== MEMCACHED_END
);
1276 for (x
= 0; x
< 3; x
++)
1278 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1279 keys
[x
], key_length
[x
],
1280 (time_t)50, (uint32_t)9);
1281 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1284 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1285 assert(rc
== MEMCACHED_SUCCESS
);
1287 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1290 assert(&results_obj
== results
);
1291 assert(rc
== MEMCACHED_SUCCESS
);
1292 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1293 assert(!memcmp(memcached_result_key_value(results
),
1294 memcached_result_value(results
),
1295 memcached_result_length(results
)));
1298 memcached_result_free(&results_obj
);
1300 return TEST_SUCCESS
;
1303 static test_return
mget_result_alloc_test(memcached_st
*memc
)
1305 memcached_return rc
;
1306 const char *keys
[]= {"fudge", "son", "food"};
1307 size_t key_length
[]= {5, 3, 4};
1310 memcached_result_st
*results
;
1312 /* We need to empty the server before continueing test */
1313 rc
= memcached_flush(memc
, 0);
1314 assert(rc
== MEMCACHED_SUCCESS
);
1316 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1317 assert(rc
== MEMCACHED_SUCCESS
);
1319 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1324 assert(rc
== MEMCACHED_END
);
1326 for (x
= 0; x
< 3; x
++)
1328 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1329 keys
[x
], key_length
[x
],
1330 (time_t)50, (uint32_t)9);
1331 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1334 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1335 assert(rc
== MEMCACHED_SUCCESS
);
1338 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1341 assert(rc
== MEMCACHED_SUCCESS
);
1342 assert(memcached_result_key_length(results
) == memcached_result_length(results
));
1343 assert(!memcmp(memcached_result_key_value(results
),
1344 memcached_result_value(results
),
1345 memcached_result_length(results
)));
1346 memcached_result_free(results
);
1350 return TEST_SUCCESS
;
1353 /* Count the results */
1354 static memcached_return
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1355 memcached_result_st
*result
__attribute__((unused
)),
1358 unsigned int *counter
= (unsigned int *)context
;
1360 *counter
= *counter
+ 1;
1362 return MEMCACHED_SUCCESS
;
1365 static test_return
mget_result_function(memcached_st
*memc
)
1367 memcached_return rc
;
1368 const char *keys
[]= {"fudge", "son", "food"};
1369 size_t key_length
[]= {5, 3, 4};
1371 unsigned int counter
;
1372 memcached_execute_function callbacks
[1];
1374 /* We need to empty the server before continueing test */
1375 rc
= memcached_flush(memc
, 0);
1376 for (x
= 0; x
< 3; x
++)
1378 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1379 keys
[x
], key_length
[x
],
1380 (time_t)50, (uint32_t)9);
1381 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1384 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1385 assert(rc
== MEMCACHED_SUCCESS
);
1387 callbacks
[0]= &callback_counter
;
1389 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1391 assert(counter
== 3);
1393 return TEST_SUCCESS
;
1396 static test_return
mget_test(memcached_st
*memc
)
1398 memcached_return rc
;
1399 const char *keys
[]= {"fudge", "son", "food"};
1400 size_t key_length
[]= {5, 3, 4};
1404 char return_key
[MEMCACHED_MAX_KEY
];
1405 size_t return_key_length
;
1407 size_t return_value_length
;
1409 /* We need to empty the server before continueing test */
1410 rc
= memcached_flush(memc
, 0);
1411 assert(rc
== MEMCACHED_SUCCESS
);
1413 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1414 assert(rc
== MEMCACHED_SUCCESS
);
1416 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1417 &return_value_length
, &flags
, &rc
)) != NULL
)
1419 assert(return_value
);
1421 assert(!return_value
);
1422 assert(return_value_length
== 0);
1423 assert(rc
== MEMCACHED_END
);
1425 for (x
= 0; x
< 3; x
++)
1427 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1428 keys
[x
], key_length
[x
],
1429 (time_t)50, (uint32_t)9);
1430 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1433 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1434 assert(rc
== MEMCACHED_SUCCESS
);
1437 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1438 &return_value_length
, &flags
, &rc
)))
1440 assert(return_value
);
1441 assert(rc
== MEMCACHED_SUCCESS
);
1442 assert(return_key_length
== return_value_length
);
1443 assert(!memcmp(return_value
, return_key
, return_value_length
));
1448 return TEST_SUCCESS
;
1451 static test_return
get_stats_keys(memcached_st
*memc
)
1455 memcached_stat_st memc_stat
;
1456 memcached_return rc
;
1458 list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1459 assert(rc
== MEMCACHED_SUCCESS
);
1460 for (ptr
= list
; *ptr
; ptr
++)
1466 return TEST_SUCCESS
;
1469 static test_return
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1471 const char *version_string
;
1473 version_string
= memcached_lib_version();
1475 assert(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1477 return TEST_SUCCESS
;
1480 static test_return
get_stats(memcached_st
*memc
)
1485 memcached_return rc
;
1486 memcached_stat_st
*memc_stat
;
1488 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1489 assert(rc
== MEMCACHED_SUCCESS
);
1491 assert(rc
== MEMCACHED_SUCCESS
);
1494 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1496 list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1497 assert(rc
== MEMCACHED_SUCCESS
);
1498 for (ptr
= list
; *ptr
; ptr
++);
1503 memcached_stat_free(NULL
, memc_stat
);
1505 return TEST_SUCCESS
;
1508 static test_return
add_host_test(memcached_st
*memc
)
1511 memcached_server_st
*servers
;
1512 memcached_return rc
;
1513 char servername
[]= "0.example.com";
1515 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1517 assert(1 == memcached_server_list_count(servers
));
1519 for (x
= 2; x
< 20; x
++)
1521 char buffer
[SMALL_STRING_LEN
];
1523 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1524 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1526 assert(rc
== MEMCACHED_SUCCESS
);
1527 assert(x
== memcached_server_list_count(servers
));
1530 rc
= memcached_server_push(memc
, servers
);
1531 assert(rc
== MEMCACHED_SUCCESS
);
1532 rc
= memcached_server_push(memc
, servers
);
1533 assert(rc
== MEMCACHED_SUCCESS
);
1535 memcached_server_list_free(servers
);
1537 return TEST_SUCCESS
;
1540 static memcached_return
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1542 return MEMCACHED_SUCCESS
;
1545 static memcached_return
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1547 return MEMCACHED_SUCCESS
;
1550 static test_return
callback_test(memcached_st
*memc
)
1552 /* Test User Data */
1556 memcached_return rc
;
1558 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1559 assert(rc
== MEMCACHED_SUCCESS
);
1560 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1561 assert(*test_ptr
== x
);
1564 /* Test Clone Callback */
1566 memcached_clone_func clone_cb
= (memcached_clone_func
)clone_test_callback
;
1567 void *clone_cb_ptr
= *(void **)&clone_cb
;
1568 void *temp_function
= NULL
;
1569 memcached_return rc
;
1571 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1573 assert(rc
== MEMCACHED_SUCCESS
);
1574 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1575 assert(temp_function
== clone_cb_ptr
);
1578 /* Test Cleanup Callback */
1580 memcached_cleanup_func cleanup_cb
=
1581 (memcached_cleanup_func
)cleanup_test_callback
;
1582 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1583 void *temp_function
= NULL
;
1584 memcached_return rc
;
1586 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1588 assert(rc
== MEMCACHED_SUCCESS
);
1589 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1590 assert(temp_function
== cleanup_cb_ptr
);
1593 return TEST_SUCCESS
;
1596 /* We don't test the behavior itself, we test the switches */
1597 static test_return
behavior_test(memcached_st
*memc
)
1602 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1603 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1606 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1607 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1610 set
= MEMCACHED_HASH_MD5
;
1611 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1612 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1613 assert(value
== MEMCACHED_HASH_MD5
);
1617 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1618 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1621 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1622 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1625 set
= MEMCACHED_HASH_DEFAULT
;
1626 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1627 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1628 assert(value
== MEMCACHED_HASH_DEFAULT
);
1630 set
= MEMCACHED_HASH_CRC
;
1631 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1632 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1633 assert(value
== MEMCACHED_HASH_CRC
);
1635 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1638 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1641 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1642 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1643 assert((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1644 return TEST_SUCCESS
;
1647 /* Test case provided by Cal Haldenbrand */
1648 static test_return
user_supplied_bug1(memcached_st
*memc
)
1650 unsigned int setter
= 1;
1653 unsigned long long total
= 0;
1656 char randomstuff
[6 * 1024];
1657 memcached_return rc
;
1659 memset(randomstuff
, 0, 6 * 1024);
1661 /* We just keep looking at the same values over and over */
1664 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1665 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1669 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1673 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1674 memset(randomstuff
, 0, 6 * 1024);
1675 assert(size
< 6 * 1024); /* Being safe here */
1677 for (j
= 0 ; j
< size
;j
++)
1678 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1681 sprintf(key
, "%d", x
);
1682 rc
= memcached_set(memc
, key
, strlen(key
),
1683 randomstuff
, strlen(randomstuff
), 10, 0);
1684 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1685 /* If we fail, lets try again */
1686 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1687 rc
= memcached_set(memc
, key
, strlen(key
),
1688 randomstuff
, strlen(randomstuff
), 10, 0);
1689 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1692 return TEST_SUCCESS
;
1695 /* Test case provided by Cal Haldenbrand */
1696 static test_return
user_supplied_bug2(memcached_st
*memc
)
1699 unsigned int setter
;
1701 unsigned long long total
;
1704 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1705 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1707 setter
= 20 * 1024576;
1708 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1709 setter
= 20 * 1024576;
1710 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1711 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1712 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1714 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1717 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1719 memcached_return rc
= MEMCACHED_SUCCESS
;
1720 char buffer
[SMALL_STRING_LEN
];
1725 memset(buffer
, 0, SMALL_STRING_LEN
);
1727 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1728 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1729 &val_len
, &flags
, &rc
);
1730 if (rc
!= MEMCACHED_SUCCESS
)
1732 if (rc
== MEMCACHED_NOTFOUND
)
1746 return TEST_SUCCESS
;
1749 /* Do a large mget() over all the keys we think exist */
1750 #define KEY_COUNT 3000 // * 1024576
1751 static test_return
user_supplied_bug3(memcached_st
*memc
)
1753 memcached_return rc
;
1754 unsigned int setter
;
1757 size_t key_lengths
[KEY_COUNT
];
1760 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1761 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1763 setter
= 20 * 1024576;
1764 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1765 setter
= 20 * 1024576;
1766 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1767 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1768 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1771 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
1773 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
1774 for (x
= 0; x
< KEY_COUNT
; x
++)
1778 snprintf(buffer
, 30, "%u", x
);
1779 keys
[x
]= strdup(buffer
);
1780 key_lengths
[x
]= strlen(keys
[x
]);
1783 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
1784 assert(rc
== MEMCACHED_SUCCESS
);
1786 /* Turn this into a help function */
1788 char return_key
[MEMCACHED_MAX_KEY
];
1789 size_t return_key_length
;
1791 size_t return_value_length
;
1794 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1795 &return_value_length
, &flags
, &rc
)))
1797 assert(return_value
);
1798 assert(rc
== MEMCACHED_SUCCESS
);
1803 for (x
= 0; x
< KEY_COUNT
; x
++)
1807 return TEST_SUCCESS
;
1810 /* Make sure we behave properly if server list has no values */
1811 static test_return
user_supplied_bug4(memcached_st
*memc
)
1813 memcached_return rc
;
1814 const char *keys
[]= {"fudge", "son", "food"};
1815 size_t key_length
[]= {5, 3, 4};
1818 char return_key
[MEMCACHED_MAX_KEY
];
1819 size_t return_key_length
;
1821 size_t return_value_length
;
1823 /* Here we free everything before running a bunch of mget tests */
1825 memcached_server_list_free(memc
->hosts
);
1827 memc
->number_of_hosts
= 0;
1831 /* We need to empty the server before continueing test */
1832 rc
= memcached_flush(memc
, 0);
1833 assert(rc
== MEMCACHED_NO_SERVERS
);
1835 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1836 assert(rc
== MEMCACHED_NO_SERVERS
);
1838 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1839 &return_value_length
, &flags
, &rc
)) != NULL
)
1841 assert(return_value
);
1843 assert(!return_value
);
1844 assert(return_value_length
== 0);
1845 assert(rc
== MEMCACHED_NO_SERVERS
);
1847 for (x
= 0; x
< 3; x
++)
1849 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1850 keys
[x
], key_length
[x
],
1851 (time_t)50, (uint32_t)9);
1852 assert(rc
== MEMCACHED_NO_SERVERS
);
1855 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1856 assert(rc
== MEMCACHED_NO_SERVERS
);
1859 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1860 &return_value_length
, &flags
, &rc
)))
1862 assert(return_value
);
1863 assert(rc
== MEMCACHED_SUCCESS
);
1864 assert(return_key_length
== return_value_length
);
1865 assert(!memcmp(return_value
, return_key
, return_value_length
));
1870 return TEST_SUCCESS
;
1873 #define VALUE_SIZE_BUG5 1048064
1874 static test_return
user_supplied_bug5(memcached_st
*memc
)
1876 memcached_return rc
;
1877 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1878 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1879 char return_key
[MEMCACHED_MAX_KEY
];
1880 size_t return_key_length
;
1882 size_t value_length
;
1886 char insert_data
[VALUE_SIZE_BUG5
];
1888 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1889 insert_data
[x
]= (signed char)rand();
1891 memcached_flush(memc
, 0);
1892 value
= memcached_get(memc
, keys
[0], key_length
[0],
1893 &value_length
, &flags
, &rc
);
1894 assert(value
== NULL
);
1895 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1898 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1899 &value_length
, &flags
, &rc
)))
1903 for (x
= 0; x
< 4; x
++)
1905 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1906 insert_data
, VALUE_SIZE_BUG5
,
1907 (time_t)0, (uint32_t)0);
1908 assert(rc
== MEMCACHED_SUCCESS
);
1911 for (x
= 0; x
< 10; x
++)
1913 value
= memcached_get(memc
, keys
[0], key_length
[0],
1914 &value_length
, &flags
, &rc
);
1918 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1920 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1921 &value_length
, &flags
, &rc
)))
1929 return TEST_SUCCESS
;
1932 static test_return
user_supplied_bug6(memcached_st
*memc
)
1934 memcached_return rc
;
1935 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1936 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1937 char return_key
[MEMCACHED_MAX_KEY
];
1938 size_t return_key_length
;
1940 size_t value_length
;
1944 char insert_data
[VALUE_SIZE_BUG5
];
1946 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
1947 insert_data
[x
]= (signed char)rand();
1949 memcached_flush(memc
, 0);
1950 value
= memcached_get(memc
, keys
[0], key_length
[0],
1951 &value_length
, &flags
, &rc
);
1952 assert(value
== NULL
);
1953 assert(rc
== MEMCACHED_NOTFOUND
);
1954 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1955 assert(rc
== MEMCACHED_SUCCESS
);
1958 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1959 &value_length
, &flags
, &rc
)))
1962 assert(rc
== MEMCACHED_END
);
1964 for (x
= 0; x
< 4; x
++)
1966 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1967 insert_data
, VALUE_SIZE_BUG5
,
1968 (time_t)0, (uint32_t)0);
1969 assert(rc
== MEMCACHED_SUCCESS
);
1972 for (x
= 0; x
< 2; x
++)
1974 value
= memcached_get(memc
, keys
[0], key_length
[0],
1975 &value_length
, &flags
, &rc
);
1979 rc
= memcached_mget(memc
, keys
, key_length
, 4);
1980 assert(rc
== MEMCACHED_SUCCESS
);
1982 /* We test for purge of partial complete fetches */
1983 for (count
= 3; count
; count
--)
1985 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1986 &value_length
, &flags
, &rc
);
1987 assert(rc
== MEMCACHED_SUCCESS
);
1988 assert(!(memcmp(value
, insert_data
, value_length
)));
1989 assert(value_length
);
1994 return TEST_SUCCESS
;
1997 static test_return
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
1999 memcached_return rc
;
2001 memcached_st
*memc_clone
;
2003 memcached_server_st
*servers
;
2004 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";
2006 servers
= memcached_servers_parse(server_list
);
2009 mine
= memcached_create(NULL
);
2010 rc
= memcached_server_push(mine
, servers
);
2011 assert(rc
== MEMCACHED_SUCCESS
);
2012 memcached_server_list_free(servers
);
2015 memc_clone
= memcached_clone(NULL
, mine
);
2017 memcached_quit(mine
);
2018 memcached_quit(memc_clone
);
2021 memcached_free(mine
);
2022 memcached_free(memc_clone
);
2024 return TEST_SUCCESS
;
2027 /* Test flag store/retrieve */
2028 static test_return
user_supplied_bug7(memcached_st
*memc
)
2030 memcached_return rc
;
2031 const char *keys
= "036790384900";
2032 size_t key_length
= strlen(keys
);
2033 char return_key
[MEMCACHED_MAX_KEY
];
2034 size_t return_key_length
;
2036 size_t value_length
;
2039 char insert_data
[VALUE_SIZE_BUG5
];
2041 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2042 insert_data
[x
]= (signed char)rand();
2044 memcached_flush(memc
, 0);
2047 rc
= memcached_set(memc
, keys
, key_length
,
2048 insert_data
, VALUE_SIZE_BUG5
,
2050 assert(rc
== MEMCACHED_SUCCESS
);
2053 value
= memcached_get(memc
, keys
, key_length
,
2054 &value_length
, &flags
, &rc
);
2055 assert(flags
== 245);
2059 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2062 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2063 &value_length
, &flags
, &rc
);
2064 assert(flags
== 245);
2069 return TEST_SUCCESS
;
2072 static test_return
user_supplied_bug9(memcached_st
*memc
)
2074 memcached_return rc
;
2075 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2076 size_t key_length
[3];
2081 char return_key
[MEMCACHED_MAX_KEY
];
2082 size_t return_key_length
;
2084 size_t return_value_length
;
2087 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2088 key_length
[1]= strlen("fudge&*@#");
2089 key_length
[2]= strlen("for^#@&$not");
2092 for (x
= 0; x
< 3; x
++)
2094 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2095 keys
[x
], key_length
[x
],
2096 (time_t)50, (uint32_t)9);
2097 assert(rc
== MEMCACHED_SUCCESS
);
2100 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2101 assert(rc
== MEMCACHED_SUCCESS
);
2103 /* We need to empty the server before continueing test */
2104 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2105 &return_value_length
, &flags
, &rc
)) != NULL
)
2107 assert(return_value
);
2113 return TEST_SUCCESS
;
2116 /* We are testing with aggressive timeout to get failures */
2117 static test_return
user_supplied_bug10(memcached_st
*memc
)
2119 const char *key
= "foo";
2121 size_t value_length
= 512;
2124 memcached_return rc
;
2125 unsigned int set
= 1;
2126 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2129 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2130 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2132 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2135 value
= (char*)malloc(value_length
* sizeof(char));
2137 for (x
= 0; x
< value_length
; x
++)
2138 value
[x
]= (char) (x
% 127);
2140 for (x
= 1; x
<= 100000; ++x
)
2142 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2144 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2145 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2147 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2152 memcached_free(mclone
);
2154 return TEST_SUCCESS
;
2158 We are looking failures in the async protocol
2160 static test_return
user_supplied_bug11(memcached_st
*memc
)
2162 const char *key
= "foo";
2164 size_t value_length
= 512;
2167 memcached_return rc
;
2168 unsigned int set
= 1;
2170 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2172 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2173 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2175 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2178 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2180 assert(timeout
== -1);
2182 value
= (char*)malloc(value_length
* sizeof(char));
2184 for (x
= 0; x
< value_length
; x
++)
2185 value
[x
]= (char) (x
% 127);
2187 for (x
= 1; x
<= 100000; ++x
)
2189 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2193 memcached_free(mclone
);
2195 return TEST_SUCCESS
;
2199 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2201 static test_return
user_supplied_bug12(memcached_st
*memc
)
2203 memcached_return rc
;
2205 size_t value_length
;
2207 uint64_t number_value
;
2209 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2210 &value_length
, &flags
, &rc
);
2211 assert(value
== NULL
);
2212 assert(rc
== MEMCACHED_NOTFOUND
);
2214 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2217 assert(value
== NULL
);
2218 /* The binary protocol will set the key if it doesn't exist */
2219 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2220 assert(rc
== MEMCACHED_SUCCESS
);
2222 assert(rc
== MEMCACHED_NOTFOUND
);
2224 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2226 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2227 &value_length
, &flags
, &rc
);
2229 assert(rc
== MEMCACHED_SUCCESS
);
2232 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2234 assert(number_value
== 2);
2235 assert(rc
== MEMCACHED_SUCCESS
);
2237 return TEST_SUCCESS
;
2241 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2242 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2244 static test_return
user_supplied_bug13(memcached_st
*memc
)
2246 char key
[] = "key34567890";
2248 memcached_return rc
;
2249 size_t overflowSize
;
2251 char commandFirst
[]= "set key34567890 0 0 ";
2252 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2253 size_t commandLength
;
2256 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2258 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2260 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2262 overflow
= malloc(testSize
);
2263 assert(overflow
!= NULL
);
2265 memset(overflow
, 'x', testSize
);
2266 rc
= memcached_set(memc
, key
, strlen(key
),
2267 overflow
, testSize
, 0, 0);
2268 assert(rc
== MEMCACHED_SUCCESS
);
2272 return TEST_SUCCESS
;
2277 Test values of many different sizes
2278 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2279 set key34567890 0 0 8169 \r\n
2280 is sent followed by buffer of size 8169, followed by 8169
2282 static test_return
user_supplied_bug14(memcached_st
*memc
)
2285 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2286 memcached_return rc
;
2287 const char *key
= "foo";
2289 size_t value_length
= 18000;
2291 size_t string_length
;
2294 size_t current_length
;
2296 value
= (char*)malloc(value_length
);
2299 for (x
= 0; x
< value_length
; x
++)
2300 value
[x
] = (char) (x
% 127);
2302 for (current_length
= 0; current_length
< value_length
; current_length
++)
2304 rc
= memcached_set(memc
, key
, strlen(key
),
2305 value
, current_length
,
2306 (time_t)0, (uint32_t)0);
2307 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2309 string
= memcached_get(memc
, key
, strlen(key
),
2310 &string_length
, &flags
, &rc
);
2312 assert(rc
== MEMCACHED_SUCCESS
);
2313 assert(string_length
== current_length
);
2314 assert(!memcmp(string
, value
, string_length
));
2321 return TEST_SUCCESS
;
2325 Look for zero length value problems
2327 static test_return
user_supplied_bug15(memcached_st
*memc
)
2330 memcached_return rc
;
2331 const char *key
= "mykey";
2336 for (x
= 0; x
< 2; x
++)
2338 rc
= memcached_set(memc
, key
, strlen(key
),
2340 (time_t)0, (uint32_t)0);
2342 assert(rc
== MEMCACHED_SUCCESS
);
2344 value
= memcached_get(memc
, key
, strlen(key
),
2345 &length
, &flags
, &rc
);
2347 assert(rc
== MEMCACHED_SUCCESS
);
2348 assert(value
== NULL
);
2349 assert(length
== 0);
2352 value
= memcached_get(memc
, key
, strlen(key
),
2353 &length
, &flags
, &rc
);
2355 assert(rc
== MEMCACHED_SUCCESS
);
2356 assert(value
== NULL
);
2357 assert(length
== 0);
2361 return TEST_SUCCESS
;
2364 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2365 static test_return
user_supplied_bug16(memcached_st
*memc
)
2367 memcached_return rc
;
2368 const char *key
= "mykey";
2373 rc
= memcached_set(memc
, key
, strlen(key
),
2375 (time_t)0, UINT32_MAX
);
2377 assert(rc
== MEMCACHED_SUCCESS
);
2379 value
= memcached_get(memc
, key
, strlen(key
),
2380 &length
, &flags
, &rc
);
2382 assert(rc
== MEMCACHED_SUCCESS
);
2383 assert(value
== NULL
);
2384 assert(length
== 0);
2385 assert(flags
== UINT32_MAX
);
2387 return TEST_SUCCESS
;
2391 /* Check the validity of chinese key*/
2392 static test_return
user_supplied_bug17(memcached_st
*memc
)
2394 memcached_return rc
;
2395 const char *key
= "豆瓣";
2396 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2401 rc
= memcached_set(memc
, key
, strlen(key
),
2402 value
, strlen(value
),
2405 assert(rc
== MEMCACHED_SUCCESS
);
2407 value2
= memcached_get(memc
, key
, strlen(key
),
2408 &length
, &flags
, &rc
);
2410 assert(length
==strlen(value
));
2411 assert(rc
== MEMCACHED_SUCCESS
);
2412 assert(memcmp(value
, value2
, length
)==0);
2415 return TEST_SUCCESS
;
2423 static test_return
user_supplied_bug19(memcached_st
*memc
)
2426 memcached_server_st
*s
;
2427 memcached_return res
;
2431 m
= memcached_create(NULL
);
2432 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2433 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2435 s
= memcached_server_by_key(m
, "a", 1, &res
);
2436 memcached_server_free(s
);
2440 return TEST_SUCCESS
;
2443 /* CAS test from Andei */
2444 static test_return
user_supplied_bug20(memcached_st
*memc
)
2446 memcached_return status
;
2447 memcached_result_st
*result
, result_obj
;
2448 const char *key
= "abc";
2449 size_t key_len
= strlen("abc");
2450 const char *value
= "foobar";
2451 size_t value_len
= strlen(value
);
2453 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2455 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2456 assert(status
== MEMCACHED_SUCCESS
);
2458 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2459 assert(status
== MEMCACHED_SUCCESS
);
2461 result
= memcached_result_create(memc
, &result_obj
);
2464 memcached_result_create(memc
, &result_obj
);
2465 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2468 assert(status
== MEMCACHED_SUCCESS
);
2470 memcached_result_free(result
);
2472 return TEST_SUCCESS
;
2475 #include "ketama_test_cases.h"
2476 static test_return
user_supplied_bug18(memcached_st
*trash
)
2478 memcached_return rc
;
2481 memcached_server_st
*server_pool
;
2486 memc
= memcached_create(NULL
);
2489 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2490 assert(rc
== MEMCACHED_SUCCESS
);
2492 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2495 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2496 assert(rc
== MEMCACHED_SUCCESS
);
2498 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2499 assert(value
== MEMCACHED_HASH_MD5
);
2501 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");
2502 memcached_server_push(memc
, server_pool
);
2504 /* verify that the server list was parsed okay. */
2505 assert(memc
->number_of_hosts
== 8);
2506 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2507 assert(server_pool
[0].port
== 11211);
2508 assert(server_pool
[0].weight
== 600);
2509 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2510 assert(server_pool
[2].port
== 11211);
2511 assert(server_pool
[2].weight
== 200);
2512 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2513 assert(server_pool
[7].port
== 11211);
2514 assert(server_pool
[7].weight
== 100);
2516 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2517 * us test the boundary wraparound.
2519 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2521 /* verify the standard ketama set. */
2522 for (x
= 0; x
< 99; x
++)
2524 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2525 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2526 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2529 memcached_server_list_free(server_pool
);
2530 memcached_free(memc
);
2532 return TEST_SUCCESS
;
2535 static test_return
auto_eject_hosts(memcached_st
*trash
)
2539 memcached_return rc
;
2540 memcached_st
*memc
= memcached_create(NULL
);
2543 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2544 assert(rc
== MEMCACHED_SUCCESS
);
2546 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2549 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2550 assert(rc
== MEMCACHED_SUCCESS
);
2552 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2553 assert(value
== MEMCACHED_HASH_MD5
);
2555 /* server should be removed when in delay */
2556 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2557 assert(rc
== MEMCACHED_SUCCESS
);
2559 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2562 memcached_server_st
*server_pool
;
2563 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");
2564 memcached_server_push(memc
, server_pool
);
2566 /* verify that the server list was parsed okay. */
2567 assert(memc
->number_of_hosts
== 8);
2568 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2569 assert(server_pool
[0].port
== 11211);
2570 assert(server_pool
[0].weight
== 600);
2571 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2572 assert(server_pool
[2].port
== 11211);
2573 assert(server_pool
[2].weight
== 200);
2574 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2575 assert(server_pool
[7].port
== 11211);
2576 assert(server_pool
[7].weight
== 100);
2578 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2579 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2581 for (int x
= 0; x
< 99; x
++)
2583 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2584 assert(server_idx
!= 2);
2587 /* and re-added when it's back. */
2588 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2589 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2590 run_distribution(memc
);
2591 for (int x
= 0; x
< 99; x
++)
2593 uint32_t server_idx
= memcached_generate_hash(memc
, test_cases
[x
].key
, strlen(test_cases
[x
].key
));
2594 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2595 assert(strcmp(hostname
, test_cases
[x
].server
) == 0);
2598 memcached_server_list_free(server_pool
);
2599 memcached_free(memc
);
2601 return TEST_SUCCESS
;
2604 static test_return
result_static(memcached_st
*memc
)
2606 memcached_result_st result
;
2607 memcached_result_st
*result_ptr
;
2609 result_ptr
= memcached_result_create(memc
, &result
);
2610 assert(result
.is_allocated
== false);
2612 memcached_result_free(&result
);
2614 return TEST_SUCCESS
;
2617 static test_return
result_alloc(memcached_st
*memc
)
2619 memcached_result_st
*result
;
2621 result
= memcached_result_create(memc
, NULL
);
2623 memcached_result_free(result
);
2625 return TEST_SUCCESS
;
2628 static test_return
string_static_null(memcached_st
*memc
)
2630 memcached_string_st string
;
2631 memcached_string_st
*string_ptr
;
2633 string_ptr
= memcached_string_create(memc
, &string
, 0);
2634 assert(string
.is_allocated
== false);
2636 memcached_string_free(&string
);
2638 return TEST_SUCCESS
;
2641 static test_return
string_alloc_null(memcached_st
*memc
)
2643 memcached_string_st
*string
;
2645 string
= memcached_string_create(memc
, NULL
, 0);
2647 memcached_string_free(string
);
2649 return TEST_SUCCESS
;
2652 static test_return
string_alloc_with_size(memcached_st
*memc
)
2654 memcached_string_st
*string
;
2656 string
= memcached_string_create(memc
, NULL
, 1024);
2658 memcached_string_free(string
);
2660 return TEST_SUCCESS
;
2663 static test_return
string_alloc_with_size_toobig(memcached_st
*memc
)
2665 memcached_string_st
*string
;
2667 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
2668 assert(string
== NULL
);
2670 return TEST_SUCCESS
;
2673 static test_return
string_alloc_append(memcached_st
*memc
)
2676 char buffer
[SMALL_STRING_LEN
];
2677 memcached_string_st
*string
;
2679 /* Ring the bell! */
2680 memset(buffer
, 6, SMALL_STRING_LEN
);
2682 string
= memcached_string_create(memc
, NULL
, 100);
2685 for (x
= 0; x
< 1024; x
++)
2687 memcached_return rc
;
2688 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2689 assert(rc
== MEMCACHED_SUCCESS
);
2691 memcached_string_free(string
);
2693 return TEST_SUCCESS
;
2696 static test_return
string_alloc_append_toobig(memcached_st
*memc
)
2698 memcached_return rc
;
2700 char buffer
[SMALL_STRING_LEN
];
2701 memcached_string_st
*string
;
2703 /* Ring the bell! */
2704 memset(buffer
, 6, SMALL_STRING_LEN
);
2706 string
= memcached_string_create(memc
, NULL
, 100);
2709 for (x
= 0; x
< 1024; x
++)
2711 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
2712 assert(rc
== MEMCACHED_SUCCESS
);
2714 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
2715 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
2716 memcached_string_free(string
);
2718 return TEST_SUCCESS
;
2721 static test_return
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
2723 pairs_free(global_pairs
);
2725 return TEST_SUCCESS
;
2728 static test_return
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
2730 unsigned long long x
;
2731 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
2732 global_count
= GLOBAL_COUNT
;
2734 for (x
= 0; x
< global_count
; x
++)
2736 global_keys
[x
]= global_pairs
[x
].key
;
2737 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2740 return TEST_SUCCESS
;
2743 static test_return
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
2745 unsigned long long x
;
2746 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
2747 global_count
= GLOBAL2_COUNT
;
2749 for (x
= 0; x
< global_count
; x
++)
2751 global_keys
[x
]= global_pairs
[x
].key
;
2752 global_keys_length
[x
]= global_pairs
[x
].key_length
;
2755 return TEST_SUCCESS
;
2758 static test_return
generate_data(memcached_st
*memc
)
2760 execute_set(memc
, global_pairs
, global_count
);
2762 return TEST_SUCCESS
;
2765 static test_return
generate_data_with_stats(memcached_st
*memc
)
2767 memcached_stat_st
*stat_p
;
2768 memcached_return rc
;
2769 uint32_t host_index
= 0;
2770 execute_set(memc
, global_pairs
, global_count
);
2772 //TODO: hosts used size stats
2773 stat_p
= memcached_stat(memc
, NULL
, &rc
);
2776 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
2778 /* This test was changes so that "make test" would work properlly */
2780 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
);
2782 assert((unsigned long long)(stat_p
+ host_index
)->bytes
);
2785 memcached_stat_free(NULL
, stat_p
);
2787 return TEST_SUCCESS
;
2789 static test_return
generate_buffer_data(memcached_st
*memc
)
2794 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2795 generate_data(memc
);
2797 return TEST_SUCCESS
;
2800 static test_return
get_read_count(memcached_st
*memc
)
2803 memcached_return rc
;
2804 memcached_st
*memc_clone
;
2806 memc_clone
= memcached_clone(NULL
, memc
);
2809 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
2813 size_t return_value_length
;
2817 for (x
= count
= 0; x
< global_count
; x
++)
2819 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
2820 &return_value_length
, &flags
, &rc
);
2821 if (rc
== MEMCACHED_SUCCESS
)
2828 fprintf(stderr
, "\t%u -> %u", global_count
, count
);
2831 memcached_free(memc_clone
);
2833 return TEST_SUCCESS
;
2836 static test_return
get_read(memcached_st
*memc
)
2839 memcached_return rc
;
2843 size_t return_value_length
;
2846 for (x
= 0; x
< global_count
; x
++)
2848 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
2849 &return_value_length
, &flags
, &rc
);
2851 assert(return_value);
2852 assert(rc == MEMCACHED_SUCCESS);
2854 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
2859 return TEST_SUCCESS
;
2862 static test_return
mget_read(memcached_st
*memc
)
2864 memcached_return rc
;
2866 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2867 assert(rc
== MEMCACHED_SUCCESS
);
2868 /* Turn this into a help function */
2870 char return_key
[MEMCACHED_MAX_KEY
];
2871 size_t return_key_length
;
2873 size_t return_value_length
;
2876 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2877 &return_value_length
, &flags
, &rc
)))
2879 assert(return_value
);
2880 assert(rc
== MEMCACHED_SUCCESS
);
2885 return TEST_SUCCESS
;
2888 static test_return
mget_read_result(memcached_st
*memc
)
2890 memcached_return rc
;
2892 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2893 assert(rc
== MEMCACHED_SUCCESS
);
2894 /* Turn this into a help function */
2896 memcached_result_st results_obj
;
2897 memcached_result_st
*results
;
2899 results
= memcached_result_create(memc
, &results_obj
);
2901 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
2904 assert(rc
== MEMCACHED_SUCCESS
);
2907 memcached_result_free(&results_obj
);
2910 return TEST_SUCCESS
;
2913 static test_return
mget_read_function(memcached_st
*memc
)
2915 memcached_return rc
;
2916 unsigned int counter
;
2917 memcached_execute_function callbacks
[1];
2919 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
2920 assert(rc
== MEMCACHED_SUCCESS
);
2922 callbacks
[0]= &callback_counter
;
2924 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
2926 return TEST_SUCCESS
;
2929 static test_return
delete_generate(memcached_st
*memc
)
2933 for (x
= 0; x
< global_count
; x
++)
2935 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2938 return TEST_SUCCESS
;
2941 static test_return
delete_buffer_generate(memcached_st
*memc
)
2947 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
2949 for (x
= 0; x
< global_count
; x
++)
2951 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
2954 return TEST_SUCCESS
;
2957 static test_return
add_host_test1(memcached_st
*memc
)
2960 memcached_return rc
;
2961 char servername
[]= "0.example.com";
2962 memcached_server_st
*servers
;
2964 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
2966 assert(1 == memcached_server_list_count(servers
));
2968 for (x
= 2; x
< 20; x
++)
2970 char buffer
[SMALL_STRING_LEN
];
2972 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
2973 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
2975 assert(rc
== MEMCACHED_SUCCESS
);
2976 assert(x
== memcached_server_list_count(servers
));
2979 rc
= memcached_server_push(memc
, servers
);
2980 assert(rc
== MEMCACHED_SUCCESS
);
2981 rc
= memcached_server_push(memc
, servers
);
2982 assert(rc
== MEMCACHED_SUCCESS
);
2984 memcached_server_list_free(servers
);
2986 return TEST_SUCCESS
;
2989 static memcached_return
pre_nonblock(memcached_st
*memc
)
2991 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
2993 return MEMCACHED_SUCCESS
;
2996 static memcached_return
pre_nonblock_binary(memcached_st
*memc
)
2998 memcached_return rc
= MEMCACHED_FAILURE
;
2999 memcached_st
*memc_clone
;
3001 memc_clone
= memcached_clone(NULL
, memc
);
3003 // The memcached_version needs to be done on a clone, because the server
3004 // will not toggle protocol on an connection.
3005 memcached_version(memc_clone
);
3007 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3009 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3010 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3011 assert(rc
== MEMCACHED_SUCCESS
);
3012 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3015 memcached_free(memc_clone
);
3019 static memcached_return
pre_murmur(memcached_st
*memc
)
3021 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3023 return MEMCACHED_SUCCESS
;
3026 static memcached_return
pre_jenkins(memcached_st
*memc
)
3028 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3030 return MEMCACHED_SUCCESS
;
3034 static memcached_return
pre_md5(memcached_st
*memc
)
3036 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3038 return MEMCACHED_SUCCESS
;
3041 static memcached_return
pre_crc(memcached_st
*memc
)
3043 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3045 return MEMCACHED_SUCCESS
;
3048 static memcached_return
pre_hsieh(memcached_st
*memc
)
3050 #ifdef HAVE_HSIEH_HASH
3051 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3052 return MEMCACHED_SUCCESS
;
3055 return MEMCACHED_FAILURE
;
3059 static memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
3061 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_64
);
3063 return MEMCACHED_SUCCESS
;
3066 static memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
3068 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3070 return MEMCACHED_SUCCESS
;
3073 static memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
3075 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3077 return MEMCACHED_SUCCESS
;
3080 static memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
3082 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3084 return MEMCACHED_SUCCESS
;
3087 static memcached_return
pre_behavior_ketama(memcached_st
*memc
)
3089 memcached_return rc
;
3092 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3093 assert(rc
== MEMCACHED_SUCCESS
);
3095 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3098 return MEMCACHED_SUCCESS
;
3101 static memcached_return
pre_behavior_ketama_weighted(memcached_st
*memc
)
3103 memcached_return rc
;
3106 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3107 assert(rc
== MEMCACHED_SUCCESS
);
3109 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3112 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3113 assert(rc
== MEMCACHED_SUCCESS
);
3115 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3116 assert(value
== MEMCACHED_HASH_MD5
);
3117 return MEMCACHED_SUCCESS
;
3120 static memcached_return
pre_binary(memcached_st
*memc
)
3122 memcached_return rc
= MEMCACHED_FAILURE
;
3123 memcached_st
*memc_clone
;
3125 memc_clone
= memcached_clone(NULL
, memc
);
3127 // The memcached_version needs to be done on a clone, because the server
3128 // will not toggle protocol on an connection.
3129 memcached_version(memc_clone
);
3131 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3133 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3134 assert(rc
== MEMCACHED_SUCCESS
);
3135 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3138 memcached_free(memc_clone
);
3142 static memcached_return
pre_replication(memcached_st
*memc
)
3144 memcached_return rc
= MEMCACHED_FAILURE
;
3145 if (pre_binary(memc
) == MEMCACHED_SUCCESS
)
3148 * Make sure that we store the item on all servers
3149 * (master + replicas == number of servers)
3151 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3152 memc
->number_of_hosts
- 1);
3153 assert(rc
== MEMCACHED_SUCCESS
);
3154 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3160 static memcached_return
pre_replication_noblock(memcached_st
*memc
)
3162 memcached_return rc
= MEMCACHED_FAILURE
;
3163 if (pre_replication(memc
) == MEMCACHED_SUCCESS
&&
3164 pre_nonblock(memc
) == MEMCACHED_SUCCESS
)
3165 rc
= MEMCACHED_SUCCESS
;
3170 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3175 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3177 void *ret
= malloc(size
);
3179 memset(ret
, 0xff, size
);
3184 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3186 return realloc(mem
, size
);
3189 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3191 return calloc(nelem
, size
);
3194 static memcached_return
set_prefix(memcached_st
*memc
)
3196 memcached_return rc
;
3197 const char *key
= "mine";
3200 /* Make sure be default none exists */
3201 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3202 assert(rc
== MEMCACHED_FAILURE
);
3204 /* Test a clean set */
3205 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3206 assert(rc
== MEMCACHED_SUCCESS
);
3208 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3209 assert(memcmp(value
, key
, 4) == 0);
3210 assert(rc
== MEMCACHED_SUCCESS
);
3212 /* Test that we can turn it off */
3213 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3214 assert(rc
== MEMCACHED_SUCCESS
);
3216 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3217 assert(rc
== MEMCACHED_FAILURE
);
3219 /* Now setup for main test */
3220 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3221 assert(rc
== MEMCACHED_SUCCESS
);
3223 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3224 assert(rc
== MEMCACHED_SUCCESS
);
3225 assert(memcmp(value
, key
, 4) == 0);
3227 /* Set to Zero, and then Set to something too large */
3230 memset(long_key
, 0, 255);
3232 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3233 assert(rc
== MEMCACHED_SUCCESS
);
3235 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3236 assert(rc
== MEMCACHED_FAILURE
);
3237 assert(value
== NULL
);
3239 /* Test a long key for failure */
3240 /* TODO, extend test to determine based on setting, what result should be */
3241 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3242 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3243 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3244 assert(rc
== MEMCACHED_SUCCESS
);
3246 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3247 strcpy(long_key
, "This is more then the allotted number of characters");
3248 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3249 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3251 /* Test for a bad prefix, but with a short key */
3252 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3253 assert(rc
== MEMCACHED_SUCCESS
);
3255 strcpy(long_key
, "dog cat");
3256 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3257 assert(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3260 return MEMCACHED_SUCCESS
;
3263 #ifdef MEMCACHED_ENABLE_DEPRECATED
3264 static memcached_return
deprecated_set_memory_alloc(memcached_st
*memc
)
3266 void *test_ptr
= NULL
;
3269 memcached_malloc_function malloc_cb
=
3270 (memcached_malloc_function
)my_malloc
;
3271 cb_ptr
= *(void **)&malloc_cb
;
3272 memcached_return rc
;
3274 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3275 assert(rc
== MEMCACHED_SUCCESS
);
3276 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3277 assert(rc
== MEMCACHED_SUCCESS
);
3278 assert(test_ptr
== cb_ptr
);
3282 memcached_realloc_function realloc_cb
=
3283 (memcached_realloc_function
)my_realloc
;
3284 cb_ptr
= *(void **)&realloc_cb
;
3285 memcached_return rc
;
3287 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3288 assert(rc
== MEMCACHED_SUCCESS
);
3289 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3290 assert(rc
== MEMCACHED_SUCCESS
);
3291 assert(test_ptr
== cb_ptr
);
3295 memcached_free_function free_cb
=
3296 (memcached_free_function
)my_free
;
3297 cb_ptr
= *(void **)&free_cb
;
3298 memcached_return rc
;
3300 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3301 assert(rc
== MEMCACHED_SUCCESS
);
3302 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3303 assert(rc
== MEMCACHED_SUCCESS
);
3304 assert(test_ptr
== cb_ptr
);
3306 return MEMCACHED_SUCCESS
;
3310 static memcached_return
set_memory_alloc(memcached_st
*memc
)
3312 memcached_return rc
;
3313 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3314 my_realloc
, my_calloc
);
3315 assert(rc
== MEMCACHED_FAILURE
);
3317 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3318 my_realloc
, my_calloc
);
3320 memcached_malloc_function mem_malloc
;
3321 memcached_free_function mem_free
;
3322 memcached_realloc_function mem_realloc
;
3323 memcached_calloc_function mem_calloc
;
3324 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3325 &mem_realloc
, &mem_calloc
);
3327 assert(mem_malloc
== my_malloc
);
3328 assert(mem_realloc
== my_realloc
);
3329 assert(mem_calloc
== my_calloc
);
3330 assert(mem_free
== my_free
);
3332 return MEMCACHED_SUCCESS
;
3335 static memcached_return
enable_consistent(memcached_st
*memc
)
3337 memcached_server_distribution value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3338 memcached_hash hash
;
3339 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3340 if (pre_hsieh(memc
) != MEMCACHED_SUCCESS
)
3341 return MEMCACHED_FAILURE
;
3343 value
= (memcached_server_distribution
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3344 assert(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3346 hash
= (memcached_hash
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3347 assert(hash
== MEMCACHED_HASH_HSIEH
);
3350 return MEMCACHED_SUCCESS
;
3353 static memcached_return
enable_cas(memcached_st
*memc
)
3355 unsigned int set
= 1;
3357 memcached_version(memc
);
3359 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3360 || memc
->hosts
[0].minor_version
> 2)
3362 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3364 return MEMCACHED_SUCCESS
;
3367 return MEMCACHED_FAILURE
;
3370 static memcached_return
check_for_1_2_3(memcached_st
*memc
)
3372 memcached_version(memc
);
3374 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3375 || memc
->hosts
[0].minor_version
> 2)
3376 return MEMCACHED_SUCCESS
;
3378 return MEMCACHED_FAILURE
;
3381 static memcached_return
pre_unix_socket(memcached_st
*memc
)
3383 memcached_return rc
;
3386 memcached_server_list_free(memc
->hosts
);
3388 memc
->number_of_hosts
= 0;
3390 if (stat("/tmp/memcached.socket", &buf
))
3391 return MEMCACHED_FAILURE
;
3393 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3398 static memcached_return
pre_nodelay(memcached_st
*memc
)
3400 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3401 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3403 return MEMCACHED_SUCCESS
;
3406 static memcached_return
pre_settimer(memcached_st
*memc
)
3408 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3409 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3411 return MEMCACHED_SUCCESS
;
3414 static memcached_return
poll_timeout(memcached_st
*memc
)
3420 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3422 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3424 assert(timeout
== 100);
3426 return MEMCACHED_SUCCESS
;
3429 static test_return
noreply_test(memcached_st
*memc
)
3431 memcached_return ret
;
3432 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3433 assert(ret
== MEMCACHED_SUCCESS
);
3434 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3435 assert(ret
== MEMCACHED_SUCCESS
);
3436 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3437 assert(ret
== MEMCACHED_SUCCESS
);
3438 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3439 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3440 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3442 for (int count
=0; count
< 5; ++count
)
3444 for (int x
=0; x
< 100; ++x
)
3447 size_t len
= (size_t)sprintf(key
, "%d", x
);
3451 ret
=memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3454 ret
=memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3457 ret
=memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3460 ret
=memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3463 ret
=memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3469 assert(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3473 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3474 ** API and is _ONLY_ done this way to verify that the library works the
3475 ** way it is supposed to do!!!!
3478 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3479 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3481 assert(no_msg
== 0);
3482 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3485 ** Now validate that all items was set properly!
3487 for (int x
=0; x
< 100; ++x
)
3490 size_t len
= (size_t)sprintf(key
, "%d", x
);
3493 char* value
=memcached_get(memc
, key
, strlen(key
),
3494 &length
, &flags
, &ret
);
3495 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3498 case 0: /* FALLTHROUGH */
3499 case 1: /* FALLTHROUGH */
3501 assert(strncmp(value
, key
, len
) == 0);
3502 assert(len
== length
);
3505 assert(length
== len
* 2);
3508 assert(length
== len
* 3);
3518 /* Try setting an illegal cas value (should not return an error to
3519 * the caller (because we don't expect a return message from the server)
3521 const char* keys
[]= {"0"};
3522 size_t lengths
[]= {1};
3525 memcached_result_st results_obj
;
3526 memcached_result_st
*results
;
3527 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3528 assert(ret
== MEMCACHED_SUCCESS
);
3530 results
= memcached_result_create(memc
, &results_obj
);
3532 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3534 assert(ret
== MEMCACHED_SUCCESS
);
3535 uint64_t cas
= memcached_result_cas(results
);
3536 memcached_result_free(&results_obj
);
3538 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3539 assert(ret
== MEMCACHED_SUCCESS
);
3542 * The item will have a new cas value, so try to set it again with the old
3543 * value. This should fail!
3545 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3546 assert(ret
== MEMCACHED_SUCCESS
);
3547 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3548 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3549 assert(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3552 return TEST_SUCCESS
;
3555 static test_return
analyzer_test(memcached_st
*memc
)
3557 memcached_return rc
;
3558 memcached_stat_st
*memc_stat
;
3559 memcached_analysis_st
*report
;
3561 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3562 assert(rc
== MEMCACHED_SUCCESS
);
3565 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3566 assert(rc
== MEMCACHED_SUCCESS
);
3570 memcached_stat_free(NULL
, memc_stat
);
3572 return TEST_SUCCESS
;
3575 /* Count the objects */
3576 static memcached_return
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3577 const char *key
__attribute__((unused
)),
3578 size_t key_length
__attribute__((unused
)),
3581 uint32_t *counter
= (uint32_t *)context
;
3583 *counter
= *counter
+ 1;
3585 return MEMCACHED_SUCCESS
;
3588 static test_return
dump_test(memcached_st
*memc
)
3590 memcached_return rc
;
3591 uint32_t counter
= 0;
3592 memcached_dump_func callbacks
[1];
3593 test_return main_rc
;
3595 callbacks
[0]= &callback_dump_counter
;
3597 /* No support for Binary protocol yet */
3598 if (memc
->flags
& MEM_BINARY_PROTOCOL
)
3599 return TEST_SUCCESS
;
3601 main_rc
= set_test3(memc
);
3603 assert (main_rc
== TEST_SUCCESS
);
3605 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3606 assert(rc
== MEMCACHED_SUCCESS
);
3608 /* We may have more then 32 if our previous flush has not completed */
3609 assert(counter
>= 32);
3611 return TEST_SUCCESS
;
3614 #ifdef HAVE_LIBMEMCACHEDUTIL
3615 static void* connection_release(void *arg
) {
3617 memcached_pool_st
* pool
;
3622 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
3626 static test_return
connection_pool_test(memcached_st
*memc
)
3628 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
3629 assert(pool
!= NULL
);
3630 memcached_st
* mmc
[10];
3631 memcached_return rc
;
3633 for (int x
= 0; x
< 10; ++x
) {
3634 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
3635 assert(mmc
[x
] != NULL
);
3636 assert(rc
== MEMCACHED_SUCCESS
);
3639 assert(memcached_pool_pop(pool
, false, &rc
) == NULL
);
3640 assert(rc
== MEMCACHED_SUCCESS
);
3644 memcached_pool_st
* pool
;
3646 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
3647 pthread_create(&tid
, NULL
, connection_release
, &item
);
3648 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
3649 assert(rc
== MEMCACHED_SUCCESS
);
3650 pthread_join(tid
, NULL
);
3651 assert(mmc
[9] == item
.mmc
);
3652 const char *key
= "key";
3653 size_t keylen
= strlen(key
);
3655 // verify that I can do ops with all connections
3656 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
3657 assert(rc
== MEMCACHED_SUCCESS
);
3659 for (unsigned int x
= 0; x
< 10; ++x
) {
3660 uint64_t number_value
;
3661 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
3662 assert(rc
== MEMCACHED_SUCCESS
);
3663 assert(number_value
== (x
+1));
3667 for (int x
= 0; x
< 10; ++x
)
3668 assert(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
3671 /* verify that I can set behaviors on the pool when I don't have all
3672 * of the connections in the pool. It should however be enabled
3673 * when I push the item into the pool
3675 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
3676 assert(mmc
[0] != NULL
);
3678 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
3679 assert(rc
== MEMCACHED_SUCCESS
);
3681 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
3682 assert(mmc
[1] != NULL
);
3684 assert(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
3685 assert(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
3686 assert(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
3688 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
3689 assert(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
3690 assert(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
3693 assert(memcached_pool_destroy(pool
) == memc
);
3694 return TEST_SUCCESS
;
3698 static test_return
replication_set_test(memcached_st
*memc
)
3700 memcached_return rc
;
3701 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3702 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3704 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
3705 assert(rc
== MEMCACHED_SUCCESS
);
3708 ** We are using the quiet commands to store the replicas, so we need
3709 ** to ensure that all of them are processed before we can continue.
3710 ** In the test we go directly from storing the object to trying to
3711 ** receive the object from all of the different servers, so we
3712 ** could end up in a race condition (the memcached server hasn't yet
3713 ** processed the quiet command from the replication set when it process
3714 ** the request from the other client (created by the clone)). As a
3715 ** workaround for that we call memcached_quit to send the quit command
3716 ** to the server and wait for the response ;-) If you use the test code
3717 ** as an example for your own code, please note that you shouldn't need
3720 memcached_quit(memc
);
3723 ** "bubba" should now be stored on all of our servers. We don't have an
3724 ** easy to use API to address each individual server, so I'll just iterate
3725 ** through a bunch of "master keys" and I should most likely hit all of the
3728 for (int x
= 'a'; x
<= 'z'; ++x
)
3730 char key
[2]= { [0]= (char)x
};
3733 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3735 assert(rc
== MEMCACHED_SUCCESS
);
3736 assert(val
!= NULL
);
3740 memcached_free(memc_clone
);
3742 return TEST_SUCCESS
;
3745 static test_return
replication_get_test(memcached_st
*memc
)
3747 memcached_return rc
;
3750 * Don't do the following in your code. I am abusing the internal details
3751 * within the library, and this is not a supported interface.
3752 * This is to verify correct behavior in the library
3754 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
3756 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3757 memc_clone
->hosts
[host
].port
= 0;
3759 for (int x
= 'a'; x
<= 'z'; ++x
)
3761 char key
[2]= { [0]= (char)x
};
3764 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
3766 assert(rc
== MEMCACHED_SUCCESS
);
3767 assert(val
!= NULL
);
3771 memcached_free(memc_clone
);
3774 return TEST_SUCCESS
;
3777 static test_return
replication_mget_test(memcached_st
*memc
)
3779 memcached_return rc
;
3780 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3781 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
3783 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3784 size_t len
[]= { 5, 4, 4, 4 };
3786 for (int x
=0; x
< 4; ++x
)
3788 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
3789 assert(rc
== MEMCACHED_SUCCESS
);
3793 ** We are using the quiet commands to store the replicas, so we need
3794 ** to ensure that all of them are processed before we can continue.
3795 ** In the test we go directly from storing the object to trying to
3796 ** receive the object from all of the different servers, so we
3797 ** could end up in a race condition (the memcached server hasn't yet
3798 ** processed the quiet command from the replication set when it process
3799 ** the request from the other client (created by the clone)). As a
3800 ** workaround for that we call memcached_quit to send the quit command
3801 ** to the server and wait for the response ;-) If you use the test code
3802 ** as an example for your own code, please note that you shouldn't need
3805 memcached_quit(memc
);
3808 * Don't do the following in your code. I am abusing the internal details
3809 * within the library, and this is not a supported interface.
3810 * This is to verify correct behavior in the library
3812 memcached_result_st result_obj
;
3813 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
3815 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
3816 new_clone
->hosts
[host
].port
= 0;
3818 for (int x
= 'a'; x
<= 'z'; ++x
)
3820 const char key
[2]= { [0]= (const char)x
};
3822 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
3823 assert(rc
== MEMCACHED_SUCCESS
);
3825 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
3829 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
3834 memcached_result_free(&result_obj
);
3837 memcached_free(new_clone
);
3840 memcached_free(memc_clone
);
3842 return TEST_SUCCESS
;
3845 static test_return
replication_delete_test(memcached_st
*memc
)
3847 memcached_return rc
;
3848 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3849 /* Delete the items from all of the servers except 1 */
3850 uint64_t repl
= memcached_behavior_get(memc
,
3851 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
3852 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
3854 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
3855 size_t len
[]= { 5, 4, 4, 4 };
3857 for (int x
=0; x
< 4; ++x
)
3859 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
3860 assert(rc
== MEMCACHED_SUCCESS
);
3864 * Don't do the following in your code. I am abusing the internal details
3865 * within the library, and this is not a supported interface.
3866 * This is to verify correct behavior in the library
3868 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
3869 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
3871 memc_clone
->hosts
[hash
].port
= 0;
3872 if (++hash
== memc_clone
->number_of_hosts
)
3876 memcached_result_st result_obj
;
3877 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
3879 for (int x
= 'a'; x
<= 'z'; ++x
)
3881 const char key
[2]= { [0]= (const char)x
};
3883 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
3884 assert(rc
== MEMCACHED_SUCCESS
);
3886 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
3890 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
3895 memcached_result_free(&result_obj
);
3898 memcached_free(memc_clone
);
3900 return TEST_SUCCESS
;
3903 static void increment_request_id(uint16_t *id
)
3906 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
3910 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
3912 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
3913 assert(ids
!= NULL
);
3916 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3917 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
3922 static test_return
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
3925 memcached_server_st
*cur_server
= memc
->hosts
;
3926 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
3928 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
3930 assert(cur_server
[x
].cursor_active
== 0);
3931 assert(cur_req_ids
[x
] == expected_req_ids
[x
]);
3933 free(expected_req_ids
);
3936 return TEST_SUCCESS
;
3940 ** There is a little bit of a hack here, instead of removing
3941 ** the servers, I just set num host to 0 and them add then new udp servers
3943 static memcached_return
init_udp(memcached_st
*memc
)
3945 memcached_version(memc
);
3946 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3947 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
3948 || memc
->hosts
[0].micro_version
< 6)
3949 return MEMCACHED_FAILURE
;
3951 uint32_t num_hosts
= memc
->number_of_hosts
;
3953 memcached_server_st servers
[num_hosts
];
3954 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
3955 for (x
= 0; x
< num_hosts
; x
++)
3956 memcached_server_free(&memc
->hosts
[x
]);
3958 memc
->number_of_hosts
= 0;
3959 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
3960 for (x
= 0; x
< num_hosts
; x
++)
3962 assert(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
3963 assert(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
3966 return MEMCACHED_SUCCESS
;
3969 static memcached_return
binary_init_udp(memcached_st
*memc
)
3972 return init_udp(memc
);
3975 /* Make sure that I cant add a tcp server to a udp client */
3976 static test_return
add_tcp_server_udp_client_test(memcached_st
*memc
)
3978 memcached_server_st server
;
3979 memcached_server_clone(&server
, &memc
->hosts
[0]);
3980 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3981 assert(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3982 return TEST_SUCCESS
;
3985 /* Make sure that I cant add a udp server to a tcp client */
3986 static test_return
add_udp_server_tcp_client_test(memcached_st
*memc
)
3988 memcached_server_st server
;
3989 memcached_server_clone(&server
, &memc
->hosts
[0]);
3990 assert(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
3992 memcached_st tcp_client
;
3993 memcached_create(&tcp_client
);
3994 assert(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
3995 return TEST_SUCCESS
;
3998 static test_return
set_udp_behavior_test(memcached_st
*memc
)
4001 memcached_quit(memc
);
4002 memc
->number_of_hosts
= 0;
4003 run_distribution(memc
);
4004 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
4005 assert(memc
->flags
& MEM_USE_UDP
);
4006 assert(memc
->flags
& MEM_NOREPLY
);;
4008 assert(memc
->number_of_hosts
== 0);
4010 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
4011 assert(!(memc
->flags
& MEM_USE_UDP
));
4012 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
4013 assert(!(memc
->flags
& MEM_NOREPLY
));
4014 return TEST_SUCCESS
;
4017 static test_return
udp_set_test(memcached_st
*memc
)
4020 unsigned int num_iters
= 1025; //request id rolls over at 1024
4021 for (x
= 0; x
< num_iters
;x
++)
4023 memcached_return rc
;
4024 const char *key
= "foo";
4025 const char *value
= "when we sanitize";
4026 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4027 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
4028 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4029 rc
= memcached_set(memc
, key
, strlen(key
),
4030 value
, strlen(value
),
4031 (time_t)0, (uint32_t)0);
4032 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4033 /** NB, the check below assumes that if new write_ptr is less than
4034 * the original write_ptr that we have flushed. For large payloads, this
4035 * maybe an invalid assumption, but for the small payload we have it is OK
4037 if (rc
== MEMCACHED_SUCCESS
||
4038 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4039 increment_request_id(&expected_ids
[server_key
]);
4041 if (rc
== MEMCACHED_SUCCESS
)
4043 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4047 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4048 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4050 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4052 return TEST_SUCCESS
;
4055 static test_return
udp_buffered_set_test(memcached_st
*memc
)
4057 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4058 return udp_set_test(memc
);
4061 static test_return
udp_set_too_big_test(memcached_st
*memc
)
4063 memcached_return rc
;
4064 const char *key
= "bar";
4065 char value
[MAX_UDP_DATAGRAM_LENGTH
];
4066 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4067 rc
= memcached_set(memc
, key
, strlen(key
),
4068 value
, MAX_UDP_DATAGRAM_LENGTH
,
4069 (time_t)0, (uint32_t)0);
4070 assert(rc
== MEMCACHED_WRITE_FAILURE
);
4071 return post_udp_op_check(memc
,expected_ids
);
4074 static test_return
udp_delete_test(memcached_st
*memc
)
4077 unsigned int num_iters
= 1025; //request id rolls over at 1024
4078 for (x
= 0; x
< num_iters
;x
++)
4080 memcached_return rc
;
4081 const char *key
= "foo";
4082 uint16_t *expected_ids
=get_udp_request_ids(memc
);
4083 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4084 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4085 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
4086 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4087 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4088 increment_request_id(&expected_ids
[server_key
]);
4089 if (rc
== MEMCACHED_SUCCESS
)
4090 assert(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4093 assert(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4094 assert(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4096 assert(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4098 return TEST_SUCCESS
;
4101 static test_return
udp_buffered_delete_test(memcached_st
*memc
)
4103 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4104 return udp_delete_test(memc
);
4107 static test_return
udp_verbosity_test(memcached_st
*memc
)
4109 memcached_return rc
;
4110 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4112 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4113 increment_request_id(&expected_ids
[x
]);
4115 rc
= memcached_verbosity(memc
,3);
4116 assert(rc
== MEMCACHED_SUCCESS
);
4117 return post_udp_op_check(memc
,expected_ids
);
4120 static test_return
udp_quit_test(memcached_st
*memc
)
4122 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4123 memcached_quit(memc
);
4124 return post_udp_op_check(memc
, expected_ids
);
4127 static test_return
udp_flush_test(memcached_st
*memc
)
4129 memcached_return rc
;
4130 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4132 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4133 increment_request_id(&expected_ids
[x
]);
4135 rc
= memcached_flush(memc
,0);
4136 assert(rc
== MEMCACHED_SUCCESS
);
4137 return post_udp_op_check(memc
,expected_ids
);
4140 static test_return
udp_incr_test(memcached_st
*memc
)
4142 memcached_return rc
;
4143 const char *key
= "incr";
4144 const char *value
= "1";
4145 rc
= memcached_set(memc
, key
, strlen(key
),
4146 value
, strlen(value
),
4147 (time_t)0, (uint32_t)0);
4149 assert(rc
== MEMCACHED_SUCCESS
);
4150 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4151 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4152 increment_request_id(&expected_ids
[server_key
]);
4154 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4155 assert(rc
== MEMCACHED_SUCCESS
);
4156 return post_udp_op_check(memc
, expected_ids
);
4159 static test_return
udp_decr_test(memcached_st
*memc
)
4161 memcached_return rc
;
4162 const char *key
= "decr";
4163 const char *value
= "1";
4164 rc
= memcached_set(memc
, key
, strlen(key
),
4165 value
, strlen(value
),
4166 (time_t)0, (uint32_t)0);
4168 assert(rc
== MEMCACHED_SUCCESS
);
4169 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4170 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4171 increment_request_id(&expected_ids
[server_key
]);
4173 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4174 assert(rc
== MEMCACHED_SUCCESS
);
4175 return post_udp_op_check(memc
, expected_ids
);
4179 static test_return
udp_stat_test(memcached_st
*memc
)
4181 memcached_stat_st
* rv
= NULL
;
4182 memcached_return rc
;
4184 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4185 rv
= memcached_stat(memc
, args
, &rc
);
4187 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4188 return post_udp_op_check(memc
, expected_ids
);
4191 static test_return
udp_version_test(memcached_st
*memc
)
4193 memcached_return rc
;
4194 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4195 rc
= memcached_version(memc
);
4196 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4197 return post_udp_op_check(memc
, expected_ids
);
4200 static test_return
udp_get_test(memcached_st
*memc
)
4202 memcached_return rc
;
4203 const char *key
= "foo";
4205 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4206 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4207 assert(rc
== MEMCACHED_NOT_SUPPORTED
);
4208 assert(val
== NULL
);
4209 return post_udp_op_check(memc
, expected_ids
);
4212 static test_return
udp_mixed_io_test(memcached_st
*memc
)
4215 test_st mixed_io_ops
[] ={
4216 {"udp_set_test", 0, udp_set_test
},
4217 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4218 {"udp_delete_test", 0, udp_delete_test
},
4219 {"udp_verbosity_test", 0, udp_verbosity_test
},
4220 {"udp_quit_test", 0, udp_quit_test
},
4221 {"udp_flush_test", 0, udp_flush_test
},
4222 {"udp_incr_test", 0, udp_incr_test
},
4223 {"udp_decr_test", 0, udp_decr_test
},
4224 {"udp_version_test", 0, udp_version_test
}
4227 for (x
= 0; x
< 500; x
++)
4229 current_op
= mixed_io_ops
[random() % 9];
4230 assert(current_op
.function(memc
) == TEST_SUCCESS
);
4232 return TEST_SUCCESS
;
4235 static test_return
hsieh_avaibility_test (memcached_st
*memc
)
4237 memcached_return expected_rc
= MEMCACHED_FAILURE
;
4238 #ifdef HAVE_HSIEH_HASH
4239 expected_rc
= MEMCACHED_SUCCESS
;
4241 memcached_return rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4242 (uint64_t)MEMCACHED_HASH_HSIEH
);
4243 assert(rc
== expected_rc
);
4244 return TEST_SUCCESS
;
4247 static const char *list
[]=
4277 static test_return
md5_run (memcached_st
*memc
__attribute__((unused
)))
4281 uint32_t values
[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4282 245758794U, 2550894432U, 121710495U, 3053817768U,
4283 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4284 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4285 2138080750U, 217675895U, 2230934345U, 1234361223U,
4286 3968582726U, 2455685270U, 1293568479U, 199067604U,
4290 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4294 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4295 assert(values
[x
] == hash_val
);
4298 return TEST_SUCCESS
;
4301 static test_return
crc_run (memcached_st
*memc
__attribute__((unused
)))
4305 uint32_t values
[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4306 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4307 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4308 17300U, 28156U, 9192U, 20484U, 16925U };
4310 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4314 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4315 assert(values
[x
] == hash_val
);
4318 return TEST_SUCCESS
;
4321 static test_return
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4325 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4326 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4327 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4328 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4329 2815549194U, 2562818319U, 224996066U, 2680194749U,
4330 3035305390U, 246890365U, 2395624193U, 4145193337U,
4333 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4337 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4338 assert(values
[x
] == hash_val
);
4341 return TEST_SUCCESS
;
4344 static test_return
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4348 uint32_t values
[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4349 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4350 734810122U, 1516407546U, 560948863U, 1767346780U,
4351 561034892U, 4156330026U, 3716417003U, 3475297030U,
4352 1518272172U, 227211583U, 3938128828U, 126112909U,
4353 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4356 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4360 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4361 assert(values
[x
] == hash_val
);
4364 return TEST_SUCCESS
;
4367 static test_return
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4371 uint32_t values
[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4372 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4373 344481048U, 3865235296U, 2181839183U, 119581266U,
4374 510234242U, 4248244304U, 1362796839U, 103389328U,
4375 1449620010U, 182962511U, 3554262370U, 3206747549U,
4376 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4380 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4384 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4385 assert(values
[x
] == hash_val
);
4388 return TEST_SUCCESS
;
4391 static test_return
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4395 uint32_t values
[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4396 459261019U, 3521085446U, 18738364U, 1625305005U,
4397 2162232970U, 777243802U, 3323728671U, 132336572U,
4398 3654473228U, 260679466U, 1169454059U, 2698319462U,
4399 1062177260U, 235516991U, 2218399068U, 405302637U,
4400 1128467232U, 3579622413U, 2138539289U, 96429129U,
4403 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4407 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4408 assert(values
[x
] == hash_val
);
4411 return TEST_SUCCESS
;
4414 static test_return
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4418 #ifdef HAVE_HSIEH_HASH
4419 uint32_t values
[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4420 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4421 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4422 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4425 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 };
4428 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4432 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4433 assert(values
[x
] == hash_val
);
4436 return TEST_SUCCESS
;
4439 static test_return
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4443 uint32_t values
[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4444 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4445 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4446 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4447 2815549194U, 2562818319U, 224996066U, 2680194749U,
4448 3035305390U, 246890365U, 2395624193U, 4145193337U,
4451 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4455 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4456 assert(values
[x
] == hash_val
);
4459 return TEST_SUCCESS
;
4462 static test_return
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4466 uint32_t values
[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4467 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4468 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4469 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4470 4153263658U, 3824871822U, 3072759809U, 798622255U,
4471 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4475 for (ptr
= list
, x
= 0; *ptr
; ptr
++, x
++)
4479 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4480 assert(values
[x
] == hash_val
);
4483 return TEST_SUCCESS
;
4486 static test_return
regression_bug_434484(memcached_st
*memc
)
4488 if (pre_binary(memc
) != TEST_SUCCESS
)
4489 return TEST_SUCCESS
;
4491 memcached_return ret
;
4492 const char *key
= "regression_bug_434484";
4493 size_t keylen
= strlen(key
);
4495 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4496 assert(ret
== MEMCACHED_NOTSTORED
);
4498 size_t size
= 2048 * 1024;
4499 void *data
= malloc(size
);
4500 assert(data
!= NULL
);
4501 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
4502 assert(ret
== MEMCACHED_E2BIG
);
4505 return TEST_SUCCESS
;
4508 test_st udp_setup_server_tests
[] ={
4509 {"set_udp_behavior_test", 0, set_udp_behavior_test
},
4510 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test
},
4511 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test
},
4515 test_st upd_io_tests
[] ={
4516 {"udp_set_test", 0, udp_set_test
},
4517 {"udp_buffered_set_test", 0, udp_buffered_set_test
},
4518 {"udp_set_too_big_test", 0, udp_set_too_big_test
},
4519 {"udp_delete_test", 0, udp_delete_test
},
4520 {"udp_buffered_delete_test", 0, udp_buffered_delete_test
},
4521 {"udp_verbosity_test", 0, udp_verbosity_test
},
4522 {"udp_quit_test", 0, udp_quit_test
},
4523 {"udp_flush_test", 0, udp_flush_test
},
4524 {"udp_incr_test", 0, udp_incr_test
},
4525 {"udp_decr_test", 0, udp_decr_test
},
4526 {"udp_stat_test", 0, udp_stat_test
},
4527 {"udp_version_test", 0, udp_version_test
},
4528 {"udp_get_test", 0, udp_get_test
},
4529 {"udp_mixed_io_test", 0, udp_mixed_io_test
},
4533 /* Clean the server before beginning testing */
4535 {"flush", 0, flush_test
},
4536 {"init", 0, init_test
},
4537 {"allocation", 0, allocation_test
},
4538 {"server_list_null_test", 0, server_list_null_test
},
4539 {"server_unsort", 0, server_unsort_test
},
4540 {"server_sort", 0, server_sort_test
},
4541 {"server_sort2", 0, server_sort2_test
},
4542 {"clone_test", 0, clone_test
},
4543 {"connection_test", 0, connection_test
},
4544 {"callback_test", 0, callback_test
},
4545 {"behavior_test", 0, behavior_test
},
4546 {"userdata_test", 0, userdata_test
},
4547 {"error", 0, error_test
},
4548 {"set", 0, set_test
},
4549 {"set2", 0, set_test2
},
4550 {"set3", 0, set_test3
},
4551 {"dump", 1, dump_test
},
4552 {"add", 1, add_test
},
4553 {"replace", 1, replace_test
},
4554 {"delete", 1, delete_test
},
4555 {"get", 1, get_test
},
4556 {"get2", 0, get_test2
},
4557 {"get3", 0, get_test3
},
4558 {"get4", 0, get_test4
},
4559 {"partial mget", 0, get_test5
},
4560 {"stats_servername", 0, stats_servername_test
},
4561 {"increment", 0, increment_test
},
4562 {"increment_with_initial", 1, increment_with_initial_test
},
4563 {"decrement", 0, decrement_test
},
4564 {"decrement_with_initial", 1, decrement_with_initial_test
},
4565 {"quit", 0, quit_test
},
4566 {"mget", 1, mget_test
},
4567 {"mget_result", 1, mget_result_test
},
4568 {"mget_result_alloc", 1, mget_result_alloc_test
},
4569 {"mget_result_function", 1, mget_result_function
},
4570 {"mget_end", 0, mget_end
},
4571 {"get_stats", 0, get_stats
},
4572 {"add_host_test", 0, add_host_test
},
4573 {"add_host_test_1", 0, add_host_test1
},
4574 {"get_stats_keys", 0, get_stats_keys
},
4575 {"behavior_test", 0, get_stats_keys
},
4576 {"callback_test", 0, get_stats_keys
},
4577 {"version_string_test", 0, version_string_test
},
4578 {"bad_key", 1, bad_key_test
},
4579 {"memcached_server_cursor", 1, memcached_server_cursor_test
},
4580 {"read_through", 1, read_through
},
4581 {"delete_through", 1, delete_through
},
4582 {"noreply", 1, noreply_test
},
4583 {"analyzer", 1, analyzer_test
},
4584 #ifdef HAVE_LIBMEMCACHEDUTIL
4585 {"connectionpool", 1, connection_pool_test
},
4590 test_st async_tests
[] ={
4591 {"add", 1, add_wrapper
},
4595 test_st string_tests
[] ={
4596 {"string static with null", 0, string_static_null
},
4597 {"string alloc with null", 0, string_alloc_null
},
4598 {"string alloc with 1K", 0, string_alloc_with_size
},
4599 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig
},
4600 {"string append", 0, string_alloc_append
},
4601 {"string append failure (too big)", 0, string_alloc_append_toobig
},
4605 test_st result_tests
[] ={
4606 {"result static", 0, result_static
},
4607 {"result alloc", 0, result_alloc
},
4611 test_st version_1_2_3
[] ={
4612 {"append", 0, append_test
},
4613 {"prepend", 0, prepend_test
},
4614 {"cas", 0, cas_test
},
4615 {"cas2", 0, cas2_test
},
4616 {"append_binary", 0, append_binary_test
},
4620 test_st user_tests
[] ={
4621 {"user_supplied_bug1", 0, user_supplied_bug1
},
4622 {"user_supplied_bug2", 0, user_supplied_bug2
},
4623 {"user_supplied_bug3", 0, user_supplied_bug3
},
4624 {"user_supplied_bug4", 0, user_supplied_bug4
},
4625 {"user_supplied_bug5", 1, user_supplied_bug5
},
4626 {"user_supplied_bug6", 1, user_supplied_bug6
},
4627 {"user_supplied_bug7", 1, user_supplied_bug7
},
4628 {"user_supplied_bug8", 1, user_supplied_bug8
},
4629 {"user_supplied_bug9", 1, user_supplied_bug9
},
4630 {"user_supplied_bug10", 1, user_supplied_bug10
},
4631 {"user_supplied_bug11", 1, user_supplied_bug11
},
4632 {"user_supplied_bug12", 1, user_supplied_bug12
},
4633 {"user_supplied_bug13", 1, user_supplied_bug13
},
4634 {"user_supplied_bug14", 1, user_supplied_bug14
},
4635 {"user_supplied_bug15", 1, user_supplied_bug15
},
4636 {"user_supplied_bug16", 1, user_supplied_bug16
},
4639 ** It seems to be something weird with the character sets..
4640 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4641 ** guess I need to find out how this is supposed to work.. Perhaps I need
4642 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4643 ** so just disable the code for now...).
4645 {"user_supplied_bug17", 1, user_supplied_bug17
},
4647 {"user_supplied_bug18", 1, user_supplied_bug18
},
4648 {"user_supplied_bug19", 1, user_supplied_bug19
},
4649 {"user_supplied_bug20", 1, user_supplied_bug20
},
4653 test_st replication_tests
[]= {
4654 {"set", 1, replication_set_test
},
4655 {"get", 0, replication_get_test
},
4656 {"mget", 0, replication_mget_test
},
4657 {"delete", 0, replication_delete_test
},
4662 * The following test suite is used to verify that we don't introduce
4663 * regression bugs. If you want more information about the bug / test,
4664 * you should look in the bug report at
4665 * http://bugs.launchpad.net/libmemcached
4667 test_st regression_tests
[]= {
4668 {"lp:434484", 1, regression_bug_434484
},
4672 test_st generate_tests
[] ={
4673 {"generate_pairs", 1, generate_pairs
},
4674 {"generate_data", 1, generate_data
},
4675 {"get_read", 0, get_read
},
4676 {"delete_generate", 0, delete_generate
},
4677 {"generate_buffer_data", 1, generate_buffer_data
},
4678 {"delete_buffer", 0, delete_buffer_generate
},
4679 {"generate_data", 1, generate_data
},
4680 {"mget_read", 0, mget_read
},
4681 {"mget_read_result", 0, mget_read_result
},
4682 {"mget_read_function", 0, mget_read_function
},
4683 {"cleanup", 1, cleanup_pairs
},
4684 {"generate_large_pairs", 1, generate_large_pairs
},
4685 {"generate_data", 1, generate_data
},
4686 {"generate_buffer_data", 1, generate_buffer_data
},
4687 {"cleanup", 1, cleanup_pairs
},
4691 test_st consistent_tests
[] ={
4692 {"generate_pairs", 1, generate_pairs
},
4693 {"generate_data", 1, generate_data
},
4694 {"get_read", 0, get_read_count
},
4695 {"cleanup", 1, cleanup_pairs
},
4699 test_st consistent_weighted_tests
[] ={
4700 {"generate_pairs", 1, generate_pairs
},
4701 {"generate_data", 1, generate_data_with_stats
},
4702 {"get_read", 0, get_read_count
},
4703 {"cleanup", 1, cleanup_pairs
},
4707 test_st hsieh_availability
[] ={
4708 {"hsieh_avaibility_test",0,hsieh_avaibility_test
},
4712 test_st ketama_auto_eject_hosts
[] ={
4713 {"auto_eject_hosts", 1, auto_eject_hosts
},
4717 test_st hash_tests
[] ={
4718 {"md5", 0, md5_run
},
4719 {"crc", 0, crc_run
},
4720 {"fnv1_64", 0, fnv1_64_run
},
4721 {"fnv1a_64", 0, fnv1a_64_run
},
4722 {"fnv1_32", 0, fnv1_32_run
},
4723 {"fnv1a_32", 0, fnv1a_32_run
},
4724 {"hsieh", 0, hsieh_run
},
4725 {"murmur", 0, murmur_run
},
4726 {"jenkis", 0, jenkins_run
},
4730 collection_st collection
[] ={
4731 {"hsieh_availability",0,0,hsieh_availability
},
4732 {"udp_setup", init_udp
, 0, udp_setup_server_tests
},
4733 {"udp_io", init_udp
, 0, upd_io_tests
},
4734 {"udp_binary_io", binary_init_udp
, 0, upd_io_tests
},
4735 {"block", 0, 0, tests
},
4736 {"binary", pre_binary
, 0, tests
},
4737 {"nonblock", pre_nonblock
, 0, tests
},
4738 {"nodelay", pre_nodelay
, 0, tests
},
4739 {"settimer", pre_settimer
, 0, tests
},
4740 {"md5", pre_md5
, 0, tests
},
4741 {"crc", pre_crc
, 0, tests
},
4742 {"hsieh", pre_hsieh
, 0, tests
},
4743 {"jenkins", pre_jenkins
, 0, tests
},
4744 {"fnv1_64", pre_hash_fnv1_64
, 0, tests
},
4745 {"fnv1a_64", pre_hash_fnv1a_64
, 0, tests
},
4746 {"fnv1_32", pre_hash_fnv1_32
, 0, tests
},
4747 {"fnv1a_32", pre_hash_fnv1a_32
, 0, tests
},
4748 {"ketama", pre_behavior_ketama
, 0, tests
},
4749 {"ketama_auto_eject_hosts", pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
4750 {"unix_socket", pre_unix_socket
, 0, tests
},
4751 {"unix_socket_nodelay", pre_nodelay
, 0, tests
},
4752 {"poll_timeout", poll_timeout
, 0, tests
},
4753 {"gets", enable_cas
, 0, tests
},
4754 {"consistent", enable_consistent
, 0, tests
},
4755 #ifdef MEMCACHED_ENABLE_DEPRECATED
4756 {"deprecated_memory_allocators", deprecated_set_memory_alloc
, 0, tests
},
4758 {"memory_allocators", set_memory_alloc
, 0, tests
},
4759 {"prefix", set_prefix
, 0, tests
},
4760 {"version_1_2_3", check_for_1_2_3
, 0, version_1_2_3
},
4761 {"string", 0, 0, string_tests
},
4762 {"result", 0, 0, result_tests
},
4763 {"async", pre_nonblock
, 0, async_tests
},
4764 {"async_binary", pre_nonblock_binary
, 0, async_tests
},
4765 {"user", 0, 0, user_tests
},
4766 {"generate", 0, 0, generate_tests
},
4767 {"generate_hsieh", pre_hsieh
, 0, generate_tests
},
4768 {"generate_ketama", pre_behavior_ketama
, 0, generate_tests
},
4769 {"generate_hsieh_consistent", enable_consistent
, 0, generate_tests
},
4770 {"generate_md5", pre_md5
, 0, generate_tests
},
4771 {"generate_murmur", pre_murmur
, 0, generate_tests
},
4772 {"generate_jenkins", pre_jenkins
, 0, generate_tests
},
4773 {"generate_nonblock", pre_nonblock
, 0, generate_tests
},
4774 {"consistent_not", 0, 0, consistent_tests
},
4775 {"consistent_ketama", pre_behavior_ketama
, 0, consistent_tests
},
4776 {"consistent_ketama_weighted", pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
4777 {"test_hashes", 0, 0, hash_tests
},
4778 {"replication", pre_replication
, 0, replication_tests
},
4779 {"replication_noblock", pre_replication_noblock
, 0, replication_tests
},
4780 {"regression", 0, 0, regression_tests
},
4784 #define SERVERS_TO_CREATE 5
4786 /* Prototypes for functions we will pass to test framework */
4787 void *world_create(void);
4788 void world_destroy(void *p
);
4790 void *world_create(void)
4792 server_startup_st
*construct
;
4794 construct
= (server_startup_st
*)malloc(sizeof(server_startup_st
));
4795 memset(construct
, 0, sizeof(server_startup_st
));
4796 construct
->count
= SERVERS_TO_CREATE
;
4798 server_startup(construct
);
4804 void world_destroy(void *p
)
4806 server_startup_st
*construct
= (server_startup_st
*)p
;
4807 memcached_server_st
*servers
= (memcached_server_st
*)construct
->servers
;
4808 memcached_server_list_free(servers
);
4810 server_shutdown(construct
);
4814 void get_world(world_st
*world
)
4816 world
->collections
= collection
;
4817 world
->create
= world_create
;
4818 world
->destroy
= world_destroy
;