1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
13 #include "libmemcached/common.h"
20 #include <sys/types.h>
26 #include "clients/generator.h"
27 #include "clients/execute.h"
30 #define INT64_MAX LONG_MAX
33 #define INT32_MAX INT_MAX
39 #ifdef HAVE_LIBMEMCACHEDUTIL
41 #include "libmemcached/memcached_util.h"
44 #include "hash_results.h"
46 #define GLOBAL_COUNT 10000
47 #define GLOBAL2_COUNT 100
48 #define SERVERS_TO_CREATE 5
49 static uint32_t global_count
;
51 static pairs_st
*global_pairs
;
52 static const char *global_keys
[GLOBAL_COUNT
];
53 static size_t global_keys_length
[GLOBAL_COUNT
];
55 static test_return_t
init_test(memcached_st
*not_used
__attribute__((unused
)))
59 (void)memcached_create(&memc
);
60 memcached_free(&memc
);
65 static test_return_t
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
67 memcached_server_st
*server_list
;
68 memcached_return_t rc
;
70 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
71 test_truth(server_list
== NULL
);
73 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
74 test_truth(server_list
== NULL
);
76 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
77 test_truth(server_list
== NULL
);
82 #define TEST_PORT_COUNT 7
83 uint32_t test_ports
[TEST_PORT_COUNT
];
85 static memcached_return_t
server_display_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
88 uint32_t bigger
= *((uint32_t *)(context
));
89 assert(bigger
<= server
->port
);
90 *((uint32_t *)(context
))= server
->port
;
92 return MEMCACHED_SUCCESS
;
95 static test_return_t
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
98 uint32_t bigger
= 0; /* Prime the value for the test_truth in server_display_function */
99 memcached_return_t rc
;
100 memcached_server_fn callbacks
[1];
101 memcached_st
*local_memc
;
103 local_memc
= memcached_create(NULL
);
104 test_truth(local_memc
);
105 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
107 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
109 test_ports
[x
]= (uint32_t)random() % 64000;
110 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
111 test_truth(local_memc
->number_of_hosts
== x
+ 1);
112 test_truth(local_memc
->hosts
[0].count
== x
+1);
113 test_truth(rc
== MEMCACHED_SUCCESS
);
116 callbacks
[0]= server_display_function
;
117 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
120 memcached_free(local_memc
);
125 static test_return_t
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
127 uint32_t bigger
= 0; /* Prime the value for the test_truth in server_display_function */
128 memcached_return_t rc
;
129 memcached_server_fn callbacks
[1];
130 memcached_st
*local_memc
;
132 local_memc
= memcached_create(NULL
);
133 test_truth(local_memc
);
134 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
135 test_truth(rc
== MEMCACHED_SUCCESS
);
137 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
138 test_truth(rc
== MEMCACHED_SUCCESS
);
139 test_truth(local_memc
->hosts
[0].port
== 43043);
141 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
142 test_truth(rc
== MEMCACHED_SUCCESS
);
143 test_truth(local_memc
->hosts
[0].port
== 43042);
144 test_truth(local_memc
->hosts
[1].port
== 43043);
146 callbacks
[0]= server_display_function
;
147 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
150 memcached_free(local_memc
);
155 static memcached_return_t
server_display_unsort_function(memcached_st
*ptr
__attribute__((unused
)), memcached_server_st
*server
, void *context
)
158 uint32_t x
= *((uint32_t *)(context
));
160 assert(test_ports
[x
] == server
->port
);
161 *((uint32_t *)(context
))= ++x
;
163 return MEMCACHED_SUCCESS
;
166 static test_return_t
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
169 uint32_t counter
= 0; /* Prime the value for the test_truth in server_display_function */
170 uint32_t bigger
= 0; /* Prime the value for the test_truth in server_display_function */
171 memcached_return_t rc
;
172 memcached_server_fn callbacks
[1];
173 memcached_st
*local_memc
;
175 local_memc
= memcached_create(NULL
);
176 test_truth(local_memc
);
178 for (x
= 0; x
< TEST_PORT_COUNT
; x
++)
180 test_ports
[x
]= (uint32_t)(random() % 64000);
181 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
182 test_truth(local_memc
->number_of_hosts
== x
+1);
183 test_truth(local_memc
->hosts
[0].count
== x
+1);
184 test_truth(rc
== MEMCACHED_SUCCESS
);
187 callbacks
[0]= server_display_unsort_function
;
188 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
190 /* Now we sort old data! */
191 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
192 callbacks
[0]= server_display_function
;
193 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
196 memcached_free(local_memc
);
201 static test_return_t
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
204 memc
= memcached_create(NULL
);
206 memcached_free(memc
);
211 static test_return_t
clone_test(memcached_st
*memc
)
215 memcached_st
*memc_clone
;
216 memc_clone
= memcached_clone(NULL
, NULL
);
217 test_truth(memc_clone
);
218 memcached_free(memc_clone
);
221 /* Can we init from null? */
223 memcached_st
*memc_clone
;
224 memc_clone
= memcached_clone(NULL
, memc
);
225 test_truth(memc_clone
);
227 test_truth(memc_clone
->call_free
== memc
->call_free
);
228 test_truth(memc_clone
->call_malloc
== memc
->call_malloc
);
229 test_truth(memc_clone
->call_realloc
== memc
->call_realloc
);
230 test_truth(memc_clone
->call_calloc
== memc
->call_calloc
);
231 test_truth(memc_clone
->connect_timeout
== memc
->connect_timeout
);
232 test_truth(memc_clone
->delete_trigger
== memc
->delete_trigger
);
233 test_truth(memc_clone
->distribution
== memc
->distribution
);
234 { // Test all of the flags
235 test_truth(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
236 test_truth(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
237 test_truth(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
238 test_truth(memc_clone
->flags
.use_md5
== memc
->flags
.use_md5
);
239 test_truth(memc_clone
->flags
.use_crc
== memc
->flags
.use_crc
);
240 test_truth(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
241 test_truth(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
242 test_truth(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
243 test_truth(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
244 test_truth(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
245 test_truth(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
246 test_truth(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
247 test_truth(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
248 test_truth(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
249 test_truth(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
250 test_truth(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
251 test_truth(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
253 test_truth(memc_clone
->get_key_failure
== memc
->get_key_failure
);
254 test_truth(memc_clone
->hash
== memc
->hash
);
255 test_truth(memc_clone
->distribution_hash
== memc
->distribution_hash
);
256 test_truth(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
257 test_truth(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
258 test_truth(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
259 test_truth(memc_clone
->on_cleanup
== memc
->on_cleanup
);
260 test_truth(memc_clone
->on_clone
== memc
->on_clone
);
261 test_truth(memc_clone
->poll_timeout
== memc
->poll_timeout
);
262 test_truth(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
263 test_truth(memc_clone
->recv_size
== memc
->recv_size
);
264 test_truth(memc_clone
->retry_timeout
== memc
->retry_timeout
);
265 test_truth(memc_clone
->send_size
== memc
->send_size
);
266 test_truth(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
267 test_truth(memc_clone
->snd_timeout
== memc
->snd_timeout
);
268 test_truth(memc_clone
->user_data
== memc
->user_data
);
270 memcached_free(memc_clone
);
273 /* Can we init from struct? */
275 memcached_st declared_clone
;
276 memcached_st
*memc_clone
;
277 memset(&declared_clone
, 0 , sizeof(memcached_st
));
278 memc_clone
= memcached_clone(&declared_clone
, NULL
);
279 test_truth(memc_clone
);
280 memcached_free(memc_clone
);
283 /* Can we init from struct? */
285 memcached_st declared_clone
;
286 memcached_st
*memc_clone
;
287 memset(&declared_clone
, 0 , sizeof(memcached_st
));
288 memc_clone
= memcached_clone(&declared_clone
, memc
);
289 test_truth(memc_clone
);
290 memcached_free(memc_clone
);
296 static test_return_t
userdata_test(memcached_st
*memc
)
299 test_truth(memcached_set_user_data(memc
, foo
) == NULL
);
300 test_truth(memcached_get_user_data(memc
) == foo
);
301 test_truth(memcached_set_user_data(memc
, NULL
) == foo
);
306 static test_return_t
connection_test(memcached_st
*memc
)
308 memcached_return_t rc
;
310 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
311 test_truth(rc
== MEMCACHED_SUCCESS
);
316 static test_return_t
error_test(memcached_st
*memc
)
318 memcached_return_t rc
;
319 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
320 982370485U, 1263635348U, 4242906218U, 3829656100U,
321 1891735253U, 334139633U, 2257084983U, 3088286104U,
322 13199785U, 2542027183U, 1097051614U, 199566778U,
323 2748246961U, 2465192557U, 1664094137U, 2405439045U,
324 1842224848U, 692413798U, 3479807801U, 919913813U,
325 4269430871U, 610793021U, 527273862U, 1437122909U,
326 2300930706U, 2943759320U, 674306647U, 2400528935U,
327 54481931U, 4186304426U, 1741088401U, 2979625118U,
328 4159057246U, 3425930182U, 2593724503U};
330 // You have updated the memcache_error messages but not updated docs/tests.
331 test_truth(MEMCACHED_MAXIMUM_RETURN
== 39);
332 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
335 const char *msg
= memcached_strerror(memc
, rc
);
336 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
337 MEMCACHED_HASH_JENKINS
);
338 test_truth(values
[rc
] == hash_val
);
344 static test_return_t
set_test(memcached_st
*memc
)
346 memcached_return_t rc
;
347 const char *key
= "foo";
348 const char *value
= "when we sanitize";
350 rc
= memcached_set(memc
, key
, strlen(key
),
351 value
, strlen(value
),
352 (time_t)0, (uint32_t)0);
353 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
358 static test_return_t
append_test(memcached_st
*memc
)
360 memcached_return_t rc
;
361 const char *key
= "fig";
362 const char *in_value
= "we";
363 char *out_value
= NULL
;
367 rc
= memcached_flush(memc
, 0);
368 test_truth(rc
== MEMCACHED_SUCCESS
);
370 rc
= memcached_set(memc
, key
, strlen(key
),
371 in_value
, strlen(in_value
),
372 (time_t)0, (uint32_t)0);
373 test_truth(rc
== MEMCACHED_SUCCESS
);
375 rc
= memcached_append(memc
, key
, strlen(key
),
376 " the", strlen(" the"),
377 (time_t)0, (uint32_t)0);
378 test_truth(rc
== MEMCACHED_SUCCESS
);
380 rc
= memcached_append(memc
, key
, strlen(key
),
381 " people", strlen(" people"),
382 (time_t)0, (uint32_t)0);
383 test_truth(rc
== MEMCACHED_SUCCESS
);
385 out_value
= memcached_get(memc
, key
, strlen(key
),
386 &value_length
, &flags
, &rc
);
387 test_truth(!memcmp(out_value
, "we the people", strlen("we the people")));
388 test_truth(strlen("we the people") == value_length
);
389 test_truth(rc
== MEMCACHED_SUCCESS
);
395 static test_return_t
append_binary_test(memcached_st
*memc
)
397 memcached_return_t rc
;
398 const char *key
= "numbers";
399 unsigned int *store_ptr
;
400 unsigned int store_list
[] = { 23, 56, 499, 98, 32847, 0 };
406 rc
= memcached_flush(memc
, 0);
407 test_truth(rc
== MEMCACHED_SUCCESS
);
409 rc
= memcached_set(memc
,
412 (time_t)0, (uint32_t)0);
413 test_truth(rc
== MEMCACHED_SUCCESS
);
415 for (x
= 0; store_list
[x
] ; x
++)
417 rc
= memcached_append(memc
,
419 (char *)&store_list
[x
], sizeof(unsigned int),
420 (time_t)0, (uint32_t)0);
421 test_truth(rc
== MEMCACHED_SUCCESS
);
424 value
= memcached_get(memc
, key
, strlen(key
),
425 &value_length
, &flags
, &rc
);
426 test_truth((value_length
== (sizeof(unsigned int) * x
)));
427 test_truth(rc
== MEMCACHED_SUCCESS
);
429 store_ptr
= (unsigned int *)value
;
431 while ((size_t)store_ptr
< (size_t)(value
+ value_length
))
433 test_truth(*store_ptr
== store_list
[x
++]);
441 static test_return_t
cas2_test(memcached_st
*memc
)
443 memcached_return_t rc
;
444 const char *keys
[]= {"fudge", "son", "food"};
445 size_t key_length
[]= {5, 3, 4};
446 const char *value
= "we the people";
447 size_t value_length
= strlen("we the people");
449 memcached_result_st results_obj
;
450 memcached_result_st
*results
;
453 rc
= memcached_flush(memc
, 0);
454 test_truth(rc
== MEMCACHED_SUCCESS
);
456 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
458 for (x
= 0; x
< 3; x
++)
460 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
461 keys
[x
], key_length
[x
],
462 (time_t)50, (uint32_t)9);
463 test_truth(rc
== MEMCACHED_SUCCESS
);
466 rc
= memcached_mget(memc
, keys
, key_length
, 3);
468 results
= memcached_result_create(memc
, &results_obj
);
470 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
472 test_truth(results
->cas
);
473 test_truth(rc
== MEMCACHED_SUCCESS
);
474 test_truth(memcached_result_cas(results
));
476 test_truth(!memcmp(value
, "we the people", strlen("we the people")));
477 test_truth(strlen("we the people") == value_length
);
478 test_truth(rc
== MEMCACHED_SUCCESS
);
480 memcached_result_free(&results_obj
);
485 static test_return_t
cas_test(memcached_st
*memc
)
487 memcached_return_t rc
;
488 const char *key
= "fun";
489 size_t key_length
= strlen(key
);
490 const char *value
= "we the people";
491 const char* keys
[2] = { key
, NULL
};
492 size_t keylengths
[2] = { strlen(key
), 0 };
493 size_t value_length
= strlen(value
);
494 const char *value2
= "change the value";
495 size_t value2_length
= strlen(value2
);
497 memcached_result_st results_obj
;
498 memcached_result_st
*results
;
501 rc
= memcached_flush(memc
, 0);
502 test_truth(rc
== MEMCACHED_SUCCESS
);
504 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
506 rc
= memcached_set(memc
, key
, strlen(key
),
507 value
, strlen(value
),
508 (time_t)0, (uint32_t)0);
509 test_truth(rc
== MEMCACHED_SUCCESS
);
511 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
513 results
= memcached_result_create(memc
, &results_obj
);
515 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
517 test_truth(rc
== MEMCACHED_SUCCESS
);
518 test_truth(memcached_result_cas(results
));
519 test_truth(!memcmp(value
, memcached_result_value(results
), value_length
));
520 test_truth(strlen(memcached_result_value(results
)) == value_length
);
521 test_truth(rc
== MEMCACHED_SUCCESS
);
522 uint64_t cas
= memcached_result_cas(results
);
525 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
526 test_truth(rc
== MEMCACHED_END
);
527 test_truth(results
== NULL
);
530 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
531 test_truth(rc
== MEMCACHED_SUCCESS
);
534 * The item will have a new cas value, so try to set it again with the old
535 * value. This should fail!
537 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
538 test_truth(rc
== MEMCACHED_DATA_EXISTS
);
540 memcached_result_free(&results_obj
);
545 static test_return_t
prepend_test(memcached_st
*memc
)
547 memcached_return_t rc
;
548 const char *key
= "fig";
549 const char *value
= "people";
550 char *out_value
= NULL
;
554 rc
= memcached_flush(memc
, 0);
555 test_truth(rc
== MEMCACHED_SUCCESS
);
557 rc
= memcached_set(memc
, key
, strlen(key
),
558 value
, strlen(value
),
559 (time_t)0, (uint32_t)0);
560 test_truth(rc
== MEMCACHED_SUCCESS
);
562 rc
= memcached_prepend(memc
, key
, strlen(key
),
563 "the ", strlen("the "),
564 (time_t)0, (uint32_t)0);
565 test_truth(rc
== MEMCACHED_SUCCESS
);
567 rc
= memcached_prepend(memc
, key
, strlen(key
),
568 "we ", strlen("we "),
569 (time_t)0, (uint32_t)0);
570 test_truth(rc
== MEMCACHED_SUCCESS
);
572 out_value
= memcached_get(memc
, key
, strlen(key
),
573 &value_length
, &flags
, &rc
);
574 test_truth(!memcmp(out_value
, "we the people", strlen("we the people")));
575 test_truth(strlen("we the people") == value_length
);
576 test_truth(rc
== MEMCACHED_SUCCESS
);
583 Set the value, then quit to make sure it is flushed.
584 Come back in and test that add fails.
586 static test_return_t
add_test(memcached_st
*memc
)
588 memcached_return_t rc
;
589 const char *key
= "foo";
590 const char *value
= "when we sanitize";
591 unsigned long long setting_value
;
593 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
595 rc
= memcached_set(memc
, key
, strlen(key
),
596 value
, strlen(value
),
597 (time_t)0, (uint32_t)0);
598 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
599 memcached_quit(memc
);
600 rc
= memcached_add(memc
, key
, strlen(key
),
601 value
, strlen(value
),
602 (time_t)0, (uint32_t)0);
604 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
607 test_truth(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
611 test_truth(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
618 ** There was a problem of leaking filedescriptors in the initial release
619 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
620 ** systems it seems that the kernel is slow on reclaiming the resources
621 ** because the connects starts to time out (the test doesn't do much
622 ** anyway, so just loop 10 iterations)
624 static test_return_t
add_wrapper(memcached_st
*memc
)
627 unsigned int max
= 10000;
635 for (x
= 0; x
< max
; x
++)
641 static test_return_t
replace_test(memcached_st
*memc
)
643 memcached_return_t rc
;
644 const char *key
= "foo";
645 const char *value
= "when we sanitize";
646 const char *original
= "first we insert some data";
648 rc
= memcached_set(memc
, key
, strlen(key
),
649 original
, strlen(original
),
650 (time_t)0, (uint32_t)0);
651 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
653 rc
= memcached_replace(memc
, key
, strlen(key
),
654 value
, strlen(value
),
655 (time_t)0, (uint32_t)0);
656 test_truth(rc
== MEMCACHED_SUCCESS
);
661 static test_return_t
delete_test(memcached_st
*memc
)
663 memcached_return_t rc
;
664 const char *key
= "foo";
665 const char *value
= "when we sanitize";
667 rc
= memcached_set(memc
, key
, strlen(key
),
668 value
, strlen(value
),
669 (time_t)0, (uint32_t)0);
670 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
672 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
673 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
678 static test_return_t
flush_test(memcached_st
*memc
)
680 memcached_return_t rc
;
682 rc
= memcached_flush(memc
, 0);
683 test_truth(rc
== MEMCACHED_SUCCESS
);
688 static memcached_return_t
server_function(memcached_st
*ptr
__attribute__((unused
)),
689 memcached_server_st
*server
__attribute__((unused
)),
690 void *context
__attribute__((unused
)))
694 return MEMCACHED_SUCCESS
;
697 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
700 strcpy(context
, "foo bad");
701 memcached_server_fn callbacks
[1];
703 callbacks
[0]= server_function
;
704 memcached_server_cursor(memc
, callbacks
, context
, 1);
708 static test_return_t
bad_key_test(memcached_st
*memc
)
710 memcached_return_t rc
;
711 const char *key
= "foo bad";
713 size_t string_length
;
715 memcached_st
*memc_clone
;
717 size_t max_keylen
= 0xffff;
719 // Just skip if we are in binary mode.
720 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
723 memc_clone
= memcached_clone(NULL
, memc
);
724 test_truth(memc_clone
);
726 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
727 test_truth(rc
== MEMCACHED_SUCCESS
);
729 /* All keys are valid in the binary protocol (except for length) */
730 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
732 string
= memcached_get(memc_clone
, key
, strlen(key
),
733 &string_length
, &flags
, &rc
);
734 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
735 test_truth(string_length
== 0);
739 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
740 test_truth(rc
== MEMCACHED_SUCCESS
);
741 string
= memcached_get(memc_clone
, key
, strlen(key
),
742 &string_length
, &flags
, &rc
);
743 test_truth(rc
== MEMCACHED_NOTFOUND
);
744 test_truth(string_length
== 0);
747 /* Test multi key for bad keys */
748 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
749 size_t key_lengths
[] = { 7, 7, 7 };
751 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
752 test_truth(rc
== MEMCACHED_SUCCESS
);
754 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
755 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
757 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
758 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
762 /* The following test should be moved to the end of this function when the
763 memcached server is updated to allow max size length of the keys in the
766 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
767 test_truth(rc
== MEMCACHED_SUCCESS
);
769 char *longkey
= malloc(max_keylen
+ 1);
772 memset(longkey
, 'a', max_keylen
+ 1);
773 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
774 &string_length
, &flags
, &rc
);
775 test_truth(rc
== MEMCACHED_NOTFOUND
);
776 test_truth(string_length
== 0);
779 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
780 &string_length
, &flags
, &rc
);
781 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
782 test_truth(string_length
== 0);
789 /* Make sure zero length keys are marked as bad */
791 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
792 test_truth(rc
== MEMCACHED_SUCCESS
);
793 string
= memcached_get(memc_clone
, key
, 0,
794 &string_length
, &flags
, &rc
);
795 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
796 test_truth(string_length
== 0);
799 memcached_free(memc_clone
);
804 #define READ_THROUGH_VALUE "set for me"
805 static memcached_return_t
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
806 char *key
__attribute__((unused
)),
807 size_t key_length
__attribute__((unused
)),
808 memcached_result_st
*result
)
811 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
814 static test_return_t
read_through(memcached_st
*memc
)
816 memcached_return_t rc
;
817 const char *key
= "foo";
819 size_t string_length
;
821 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
823 string
= memcached_get(memc
, key
, strlen(key
),
824 &string_length
, &flags
, &rc
);
826 test_truth(rc
== MEMCACHED_NOTFOUND
);
827 test_truth(string_length
== 0);
830 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
832 test_truth(rc
== MEMCACHED_SUCCESS
);
834 string
= memcached_get(memc
, key
, strlen(key
),
835 &string_length
, &flags
, &rc
);
837 test_truth(rc
== MEMCACHED_SUCCESS
);
838 test_truth(string_length
== strlen(READ_THROUGH_VALUE
));
839 test_truth(!strcmp(READ_THROUGH_VALUE
, string
));
842 string
= memcached_get(memc
, key
, strlen(key
),
843 &string_length
, &flags
, &rc
);
845 test_truth(rc
== MEMCACHED_SUCCESS
);
846 test_truth(string_length
== strlen(READ_THROUGH_VALUE
));
847 test_truth(!strcmp(READ_THROUGH_VALUE
, string
));
853 static memcached_return_t
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
855 size_t key_length
__attribute__((unused
)))
859 return MEMCACHED_SUCCESS
;
862 static test_return_t
delete_through(memcached_st
*memc
)
864 memcached_trigger_delete_key_fn callback
;
865 memcached_return_t rc
;
867 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
869 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
870 test_truth(rc
== MEMCACHED_SUCCESS
);
875 static test_return_t
get_test(memcached_st
*memc
)
877 memcached_return_t rc
;
878 const char *key
= "foo";
880 size_t string_length
;
883 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
884 test_truth(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
886 string
= memcached_get(memc
, key
, strlen(key
),
887 &string_length
, &flags
, &rc
);
889 test_truth(rc
== MEMCACHED_NOTFOUND
);
890 test_truth(string_length
== 0);
896 static test_return_t
get_test2(memcached_st
*memc
)
898 memcached_return_t rc
;
899 const char *key
= "foo";
900 const char *value
= "when we sanitize";
902 size_t string_length
;
905 rc
= memcached_set(memc
, key
, strlen(key
),
906 value
, strlen(value
),
907 (time_t)0, (uint32_t)0);
908 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
910 string
= memcached_get(memc
, key
, strlen(key
),
911 &string_length
, &flags
, &rc
);
914 test_truth(rc
== MEMCACHED_SUCCESS
);
915 test_truth(string_length
== strlen(value
));
916 test_truth(!memcmp(string
, value
, string_length
));
923 static test_return_t
set_test2(memcached_st
*memc
)
925 memcached_return_t rc
;
926 const char *key
= "foo";
927 const char *value
= "train in the brain";
928 size_t value_length
= strlen(value
);
931 for (x
= 0; x
< 10; x
++)
933 rc
= memcached_set(memc
, key
, strlen(key
),
935 (time_t)0, (uint32_t)0);
936 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
942 static test_return_t
set_test3(memcached_st
*memc
)
944 memcached_return_t rc
;
946 size_t value_length
= 8191;
949 value
= (char*)malloc(value_length
);
952 for (x
= 0; x
< value_length
; x
++)
953 value
[x
] = (char) (x
% 127);
955 /* The dump test relies on there being at least 32 items in memcached */
956 for (x
= 0; x
< 32; x
++)
960 sprintf(key
, "foo%u", x
);
962 rc
= memcached_set(memc
, key
, strlen(key
),
964 (time_t)0, (uint32_t)0);
965 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
973 static test_return_t
get_test3(memcached_st
*memc
)
975 memcached_return_t rc
;
976 const char *key
= "foo";
978 size_t value_length
= 8191;
980 size_t string_length
;
984 value
= (char*)malloc(value_length
);
987 for (x
= 0; x
< value_length
; x
++)
988 value
[x
] = (char) (x
% 127);
990 rc
= memcached_set(memc
, key
, strlen(key
),
992 (time_t)0, (uint32_t)0);
993 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
995 string
= memcached_get(memc
, key
, strlen(key
),
996 &string_length
, &flags
, &rc
);
998 test_truth(rc
== MEMCACHED_SUCCESS
);
1000 test_truth(string_length
== value_length
);
1001 test_truth(!memcmp(string
, value
, string_length
));
1006 return TEST_SUCCESS
;
1009 static test_return_t
get_test4(memcached_st
*memc
)
1011 memcached_return_t rc
;
1012 const char *key
= "foo";
1014 size_t value_length
= 8191;
1016 size_t string_length
;
1020 value
= (char*)malloc(value_length
);
1023 for (x
= 0; x
< value_length
; x
++)
1024 value
[x
] = (char) (x
% 127);
1026 rc
= memcached_set(memc
, key
, strlen(key
),
1027 value
, value_length
,
1028 (time_t)0, (uint32_t)0);
1029 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1031 for (x
= 0; x
< 10; x
++)
1033 string
= memcached_get(memc
, key
, strlen(key
),
1034 &string_length
, &flags
, &rc
);
1036 test_truth(rc
== MEMCACHED_SUCCESS
);
1038 test_truth(string_length
== value_length
);
1039 test_truth(!memcmp(string
, value
, string_length
));
1045 return TEST_SUCCESS
;
1049 * This test verifies that memcached_read_one_response doesn't try to
1050 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1051 * responses before you execute a storage command.
1053 static test_return_t
get_test5(memcached_st
*memc
)
1056 ** Request the same key twice, to ensure that we hash to the same server
1057 ** (so that we have multiple response values queued up) ;-)
1059 const char *keys
[]= { "key", "key" };
1060 size_t lengths
[]= { 3, 3 };
1064 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1065 keys
[0], lengths
[0], 0, 0);
1066 test_truth(rc
== MEMCACHED_SUCCESS
);
1067 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1069 memcached_result_st results_obj
;
1070 memcached_result_st
*results
;
1071 results
=memcached_result_create(memc
, &results_obj
);
1072 test_truth(results
);
1073 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1074 test_truth(results
);
1075 memcached_result_free(&results_obj
);
1077 /* Don't read out the second result, but issue a set instead.. */
1078 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1079 test_truth(rc
== MEMCACHED_SUCCESS
);
1081 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1082 &rlen
, &flags
, &rc
);
1083 test_truth(val
== NULL
);
1084 test_truth(rc
== MEMCACHED_NOTFOUND
);
1085 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1086 test_truth(val
!= NULL
);
1087 test_truth(rc
== MEMCACHED_SUCCESS
);
1090 return TEST_SUCCESS
;
1093 static test_return_t
mget_end(memcached_st
*memc
)
1095 const char *keys
[]= { "foo", "foo2" };
1096 size_t lengths
[]= { 3, 4 };
1097 const char *values
[]= { "fjord", "41" };
1099 memcached_return_t rc
;
1102 for (int i
= 0; i
< 2; i
++)
1104 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1105 (time_t)0, (uint32_t)0);
1106 test_truth(rc
== MEMCACHED_SUCCESS
);
1110 size_t string_length
;
1113 // retrieve both via mget
1114 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1115 test_truth(rc
== MEMCACHED_SUCCESS
);
1117 char key
[MEMCACHED_MAX_KEY
];
1120 // this should get both
1121 for (int i
= 0; i
< 2; i
++)
1123 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1125 test_truth(rc
== MEMCACHED_SUCCESS
);
1127 if (key_length
== 4)
1129 test_truth(string_length
== strlen(values
[val
]));
1130 test_truth(strncmp(values
[val
], string
, string_length
) == 0);
1134 // this should indicate end
1135 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1136 test_truth(rc
== MEMCACHED_END
);
1139 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1140 test_truth(rc
== MEMCACHED_SUCCESS
);
1142 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1143 test_truth(key_length
== lengths
[0]);
1144 test_truth(strncmp(keys
[0], key
, key_length
) == 0);
1145 test_truth(string_length
== strlen(values
[0]));
1146 test_truth(strncmp(values
[0], string
, string_length
) == 0);
1147 test_truth(rc
== MEMCACHED_SUCCESS
);
1150 // this should indicate end
1151 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1152 test_truth(rc
== MEMCACHED_END
);
1154 return TEST_SUCCESS
;
1157 /* Do not copy the style of this code, I just access hosts to testthis function */
1158 static test_return_t
stats_servername_test(memcached_st
*memc
)
1160 memcached_return_t rc
;
1161 memcached_stat_st memc_stat
;
1162 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1163 memc
->hosts
[0].hostname
,
1164 memc
->hosts
[0].port
);
1166 return TEST_SUCCESS
;
1169 static test_return_t
increment_test(memcached_st
*memc
)
1171 uint64_t new_number
;
1172 memcached_return_t rc
;
1173 const char *key
= "number";
1174 const char *value
= "0";
1176 rc
= memcached_set(memc
, key
, strlen(key
),
1177 value
, strlen(value
),
1178 (time_t)0, (uint32_t)0);
1179 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1181 rc
= memcached_increment(memc
, key
, strlen(key
),
1183 test_truth(rc
== MEMCACHED_SUCCESS
);
1184 test_truth(new_number
== 1);
1186 rc
= memcached_increment(memc
, key
, strlen(key
),
1188 test_truth(rc
== MEMCACHED_SUCCESS
);
1189 test_truth(new_number
== 2);
1191 return TEST_SUCCESS
;
1194 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1196 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1198 uint64_t new_number
;
1199 memcached_return_t rc
;
1200 const char *key
= "number";
1201 uint64_t initial
= 0;
1203 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1204 1, initial
, 0, &new_number
);
1205 test_truth(rc
== MEMCACHED_SUCCESS
);
1206 test_truth(new_number
== initial
);
1208 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1209 1, initial
, 0, &new_number
);
1210 test_truth(rc
== MEMCACHED_SUCCESS
);
1211 test_truth(new_number
== (initial
+ 1));
1213 return TEST_SUCCESS
;
1216 static test_return_t
decrement_test(memcached_st
*memc
)
1218 uint64_t new_number
;
1219 memcached_return_t rc
;
1220 const char *key
= "number";
1221 const char *value
= "3";
1223 rc
= memcached_set(memc
, key
, strlen(key
),
1224 value
, strlen(value
),
1225 (time_t)0, (uint32_t)0);
1226 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1228 rc
= memcached_decrement(memc
, key
, strlen(key
),
1230 test_truth(rc
== MEMCACHED_SUCCESS
);
1231 test_truth(new_number
== 2);
1233 rc
= memcached_decrement(memc
, key
, strlen(key
),
1235 test_truth(rc
== MEMCACHED_SUCCESS
);
1236 test_truth(new_number
== 1);
1238 return TEST_SUCCESS
;
1241 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1243 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1245 uint64_t new_number
;
1246 memcached_return_t rc
;
1247 const char *key
= "number";
1248 uint64_t initial
= 3;
1250 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1251 1, initial
, 0, &new_number
);
1252 test_truth(rc
== MEMCACHED_SUCCESS
);
1253 test_truth(new_number
== initial
);
1255 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1256 1, initial
, 0, &new_number
);
1257 test_truth(rc
== MEMCACHED_SUCCESS
);
1258 test_truth(new_number
== (initial
- 1));
1260 return TEST_SUCCESS
;
1263 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1265 uint64_t new_number
;
1266 memcached_return_t rc
;
1267 const char *master_key
= "foo";
1268 const char *key
= "number";
1269 const char *value
= "0";
1271 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1273 value
, strlen(value
),
1274 (time_t)0, (uint32_t)0);
1275 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1277 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1279 test_truth(rc
== MEMCACHED_SUCCESS
);
1280 test_truth(new_number
== 1);
1282 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1284 test_truth(rc
== MEMCACHED_SUCCESS
);
1285 test_truth(new_number
== 2);
1287 return TEST_SUCCESS
;
1290 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1292 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1294 uint64_t new_number
;
1295 memcached_return_t rc
;
1296 const char *master_key
= "foo";
1297 const char *key
= "number";
1298 uint64_t initial
= 0;
1300 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1302 1, initial
, 0, &new_number
);
1303 test_truth(rc
== MEMCACHED_SUCCESS
);
1304 test_truth(new_number
== initial
);
1306 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1308 1, initial
, 0, &new_number
);
1309 test_truth(rc
== MEMCACHED_SUCCESS
);
1310 test_truth(new_number
== (initial
+ 1));
1312 return TEST_SUCCESS
;
1315 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1317 uint64_t new_number
;
1318 memcached_return_t rc
;
1319 const char *master_key
= "foo";
1320 const char *key
= "number";
1321 const char *value
= "3";
1323 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1325 value
, strlen(value
),
1326 (time_t)0, (uint32_t)0);
1327 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1329 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1332 test_truth(rc
== MEMCACHED_SUCCESS
);
1333 test_truth(new_number
== 2);
1335 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1338 test_truth(rc
== MEMCACHED_SUCCESS
);
1339 test_truth(new_number
== 1);
1341 return TEST_SUCCESS
;
1344 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1346 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1348 uint64_t new_number
;
1349 memcached_return_t rc
;
1350 const char *master_key
= "foo";
1351 const char *key
= "number";
1352 uint64_t initial
= 3;
1354 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1356 1, initial
, 0, &new_number
);
1357 test_truth(rc
== MEMCACHED_SUCCESS
);
1358 test_truth(new_number
== initial
);
1360 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1362 1, initial
, 0, &new_number
);
1363 test_truth(rc
== MEMCACHED_SUCCESS
);
1364 test_truth(new_number
== (initial
- 1));
1366 return TEST_SUCCESS
;
1369 static test_return_t
quit_test(memcached_st
*memc
)
1371 memcached_return_t rc
;
1372 const char *key
= "fudge";
1373 const char *value
= "sanford and sun";
1375 rc
= memcached_set(memc
, key
, strlen(key
),
1376 value
, strlen(value
),
1377 (time_t)10, (uint32_t)3);
1378 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1379 memcached_quit(memc
);
1381 rc
= memcached_set(memc
, key
, strlen(key
),
1382 value
, strlen(value
),
1383 (time_t)50, (uint32_t)9);
1384 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1386 return TEST_SUCCESS
;
1389 static test_return_t
mget_result_test(memcached_st
*memc
)
1391 memcached_return_t rc
;
1392 const char *keys
[]= {"fudge", "son", "food"};
1393 size_t key_length
[]= {5, 3, 4};
1396 memcached_result_st results_obj
;
1397 memcached_result_st
*results
;
1399 results
= memcached_result_create(memc
, &results_obj
);
1400 test_truth(results
);
1401 test_truth(&results_obj
== results
);
1403 /* We need to empty the server before continueing test */
1404 rc
= memcached_flush(memc
, 0);
1405 test_truth(rc
== MEMCACHED_SUCCESS
);
1407 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1408 test_truth(rc
== MEMCACHED_SUCCESS
);
1410 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1412 test_truth(results
);
1415 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1416 test_truth(!results
);
1417 test_truth(rc
== MEMCACHED_END
);
1419 for (x
= 0; x
< 3; x
++)
1421 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1422 keys
[x
], key_length
[x
],
1423 (time_t)50, (uint32_t)9);
1424 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1427 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1428 test_truth(rc
== MEMCACHED_SUCCESS
);
1430 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1432 test_truth(results
);
1433 test_truth(&results_obj
== results
);
1434 test_truth(rc
== MEMCACHED_SUCCESS
);
1435 test_truth(memcached_result_key_length(results
) == memcached_result_length(results
));
1436 test_truth(!memcmp(memcached_result_key_value(results
),
1437 memcached_result_value(results
),
1438 memcached_result_length(results
)));
1441 memcached_result_free(&results_obj
);
1443 return TEST_SUCCESS
;
1446 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1448 memcached_return_t rc
;
1449 const char *keys
[]= {"fudge", "son", "food"};
1450 size_t key_length
[]= {5, 3, 4};
1453 memcached_result_st
*results
;
1455 /* We need to empty the server before continueing test */
1456 rc
= memcached_flush(memc
, 0);
1457 test_truth(rc
== MEMCACHED_SUCCESS
);
1459 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1460 test_truth(rc
== MEMCACHED_SUCCESS
);
1462 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1464 test_truth(results
);
1466 test_truth(!results
);
1467 test_truth(rc
== MEMCACHED_END
);
1469 for (x
= 0; x
< 3; x
++)
1471 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1472 keys
[x
], key_length
[x
],
1473 (time_t)50, (uint32_t)9);
1474 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1477 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1478 test_truth(rc
== MEMCACHED_SUCCESS
);
1481 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1483 test_truth(results
);
1484 test_truth(rc
== MEMCACHED_SUCCESS
);
1485 test_truth(memcached_result_key_length(results
) == memcached_result_length(results
));
1486 test_truth(!memcmp(memcached_result_key_value(results
),
1487 memcached_result_value(results
),
1488 memcached_result_length(results
)));
1489 memcached_result_free(results
);
1493 return TEST_SUCCESS
;
1496 /* Count the results */
1497 static memcached_return_t
callback_counter(memcached_st
*ptr
__attribute__((unused
)),
1498 memcached_result_st
*result
__attribute__((unused
)),
1501 unsigned int *counter
= (unsigned int *)context
;
1503 *counter
= *counter
+ 1;
1505 return MEMCACHED_SUCCESS
;
1508 static test_return_t
mget_result_function(memcached_st
*memc
)
1510 memcached_return_t rc
;
1511 const char *keys
[]= {"fudge", "son", "food"};
1512 size_t key_length
[]= {5, 3, 4};
1514 unsigned int counter
;
1515 memcached_execute_fn callbacks
[1];
1517 /* We need to empty the server before continueing test */
1518 rc
= memcached_flush(memc
, 0);
1519 for (x
= 0; x
< 3; x
++)
1521 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1522 keys
[x
], key_length
[x
],
1523 (time_t)50, (uint32_t)9);
1524 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1527 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1528 test_truth(rc
== MEMCACHED_SUCCESS
);
1530 callbacks
[0]= &callback_counter
;
1532 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1534 test_truth(counter
== 3);
1536 return TEST_SUCCESS
;
1539 static test_return_t
mget_test(memcached_st
*memc
)
1541 memcached_return_t rc
;
1542 const char *keys
[]= {"fudge", "son", "food"};
1543 size_t key_length
[]= {5, 3, 4};
1547 char return_key
[MEMCACHED_MAX_KEY
];
1548 size_t return_key_length
;
1550 size_t return_value_length
;
1552 /* We need to empty the server before continueing test */
1553 rc
= memcached_flush(memc
, 0);
1554 test_truth(rc
== MEMCACHED_SUCCESS
);
1556 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1557 test_truth(rc
== MEMCACHED_SUCCESS
);
1559 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1560 &return_value_length
, &flags
, &rc
)) != NULL
)
1562 test_truth(return_value
);
1564 test_truth(!return_value
);
1565 test_truth(return_value_length
== 0);
1566 test_truth(rc
== MEMCACHED_END
);
1568 for (x
= 0; x
< 3; x
++)
1570 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1571 keys
[x
], key_length
[x
],
1572 (time_t)50, (uint32_t)9);
1573 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1576 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1577 test_truth(rc
== MEMCACHED_SUCCESS
);
1580 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1581 &return_value_length
, &flags
, &rc
)))
1583 test_truth(return_value
);
1584 test_truth(rc
== MEMCACHED_SUCCESS
);
1585 test_truth(return_key_length
== return_value_length
);
1586 test_truth(!memcmp(return_value
, return_key
, return_value_length
));
1591 return TEST_SUCCESS
;
1594 static test_return_t
mget_execute(memcached_st
*memc
)
1597 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1601 * I only want to hit _one_ server so I know the number of requests I'm
1602 * sending in the pipeline.
1604 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1605 memc
->number_of_hosts
= 1;
1607 int max_keys
= binary
? 20480 : 1;
1610 char **keys
= calloc((size_t)max_keys
, sizeof(char*));
1611 size_t *key_length
=calloc((size_t)max_keys
, sizeof(size_t));
1613 /* First add all of the items.. */
1614 char blob
[1024] = {0};
1615 memcached_return_t rc
;
1616 for (int x
= 0; x
< max_keys
; ++x
)
1619 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
1621 test_truth(keys
[x
] != NULL
);
1622 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1623 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1626 /* Try to get all of them with a large multiget */
1627 unsigned int counter
= 0;
1628 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1629 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1630 (size_t)max_keys
, callbacks
, &counter
, 1);
1634 test_truth(rc
== MEMCACHED_SUCCESS
);
1636 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1637 test_truth(rc
== MEMCACHED_END
);
1639 /* Verify that we got all of the items */
1640 test_truth(counter
== (unsigned int)max_keys
);
1644 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
1645 test_truth(counter
== 0);
1648 /* Release all allocated resources */
1649 for (int x
= 0; x
< max_keys
; ++x
)
1654 memc
->number_of_hosts
= number_of_hosts
;
1655 return TEST_SUCCESS
;
1658 static test_return_t
get_stats_keys(memcached_st
*memc
)
1662 memcached_stat_st memc_stat
;
1663 memcached_return_t rc
;
1665 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1666 test_truth(rc
== MEMCACHED_SUCCESS
);
1667 for (ptr
= stat_list
; *ptr
; ptr
++)
1673 return TEST_SUCCESS
;
1676 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1678 const char *version_string
;
1680 version_string
= memcached_lib_version();
1682 test_truth(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1684 return TEST_SUCCESS
;
1687 static test_return_t
get_stats(memcached_st
*memc
)
1692 memcached_return_t rc
;
1693 memcached_stat_st
*memc_stat
;
1695 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1696 test_truth(rc
== MEMCACHED_SUCCESS
);
1698 test_truth(rc
== MEMCACHED_SUCCESS
);
1699 test_truth(memc_stat
);
1701 for (x
= 0; x
< memcached_server_count(memc
); x
++)
1703 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1704 test_truth(rc
== MEMCACHED_SUCCESS
);
1705 for (ptr
= stat_list
; *ptr
; ptr
++);
1710 memcached_stat_free(NULL
, memc_stat
);
1712 return TEST_SUCCESS
;
1715 static test_return_t
add_host_test(memcached_st
*memc
)
1718 memcached_server_st
*servers
;
1719 memcached_return_t rc
;
1720 char servername
[]= "0.example.com";
1722 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1723 test_truth(servers
);
1724 test_truth(1 == memcached_server_list_count(servers
));
1726 for (x
= 2; x
< 20; x
++)
1728 char buffer
[SMALL_STRING_LEN
];
1730 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1731 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1733 test_truth(rc
== MEMCACHED_SUCCESS
);
1734 test_truth(x
== memcached_server_list_count(servers
));
1737 rc
= memcached_server_push(memc
, servers
);
1738 test_truth(rc
== MEMCACHED_SUCCESS
);
1739 rc
= memcached_server_push(memc
, servers
);
1740 test_truth(rc
== MEMCACHED_SUCCESS
);
1742 memcached_server_list_free(servers
);
1744 return TEST_SUCCESS
;
1747 static memcached_return_t
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1749 return MEMCACHED_SUCCESS
;
1752 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1754 return MEMCACHED_SUCCESS
;
1757 static test_return_t
callback_test(memcached_st
*memc
)
1759 /* Test User Data */
1763 memcached_return_t rc
;
1765 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1766 test_truth(rc
== MEMCACHED_SUCCESS
);
1767 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1768 test_truth(*test_ptr
== x
);
1771 /* Test Clone Callback */
1773 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1774 void *clone_cb_ptr
= *(void **)&clone_cb
;
1775 void *temp_function
= NULL
;
1776 memcached_return_t rc
;
1778 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1780 test_truth(rc
== MEMCACHED_SUCCESS
);
1781 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1782 test_truth(temp_function
== clone_cb_ptr
);
1785 /* Test Cleanup Callback */
1787 memcached_cleanup_fn cleanup_cb
=
1788 (memcached_cleanup_fn
)cleanup_test_callback
;
1789 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1790 void *temp_function
= NULL
;
1791 memcached_return_t rc
;
1793 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1795 test_truth(rc
== MEMCACHED_SUCCESS
);
1796 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1797 test_truth(temp_function
== cleanup_cb_ptr
);
1800 return TEST_SUCCESS
;
1803 /* We don't test the behavior itself, we test the switches */
1804 static test_return_t
behavior_test(memcached_st
*memc
)
1809 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1810 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1811 test_truth(value
== 1);
1813 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1814 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1815 test_truth(value
== 1);
1817 set
= MEMCACHED_HASH_MD5
;
1818 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1819 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1820 test_truth(value
== MEMCACHED_HASH_MD5
);
1824 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1825 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1826 test_truth(value
== 0);
1828 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1829 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1830 test_truth(value
== 0);
1832 set
= MEMCACHED_HASH_DEFAULT
;
1833 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1834 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1835 test_truth(value
== MEMCACHED_HASH_DEFAULT
);
1837 set
= MEMCACHED_HASH_CRC
;
1838 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1839 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1840 test_truth(value
== MEMCACHED_HASH_CRC
);
1842 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1843 test_truth(value
> 0);
1845 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1846 test_truth(value
> 0);
1848 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1849 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1850 test_truth((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1851 return TEST_SUCCESS
;
1854 static test_return_t
fetch_all_results(memcached_st
*memc
)
1856 memcached_return_t rc
= MEMCACHED_SUCCESS
;
1857 char return_key
[MEMCACHED_MAX_KEY
];
1858 size_t return_key_length
;
1860 size_t return_value_length
;
1863 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1864 &return_value_length
, &flags
, &rc
)))
1866 test_truth(return_value
);
1867 test_truth(rc
== MEMCACHED_SUCCESS
);
1871 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
1874 /* Test case provided by Cal Haldenbrand */
1875 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
1877 unsigned int setter
= 1;
1880 unsigned long long total
= 0;
1883 char randomstuff
[6 * 1024];
1884 memcached_return_t rc
;
1886 memset(randomstuff
, 0, 6 * 1024);
1888 /* We just keep looking at the same values over and over */
1891 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1892 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1896 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
1900 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1901 memset(randomstuff
, 0, 6 * 1024);
1902 test_truth(size
< 6 * 1024); /* Being safe here */
1904 for (j
= 0 ; j
< size
;j
++)
1905 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
1908 sprintf(key
, "%d", x
);
1909 rc
= memcached_set(memc
, key
, strlen(key
),
1910 randomstuff
, strlen(randomstuff
), 10, 0);
1911 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1912 /* If we fail, lets try again */
1913 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
1914 rc
= memcached_set(memc
, key
, strlen(key
),
1915 randomstuff
, strlen(randomstuff
), 10, 0);
1916 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1919 return TEST_SUCCESS
;
1922 /* Test case provided by Cal Haldenbrand */
1923 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
1926 unsigned int setter
;
1928 unsigned long long total
;
1931 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1932 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1934 setter
= 20 * 1024576;
1935 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1936 setter
= 20 * 1024576;
1937 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1938 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1939 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1941 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
1944 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
1946 memcached_return_t rc
= MEMCACHED_SUCCESS
;
1947 char buffer
[SMALL_STRING_LEN
];
1952 memset(buffer
, 0, SMALL_STRING_LEN
);
1954 snprintf(buffer
, SMALL_STRING_LEN
, "%u", x
);
1955 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
1956 &val_len
, &flags
, &rc
);
1957 if (rc
!= MEMCACHED_SUCCESS
)
1959 if (rc
== MEMCACHED_NOTFOUND
)
1973 return TEST_SUCCESS
;
1976 /* Do a large mget() over all the keys we think exist */
1977 #define KEY_COUNT 3000 // * 1024576
1978 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
1980 memcached_return_t rc
;
1981 unsigned int setter
;
1984 size_t key_lengths
[KEY_COUNT
];
1987 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
1988 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
1990 setter
= 20 * 1024576;
1991 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
1992 setter
= 20 * 1024576;
1993 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
1994 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1995 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1998 keys
= calloc(KEY_COUNT
, sizeof(char *));
2000 for (x
= 0; x
< KEY_COUNT
; x
++)
2004 snprintf(buffer
, 30, "%u", x
);
2005 keys
[x
]= strdup(buffer
);
2006 key_lengths
[x
]= strlen(keys
[x
]);
2009 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2010 test_truth(rc
== MEMCACHED_SUCCESS
);
2012 test_truth(fetch_all_results(memc
) == TEST_SUCCESS
);
2014 for (x
= 0; x
< KEY_COUNT
; x
++)
2018 return TEST_SUCCESS
;
2021 /* Make sure we behave properly if server list has no values */
2022 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2024 memcached_return_t rc
;
2025 const char *keys
[]= {"fudge", "son", "food"};
2026 size_t key_length
[]= {5, 3, 4};
2029 char return_key
[MEMCACHED_MAX_KEY
];
2030 size_t return_key_length
;
2032 size_t return_value_length
;
2034 /* Here we free everything before running a bunch of mget tests */
2036 memcached_server_list_free(memc
->hosts
);
2038 memc
->number_of_hosts
= 0;
2042 /* We need to empty the server before continueing test */
2043 rc
= memcached_flush(memc
, 0);
2044 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2046 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2047 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2049 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2050 &return_value_length
, &flags
, &rc
)) != NULL
)
2052 test_truth(return_value
);
2054 test_truth(!return_value
);
2055 test_truth(return_value_length
== 0);
2056 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2058 for (x
= 0; x
< 3; x
++)
2060 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2061 keys
[x
], key_length
[x
],
2062 (time_t)50, (uint32_t)9);
2063 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2066 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2067 test_truth(rc
== MEMCACHED_NO_SERVERS
);
2070 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2071 &return_value_length
, &flags
, &rc
)))
2073 test_truth(return_value
);
2074 test_truth(rc
== MEMCACHED_SUCCESS
);
2075 test_truth(return_key_length
== return_value_length
);
2076 test_truth(!memcmp(return_value
, return_key
, return_value_length
));
2081 return TEST_SUCCESS
;
2084 #define VALUE_SIZE_BUG5 1048064
2085 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2087 memcached_return_t rc
;
2088 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2089 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2090 char return_key
[MEMCACHED_MAX_KEY
];
2091 size_t return_key_length
;
2093 size_t value_length
;
2097 char insert_data
[VALUE_SIZE_BUG5
];
2099 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2100 insert_data
[x
]= (signed char)rand();
2102 memcached_flush(memc
, 0);
2103 value
= memcached_get(memc
, keys
[0], key_length
[0],
2104 &value_length
, &flags
, &rc
);
2105 test_truth(value
== NULL
);
2106 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2109 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2110 &value_length
, &flags
, &rc
)))
2112 test_truth(count
== 0);
2114 for (x
= 0; x
< 4; x
++)
2116 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2117 insert_data
, VALUE_SIZE_BUG5
,
2118 (time_t)0, (uint32_t)0);
2119 test_truth(rc
== MEMCACHED_SUCCESS
);
2122 for (x
= 0; x
< 10; x
++)
2124 value
= memcached_get(memc
, keys
[0], key_length
[0],
2125 &value_length
, &flags
, &rc
);
2129 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2131 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2132 &value_length
, &flags
, &rc
)))
2137 test_truth(count
== 4);
2140 return TEST_SUCCESS
;
2143 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2145 memcached_return_t rc
;
2146 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2147 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2148 char return_key
[MEMCACHED_MAX_KEY
];
2149 size_t return_key_length
;
2151 size_t value_length
;
2155 char insert_data
[VALUE_SIZE_BUG5
];
2157 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2158 insert_data
[x
]= (signed char)rand();
2160 memcached_flush(memc
, 0);
2161 value
= memcached_get(memc
, keys
[0], key_length
[0],
2162 &value_length
, &flags
, &rc
);
2163 test_truth(value
== NULL
);
2164 test_truth(rc
== MEMCACHED_NOTFOUND
);
2165 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2166 test_truth(rc
== MEMCACHED_SUCCESS
);
2169 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2170 &value_length
, &flags
, &rc
)))
2172 test_truth(count
== 0);
2173 test_truth(rc
== MEMCACHED_END
);
2175 for (x
= 0; x
< 4; x
++)
2177 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2178 insert_data
, VALUE_SIZE_BUG5
,
2179 (time_t)0, (uint32_t)0);
2180 test_truth(rc
== MEMCACHED_SUCCESS
);
2183 for (x
= 0; x
< 2; x
++)
2185 value
= memcached_get(memc
, keys
[0], key_length
[0],
2186 &value_length
, &flags
, &rc
);
2190 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2191 test_truth(rc
== MEMCACHED_SUCCESS
);
2193 /* We test for purge of partial complete fetches */
2194 for (count
= 3; count
; count
--)
2196 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2197 &value_length
, &flags
, &rc
);
2198 test_truth(rc
== MEMCACHED_SUCCESS
);
2199 test_truth(!(memcmp(value
, insert_data
, value_length
)));
2200 test_truth(value_length
);
2205 return TEST_SUCCESS
;
2208 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2210 memcached_return_t rc
;
2212 memcached_st
*memc_clone
;
2214 memcached_server_st
*servers
;
2215 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";
2217 servers
= memcached_servers_parse(server_list
);
2218 test_truth(servers
);
2220 mine
= memcached_create(NULL
);
2221 rc
= memcached_server_push(mine
, servers
);
2222 test_truth(rc
== MEMCACHED_SUCCESS
);
2223 memcached_server_list_free(servers
);
2226 memc_clone
= memcached_clone(NULL
, mine
);
2228 memcached_quit(mine
);
2229 memcached_quit(memc_clone
);
2232 memcached_free(mine
);
2233 memcached_free(memc_clone
);
2235 return TEST_SUCCESS
;
2238 /* Test flag store/retrieve */
2239 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2241 memcached_return_t rc
;
2242 const char *keys
= "036790384900";
2243 size_t key_length
= strlen(keys
);
2244 char return_key
[MEMCACHED_MAX_KEY
];
2245 size_t return_key_length
;
2247 size_t value_length
;
2250 char insert_data
[VALUE_SIZE_BUG5
];
2252 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2253 insert_data
[x
]= (signed char)rand();
2255 memcached_flush(memc
, 0);
2258 rc
= memcached_set(memc
, keys
, key_length
,
2259 insert_data
, VALUE_SIZE_BUG5
,
2261 test_truth(rc
== MEMCACHED_SUCCESS
);
2264 value
= memcached_get(memc
, keys
, key_length
,
2265 &value_length
, &flags
, &rc
);
2266 test_truth(flags
== 245);
2270 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2273 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2274 &value_length
, &flags
, &rc
);
2275 test_truth(flags
== 245);
2280 return TEST_SUCCESS
;
2283 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2285 memcached_return_t rc
;
2286 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2287 size_t key_length
[3];
2292 char return_key
[MEMCACHED_MAX_KEY
];
2293 size_t return_key_length
;
2295 size_t return_value_length
;
2298 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2299 key_length
[1]= strlen("fudge&*@#");
2300 key_length
[2]= strlen("for^#@&$not");
2303 for (x
= 0; x
< 3; x
++)
2305 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2306 keys
[x
], key_length
[x
],
2307 (time_t)50, (uint32_t)9);
2308 test_truth(rc
== MEMCACHED_SUCCESS
);
2311 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2312 test_truth(rc
== MEMCACHED_SUCCESS
);
2314 /* We need to empty the server before continueing test */
2315 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2316 &return_value_length
, &flags
, &rc
)) != NULL
)
2318 test_truth(return_value
);
2322 test_truth(count
== 3);
2324 return TEST_SUCCESS
;
2327 /* We are testing with aggressive timeout to get failures */
2328 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2330 const char *key
= "foo";
2332 size_t value_length
= 512;
2335 memcached_return_t rc
;
2336 unsigned int set
= 1;
2337 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2340 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2341 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2343 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2346 value
= (char*)malloc(value_length
* sizeof(char));
2348 for (x
= 0; x
< value_length
; x
++)
2349 value
[x
]= (char) (x
% 127);
2351 for (x
= 1; x
<= 100000; ++x
)
2353 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2355 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2356 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2358 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2363 memcached_free(mclone
);
2365 return TEST_SUCCESS
;
2369 We are looking failures in the async protocol
2371 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2373 const char *key
= "foo";
2375 size_t value_length
= 512;
2378 memcached_return_t rc
;
2379 unsigned int set
= 1;
2381 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2383 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2384 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2386 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2389 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2391 test_truth(timeout
== -1);
2393 value
= (char*)malloc(value_length
* sizeof(char));
2395 for (x
= 0; x
< value_length
; x
++)
2396 value
[x
]= (char) (x
% 127);
2398 for (x
= 1; x
<= 100000; ++x
)
2400 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2404 memcached_free(mclone
);
2406 return TEST_SUCCESS
;
2410 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2412 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2414 memcached_return_t rc
;
2416 size_t value_length
;
2418 uint64_t number_value
;
2420 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2421 &value_length
, &flags
, &rc
);
2422 test_truth(value
== NULL
);
2423 test_truth(rc
== MEMCACHED_NOTFOUND
);
2425 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2428 test_truth(value
== NULL
);
2429 /* The binary protocol will set the key if it doesn't exist */
2430 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2432 test_truth(rc
== MEMCACHED_SUCCESS
);
2436 test_truth(rc
== MEMCACHED_NOTFOUND
);
2439 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2441 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2442 &value_length
, &flags
, &rc
);
2444 test_truth(rc
== MEMCACHED_SUCCESS
);
2447 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2449 test_truth(number_value
== 2);
2450 test_truth(rc
== MEMCACHED_SUCCESS
);
2452 return TEST_SUCCESS
;
2456 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2457 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2459 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2461 char key
[] = "key34567890";
2463 memcached_return_t rc
;
2464 size_t overflowSize
;
2466 char commandFirst
[]= "set key34567890 0 0 ";
2467 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2468 size_t commandLength
;
2471 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2473 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2475 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2477 overflow
= malloc(testSize
);
2478 test_truth(overflow
!= NULL
);
2480 memset(overflow
, 'x', testSize
);
2481 rc
= memcached_set(memc
, key
, strlen(key
),
2482 overflow
, testSize
, 0, 0);
2483 test_truth(rc
== MEMCACHED_SUCCESS
);
2487 return TEST_SUCCESS
;
2492 Test values of many different sizes
2493 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2494 set key34567890 0 0 8169 \r\n
2495 is sent followed by buffer of size 8169, followed by 8169
2497 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2500 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2501 memcached_return_t rc
;
2502 const char *key
= "foo";
2504 size_t value_length
= 18000;
2506 size_t string_length
;
2509 size_t current_length
;
2511 value
= (char*)malloc(value_length
);
2514 for (x
= 0; x
< value_length
; x
++)
2515 value
[x
] = (char) (x
% 127);
2517 for (current_length
= 0; current_length
< value_length
; current_length
++)
2519 rc
= memcached_set(memc
, key
, strlen(key
),
2520 value
, current_length
,
2521 (time_t)0, (uint32_t)0);
2522 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2524 string
= memcached_get(memc
, key
, strlen(key
),
2525 &string_length
, &flags
, &rc
);
2527 test_truth(rc
== MEMCACHED_SUCCESS
);
2528 test_truth(string_length
== current_length
);
2529 test_truth(!memcmp(string
, value
, string_length
));
2536 return TEST_SUCCESS
;
2540 Look for zero length value problems
2542 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2545 memcached_return_t rc
;
2546 const char *key
= "mykey";
2551 for (x
= 0; x
< 2; x
++)
2553 rc
= memcached_set(memc
, key
, strlen(key
),
2555 (time_t)0, (uint32_t)0);
2557 test_truth(rc
== MEMCACHED_SUCCESS
);
2559 value
= memcached_get(memc
, key
, strlen(key
),
2560 &length
, &flags
, &rc
);
2562 test_truth(rc
== MEMCACHED_SUCCESS
);
2563 test_truth(value
== NULL
);
2564 test_truth(length
== 0);
2565 test_truth(flags
== 0);
2567 value
= memcached_get(memc
, key
, strlen(key
),
2568 &length
, &flags
, &rc
);
2570 test_truth(rc
== MEMCACHED_SUCCESS
);
2571 test_truth(value
== NULL
);
2572 test_truth(length
== 0);
2573 test_truth(flags
== 0);
2576 return TEST_SUCCESS
;
2579 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2580 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2582 memcached_return_t rc
;
2583 const char *key
= "mykey";
2588 rc
= memcached_set(memc
, key
, strlen(key
),
2590 (time_t)0, UINT32_MAX
);
2592 test_truth(rc
== MEMCACHED_SUCCESS
);
2594 value
= memcached_get(memc
, key
, strlen(key
),
2595 &length
, &flags
, &rc
);
2597 test_truth(rc
== MEMCACHED_SUCCESS
);
2598 test_truth(value
== NULL
);
2599 test_truth(length
== 0);
2600 test_truth(flags
== UINT32_MAX
);
2602 return TEST_SUCCESS
;
2606 /* Check the validity of chinese key*/
2607 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2609 memcached_return_t rc
;
2610 const char *key
= "豆瓣";
2611 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2616 rc
= memcached_set(memc
, key
, strlen(key
),
2617 value
, strlen(value
),
2620 test_truth(rc
== MEMCACHED_SUCCESS
);
2622 value2
= memcached_get(memc
, key
, strlen(key
),
2623 &length
, &flags
, &rc
);
2625 test_truth(length
==strlen(value
));
2626 test_truth(rc
== MEMCACHED_SUCCESS
);
2627 test_truth(memcmp(value
, value2
, length
)==0);
2630 return TEST_SUCCESS
;
2638 static test_return_t
user_supplied_bug19(memcached_st
*memc
)
2641 memcached_server_st
*s
;
2642 memcached_return_t res
;
2646 m
= memcached_create(NULL
);
2647 memcached_server_add_with_weight(m
, "localhost", 11311, 100);
2648 memcached_server_add_with_weight(m
, "localhost", 11312, 100);
2650 s
= memcached_server_by_key(m
, "a", 1, &res
);
2651 memcached_server_free(s
);
2655 return TEST_SUCCESS
;
2658 /* CAS test from Andei */
2659 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2661 memcached_return_t status
;
2662 memcached_result_st
*result
, result_obj
;
2663 const char *key
= "abc";
2664 size_t key_len
= strlen("abc");
2665 const char *value
= "foobar";
2666 size_t value_len
= strlen(value
);
2668 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2670 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2671 test_truth(status
== MEMCACHED_SUCCESS
);
2673 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2674 test_truth(status
== MEMCACHED_SUCCESS
);
2676 result
= memcached_result_create(memc
, &result_obj
);
2679 memcached_result_create(memc
, &result_obj
);
2680 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2683 test_truth(status
== MEMCACHED_SUCCESS
);
2685 memcached_result_free(result
);
2687 return TEST_SUCCESS
;
2690 #include "ketama_test_cases.h"
2691 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2693 memcached_return_t rc
;
2696 memcached_server_st
*server_pool
;
2701 memc
= memcached_create(NULL
);
2704 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2705 test_truth(rc
== MEMCACHED_SUCCESS
);
2707 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2708 test_truth(value
== 1);
2710 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2711 test_truth(rc
== MEMCACHED_SUCCESS
);
2713 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2714 test_truth(value
== MEMCACHED_HASH_MD5
);
2716 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");
2717 memcached_server_push(memc
, server_pool
);
2719 /* verify that the server list was parsed okay. */
2720 test_truth(memc
->number_of_hosts
== 8);
2721 test_truth(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2722 test_truth(server_pool
[0].port
== 11211);
2723 test_truth(server_pool
[0].weight
== 600);
2724 test_truth(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2725 test_truth(server_pool
[2].port
== 11211);
2726 test_truth(server_pool
[2].weight
== 200);
2727 test_truth(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2728 test_truth(server_pool
[7].port
== 11211);
2729 test_truth(server_pool
[7].weight
== 100);
2731 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2732 * us test the boundary wraparound.
2734 test_truth(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2736 /* verify the standard ketama set. */
2737 for (x
= 0; x
< 99; x
++)
2739 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2740 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2741 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2744 memcached_server_list_free(server_pool
);
2745 memcached_free(memc
);
2747 return TEST_SUCCESS
;
2750 /* Large mget() of missing keys with binary proto
2752 * If many binary quiet commands (such as getq's in an mget) fill the output
2753 * buffer and the server chooses not to respond, memcached_flush hangs. See
2754 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2757 /* sighandler_t function that always asserts false */
2758 static void fail(int unused
__attribute__((unused
)))
2764 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2766 memcached_return_t rc
;
2769 size_t* key_lengths
;
2770 void (*oldalarm
)(int);
2771 memcached_st
*memc_clone
;
2773 memc_clone
= memcached_clone(NULL
, memc
);
2774 test_truth(memc_clone
);
2776 /* only binproto uses getq for mget */
2777 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2779 /* empty the cache to ensure misses (hence non-responses) */
2780 rc
= memcached_flush(memc_clone
, 0);
2781 test_truth(rc
== MEMCACHED_SUCCESS
);
2783 key_lengths
= calloc(key_count
, sizeof(size_t));
2784 keys
= calloc(key_count
, sizeof(char *));
2786 for (x
= 0; x
< key_count
; x
++)
2790 snprintf(buffer
, 30, "%u", x
);
2791 keys
[x
]= strdup(buffer
);
2792 key_lengths
[x
]= strlen(keys
[x
]);
2795 oldalarm
= signal(SIGALRM
, fail
);
2798 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2799 test_truth(rc
== MEMCACHED_SUCCESS
);
2802 signal(SIGALRM
, oldalarm
);
2804 test_truth(fetch_all_results(memc
) == TEST_SUCCESS
);
2806 for (x
= 0; x
< key_count
; x
++)
2811 memcached_free(memc_clone
);
2813 return TEST_SUCCESS
;
2816 static test_return_t
pre_binary(memcached_st
*memc
);
2818 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
2820 test_return_t test_rc
;
2821 test_rc
= pre_binary(memc
);
2823 if (test_rc
!= TEST_SUCCESS
)
2828 /* should work as of r580 */
2829 rc
= _user_supplied_bug21(memc
, 10);
2830 test_truth(rc
== TEST_SUCCESS
);
2832 /* should fail as of r580 */
2833 rc
= _user_supplied_bug21(memc
, 1000);
2834 test_truth(rc
== TEST_SUCCESS
);
2836 return TEST_SUCCESS
;
2839 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
2843 memcached_return_t rc
;
2844 memcached_st
*memc
= memcached_create(NULL
);
2847 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2848 test_truth(rc
== MEMCACHED_SUCCESS
);
2850 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2851 test_truth(value
== 1);
2853 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2854 test_truth(rc
== MEMCACHED_SUCCESS
);
2856 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2857 test_truth(value
== MEMCACHED_HASH_MD5
);
2859 /* server should be removed when in delay */
2860 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
2861 test_truth(rc
== MEMCACHED_SUCCESS
);
2863 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
2864 test_truth(value
== 1);
2866 memcached_server_st
*server_pool
;
2867 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");
2868 memcached_server_push(memc
, server_pool
);
2870 /* verify that the server list was parsed okay. */
2871 test_truth(memc
->number_of_hosts
== 8);
2872 test_truth(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2873 test_truth(server_pool
[0].port
== 11211);
2874 test_truth(server_pool
[0].weight
== 600);
2875 test_truth(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2876 test_truth(server_pool
[2].port
== 11211);
2877 test_truth(server_pool
[2].weight
== 200);
2878 test_truth(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2879 test_truth(server_pool
[7].port
== 11211);
2880 test_truth(server_pool
[7].weight
== 100);
2882 memc
->hosts
[2].next_retry
= time(NULL
) + 15;
2883 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2885 for (int x
= 0; x
< 99; x
++)
2887 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2888 test_truth(server_idx
!= 2);
2891 /* and re-added when it's back. */
2892 memc
->hosts
[2].next_retry
= time(NULL
) - 1;
2893 memc
->next_distribution_rebuild
= time(NULL
) - 1;
2894 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
2895 memc
->distribution
);
2896 for (int x
= 0; x
< 99; x
++)
2898 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2899 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2900 test_truth(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
2903 memcached_server_list_free(server_pool
);
2904 memcached_free(memc
);
2906 return TEST_SUCCESS
;
2909 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
2913 memcached_return_t rc
;
2914 memcached_st
*memc
= memcached_create(NULL
);
2918 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2919 test_truth(rc
== MEMCACHED_SUCCESS
);
2921 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2922 test_truth(value
== 1);
2924 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2925 test_truth(rc
== MEMCACHED_SUCCESS
);
2927 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2928 test_truth(value
== MEMCACHED_HASH_MD5
);
2931 test_truth(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
2933 memcached_server_st
*server_pool
;
2934 server_pool
= memcached_servers_parse("10.0.1.1:11211,10.0.1.2:11211,10.0.1.3:11211,10.0.1.4:11211,10.0.1.5:11211,10.0.1.6:11211,10.0.1.7:11211,10.0.1.8:11211,192.168.1.1:11211,192.168.100.1:11211");
2935 memcached_server_push(memc
, server_pool
);
2937 // @todo this needs to be refactored to actually test something.
2940 if ((fp
= fopen("ketama_keys.txt", "w")))
2944 printf("cannot write to file ketama_keys.txt");
2945 return TEST_FAILURE
;
2948 for (int x
= 0; x
< 10000; x
++)
2951 sprintf(key
, "%d", x
);
2953 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
2954 char *hostname
= memc
->hosts
[server_idx
].hostname
;
2955 unsigned int port
= memc
->hosts
[server_idx
].port
;
2956 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
2960 memcached_server_list_free(server_pool
);
2961 memcached_free(memc
);
2963 return TEST_SUCCESS
;
2967 static test_return_t
result_static(memcached_st
*memc
)
2969 memcached_result_st result
;
2970 memcached_result_st
*result_ptr
;
2972 result_ptr
= memcached_result_create(memc
, &result
);
2973 test_truth(result
.options
.is_allocated
== false);
2974 test_truth(memcached_is_initialized(&result
) == true);
2975 test_truth(result_ptr
);
2976 test_truth(result_ptr
== &result
);
2978 memcached_result_free(&result
);
2980 test_truth(result
.options
.is_allocated
== false);
2981 test_truth(memcached_is_initialized(&result
) == false);
2983 return TEST_SUCCESS
;
2986 static test_return_t
result_alloc(memcached_st
*memc
)
2988 memcached_result_st
*result_ptr
;
2990 result_ptr
= memcached_result_create(memc
, NULL
);
2991 test_truth(result_ptr
);
2992 test_truth(result_ptr
->options
.is_allocated
== true);
2993 test_truth(memcached_is_initialized(result_ptr
) == true);
2994 memcached_result_free(result_ptr
);
2996 return TEST_SUCCESS
;
2999 static test_return_t
string_static_null(memcached_st
*memc
)
3001 memcached_string_st string
;
3002 memcached_string_st
*string_ptr
;
3004 string_ptr
= memcached_string_create(memc
, &string
, 0);
3005 test_truth(string
.options
.is_initialized
== true);
3006 test_truth(string_ptr
);
3008 /* The following two better be the same! */
3009 test_truth(memcached_is_allocated(string_ptr
) == false);
3010 test_truth(memcached_is_allocated(&string
) == false);
3011 test_truth(&string
== string_ptr
);
3013 test_truth(string
.options
.is_initialized
== true);
3014 test_truth(memcached_is_initialized(&string
) == true);
3015 memcached_string_free(&string
);
3016 test_truth(memcached_is_initialized(&string
) == false);
3018 return TEST_SUCCESS
;
3021 static test_return_t
string_alloc_null(memcached_st
*memc
)
3023 memcached_string_st
*string
;
3025 string
= memcached_string_create(memc
, NULL
, 0);
3027 test_truth(memcached_is_allocated(string
) == true);
3028 test_truth(memcached_is_initialized(string
) == true);
3029 memcached_string_free(string
);
3031 return TEST_SUCCESS
;
3034 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3036 memcached_string_st
*string
;
3038 string
= memcached_string_create(memc
, NULL
, 1024);
3040 test_truth(memcached_is_allocated(string
) == true);
3041 test_truth(memcached_is_initialized(string
) == true);
3042 memcached_string_free(string
);
3044 return TEST_SUCCESS
;
3047 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3049 memcached_string_st
*string
;
3051 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3052 test_truth(string
== NULL
);
3054 return TEST_SUCCESS
;
3057 static test_return_t
string_alloc_append(memcached_st
*memc
)
3060 char buffer
[SMALL_STRING_LEN
];
3061 memcached_string_st
*string
;
3063 /* Ring the bell! */
3064 memset(buffer
, 6, SMALL_STRING_LEN
);
3066 string
= memcached_string_create(memc
, NULL
, 100);
3068 test_truth(memcached_is_allocated(string
) == true);
3069 test_truth(memcached_is_initialized(string
) == true);
3071 for (x
= 0; x
< 1024; x
++)
3073 memcached_return_t rc
;
3074 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3075 test_truth(rc
== MEMCACHED_SUCCESS
);
3077 test_truth(memcached_is_allocated(string
) == true);
3078 memcached_string_free(string
);
3080 return TEST_SUCCESS
;
3083 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3085 memcached_return_t rc
;
3087 char buffer
[SMALL_STRING_LEN
];
3088 memcached_string_st
*string
;
3090 /* Ring the bell! */
3091 memset(buffer
, 6, SMALL_STRING_LEN
);
3093 string
= memcached_string_create(memc
, NULL
, 100);
3095 test_truth(memcached_is_allocated(string
) == true);
3096 test_truth(memcached_is_initialized(string
) == true);
3098 for (x
= 0; x
< 1024; x
++)
3100 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3101 test_truth(rc
== MEMCACHED_SUCCESS
);
3103 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3104 test_truth(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3105 test_truth(memcached_is_allocated(string
) == true);
3106 memcached_string_free(string
);
3108 return TEST_SUCCESS
;
3111 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3113 pairs_free(global_pairs
);
3115 return TEST_SUCCESS
;
3118 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3120 unsigned long long x
;
3121 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3122 global_count
= GLOBAL_COUNT
;
3124 for (x
= 0; x
< global_count
; x
++)
3126 global_keys
[x
]= global_pairs
[x
].key
;
3127 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3130 return TEST_SUCCESS
;
3133 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3135 unsigned long long x
;
3136 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3137 global_count
= GLOBAL2_COUNT
;
3139 for (x
= 0; x
< global_count
; x
++)
3141 global_keys
[x
]= global_pairs
[x
].key
;
3142 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3145 return TEST_SUCCESS
;
3148 static test_return_t
generate_data(memcached_st
*memc
)
3150 execute_set(memc
, global_pairs
, global_count
);
3152 return TEST_SUCCESS
;
3155 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3157 memcached_stat_st
*stat_p
;
3158 memcached_return_t rc
;
3159 uint32_t host_index
= 0;
3160 execute_set(memc
, global_pairs
, global_count
);
3162 //TODO: hosts used size stats
3163 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3166 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3168 /* This test was changes so that "make test" would work properlly */
3170 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
);
3172 test_truth((unsigned long long)(stat_p
+ host_index
)->bytes
);
3175 memcached_stat_free(NULL
, stat_p
);
3177 return TEST_SUCCESS
;
3179 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3184 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3185 generate_data(memc
);
3187 return TEST_SUCCESS
;
3190 static test_return_t
get_read_count(memcached_st
*memc
)
3193 memcached_return_t rc
;
3194 memcached_st
*memc_clone
;
3196 memc_clone
= memcached_clone(NULL
, memc
);
3197 test_truth(memc_clone
);
3199 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3203 size_t return_value_length
;
3207 for (x
= count
= 0; x
< global_count
; x
++)
3209 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3210 &return_value_length
, &flags
, &rc
);
3211 if (rc
== MEMCACHED_SUCCESS
)
3220 memcached_free(memc_clone
);
3222 return TEST_SUCCESS
;
3225 static test_return_t
get_read(memcached_st
*memc
)
3228 memcached_return_t rc
;
3232 size_t return_value_length
;
3235 for (x
= 0; x
< global_count
; x
++)
3237 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3238 &return_value_length
, &flags
, &rc
);
3240 test_truth(return_value);
3241 test_truth(rc == MEMCACHED_SUCCESS);
3243 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3248 return TEST_SUCCESS
;
3251 static test_return_t
mget_read(memcached_st
*memc
)
3253 memcached_return_t rc
;
3255 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3256 test_truth(rc
== MEMCACHED_SUCCESS
);
3257 test_truth(fetch_all_results(memc
) == TEST_SUCCESS
);
3259 return TEST_SUCCESS
;
3262 static test_return_t
mget_read_result(memcached_st
*memc
)
3264 memcached_return_t rc
;
3266 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3267 test_truth(rc
== MEMCACHED_SUCCESS
);
3268 /* Turn this into a help function */
3270 memcached_result_st results_obj
;
3271 memcached_result_st
*results
;
3273 results
= memcached_result_create(memc
, &results_obj
);
3275 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3277 test_truth(results
);
3278 test_truth(rc
== MEMCACHED_SUCCESS
);
3281 memcached_result_free(&results_obj
);
3284 return TEST_SUCCESS
;
3287 static test_return_t
mget_read_function(memcached_st
*memc
)
3289 memcached_return_t rc
;
3290 unsigned int counter
;
3291 memcached_execute_fn callbacks
[1];
3293 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3294 test_truth(rc
== MEMCACHED_SUCCESS
);
3296 callbacks
[0]= &callback_counter
;
3298 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3300 return TEST_SUCCESS
;
3303 static test_return_t
delete_generate(memcached_st
*memc
)
3307 for (x
= 0; x
< global_count
; x
++)
3309 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3312 return TEST_SUCCESS
;
3315 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3321 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3323 for (x
= 0; x
< global_count
; x
++)
3325 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3328 return TEST_SUCCESS
;
3331 static test_return_t
add_host_test1(memcached_st
*memc
)
3334 memcached_return_t rc
;
3335 char servername
[]= "0.example.com";
3336 memcached_server_st
*servers
;
3338 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3339 test_truth(servers
);
3340 test_truth(1 == memcached_server_list_count(servers
));
3342 for (x
= 2; x
< 20; x
++)
3344 char buffer
[SMALL_STRING_LEN
];
3346 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
3347 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3349 test_truth(rc
== MEMCACHED_SUCCESS
);
3350 test_truth(x
== memcached_server_list_count(servers
));
3353 rc
= memcached_server_push(memc
, servers
);
3354 test_truth(rc
== MEMCACHED_SUCCESS
);
3355 rc
= memcached_server_push(memc
, servers
);
3356 test_truth(rc
== MEMCACHED_SUCCESS
);
3358 memcached_server_list_free(servers
);
3360 return TEST_SUCCESS
;
3363 static test_return_t
pre_nonblock(memcached_st
*memc
)
3365 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3367 return TEST_SUCCESS
;
3370 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3372 memcached_return_t rc
= MEMCACHED_FAILURE
;
3373 memcached_st
*memc_clone
;
3375 memc_clone
= memcached_clone(NULL
, memc
);
3377 // The memcached_version needs to be done on a clone, because the server
3378 // will not toggle protocol on an connection.
3379 memcached_version(memc_clone
);
3381 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3383 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3384 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3385 test_truth(rc
== MEMCACHED_SUCCESS
);
3386 assert(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3390 return TEST_SKIPPED
;
3393 memcached_free(memc_clone
);
3395 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3398 static test_return_t
pre_murmur(memcached_st
*memc
)
3400 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3402 return TEST_SUCCESS
;
3405 static test_return_t
pre_jenkins(memcached_st
*memc
)
3407 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3409 return TEST_SUCCESS
;
3413 static test_return_t
pre_md5(memcached_st
*memc
)
3415 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3417 return TEST_SUCCESS
;
3420 static test_return_t
pre_crc(memcached_st
*memc
)
3422 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3424 return TEST_SUCCESS
;
3427 static test_return_t
pre_hsieh(memcached_st
*memc
)
3429 #ifdef HAVE_HSIEH_HASH
3430 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3431 return TEST_SUCCESS
;
3434 return TEST_SKIPPED
;
3438 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3440 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3442 return TEST_SUCCESS
;
3445 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3447 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3449 return TEST_SUCCESS
;
3452 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3454 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3456 return TEST_SUCCESS
;
3459 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3461 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3463 return TEST_SUCCESS
;
3466 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3468 memcached_return_t rc
;
3471 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3472 test_truth(rc
== MEMCACHED_SUCCESS
);
3474 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3475 test_truth(value
== 1);
3477 return TEST_SUCCESS
;
3480 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3482 memcached_return_t rc
;
3485 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3486 test_truth(rc
== MEMCACHED_SUCCESS
);
3488 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3489 test_truth(value
== 1);
3491 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3492 test_truth(rc
== MEMCACHED_SUCCESS
);
3494 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3495 test_truth(value
== MEMCACHED_HASH_MD5
);
3497 return TEST_SUCCESS
;
3501 @note This should be testing to see if the server really supports the binary protocol.
3503 static test_return_t
pre_binary(memcached_st
*memc
)
3505 memcached_return_t rc
= MEMCACHED_FAILURE
;
3506 memcached_st
*memc_clone
;
3508 memc_clone
= memcached_clone(NULL
, memc
);
3509 test_truth(memc_clone
);
3510 // The memcached_version needs to be done on a clone, because the server
3511 // will not toggle protocol on an connection.
3512 memcached_version(memc_clone
);
3514 if (memc_clone
->hosts
[0].major_version
>= 1 && memc_clone
->hosts
[0].minor_version
> 2)
3516 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3517 test_truth(rc
== MEMCACHED_SUCCESS
);
3518 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3521 memcached_free(memc_clone
);
3523 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3526 static test_return_t
pre_replication(memcached_st
*memc
)
3528 test_return_t test_rc
;
3529 test_rc
= pre_binary(memc
);
3531 if (test_rc
!= TEST_SUCCESS
)
3535 * Make sure that we store the item on all servers
3536 * (master + replicas == number of servers)
3538 memcached_return_t rc
;
3539 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3540 memc
->number_of_hosts
- 1);
3541 test_truth(rc
== MEMCACHED_SUCCESS
);
3542 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memc
->number_of_hosts
- 1);
3544 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3547 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3549 test_return_t rc
= MEMCACHED_FAILURE
;
3550 if (pre_replication(memc
) == TEST_SUCCESS
&&
3551 pre_nonblock(memc
) == TEST_SUCCESS
)
3557 static void my_free(memcached_st
*ptr
__attribute__((unused
)), void *mem
)
3562 static void *my_malloc(memcached_st
*ptr
__attribute__((unused
)), const size_t size
)
3564 void *ret
= malloc(size
);
3566 memset(ret
, 0xff, size
);
3571 static void *my_realloc(memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
)
3573 return realloc(mem
, size
);
3576 static void *my_calloc(memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
)
3578 return calloc(nelem
, size
);
3581 static test_return_t
set_prefix(memcached_st
*memc
)
3583 memcached_return_t rc
;
3584 const char *key
= "mine";
3587 /* Make sure be default none exists */
3588 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3589 test_truth(rc
== MEMCACHED_FAILURE
);
3591 /* Test a clean set */
3592 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3593 test_truth(rc
== MEMCACHED_SUCCESS
);
3595 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3596 test_truth(memcmp(value
, key
, 4) == 0);
3597 test_truth(rc
== MEMCACHED_SUCCESS
);
3599 /* Test that we can turn it off */
3600 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3601 test_truth(rc
== MEMCACHED_SUCCESS
);
3603 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3604 test_truth(rc
== MEMCACHED_FAILURE
);
3606 /* Now setup for main test */
3607 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3608 test_truth(rc
== MEMCACHED_SUCCESS
);
3610 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3611 test_truth(rc
== MEMCACHED_SUCCESS
);
3612 test_truth(memcmp(value
, key
, 4) == 0);
3614 /* Set to Zero, and then Set to something too large */
3617 memset(long_key
, 0, 255);
3619 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3620 test_truth(rc
== MEMCACHED_SUCCESS
);
3622 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3623 test_truth(rc
== MEMCACHED_FAILURE
);
3624 test_truth(value
== NULL
);
3626 /* Test a long key for failure */
3627 /* TODO, extend test to determine based on setting, what result should be */
3628 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3629 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3630 //test_truth(rc == MEMCACHED_BAD_KEY_PROVIDED);
3631 test_truth(rc
== MEMCACHED_SUCCESS
);
3633 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3634 strcpy(long_key
, "This is more then the allotted number of characters");
3635 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3636 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3638 /* Test for a bad prefix, but with a short key */
3639 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3640 test_truth(rc
== MEMCACHED_SUCCESS
);
3642 strcpy(long_key
, "dog cat");
3643 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3644 test_truth(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3647 return TEST_SUCCESS
;
3650 #ifdef MEMCACHED_ENABLE_DEPRECATED
3651 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3653 void *test_ptr
= NULL
;
3656 memcached_malloc_fn malloc_cb
=
3657 (memcached_malloc_fn
)my_malloc
;
3658 cb_ptr
= *(void **)&malloc_cb
;
3659 memcached_return_t rc
;
3661 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3662 test_truth(rc
== MEMCACHED_SUCCESS
);
3663 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3664 test_truth(rc
== MEMCACHED_SUCCESS
);
3665 test_truth(test_ptr
== cb_ptr
);
3669 memcached_realloc_fn realloc_cb
=
3670 (memcached_realloc_fn
)my_realloc
;
3671 cb_ptr
= *(void **)&realloc_cb
;
3672 memcached_return_t rc
;
3674 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3675 test_truth(rc
== MEMCACHED_SUCCESS
);
3676 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3677 test_truth(rc
== MEMCACHED_SUCCESS
);
3678 test_truth(test_ptr
== cb_ptr
);
3682 memcached_free_fn free_cb
=
3683 (memcached_free_fn
)my_free
;
3684 cb_ptr
= *(void **)&free_cb
;
3685 memcached_return_t rc
;
3687 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3688 test_truth(rc
== MEMCACHED_SUCCESS
);
3689 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3690 test_truth(rc
== MEMCACHED_SUCCESS
);
3691 test_truth(test_ptr
== cb_ptr
);
3694 return TEST_SUCCESS
;
3698 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3700 memcached_return_t rc
;
3701 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3702 my_realloc
, my_calloc
);
3703 test_truth(rc
== MEMCACHED_FAILURE
);
3705 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3706 my_realloc
, my_calloc
);
3708 memcached_malloc_fn mem_malloc
;
3709 memcached_free_fn mem_free
;
3710 memcached_realloc_fn mem_realloc
;
3711 memcached_calloc_fn mem_calloc
;
3712 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3713 &mem_realloc
, &mem_calloc
);
3715 test_truth(mem_malloc
== my_malloc
);
3716 test_truth(mem_realloc
== my_realloc
);
3717 test_truth(mem_calloc
== my_calloc
);
3718 test_truth(mem_free
== my_free
);
3720 return TEST_SUCCESS
;
3723 static test_return_t
enable_consistent(memcached_st
*memc
)
3726 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3727 memcached_hash_t hash
;
3728 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3729 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
3732 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3733 test_truth(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3735 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3737 if (hash
!= MEMCACHED_HASH_HSIEH
)
3738 return TEST_SKIPPED
;
3741 return TEST_SUCCESS
;
3744 static test_return_t
enable_cas(memcached_st
*memc
)
3746 unsigned int set
= 1;
3748 memcached_version(memc
);
3750 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3751 || memc
->hosts
[0].minor_version
> 2)
3753 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
3755 return TEST_SUCCESS
;
3758 return TEST_SKIPPED
;
3761 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
3763 memcached_version(memc
);
3765 if ((memc
->hosts
[0].major_version
>= 1 && (memc
->hosts
[0].minor_version
== 2 && memc
->hosts
[0].micro_version
>= 4))
3766 || memc
->hosts
[0].minor_version
> 2)
3767 return TEST_SUCCESS
;
3769 return TEST_SKIPPED
;
3772 static test_return_t
pre_unix_socket(memcached_st
*memc
)
3774 memcached_return_t rc
;
3777 memcached_server_list_free(memc
->hosts
);
3779 memc
->number_of_hosts
= 0;
3781 if (stat("/tmp/memcached.socket", &buf
))
3782 return TEST_SKIPPED
;
3784 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
3786 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
3789 static test_return_t
pre_nodelay(memcached_st
*memc
)
3791 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3792 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
3794 return TEST_SUCCESS
;
3797 static test_return_t
pre_settimer(memcached_st
*memc
)
3799 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
3800 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
3802 return TEST_SUCCESS
;
3805 static test_return_t
poll_timeout(memcached_st
*memc
)
3811 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
3813 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
3815 test_truth(timeout
== 100);
3817 return TEST_SUCCESS
;
3820 static test_return_t
noreply_test(memcached_st
*memc
)
3822 memcached_return_t ret
;
3823 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
3824 test_truth(ret
== MEMCACHED_SUCCESS
);
3825 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
3826 test_truth(ret
== MEMCACHED_SUCCESS
);
3827 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
3828 test_truth(ret
== MEMCACHED_SUCCESS
);
3829 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
3830 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
3831 test_truth(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
3833 for (int count
=0; count
< 5; ++count
)
3835 for (int x
=0; x
< 100; ++x
)
3838 size_t len
= (size_t)sprintf(key
, "%d", x
);
3842 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
3845 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
3848 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
3851 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
3854 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
3860 test_truth(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
3864 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3865 ** API and is _ONLY_ done this way to verify that the library works the
3866 ** way it is supposed to do!!!!
3869 for (uint32_t x
=0; x
< memc
->number_of_hosts
; ++x
)
3870 no_msg
+=(int)(memc
->hosts
[x
].cursor_active
);
3872 test_truth(no_msg
== 0);
3873 test_truth(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3876 ** Now validate that all items was set properly!
3878 for (int x
=0; x
< 100; ++x
)
3881 size_t len
= (size_t)sprintf(key
, "%d", x
);
3884 char* value
=memcached_get(memc
, key
, strlen(key
),
3885 &length
, &flags
, &ret
);
3886 test_truth(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3889 case 0: /* FALLTHROUGH */
3890 case 1: /* FALLTHROUGH */
3892 test_truth(strncmp(value
, key
, len
) == 0);
3893 test_truth(len
== length
);
3896 test_truth(length
== len
* 2);
3899 test_truth(length
== len
* 3);
3909 /* Try setting an illegal cas value (should not return an error to
3910 * the caller (because we don't expect a return message from the server)
3912 const char* keys
[]= {"0"};
3913 size_t lengths
[]= {1};
3916 memcached_result_st results_obj
;
3917 memcached_result_st
*results
;
3918 ret
= memcached_mget(memc
, keys
, lengths
, 1);
3919 test_truth(ret
== MEMCACHED_SUCCESS
);
3921 results
= memcached_result_create(memc
, &results_obj
);
3922 test_truth(results
);
3923 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
3924 test_truth(results
);
3925 test_truth(ret
== MEMCACHED_SUCCESS
);
3926 uint64_t cas
= memcached_result_cas(results
);
3927 memcached_result_free(&results_obj
);
3929 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3930 test_truth(ret
== MEMCACHED_SUCCESS
);
3933 * The item will have a new cas value, so try to set it again with the old
3934 * value. This should fail!
3936 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
3937 test_truth(ret
== MEMCACHED_SUCCESS
);
3938 test_truth(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
3939 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
3940 test_truth(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
3943 return TEST_SUCCESS
;
3946 static test_return_t
analyzer_test(memcached_st
*memc
)
3948 memcached_return_t rc
;
3949 memcached_stat_st
*memc_stat
;
3950 memcached_analysis_st
*report
;
3952 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
3953 test_truth(rc
== MEMCACHED_SUCCESS
);
3954 test_truth(memc_stat
);
3956 report
= memcached_analyze(memc
, memc_stat
, &rc
);
3957 test_truth(rc
== MEMCACHED_SUCCESS
);
3961 memcached_stat_free(NULL
, memc_stat
);
3963 return TEST_SUCCESS
;
3966 /* Count the objects */
3967 static memcached_return_t
callback_dump_counter(memcached_st
*ptr
__attribute__((unused
)),
3968 const char *key
__attribute__((unused
)),
3969 size_t key_length
__attribute__((unused
)),
3972 uint32_t *counter
= (uint32_t *)context
;
3974 *counter
= *counter
+ 1;
3976 return MEMCACHED_SUCCESS
;
3979 static test_return_t
dump_test(memcached_st
*memc
)
3981 memcached_return_t rc
;
3982 uint32_t counter
= 0;
3983 memcached_dump_fn callbacks
[1];
3984 test_return_t main_rc
;
3986 callbacks
[0]= &callback_dump_counter
;
3988 /* No support for Binary protocol yet */
3989 if (memc
->flags
.binary_protocol
)
3990 return TEST_SUCCESS
;
3992 main_rc
= set_test3(memc
);
3994 test_truth (main_rc
== TEST_SUCCESS
);
3996 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
3997 test_truth(rc
== MEMCACHED_SUCCESS
);
3999 /* We may have more then 32 if our previous flush has not completed */
4000 test_truth(counter
>= 32);
4002 return TEST_SUCCESS
;
4005 #ifdef HAVE_LIBMEMCACHEDUTIL
4006 static void* connection_release(void *arg
)
4009 memcached_pool_st
* pool
;
4014 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4018 static test_return_t
connection_pool_test(memcached_st
*memc
)
4020 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
4021 test_truth(pool
!= NULL
);
4022 memcached_st
* mmc
[10];
4023 memcached_return_t rc
;
4025 for (int x
= 0; x
< 10; ++x
) {
4026 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4027 test_truth(mmc
[x
] != NULL
);
4028 test_truth(rc
== MEMCACHED_SUCCESS
);
4031 test_truth(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4032 test_truth(rc
== MEMCACHED_SUCCESS
);
4036 memcached_pool_st
* pool
;
4038 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4039 pthread_create(&tid
, NULL
, connection_release
, &item
);
4040 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4041 test_truth(rc
== MEMCACHED_SUCCESS
);
4042 pthread_join(tid
, NULL
);
4043 test_truth(mmc
[9] == item
.mmc
);
4044 const char *key
= "key";
4045 size_t keylen
= strlen(key
);
4047 // verify that I can do ops with all connections
4048 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4049 test_truth(rc
== MEMCACHED_SUCCESS
);
4051 for (unsigned int x
= 0; x
< 10; ++x
) {
4052 uint64_t number_value
;
4053 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4054 test_truth(rc
== MEMCACHED_SUCCESS
);
4055 test_truth(number_value
== (x
+1));
4059 for (int x
= 0; x
< 10; ++x
)
4060 test_truth(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4063 /* verify that I can set behaviors on the pool when I don't have all
4064 * of the connections in the pool. It should however be enabled
4065 * when I push the item into the pool
4067 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4068 test_truth(mmc
[0] != NULL
);
4070 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4071 test_truth(rc
== MEMCACHED_SUCCESS
);
4073 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4074 test_truth(mmc
[1] != NULL
);
4076 test_truth(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4077 test_truth(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4078 test_truth(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4080 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4081 test_truth(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4082 test_truth(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4085 test_truth(memcached_pool_destroy(pool
) == memc
);
4086 return TEST_SUCCESS
;
4090 static test_return_t
replication_set_test(memcached_st
*memc
)
4092 memcached_return_t rc
;
4093 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4094 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4096 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4097 test_truth(rc
== MEMCACHED_SUCCESS
);
4100 ** We are using the quiet commands to store the replicas, so we need
4101 ** to ensure that all of them are processed before we can continue.
4102 ** In the test we go directly from storing the object to trying to
4103 ** receive the object from all of the different servers, so we
4104 ** could end up in a race condition (the memcached server hasn't yet
4105 ** processed the quiet command from the replication set when it process
4106 ** the request from the other client (created by the clone)). As a
4107 ** workaround for that we call memcached_quit to send the quit command
4108 ** to the server and wait for the response ;-) If you use the test code
4109 ** as an example for your own code, please note that you shouldn't need
4112 memcached_quit(memc
);
4115 ** "bubba" should now be stored on all of our servers. We don't have an
4116 ** easy to use API to address each individual server, so I'll just iterate
4117 ** through a bunch of "master keys" and I should most likely hit all of the
4120 for (int x
= 'a'; x
<= 'z'; ++x
)
4122 char key
[2]= { [0]= (char)x
};
4125 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4127 test_truth(rc
== MEMCACHED_SUCCESS
);
4128 test_truth(val
!= NULL
);
4132 memcached_free(memc_clone
);
4134 return TEST_SUCCESS
;
4137 static test_return_t
replication_get_test(memcached_st
*memc
)
4139 memcached_return_t rc
;
4142 * Don't do the following in your code. I am abusing the internal details
4143 * within the library, and this is not a supported interface.
4144 * This is to verify correct behavior in the library
4146 for (uint32_t host
= 0; host
< memc
->number_of_hosts
; ++host
)
4148 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4149 memc_clone
->hosts
[host
].port
= 0;
4151 for (int x
= 'a'; x
<= 'z'; ++x
)
4153 char key
[2]= { [0]= (char)x
};
4156 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4158 test_truth(rc
== MEMCACHED_SUCCESS
);
4159 test_truth(val
!= NULL
);
4163 memcached_free(memc_clone
);
4166 return TEST_SUCCESS
;
4169 static test_return_t
replication_mget_test(memcached_st
*memc
)
4171 memcached_return_t rc
;
4172 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4173 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4175 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4176 size_t len
[]= { 5, 4, 4, 4 };
4178 for (int x
=0; x
< 4; ++x
)
4180 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4181 test_truth(rc
== MEMCACHED_SUCCESS
);
4185 ** We are using the quiet commands to store the replicas, so we need
4186 ** to ensure that all of them are processed before we can continue.
4187 ** In the test we go directly from storing the object to trying to
4188 ** receive the object from all of the different servers, so we
4189 ** could end up in a race condition (the memcached server hasn't yet
4190 ** processed the quiet command from the replication set when it process
4191 ** the request from the other client (created by the clone)). As a
4192 ** workaround for that we call memcached_quit to send the quit command
4193 ** to the server and wait for the response ;-) If you use the test code
4194 ** as an example for your own code, please note that you shouldn't need
4197 memcached_quit(memc
);
4200 * Don't do the following in your code. I am abusing the internal details
4201 * within the library, and this is not a supported interface.
4202 * This is to verify correct behavior in the library
4204 memcached_result_st result_obj
;
4205 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4207 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4208 new_clone
->hosts
[host
].port
= 0;
4210 for (int x
= 'a'; x
<= 'z'; ++x
)
4212 const char key
[2]= { [0]= (const char)x
};
4214 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4215 test_truth(rc
== MEMCACHED_SUCCESS
);
4217 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4218 test_truth(results
);
4221 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4225 test_truth(hits
== 4);
4226 memcached_result_free(&result_obj
);
4229 memcached_free(new_clone
);
4232 memcached_free(memc_clone
);
4234 return TEST_SUCCESS
;
4237 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4239 memcached_result_st result_obj
;
4240 memcached_return_t rc
;
4241 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4242 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4243 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4245 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4246 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4248 for (int x
=0; x
< 7; ++x
)
4250 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4251 test_truth(rc
== MEMCACHED_SUCCESS
);
4254 memcached_quit(memc
);
4256 for (int x
=0; x
< 7; ++x
) {
4257 const char key
[2]= { [0]= (const char)x
};
4259 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4260 test_truth(rc
== MEMCACHED_SUCCESS
);
4262 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4263 test_truth(results
);
4266 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4270 test_truth(hits
== 7);
4271 memcached_result_free(&result_obj
);
4273 memcached_free(memc_clone
);
4274 return TEST_SUCCESS
;
4277 static test_return_t
replication_delete_test(memcached_st
*memc
)
4279 memcached_return_t rc
;
4280 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4281 /* Delete the items from all of the servers except 1 */
4282 uint64_t repl
= memcached_behavior_get(memc
,
4283 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4284 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4286 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4287 size_t len
[]= { 5, 4, 4, 4 };
4289 for (int x
=0; x
< 4; ++x
)
4291 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4292 test_truth(rc
== MEMCACHED_SUCCESS
);
4296 * Don't do the following in your code. I am abusing the internal details
4297 * within the library, and this is not a supported interface.
4298 * This is to verify correct behavior in the library
4300 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4301 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4303 memc_clone
->hosts
[hash
].port
= 0;
4304 if (++hash
== memc_clone
->number_of_hosts
)
4308 memcached_result_st result_obj
;
4309 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4311 for (int x
= 'a'; x
<= 'z'; ++x
)
4313 const char key
[2]= { [0]= (const char)x
};
4315 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4316 test_truth(rc
== MEMCACHED_SUCCESS
);
4318 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4319 test_truth(results
);
4322 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4326 test_truth(hits
== 4);
4327 memcached_result_free(&result_obj
);
4330 memcached_free(memc_clone
);
4332 return TEST_SUCCESS
;
4335 static void increment_request_id(uint16_t *id
)
4338 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
4342 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
4344 uint16_t *ids
= malloc(sizeof(uint16_t) * memc
->number_of_hosts
);
4345 assert(ids
!= NULL
);
4348 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4349 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) memc
->hosts
[x
].write_buffer
);
4354 static test_return_t
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
4357 memcached_server_st
*cur_server
= memc
->hosts
;
4358 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
4360 for (x
= 0; x
< memc
->number_of_hosts
; x
++)
4362 test_truth(cur_server
[x
].cursor_active
== 0);
4363 test_truth(cur_req_ids
[x
] == expected_req_ids
[x
]);
4365 free(expected_req_ids
);
4368 return TEST_SUCCESS
;
4372 ** There is a little bit of a hack here, instead of removing
4373 ** the servers, I just set num host to 0 and them add then new udp servers
4375 static test_return_t
init_udp(memcached_st
*memc
)
4377 memcached_version(memc
);
4378 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
4379 if (memc
->hosts
[0].major_version
!= 1 || memc
->hosts
[0].minor_version
!= 2
4380 || memc
->hosts
[0].micro_version
< 6)
4381 return TEST_SKIPPED
;
4383 uint32_t num_hosts
= memc
->number_of_hosts
;
4385 memcached_server_st servers
[num_hosts
];
4386 memcpy(servers
, memc
->hosts
, sizeof(memcached_server_st
) * num_hosts
);
4387 for (x
= 0; x
< num_hosts
; x
++)
4388 memcached_server_free(&memc
->hosts
[x
]);
4390 memc
->number_of_hosts
= 0;
4391 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
4392 for (x
= 0; x
< num_hosts
; x
++)
4394 test_truth(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
4395 test_truth(memc
->hosts
[x
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4398 return TEST_SUCCESS
;
4401 static test_return_t
binary_init_udp(memcached_st
*memc
)
4403 test_return_t test_rc
;
4404 test_rc
= pre_binary(memc
);
4406 if (test_rc
!= TEST_SUCCESS
)
4409 return init_udp(memc
);
4412 /* Make sure that I cant add a tcp server to a udp client */
4413 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
4415 memcached_server_st server
;
4416 memcached_server_clone(&server
, &memc
->hosts
[0]);
4417 test_truth(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4418 test_truth(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4419 return TEST_SUCCESS
;
4422 /* Make sure that I cant add a udp server to a tcp client */
4423 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
4425 memcached_server_st server
;
4426 memcached_server_clone(&server
, &memc
->hosts
[0]);
4427 test_truth(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
4429 memcached_st tcp_client
;
4430 memcached_create(&tcp_client
);
4431 test_truth(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
4433 return TEST_SUCCESS
;
4436 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
4439 memcached_quit(memc
);
4440 memc
->number_of_hosts
= 0;
4441 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
);
4442 test_truth(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
4443 test_truth(memc
->flags
.use_udp
);
4444 test_truth(memc
->flags
.no_reply
);
4446 test_truth(memc
->number_of_hosts
== 0);
4448 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
4449 test_truth(! (memc
->flags
.use_udp
));
4450 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
4451 test_truth(! (memc
->flags
.no_reply
));
4453 return TEST_SUCCESS
;
4456 static test_return_t
udp_set_test(memcached_st
*memc
)
4459 unsigned int num_iters
= 1025; //request id rolls over at 1024
4460 for (x
= 0; x
< num_iters
;x
++)
4462 memcached_return_t rc
;
4463 const char *key
= "foo";
4464 const char *value
= "when we sanitize";
4465 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4466 unsigned int server_key
= memcached_generate_hash(memc
,key
,strlen(key
));
4467 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4468 rc
= memcached_set(memc
, key
, strlen(key
),
4469 value
, strlen(value
),
4470 (time_t)0, (uint32_t)0);
4471 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4472 /** NB, the check below assumes that if new write_ptr is less than
4473 * the original write_ptr that we have flushed. For large payloads, this
4474 * maybe an invalid assumption, but for the small payload we have it is OK
4476 if (rc
== MEMCACHED_SUCCESS
||
4477 memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4478 increment_request_id(&expected_ids
[server_key
]);
4480 if (rc
== MEMCACHED_SUCCESS
)
4482 test_truth(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4486 test_truth(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4487 test_truth(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4489 test_truth(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4491 return TEST_SUCCESS
;
4494 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
4496 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4497 return udp_set_test(memc
);
4500 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
4502 memcached_return_t rc
;
4503 const char *key
= "bar";
4504 char value
[MAX_UDP_DATAGRAM_LENGTH
];
4505 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4506 rc
= memcached_set(memc
, key
, strlen(key
),
4507 value
, MAX_UDP_DATAGRAM_LENGTH
,
4508 (time_t)0, (uint32_t)0);
4509 test_truth(rc
== MEMCACHED_WRITE_FAILURE
);
4510 return post_udp_op_check(memc
,expected_ids
);
4513 static test_return_t
udp_delete_test(memcached_st
*memc
)
4516 unsigned int num_iters
= 1025; //request id rolls over at 1024
4517 for (x
= 0; x
< num_iters
;x
++)
4519 memcached_return_t rc
;
4520 const char *key
= "foo";
4521 uint16_t *expected_ids
=get_udp_request_ids(memc
);
4522 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4523 size_t init_offset
= memc
->hosts
[server_key
].write_buffer_offset
;
4524 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
4525 test_truth(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
4526 if (rc
== MEMCACHED_SUCCESS
|| memc
->hosts
[server_key
].write_buffer_offset
< init_offset
)
4527 increment_request_id(&expected_ids
[server_key
]);
4528 if (rc
== MEMCACHED_SUCCESS
)
4530 test_truth(memc
->hosts
[server_key
].write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
4534 test_truth(memc
->hosts
[server_key
].write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
4535 test_truth(memc
->hosts
[server_key
].write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
4537 test_truth(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
4539 return TEST_SUCCESS
;
4542 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
4544 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4545 return udp_delete_test(memc
);
4548 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
4550 memcached_return_t rc
;
4551 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4553 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4554 increment_request_id(&expected_ids
[x
]);
4556 rc
= memcached_verbosity(memc
,3);
4557 test_truth(rc
== MEMCACHED_SUCCESS
);
4558 return post_udp_op_check(memc
,expected_ids
);
4561 static test_return_t
udp_quit_test(memcached_st
*memc
)
4563 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4564 memcached_quit(memc
);
4565 return post_udp_op_check(memc
, expected_ids
);
4568 static test_return_t
udp_flush_test(memcached_st
*memc
)
4570 memcached_return_t rc
;
4571 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4573 for (x
= 0; x
< memc
->number_of_hosts
;x
++)
4574 increment_request_id(&expected_ids
[x
]);
4576 rc
= memcached_flush(memc
,0);
4577 test_truth(rc
== MEMCACHED_SUCCESS
);
4578 return post_udp_op_check(memc
,expected_ids
);
4581 static test_return_t
udp_incr_test(memcached_st
*memc
)
4583 memcached_return_t rc
;
4584 const char *key
= "incr";
4585 const char *value
= "1";
4586 rc
= memcached_set(memc
, key
, strlen(key
),
4587 value
, strlen(value
),
4588 (time_t)0, (uint32_t)0);
4590 test_truth(rc
== MEMCACHED_SUCCESS
);
4591 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4592 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4593 increment_request_id(&expected_ids
[server_key
]);
4595 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
4596 test_truth(rc
== MEMCACHED_SUCCESS
);
4597 return post_udp_op_check(memc
, expected_ids
);
4600 static test_return_t
udp_decr_test(memcached_st
*memc
)
4602 memcached_return_t rc
;
4603 const char *key
= "decr";
4604 const char *value
= "1";
4605 rc
= memcached_set(memc
, key
, strlen(key
),
4606 value
, strlen(value
),
4607 (time_t)0, (uint32_t)0);
4609 test_truth(rc
== MEMCACHED_SUCCESS
);
4610 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4611 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
4612 increment_request_id(&expected_ids
[server_key
]);
4614 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
4615 test_truth(rc
== MEMCACHED_SUCCESS
);
4616 return post_udp_op_check(memc
, expected_ids
);
4620 static test_return_t
udp_stat_test(memcached_st
*memc
)
4622 memcached_stat_st
* rv
= NULL
;
4623 memcached_return_t rc
;
4625 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4626 rv
= memcached_stat(memc
, args
, &rc
);
4628 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
4629 return post_udp_op_check(memc
, expected_ids
);
4632 static test_return_t
udp_version_test(memcached_st
*memc
)
4634 memcached_return_t rc
;
4635 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4636 rc
= memcached_version(memc
);
4637 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
4638 return post_udp_op_check(memc
, expected_ids
);
4641 static test_return_t
udp_get_test(memcached_st
*memc
)
4643 memcached_return_t rc
;
4644 const char *key
= "foo";
4646 uint16_t *expected_ids
= get_udp_request_ids(memc
);
4647 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
4648 test_truth(rc
== MEMCACHED_NOT_SUPPORTED
);
4649 test_truth(val
== NULL
);
4650 return post_udp_op_check(memc
, expected_ids
);
4653 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
4656 test_st mixed_io_ops
[] ={
4658 (test_callback_fn
)udp_set_test
},
4659 {"udp_set_too_big_test", 0,
4660 (test_callback_fn
)udp_set_too_big_test
},
4661 {"udp_delete_test", 0,
4662 (test_callback_fn
)udp_delete_test
},
4663 {"udp_verbosity_test", 0,
4664 (test_callback_fn
)udp_verbosity_test
},
4665 {"udp_quit_test", 0,
4666 (test_callback_fn
)udp_quit_test
},
4667 {"udp_flush_test", 0,
4668 (test_callback_fn
)udp_flush_test
},
4669 {"udp_incr_test", 0,
4670 (test_callback_fn
)udp_incr_test
},
4671 {"udp_decr_test", 0,
4672 (test_callback_fn
)udp_decr_test
},
4673 {"udp_version_test", 0,
4674 (test_callback_fn
)udp_version_test
}
4677 for (x
= 0; x
< 500; x
++)
4679 current_op
= mixed_io_ops
[random() % 9];
4680 test_truth(current_op
.test_fn(memc
) == TEST_SUCCESS
);
4682 return TEST_SUCCESS
;
4686 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4690 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4691 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4692 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4693 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4694 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4695 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4696 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4697 #ifdef HAVE_HSIEH_HASH
4698 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4700 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4701 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4702 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4704 return TEST_SUCCESS
;
4708 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4710 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4711 #ifdef HAVE_HSIEH_HASH
4712 expected_rc
= MEMCACHED_SUCCESS
;
4714 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4715 (uint64_t)MEMCACHED_HASH_HSIEH
);
4716 test_truth(rc
== expected_rc
);
4717 return TEST_SUCCESS
;
4720 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4725 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4729 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4730 test_truth(md5_values
[x
] == hash_val
);
4733 return TEST_SUCCESS
;
4736 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4741 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4745 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4746 test_truth(crc_values
[x
] == hash_val
);
4749 return TEST_SUCCESS
;
4752 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4757 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4761 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4762 test_truth(fnv1_64_values
[x
] == hash_val
);
4765 return TEST_SUCCESS
;
4768 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4773 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4777 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4778 test_truth(fnv1a_64_values
[x
] == hash_val
);
4781 return TEST_SUCCESS
;
4784 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4790 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4794 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4795 test_truth(fnv1_32_values
[x
] == hash_val
);
4798 return TEST_SUCCESS
;
4801 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4806 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4810 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4811 test_truth(fnv1a_32_values
[x
] == hash_val
);
4814 return TEST_SUCCESS
;
4817 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4822 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4826 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4827 test_truth(hsieh_values
[x
] == hash_val
);
4830 return TEST_SUCCESS
;
4833 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4836 return TEST_SKIPPED
;
4841 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4845 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4846 test_truth(murmur_values
[x
] == hash_val
);
4849 return TEST_SUCCESS
;
4853 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4859 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4863 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4864 test_truth(jenkins_values
[x
] == hash_val
);
4867 return TEST_SUCCESS
;
4871 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4873 memcached_return_t rc
;
4876 memcached_server_st
*server_pool
;
4881 memc
= memcached_create(NULL
);
4884 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4885 test_truth(rc
== MEMCACHED_SUCCESS
);
4887 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4888 test_truth(value
== 1);
4890 test_truth(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4891 test_truth(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4894 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");
4895 memcached_server_push(memc
, server_pool
);
4897 /* verify that the server list was parsed okay. */
4898 assert(memc
->number_of_hosts
== 8);
4899 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
4900 assert(server_pool
[0].port
== 11211);
4901 assert(server_pool
[0].weight
== 600);
4902 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
4903 assert(server_pool
[2].port
== 11211);
4904 assert(server_pool
[2].weight
== 200);
4905 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
4906 assert(server_pool
[7].port
== 11211);
4907 assert(server_pool
[7].weight
== 100);
4909 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4910 * us test the boundary wraparound.
4912 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4914 /* verify the standard ketama set. */
4915 for (x
= 0; x
< 99; x
++)
4917 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4918 char *hostname
= memc
->hosts
[server_idx
].hostname
;
4919 assert(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
4922 memcached_server_list_free(server_pool
);
4923 memcached_free(memc
);
4925 return TEST_SUCCESS
;
4928 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4930 memcached_return_t rc
;
4933 memcached_server_st
*server_pool
;
4938 memc
= memcached_create(NULL
);
4941 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4942 test_truth(rc
== MEMCACHED_SUCCESS
);
4944 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4945 test_truth(value
== 1);
4947 test_truth(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4948 test_truth(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4950 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");
4951 memcached_server_push(memc
, server_pool
);
4953 /* verify that the server list was parsed okay. */
4954 assert(memc
->number_of_hosts
== 8);
4955 assert(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
4956 assert(server_pool
[0].port
== 11211);
4957 assert(server_pool
[0].weight
== 600);
4958 assert(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
4959 assert(server_pool
[2].port
== 11211);
4960 assert(server_pool
[2].weight
== 200);
4961 assert(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
4962 assert(server_pool
[7].port
== 11211);
4963 assert(server_pool
[7].weight
== 100);
4965 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4966 * us test the boundary wraparound.
4968 assert(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
4970 /* verify the standard ketama set. */
4971 for (x
= 0; x
< 99; x
++)
4973 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4974 char *hostname
= memc
->hosts
[server_idx
].hostname
;
4975 assert(strcmp(hostname
, ketama_test_cases_spy
[x
].server
) == 0);
4978 memcached_server_list_free(server_pool
);
4979 memcached_free(memc
);
4981 return TEST_SUCCESS
;
4984 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4986 test_return_t test_rc
;
4987 test_rc
= pre_binary(memc
);
4989 if (test_rc
!= TEST_SUCCESS
)
4992 memcached_return_t ret
;
4993 const char *key
= "regression_bug_434484";
4994 size_t keylen
= strlen(key
);
4996 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4997 assert(ret
== MEMCACHED_NOTSTORED
);
4999 size_t size
= 2048 * 1024;
5000 void *data
= calloc(1, size
);
5001 assert(data
!= NULL
);
5002 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5003 assert(ret
== MEMCACHED_E2BIG
);
5006 return TEST_SUCCESS
;
5009 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5011 test_return_t test_rc
;
5012 test_rc
= pre_binary(memc
);
5014 if (test_rc
!= TEST_SUCCESS
)
5017 memcached_return_t rc
;
5018 unsigned int counter
= 0;
5019 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5022 * I only want to hit only _one_ server so I know the number of requests I'm
5023 * sending in the pipleine to the server. Let's try to do a multiget of
5024 * 1024 (that should satisfy most users don't you think?). Future versions
5025 * will include a mget_execute function call if you need a higher number.
5027 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5028 memc
->number_of_hosts
= 1;
5029 const size_t max_keys
= 1024;
5030 char **keys
= calloc(max_keys
, sizeof(char*));
5031 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5033 for (int x
= 0; x
< (int)max_keys
; ++x
)
5036 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
5038 assert(keys
[x
] != NULL
);
5042 * Run two times.. the first time we should have 100% cache miss,
5043 * and the second time we should have 100% cache hits
5045 for (int y
= 0; y
< 2; ++y
)
5047 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5048 assert(rc
== MEMCACHED_SUCCESS
);
5049 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5052 /* The first iteration should give me a 100% cache miss. verify that*/
5053 assert(counter
== 0);
5054 char blob
[1024]= { 0 };
5055 for (int x
= 0; x
< (int)max_keys
; ++x
)
5057 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5058 blob
, sizeof(blob
), 0, 0);
5059 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5064 /* Verify that we received all of the key/value pairs */
5065 assert(counter
== (unsigned int)max_keys
);
5069 /* Release allocated resources */
5070 for (size_t x
= 0; x
< max_keys
; ++x
)
5075 memc
->number_of_hosts
= number_of_hosts
;
5076 return TEST_SUCCESS
;
5079 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5081 memcached_return_t rc
;
5082 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5083 assert(rc
== MEMCACHED_SUCCESS
);
5085 return regression_bug_434843(memc
);
5088 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5090 memcached_return_t rc
;
5091 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5092 assert(rc
== MEMCACHED_SUCCESS
);
5094 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5095 assert(rc
== MEMCACHED_SUCCESS
);
5096 assert(bytes
!= NULL
);
5097 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5099 assert(rc
== MEMCACHED_SUCCESS
);
5100 assert(bytes_read
!= NULL
);
5102 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5103 "bytes_written", &rc
);
5104 assert(rc
== MEMCACHED_SUCCESS
);
5105 assert(bytes_written
!= NULL
);
5107 assert(strcmp(bytes
, bytes_read
) != 0);
5108 assert(strcmp(bytes
, bytes_written
) != 0);
5110 /* Release allocated resources */
5113 free(bytes_written
);
5114 memcached_stat_free(NULL
, memc_stat
);
5115 return TEST_SUCCESS
;
5119 * The test case isn't obvious so I should probably document why
5120 * it works the way it does. Bug 442914 was caused by a bug
5121 * in the logic in memcached_purge (it did not handle the case
5122 * where the number of bytes sent was equal to the watermark).
5123 * In this test case, create messages so that we hit that case
5124 * and then disable noreply mode and issue a new command to
5125 * verify that it isn't stuck. If we change the format for the
5126 * delete command or the watermarks, we need to update this
5129 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5131 memcached_return_t rc
;
5132 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5133 assert(rc
== MEMCACHED_SUCCESS
);
5134 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5136 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5137 memc
->number_of_hosts
= 1;
5142 for (int x
= 0; x
< 250; ++x
)
5144 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5145 rc
= memcached_delete(memc
, k
, len
, 0);
5146 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5149 len
= (size_t)snprintf(k
, sizeof(k
), "%037u", 251);
5150 rc
= memcached_delete(memc
, k
, len
, 0);
5151 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5153 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5154 assert(rc
== MEMCACHED_SUCCESS
);
5155 rc
= memcached_delete(memc
, k
, len
, 0);
5156 assert(rc
== MEMCACHED_NOTFOUND
);
5158 memc
->number_of_hosts
= number_of_hosts
;
5160 return TEST_SUCCESS
;
5163 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5165 if (memc
->number_of_hosts
< 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5166 return TEST_SKIPPED
;
5168 memcached_return_t rc
;
5170 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5171 assert(rc
== MEMCACHED_SUCCESS
);
5173 const size_t max_keys
= 100;
5174 char **keys
= calloc(max_keys
, sizeof(char*));
5175 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5177 for (int x
= 0; x
< (int)max_keys
; ++x
)
5180 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%u", x
);
5182 assert(keys
[x
] != NULL
);
5183 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5184 assert(rc
== MEMCACHED_SUCCESS
);
5188 ** We are using the quiet commands to store the replicas, so we need
5189 ** to ensure that all of them are processed before we can continue.
5190 ** In the test we go directly from storing the object to trying to
5191 ** receive the object from all of the different servers, so we
5192 ** could end up in a race condition (the memcached server hasn't yet
5193 ** processed the quiet command from the replication set when it process
5194 ** the request from the other client (created by the clone)). As a
5195 ** workaround for that we call memcached_quit to send the quit command
5196 ** to the server and wait for the response ;-) If you use the test code
5197 ** as an example for your own code, please note that you shouldn't need
5200 memcached_quit(memc
);
5202 /* Verify that all messages are stored, and we didn't stuff too much
5205 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5206 assert(rc
== MEMCACHED_SUCCESS
);
5208 unsigned int counter
= 0;
5209 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5210 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5211 /* Verify that we received all of the key/value pairs */
5212 assert(counter
== (unsigned int)max_keys
);
5214 memcached_quit(memc
);
5216 * Don't do the following in your code. I am abusing the internal details
5217 * within the library, and this is not a supported interface.
5218 * This is to verify correct behavior in the library. Fake that two servers
5221 unsigned int port0
= memc
->hosts
[0].port
;
5222 unsigned int port2
= memc
->hosts
[2].port
;
5223 memc
->hosts
[0].port
= 0;
5224 memc
->hosts
[2].port
= 0;
5226 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5227 assert(rc
== MEMCACHED_SUCCESS
);
5230 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5231 assert(counter
== (unsigned int)max_keys
);
5233 /* restore the memc handle */
5234 memc
->hosts
[0].port
= port0
;
5235 memc
->hosts
[2].port
= port2
;
5237 memcached_quit(memc
);
5239 /* Remove half of the objects */
5240 for (int x
= 0; x
< (int)max_keys
; ++x
)
5243 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5244 assert(rc
== MEMCACHED_SUCCESS
);
5247 memcached_quit(memc
);
5248 memc
->hosts
[0].port
= 0;
5249 memc
->hosts
[2].port
= 0;
5251 /* now retry the command, this time we should have cache misses */
5252 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5253 assert(rc
== MEMCACHED_SUCCESS
);
5256 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5257 assert(counter
== (unsigned int)(max_keys
>> 1));
5259 /* Release allocated resources */
5260 for (size_t x
= 0; x
< max_keys
; ++x
)
5265 /* restore the memc handle */
5266 memc
->hosts
[0].port
= port0
;
5267 memc
->hosts
[2].port
= port2
;
5268 return TEST_SUCCESS
;
5271 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5273 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5274 assert(memc_clone
!= NULL
);
5275 assert(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5277 if (memc_clone
->hosts
[0].major_version
> 1 ||
5278 (memc_clone
->hosts
[0].major_version
== 1 &&
5279 memc_clone
->hosts
[0].minor_version
> 2))
5281 /* Binary protocol doesn't support deferred delete */
5282 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5283 assert(bin_clone
!= NULL
);
5284 assert(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5285 assert(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5286 memcached_free(bin_clone
);
5288 memcached_quit(memc_clone
);
5290 /* If we know the server version, deferred delete should fail
5291 * with invalid arguments */
5292 assert(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5294 /* If we don't know the server version, we should get a protocol error */
5295 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5296 /* but there is a bug in some of the memcached servers (1.4) that treats
5297 * the counter as noreply so it doesn't send the proper error message
5299 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5301 /* And buffered mode should be disabled and we should get protocol error */
5302 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5303 rc
= memcached_delete(memc
, "foo", 3, 1);
5304 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5306 /* Same goes for noreply... */
5307 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5308 rc
= memcached_delete(memc
, "foo", 3, 1);
5309 assert(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5311 /* but a normal request should go through (and be buffered) */
5312 assert((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5313 assert(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5315 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5316 /* unbuffered noreply should be success */
5317 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5318 /* unbuffered with reply should be not found... */
5319 assert(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5320 assert(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5323 memcached_free(memc_clone
);
5324 return TEST_SUCCESS
;
5328 /* Test memcached_server_get_last_disconnect
5329 * For a working server set, shall be NULL
5330 * For a set of non existing server, shall not be NULL
5332 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5334 memcached_return_t rc
;
5335 memcached_server_st
*disconnected_server
;
5337 /* With the working set of server */
5338 const char *key
= "marmotte";
5339 const char *value
= "milka";
5341 rc
= memcached_set(memc
, key
, strlen(key
),
5342 value
, strlen(value
),
5343 (time_t)0, (uint32_t)0);
5344 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5346 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5347 assert(disconnected_server
== NULL
);
5349 /* With a non existing server */
5351 memcached_server_st
*servers
;
5353 const char *server_list
= "localhost:9";
5355 servers
= memcached_servers_parse(server_list
);
5357 mine
= memcached_create(NULL
);
5358 rc
= memcached_server_push(mine
, servers
);
5359 assert(rc
== MEMCACHED_SUCCESS
);
5360 memcached_server_list_free(servers
);
5363 rc
= memcached_set(mine
, key
, strlen(key
),
5364 value
, strlen(value
),
5365 (time_t)0, (uint32_t)0);
5366 assert(rc
!= MEMCACHED_SUCCESS
);
5368 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5369 assert(disconnected_server
!= NULL
);
5370 assert(disconnected_server
->port
== 9);
5371 assert(strncmp(disconnected_server
->hostname
,"localhost",9) == 0);
5373 memcached_quit(mine
);
5374 memcached_free(mine
);
5376 return TEST_SUCCESS
;
5380 * This test ensures that the failure counter isn't incremented during
5381 * normal termination of the memcached instance.
5383 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5385 memcached_return_t rc
;
5387 /* Set value to force connection to the server */
5388 const char *key
= "marmotte";
5389 const char *value
= "milka";
5392 * Please note that I'm abusing the internal structures in libmemcached
5393 * in a non-portable way and you shouldn't be doing this. I'm only
5394 * doing this in order to verify that the library works the way it should
5396 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5397 memc
->number_of_hosts
= 1;
5399 /* Ensure that we are connected to the server by setting a value */
5400 rc
= memcached_set(memc
, key
, strlen(key
),
5401 value
, strlen(value
),
5402 (time_t)0, (uint32_t)0);
5403 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5406 /* The test is to see that the memcached_quit doesn't increase the
5407 * the server failure conter, so let's ensure that it is zero
5408 * before sending quit
5410 memc
->hosts
[0].server_failure_counter
= 0;
5412 memcached_quit(memc
);
5414 /* Verify that it memcached_quit didn't increment the failure counter
5415 * Please note that this isn't bullet proof, because an error could
5418 assert(memc
->hosts
[0].server_failure_counter
== 0);
5420 /* restore the instance */
5421 memc
->number_of_hosts
= number_of_hosts
;
5423 return TEST_SUCCESS
;
5426 test_st udp_setup_server_tests
[] ={
5427 {"set_udp_behavior_test", 0, (test_callback_fn
)set_udp_behavior_test
},
5428 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
)add_tcp_server_udp_client_test
},
5429 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
)add_udp_server_tcp_client_test
},
5433 test_st upd_io_tests
[] ={
5434 {"udp_set_test", 0, (test_callback_fn
)udp_set_test
},
5435 {"udp_buffered_set_test", 0, (test_callback_fn
)udp_buffered_set_test
},
5436 {"udp_set_too_big_test", 0, (test_callback_fn
)udp_set_too_big_test
},
5437 {"udp_delete_test", 0, (test_callback_fn
)udp_delete_test
},
5438 {"udp_buffered_delete_test", 0, (test_callback_fn
)udp_buffered_delete_test
},
5439 {"udp_verbosity_test", 0, (test_callback_fn
)udp_verbosity_test
},
5440 {"udp_quit_test", 0, (test_callback_fn
)udp_quit_test
},
5441 {"udp_flush_test", 0, (test_callback_fn
)udp_flush_test
},
5442 {"udp_incr_test", 0, (test_callback_fn
)udp_incr_test
},
5443 {"udp_decr_test", 0, (test_callback_fn
)udp_decr_test
},
5444 {"udp_stat_test", 0, (test_callback_fn
)udp_stat_test
},
5445 {"udp_version_test", 0, (test_callback_fn
)udp_version_test
},
5446 {"udp_get_test", 0, (test_callback_fn
)udp_get_test
},
5447 {"udp_mixed_io_test", 0, (test_callback_fn
)udp_mixed_io_test
},
5451 /* Clean the server before beginning testing */
5453 {"flush", 0, (test_callback_fn
)flush_test
},
5454 {"init", 0, (test_callback_fn
)init_test
},
5455 {"allocation", 0, (test_callback_fn
)allocation_test
},
5456 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5457 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5458 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5459 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5460 {"clone_test", 0, (test_callback_fn
)clone_test
},
5461 {"connection_test", 0, (test_callback_fn
)connection_test
},
5462 {"callback_test", 0, (test_callback_fn
)callback_test
},
5463 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
5464 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5465 {"error", 0, (test_callback_fn
)error_test
},
5466 {"set", 0, (test_callback_fn
)set_test
},
5467 {"set2", 0, (test_callback_fn
)set_test2
},
5468 {"set3", 0, (test_callback_fn
)set_test3
},
5469 {"dump", 1, (test_callback_fn
)dump_test
},
5470 {"add", 1, (test_callback_fn
)add_test
},
5471 {"replace", 1, (test_callback_fn
)replace_test
},
5472 {"delete", 1, (test_callback_fn
)delete_test
},
5473 {"get", 1, (test_callback_fn
)get_test
},
5474 {"get2", 0, (test_callback_fn
)get_test2
},
5475 {"get3", 0, (test_callback_fn
)get_test3
},
5476 {"get4", 0, (test_callback_fn
)get_test4
},
5477 {"partial mget", 0, (test_callback_fn
)get_test5
},
5478 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5479 {"increment", 0, (test_callback_fn
)increment_test
},
5480 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5481 {"decrement", 0, (test_callback_fn
)decrement_test
},
5482 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5483 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5484 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5485 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5486 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5487 {"quit", 0, (test_callback_fn
)quit_test
},
5488 {"mget", 1, (test_callback_fn
)mget_test
},
5489 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5490 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5491 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5492 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5493 {"mget_end", 0, (test_callback_fn
)mget_end
},
5494 {"get_stats", 0, (test_callback_fn
)get_stats
},
5495 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5496 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5497 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5498 {"behavior_test", 0, (test_callback_fn
)get_stats_keys
},
5499 {"callback_test", 0, (test_callback_fn
)get_stats_keys
},
5500 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5501 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
5502 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
5503 {"read_through", 1, (test_callback_fn
)read_through
},
5504 {"delete_through", 1, (test_callback_fn
)delete_through
},
5505 {"noreply", 1, (test_callback_fn
)noreply_test
},
5506 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
5507 #ifdef HAVE_LIBMEMCACHEDUTIL
5508 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
5510 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
5514 test_st async_tests
[] ={
5515 {"add", 1, (test_callback_fn
)add_wrapper
},
5519 test_st string_tests
[] ={
5520 {"string static with null", 0, (test_callback_fn
)string_static_null
},
5521 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
5522 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
5523 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
5524 {"string append", 0, (test_callback_fn
)string_alloc_append
},
5525 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
5526 {0, 0, (test_callback_fn
)0}
5529 test_st result_tests
[] ={
5530 {"result static", 0, (test_callback_fn
)result_static
},
5531 {"result alloc", 0, (test_callback_fn
)result_alloc
},
5532 {0, 0, (test_callback_fn
)0}
5535 test_st version_1_2_3
[] ={
5536 {"append", 0, (test_callback_fn
)append_test
},
5537 {"prepend", 0, (test_callback_fn
)prepend_test
},
5538 {"cas", 0, (test_callback_fn
)cas_test
},
5539 {"cas2", 0, (test_callback_fn
)cas2_test
},
5540 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
5541 {0, 0, (test_callback_fn
)0}
5544 test_st user_tests
[] ={
5545 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
5546 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
5547 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
5548 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
5549 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
5550 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
5551 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
5552 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
5553 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
5554 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
5555 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
5556 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
5557 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
5558 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
5559 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
5560 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
5563 ** It seems to be something weird with the character sets..
5564 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
5565 ** guess I need to find out how this is supposed to work.. Perhaps I need
5566 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
5567 ** so just disable the code for now...).
5569 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
5571 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
5572 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
5573 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
5574 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
5575 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
5576 {0, 0, (test_callback_fn
)0}
5579 test_st replication_tests
[]= {
5580 {"set", 1, (test_callback_fn
)replication_set_test
},
5581 {"get", 0, (test_callback_fn
)replication_get_test
},
5582 {"mget", 0, (test_callback_fn
)replication_mget_test
},
5583 {"delete", 0, (test_callback_fn
)replication_delete_test
},
5584 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
5585 {0, 0, (test_callback_fn
)0}
5589 * The following test suite is used to verify that we don't introduce
5590 * regression bugs. If you want more information about the bug / test,
5591 * you should look in the bug report at
5592 * http://bugs.launchpad.net/libmemcached
5594 test_st regression_tests
[]= {
5595 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
5596 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
5597 {"lp:434843 buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
5598 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
5599 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
5600 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
5601 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
5602 {0, 0, (test_callback_fn
)0}
5605 test_st ketama_compatibility
[]= {
5606 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
5607 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
5608 {0, 0, (test_callback_fn
)0}
5611 test_st generate_tests
[] ={
5612 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5613 {"generate_data", 1, (test_callback_fn
)generate_data
},
5614 {"get_read", 0, (test_callback_fn
)get_read
},
5615 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
5616 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
5617 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
5618 {"generate_data", 1, (test_callback_fn
)generate_data
},
5619 {"mget_read", 0, (test_callback_fn
)mget_read
},
5620 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
5621 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
5622 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5623 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
5624 {"generate_data", 1, (test_callback_fn
)generate_data
},
5625 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
5626 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5627 {0, 0, (test_callback_fn
)0}
5630 test_st consistent_tests
[] ={
5631 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5632 {"generate_data", 1, (test_callback_fn
)generate_data
},
5633 {"get_read", 0, (test_callback_fn
)get_read_count
},
5634 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5635 {0, 0, (test_callback_fn
)0}
5638 test_st consistent_weighted_tests
[] ={
5639 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
5640 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
5641 {"get_read", 0, (test_callback_fn
)get_read_count
},
5642 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
5643 {0, 0, (test_callback_fn
)0}
5646 test_st hsieh_availability
[] ={
5647 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
5648 {0, 0, (test_callback_fn
)0}
5652 test_st hash_sanity
[] ={
5653 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
5654 {0, 0, (test_callback_fn
)0}
5658 test_st ketama_auto_eject_hosts
[] ={
5659 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
5660 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
5661 {0, 0, (test_callback_fn
)0}
5664 test_st hash_tests
[] ={
5665 {"md5", 0, (test_callback_fn
)md5_run
},
5666 {"crc", 0, (test_callback_fn
)crc_run
},
5667 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
5668 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
5669 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
5670 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
5671 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
5672 {"murmur", 0, (test_callback_fn
)murmur_run
},
5673 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
5674 {0, 0, (test_callback_fn
)0}
5677 collection_st collection
[] ={
5679 {"hash_sanity", 0, 0, hash_sanity
},
5681 {"hsieh_availability", 0, 0, hsieh_availability
},
5682 {"udp_setup", (test_callback_fn
)init_udp
, 0, udp_setup_server_tests
},
5683 {"udp_io", (test_callback_fn
)init_udp
, 0, upd_io_tests
},
5684 {"udp_binary_io", (test_callback_fn
)binary_init_udp
, 0, upd_io_tests
},
5685 {"block", 0, 0, tests
},
5686 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
5687 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
5688 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
5689 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
5690 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
5691 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
5692 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
5693 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
5694 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
5695 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
5696 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
5697 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
5698 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
5699 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
5700 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
5701 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
5702 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
5703 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
5704 {"consistent", (test_callback_fn
)enable_consistent
, 0, tests
},
5705 #ifdef MEMCACHED_ENABLE_DEPRECATED
5706 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
5708 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
5709 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
5710 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
5711 {"string", 0, 0, string_tests
},
5712 {"result", 0, 0, result_tests
},
5713 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
5714 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
5715 {"user", 0, 0, user_tests
},
5716 {"generate", 0, 0, generate_tests
},
5717 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
5718 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
5719 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent
, 0, generate_tests
},
5720 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
5721 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
5722 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
5723 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
5724 {"consistent_not", 0, 0, consistent_tests
},
5725 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
5726 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
5727 {"ketama_compat", 0, 0, ketama_compatibility
},
5728 {"test_hashes", 0, 0, hash_tests
},
5729 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
5730 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
5731 {"regression", 0, 0, regression_tests
},
5735 #define SERVERS_TO_CREATE 5
5737 #include "libmemcached_world.h"
5739 void get_world(world_st
*world
)
5741 world
->collections
= collection
;
5742 world
->collection_startup
= (test_callback_fn
)world_collection_startup
;
5743 world
->flush
= (test_callback_fn
)world_flush
;
5744 world
->pre_run
= (test_callback_fn
)world_pre_run
;
5745 world
->create
= (test_callback_create_fn
)world_create
;
5746 world
->post_run
= (test_callback_fn
)world_post_run
;
5747 world
->on_error
= (test_callback_error_fn
)world_on_error
;
5748 world
->destroy
= (test_callback_fn
)world_destroy
;
5749 world
->runner
= &defualt_libmemcached_runner
;